aewl

view event.c @ 491:12395ef46d97

added configure(), but this doesn't really fix those frking broken SDL apps
author arg@mmvi
date Tue, 26 Sep 2006 07:40:19 +0200
parents 303d3384720e
children 5df494262dfd
line source
1 #include <stdio.h>
2 /*
3 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
4 * See LICENSE file for license details.
5 */
6 #include "dwm.h"
7 #include <stdlib.h>
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
11 /* static */
13 typedef struct {
14 unsigned long mod;
15 KeySym keysym;
16 void (*func)(Arg *arg);
17 Arg arg;
18 } Key;
20 KEYS
22 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 static void
25 movemouse(Client *c) {
26 int x1, y1, ocx, ocy, di;
27 unsigned int dui;
28 Window dummy;
29 XEvent ev;
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 c->ismax = False;
37 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
38 for(;;) {
39 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
40 switch (ev.type) {
41 case ButtonRelease:
42 resize(c, True, TopLeft);
43 XUngrabPointer(dpy, CurrentTime);
44 return;
45 case Expose:
46 handler[Expose](&ev);
47 break;
48 case MotionNotify:
49 XSync(dpy, False);
50 c->x = ocx + (ev.xmotion.x - x1);
51 c->y = ocy + (ev.xmotion.y - y1);
52 resize(c, False, TopLeft);
53 break;
54 }
55 }
56 }
58 static void
59 resizemouse(Client *c) {
60 int ocx, ocy;
61 int nw, nh;
62 Corner sticky;
63 XEvent ev;
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 c->ismax = False;
71 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
72 for(;;) {
73 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
74 switch(ev.type) {
75 case ButtonRelease:
76 resize(c, True, TopLeft);
77 XUngrabPointer(dpy, CurrentTime);
78 return;
79 case Expose:
80 handler[Expose](&ev);
81 break;
82 case MotionNotify:
83 XSync(dpy, False);
84 if((nw = abs(ocx - ev.xmotion.x)))
85 c->w = nw;
86 if((nh = abs(ocy - ev.xmotion.y)))
87 c->h = nh;
88 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
89 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
90 if(ocx <= ev.xmotion.x)
91 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
92 else
93 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
94 resize(c, True, sticky);
95 break;
96 }
97 }
98 }
100 static void
101 buttonpress(XEvent *e) {
102 int x;
103 Arg a;
104 Client *c;
105 XButtonPressedEvent *ev = &e->xbutton;
107 if(barwin == ev->window) {
108 x = 0;
109 for(a.i = 0; a.i < ntags; a.i++) {
110 x += textw(tags[a.i]);
111 if(ev->x < x) {
112 if(ev->button == Button1) {
113 if(ev->state & MODKEY)
114 tag(&a);
115 else
116 view(&a);
117 }
118 else if(ev->button == Button3) {
119 if(ev->state & MODKEY)
120 toggletag(&a);
121 else
122 toggleview(&a);
123 }
124 return;
125 }
126 }
127 if(ev->x < x + bmw) {
128 if(ev->button == Button1)
129 togglemode(NULL);
130 }
131 }
132 else if((c = getclient(ev->window))) {
133 focus(c);
134 if(CLEANMASK(ev->state) != MODKEY)
135 return;
136 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
137 restack();
138 movemouse(c);
139 }
140 else if(ev->button == Button2)
141 zoom(NULL);
142 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
143 restack();
144 resizemouse(c);
145 }
146 }
147 }
149 static void
150 configurerequest(XEvent *e) {
151 unsigned long newmask;
152 Client *c;
153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
154 XEvent synev;
155 XWindowChanges wc;
157 fputs("configurerequest\n", stderr);
158 if((c = getclient(ev->window))) {
159 c->ismax = False;
160 gravitate(c, True);
161 if(ev->value_mask & CWX)
162 c->x = ev->x;
163 if(ev->value_mask & CWY)
164 c->y = ev->y;
165 if(ev->value_mask & CWWidth)
166 c->w = ev->width;
167 if(ev->value_mask & CWHeight)
168 c->h = ev->height;
169 if(ev->value_mask & CWBorderWidth)
170 c->border = ev->border_width;
171 gravitate(c, False);
172 wc.x = c->x;
173 wc.y = c->y;
174 wc.width = c->w;
175 wc.height = c->h;
176 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
177 if(newmask)
178 XConfigureWindow(dpy, c->win, newmask, &wc);
179 else
180 configure(c);
181 XSync(dpy, False);
182 if(c->isfloat)
183 resize(c, False, TopLeft);
184 else
185 arrange(NULL);
186 }
187 else {
188 wc.x = ev->x;
189 wc.y = ev->y;
190 wc.width = ev->width;
191 wc.height = ev->height;
192 wc.border_width = ev->border_width;
193 wc.sibling = ev->above;
194 wc.stack_mode = ev->detail;
195 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
196 XSync(dpy, False);
197 }
198 }
200 static void
201 destroynotify(XEvent *e) {
202 Client *c;
203 XDestroyWindowEvent *ev = &e->xdestroywindow;
205 if((c = getclient(ev->window)))
206 unmanage(c);
207 }
209 static void
210 enternotify(XEvent *e) {
211 Client *c;
212 XCrossingEvent *ev = &e->xcrossing;
214 fputs("enternotify\n", stderr);
215 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
216 return;
218 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
219 focus(c);
220 else if(ev->window == root) {
221 issel = True;
222 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
223 drawall();
224 }
225 }
227 static void
228 expose(XEvent *e) {
229 Client *c;
230 XExposeEvent *ev = &e->xexpose;
232 if(ev->count == 0) {
233 if(barwin == ev->window)
234 drawstatus();
235 else if((c = getctitle(ev->window)))
236 drawtitle(c);
237 }
238 }
240 static void
241 keypress(XEvent *e) {
242 static unsigned int len = sizeof(key) / sizeof(key[0]);
243 unsigned int i;
244 KeySym keysym;
245 XKeyEvent *ev = &e->xkey;
247 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
248 for(i = 0; i < len; i++) {
249 if(keysym == key[i].keysym
250 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
251 {
252 if(key[i].func)
253 key[i].func(&key[i].arg);
254 return;
255 }
256 }
257 }
259 static void
260 leavenotify(XEvent *e) {
261 XCrossingEvent *ev = &e->xcrossing;
263 if((ev->window == root) && !ev->same_screen) {
264 issel = False;
265 drawall();
266 }
267 }
269 static void
270 mappingnotify(XEvent *e) {
271 XMappingEvent *ev = &e->xmapping;
273 XRefreshKeyboardMapping(ev);
274 if(ev->request == MappingKeyboard)
275 grabkeys();
276 }
278 static void
279 maprequest(XEvent *e) {
280 static XWindowAttributes wa;
281 XMapRequestEvent *ev = &e->xmaprequest;
283 if(!XGetWindowAttributes(dpy, ev->window, &wa))
284 return;
286 if(wa.override_redirect) {
287 XSelectInput(dpy, ev->window,
288 (StructureNotifyMask | PropertyChangeMask));
289 return;
290 }
292 if(!getclient(ev->window))
293 manage(ev->window, &wa);
294 }
296 static void
297 propertynotify(XEvent *e) {
298 Client *c;
299 Window trans;
300 XPropertyEvent *ev = &e->xproperty;
302 fputs("propertynotify\n", stderr);
303 if(ev->state == PropertyDelete)
304 return; /* ignore */
306 if((c = getclient(ev->window))) {
307 if(ev->atom == wmatom[WMProtocols]) {
308 c->proto = getproto(c->win);
309 return;
310 }
311 switch (ev->atom) {
312 default: break;
313 case XA_WM_TRANSIENT_FOR:
314 XGetTransientForHint(dpy, c->win, &trans);
315 if(!c->isfloat && (c->isfloat = (trans != 0)))
316 arrange(NULL);
317 break;
318 case XA_WM_NORMAL_HINTS:
319 updatesize(c);
320 break;
321 }
322 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
323 updatetitle(c);
324 drawtitle(c);
325 }
326 }
327 }
329 static void
330 unmapnotify(XEvent *e) {
331 Client *c;
332 XUnmapEvent *ev = &e->xunmap;
334 if((c = getclient(ev->window)))
335 unmanage(c);
336 }
338 /* extern */
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 [MappingNotify] = mappingnotify,
349 [MapRequest] = maprequest,
350 [PropertyNotify] = propertynotify,
351 [UnmapNotify] = unmapnotify
352 };
354 void
355 grabkeys(void) {
356 static unsigned int len = sizeof(key) / sizeof(key[0]);
357 unsigned int i;
358 KeyCode code;
360 XUngrabKey(dpy, AnyKey, AnyModifier, root);
361 for(i = 0; i < len; i++) {
362 code = XKeysymToKeycode(dpy, key[i].keysym);
363 XGrabKey(dpy, code, key[i].mod, root, True,
364 GrabModeAsync, GrabModeAsync);
365 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
368 GrabModeAsync, GrabModeAsync);
369 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
370 GrabModeAsync, GrabModeAsync);
371 }
372 }
374 void
375 procevent(void) {
376 XEvent ev;
378 while(XPending(dpy)) {
379 XNextEvent(dpy, &ev);
380 if(handler[ev.type])
381 (handler[ev.type])(&ev); /* call handler */
382 }
383 }