-
Notifications
You must be signed in to change notification settings - Fork 1
/
autosleep.c
447 lines (353 loc) · 10.9 KB
/
autosleep.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
autosleep.c
See README about what it does.
See LICENSE for licensing details.
Per Weijnitz <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <X11/extensions/XInput.h>
#include <gdk/gdkx.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include "config.h"
// shared memory segment size
#define SEGSIZE 100
#define USAGE "\n\nUsage: seconds keyboard_id /full/path/to/command args..."
// mouse coordinates
typedef struct _coords {
int x;
int y;
} Coords;
// some macros
#define DIE(...) do{ fprintf(stderr, __VA_ARGS__); \
exit(EXIT_FAILURE); } while(0)
#define DIE_PERROR(...) do{ fprintf(stderr, __VA_ARGS__); perror(0); \
exit(EXIT_FAILURE); } while(0)
// prototypes
int get_mouse_pos(Display *display, Coords *coords);
int check_for_mouse_events(Display *display);
XDeviceInfo* find_device(Display *display, XID id);
int register_events(Display *dpy, XDeviceInfo *info);
int detect_events(XID keyboard_id);
int check_for_events_within_timelimit(int shmid,
unsigned int timelimit_secs,
XID keyboard_id,
Display *display);
void main_loop(int shmid, unsigned int timelimit_secs,
char **suspend_cmd, XID keyboard_id,
Display *display);
/*
* Function: get_mouse_pos
* -----------------------
* Retrieves the current mouse pointer position.
*
* Inspired by https://ubuntuforums.org/showthread.php?t=562087
* (user: cwcentral).
*
* display: the current display
* coords: a Coords struct in which to write the position.
*
* returns: coords, same as input.
*/
int get_mouse_pos(Display *display, Coords *coords) {
XEvent event;
/* get info about current pointer position */
(void)XQueryPointer(display, RootWindow(display, DefaultScreen(display)),
&event.xbutton.root, &event.xbutton.window,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
coords->x = event.xbutton.x;
coords->y = event.xbutton.y;
return 1;
}
/*
* Function: check_for_mouse_events
* --------------------------------
* A non-threadsafe way to keep track of mouse position movements.
*
* display: the current display
*
* returns: 1 if a movement has happened, otherwise 0.
*/
int check_for_mouse_events(Display *display) {
static Coords prev_coords;
Coords coords;
coords.x = 0;
coords.y = 0;
(void)get_mouse_pos(display, &coords);
if(coords.x != prev_coords.x || coords.y != prev_coords.y) {
prev_coords.x = coords.x;
prev_coords.y = coords.y;
return 1;
}
return 0;
}
/*
* Function: find_device
* ---------------------
* Find the XDevice matching the given id.
* The ID can be determined by the user by running "xinput -list".
*
* display: the current Display.
* id: the ID of the keyboard to monitor for activity.
*
* returns: the XDevice if found, otherwise NULL.
*/
XDeviceInfo* find_device(Display *display, XID id) {
XDeviceInfo *devices;
XDeviceInfo *found = NULL;
int loop;
int num_devices;
devices = XListInputDevices(display, &num_devices);
for(loop=0; loop<num_devices; loop++) {
if (devices[loop].use >= IsXExtensionDevice && devices[loop].id == id) {
found = &devices[loop];
break;
}
}
return found;
}
/*
* Function: register_events
* -------------------------
* Registers events we want to pick up.
*
* Acknowledgement:
* Frederic Lepied, France. <[email protected]>
* https://anonscm.debian.org/cgit/pkg-xorg/app/xinput.git/tree/src/test.c
*
* dpy: current display.
* info: the keyboard XDevice info.
*
* returns: the number of registered event types.
*/
int register_events(Display *dpy, XDeviceInfo *info)
{
int number = 0; /* number of events registered */
XEventClass event_list[7];
int i;
XDevice *device;
Window root_win;
unsigned long screen;
XInputClassInfo *ip;
screen = DefaultScreen(dpy);
root_win = RootWindow(dpy, screen);
device = XOpenDevice(dpy, info->id);
if (!device) {
DIE("unable to open device\n");
}
if (device->num_classes > 0) {
int typearg = 0;
for (ip = device->classes, i=0; i<info->num_classes; ip++, i++) {
switch (ip->input_class) {
case KeyClass:
DeviceKeyPress(device, typearg, event_list[number]); number++;
DeviceKeyRelease(device, typearg, event_list[number]); number++;
break;
case ButtonClass:
DeviceButtonPress(device, typearg, event_list[number]); number++;
DeviceButtonRelease(device, typearg, event_list[number]); number++;
break;
case ValuatorClass:
DeviceMotionNotify(device, typearg, event_list[number]); number++;
break;
default:
fprintf(stderr, "unknown class\n");
break;
}
}
if (XSelectExtensionEvent(dpy, root_win, event_list, number)) {
fprintf(stderr, "error selecting extended events\n");
return 0;
}
}
return number;
}
/*
* Function: detect_events
* -----------------------
* Blocks until a registered event happens, then returns its type.
*
* display: the current display.
*
* returns: the occured event's type.
*/
int detect_events(XID keyboard_id) {
XDeviceInfo *info;
XEvent Event;
Display *display = XOpenDisplay( NULL );
if( !display ){ return 1; }
info = find_device(display, keyboard_id);
if (!info) {
DIE("unable to find device '%d'\n", (int)keyboard_id);
}
if(! register_events(display, info)) {
DIE("no event registered...\n");
}
setvbuf(stdout, NULL, _IOLBF, 0);
XNextEvent(display, &Event);
return Event.type;
}
/*
* Function: check_for_events_within_timelimit
* -------------------------------------------
* Waits for at most timelimit_secs seconds waiting for an event.
* Its not very elegant, but we need to fight off zombie processes.
*
* shmid: a shared memory segment id.
* timelimit_secs: the number of seconds to wait.
* keyboard_monitor: the keyboard id to monitor.
*
* returns: 1 if an event has happened, otherwise 0.
*/
int check_for_events_within_timelimit(int shmid, unsigned int timelimit_secs,
XID keyboard_id, Display *display) {
char *segptr = NULL;
pid_t pid = fork();
if (pid == -1) {
perror(0);
exit(EXIT_FAILURE);
}
if((segptr = shmat(shmid, 0, 0)) == (void *)-1) {
DIE_PERROR("shmat");
}
sprintf(segptr, "%s", "");
int updated = 0;
if (pid) {
// parent
signal(SIGCHLD, SIG_IGN);
unsigned int i = 0;
for(i = 0; i < timelimit_secs; i++) {
if(strnlen(segptr, SEGSIZE)) {
updated = 1;
break;
}
if(check_for_mouse_events(display)) {
updated = 1;
break;
}
sleep(1);
}
kill(pid, SIGTERM);
shmdt(segptr);
} else {
/* child */
if((segptr = shmat(shmid, 0, 0)) == (void *)-1) {
DIE_PERROR("shmat");
}
int checkev = detect_events(keyboard_id);
if(checkev != -1) {
snprintf(segptr, SEGSIZE, "%d", checkev);
}
shmdt(segptr);
exit(0);
}
return updated;
}
/*
* Function: main_loop
* -------------------
* Waits for an event to happen within a timelimit. If it doesn't,
* the given command is executed. Then it loops and starts over again.
*
* shmid: a shared memory segment.
* timelimit_secs: a time limit.
* suspend_cmd: a NULL terminated array of the command and arguments.
* keyboard_id: the id of the keyboard to monitor.
* display: the current display.
*
*/
void main_loop(int shmid, unsigned int timelimit_secs,
char **suspend_cmd, XID keyboard_id, Display *display) {
pid_t pid = 0;
int updated = 0;
while(1) {
updated = check_for_events_within_timelimit(shmid, timelimit_secs,
keyboard_id, display);
if(!updated) {
pid = fork();
if (pid == -1) {
perror(0);
_exit(EXIT_FAILURE);
} else if (pid > 0) {
// parent
int status;
waitpid(pid, &status, 0);
} else {
// child
setsid();
execv(suspend_cmd[0], (char * const*)suspend_cmd);
perror("execv");
_exit(EXIT_FAILURE); // exec never returns
}
}
sleep(1);
}
}
int main(int argc, char *argv[]) {
key_t key;
int shmid = 0;
XID keyboard_id = 0;
Display *display = NULL;
unsigned int timelimit_secs = 0;
char **suspend_cmd = NULL;
int i = 0;
// Do all the initiation here
if(argc < 4) {
DIE("wrong number of arguments %d %s. %s\n", argc, argv[1],
USAGE);
return 1;
}
timelimit_secs = (unsigned int)strtol(argv[1], NULL, 10);
if (timelimit_secs == 0 && errno == EINVAL) {
DIE("Error: supply time limit in seconds. %s\n", USAGE);
}
keyboard_id = (XID)strtol(argv[2], NULL, 10);
if (keyboard_id == 0 && errno == EINVAL) {
DIE("Error: supply keyboard ID (use xinput -list). %s\n",
USAGE);
}
key = ftok(".", 'S');
if (key == -1) {
DIE("Error: could not generate a System V IPC key\n");
}
shmid = shmget( key, SEGSIZE, IPC_CREAT | 0660 );
if(shmid == -1) {
DIE("Error: could not get a shared mem segment\n");
}
display = XOpenDisplay( NULL );
if( !display ) {
DIE("Error: could not get the display\n");
}
if(access(argv[3], F_OK) == -1) {
DIE("Error: supply full path to existing command. %s\n",
USAGE);
}
/* prepare the suspend command */
suspend_cmd = (char **)malloc(sizeof(char *) * argc - 1);
if(suspend_cmd == NULL) {
DIE("Error: could not allocate memory\n");
}
for(i = 0; i < argc - 3; i++) {
suspend_cmd[i] = argv[3+i];
}
suspend_cmd[i] = NULL;
main_loop(shmid, timelimit_secs, suspend_cmd, keyboard_id, display);
return 0;
}
/* eof */