JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied sanders patch
[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                                 switch(ev->button) {
113                                 case Button1:
114                                         if(ev->state & MODKEY)
115                                                 tag(&a);
116                                         else
117                                                 view(&a);
118                                         break;
119                                 case Button3:
120                                         if(ev->state & MODKEY)
121                                                 toggletag(&a);
122                                         else
123                                                 toggleview(&a);
124                                         break;
125                                 }
126                                 return;
127                         }
128                 }
129                 if(ev->x < x + bmw) {
130                         if(ev->button == Button1)
131                                 togglemode(NULL);
132                 }
133         }
134         else if((c = getclient(ev->window))) {
135                 focus(c);
136                 if(CLEANMASK(ev->state) != MODKEY)
137                         return;
138                 switch(ev->button) {
139                 default:
140                         break;
141                 case Button1:
142                         if(!c->ismax && (arrange == dofloat || c->isfloat)) {
143                                 restack(c);
144                                 movemouse(c);
145                         }
146                         break;
147                 case Button2:
148                         zoom(NULL);
149                         break;
150                 case Button3:
151                         if(!c->ismax && (arrange == dofloat || c->isfloat)) {
152                                 restack(c);
153                                 resizemouse(c);
154                         }
155                         break;
156                 }
157         }
158 }
159
160 static void
161 synconfig(Client *c, int x, int y, int w, int h, unsigned int border)
162 {
163         XEvent synev;
164
165         synev.type = ConfigureNotify;
166         synev.xconfigure.display = dpy;
167         synev.xconfigure.event = c->win;
168         synev.xconfigure.window = c->win;
169         synev.xconfigure.x = x;
170         synev.xconfigure.y = y;
171         synev.xconfigure.width = w;
172         synev.xconfigure.height = h;
173         synev.xconfigure.border_width = border;
174         synev.xconfigure.above = None;
175         XSendEvent(dpy, c->win, True, NoEventMask, &synev);
176 }
177
178 static void
179 configurerequest(XEvent *e)
180 {
181         unsigned long newmask;
182         Client *c;
183         XConfigureRequestEvent *ev = &e->xconfigurerequest;
184         XWindowChanges wc;
185
186         if((c = getclient(ev->window))) {
187                 if(!c->isfloat && (arrange != dofloat) && c->ismax) {
188                         synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
189                         XSync(dpy, False);
190                         return;
191                 }
192                 gravitate(c, True);
193                 if(ev->value_mask & CWX)
194                         c->x = ev->x;
195                 if(ev->value_mask & CWY)
196                         c->y = ev->y;
197                 if(ev->value_mask & CWWidth)
198                         c->w = ev->width;
199                 if(ev->value_mask & CWHeight)
200                         c->h = ev->height;
201                 if(ev->value_mask & CWBorderWidth)
202                         c->border = ev->border_width;
203                 gravitate(c, False);
204                 wc.x = c->x;
205                 wc.y = c->y;
206                 wc.width = c->w;
207                 wc.height = c->h;
208                 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
209                 if(newmask)
210                         XConfigureWindow(dpy, c->win, newmask, &wc);
211                 else
212                         synconfig(c, c->x, c->y, c->w, c->h, c->border);
213                 XSync(dpy, False);
214                 if(c->isfloat)
215                         resize(c, False, TopLeft);
216                 else
217                         arrange(NULL);
218         }
219         else {
220                 wc.x = ev->x;
221                 wc.y = ev->y;
222                 wc.width = ev->width;
223                 wc.height = ev->height;
224                 wc.border_width = ev->border_width;
225                 wc.sibling = ev->above;
226                 wc.stack_mode = ev->detail;
227                 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
228                 XSync(dpy, False);
229         }
230 }
231
232 static void
233 destroynotify(XEvent *e)
234 {
235         Client *c;
236         XDestroyWindowEvent *ev = &e->xdestroywindow;
237
238         if((c = getclient(ev->window)))
239                 unmanage(c);
240 }
241
242 static void
243 enternotify(XEvent *e)
244 {
245         Client *c;
246         XCrossingEvent *ev = &e->xcrossing;
247
248         if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
249                 return;
250
251         if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
252                 focus(c);
253         else if(ev->window == root) {
254                 issel = True;
255                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
256                 drawall();
257         }
258 }
259
260 static void
261 expose(XEvent *e)
262 {
263         Client *c;
264         XExposeEvent *ev = &e->xexpose;
265
266         if(ev->count == 0) {
267                 if(barwin == ev->window)
268                         drawstatus();
269                 else if((c = getctitle(ev->window)))
270                         drawtitle(c);
271         }
272 }
273
274 static void
275 keypress(XEvent *e)
276 {
277         static unsigned int len = sizeof(key) / sizeof(key[0]);
278         unsigned int i;
279         KeySym keysym;
280         XKeyEvent *ev = &e->xkey;
281
282         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
283         for(i = 0; i < len; i++) {
284                 if(keysym == key[i].keysym &&
285                                 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
286                 {
287                         if(key[i].func)
288                                 key[i].func(&key[i].arg);
289                         return;
290                 }
291         }
292 }
293
294 static void
295 leavenotify(XEvent *e)
296 {
297         XCrossingEvent *ev = &e->xcrossing;
298
299         if((ev->window == root) && !ev->same_screen) {
300                 issel = False;
301                 drawall();
302         }
303 }
304
305 static void
306 mappingnotify(XEvent *e)
307 {
308         XMappingEvent *ev = &e->xmapping;
309
310         XRefreshKeyboardMapping(ev);
311         if(ev->request == MappingKeyboard)
312                 grabkeys();
313 }
314
315 static void
316 maprequest(XEvent *e)
317 {
318         static XWindowAttributes wa;
319         XMapRequestEvent *ev = &e->xmaprequest;
320
321         if(!XGetWindowAttributes(dpy, ev->window, &wa))
322                 return;
323
324         if(wa.override_redirect) {
325                 XSelectInput(dpy, ev->window,
326                                 (StructureNotifyMask | PropertyChangeMask));
327                 return;
328         }
329
330         if(!getclient(ev->window))
331                 manage(ev->window, &wa);
332 }
333
334 static void
335 propertynotify(XEvent *e)
336 {
337         Client *c;
338         Window trans;
339         XPropertyEvent *ev = &e->xproperty;
340
341         if(ev->state == PropertyDelete)
342                 return; /* ignore */
343
344         if((c = getclient(ev->window))) {
345                 if(ev->atom == wmatom[WMProtocols]) {
346                         c->proto = getproto(c->win);
347                         return;
348                 }
349                 switch (ev->atom) {
350                         default: break;
351                         case XA_WM_TRANSIENT_FOR:
352                                 XGetTransientForHint(dpy, c->win, &trans);
353                                 if(!c->isfloat && (c->isfloat = (trans != 0)))
354                                         arrange(NULL);
355                                 break;
356                         case XA_WM_NORMAL_HINTS:
357                                 setsize(c);
358                                 break;
359                 }
360                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
361                         settitle(c);
362                         drawtitle(c);
363                 }
364         }
365 }
366
367 static void
368 unmapnotify(XEvent *e)
369 {
370         Client *c;
371         XUnmapEvent *ev = &e->xunmap;
372
373         if((c = getclient(ev->window)))
374                 unmanage(c);
375 }
376
377 /* extern */
378
379 void (*handler[LASTEvent]) (XEvent *) = {
380         [ButtonPress] = buttonpress,
381         [ConfigureRequest] = configurerequest,
382         [DestroyNotify] = destroynotify,
383         [EnterNotify] = enternotify,
384         [LeaveNotify] = leavenotify,
385         [Expose] = expose,
386         [KeyPress] = keypress,
387         [MappingNotify] = mappingnotify,
388         [MapRequest] = maprequest,
389         [PropertyNotify] = propertynotify,
390         [UnmapNotify] = unmapnotify
391 };
392
393 void
394 grabkeys()
395 {
396         static unsigned int len = sizeof(key) / sizeof(key[0]);
397         unsigned int i;
398         KeyCode code;
399
400         XUngrabKey(dpy, AnyKey, AnyModifier, root);
401         for(i = 0; i < len; i++) {
402                 code = XKeysymToKeycode(dpy, key[i].keysym);
403                 XGrabKey(dpy, code, key[i].mod, root, True,
404                                 GrabModeAsync, GrabModeAsync);
405                 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
406                                 GrabModeAsync, GrabModeAsync);
407                 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
408                                 GrabModeAsync, GrabModeAsync);
409                 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
410                                 GrabModeAsync, GrabModeAsync);
411         }
412 }
413
414 void
415 procevent()
416 {
417         XEvent ev;
418
419         while(XPending(dpy)) {
420                 XNextEvent(dpy, &ev);
421                 if(handler[ev.type])
422                         (handler[ev.type])(&ev); /* call handler */
423         }
424 }
425