aewl

view event.c @ 507:2824b5d0f0f0

prelim of dotile()
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 29 Sep 2006 14:39:03 +0200
parents 2c29d74b11dc
children ede48935f2b3
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 resize(c, True, TopLeft);
42 XUngrabPointer(dpy, CurrentTime);
43 return;
44 case Expose:
45 handler[Expose](&ev);
46 break;
47 case MotionNotify:
48 XSync(dpy, False);
49 c->x = ocx + (ev.xmotion.x - x1);
50 c->y = ocy + (ev.xmotion.y - y1);
51 resize(c, False, TopLeft);
52 break;
53 }
54 }
55 }
57 static void
58 resizemouse(Client *c) {
59 int ocx, ocy;
60 int nw, nh;
61 Corner sticky;
62 XEvent ev;
64 ocx = c->x;
65 ocy = c->y;
66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
68 return;
69 c->ismax = False;
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 case ButtonRelease:
75 resize(c, True, TopLeft);
76 XUngrabPointer(dpy, CurrentTime);
77 return;
78 case Expose:
79 handler[Expose](&ev);
80 break;
81 case MotionNotify:
82 XSync(dpy, False);
83 if((nw = abs(ocx - ev.xmotion.x)))
84 c->w = nw;
85 if((nh = abs(ocy - ev.xmotion.y)))
86 c->h = nh;
87 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
88 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
89 if(ocx <= ev.xmotion.x)
90 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
91 else
92 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
93 resize(c, True, sticky);
94 break;
95 }
96 }
97 }
99 static void
100 buttonpress(XEvent *e) {
101 int x;
102 Arg a;
103 Client *c;
104 XButtonPressedEvent *ev = &e->xbutton;
106 if(barwin == ev->window) {
107 x = 0;
108 for(a.i = 0; a.i < ntags; a.i++) {
109 x += textw(tags[a.i]);
110 if(ev->x < x) {
111 if(ev->button == Button1) {
112 if(ev->state & MODKEY)
113 tag(&a);
114 else
115 view(&a);
116 }
117 else if(ev->button == Button3) {
118 if(ev->state & MODKEY)
119 toggletag(&a);
120 else
121 toggleview(&a);
122 }
123 return;
124 }
125 }
126 if(ev->x < x + bmw) {
127 if(ev->button == Button1)
128 togglemode(NULL);
129 }
130 }
131 else if((c = getclient(ev->window))) {
132 focus(c);
133 if(CLEANMASK(ev->state) != MODKEY)
134 return;
135 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
136 restack();
137 movemouse(c);
138 }
139 else if(ev->button == Button2)
140 zoom(NULL);
141 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
142 restack();
143 resizemouse(c);
144 }
145 }
146 }
148 static void
149 configurerequest(XEvent *e) {
150 unsigned long newmask;
151 Client *c;
152 XConfigureRequestEvent *ev = &e->xconfigurerequest;
153 XWindowChanges wc;
155 if((c = getclient(ev->window))) {
156 c->ismax = False;
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 = ev->border_width;
168 gravitate(c, False);
169 wc.x = c->x;
170 wc.y = c->y;
171 wc.width = c->w;
172 wc.height = c->h;
173 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
174 if(newmask)
175 XConfigureWindow(dpy, c->win, newmask, &wc);
176 else
177 configure(c);
178 XSync(dpy, False);
179 if(c->isfloat) {
180 if(isvisible(c))
181 resize(c, False, TopLeft);
182 }
183 else
184 arrange(NULL);
185 }
186 else {
187 wc.x = ev->x;
188 wc.y = ev->y;
189 wc.width = ev->width;
190 wc.height = ev->height;
191 wc.border_width = ev->border_width;
192 wc.sibling = ev->above;
193 wc.stack_mode = ev->detail;
194 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
195 XSync(dpy, False);
196 }
197 }
199 static void
200 destroynotify(XEvent *e) {
201 Client *c;
202 XDestroyWindowEvent *ev = &e->xdestroywindow;
204 if((c = getclient(ev->window)))
205 unmanage(c);
206 }
208 static void
209 enternotify(XEvent *e) {
210 Client *c;
211 XCrossingEvent *ev = &e->xcrossing;
213 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
214 return;
216 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
217 focus(c);
218 else if(ev->window == root) {
219 issel = True;
220 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
221 drawall();
222 }
223 }
225 static void
226 expose(XEvent *e) {
227 Client *c;
228 XExposeEvent *ev = &e->xexpose;
230 if(ev->count == 0) {
231 if(barwin == ev->window)
232 drawstatus();
233 else if((c = getctitle(ev->window)))
234 drawtitle(c);
235 }
236 }
238 static void
239 keypress(XEvent *e) {
240 static unsigned int len = sizeof(key) / sizeof(key[0]);
241 unsigned int i;
242 KeySym keysym;
243 XKeyEvent *ev = &e->xkey;
245 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
246 for(i = 0; i < len; i++) {
247 if(keysym == key[i].keysym
248 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
249 {
250 if(key[i].func)
251 key[i].func(&key[i].arg);
252 return;
253 }
254 }
255 }
257 static void
258 leavenotify(XEvent *e) {
259 XCrossingEvent *ev = &e->xcrossing;
261 if((ev->window == root) && !ev->same_screen) {
262 issel = False;
263 drawall();
264 }
265 }
267 static void
268 mappingnotify(XEvent *e) {
269 XMappingEvent *ev = &e->xmapping;
271 XRefreshKeyboardMapping(ev);
272 if(ev->request == MappingKeyboard)
273 grabkeys();
274 }
276 static void
277 maprequest(XEvent *e) {
278 static XWindowAttributes wa;
279 XMapRequestEvent *ev = &e->xmaprequest;
281 if(!XGetWindowAttributes(dpy, ev->window, &wa))
282 return;
284 if(wa.override_redirect) {
285 XSelectInput(dpy, ev->window,
286 (StructureNotifyMask | PropertyChangeMask));
287 return;
288 }
290 if(!getclient(ev->window))
291 manage(ev->window, &wa);
292 }
294 static void
295 propertynotify(XEvent *e) {
296 Client *c;
297 Window trans;
298 XPropertyEvent *ev = &e->xproperty;
300 if(ev->state == PropertyDelete)
301 return; /* ignore */
303 if((c = getclient(ev->window))) {
304 if(ev->atom == wmatom[WMProtocols]) {
305 c->proto = getproto(c->win);
306 return;
307 }
308 switch (ev->atom) {
309 default: break;
310 case XA_WM_TRANSIENT_FOR:
311 XGetTransientForHint(dpy, c->win, &trans);
312 if(!c->isfloat && (c->isfloat = (trans != 0)))
313 arrange(NULL);
314 break;
315 case XA_WM_NORMAL_HINTS:
316 updatesize(c);
317 break;
318 }
319 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
320 updatetitle(c);
321 resizetitle(c);
322 drawtitle(c);
323 }
324 }
325 }
327 static void
328 unmapnotify(XEvent *e) {
329 Client *c;
330 XUnmapEvent *ev = &e->xunmap;
332 if((c = getclient(ev->window)))
333 unmanage(c);
334 }
336 /* extern */
338 void (*handler[LASTEvent]) (XEvent *) = {
339 [ButtonPress] = buttonpress,
340 [ConfigureRequest] = configurerequest,
341 [DestroyNotify] = destroynotify,
342 [EnterNotify] = enternotify,
343 [LeaveNotify] = leavenotify,
344 [Expose] = expose,
345 [KeyPress] = keypress,
346 [MappingNotify] = mappingnotify,
347 [MapRequest] = maprequest,
348 [PropertyNotify] = propertynotify,
349 [UnmapNotify] = unmapnotify
350 };
352 void
353 grabkeys(void) {
354 static unsigned int len = sizeof(key) / sizeof(key[0]);
355 unsigned int i;
356 KeyCode code;
358 XUngrabKey(dpy, AnyKey, AnyModifier, root);
359 for(i = 0; i < len; i++) {
360 code = XKeysymToKeycode(dpy, key[i].keysym);
361 XGrabKey(dpy, code, key[i].mod, root, True,
362 GrabModeAsync, GrabModeAsync);
363 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
364 GrabModeAsync, GrabModeAsync);
365 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
368 GrabModeAsync, GrabModeAsync);
369 }
370 }
372 void
373 procevent(void) {
374 XEvent ev;
376 while(XPending(dpy)) {
377 XNextEvent(dpy, &ev);
378 if(handler[ev.type])
379 (handler[ev.type])(&ev); /* call handler */
380 }
381 }