JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
some cleanups/fixes inspired by Jukka Salmi's feedback
[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
7 #include <stdlib.h>
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
10
11 #define ButtonMask      (ButtonPressMask | ButtonReleaseMask)
12 #define MouseMask       (ButtonMask | PointerMotionMask)
13
14 /* CUSTOMIZE */
15
16 typedef struct {
17         unsigned long mod;
18         KeySym keysym;
19         void (*func)(Arg *arg);
20         Arg arg;
21 } Key;
22
23 const char *browse[] = { "firefox", NULL };
24 const char *gimp[] = { "gimp", NULL };
25 const char *term[] = { 
26         "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
27         "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
28 };
29 const char *xlock[] = { "xlock", NULL };
30
31 static Key key[] = {
32         /* modifier             key             function        arguments */
33         { MODKEY,               XK_0,           view,           { .i = Tfnord } }, 
34         { MODKEY,               XK_1,           view,           { .i = Tdev } }, 
35         { MODKEY,               XK_2,           view,           { .i = Tnet } }, 
36         { MODKEY,               XK_3,           view,           { .i = Twork } }, 
37         { MODKEY,               XK_4,           view,           { .i = Tmisc} }, 
38         { MODKEY,               XK_j,           focusnext,      { 0 } }, 
39         { MODKEY,               XK_k,           focusprev,      { 0 } },
40         { MODKEY,               XK_m,           togglemax,      { 0 } }, 
41         { MODKEY,               XK_space,       togglemode,     { 0 } }, 
42         { MODKEY,               XK_Return,      zoom,           { 0 } },
43         { MODKEY|ControlMask,   XK_0,           appendtag,      { .i = Tfnord } }, 
44         { MODKEY|ControlMask,   XK_1,           appendtag,      { .i = Tdev } }, 
45         { MODKEY|ControlMask,   XK_2,           appendtag,      { .i = Tnet } }, 
46         { MODKEY|ControlMask,   XK_3,           appendtag,      { .i = Twork } }, 
47         { MODKEY|ControlMask,   XK_4,           appendtag,      { .i = Tmisc } }, 
48         { MODKEY|ShiftMask,     XK_0,           replacetag,     { .i = Tfnord } }, 
49         { MODKEY|ShiftMask,     XK_1,           replacetag,     { .i = Tdev } }, 
50         { MODKEY|ShiftMask,     XK_2,           replacetag,     { .i = Tnet } }, 
51         { MODKEY|ShiftMask,     XK_3,           replacetag,     { .i = Twork } }, 
52         { MODKEY|ShiftMask,     XK_4,           replacetag,     { .i = Tmisc } }, 
53         { MODKEY|ShiftMask,     XK_c,           killclient,     { 0 } }, 
54         { MODKEY|ShiftMask,     XK_g,           spawn,          { .argv = gimp } },
55         { MODKEY|ShiftMask,     XK_l,           spawn,          { .argv = xlock } },
56         { MODKEY|ShiftMask,     XK_q,           quit,           { 0 } },
57         { MODKEY|ShiftMask,     XK_w,           spawn,          { .argv = browse } },
58         { MODKEY|ShiftMask,     XK_Return,      spawn,          { .argv = term } },
59 };
60
61 /* END CUSTOMIZE */
62
63 /* static */
64
65 static void
66 movemouse(Client *c)
67 {
68         int x1, y1, ocx, ocy, di;
69         unsigned int dui;
70         Window dummy;
71         XEvent ev;
72
73         ocx = c->x;
74         ocy = c->y;
75         if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
76                         None, cursor[CurMove], CurrentTime) != GrabSuccess)
77                 return;
78         XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
79         for(;;) {
80                 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
81                 switch (ev.type) {
82                 default: break;
83                 case Expose:
84                         handler[Expose](&ev);
85                         break;
86                 case MotionNotify:
87                         XSync(dpy, False);
88                         c->x = ocx + (ev.xmotion.x - x1);
89                         c->y = ocy + (ev.xmotion.y - y1);
90                         resize(c, False, TopLeft);
91                         break;
92                 case ButtonRelease:
93                         XUngrabPointer(dpy, CurrentTime);
94                         return;
95                 }
96         }
97 }
98
99 static void
100 resizemouse(Client *c)
101 {
102         int ocx, ocy;
103         Corner sticky;
104         XEvent ev;
105
106         ocx = c->x;
107         ocy = c->y;
108         if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
109                                 None, cursor[CurResize], CurrentTime) != GrabSuccess)
110                 return;
111         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
112         for(;;) {
113                 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
114                 switch(ev.type) {
115                 default: break;
116                 case Expose:
117                         handler[Expose](&ev);
118                         break;
119                 case MotionNotify:
120                         XSync(dpy, False);
121                         c->w = abs(ocx - ev.xmotion.x);
122                         c->h = abs(ocy - ev.xmotion.y);
123                         c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
124                         c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
125                         if(ocx <= ev.xmotion.x)
126                                 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
127                         else
128                                 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
129                         resize(c, True, sticky);
130                         break;
131                 case ButtonRelease:
132                         XUngrabPointer(dpy, CurrentTime);
133                         return;
134                 }
135         }
136 }
137
138 static void
139 buttonpress(XEvent *e)
140 {
141         int x;
142         Arg a;
143         Client *c;
144         XButtonPressedEvent *ev = &e->xbutton;
145
146         if(barwin == ev->window) {
147                 switch(ev->button) {
148                 default:
149                         x = 0;
150                         for(a.i = 0; a.i < TLast; a.i++) {
151                                 x += textw(tags[a.i]);
152                                 if(ev->x < x) {
153                                         view(&a);
154                                         break;
155                                 }
156                         }
157                         break;
158                 case Button4:
159                         a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
160                         view(&a);
161                         break;
162                 case Button5:
163                         a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
164                         view(&a);
165                         break;
166                 }
167         }
168         else if((c = getclient(ev->window))) {
169                 switch(ev->button) {
170                 default:
171                         break;
172                 case Button1:
173                         if(!c->ismax && (arrange == dofloat || c->isfloat)) {
174                                 higher(c);
175                                 movemouse(c);
176                         }
177                         break;
178                 case Button2:
179                         lower(c);
180                         break;
181                 case Button3:
182                         if(!c->ismax && (arrange == dofloat || c->isfloat)) {
183                                 higher(c);
184                                 resizemouse(c);
185                         }
186                         break;
187                 }
188         }
189 }
190
191 static void
192 configurerequest(XEvent *e)
193 {
194         Client *c;
195         XConfigureRequestEvent *ev = &e->xconfigurerequest;
196         XWindowChanges wc;
197
198         ev->value_mask &= ~CWSibling;
199         if((c = getclient(ev->window))) {
200                 gravitate(c, True);
201                 if(ev->value_mask & CWX)
202                         c->x = ev->x;
203                 if(ev->value_mask & CWY)
204                         c->y = ev->y;
205                 if(ev->value_mask & CWWidth)
206                         c->w = ev->width;
207                 if(ev->value_mask & CWHeight)
208                         c->h = ev->height;
209                 if(ev->value_mask & CWBorderWidth)
210                         c->border = 1;
211                 gravitate(c, False);
212                 resize(c, True, TopLeft);
213         }
214
215         wc.x = ev->x;
216         wc.y = ev->y;
217         wc.width = ev->width;
218         wc.height = ev->height;
219         wc.border_width = 1;
220         wc.sibling = None;
221         wc.stack_mode = Above;
222         ev->value_mask &= ~CWStackMode;
223         ev->value_mask |= CWBorderWidth;
224         XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
225         XSync(dpy, False);
226 }
227
228 static void
229 destroynotify(XEvent *e)
230 {
231         Client *c;
232         XDestroyWindowEvent *ev = &e->xdestroywindow;
233
234         if((c = getclient(ev->window)))
235                 unmanage(c);
236 }
237
238 static void
239 enternotify(XEvent *e)
240 {
241         Client *c;
242         XCrossingEvent *ev = &e->xcrossing;
243
244         if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
245                 return;
246
247         if((c = getclient(ev->window)))
248                 focus(c);
249         else if(ev->window == root)
250                 issel = True;
251 }
252
253 static void
254 expose(XEvent *e)
255 {
256         Client *c;
257         XExposeEvent *ev = &e->xexpose;
258
259         if(ev->count == 0) {
260                 if(barwin == ev->window)
261                         drawstatus();
262                 else if((c = getctitle(ev->window)))
263                         drawtitle(c);
264         }
265 }
266
267 static void
268 keypress(XEvent *e)
269 {
270         static unsigned int len = sizeof(key) / sizeof(key[0]);
271         unsigned int i;
272         KeySym keysym;
273         XKeyEvent *ev = &e->xkey;
274
275         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
276         for(i = 0; i < len; i++)
277                 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
278                         if(key[i].func)
279                                 key[i].func(&key[i].arg);
280                         return;
281                 }
282 }
283
284 static void
285 leavenotify(XEvent *e)
286 {
287         XCrossingEvent *ev = &e->xcrossing;
288
289         if((ev->window == root) && !ev->same_screen)
290                 issel = True;
291 }
292
293 static void
294 maprequest(XEvent *e)
295 {
296         static XWindowAttributes wa;
297         XMapRequestEvent *ev = &e->xmaprequest;
298
299         if(!XGetWindowAttributes(dpy, ev->window, &wa))
300                 return;
301
302         if(wa.override_redirect) {
303                 XSelectInput(dpy, ev->window,
304                                 (StructureNotifyMask | PropertyChangeMask));
305                 return;
306         }
307
308         if(!getclient(ev->window))
309                 manage(ev->window, &wa);
310 }
311
312 static void
313 propertynotify(XEvent *e)
314 {
315         Client *c;
316         Window trans;
317         XPropertyEvent *ev = &e->xproperty;
318
319         if(ev->state == PropertyDelete)
320                 return; /* ignore */
321
322         if((c = getclient(ev->window))) {
323                 if(ev->atom == wmatom[WMProtocols]) {
324                         c->proto = getproto(c->win);
325                         return;
326                 }
327                 switch (ev->atom) {
328                         default: break;
329                         case XA_WM_TRANSIENT_FOR:
330                                 XGetTransientForHint(dpy, c->win, &trans);
331                                 if(!c->isfloat && (c->isfloat = (trans != 0)))
332                                         arrange(NULL);
333                                 break;
334                         case XA_WM_NORMAL_HINTS:
335                                 setsize(c);
336                                 break;
337                 }
338                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
339                         settitle(c);
340                         drawtitle(c);
341                 }
342         }
343 }
344
345 static void
346 unmapnotify(XEvent *e)
347 {
348         Client *c;
349         XUnmapEvent *ev = &e->xunmap;
350
351         if((c = getclient(ev->window)))
352                 unmanage(c);
353 }
354
355 /* extern */
356
357 void (*handler[LASTEvent]) (XEvent *) = {
358         [ButtonPress] = buttonpress,
359         [ConfigureRequest] = configurerequest,
360         [DestroyNotify] = destroynotify,
361         [EnterNotify] = enternotify,
362         [LeaveNotify] = leavenotify,
363         [Expose] = expose,
364         [KeyPress] = keypress,
365         [MapRequest] = maprequest,
366         [PropertyNotify] = propertynotify,
367         [UnmapNotify] = unmapnotify
368 };
369
370 void
371 grabkeys()
372 {
373         static unsigned int len = sizeof(key) / sizeof(key[0]);
374         unsigned int i;
375         KeyCode code;
376
377         for(i = 0; i < len; i++) {
378                 code = XKeysymToKeycode(dpy, key[i].keysym);
379                 XUngrabKey(dpy, code, key[i].mod, root);
380                 XGrabKey(dpy, code, key[i].mod, root, True,
381                                 GrabModeAsync, GrabModeAsync);
382         }
383 }