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