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