-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmousemode.c
More file actions
151 lines (138 loc) · 4.85 KB
/
mousemode.c
File metadata and controls
151 lines (138 loc) · 4.85 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
#define _POSIX_C_SOURCE 199309L
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <time.h>
#include <signal.h>
#define FREQUENCY 60
Display *dpy;
int running = 1;
int root;
char keys_return[32] = {0};
int mousespdl = 0;
int mousespdd = 0;
int mousespdu = 0;
int mousespdr = 0;
int mousepress1 = 0;
int mousepress2 = 0;
int mousepress3 = 0;
int mousescrollrate = 0;
int iskeydown(int keysym)
{
KeyCode c = XKeysymToKeycode(dpy, keysym);
return (keys_return[ c >> 3 ] & (1 << (c & 7)))? 1 : 0;
}
int handleKeys()
{
/* References:
* https://stackoverflow.com/a/49840783
* https://pastebin.com/sk7FZ6AP
*/
XQueryKeymap(dpy, keys_return);
if (iskeydown(XK_q))
return 0;
if (iskeydown(XK_Super_L) || iskeydown(XK_Super_R))
return 1;
int isfast, isslow, isleft, isdown, isup, isright, ism1, ism2, ism3, ism4, ism5;
isfast = iskeydown(XK_f);
isslow = iskeydown(XK_c);
isleft = (iskeydown(XK_h) || iskeydown(XK_Left) || iskeydown(XK_KP_Left))? 1 : 0;
isdown = (iskeydown(XK_j) || iskeydown(XK_Down) || iskeydown(XK_KP_Down))? 1 : 0;
isup = (iskeydown(XK_k) || iskeydown(XK_Up) || iskeydown(XK_KP_Up))? 1 : 0;
isright = (iskeydown(XK_l) || iskeydown(XK_Right)|| iskeydown(XK_KP_Right))? 1 : 0;
ism1 = iskeydown(XK_a);
ism2 = iskeydown(XK_s);
ism3 = iskeydown(XK_d);
ism4 = iskeydown(XK_y);
ism5 = iskeydown(XK_e);
mousespdl = isleft * (isslow? 1 : 8) * (isfast? 2 : 1);
mousespdd = isdown * (isslow? 1 : 8) * (isfast? 2 : 1);
mousespdu = isup * (isslow? 1 : 8) * (isfast? 2 : 1);
mousespdr = isright * (isslow? 1 : 8) * (isfast? 2 : 1);
XWarpPointer(dpy, None, None, 0, 0, 0, 0, mousespdr - mousespdl, mousespdd - mousespdu);
if (ism1 && !mousepress1) {
XTestFakeButtonEvent(dpy, 1, True, CurrentTime);
mousepress1 = 1;
} else if (!ism1 && mousepress1) {
XTestFakeButtonEvent(dpy, 1, False, CurrentTime);
mousepress1 = 0;
}
if (ism2 && !mousepress2) {
XTestFakeButtonEvent(dpy, 2, True, CurrentTime);
mousepress2 = 1;
} else if (!ism2 && mousepress2) {
XTestFakeButtonEvent(dpy, 2, False, CurrentTime);
mousepress2 = 0;
}
if (ism3 && !mousepress3) {
XTestFakeButtonEvent(dpy, 3, True, CurrentTime);
mousepress3 = 1;
} else if (!ism3 && mousepress3) {
XTestFakeButtonEvent(dpy, 3, False, CurrentTime);
mousepress3 = 0;
}
if (!isslow) {
if (ism4 && !ism5 && mousescrollrate-- <= 0) {
XTestFakeButtonEvent(dpy, 4, True, CurrentTime);
XTestFakeButtonEvent(dpy, 4, False, CurrentTime);
mousescrollrate = (isfast? 1 : 2);
} else if (!ism4 && ism5 && mousescrollrate-- <= 0) {
XTestFakeButtonEvent(dpy, 5, True, CurrentTime);
XTestFakeButtonEvent(dpy, 5, False, CurrentTime);
mousescrollrate = (isfast? 1 : 2);
} else if (!ism4 && !ism5) {
mousescrollrate = 0;
}
} else {
XTestFakeButtonEvent(dpy, 4, ism4? True : False, CurrentTime);
XTestFakeButtonEvent(dpy, 5, ism5? True : False, CurrentTime);
}
XFlush(dpy);
return 1;
}
void grabkey(int keysym)
{
KeyCode code;
if ((code = XKeysymToKeycode(dpy, keysym))) {
XGrabKey(dpy, code, 0, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, ShiftMask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, ControlMask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, Mod1Mask, root, True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, code, ShiftMask|ControlMask, root, True, GrabModeAsync, GrabModeAsync);
}
}
void termhandler(int signum)
{
running = 0;
}
int main()
{
signal(SIGTERM, termhandler);
signal(SIGINT, termhandler);
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "mousemode: failed to open display");
return 2;
}
root = RootWindow(dpy, DefaultScreen(dpy));
{
grabkey(XK_q);
grabkey(XK_a); grabkey(XK_s); grabkey(XK_d);
grabkey(XK_e); grabkey(XK_y);
grabkey(XK_f); grabkey(XK_c);
grabkey(XK_h) ; grabkey(XK_j) ; grabkey(XK_k) ; grabkey(XK_l) ;
grabkey(XK_Left) ; grabkey(XK_Down) ; grabkey(XK_Up) ; grabkey(XK_Right) ;
grabkey(XK_KP_Left); grabkey(XK_KP_Down); grabkey(XK_KP_Up); grabkey(XK_KP_Right);
}
while (running) {
const struct timespec sleep_timespec = {0, 1e9 / FREQUENCY};
running &= handleKeys();
nanosleep(&sleep_timespec, NULL);
}
XUngrabKey(dpy, AnyKey, AnyModifier, root);
for (int i = 1; i <= 5; i++)
XTestFakeButtonEvent(dpy, i, False, CurrentTime);
XCloseDisplay(dpy);
return 0;
}