forked from thjaeger/easystroke
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.cc
More file actions
1993 lines (1781 loc) · 65.7 KB
/
handler.cc
File metadata and controls
1993 lines (1781 loc) · 65.7 KB
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2012, Thomas Jaeger <ThJaeger@gmail.com>
*
* Permission to use, copy, modify, and/or 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 "handler.h"
#include "main.h"
#include "trace.h"
#include "win.h" // Why?
#include "prefs.h" // Why?
#include <gtkmm.h>
#include <X11/extensions/Xfixes.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/XTest.h>
#include <X11/XKBlib.h>
#include <X11/Xproto.h>
#include <cmath> // std::abs(float)
#include <type_traits>
#include <utility>
using std::abs;
XState *xstate = nullptr;
extern Window get_app_window(Window w);
extern Source<Window> current_app_window;
extern boost::shared_ptr<Trace> trace;
boost::shared_ptr<sigc::slot<void, RStroke> > stroke_action;
template <typename T>
struct has_data_len_member {
private:
template <typename U>
static auto test(int) -> decltype(std::declval<U>().data_len, std::true_type());
template <typename>
static std::false_type test(...);
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
template <typename T>
static inline typename std::enable_if<has_data_len_member<T>::value, int>::type get_cookie_data_len(const T *cookie) {
return cookie->data_len;
}
template <typename T>
static inline typename std::enable_if<!has_data_len_member<T>::value, int>::type get_cookie_data_len(const T *) {
return -1;
}
static XAtom EASYSTROKE_PING("EASYSTROKE_PING");
namespace {
struct PendingFakeMotion {
int x = 0;
int y = 0;
unsigned int count = 0;
} pending_fake_motion;
void record_fake_motion(const double x, const double y) {
pending_fake_motion.x = static_cast<int>(std::lround(x));
pending_fake_motion.y = static_cast<int>(std::lround(y));
pending_fake_motion.count++;
}
bool consume_fake_motion(const double x, const double y) {
if (!pending_fake_motion.count) {
return false;
}
if (pending_fake_motion.x != static_cast<int>(std::lround(x)) ||
pending_fake_motion.y != static_cast<int>(std::lround(y))) {
return false;
}
pending_fake_motion.count--;
return true;
}
void fake_pointer_motion(const double x, const double y, const Time when) {
record_fake_motion(x, y);
XTestFakeMotionEvent(dpy, DefaultScreen(dpy), static_cast<int>(std::lround(x)),
static_cast<int>(std::lround(y)), when);
}
}
bool XState::idle() {
return !handler->child;
}
void XState::queue(sigc::slot<void> f) {
if (f.empty())
return;
if (idle()) {
f();
XFlush(dpy);
} else
queued.push_back(f);
}
void XState::handle_enter_leave(XEvent &ev) {
if (ev.xcrossing.mode == NotifyGrab)
return;
if (ev.xcrossing.detail == NotifyInferior)
return;
const Window w = ev.xcrossing.window;
if (ev.type == EnterNotify) {
current_app_window.set(get_app_window(w));
if (verbosity >= 3)
printf("Entered window 0x%lx -> 0x%lx\n", w, current_app_window.get());
} else printf("Error: Bogus Enter/Leave event\n");
}
#define H (handler->top())
void XState::handle_event(XEvent &ev) {
if (randr_event_base >= 0 &&
(ev.type == randr_event_base + RRScreenChangeNotify || ev.type == randr_event_base + RRNotify)) {
handle_randr_event(ev);
return;
}
switch (ev.type) {
case EnterNotify:
case LeaveNotify:
handle_enter_leave(ev);
return;
case PropertyNotify:
if (current_app_window.get() == ev.xproperty.window && ev.xproperty.atom == XA_WM_CLASS)
current_app_window.notify();
return;
case ButtonPress:
if (verbosity >= 3)
printf("Press (master): %d (%d, %d) at t = %ld\n", ev.xbutton.button, ev.xbutton.x, ev.xbutton.y,
ev.xbutton.time);
H->press_master(ev.xbutton.button, ev.xbutton.time);
return;
case ClientMessage:
if (ev.xclient.window != ping_window)
return;
if (ev.xclient.message_type == *EASYSTROKE_PING) {
if (verbosity >= 3)
printf("Pong\n");
H->pong();
}
return;
case MappingNotify:
if (ev.xmapping.request == MappingPointer)
update_core_mapping();
if (ev.xmapping.request == MappingKeyboard || ev.xmapping.request == MappingModifier)
XRefreshKeyboardMapping(&ev.xmapping);
return;
case GenericEvent:
if (ev.xcookie.extension == grabber->opcode && XGetEventData(dpy, &ev.xcookie)) {
handle_xi2_event(&ev.xcookie);
XFreeEventData(dpy, &ev.xcookie);
}
}
}
void XState::activate_window(Window w, Time t) {
static XAtom _NET_ACTIVE_WINDOW("_NET_ACTIVE_WINDOW");
static XAtom _NET_WM_WINDOW_TYPE("_NET_WM_WINDOW_TYPE");
static XAtom _NET_WM_WINDOW_TYPE_DOCK("_NET_WM_WINDOW_TYPE_DOCK");
static XAtom WM_PROTOCOLS("WM_PROTOCOLS");
static XAtom WM_TAKE_FOCUS("WM_TAKE_FOCUS");
if (w == get_window(ROOT, *_NET_ACTIVE_WINDOW)) {
printf("Ignoring ROOT active window\n");
return;
}
const Atom window_type = get_atom(w, *_NET_WM_WINDOW_TYPE);
if (window_type == *_NET_WM_WINDOW_TYPE_DOCK) {
printf("Ignoring dock window\n");
return;
}
XWMHints *wm_hints = XGetWMHints(dpy, w);
if (wm_hints) {
bool input = wm_hints->input;
XFree(wm_hints);
if (!input)
return;
}
if (!has_atom(w, *WM_PROTOCOLS, *WM_TAKE_FOCUS)) {
return;
}
XWindowAttributes attr;
if (XGetWindowAttributes(dpy, w, &attr) && attr.override_redirect) {
printf("Ignoring override_redirect window\n");
return;
}
if (verbosity >= 3) {
printf("Giving focus to window 0x%lx\n", w);
}
icccm_client_message(w, *WM_TAKE_FOCUS, t);
}
Window XState::get_window(Window w, Atom prop) {
if (!w || !prop) {
return None;
}
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *prop_return = nullptr;
if (XGetWindowProperty(dpy, w, prop, 0, 1, False, XA_WINDOW, &actual_type, &actual_format,
&nitems, &bytes_after, &prop_return) != Success)
return None;
if (!prop_return)
return None;
if (actual_type != XA_WINDOW || actual_format != 32 || nitems < 1) {
XFree(prop_return);
return None;
}
const Window ret = *reinterpret_cast<Window *>(prop_return);
XFree(prop_return);
return ret;
}
Atom XState::get_atom(Window w, Atom prop) {
if (!w || !prop) {
return None;
}
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *prop_return = nullptr;
if (XGetWindowProperty(dpy, w, prop, 0, 1, False, XA_ATOM, &actual_type, &actual_format,
&nitems, &bytes_after, &prop_return) != Success)
return None;
if (!prop_return)
return None;
if (actual_type != XA_ATOM || actual_format != 32 || nitems < 1) {
XFree(prop_return);
return None;
}
const Atom atom = *reinterpret_cast<Atom *>(prop_return);
XFree(prop_return);
return atom;
}
bool XState::has_atom(Window w, Atom prop, Atom value) {
if (!w || !prop || !value) {
return false;
}
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *prop_return = nullptr;
if (XGetWindowProperty(dpy, w, prop, 0, 1024, False, XA_ATOM, &actual_type, &actual_format,
&nitems, &bytes_after, &prop_return) != Success)
return false;
if (!prop_return)
return false;
if (actual_type != XA_ATOM || actual_format != 32 || nitems < 1) {
XFree(prop_return);
return false;
}
const auto atoms = reinterpret_cast<Atom *>(prop_return);
bool ans = false;
for (unsigned long i = 0; i < nitems; i++)
if (atoms[i] == value)
ans = true;
XFree(prop_return);
return ans;
}
void XState::icccm_client_message(Window w, Atom a, Time t) {
static XAtom WM_PROTOCOLS("WM_PROTOCOLS");
XClientMessageEvent ev;
ev.type = ClientMessage;
ev.window = w;
ev.message_type = *WM_PROTOCOLS;
ev.format = 32;
ev.data.l[0] = a;
ev.data.l[1] = t;
XSendEvent(dpy, w, False, 0, reinterpret_cast<XEvent *>(&ev));
}
static void print_coordinates(XIValuatorState *valuators, double *values) {
int n = 0;
for (int i = valuators->mask_len - 1; i >= 0; i--)
if (XIMaskIsSet(valuators->mask, i)) {
n = i + 1;
break;
}
bool first = true;
int elt = 0;
for (int i = 0; i < n; i++) {
if (first)
first = false;
else
printf(", ");
if (XIMaskIsSet(valuators->mask, i))
printf("%.3f", values[elt++]);
else
printf("*");
}
}
static double get_axis(XIValuatorState &valuators, int axis) {
if (axis < 0 || !XIMaskIsSet(valuators.mask, axis)) {
return 0.0;
}
const double *val = valuators.values;
for (int i = 0; i < axis; i++) {
if (XIMaskIsSet(valuators.mask, i)) {
val++;
}
}
return *val;
}
void XState::report_xi2_event(XIDeviceEvent *event, const char *type) {
printf("%s (XI2): ", type);
if (event->detail) {
printf("%d ", event->detail);
}
if (std::strcmp(type, "RawMotion") != 0) {
printf("(%.3f, %.3f) - (", event->root_x, event->root_y);
}
print_coordinates(&event->valuators, event->valuators.values);
printf(") at t = %ld\n", event->time);
}
bool XState::is_synergy_bound(int x, int y) {
return x >= x_min && x <= x_max && y >= y_min && y <= y_max;
}
bool XState::is_cycling_detected(MouseState currentState) {
// Detect if the state transitions from EDGE to CENTER (Cycling behavior)
if (prevState == BOUND && currentState == CENTER) {
transitionOutCount = 0;
transitionCount++;
// If we've seen the transition twice, we can assume it's controlled
if (transitionCount >= requiredTransitions) {
if (!controlled) {
if (verbosity >= 4) printf("Transition in threshold reached!\n");
}
transitionCount = 0;
prevState = currentState;
return true;
}
return false;
}
if (prevState == BOUND && currentState == OUTSIDE) {
transitionCount = 0;
transitionOutCount++;
if (transitionOutCount >= requiredOutTransitions) {
if (controlled) {
if (verbosity >= 4) printf("Transition out threshold reached!\n");
}
transitionOutCount = 0;
prevState = currentState;
return true;
}
return false;
}
if (prevState == BOUND && currentState == BOUND) {
return false;
}
transitionCount = 0;
transitionOutCount = 0;
// Update the previous state
prevState = currentState;
return false;
}
MouseState XState::get_mouse_state(int x, int y) {
bool in_bounds = is_synergy_bound(x, y);
int centerX = (x_min + x_max) / 2;
int centerY = (y_min + y_max) / 2;
bool near_center = std::abs(x - centerX) <= cycling_threshold && std::abs(y - centerY) <= cycling_threshold;
switch (prevState) {
case NONE:
if (near_center) {
if (verbosity >= 4) printf("%s -> Center ( %d, %d )\n", state_name[prevState], x, y);
prevState = CENTER;
} else if (in_bounds) {
if (verbosity >= 4) printf("%s -> Bound ( %d, %d)\n", state_name[prevState], x, y);
prevState = BOUND;
} else {
if (verbosity >= 4) printf("%s -> Outside ( %d, %d)\n", state_name[prevState], x, y);
prevState = OUTSIDE;
}
return prevState;
case CENTER:
if (near_center) {
return CENTER;
}
if (!controlled && in_bounds) {
if (verbosity >= 4) printf("%s -> Bound ( %d, %d )\n", state_name[prevState], x, y);
return BOUND;
}
return OUTSIDE;
case OUTSIDE:
if (!controlled && near_center) {
if (verbosity >= 4) printf("%s -> Center ( %d, %d )\n", state_name[prevState], x, y);
return CENTER;
}
if (controlled && in_bounds) {
if (verbosity >= 4) printf("%s -> Bound ( %d, %d )\n", state_name[prevState], x, y);
return BOUND;
}
break;
case BOUND: {
if (!controlled && near_center) {
if (verbosity >= 4) printf("%s -> Center ( %d, %d )\n", state_name[prevState], x, y);
return CENTER;
}
if (!in_bounds) {
if (verbosity >= 4) printf("%s -> Outside ( %d, %d )\n", state_name[prevState], x, y);
return OUTSIDE;
}
}
}
return prevState;
}
void XState::handle_xi2_event(XGenericEventCookie *cookie) {
if (!cookie || !cookie->data)
return;
auto require_cookie_size = [&](size_t size, const char *name) -> bool {
const int data_len = get_cookie_data_len(cookie);
if (data_len >= 0) {
if (data_len < static_cast<int>(size)) {
if (verbosity >= 1) {
printf("Ignoring XI2 %s event: size %d < %zu\n", name, data_len, size);
}
return false;
}
}
return true;
};
if (!require_cookie_size(sizeof(XIEvent), "header"))
return;
const int evtype = cookie->evtype;
XIEvent *header = static_cast<XIEvent *>(cookie->data);
if (header->evtype != evtype) {
if (verbosity >= 1) {
printf("Ignoring XI2 event: cookie evtype %d != event evtype %d\n", evtype, header->evtype);
}
return;
}
switch (evtype) {
case XI_ButtonPress:
if (!require_cookie_size(sizeof(XIDeviceEvent), "ButtonPress"))
break;
{
XIDeviceEvent *event = static_cast<XIDeviceEvent *>(cookie->data);
if (verbosity >= 3)
report_xi2_event(event, "Press");
if (!xinput_pressed.empty()) {
if (!current_dev || current_dev->dev != event->deviceid) {
break;
}
} else {
current_app_window.set(get_app_window(event->child));
if (verbosity >= 3) {
printf("Active window 0x%lx -> 0x%lx\n", event->child, current_app_window.get());
}
}
current_dev = grabber->get_xi_dev(event->deviceid);
if (!current_dev) {
printf("Warning: Spurious device event\n");
break;
}
if (current_dev->master) {
XISetClientPointer(dpy, None, current_dev->master);
}
if (xinput_pressed.empty()) {
const guint default_mods = grabber->get_default_mods(event->detail);
if (default_mods == AnyModifier || default_mods == static_cast<guint>(event->mods.base)) {
modifiers = AnyModifier;
} else {
modifiers = event->mods.base;
}
}
xinput_pressed.insert(event->detail);
in_proximity = get_axis(event->valuators, current_dev->proximity_axis);
H->press(event->detail, create_triple(event->root_x, event->root_y, event->time));
}
break;
case XI_ButtonRelease:
if (!require_cookie_size(sizeof(XIDeviceEvent), "ButtonRelease"))
break;
{
XIDeviceEvent *event = static_cast<XIDeviceEvent *>(cookie->data);
if (verbosity >= 3) {
report_xi2_event(event, "Release");
}
if (!current_dev || current_dev->dev != event->deviceid) {
printf("not current_dev, or deviceid is wrong; ignoring event\n");
break;
}
xinput_pressed.erase(event->detail);
in_proximity = get_axis(event->valuators, current_dev->proximity_axis);
if (experimental) {
//if device is disabled, but extra buttons followed, treat last extra button as the default button
const auto xi_dev = grabber->get_xi_dev(event->deviceid);
if (!xi_dev) {
if (verbosity >= 2) {
printf("Skipping experimental remap: device %d missing\n", event->deviceid);
}
H->release(event->detail, create_triple(event->root_x, event->root_y, event->time));
break;
}
for (auto i = prefs.excluded_devices.ref().begin(); i != prefs.excluded_devices.ref().end(); ++i) {
//check if the grabbed device name is in disabled device list
if (!i->compare(xi_dev->name)) {
//check if the button is same as last extra button
if (!prefs.extra_buttons.ref().empty() && static_cast<guint>(event->detail) == prefs.
extra_buttons.ref().rbegin()->button) {
event->detail = 100 + event->detail; //fake the default button
}
//event->detail = prefs.button.ref().button; //fake the default button
}
}
}
H->release(event->detail, create_triple(event->root_x, event->root_y, event->time));
}
break;
case XI_Motion:
if (!require_cookie_size(sizeof(XIDeviceEvent), "Motion"))
break;
{
XIDeviceEvent *event = static_cast<XIDeviceEvent *>(cookie->data);
if (consume_fake_motion(event->root_x, event->root_y)) {
if (verbosity >= 5) {
printf("Ignoring synthetic XI2 motion at (%.3f, %.3f)\n", event->root_x, event->root_y);
}
break;
}
{
MouseState currentState = get_mouse_state(event->root_x, event->root_y);
bool cycling_detected = is_cycling_detected(currentState);
if (cycling_detected || prevState != currentState) {
if (prevState != currentState) {
if (verbosity >= 4) printf("Mouse state changed from %s to %s ( %0.2f, %0.2f )\n", state_name[prevState], state_name[currentState], event->root_x, event->root_y);
}
if (cycling_detected) {
if (!controlled && prevState == CENTER && currentState == CENTER) {
if (verbosity >= 3) printf("Mouse is now controlled by Synergy (Cycling detected)!\n");
request_control_state(true, "cycling detection while centered");
} else if (prevState == OUTSIDE && currentState == OUTSIDE) {
if (verbosity >= 3) printf("Mouse is likely no longer controlled by Synergy\n");
request_control_state(false, "cycling detection outside");
}
}
}
}
if (verbosity >= 5) {
report_xi2_event(event, "Motion");
}
if (!current_dev || current_dev->dev != event->deviceid) {
break;
}
H->motion(create_triple(event->root_x, event->root_y, event->time));
}
break;
case XI_RawMotion:
if (!require_cookie_size(sizeof(XIRawEvent), "RawMotion"))
break;
{
XIRawEvent *raw = static_cast<XIRawEvent *>(cookie->data);
if (raw->evtype != XI_RawMotion) {
if (verbosity >= 1) {
printf("Ignoring XI2 RawMotion: evtype %d\n", raw->evtype);
}
break;
}
if (current_dev && current_dev->dev == raw->deviceid) {
in_proximity = get_axis(raw->valuators, current_dev->proximity_axis);
}
handle_raw_motion(raw);
}
break;
case XI_HierarchyChanged:
if (!require_cookie_size(sizeof(XIHierarchyEvent), "HierarchyChanged"))
break;
if (grabber->hierarchy_changed(static_cast<XIHierarchyEvent *>(cookie->data))) {
win->prefs_tab->update_device_list();
}
break;
case XI_BarrierHit: {
if (!require_cookie_size(sizeof(XIBarrierEvent), "BarrierHit"))
break;
const XIBarrierEvent *ev = static_cast<XIBarrierEvent *>(cookie->data);
if (ev->barrier)
XIBarrierReleasePointer(dpy, ev->deviceid, ev->barrier, ev->eventid);
XFlush(dpy);
if (!top && !bottom && !left && !right) {
if (verbosity >= 3) printf("Barrier hit ignored: no active barriers\n");
break;
}
if (!((top && ev->barrier == top) || (bottom && ev->barrier == bottom) ||
(left && ev->barrier == left) || (right && ev->barrier == right))) {
if (verbosity >= 3) printf("Barrier hit ignored: unknown barrier %lu\n", static_cast<unsigned long>(ev->barrier));
break;
}
if (ev->barrier == top) {
if (verbosity >= 3) printf("Top barrier hit %s (%0.2f, %0.2f)\n", controlled ? "controlled" : "uncontrolled", ev->root_x, ev->root_y);
} else if (ev->barrier == bottom) {
if (verbosity >= 3) printf("Bottom barrier hit %s (%0.2f, %0.2f)\n", controlled ? "controlled" : "uncontrolled", ev->root_x, ev->root_y);
} else if (ev->barrier == left) {
if (verbosity >= 3) printf("Left barrier hit %s (%0.2f, %0.2f)\n", controlled ? "controlled" : "uncontrolled", ev->root_x, ev->root_y);
} else if (ev->barrier == right) {
if (verbosity >= 3) printf("Right barrier hit %s (%0.2f, %0.2f)\n", controlled ? "controlled" : "uncontrolled", ev->root_x, ev->root_y);
}
}
break;
case XI_BarrierLeave: {
if (!require_cookie_size(sizeof(XIBarrierEvent), "BarrierLeave"))
break;
const XIBarrierEvent *ev = static_cast<XIBarrierEvent *>(cookie->data);
if (ev->barrier)
XIBarrierReleasePointer(dpy, ev->deviceid, ev->barrier, ev->eventid);
XFlush(dpy);
if (!top && !bottom && !left && !right) {
if (verbosity >= 3) printf("Barrier leave ignored: no active barriers\n");
break;
}
if (!((top && ev->barrier == top) || (bottom && ev->barrier == bottom) ||
(left && ev->barrier == left) || (right && ev->barrier == right))) {
if (verbosity >= 3) printf("Barrier leave ignored: unknown barrier %lu\n", static_cast<unsigned long>(ev->barrier));
break;
}
if (ev->barrier == top) {
if (ev->root_y > screenTop) {
reset_control_tracking(OUTSIDE);
request_control_state(false, "top leave below barrier");
if (verbosity >= 3) printf("Top barrier leave (resumed) uncontrolled (%0.2f, %0.2f)\n", ev->root_x, ev->root_y);
} else if (ev->root_y <= screenTop) {
reset_control_tracking(OUTSIDE);
request_control_state(true, "top leave above barrier");
if (verbosity >= 3) printf("Top barrier leave (suspended) controlled (%0.2f, %0.2f)\n", ev->root_x, ev->root_y);
}
} else if (ev->barrier == bottom) {
if (ev->root_y >= screenBot) {
reset_control_tracking(OUTSIDE);
request_control_state(true, "bottom leave below screen");
if (verbosity >= 3) printf("Bottom barrier leave DOWN controlled (%0.2f, %0.2f)\n", ev->root_x, ev->root_y);
}
} else if (ev->barrier == left) {
if (ev->root_x <= screenLeft) {
reset_control_tracking(OUTSIDE);
request_control_state(true, "left barrier leave");
if (verbosity >= 3) printf("Left barrier leave LEFT controlled (%0.2f, %0.2f)\n", ev->root_x, ev->root_y);
}
} else if (ev->barrier == right) {
if (ev->root_x >= screenRight) {
reset_control_tracking(OUTSIDE);
request_control_state(true, "right barrier leave");
if (verbosity >= 3) printf("Right barrier leave RIGHT controlled (%0.2f, %0.2f)\n", ev->root_x, ev->root_y);
}
}
if (verbosity >= 3)
printf("[Barrier Leave] Barrier properties: %d, %d, %d, %d\n", screenLeft, screenTop, screenRight, screenBot);
}
break;
}
}
void XState::handle_raw_motion(XIRawEvent *event) {
if (!current_dev || current_dev->dev != event->deviceid)
return;
double x = 0.0, y = 0.0;
bool abs_x = current_dev->absolute;
bool abs_y = current_dev->absolute;
const int max_bits = event->valuators.mask_len * 8;
auto axis_index = [&](int axis) -> int {
if (axis < 0 || axis >= max_bits)
return -1;
if (!XIMaskIsSet(event->valuators.mask, axis))
return -1;
int idx = 0;
for (int bit = 0; bit < axis; ++bit) {
if (XIMaskIsSet(event->valuators.mask, bit))
idx++;
}
return idx;
};
if (!event->raw_values) {
if (verbosity >= 2)
printf("Raw motion (XI2): missing raw_values\n");
return;
}
const int x_index = axis_index(0);
if (x_index >= 0)
x = event->raw_values[x_index];
else
abs_x = false;
const int y_index = axis_index(1);
if (y_index >= 0)
y = event->raw_values[y_index];
else
abs_y = false;
if (verbosity >= 5) {
printf("Raw motion (XI2): (");
print_coordinates(&event->valuators, event->raw_values);
printf(") at t = %ld\n", event->time);
}
H->raw_motion(create_triple(x * current_dev->scale_x, y * current_dev->scale_y, event->time), abs_x, abs_y);
}
#undef H
bool XState::handle(Glib::IOCondition) {
bool more = drain_pending_events_batch(64);
if (more && !drain_idle.connected())
drain_idle = Glib::signal_idle().connect(sigc::mem_fun(*this, &XState::drain_pending_events));
return true;
}
void XState::update_core_mapping() {
unsigned char map[MAX_BUTTONS];
const int n = XGetPointerMapping(dpy, map, MAX_BUTTONS);
core_inv_map.clear();
for (int i = n - 1; i; i--) {
if (map[i] == i + 1) {
core_inv_map.erase(i + 1);
} else {
core_inv_map[map[i]] = i + 1;
}
}
}
void XState::fake_core_button(guint b, bool press) {
if (core_inv_map.count(b)) {
b = core_inv_map[b];
}
XTestFakeButtonEvent(dpy, b, press, CurrentTime);
XSync(dpy, False);
}
void XState::fake_click(guint b) {
fake_core_button(b, true);
fake_core_button(b, false);
}
void Handler::replace_child(Handler *c) {
delete child;
child = c;
if (child)
child->parent = this;
if (verbosity >= 2) {
std::string stack;
for (Handler *h = child ? child : this; h; h = h->parent) {
stack = h->name() + " " + stack;
}
printf("New event handling stack: %s\n", stack.c_str());
}
Handler *new_handler = child ? child : this;
grabber->grab(new_handler->grab_mode());
if (child) {
child->init();
}
while (!xstate->queued.empty() && xstate->idle()) {
sigc::slot<void> callback = *xstate->queued.begin();
xstate->queued.pop_front();
if (!callback.empty())
callback();
}
}
class IgnoreHandler : public Handler {
RModifiers mods;
bool proximity;
public:
IgnoreHandler(RModifiers mods_) : mods(mods_), proximity(xstate->in_proximity && prefs.proximity.get()) {
}
virtual void press(guint b, RTriple e) {
if (xstate->current_dev && xstate->current_dev->master) {
fake_pointer_motion(e->x, e->y, 0);
XTestFakeButtonEvent(dpy, b, true, CurrentTime);
}
}
virtual void motion(RTriple e) {
if (xstate->current_dev && xstate->current_dev->master) {
fake_pointer_motion(e->x, e->y, 0);
}
if (proximity && !xstate->in_proximity)
parent->replace_child(nullptr);
}
virtual void release(guint b, RTriple e) {
if (xstate->current_dev && xstate->current_dev->master) {
fake_pointer_motion(e->x, e->y, 0);
XTestFakeButtonEvent(dpy, b, false, CurrentTime);
}
if (proximity ? !xstate->in_proximity : xstate->xinput_pressed.empty())
parent->replace_child(nullptr);
}
virtual std::string name() { return "Ignore"; }
virtual Grabber::State grab_mode() { return Grabber::NONE; }
};
class ButtonHandler : public Handler {
RModifiers mods;
guint button, real_button;
bool proximity;
public:
ButtonHandler(RModifiers mods_, guint button_) : mods(mods_),
button(button_),
real_button(0),
proximity(xstate->in_proximity && prefs.proximity.get()) {
}
virtual void press(guint b, RTriple e) {
if (xstate->current_dev && xstate->current_dev->master) {
if (!real_button)
real_button = b;
if (real_button == b)
b = button;
fake_pointer_motion(e->x, e->y, 0);
XTestFakeButtonEvent(dpy, b, true, CurrentTime);
}
}
virtual void motion(RTriple e) {
if (xstate->current_dev && xstate->current_dev->master)
fake_pointer_motion(e->x, e->y, 0);
if (proximity && !xstate->in_proximity)
parent->replace_child(nullptr);
}
virtual void release(guint b, RTriple e) {
if (xstate->current_dev && xstate->current_dev->master) {
if (real_button == b)
b = button;
fake_pointer_motion(e->x, e->y, 0);
XTestFakeButtonEvent(dpy, b, false, CurrentTime);
}
if (proximity ? !xstate->in_proximity : xstate->xinput_pressed.empty())
parent->replace_child(nullptr);
}
virtual std::string name() { return "Button"; }
virtual Grabber::State grab_mode() { return Grabber::NONE; }
};
void XState::bail_out() {
handler->replace_child(nullptr);
xinput_pressed.clear();
XFlush(dpy);
}
int XState::xErrorHandler(Display *dpy2, XErrorEvent *e) {
if (dpy != dpy2)
return xstate->oldHandler(dpy2, e);
if (verbosity <= 5 && e->error_code == BadWindow) {
switch (e->request_code) {
case X_ChangeWindowAttributes:
case X_GetProperty:
case X_QueryTree:
return 0;
}
}
char text[64];
XGetErrorText(dpy, e->error_code, text, sizeof text);
char msg[16];
snprintf(msg, sizeof msg, "%d", e->request_code);
char def[128];
if (e->request_code < 128)
snprintf(def, sizeof def, "request_code=%d, minor_code=%d", e->request_code, e->minor_code);
else
snprintf(def, sizeof def, "extension=%s, request_code=%d", xstate->opcodes[e->request_code].c_str(),
e->minor_code);
char dbtext[128];
XGetErrorDatabaseText(dpy, "XRequest", msg, def, dbtext, sizeof dbtext);
printf("XError: %s: %s\n", text, dbtext);
return 0;
}
int XState::xIOErrorHandler(Display *dpy2) {
if (dpy != dpy2)
return xstate->oldIOHandler(dpy2);
printf("Fatal Error: Connection to X server lost\n");
quit();
return 0;
}
void XState::ping() {
XClientMessageEvent ev;
ev.type = ClientMessage;
ev.window = ping_window;
ev.message_type = *EASYSTROKE_PING;
ev.format = 32;
XSendEvent(dpy, ping_window, False, 0, reinterpret_cast<XEvent *>(&ev));
XFlush(dpy);
}
void XState::remove_device(int deviceid) {
if (current_dev && current_dev->dev == deviceid)
current_dev = nullptr;
}
void XState::ungrab(int deviceid) {
if (current_dev && current_dev->dev == deviceid)
xinput_pressed.clear();
}
void XState::reset_local_input_state() {
xinput_pressed.clear();
current_dev = nullptr;
in_proximity = false;
modifiers = 0;
}
void XState::reset_control_tracking(MouseState state) {
prevState = state;
transitionCount = 0;
transitionOutCount = 0;
}
void XState::cancel_pending_control_state() {
if (control_timeout.connected())
control_timeout.disconnect();
if (!controlled)
set_pending_control_suspend(false);
target_controlled = controlled;
pending_control_reason.clear();
}
void XState::set_pending_control_suspend(bool suspend) {
if (suspend) {
if (pending_control_suspend)
return;
pending_control_suspend = true;
grabber->suspend();
return;
}
if (!pending_control_suspend)
return;
pending_control_suspend = false;
grabber->resume();
}
class WaitForPongHandler : public Handler, protected Timeout {
public:
WaitForPongHandler() { set_timeout(100); }
virtual void timeout() {
printf("Warning: %s timed out\n", "WaitForPongHandler");
xstate->bail_out();
}
virtual void pong() { parent->replace_child(nullptr); }
virtual std::string name() { return "WaitForPong"; }