aewl
view event.c @ 385:37706ba7d492
fixed
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Tue, 29 Aug 2006 17:33:27 +0200 |
parents | 126e78129f1d |
children | 58288246d16c |
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 {
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 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 }
57 static void
58 resizemouse(Client *c)
59 {
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 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 if((nw = abs(ocx - ev.xmotion.x)))
81 c->w = abs(ocx - ev.xmotion.x);
82 if((nh = abs(ocy - ev.xmotion.y)))
83 c->h = abs(ocy - ev.xmotion.y);
84 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
85 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
86 if(ocx <= ev.xmotion.x)
87 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
88 else
89 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
90 resize(c, True, sticky);
91 break;
92 case ButtonRelease:
93 XUngrabPointer(dpy, CurrentTime);
94 return;
95 }
96 }
97 }
99 static void
100 buttonpress(XEvent *e)
101 {
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 view(&a);
114 else if(ev->button == Button3)
115 toggleview(&a);
116 return;
117 }
118 }
119 if(ev->x < x + bmw) {
120 if(ev->button == Button1)
121 togglemode(NULL);
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 focus(c);
126 if(CLEANMASK(ev->state) != MODKEY)
127 return;
128 switch(ev->button) {
129 default:
130 break;
131 case Button1:
132 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
133 restack(c);
134 movemouse(c);
135 }
136 break;
137 case Button2:
138 zoom(NULL);
139 break;
140 case Button3:
141 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
142 restack(c);
143 resizemouse(c);
144 }
145 break;
146 }
147 }
148 }
150 static void
151 configurerequest(XEvent *e)
152 {
153 int ox, oy, ow, oh;
154 unsigned long newmask;
155 Client *c;
156 XConfigureRequestEvent *ev = &e->xconfigurerequest;
157 XEvent synev;
158 XWindowChanges wc;
160 if((c = getclient(ev->window))) {
161 ox = c->x;
162 oy = c->y;
163 ow = c->w;
164 oh = c->h;
165 gravitate(c, True);
166 if(ev->value_mask & CWX)
167 c->x = ev->x;
168 if(ev->value_mask & CWY)
169 c->y = ev->y;
170 if(ev->value_mask & CWWidth)
171 c->w = ev->width;
172 if(ev->value_mask & CWHeight)
173 c->h = ev->height;
174 if(ev->value_mask & CWBorderWidth)
175 c->border = ev->border_width;
176 gravitate(c, False);
177 wc.x = c->x;
178 wc.y = c->y;
179 wc.width = c->w;
180 wc.height = c->h;
181 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
182 if(newmask)
183 XConfigureWindow(dpy, c->win, newmask, &wc);
184 else {
185 synev.type = ConfigureNotify;
186 synev.xconfigure.display = dpy;
187 synev.xconfigure.event = c->win;
188 synev.xconfigure.window = c->win;
189 synev.xconfigure.x = c->x;
190 synev.xconfigure.y = c->y;
191 synev.xconfigure.width = c->w;
192 synev.xconfigure.height = c->h;
193 synev.xconfigure.border_width = c->border;
194 synev.xconfigure.above = None;
195 /* Send synthetic ConfigureNotify */
196 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
197 }
198 XSync(dpy, False);
199 if(c->isfloat)
200 resize(c, False, TopLeft);
201 else if(c->ismax) {
202 resize(c, False, TopLeft);
203 c->x = ox;
204 c->y = oy;
205 c->w = ow;
206 c->h = oh;
207 }
208 else
209 arrange(NULL);
210 }
211 else {
212 wc.x = ev->x;
213 wc.y = ev->y;
214 wc.width = ev->width;
215 wc.height = ev->height;
216 wc.border_width = ev->border_width;
217 wc.sibling = ev->above;
218 wc.stack_mode = ev->detail;
219 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
220 XSync(dpy, False);
221 }
222 }
224 static void
225 destroynotify(XEvent *e)
226 {
227 Client *c;
228 XDestroyWindowEvent *ev = &e->xdestroywindow;
230 if((c = getclient(ev->window)))
231 unmanage(c);
232 }
234 static void
235 enternotify(XEvent *e)
236 {
237 Client *c;
238 XCrossingEvent *ev = &e->xcrossing;
240 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
241 return;
243 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
244 focus(c);
245 else if(ev->window == root) {
246 issel = True;
247 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
248 drawall();
249 }
250 }
252 static void
253 expose(XEvent *e)
254 {
255 Client *c;
256 XExposeEvent *ev = &e->xexpose;
258 if(ev->count == 0) {
259 if(barwin == ev->window)
260 drawstatus();
261 else if((c = getctitle(ev->window)))
262 drawtitle(c);
263 }
264 }
266 static void
267 keypress(XEvent *e)
268 {
269 static unsigned int len = sizeof(key) / sizeof(key[0]);
270 unsigned int i;
271 KeySym keysym;
272 XKeyEvent *ev = &e->xkey;
274 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
275 for(i = 0; i < len; i++) {
276 if(keysym == key[i].keysym &&
277 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
278 {
279 if(key[i].func)
280 key[i].func(&key[i].arg);
281 return;
282 }
283 }
284 }
286 static void
287 leavenotify(XEvent *e)
288 {
289 XCrossingEvent *ev = &e->xcrossing;
291 if((ev->window == root) && !ev->same_screen) {
292 issel = False;
293 drawall();
294 }
295 }
297 static void
298 mappingnotify(XEvent *e)
299 {
300 XMappingEvent *ev = &e->xmapping;
302 XRefreshKeyboardMapping(ev);
303 if(ev->request == MappingKeyboard)
304 grabkeys();
305 }
307 static void
308 maprequest(XEvent *e)
309 {
310 static XWindowAttributes wa;
311 XMapRequestEvent *ev = &e->xmaprequest;
313 if(!XGetWindowAttributes(dpy, ev->window, &wa))
314 return;
316 if(wa.override_redirect) {
317 XSelectInput(dpy, ev->window,
318 (StructureNotifyMask | PropertyChangeMask));
319 return;
320 }
322 if(!getclient(ev->window))
323 manage(ev->window, &wa);
324 }
326 static void
327 propertynotify(XEvent *e)
328 {
329 Client *c;
330 Window trans;
331 XPropertyEvent *ev = &e->xproperty;
333 if(ev->state == PropertyDelete)
334 return; /* ignore */
336 if((c = getclient(ev->window))) {
337 if(ev->atom == wmatom[WMProtocols]) {
338 c->proto = getproto(c->win);
339 return;
340 }
341 switch (ev->atom) {
342 default: break;
343 case XA_WM_TRANSIENT_FOR:
344 XGetTransientForHint(dpy, c->win, &trans);
345 if(!c->isfloat && (c->isfloat = (trans != 0)))
346 arrange(NULL);
347 break;
348 case XA_WM_NORMAL_HINTS:
349 setsize(c);
350 break;
351 }
352 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
353 settitle(c);
354 drawtitle(c);
355 }
356 }
357 }
359 static void
360 unmapnotify(XEvent *e)
361 {
362 Client *c;
363 XUnmapEvent *ev = &e->xunmap;
365 if((c = getclient(ev->window)))
366 unmanage(c);
367 }
369 /* extern */
371 void (*handler[LASTEvent]) (XEvent *) = {
372 [ButtonPress] = buttonpress,
373 [ConfigureRequest] = configurerequest,
374 [DestroyNotify] = destroynotify,
375 [EnterNotify] = enternotify,
376 [LeaveNotify] = leavenotify,
377 [Expose] = expose,
378 [KeyPress] = keypress,
379 [MappingNotify] = mappingnotify,
380 [MapRequest] = maprequest,
381 [PropertyNotify] = propertynotify,
382 [UnmapNotify] = unmapnotify
383 };
385 void
386 grabkeys()
387 {
388 static unsigned int len = sizeof(key) / sizeof(key[0]);
389 unsigned int i;
390 KeyCode code;
392 XUngrabKey(dpy, AnyKey, AnyModifier, root);
393 for(i = 0; i < len; i++) {
394 code = XKeysymToKeycode(dpy, key[i].keysym);
395 XGrabKey(dpy, code, key[i].mod, root, True,
396 GrabModeAsync, GrabModeAsync);
397 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
398 GrabModeAsync, GrabModeAsync);
399 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
400 GrabModeAsync, GrabModeAsync);
401 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
402 GrabModeAsync, GrabModeAsync);
403 }
404 }
406 void
407 procevent()
408 {
409 XEvent ev;
411 while(XPending(dpy)) {
412 XNextEvent(dpy, &ev);
413 if(handler[ev.type])
414 (handler[ev.type])(&ev); /* call handler */
415 }
416 }