-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpointerbarrier.c
314 lines (268 loc) · 8.7 KB
/
xpointerbarrier.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
/*
* Based on Peter Hutterer's example:
*
* http://who-t.blogspot.de/2012/12/whats-new-in-xi-23-pointer-barrier.html
*/
#define _POSIX_C_SOURCE 199309L /* for sigaction */
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xrandr.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
struct Insets
{
int top, left, right, bottom;
};
bool do_toggle = false;
bool verbose = false;
PointerBarrier
create_barrier_verbose(Display *dpy, Window w, int x1, int y1,
int x2, int y2, int directions,
int num_devices, int *devices)
{
PointerBarrier b;
b = XFixesCreatePointerBarrier(dpy, w, x1, y1, x2, y2, directions,
num_devices, devices);
if (verbose)
fprintf(stderr, __NAME__": + Created barrier %lu (%d, %d) -> (%d, %d)\n",
b, x1, y1, x2, y2);
return b;
}
PointerBarrier *
create(Display *dpy, Window root, struct Insets *insets, int *num)
{
XRRMonitorInfo *moninf;
int i, barr_i, nmon;
PointerBarrier *barriers = NULL;
moninf = XRRGetMonitors(dpy, root, True, &nmon);
if (nmon <= 0 || moninf == NULL)
{
fprintf(stderr, __NAME__": No XRandR screens found\n");
return NULL;
}
if (verbose)
fprintf(stderr, __NAME__": We found %d XRandR screens\n", nmon);
/* Per CRTC, we will create 4 barriers */
*num = nmon * 4;
barriers = calloc(*num, sizeof (PointerBarrier));
if (barriers == NULL)
{
fprintf(stderr, __NAME__": Could not allocate memory for pointer "
"barriers\n");
return NULL;
}
barr_i = 0;
for (i = 0; i < nmon; i++)
{
/* Top, left, right, bottom.
*
* If an inset of zero px has been specified, then we'll create
* an omnidirectional barrier. This avoids overlapping barriers
* which would cancel each out (partially). */
barriers[barr_i++] = create_barrier_verbose(
dpy, root,
moninf[i].x, moninf[i].y + insets->top,
moninf[i].x + moninf[i].width, moninf[i].y + insets->top,
insets->top != 0 ? BarrierPositiveY : 0, 0, NULL
);
barriers[barr_i++] = create_barrier_verbose(
dpy, root,
moninf[i].x + insets->left, moninf[i].y,
moninf[i].x + insets->left, moninf[i].y + moninf[i].height,
insets->left != 0 ? BarrierPositiveX : 0, 0, NULL
);
barriers[barr_i++] = create_barrier_verbose(
dpy, root,
moninf[i].x + moninf[i].width - insets->right, moninf[i].y,
moninf[i].x + moninf[i].width - insets->right, moninf[i].y + moninf[i].height,
insets->right != 0 ? BarrierNegativeX : 0, 0, NULL
);
barriers[barr_i++] = create_barrier_verbose(
dpy, root,
moninf[i].x, moninf[i].y + moninf[i].height - insets->bottom,
moninf[i].x + moninf[i].width, moninf[i].y + moninf[i].height - insets->bottom,
insets->bottom != 0 ? BarrierNegativeY : 0, 0, NULL
);
}
XRRFreeMonitors(moninf);
XSync(dpy, False);
return barriers;
}
void
destroy(Display *dpy, PointerBarrier *barriers, int num)
{
int i;
for (i = 0; i < num; i++)
{
XFixesDestroyPointerBarrier(dpy, barriers[i]);
if (verbose)
fprintf(stderr, __NAME__": - Destroyed barrier %lu\n", barriers[i]);
}
free(barriers);
XSync(dpy, False);
}
void
handle_sigusr1(int dummy)
{
(void)dummy;
do_toggle = true;
}
bool
read_katria_insets(Display *dpy, Window root, struct Insets *insets)
{
Atom da;
int di;
unsigned char *prop_ret = NULL;
unsigned long nitems, dl;
int try;
for (try = 0; try < 60; try++)
{
if (XGetWindowProperty(dpy, root,
XInternAtom(dpy, "_KATRIA_INSETS", False),
0, 4, False, XA_INTEGER, &da, &di, &nitems,
&dl, &prop_ret)
== Success && nitems == 4 && prop_ret != NULL)
{
/* Truncation might occur, checked below. */
insets->top = ((unsigned long *)prop_ret)[0];
insets->left = ((unsigned long *)prop_ret)[1];
insets->right = ((unsigned long *)prop_ret)[2];
insets->bottom = ((unsigned long *)prop_ret)[3];
XFree(prop_ret);
return true;
}
if (verbose)
fprintf(stderr, __NAME__": Waiting for _KATRIA_INSETS ...\n");
sleep(1);
}
return false;
}
int
main(int argc, char **argv)
{
Display *dpy;
int fixes_opcode, fixes_event_base, fixes_error_base;
int screen;
Window root;
XEvent ev;
XConfigureEvent *cev;
struct Insets insets;
PointerBarrier *barriers = NULL;
int barriers_num;
struct sigaction sa;
fd_set fds;
int xfd;
bool barriers_active = true;
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
fprintf(stderr, __NAME__": Cannot open display\n");
exit(EXIT_FAILURE);
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
if (argc > 0 && strncmp(argv[argc - 1], "-v", 2) == 0)
verbose = true;
if (argc >= 2 && argc < 5 && strncmp(argv[1], "-k", 2) == 0)
{
if (!read_katria_insets(dpy, root, &insets))
{
fprintf(stderr, __NAME__": Could not read _KATRIA_INSETS\n");
exit(EXIT_FAILURE);
}
}
else if (argc >= 5)
{
insets.top = atoi(argv[1]);
insets.left = atoi(argv[2]);
insets.right = atoi(argv[3]);
insets.bottom = atoi(argv[4]);
}
else
{
fprintf(stderr, "Usage: "__NAME__
" [-k | <top> <left> <right> <bottom>] [-v]\n");
exit(EXIT_FAILURE);
}
if (insets.top < 0 || insets.left < 0 || insets.right < 0 || insets.bottom < 0)
{
fprintf(stderr, __NAME__": Negative insets are invalid\n");
exit(EXIT_FAILURE);
}
if (verbose)
fprintf(stderr, __NAME__": Insets: top %d, left %d, right %d, bottom %d\n",
insets.top, insets.left, insets.right, insets.bottom);
if (!XQueryExtension(dpy, "XFIXES", &fixes_opcode, &fixes_event_base,
&fixes_error_base))
{
fprintf(stderr, __NAME__": No XFIXES extension available\n");
exit(EXIT_FAILURE);
}
/* Note: SA_RESTART is not set, which means that syscalls will
* return with errno = EINTR when a signal is sent. This is crucial. */
sa.sa_handler = handle_sigusr1;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGUSR1, &sa, NULL) == -1)
{
perror("Cannot set up handler for SIGUSR1");
exit(EXIT_FAILURE);
}
/* The xlib docs say: On a POSIX system, the connection number is
* the file descriptor associated with the connection. */
xfd = ConnectionNumber(dpy);
barriers = create(dpy, root, &insets, &barriers_num);
/* Selecting for StructureNotifyMask will advise the X server to
* send us ConfigureNotify events when the size of the root window
* changes */
XSelectInput(dpy, root, StructureNotifyMask);
XSync(dpy, False);
for (;;)
{
FD_ZERO(&fds);
FD_SET(xfd, &fds);
if (select(xfd + 1, &fds, NULL, NULL, NULL) == -1 && errno != EINTR)
{
perror(__NAME__": select() returned with error");
exit(EXIT_FAILURE);
}
while (XPending(dpy))
{
XNextEvent(dpy, &ev);
if (ev.type == ConfigureNotify)
{
cev = &ev.xconfigure;
if (verbose)
fprintf(stderr, __NAME__": Got ConfigureNotify, size %dx%d\n",
cev->width, cev->height);
if (barriers != NULL)
destroy(dpy, barriers, barriers_num);
if (barriers_active)
barriers = create(dpy, root, &insets, &barriers_num);
else
barriers = NULL;
}
}
if (do_toggle)
{
if (verbose)
fprintf(stderr, __NAME__": Received signal, toggling\n");
do_toggle = false;
barriers_active = !barriers_active;
if (barriers != NULL)
destroy(dpy, barriers, barriers_num);
if (barriers_active)
barriers = create(dpy, root, &insets, &barriers_num);
else
barriers = NULL;
}
}
exit(EXIT_SUCCESS);
}