JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new resize stuff (using XConfigureWindow instead of XSendEvent)
[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         if((c = getclient(ev->window))) {
157                 gravitate(c, True);
158                 if(ev->value_mask & CWX)
159                         c->x = ev->x;
160                 if(ev->value_mask & CWY)
161                         c->y = ev->y;
162                 if(ev->value_mask & CWWidth)
163                         c->w = ev->width;
164                 if(ev->value_mask & CWHeight)
165                         c->h = ev->height;
166                 if(ev->value_mask & CWBorderWidth)
167                         c->border = 1;
168                 gravitate(c, False);
169                 resize(c, True, TopLeft);
170         }
171         else {
172                 wc.x = ev->x;
173                 wc.y = ev->y;
174                 wc.width = ev->width;
175                 wc.height = ev->height;
176                 wc.border_width = 1;
177                 XConfigureWindow(dpy, ev->window,
178                                 CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
179                 XSync(dpy, False);
180         }
181 }
182
183 static void
184 destroynotify(XEvent *e)
185 {
186         Client *c;
187         XDestroyWindowEvent *ev = &e->xdestroywindow;
188
189         if((c = getclient(ev->window)))
190                 unmanage(c);
191 }
192
193 static void
194 enternotify(XEvent *e)
195 {
196         Client *c;
197         XCrossingEvent *ev = &e->xcrossing;
198
199         if(ev->detail == NotifyInferior)
200                 return;
201
202         if((c = getclient(ev->window)))
203                 focus(c);
204         else if(ev->window == root)
205                 issel = True;
206 }
207
208 static void
209 expose(XEvent *e)
210 {
211         Client *c;
212         XExposeEvent *ev = &e->xexpose;
213
214         if(ev->count == 0) {
215                 if(barwin == ev->window)
216                         drawstatus();
217                 else if((c = getctitle(ev->window)))
218                         drawtitle(c);
219         }
220 }
221
222 static void
223 keypress(XEvent *e)
224 {
225         static unsigned int len = sizeof(key) / sizeof(key[0]);
226         unsigned int i;
227         KeySym keysym;
228         XKeyEvent *ev = &e->xkey;
229         ev->state &= valid_mask;
230
231         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
232         for(i = 0; i < len; i++)
233                 if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
234                         if(key[i].func)
235                                 key[i].func(&key[i].arg);
236                         return;
237                 }
238 }
239
240 static void
241 leavenotify(XEvent *e)
242 {
243         XCrossingEvent *ev = &e->xcrossing;
244
245         if((ev->window == root) && !ev->same_screen)
246                 issel = True;
247 }
248
249 static void
250 maprequest(XEvent *e)
251 {
252         static XWindowAttributes wa;
253         XMapRequestEvent *ev = &e->xmaprequest;
254
255         if(!XGetWindowAttributes(dpy, ev->window, &wa))
256                 return;
257
258         if(wa.override_redirect) {
259                 XSelectInput(dpy, ev->window,
260                                 (StructureNotifyMask | PropertyChangeMask));
261                 return;
262         }
263
264         if(!getclient(ev->window))
265                 manage(ev->window, &wa);
266 }
267
268 static void
269 propertynotify(XEvent *e)
270 {
271         Client *c;
272         Window trans;
273         XPropertyEvent *ev = &e->xproperty;
274
275         if(ev->state == PropertyDelete)
276                 return; /* ignore */
277
278         if((c = getclient(ev->window))) {
279                 if(ev->atom == wmatom[WMProtocols]) {
280                         c->proto = getproto(c->win);
281                         return;
282                 }
283                 switch (ev->atom) {
284                         default: break;
285                         case XA_WM_TRANSIENT_FOR:
286                                 XGetTransientForHint(dpy, c->win, &trans);
287                                 if(!c->isfloat && (c->isfloat = (trans != 0)))
288                                         arrange(NULL);
289                                 break;
290                         case XA_WM_NORMAL_HINTS:
291                                 setsize(c);
292                                 break;
293                 }
294                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
295                         settitle(c);
296                         drawtitle(c);
297                 }
298         }
299 }
300
301 static void
302 unmapnotify(XEvent *e)
303 {
304         Client *c;
305         XUnmapEvent *ev = &e->xunmap;
306
307         if((c = getclient(ev->window)))
308                 unmanage(c);
309 }
310
311 /* extern */
312
313 void (*handler[LASTEvent]) (XEvent *) = {
314         [ButtonPress] = buttonpress,
315         [ConfigureRequest] = configurerequest,
316         [DestroyNotify] = destroynotify,
317         [EnterNotify] = enternotify,
318         [LeaveNotify] = leavenotify,
319         [Expose] = expose,
320         [KeyPress] = keypress,
321         [MapRequest] = maprequest,
322         [PropertyNotify] = propertynotify,
323         [UnmapNotify] = unmapnotify
324 };
325
326 void
327 grabkeys()
328 {
329         static unsigned int len = sizeof(key) / sizeof(key[0]);
330         unsigned int i;
331         KeyCode code;
332
333         for(i = 0; i < len; i++) {
334                 code = XKeysymToKeycode(dpy, key[i].keysym);
335                 XUngrabKey(dpy, code, key[i].mod, root);
336                 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
337                 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
338                 XGrabKey(dpy, code, key[i].mod, root, True,
339                                 GrabModeAsync, GrabModeAsync);
340                 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
341                                 GrabModeAsync, GrabModeAsync);
342                 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
343                                 GrabModeAsync, GrabModeAsync);
344         }
345 }