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