forked from AtomToast/xbanish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbanish.c
685 lines (589 loc) · 15.5 KB
/
xbanish.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* xbanish
* Copyright (c) 2013-2021 joshua stein <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/sync.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xutil.h>
void hide_cursor(void);
void show_cursor(void);
void snoop_root(void);
int snoop_xinput(Window);
void snoop_legacy(Window);
void set_alarm(XSyncAlarm *, XSyncTestType);
void usage(char *);
int swallow_error(Display *, XErrorEvent *);
int parse_geometry(const char *s);
/* xinput event type ids to be filled in later */
static int button_press_type = -1;
static int button_release_type = -1;
static int key_press_type = -1;
static int key_release_type = -1;
static int motion_type = -1;
static int device_change_type = -1;
static long last_device_change = -1;
static Display *dpy;
static int hiding = 0, legacy = 0, always_hide = 0, keep_touch_cursor = 0, ignore_scroll = 0;
static unsigned timeout = 0;
static unsigned char ignored;
static XSyncCounter idler_counter = 0;
static XSyncAlarm idle_alarm = None;
static int debug = 0;
#define DPRINTF(x) { if (debug) { printf x; } };
#define MAX_COMMAND_LENGTH 512
#define MAX_XINPUT_DEVICE 64
static int move = 0, move_x, move_y, move_custom_x, move_custom_y, move_custom_mask;
enum move_types {
MOVE_NW = 1,
MOVE_NE,
MOVE_SW,
MOVE_SE,
MOVE_WIN_NW,
MOVE_WIN_NE,
MOVE_WIN_SW,
MOVE_WIN_SE,
MOVE_CUSTOM,
};
int
main(int argc, char *argv[])
{
int ch, i;
XEvent e;
XSyncAlarmNotifyEvent *alarm_e;
XGenericEventCookie *cookie;
XSyncSystemCounter *counters;
int sync_event, error;
int major, minor, ncounters;
int current_device;
struct mod_lookup {
char *name;
int mask;
} mods[] = {
{"shift", ShiftMask}, {"lock", LockMask},
{"control", ControlMask}, {"mod1", Mod1Mask},
{"mod2", Mod2Mask}, {"mod3", Mod3Mask},
{"mod4", Mod4Mask}, {"mod5", Mod5Mask},
{"all", -1},
};
int xinput_device = -1;
int n_commands = 0;
int devices[MAX_XINPUT_DEVICE] = {0};
char device_command[MAX_XINPUT_DEVICE][MAX_COMMAND_LENGTH] = {0};
while ((ch = getopt(argc, argv, "adki:m:t:sx:c:")) != -1)
switch (ch) {
case 'a':
always_hide = 1;
break;
case 'd':
debug = 1;
break;
case 'k':
keep_touch_cursor = 1;
break;
case 'i':
for (i = 0;
i < sizeof(mods) / sizeof(struct mod_lookup); i++)
if (strcasecmp(optarg, mods[i].name) == 0)
ignored |= mods[i].mask;
break;
case 'm':
if (strcmp(optarg, "nw") == 0)
move = MOVE_NW;
else if (strcmp(optarg, "ne") == 0)
move = MOVE_NE;
else if (strcmp(optarg, "sw") == 0)
move = MOVE_SW;
else if (strcmp(optarg, "se") == 0)
move = MOVE_SE;
else if (strcmp(optarg, "wnw") == 0)
move = MOVE_WIN_NW;
else if (strcmp(optarg, "wne") == 0)
move = MOVE_WIN_NE;
else if (strcmp(optarg, "wsw") == 0)
move = MOVE_WIN_SW;
else if (strcmp(optarg, "wse") == 0)
move = MOVE_WIN_SE;
else if (parse_geometry(optarg))
move = MOVE_CUSTOM;
else {
warnx("invalid '-m' argument");
usage(argv[0]);
}
break;
case 't':
timeout = strtoul(optarg, NULL, 0);
break;
case 's':
ignore_scroll = 1;
break;
case 'x':
if (xinput_device >= 0) {
warnx("invalid '-x' argument");
usage(argv[0]);
}
xinput_device = strtol(optarg, NULL, 0);
if (xinput_device > MAX_XINPUT_DEVICE) {
warnx("invalid '-x' argument");
usage(argv[0]);
}
break;
case 'c':
if (xinput_device < 0) {
warnx("invalid '-c' argument");
usage(argv[0]);
exit(1);
}
if (strlen(optarg) > MAX_COMMAND_LENGTH - 1) {
warnx("'-c' argument is too long.\n");
exit(1);
}
strcpy(device_command[xinput_device], optarg);
devices[n_commands] = xinput_device;
n_commands += 1;
xinput_device = -1;
break;
default:
usage(argv[0]);
}
argc -= optind;
argv += optind;
if (!(dpy = XOpenDisplay(NULL)))
errx(1, "can't open display %s", XDisplayName(NULL));
#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
#endif
XSetErrorHandler(swallow_error);
snoop_root();
if (always_hide)
hide_cursor();
/* required setup for the xsync alarms used by timeout */
if (timeout) {
if (XSyncQueryExtension(dpy, &sync_event, &error) != True)
errx(1, "no sync extension available");
XSyncInitialize(dpy, &major, &minor);
counters = XSyncListSystemCounters(dpy, &ncounters);
for (i = 0; i < ncounters; i++) {
if (!strcmp(counters[i].name, "IDLETIME")) {
idler_counter = counters[i].counter;
break;
}
}
XSyncFreeSystemCounterList(counters);
if (!idler_counter)
errx(1, "no idle counter");
}
current_device = -1;
system("gsettings set org.gnome.desktop.interface cursor-theme Hackneyed &");
for (;;) {
cookie = &e.xcookie;
XNextEvent(dpy, &e);
int etype = e.type;
if (e.type == motion_type)
etype = MotionNotify;
else if (e.type == key_press_type ||
e.type == key_release_type)
etype = KeyRelease;
else if (e.type == button_press_type ||
e.type == button_release_type)
etype = ButtonRelease;
else if (e.type == device_change_type) {
XDevicePresenceNotifyEvent *xdpe =
(XDevicePresenceNotifyEvent *)&e;
if (last_device_change == xdpe->serial)
continue;
snoop_root();
last_device_change = xdpe->serial;
continue;
}
switch (etype) {
case KeyRelease:
if (ignored) {
unsigned int state = 0;
/* masks are only set on key release, if
* ignore is set we must throw out non-release
* events here */
if (e.type == key_press_type) {
break;
}
/* extract modifier state */
if (e.type == key_release_type) {
/* xinput device event */
XDeviceKeyEvent *key =
(XDeviceKeyEvent *) &e;
state = key->state;
} else if (e.type == KeyRelease) {
/* legacy event */
state = e.xkey.state;
}
if (state & ignored) {
DPRINTF(("ignoring key %d\n", state));
break;
}
}
hide_cursor();
break;
case ButtonRelease:
case MotionNotify:
if (!always_hide)
show_cursor();
break;
case CreateNotify:
if (legacy) {
DPRINTF(("new window, snooping on it\n"));
/* not sure why snooping directly on the window
* doesn't work, so snoop on all windows from
* its parent (probably root) */
snoop_legacy(e.xcreatewindow.parent);
}
break;
case GenericEvent:
/* xi2 raw event */
XGetEventData(dpy, cookie);
XIDeviceEvent *xie = (XIDeviceEvent *)cookie->data;
if (current_device != xie->sourceid) {
current_device = xie->sourceid;
int i = 0;
while (i < n_commands) {
if (current_device == devices[i]) {
system(device_command[devices[i]]);
break;
}
i += 1;
}
if (i == n_commands) {
system(device_command[0]);
}
}
switch (xie->evtype) {
case XI_RawMotion:
case XI_RawButtonPress:
if (ignore_scroll && ((xie->detail >= 4 && xie->detail <= 7) ||
xie->event_x == xie->event_y))
break;
if (!always_hide)
show_cursor();
break;
case XI_RawButtonRelease:
break;
case XI_RawTouchBegin:
case XI_RawTouchEnd:
case XI_RawTouchUpdate:
if (!keep_touch_cursor)
hide_cursor();
break;
default:
DPRINTF(("unknown XI event type %d\n",
xie->evtype));
}
XFreeEventData(dpy, cookie);
break;
default:
if (!timeout ||
e.type != (sync_event + XSyncAlarmNotify)) {
DPRINTF(("unknown event type %d\n", e.type));
break;
}
alarm_e = (XSyncAlarmNotifyEvent *)&e;
if (alarm_e->alarm == idle_alarm) {
DPRINTF(("idle counter reached %dms, hiding "
"cursor\n",
XSyncValueLow32(alarm_e->counter_value)));
hide_cursor();
}
}
}
}
void
hide_cursor(void)
{
Window win;
XWindowAttributes attrs;
int x, y, h, w, junk;
unsigned int ujunk;
DPRINTF(("keystroke, %shiding cursor\n", (hiding ? "already " : "")));
if (hiding)
return;
if (move) {
if (XQueryPointer(dpy, DefaultRootWindow(dpy),
&win, &win, &x, &y, &junk, &junk, &ujunk)) {
move_x = x;
move_y = y;
XGetWindowAttributes(dpy, win, &attrs);
h = XHeightOfScreen(DefaultScreenOfDisplay(dpy));
w = XWidthOfScreen(DefaultScreenOfDisplay(dpy));
switch (move) {
case MOVE_NW:
x = 0;
y = 0;
break;
case MOVE_NE:
x = w;
y = 0;
break;
case MOVE_SW:
x = 0;
y = h;
break;
case MOVE_SE:
x = w;
y = h;
break;
case MOVE_WIN_NW:
x = attrs.x;
y = attrs.y;
break;
case MOVE_WIN_NE:
x = attrs.x + attrs.width;
y = attrs.y;
break;
case MOVE_WIN_SW:
x = attrs.x;
y = attrs.x + attrs.height;
break;
case MOVE_WIN_SE:
x = attrs.x + attrs.width;
y = attrs.x + attrs.height;
break;
case MOVE_CUSTOM:
x = (move_custom_mask & XNegative ? w : 0) + move_custom_x;
y = (move_custom_mask & YNegative ? h : 0) + move_custom_y;
break;
}
XWarpPointer(dpy, None, DefaultRootWindow(dpy),
0, 0, 0, 0, x, y);
} else {
move_x = -1;
move_y = -1;
warn("failed finding cursor coordinates");
}
}
XFixesHideCursor(dpy, DefaultRootWindow(dpy));
hiding = 1;
}
void
show_cursor(void)
{
DPRINTF(("mouse moved, %sunhiding cursor\n",
(hiding ? "" : "already ")));
if (timeout) {
DPRINTF(("(re)setting timeout of %us\n", timeout));
set_alarm(&idle_alarm, XSyncPositiveComparison);
}
if (!hiding)
return;
if (move && move_x != -1 && move_y != -1)
XWarpPointer(dpy, None, DefaultRootWindow(dpy), 0, 0, 0, 0,
move_x, move_y);
XFixesShowCursor(dpy, DefaultRootWindow(dpy));
hiding = 0;
}
void
snoop_root(void)
{
if (snoop_xinput(DefaultRootWindow(dpy)) == 0) {
DPRINTF(("no XInput devices found, using legacy snooping"));
legacy = 1;
snoop_legacy(DefaultRootWindow(dpy));
}
}
int
snoop_xinput(Window win)
{
int opcode, event, error, numdevs, i, j;
int major, minor, rc, rawmotion = 0;
int ev = 0;
unsigned char mask[(XI_LASTEVENT + 7)/8];
XDeviceInfo *devinfo = NULL;
XInputClassInfo *ici;
XDevice *device;
XIEventMask evmasks[1];
XEventClass class_presence;
if (!XQueryExtension(dpy, "XInputExtension", &opcode, &event, &error)) {
DPRINTF(("XInput extension not available"));
return 0;
}
/*
* If we support xinput 2, use that for raw motion and button events to
* get pointer data when the cursor is over a Chromium window. We
* could also use this to get raw key input and avoid the other XInput
* stuff, but we may need to be able to examine the key value later to
* filter out ignored keys.
*/
major = minor = 2;
rc = XIQueryVersion(dpy, &major, &minor);
if (rc != BadRequest) {
memset(mask, 0, sizeof(mask));
XISetMask(mask, XI_RawMotion);
XISetMask(mask, XI_RawButtonPress);
XISetMask(mask, XI_RawTouchBegin);
evmasks[0].deviceid = XIAllMasterDevices;
evmasks[0].mask_len = sizeof(mask);
evmasks[0].mask = mask;
XISelectEvents(dpy, win, evmasks, 1);
XFlush(dpy);
rawmotion = 1;
DPRINTF(("using xinput2 raw motion events\n"));
}
devinfo = XListInputDevices(dpy, &numdevs);
XEventClass event_list[numdevs * 2];
for (i = 0; i < numdevs; i++) {
if (devinfo[i].use != IsXExtensionKeyboard &&
devinfo[i].use != IsXExtensionPointer)
continue;
if (!(device = XOpenDevice(dpy, devinfo[i].id)))
break;
for (ici = device->classes, j = 0; j < devinfo[i].num_classes;
ici++, j++) {
switch (ici->input_class) {
case KeyClass:
DPRINTF(("attaching to keyboard device %s "
"(use %d)\n", devinfo[i].name,
devinfo[i].use));
DeviceKeyPress(device, key_press_type,
event_list[ev]); ev++;
DeviceKeyRelease(device, key_release_type,
event_list[ev]); ev++;
break;
case ButtonClass:
if (rawmotion)
continue;
DPRINTF(("attaching to buttoned device %s "
"(use %d)\n", devinfo[i].name,
devinfo[i].use));
DeviceButtonPress(device, button_press_type,
event_list[ev]); ev++;
DeviceButtonRelease(device,
button_release_type, event_list[ev]); ev++;
break;
case ValuatorClass:
if (rawmotion)
continue;
DPRINTF(("attaching to pointing device %s "
"(use %d)\n", devinfo[i].name,
devinfo[i].use));
DeviceMotionNotify(device, motion_type,
event_list[ev]); ev++;
break;
}
}
XCloseDevice(dpy, device);
if (XSelectExtensionEvent(dpy, win, event_list, ev)) {
warn("error selecting extension events");
ev = 0;
goto done;
}
}
DevicePresence(dpy, device_change_type, class_presence);
if (XSelectExtensionEvent(dpy, win, &class_presence, 1)) {
warn("error selecting extension events");
ev = 0;
goto done;
}
done:
if (devinfo != NULL)
XFreeDeviceList(devinfo);
return ev;
}
void
snoop_legacy(Window win)
{
Window parent, root, *kids = NULL;
XSetWindowAttributes sattrs;
unsigned int nkids = 0, i;
/*
* Firefox stops responding to keys when KeyPressMask is used, so
* settle for KeyReleaseMask
*/
int type = PointerMotionMask | KeyReleaseMask | Button1MotionMask |
Button2MotionMask | Button3MotionMask | Button4MotionMask |
Button5MotionMask | ButtonMotionMask;
if (XQueryTree(dpy, win, &root, &parent, &kids, &nkids) == 0) {
warn("can't query window tree\n");
goto done;
}
XSelectInput(dpy, root, type);
/* listen for newly mapped windows */
sattrs.event_mask = SubstructureNotifyMask;
XChangeWindowAttributes(dpy, root, CWEventMask, &sattrs);
for (i = 0; i < nkids; i++) {
XSelectInput(dpy, kids[i], type);
snoop_legacy(kids[i]);
}
done:
if (kids != NULL)
XFree(kids); /* hide yo kids */
}
void
set_alarm(XSyncAlarm *alarm, XSyncTestType test)
{
XSyncAlarmAttributes attr;
XSyncValue value;
unsigned int flags;
XSyncQueryCounter(dpy, idler_counter, &value);
attr.trigger.counter = idler_counter;
attr.trigger.test_type = test;
attr.trigger.value_type = XSyncRelative;
XSyncIntsToValue(&attr.trigger.wait_value, timeout * 1000,
(unsigned long)(timeout * 1000) >> 32);
XSyncIntToValue(&attr.delta, 0);
flags = XSyncCACounter | XSyncCATestType | XSyncCAValue | XSyncCADelta;
if (*alarm)
XSyncDestroyAlarm(dpy, *alarm);
*alarm = XSyncCreateAlarm(dpy, flags, &attr);
}
void
usage(char *progname)
{
fprintf(stderr, "usage: %s [-a] [-d] [-k] [-i mod] [-m [w]nw|ne|sw|se|+/-xy] "
"[-t seconds] [-s]\n", progname);
exit(1);
}
int
swallow_error(Display *d, XErrorEvent *e)
{
if (e->error_code == BadWindow)
/* no biggie */
return 0;
if (e->error_code & FirstExtensionError)
/* error requesting input on a particular xinput device */
return 0;
errx(1, "got X error %d", e->error_code);
}
int
parse_geometry(const char *s)
{
int x, y;
unsigned int junk;
int ret = XParseGeometry(s, &x, &y, &junk, &junk);
if (((ret & XValue) || (ret & XNegative)) &&
((ret & YValue) || (ret & YNegative))) {
move_custom_x = x;
move_custom_y = y;
move_custom_mask = ret;
return 1;
}
return 0;
}