-
Notifications
You must be signed in to change notification settings - Fork 30
/
dwm-banish-cursorwarp-6.4_full.diff
411 lines (386 loc) · 10.7 KB
/
dwm-banish-cursorwarp-6.4_full.diff
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
From fdd7f85a71dd137e9e3ea313ce8c1588ba61d51a Mon Sep 17 00:00:00 2001
From: Bakkeby <bakkeby@gmail.com>
Date: Mon, 24 Jun 2024 09:58:13 +0200
Subject: [PATCH 1/2] Banish patch - behaviour similiar to xbanish and
unclutter
---
config.mk | 6 ++-
dwm.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 149 insertions(+), 7 deletions(-)
diff --git a/config.mk b/config.mk
index ef8acf7..ae0a7f3 100644
--- a/config.mk
+++ b/config.mk
@@ -14,6 +14,10 @@ X11LIB = /usr/X11R6/lib
XINERAMALIBS = -lXinerama
XINERAMAFLAGS = -DXINERAMA
+# Banish: dependency on libxi and libxfixes for mouse related features
+XINPUTLIBS = -lXi
+XFIXESLIBS = -lXfixes
+
# freetype
FREETYPELIBS = -lfontconfig -lXft
FREETYPEINC = /usr/include/freetype2
@@ -23,7 +27,7 @@ FREETYPEINC = /usr/include/freetype2
# includes and libs
INCS = -I${X11INC} -I${FREETYPEINC}
-LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
+LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} ${XINPUTLIBS} ${XFIXESLIBS}
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
diff --git a/dwm.c b/dwm.c
index e5efb6a..bb7760b 100644
--- a/dwm.c
+++ b/dwm.c
@@ -30,6 +30,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <time.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
@@ -39,6 +40,8 @@
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
+#include <X11/extensions/Xfixes.h>
+#include <X11/extensions/XInput2.h>
#include <X11/Xft/Xft.h>
#include "drw.h"
@@ -49,6 +52,8 @@
#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
#define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
* MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
+#define INTERSECTC(X,Y,W,H,Z) (MAX(0, MIN((X)+(W),(Z)->x+(Z)->w) - MAX((X),(Z)->x)) \
+ * MAX(0, MIN((Y)+(H),(Z)->y+(Z)->h) - MAX((Y),(Z)->y)))
#define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
@@ -169,12 +174,14 @@ static void focus(Client *c);
static void focusin(XEvent *e);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
+static void genericevent(XEvent *e);
static Atom getatomprop(Client *c, Atom prop);
static int getrootptr(int *x, int *y);
static long getstate(Window w);
static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void grabbuttons(Client *c, int focused);
static void grabkeys(void);
+static void hidecursor(const Arg *arg);
static void incnmaster(const Arg *arg);
static void keypress(XEvent *e);
static void killclient(const Arg *arg);
@@ -185,9 +192,11 @@ static void monocle(Monitor *m);
static void motionnotify(XEvent *e);
static void movemouse(const Arg *arg);
static Client *nexttiled(Client *c);
+static unsigned long long now(void);
static void pop(Client *c);
static void propertynotify(XEvent *e);
static void quit(const Arg *arg);
+static Client *recttoclient(int x, int y, int w, int h, int include_floating);
static Monitor *recttomon(int x, int y, int w, int h);
static void resize(Client *c, int x, int y, int w, int h, int interact);
static void resizeclient(Client *c, int x, int y, int w, int h);
@@ -204,6 +213,7 @@ static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setup(void);
static void seturgent(Client *c, int urg);
+static void showcursor(const Arg *arg);
static void showhide(Client *c);
static void sigchld(int unused);
static void spawn(const Arg *arg);
@@ -244,6 +254,14 @@ static int bh; /* bar height */
static int lrpad; /* sum of left and right padding for text */
static int (*xerrorxlib)(Display *, XErrorEvent *);
static unsigned int numlockmask = 0;
+static int cursor_hidden = 0;
+/* mouse_x and mouse_y are used to store the actual co-ordinates of the mouse cursor when
+ * hidden. These can be manipulated freely, e.g. when using the warp patch, to set a new
+ * cursor position for when the cursor is to be revealed again. */
+static int mouse_x = 0;
+static int mouse_y = 0;
+static int xi_opcode;
+static unsigned long long last_button_press = 0;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
@@ -252,6 +270,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
+ [GenericEvent] = genericevent,
[FocusIn] = focusin,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
@@ -432,7 +451,20 @@ buttonpress(XEvent *e)
selmon = m;
focus(NULL);
}
- if (ev->window == selmon->barwin) {
+
+ c = wintoclient(ev->window);
+
+ if (!c && cursor_hidden) {
+ c = recttoclient(mouse_x, mouse_y, 1, 1, 1);
+ showcursor(NULL);
+ }
+
+ if (c) {
+ focus(c);
+ restack(selmon);
+ XAllowEvents(dpy, ReplayPointer, CurrentTime);
+ click = ClkClientWin;
+ } else if (ev->window == selmon->barwin) {
i = x = 0;
do
x += TEXTW(tags[i]);
@@ -446,16 +478,14 @@ buttonpress(XEvent *e)
click = ClkStatusText;
else
click = ClkWinTitle;
- } else if ((c = wintoclient(ev->window))) {
- focus(c);
- restack(selmon);
- XAllowEvents(dpy, ReplayPointer, CurrentTime);
- click = ClkClientWin;
}
+
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
+
+ last_button_press = now();
}
void
@@ -765,6 +795,9 @@ enternotify(XEvent *e)
Monitor *m;
XCrossingEvent *ev = &e->xcrossing;
+ if (cursor_hidden)
+ return;
+
if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
return;
c = wintoclient(ev->window);
@@ -862,6 +895,36 @@ focusstack(const Arg *arg)
}
}
+void
+genericevent(XEvent *e)
+{
+ if (e->xcookie.extension != xi_opcode)
+ return;
+
+ if (!XGetEventData(dpy, &e->xcookie))
+ return;
+
+ switch (e->xcookie.evtype) {
+ case XI_RawMotion:
+ if (cursor_hidden)
+ showcursor(NULL);
+ break;
+ case XI_RawTouchBegin:
+ case XI_RawTouchEnd:
+ case XI_RawTouchUpdate:
+ if (!cursor_hidden)
+ hidecursor(NULL);
+ break;
+ case XI_RawKeyRelease:
+ if (now() - last_button_press > 2000 && !cursor_hidden) {
+ hidecursor(NULL);
+ }
+ break;
+ }
+
+ XFreeEventData(dpy, &e->xcookie);
+}
+
Atom
getatomprop(Client *c, Atom prop)
{
@@ -885,6 +948,12 @@ getrootptr(int *x, int *y)
unsigned int dui;
Window dummy;
+ if (cursor_hidden) {
+ *x = mouse_x;
+ *y = mouse_y;
+ return 1;
+ }
+
return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
}
@@ -968,6 +1037,20 @@ grabkeys(void)
}
}
+void
+hidecursor(const Arg *arg)
+{
+ if (cursor_hidden)
+ return;
+
+ XFixesHideCursor(dpy, root);
+ if (getrootptr(&mouse_x, &mouse_y)) {
+ XWarpPointer(dpy, None, root, 0, 0, 0, 0, selmon->mx + selmon->mw, selmon->my);
+ }
+
+ cursor_hidden = 1;
+}
+
void
incnmaster(const Arg *arg)
{
@@ -1200,6 +1283,13 @@ nexttiled(Client *c)
return c;
}
+unsigned long long
+now(void) {
+ struct timespec currentTime;
+ clock_gettime(CLOCK_REALTIME, ¤tTime);
+ return currentTime.tv_sec * 1000LL + currentTime.tv_nsec / 1000000LL;
+}
+
void
pop(Client *c)
{
@@ -1252,6 +1342,23 @@ quit(const Arg *arg)
running = 0;
}
+Client *
+recttoclient(int x, int y, int w, int h, int include_floating)
+{
+ Client *c, *r = NULL;
+ int a, area = 1;
+
+ for (c = selmon->stack; c; c = c->snext) {
+ if (!ISVISIBLE(c) || (c->isfloating && !include_floating))
+ continue;
+ if ((a = INTERSECTC(x, y, w, h, c)) >= area) {
+ area = a;
+ r = c;
+ }
+ }
+ return r;
+}
+
Monitor *
recttomon(int x, int y, int w, int h)
{
@@ -1593,6 +1700,25 @@ setup(void)
|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
XSelectInput(dpy, root, wa.event_mask);
+
+ if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &i, &i)) {
+ fprintf(stderr, "Warning: XInput is not available.");
+ }
+ /* Tell XInput to send us all RawMotion events. */
+ unsigned char mask_bytes[XIMaskLen(XI_LASTEVENT)];
+ memset(mask_bytes, 0, sizeof(mask_bytes));
+ XISetMask(mask_bytes, XI_RawMotion);
+ XISetMask(mask_bytes, XI_RawKeyRelease);
+ XISetMask(mask_bytes, XI_RawTouchBegin);
+ XISetMask(mask_bytes, XI_RawTouchEnd);
+ XISetMask(mask_bytes, XI_RawTouchUpdate);
+
+ XIEventMask mask;
+ mask.deviceid = XIAllMasterDevices;
+ mask.mask_len = sizeof(mask_bytes);
+ mask.mask = mask_bytes;
+ XISelectEvents(dpy, root, &mask, 1);
+
grabkeys();
focus(NULL);
}
@@ -1610,6 +1736,18 @@ seturgent(Client *c, int urg)
XFree(wmh);
}
+void
+showcursor(const Arg *arg)
+{
+ if (!cursor_hidden)
+ return;
+
+ XWarpPointer(dpy, None, root, 0, 0, 0, 0, mouse_x, mouse_y);
+ XFixesShowCursor(dpy, root);
+
+ cursor_hidden = 0;
+}
+
void
showhide(Client *c)
{
--
2.45.2
From 2d9dd6a13f402f61e890cbbac4234f7c821bd1de Mon Sep 17 00:00:00 2001
From: Bakkeby <bakkeby@gmail.com>
Date: Mon, 24 Jun 2024 09:59:29 +0200
Subject: [PATCH 2/2] Adding cursorwarp patch on top of banish
---
dwm.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/dwm.c b/dwm.c
index bb7760b..1a8e311 100644
--- a/dwm.c
+++ b/dwm.c
@@ -238,6 +238,7 @@ static void updatetitle(Client *c);
static void updatewindowtype(Client *c);
static void updatewmhints(Client *c);
static void view(const Arg *arg);
+static void warp(Client *c);
static Client *wintoclient(Window w);
static Monitor *wintomon(Window w);
static int xerror(Display *dpy, XErrorEvent *ee);
@@ -867,6 +868,8 @@ focusmon(const Arg *arg)
unfocus(selmon->sel, 0);
selmon = m;
focus(NULL);
+ if (m->sel)
+ warp(m->sel)
}
void
@@ -892,6 +895,7 @@ focusstack(const Arg *arg)
if (c) {
focus(c);
restack(selmon);
+ warp(c)
}
}
@@ -1159,6 +1163,8 @@ manage(Window w, XWindowAttributes *wa)
c->mon->sel = c;
arrange(c->mon);
XMapWindow(dpy, c->win);
+ if (c && c->mon == selmon && ISVISIBLE(c))
+ warp(c);
focus(NULL);
}
@@ -1919,6 +1925,8 @@ unmanage(Client *c, int destroyed)
focus(NULL);
updateclientlist();
arrange(m);
+ if (m->sel && m == selmon)
+ warp(m->sel);
}
void
@@ -2182,6 +2190,18 @@ view(const Arg *arg)
arrange(selmon);
}
+void
+warp(Client *c)
+{
+ if (cursor_hidden) {
+ mouse_x = c->x + c->w/2;
+ mouse_y = c->y + c->h/2;
+ return;
+ }
+
+ XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2);
+}
+
Client *
wintoclient(Window w)
{
--
2.45.2