1 /*
2 
3 This program prints the "idle time" of the user to stdout.  The "idle
4 time" is the number of milliseconds since input was received on any
5 input device.  If unsuccessful, the program prints a message to stderr
6 and exits with a non-zero exit code.
7 
8 Copyright (c) 2005, 2008 Magnus Henoch <henoch@dtek.chalmers.se>
9 Copyright (c) 2006, 2007 by Danny Kukawka
10                          <dkukawka@suse.de>, <danny.kukawka@web.de>
11 Copyright (c) 2008 Eivind Magnus Hvidevold <hvidevold@gmail.com>
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of version 2 of the GNU General Public License
15 as published by the Free Software Foundation.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the
24 Free Software Foundation, Inc.,
25 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 
27 */
28 
29 #include <X11/Xlib.h>
30 #include <X11/extensions/dpms.h>
31 #include <X11/extensions/scrnsaver.h>
32 #include <stdio.h>
33 
34 void usage(char *name);
35 
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38   XScreenSaverInfo *ssi;
39   Display *dpy;
40   int event_basep, error_basep;
41 
42   if (argc != 1) {
43     usage(argv[0]);
44     return 1;
45   }
46 
47   dpy = XOpenDisplay(NULL);
48   if (dpy == NULL) {
49     fprintf(stderr, "couldn't open display\n");
50     return 1;
51   }
52 
53   if (!XScreenSaverQueryExtension(dpy, &event_basep, &error_basep)) {
54     fprintf(stderr, "screen saver extension not supported\n");
55     return 1;
56   }
57 
58   ssi = XScreenSaverAllocInfo();
59   if (ssi == NULL) {
60     fprintf(stderr, "couldn't allocate screen saver info\n");
61     return 1;
62   }
63 
64   if (!XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), ssi)) {
65     fprintf(stderr, "couldn't query screen saver info\n");
66     return 1;
67   }
68 
69   printf("%lu\n", ssi->idle);
70 
71   XFree(ssi);
72   XCloseDisplay(dpy);
73   return 0;
74 }
75 
usage(char * name)76 void usage(char *name)
77 {
78   fprintf(stderr,
79 	  "Usage:\n"
80 	  "%s\n"
81 	  "That is, no command line arguments.  The user's idle time\n"
82 	  "in milliseconds is printed on stdout.\n",
83 	  name);
84 }
85