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