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