changed -v output (now also anydot and nsz are contained in this output)
[dwm.git] / event.c
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2  * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3  * © 2007 Premysl Hruby <dfenze at gmail dot com>
4  * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
5  * See LICENSE file for license details. */
6 #include "dwm.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <X11/keysym.h>
10 #include <X11/Xatom.h>
11
12 /* static */
13
14 typedef struct {
15         unsigned long mod;
16         KeySym keysym;
17         void (*func)(const char *arg);
18         const char *arg;
19 } Key;
20
21 KEYS
22
23 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 #define MOUSEMASK               (BUTTONMASK | PointerMotionMask)
25
26 static Client *
27 getclient(Window w) {
28         Client *c;
29
30         for(c = clients; c && c->win != w; c = c->next);
31         return c;
32 }
33
34 static void
35 movemouse(Client *c) {
36         int x1, y1, ocx, ocy, di, nx, ny;
37         unsigned int dui;
38         Window dummy;
39         XEvent ev;
40
41         ocx = nx = c->x;
42         ocy = ny = c->y;
43         if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
44                         None, cursor[CurMove], CurrentTime) != GrabSuccess)
45                 return;
46         c->ismax = False;
47         XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
48         for(;;) {
49                 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
50                 switch (ev.type) {
51                 case ButtonRelease:
52                         XUngrabPointer(dpy, CurrentTime);
53                         return;
54                 case ConfigureRequest:
55                 case Expose:
56                 case MapRequest:
57                         handler[ev.type](&ev);
58                         break;
59                 case MotionNotify:
60                         XSync(dpy, False);
61                         nx = ocx + (ev.xmotion.x - x1);
62                         ny = ocy + (ev.xmotion.y - y1);
63                         if(abs(wax + nx) < SNAP)
64                                 nx = wax;
65                         else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
66                                 nx = wax + waw - c->w - 2 * c->border;
67                         if(abs(way - ny) < SNAP)
68                                 ny = way;
69                         else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
70                                 ny = way + wah - c->h - 2 * c->border;
71                         resize(c, nx, ny, c->w, c->h, False);
72                         break;
73                 }
74         }
75 }
76
77 static void
78 resizemouse(Client *c) {
79         int ocx, ocy;
80         int nw, nh;
81         XEvent ev;
82
83         ocx = c->x;
84         ocy = c->y;
85         if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
86                         None, cursor[CurResize], CurrentTime) != GrabSuccess)
87                 return;
88         c->ismax = False;
89         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
90         for(;;) {
91                 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
92                 switch(ev.type) {
93                 case ButtonRelease:
94                         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
95                                         c->w + c->border - 1, c->h + c->border - 1);
96                         XUngrabPointer(dpy, CurrentTime);
97                         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
98                         return;
99                 case ConfigureRequest:
100                 case Expose:
101                 case MapRequest:
102                         handler[ev.type](&ev);
103                         break;
104                 case MotionNotify:
105                         XSync(dpy, False);
106                         if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
107                                 nw = 1;
108                         if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
109                                 nh = 1;
110                         resize(c, c->x, c->y, nw, nh, True);
111                         break;
112                 }
113         }
114 }
115
116 static void
117 buttonpress(XEvent *e) {
118         static char buf[32];
119         unsigned int i, x;
120         Client *c;
121         XButtonPressedEvent *ev = &e->xbutton;
122
123         buf[0] = 0;
124         if(barwin == ev->window) {
125                 x = 0;
126                 for(i = 0; i < ntags; i++) {
127                         x += textw(tags[i]);
128                         if(ev->x < x) {
129                                 snprintf(buf, sizeof buf, "%d", i);
130                                 if(ev->button == Button1) {
131                                         if(ev->state & MODKEY)
132                                                 tag(buf);
133                                         else
134                                                 view(buf);
135                                 }
136                                 else if(ev->button == Button3) {
137                                         if(ev->state & MODKEY)
138                                                 toggletag(buf);
139                                         else
140                                                 toggleview(buf);
141                                 }
142                                 return;
143                         }
144                 }
145                 if(ev->x < x + blw)
146                         switch(ev->button) {
147                         case Button1:
148                                 setlayout(NULL);
149                                 break;
150                         }
151         }
152         else if((c = getclient(ev->window))) {
153                 focus(c);
154                 if(CLEANMASK(ev->state) != MODKEY)
155                         return;
156                 if(ev->button == Button1 && (lt->arrange == floating || c->isfloating)) {
157                         restack();
158                         movemouse(c);
159                 }
160                 else if(ev->button == Button2)
161                         zoom(NULL);
162                 else if(ev->button == Button3
163                 && (lt->arrange == floating || c->isfloating) && !c->isfixed)
164                 {
165                         restack();
166                         resizemouse(c);
167                 }
168         }
169 }
170
171 static void
172 configurerequest(XEvent *e) {
173         Client *c;
174         XConfigureRequestEvent *ev = &e->xconfigurerequest;
175         XWindowChanges wc;
176
177         if((c = getclient(ev->window))) {
178                 c->ismax = False;
179                 if(ev->value_mask & CWBorderWidth)
180                         c->border = ev->border_width;
181                 if(c->isfixed || c->isfloating || (lt->arrange == floating)) {
182                         if(ev->value_mask & CWX)
183                                 c->x = ev->x;
184                         if(ev->value_mask & CWY)
185                                 c->y = ev->y;
186                         if(ev->value_mask & CWWidth)
187                                 c->w = ev->width;
188                         if(ev->value_mask & CWHeight)
189                                 c->h = ev->height;
190                         if((c->x + c->w) > sw && c->isfloating)
191                                 c->x = sw / 2 - c->w / 2; /* center in x direction */
192                         if((c->y + c->h) > sh && c->isfloating)
193                                 c->y = sh / 2 - c->h / 2; /* center in y direction */
194                         if((ev->value_mask & (CWX | CWY))
195                         && !(ev->value_mask & (CWWidth | CWHeight)))
196                                 configure(c);
197                         if(isvisible(c))
198                                 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
199                 }
200                 else
201                         configure(c);
202         }
203         else {
204                 wc.x = ev->x;
205                 wc.y = ev->y;
206                 wc.width = ev->width;
207                 wc.height = ev->height;
208                 wc.border_width = ev->border_width;
209                 wc.sibling = ev->above;
210                 wc.stack_mode = ev->detail;
211                 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
212         }
213         XSync(dpy, False);
214 }
215
216 static void
217 configurenotify(XEvent *e) {
218         XConfigureEvent *ev = &e->xconfigure;
219
220         if (ev->window == root && (ev->width != sw || ev->height != sh)) {
221                 sw = ev->width;
222                 sh = ev->height;
223                 XFreePixmap(dpy, dc.drawable);
224                 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
225                 XResizeWindow(dpy, barwin, sw, bh);
226                 updatebarpos();
227                 lt->arrange();
228         }
229 }
230
231 static void
232 destroynotify(XEvent *e) {
233         Client *c;
234         XDestroyWindowEvent *ev = &e->xdestroywindow;
235
236         if((c = getclient(ev->window)))
237                 unmanage(c);
238 }
239
240 static void
241 enternotify(XEvent *e) {
242         Client *c;
243         XCrossingEvent *ev = &e->xcrossing;
244
245         if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
246                 return;
247         if(c = getclient(ev->window))
248                 focus(c);
249         else if(ev->window == root) {
250                 selscreen = True;
251                 focus(NULL);
252         }
253 }
254
255 static void
256 expose(XEvent *e) {
257         XExposeEvent *ev = &e->xexpose;
258
259         if(ev->count == 0) {
260                 if(barwin == ev->window)
261                         drawstatus();
262         }
263 }
264
265 static void
266 keypress(XEvent *e) {
267         static unsigned int len = sizeof key / sizeof key[0];
268         unsigned int i;
269         KeySym keysym;
270         XKeyEvent *ev = &e->xkey;
271
272         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
273         for(i = 0; i < len; i++)
274                 if(keysym == key[i].keysym
275                 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
276                 {
277                         if(key[i].func)
278                                 key[i].func(key[i].arg);
279                 }
280 }
281
282 static void
283 leavenotify(XEvent *e) {
284         XCrossingEvent *ev = &e->xcrossing;
285
286         if((ev->window == root) && !ev->same_screen) {
287                 selscreen = False;
288                 focus(NULL);
289         }
290 }
291
292 static void
293 mappingnotify(XEvent *e) {
294         XMappingEvent *ev = &e->xmapping;
295
296         XRefreshKeyboardMapping(ev);
297         if(ev->request == MappingKeyboard)
298                 grabkeys();
299 }
300
301 static void
302 maprequest(XEvent *e) {
303         static XWindowAttributes wa;
304         XMapRequestEvent *ev = &e->xmaprequest;
305
306         if(!XGetWindowAttributes(dpy, ev->window, &wa))
307                 return;
308         if(wa.override_redirect)
309                 return;
310         if(!getclient(ev->window))
311                 manage(ev->window, &wa);
312 }
313
314 static void
315 propertynotify(XEvent *e) {
316         Client *c;
317         Window trans;
318         XPropertyEvent *ev = &e->xproperty;
319
320         if(ev->state == PropertyDelete)
321                 return; /* ignore */
322         if((c = getclient(ev->window))) {
323                 switch (ev->atom) {
324                         default: break;
325                         case XA_WM_TRANSIENT_FOR:
326                                 XGetTransientForHint(dpy, c->win, &trans);
327                                 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
328                                         lt->arrange();
329                                 break;
330                         case XA_WM_NORMAL_HINTS:
331                                 updatesizehints(c);
332                                 break;
333                 }
334                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
335                         updatetitle(c);
336                         if(c == sel)
337                                 drawstatus();
338                 }
339         }
340 }
341
342 static void
343 unmapnotify(XEvent *e) {
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         [ConfigureNotify] = configurenotify,
357         [DestroyNotify] = destroynotify,
358         [EnterNotify] = enternotify,
359         [LeaveNotify] = leavenotify,
360         [Expose] = expose,
361         [KeyPress] = keypress,
362         [MappingNotify] = mappingnotify,
363         [MapRequest] = maprequest,
364         [PropertyNotify] = propertynotify,
365         [UnmapNotify] = unmapnotify
366 };
367
368 void
369 grabkeys(void) {
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 }