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