rev |
line source |
garbeam@5
|
1 /*
|
garbeam@5
|
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@5
|
3 * See LICENSE file for license details.
|
garbeam@5
|
4 */
|
garbeam@76
|
5 #include "dwm.h"
|
garbeam@5
|
6 #include <stdlib.h>
|
garbeam@5
|
7 #include <X11/keysym.h>
|
garbeam@13
|
8 #include <X11/Xatom.h>
|
garbeam@5
|
9
|
arg@146
|
10 /* static */
|
arg@114
|
11
|
arg@114
|
12 typedef struct {
|
arg@114
|
13 unsigned long mod;
|
arg@114
|
14 KeySym keysym;
|
arg@114
|
15 void (*func)(Arg *arg);
|
arg@114
|
16 Arg arg;
|
arg@114
|
17 } Key;
|
arg@114
|
18
|
arg@146
|
19 KEYS
|
garbeam@75
|
20
|
arg@291
|
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
garbeam@5
|
22
|
garbeam@77
|
23 static void
|
arg@476
|
24 synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
|
arg@476
|
25 XEvent synev;
|
arg@476
|
26
|
arg@476
|
27 synev.type = ConfigureNotify;
|
arg@476
|
28 synev.xconfigure.display = dpy;
|
arg@476
|
29 synev.xconfigure.event = c->win;
|
arg@476
|
30 synev.xconfigure.window = c->win;
|
arg@476
|
31 synev.xconfigure.x = x;
|
arg@476
|
32 synev.xconfigure.y = y;
|
arg@476
|
33 synev.xconfigure.width = w;
|
arg@476
|
34 synev.xconfigure.height = h;
|
arg@476
|
35 synev.xconfigure.border_width = border;
|
arg@476
|
36 synev.xconfigure.above = None;
|
arg@476
|
37 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
|
arg@476
|
38 }
|
arg@476
|
39
|
arg@476
|
40 static void
|
arg@461
|
41 movemouse(Client *c) {
|
garbeam@77
|
42 int x1, y1, ocx, ocy, di;
|
garbeam@77
|
43 unsigned int dui;
|
garbeam@77
|
44 Window dummy;
|
arg@123
|
45 XEvent ev;
|
garbeam@77
|
46
|
arg@115
|
47 ocx = c->x;
|
arg@115
|
48 ocy = c->y;
|
arg@148
|
49 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
arg@123
|
50 None, cursor[CurMove], CurrentTime) != GrabSuccess)
|
garbeam@77
|
51 return;
|
arg@482
|
52 c->ismax = False;
|
garbeam@77
|
53 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
|
garbeam@77
|
54 for(;;) {
|
arg@476
|
55 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
|
garbeam@77
|
56 switch (ev.type) {
|
arg@461
|
57 default:
|
arg@461
|
58 break;
|
arg@476
|
59 case ConfigureRequest:
|
arg@476
|
60 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
|
arg@476
|
61 XSync(dpy, False);
|
arg@476
|
62 break;
|
garbeam@77
|
63 case Expose:
|
garbeam@77
|
64 handler[Expose](&ev);
|
garbeam@77
|
65 break;
|
garbeam@77
|
66 case MotionNotify:
|
garbeam@79
|
67 XSync(dpy, False);
|
arg@115
|
68 c->x = ocx + (ev.xmotion.x - x1);
|
arg@115
|
69 c->y = ocy + (ev.xmotion.y - y1);
|
arg@99
|
70 resize(c, False, TopLeft);
|
garbeam@77
|
71 break;
|
garbeam@77
|
72 case ButtonRelease:
|
garbeam@77
|
73 XUngrabPointer(dpy, CurrentTime);
|
garbeam@77
|
74 return;
|
arg@476
|
75 case DestroyNotify:
|
arg@476
|
76 case UnmapNotify:
|
arg@476
|
77 XUngrabPointer(dpy, CurrentTime);
|
arg@476
|
78 handler[ev.type](&ev);
|
arg@476
|
79 return;
|
garbeam@77
|
80 }
|
garbeam@77
|
81 }
|
garbeam@77
|
82 }
|
garbeam@77
|
83
|
garbeam@77
|
84 static void
|
arg@461
|
85 resizemouse(Client *c) {
|
garbeam@77
|
86 int ocx, ocy;
|
arg@268
|
87 int nw, nh;
|
arg@99
|
88 Corner sticky;
|
arg@123
|
89 XEvent ev;
|
garbeam@77
|
90
|
arg@115
|
91 ocx = c->x;
|
arg@115
|
92 ocy = c->y;
|
arg@148
|
93 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
garbeam@77
|
94 None, cursor[CurResize], CurrentTime) != GrabSuccess)
|
garbeam@77
|
95 return;
|
arg@482
|
96 c->ismax = False;
|
arg@115
|
97 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
|
garbeam@77
|
98 for(;;) {
|
arg@476
|
99 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
|
garbeam@77
|
100 switch(ev.type) {
|
arg@461
|
101 default:
|
arg@461
|
102 break;
|
arg@476
|
103 case ConfigureRequest:
|
arg@476
|
104 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
|
arg@476
|
105 XSync(dpy, False);
|
arg@476
|
106 break;
|
garbeam@77
|
107 case Expose:
|
garbeam@77
|
108 handler[Expose](&ev);
|
garbeam@77
|
109 break;
|
garbeam@77
|
110 case MotionNotify:
|
garbeam@79
|
111 XSync(dpy, False);
|
arg@268
|
112 if((nw = abs(ocx - ev.xmotion.x)))
|
arg@478
|
113 c->w = nw;
|
arg@268
|
114 if((nh = abs(ocy - ev.xmotion.y)))
|
arg@478
|
115 c->h = nh;
|
arg@115
|
116 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
|
arg@115
|
117 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
|
arg@105
|
118 if(ocx <= ev.xmotion.x)
|
arg@105
|
119 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
|
arg@105
|
120 else
|
arg@105
|
121 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
|
arg@99
|
122 resize(c, True, sticky);
|
garbeam@77
|
123 break;
|
garbeam@77
|
124 case ButtonRelease:
|
garbeam@77
|
125 XUngrabPointer(dpy, CurrentTime);
|
garbeam@77
|
126 return;
|
arg@476
|
127 case DestroyNotify:
|
arg@476
|
128 case UnmapNotify:
|
arg@476
|
129 XUngrabPointer(dpy, CurrentTime);
|
arg@476
|
130 handler[ev.type](&ev);
|
arg@476
|
131 return;
|
garbeam@77
|
132 }
|
garbeam@77
|
133 }
|
garbeam@77
|
134 }
|
garbeam@73
|
135
|
garbeam@73
|
136 static void
|
arg@461
|
137 buttonpress(XEvent *e) {
|
garbeam@73
|
138 int x;
|
garbeam@73
|
139 Arg a;
|
arg@123
|
140 Client *c;
|
garbeam@18
|
141 XButtonPressedEvent *ev = &e->xbutton;
|
garbeam@18
|
142
|
garbeam@73
|
143 if(barwin == ev->window) {
|
arg@362
|
144 x = 0;
|
arg@362
|
145 for(a.i = 0; a.i < ntags; a.i++) {
|
arg@362
|
146 x += textw(tags[a.i]);
|
arg@362
|
147 if(ev->x < x) {
|
arg@399
|
148 if(ev->button == Button1) {
|
arg@398
|
149 if(ev->state & MODKEY)
|
arg@398
|
150 tag(&a);
|
arg@398
|
151 else
|
arg@398
|
152 view(&a);
|
arg@399
|
153 }
|
arg@399
|
154 else if(ev->button == Button3) {
|
arg@398
|
155 if(ev->state & MODKEY)
|
arg@398
|
156 toggletag(&a);
|
arg@398
|
157 else
|
arg@398
|
158 toggleview(&a);
|
arg@394
|
159 }
|
arg@362
|
160 return;
|
garbeam@73
|
161 }
|
garbeam@73
|
162 }
|
arg@371
|
163 if(ev->x < x + bmw) {
|
arg@371
|
164 if(ev->button == Button1)
|
arg@371
|
165 togglemode(NULL);
|
arg@371
|
166 }
|
garbeam@73
|
167 }
|
garbeam@58
|
168 else if((c = getclient(ev->window))) {
|
arg@143
|
169 focus(c);
|
arg@473
|
170 if(CLEANMASK(ev->state) != MODKEY)
|
arg@318
|
171 return;
|
arg@400
|
172 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
|
arg@487
|
173 restack();
|
arg@399
|
174 movemouse(c);
|
arg@399
|
175 }
|
arg@399
|
176 else if(ev->button == Button2)
|
arg@248
|
177 zoom(NULL);
|
arg@400
|
178 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
|
arg@487
|
179 restack();
|
arg@399
|
180 resizemouse(c);
|
garbeam@18
|
181 }
|
garbeam@18
|
182 }
|
garbeam@18
|
183 }
|
garbeam@18
|
184
|
garbeam@18
|
185 static void
|
arg@461
|
186 configurerequest(XEvent *e) {
|
arg@286
|
187 unsigned long newmask;
|
arg@123
|
188 Client *c;
|
garbeam@5
|
189 XConfigureRequestEvent *ev = &e->xconfigurerequest;
|
garbeam@5
|
190 XWindowChanges wc;
|
garbeam@5
|
191
|
garbeam@18
|
192 if((c = getclient(ev->window))) {
|
arg@488
|
193 c->ismax = False;
|
garbeam@29
|
194 gravitate(c, True);
|
arg@195
|
195 if(ev->value_mask & CWX)
|
arg@195
|
196 c->x = ev->x;
|
arg@195
|
197 if(ev->value_mask & CWY)
|
arg@195
|
198 c->y = ev->y;
|
arg@195
|
199 if(ev->value_mask & CWWidth)
|
arg@195
|
200 c->w = ev->width;
|
arg@195
|
201 if(ev->value_mask & CWHeight)
|
arg@195
|
202 c->h = ev->height;
|
garbeam@29
|
203 if(ev->value_mask & CWBorderWidth)
|
arg@164
|
204 c->border = ev->border_width;
|
garbeam@29
|
205 gravitate(c, False);
|
arg@164
|
206 wc.x = c->x;
|
arg@164
|
207 wc.y = c->y;
|
arg@164
|
208 wc.width = c->w;
|
arg@164
|
209 wc.height = c->h;
|
arg@164
|
210 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
|
arg@164
|
211 if(newmask)
|
arg@164
|
212 XConfigureWindow(dpy, c->win, newmask, &wc);
|
arg@387
|
213 else
|
arg@387
|
214 synconfig(c, c->x, c->y, c->w, c->h, c->border);
|
arg@195
|
215 XSync(dpy, False);
|
arg@385
|
216 if(c->isfloat)
|
arg@385
|
217 resize(c, False, TopLeft);
|
arg@196
|
218 else
|
arg@196
|
219 arrange(NULL);
|
garbeam@5
|
220 }
|
arg@164
|
221 else {
|
arg@164
|
222 wc.x = ev->x;
|
arg@164
|
223 wc.y = ev->y;
|
arg@164
|
224 wc.width = ev->width;
|
arg@164
|
225 wc.height = ev->height;
|
arg@164
|
226 wc.border_width = ev->border_width;
|
arg@164
|
227 wc.sibling = ev->above;
|
arg@164
|
228 wc.stack_mode = ev->detail;
|
arg@164
|
229 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
|
arg@195
|
230 XSync(dpy, False);
|
arg@164
|
231 }
|
garbeam@5
|
232 }
|
garbeam@5
|
233
|
garbeam@5
|
234 static void
|
arg@461
|
235 destroynotify(XEvent *e) {
|
garbeam@5
|
236 Client *c;
|
garbeam@5
|
237 XDestroyWindowEvent *ev = &e->xdestroywindow;
|
garbeam@5
|
238
|
garbeam@11
|
239 if((c = getclient(ev->window)))
|
garbeam@11
|
240 unmanage(c);
|
garbeam@5
|
241 }
|
garbeam@5
|
242
|
garbeam@5
|
243 static void
|
arg@461
|
244 enternotify(XEvent *e) {
|
arg@123
|
245 Client *c;
|
garbeam@5
|
246 XCrossingEvent *ev = &e->xcrossing;
|
garbeam@5
|
247
|
arg@232
|
248 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
|
garbeam@5
|
249 return;
|
garbeam@5
|
250
|
arg@466
|
251 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
|
garbeam@13
|
252 focus(c);
|
arg@239
|
253 else if(ev->window == root) {
|
garbeam@31
|
254 issel = True;
|
arg@239
|
255 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
|
arg@239
|
256 drawall();
|
arg@239
|
257 }
|
garbeam@5
|
258 }
|
garbeam@5
|
259
|
garbeam@5
|
260 static void
|
arg@461
|
261 expose(XEvent *e) {
|
arg@123
|
262 Client *c;
|
garbeam@5
|
263 XExposeEvent *ev = &e->xexpose;
|
garbeam@5
|
264
|
garbeam@5
|
265 if(ev->count == 0) {
|
garbeam@70
|
266 if(barwin == ev->window)
|
garbeam@74
|
267 drawstatus();
|
garbeam@75
|
268 else if((c = getctitle(ev->window)))
|
garbeam@74
|
269 drawtitle(c);
|
garbeam@5
|
270 }
|
garbeam@5
|
271 }
|
garbeam@5
|
272
|
garbeam@5
|
273 static void
|
arg@461
|
274 keypress(XEvent *e) {
|
arg@138
|
275 static unsigned int len = sizeof(key) / sizeof(key[0]);
|
garbeam@76
|
276 unsigned int i;
|
garbeam@76
|
277 KeySym keysym;
|
arg@123
|
278 XKeyEvent *ev = &e->xkey;
|
garbeam@76
|
279
|
garbeam@76
|
280 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
|
arg@275
|
281 for(i = 0; i < len; i++) {
|
arg@461
|
282 if(keysym == key[i].keysym
|
arg@461
|
283 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
|
arg@275
|
284 {
|
garbeam@76
|
285 if(key[i].func)
|
garbeam@76
|
286 key[i].func(&key[i].arg);
|
garbeam@76
|
287 return;
|
garbeam@76
|
288 }
|
arg@275
|
289 }
|
garbeam@76
|
290 }
|
garbeam@76
|
291
|
garbeam@76
|
292 static void
|
arg@461
|
293 leavenotify(XEvent *e) {
|
garbeam@76
|
294 XCrossingEvent *ev = &e->xcrossing;
|
garbeam@76
|
295
|
arg@239
|
296 if((ev->window == root) && !ev->same_screen) {
|
arg@239
|
297 issel = False;
|
arg@239
|
298 drawall();
|
arg@239
|
299 }
|
garbeam@76
|
300 }
|
garbeam@76
|
301
|
garbeam@76
|
302 static void
|
arg@461
|
303 mappingnotify(XEvent *e) {
|
arg@279
|
304 XMappingEvent *ev = &e->xmapping;
|
arg@279
|
305
|
arg@279
|
306 XRefreshKeyboardMapping(ev);
|
arg@279
|
307 if(ev->request == MappingKeyboard)
|
arg@279
|
308 grabkeys();
|
arg@279
|
309 }
|
arg@279
|
310
|
arg@279
|
311 static void
|
arg@461
|
312 maprequest(XEvent *e) {
|
arg@123
|
313 static XWindowAttributes wa;
|
garbeam@5
|
314 XMapRequestEvent *ev = &e->xmaprequest;
|
garbeam@5
|
315
|
garbeam@5
|
316 if(!XGetWindowAttributes(dpy, ev->window, &wa))
|
garbeam@5
|
317 return;
|
garbeam@5
|
318
|
garbeam@5
|
319 if(wa.override_redirect) {
|
garbeam@5
|
320 XSelectInput(dpy, ev->window,
|
garbeam@5
|
321 (StructureNotifyMask | PropertyChangeMask));
|
garbeam@5
|
322 return;
|
garbeam@5
|
323 }
|
garbeam@5
|
324
|
garbeam@10
|
325 if(!getclient(ev->window))
|
garbeam@10
|
326 manage(ev->window, &wa);
|
garbeam@5
|
327 }
|
garbeam@5
|
328
|
garbeam@5
|
329 static void
|
arg@461
|
330 propertynotify(XEvent *e) {
|
arg@123
|
331 Client *c;
|
arg@123
|
332 Window trans;
|
garbeam@5
|
333 XPropertyEvent *ev = &e->xproperty;
|
garbeam@5
|
334
|
garbeam@5
|
335 if(ev->state == PropertyDelete)
|
garbeam@5
|
336 return; /* ignore */
|
garbeam@5
|
337
|
garbeam@13
|
338 if((c = getclient(ev->window))) {
|
garbeam@77
|
339 if(ev->atom == wmatom[WMProtocols]) {
|
garbeam@75
|
340 c->proto = getproto(c->win);
|
garbeam@30
|
341 return;
|
garbeam@30
|
342 }
|
garbeam@13
|
343 switch (ev->atom) {
|
garbeam@13
|
344 default: break;
|
garbeam@13
|
345 case XA_WM_TRANSIENT_FOR:
|
garbeam@53
|
346 XGetTransientForHint(dpy, c->win, &trans);
|
garbeam@80
|
347 if(!c->isfloat && (c->isfloat = (trans != 0)))
|
garbeam@53
|
348 arrange(NULL);
|
garbeam@13
|
349 break;
|
garbeam@13
|
350 case XA_WM_NORMAL_HINTS:
|
arg@454
|
351 updatesize(c);
|
garbeam@13
|
352 break;
|
garbeam@13
|
353 }
|
garbeam@77
|
354 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
|
arg@454
|
355 updatetitle(c);
|
garbeam@74
|
356 drawtitle(c);
|
garbeam@13
|
357 }
|
garbeam@13
|
358 }
|
garbeam@5
|
359 }
|
garbeam@5
|
360
|
garbeam@5
|
361 static void
|
arg@461
|
362 unmapnotify(XEvent *e) {
|
garbeam@5
|
363 Client *c;
|
garbeam@5
|
364 XUnmapEvent *ev = &e->xunmap;
|
garbeam@5
|
365
|
garbeam@10
|
366 if((c = getclient(ev->window)))
|
garbeam@10
|
367 unmanage(c);
|
garbeam@5
|
368 }
|
garbeam@76
|
369
|
garbeam@84
|
370 /* extern */
|
garbeam@76
|
371
|
garbeam@76
|
372 void (*handler[LASTEvent]) (XEvent *) = {
|
garbeam@76
|
373 [ButtonPress] = buttonpress,
|
garbeam@76
|
374 [ConfigureRequest] = configurerequest,
|
garbeam@76
|
375 [DestroyNotify] = destroynotify,
|
garbeam@76
|
376 [EnterNotify] = enternotify,
|
garbeam@76
|
377 [LeaveNotify] = leavenotify,
|
garbeam@76
|
378 [Expose] = expose,
|
garbeam@76
|
379 [KeyPress] = keypress,
|
arg@279
|
380 [MappingNotify] = mappingnotify,
|
garbeam@76
|
381 [MapRequest] = maprequest,
|
garbeam@76
|
382 [PropertyNotify] = propertynotify,
|
garbeam@76
|
383 [UnmapNotify] = unmapnotify
|
garbeam@76
|
384 };
|
garbeam@76
|
385
|
garbeam@76
|
386 void
|
arg@487
|
387 grabkeys(void) {
|
arg@138
|
388 static unsigned int len = sizeof(key) / sizeof(key[0]);
|
garbeam@76
|
389 unsigned int i;
|
garbeam@76
|
390 KeyCode code;
|
garbeam@76
|
391
|
arg@279
|
392 XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
garbeam@76
|
393 for(i = 0; i < len; i++) {
|
garbeam@76
|
394 code = XKeysymToKeycode(dpy, key[i].keysym);
|
garbeam@76
|
395 XGrabKey(dpy, code, key[i].mod, root, True,
|
garbeam@76
|
396 GrabModeAsync, GrabModeAsync);
|
arg@160
|
397 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
|
arg@160
|
398 GrabModeAsync, GrabModeAsync);
|
arg@291
|
399 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
|
arg@146
|
400 GrabModeAsync, GrabModeAsync);
|
arg@291
|
401 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
|
arg@146
|
402 GrabModeAsync, GrabModeAsync);
|
garbeam@76
|
403 }
|
garbeam@76
|
404 }
|
arg@292
|
405
|
arg@292
|
406 void
|
arg@487
|
407 procevent(void) {
|
arg@292
|
408 XEvent ev;
|
arg@292
|
409
|
arg@292
|
410 while(XPending(dpy)) {
|
arg@292
|
411 XNextEvent(dpy, &ev);
|
arg@292
|
412 if(handler[ev.type])
|
arg@292
|
413 (handler[ev.type])(&ev); /* call handler */
|
arg@292
|
414 }
|
arg@292
|
415 }
|