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