-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxidlerun.c
78 lines (64 loc) · 1.69 KB
/
xidlerun.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
float getIdleTime()
{
Display *display;
int event_base, error_base;
XScreenSaverInfo info;
display = XOpenDisplay("");
if (XScreenSaverQueryExtension(display, &event_base, &error_base)) {
XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);
return (float)info.idle/1000.0f;
}
else {
fprintf(stderr,"Error: XScreenSaver Extension not present\n");
return -1;
}
}
int main(int argc, char *argv[])
{
unsigned int secondsToReset = 60;
char *command = NULL;
bool didReset = false;
float idleTime = 0.0;
// read options
int c;
int targ;
while ((c = getopt (argc, argv, "t:c:")) != -1)
switch (c)
{
case 't':
targ = atoi(optarg);
if (targ > 0) secondsToReset = targ;
break;
case 'c':
command = optarg;
break;
case '?':
return 1;
default:
abort ();
}
if (!command) {
fprintf(stderr, "option -c is required\n");
return 1;
}
// now observe idle time
while(idleTime >= 0) {
idleTime = getIdleTime();
if (idleTime > secondsToReset && !didReset) {
printf("Reached %f seconds of idle time. Executing command %s.\n", idleTime, command);
system(command);
didReset = true;
}
if (idleTime < secondsToReset && didReset) {
printf("User interaction detected, resetting time.\n");
didReset = false;
}
sleep(1);
}
}