aewl

view event.c @ 490:303d3384720e

slight change of event handling order
author arg@mmvi
date Mon, 25 Sep 2006 21:28:00 +0200
parents d34374cbe926
children 12395ef46d97
line source
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>
10 /* static */
12 typedef struct {
13 unsigned long mod;
14 KeySym keysym;
15 void (*func)(Arg *arg);
16 Arg arg;
17 } Key;
19 KEYS
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
23 static void
24 movemouse(Client *c) {
25 int x1, y1, ocx, ocy, di;
26 unsigned int dui;
27 Window dummy;
28 XEvent ev;
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 XUngrabPointer(dpy, CurrentTime);
42 return;
43 case Expose:
44 handler[Expose](&ev);
45 break;
46 case MotionNotify:
47 XSync(dpy, False);
48 c->x = ocx + (ev.xmotion.x - x1);
49 c->y = ocy + (ev.xmotion.y - y1);
50 resize(c, False, TopLeft);
51 break;
52 }
53 }
54 }
56 static void
57 resizemouse(Client *c) {
58 int ocx, ocy;
59 int nw, nh;
60 Corner sticky;
61 XEvent ev;
63 ocx = c->x;
64 ocy = c->y;
65 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
66 None, cursor[CurResize], CurrentTime) != GrabSuccess)
67 return;
68 c->ismax = False;
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 case ButtonRelease:
74 XUngrabPointer(dpy, CurrentTime);
75 return;
76 case Expose:
77 handler[Expose](&ev);
78 break;
79 case MotionNotify:
80 XSync(dpy, False);
81 if((nw = abs(ocx - ev.xmotion.x)))
82 c->w = nw;
83 if((nh = abs(ocy - ev.xmotion.y)))
84 c->h = nh;
85 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
86 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
87 if(ocx <= ev.xmotion.x)
88 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
89 else
90 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
91 resize(c, True, sticky);
92 break;
93 }
94 }
95 }
97 static void
98 buttonpress(XEvent *e) {
99 int x;
100 Arg a;
101 Client *c;
102 XButtonPressedEvent *ev = &e->xbutton;
104 if(barwin == ev->window) {
105 x = 0;
106 for(a.i = 0; a.i < ntags; a.i++) {
107 x += textw(tags[a.i]);
108 if(ev->x < x) {
109 if(ev->button == Button1) {
110 if(ev->state & MODKEY)
111 tag(&a);
112 else
113 view(&a);
114 }
115 else if(ev->button == Button3) {
116 if(ev->state & MODKEY)
117 toggletag(&a);
118 else
119 toggleview(&a);
120 }
121 return;
122 }
123 }
124 if(ev->x < x + bmw) {
125 if(ev->button == Button1)
126 togglemode(NULL);
127 }
128 }
129 else if((c = getclient(ev->window))) {
130 focus(c);
131 if(CLEANMASK(ev->state) != MODKEY)
132 return;
133 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
134 restack();
135 movemouse(c);
136 }
137 else if(ev->button == Button2)
138 zoom(NULL);
139 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
140 restack();
141 resizemouse(c);
142 }
143 }
144 }
146 static void
147 configurerequest(XEvent *e) {
148 unsigned long newmask;
149 Client *c;
150 XConfigureRequestEvent *ev = &e->xconfigurerequest;
151 XEvent synev;
152 XWindowChanges wc;
154 if((c = getclient(ev->window))) {
155 c->ismax = False;
156 gravitate(c, True);
157 if(ev->value_mask & CWX)
158 c->x = ev->x;
159 if(ev->value_mask & CWY)
160 c->y = ev->y;
161 if(ev->value_mask & CWWidth)
162 c->w = ev->width;
163 if(ev->value_mask & CWHeight)
164 c->h = ev->height;
165 if(ev->value_mask & CWBorderWidth)
166 c->border = ev->border_width;
167 gravitate(c, False);
168 wc.x = c->x;
169 wc.y = c->y;
170 wc.width = c->w;
171 wc.height = c->h;
172 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
173 if(newmask)
174 XConfigureWindow(dpy, c->win, newmask, &wc);
175 else {
176 synev.type = ConfigureNotify;
177 synev.xconfigure.display = dpy;
178 synev.xconfigure.event = c->win;
179 synev.xconfigure.window = c->win;
180 synev.xconfigure.x = c->x;
181 synev.xconfigure.y = c->y;
182 synev.xconfigure.width = c->w;
183 synev.xconfigure.height = c->h;
184 synev.xconfigure.border_width = c->border;
185 synev.xconfigure.above = None;
186 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
187 }
188 XSync(dpy, False);
189 if(c->isfloat)
190 resize(c, False, TopLeft);
191 else
192 arrange(NULL);
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 }
207 static void
208 destroynotify(XEvent *e) {
209 Client *c;
210 XDestroyWindowEvent *ev = &e->xdestroywindow;
212 if((c = getclient(ev->window)))
213 unmanage(c);
214 }
216 static void
217 enternotify(XEvent *e) {
218 Client *c;
219 XCrossingEvent *ev = &e->xcrossing;
221 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
222 return;
224 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
225 focus(c);
226 else if(ev->window == root) {
227 issel = True;
228 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
229 drawall();
230 }
231 }
233 static void
234 expose(XEvent *e) {
235 Client *c;
236 XExposeEvent *ev = &e->xexpose;
238 if(ev->count == 0) {
239 if(barwin == ev->window)
240 drawstatus();
241 else if((c = getctitle(ev->window)))
242 drawtitle(c);
243 }
244 }
246 static void
247 keypress(XEvent *e) {
248 static unsigned int len = sizeof(key) / sizeof(key[0]);
249 unsigned int i;
250 KeySym keysym;
251 XKeyEvent *ev = &e->xkey;
253 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
254 for(i = 0; i < len; i++) {
255 if(keysym == key[i].keysym
256 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
257 {
258 if(key[i].func)
259 key[i].func(&key[i].arg);
260 return;
261 }
262 }
263 }
265 static void
266 leavenotify(XEvent *e) {
267 XCrossingEvent *ev = &e->xcrossing;
269 if((ev->window == root) && !ev->same_screen) {
270 issel = False;
271 drawall();
272 }
273 }
275 static void
276 mappingnotify(XEvent *e) {
277 XMappingEvent *ev = &e->xmapping;
279 XRefreshKeyboardMapping(ev);
280 if(ev->request == MappingKeyboard)
281 grabkeys();
282 }
284 static void
285 maprequest(XEvent *e) {
286 static XWindowAttributes wa;
287 XMapRequestEvent *ev = &e->xmaprequest;
289 if(!XGetWindowAttributes(dpy, ev->window, &wa))
290 return;
292 if(wa.override_redirect) {
293 XSelectInput(dpy, ev->window,
294 (StructureNotifyMask | PropertyChangeMask));
295 return;
296 }
298 if(!getclient(ev->window))
299 manage(ev->window, &wa);
300 }
302 static void
303 propertynotify(XEvent *e) {
304 Client *c;
305 Window trans;
306 XPropertyEvent *ev = &e->xproperty;
308 if(ev->state == PropertyDelete)
309 return; /* ignore */
311 if((c = getclient(ev->window))) {
312 if(ev->atom == wmatom[WMProtocols]) {
313 c->proto = getproto(c->win);
314 return;
315 }
316 switch (ev->atom) {
317 default: break;
318 case XA_WM_TRANSIENT_FOR:
319 XGetTransientForHint(dpy, c->win, &trans);
320 if(!c->isfloat && (c->isfloat = (trans != 0)))
321 arrange(NULL);
322 break;
323 case XA_WM_NORMAL_HINTS:
324 updatesize(c);
325 break;
326 }
327 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
328 updatetitle(c);
329 drawtitle(c);
330 }
331 }
332 }
334 static void
335 unmapnotify(XEvent *e) {
336 Client *c;
337 XUnmapEvent *ev = &e->xunmap;
339 if((c = getclient(ev->window)))
340 unmanage(c);
341 }
343 /* extern */
345 void (*handler[LASTEvent]) (XEvent *) = {
346 [ButtonPress] = buttonpress,
347 [ConfigureRequest] = configurerequest,
348 [DestroyNotify] = destroynotify,
349 [EnterNotify] = enternotify,
350 [LeaveNotify] = leavenotify,
351 [Expose] = expose,
352 [KeyPress] = keypress,
353 [MappingNotify] = mappingnotify,
354 [MapRequest] = maprequest,
355 [PropertyNotify] = propertynotify,
356 [UnmapNotify] = unmapnotify
357 };
359 void
360 grabkeys(void) {
361 static unsigned int len = sizeof(key) / sizeof(key[0]);
362 unsigned int i;
363 KeyCode code;
365 XUngrabKey(dpy, AnyKey, AnyModifier, root);
366 for(i = 0; i < len; i++) {
367 code = XKeysymToKeycode(dpy, key[i].keysym);
368 XGrabKey(dpy, code, key[i].mod, root, True,
369 GrabModeAsync, GrabModeAsync);
370 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
371 GrabModeAsync, GrabModeAsync);
372 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
373 GrabModeAsync, GrabModeAsync);
374 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
375 GrabModeAsync, GrabModeAsync);
376 }
377 }
379 void
380 procevent(void) {
381 XEvent ev;
383 while(XPending(dpy)) {
384 XNextEvent(dpy, &ev);
385 if(handler[ev.type])
386 (handler[ev.type])(&ev); /* call handler */
387 }
388 }