applied Gottox patch to simplify the resizing of col, instead of resizing the current...
[dwm.git] / event.c
1 /* (C)opyright MMVI 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(sx + c->x) < SNAP)
52                                 c->x = sx;
53                         else if(abs((sx + sw) - (c->x + c->w)) < SNAP)
54                                 c->x = sw - c->w - 2 * BORDERPX;
55                         if(abs((sy + bh) - c->y) < SNAP)
56                                 c->y = sy + bh;
57                         else if(abs((sy + sh) - (c->y + c->h)) < SNAP)
58                                 c->y = sh - 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) && (ev->button == Button1))
135                         togglemode(NULL);
136         }
137         else if((c = getclient(ev->window))) {
138                 focus(c);
139                 if(CLEANMASK(ev->state) != MODKEY)
140                         return;
141                 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
142                         restack();
143                         movemouse(c);
144                 }
145                 else if(ev->button == Button2)
146                         zoom(NULL);
147                 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
148                                 !c->isfixed) {
149                         restack();
150                         resizemouse(c);
151                 }
152         }
153 }
154
155 static void
156 configurerequest(XEvent *e) {
157         unsigned long newmask;
158         Client *c;
159         XConfigureRequestEvent *ev = &e->xconfigurerequest;
160         XWindowChanges wc;
161
162         if((c = getclient(ev->window))) {
163                 c->ismax = False;
164                 gravitate(c, True);
165                 if(ev->value_mask & CWX)
166                         c->x = ev->x;
167                 if(ev->value_mask & CWY)
168                         c->y = ev->y;
169                 if(ev->value_mask & CWWidth)
170                         c->w = ev->width;
171                 if(ev->value_mask & CWHeight)
172                         c->h = ev->height;
173                 if(ev->value_mask & CWBorderWidth)
174                         c->border = ev->border_width;
175                 gravitate(c, False);
176                 wc.x = c->x;
177                 wc.y = c->y;
178                 wc.width = c->w;
179                 wc.height = c->h;
180                 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
181                 if(newmask)
182                         XConfigureWindow(dpy, c->win, newmask, &wc);
183                 else
184                         configure(c);
185                 XSync(dpy, False);
186                 if(c->isfloat) {
187                         resize(c, False, TopLeft);
188                         if(!isvisible(c))
189                                 ban(c);
190                 }
191                 else
192                         arrange();
193         }
194         else {
195                 wc.x = ev->x;
196                 wc.y = ev->y;
197                 wc.width = ev->width;
198                 wc.height = ev->height;
199                 wc.border_width = ev->border_width;
200                 wc.sibling = ev->above;
201                 wc.stack_mode = ev->detail;
202                 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
203                 XSync(dpy, False);
204         }
205 }
206
207 static void
208 destroynotify(XEvent *e) {
209         Client *c;
210         XDestroyWindowEvent *ev = &e->xdestroywindow;
211
212         if((c = getclient(ev->window)))
213                 unmanage(c);
214 }
215
216 static void
217 enternotify(XEvent *e) {
218         Client *c;
219         XCrossingEvent *ev = &e->xcrossing;
220
221         if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
222                 return;
223         if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
224                 focus(c);
225         else if(ev->window == root) {
226                 issel = True;
227                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
228                 drawall();
229         }
230 }
231
232 static void
233 expose(XEvent *e) {
234         Client *c;
235         XExposeEvent *ev = &e->xexpose;
236
237         if(ev->count == 0) {
238                 if(barwin == ev->window)
239                         drawstatus();
240                 else if((c = getctitle(ev->window)))
241                         drawtitle(c);
242         }
243 }
244
245 static void
246 keypress(XEvent *e) {
247         static unsigned int len = sizeof(key) / sizeof(key[0]);
248         unsigned int i;
249         KeySym keysym;
250         XKeyEvent *ev = &e->xkey;
251
252         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
253         for(i = 0; i < len; i++) {
254                 if(keysym == key[i].keysym
255                         && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
256                 {
257                         if(key[i].func)
258                                 key[i].func(&key[i].arg);
259                         return;
260                 }
261         }
262 }
263
264 static void
265 leavenotify(XEvent *e) {
266         XCrossingEvent *ev = &e->xcrossing;
267
268         if((ev->window == root) && !ev->same_screen) {
269                 issel = False;
270                 drawall();
271         }
272 }
273
274 static void
275 mappingnotify(XEvent *e) {
276         XMappingEvent *ev = &e->xmapping;
277
278         XRefreshKeyboardMapping(ev);
279         if(ev->request == MappingKeyboard)
280                 grabkeys();
281 }
282
283 static void
284 maprequest(XEvent *e) {
285         static XWindowAttributes wa;
286         XMapRequestEvent *ev = &e->xmaprequest;
287
288         if(!XGetWindowAttributes(dpy, ev->window, &wa))
289                 return;
290         if(wa.override_redirect) {
291                 XSelectInput(dpy, ev->window,
292                                 (StructureNotifyMask | PropertyChangeMask));
293                 return;
294         }
295         if(!getclient(ev->window))
296                 manage(ev->window, &wa);
297 }
298
299 static void
300 propertynotify(XEvent *e) {
301         Client *c;
302         Window trans;
303         XPropertyEvent *ev = &e->xproperty;
304
305         if(ev->state == PropertyDelete)
306                 return; /* ignore */
307         if((c = getclient(ev->window))) {
308                 if(ev->atom == wmatom[WMProtocols]) {
309                         c->proto = getproto(c->win);
310                         return;
311                 }
312                 switch (ev->atom) {
313                         default: break;
314                         case XA_WM_TRANSIENT_FOR:
315                                 XGetTransientForHint(dpy, c->win, &trans);
316                                 if(!c->isfloat && (c->isfloat = (trans != 0)))
317                                         arrange();
318                                 break;
319                         case XA_WM_NORMAL_HINTS:
320                                 updatesize(c);
321                                 break;
322                 }
323                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
324                         updatetitle(c);
325                         resizetitle(c);
326                         drawtitle(c);
327                 }
328         }
329 }
330
331 static void
332 unmapnotify(XEvent *e) {
333         Client *c;
334         XUnmapEvent *ev = &e->xunmap;
335
336         if((c = getclient(ev->window)))
337                 unmanage(c);
338 }
339
340 /* extern */
341
342 void (*handler[LASTEvent]) (XEvent *) = {
343         [ButtonPress] = buttonpress,
344         [ConfigureRequest] = configurerequest,
345         [DestroyNotify] = destroynotify,
346         [EnterNotify] = enternotify,
347         [LeaveNotify] = leavenotify,
348         [Expose] = expose,
349         [KeyPress] = keypress,
350         [MappingNotify] = mappingnotify,
351         [MapRequest] = maprequest,
352         [PropertyNotify] = propertynotify,
353         [UnmapNotify] = unmapnotify
354 };
355
356 void
357 grabkeys(void) {
358         static unsigned int len = sizeof(key) / sizeof(key[0]);
359         unsigned int i;
360         KeyCode code;
361
362         XUngrabKey(dpy, AnyKey, AnyModifier, root);
363         for(i = 0; i < len; i++) {
364                 code = XKeysymToKeycode(dpy, key[i].keysym);
365                 XGrabKey(dpy, code, key[i].mod, root, True,
366                                 GrabModeAsync, GrabModeAsync);
367                 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
368                                 GrabModeAsync, GrabModeAsync);
369                 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
370                                 GrabModeAsync, GrabModeAsync);
371                 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
372                                 GrabModeAsync, GrabModeAsync);
373         }
374 }
375
376 void
377 procevent(void) {
378         XEvent ev;
379
380         while(XPending(dpy)) {
381                 XNextEvent(dpy, &ev);
382                 if(handler[ev.type])
383                         (handler[ev.type])(&ev); /* call handler */
384         }
385 }