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