dwm-meillo

view event.c @ 367:5870a42e4c8d

patched arg.h
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 25 Aug 2006 17:42:58 +0200
parents c2a65784cb71
children fc9d35252ab4
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 togglemode(NULL);
121 }
122 else if((c = getclient(ev->window))) {
123 focus(c);
124 if(CLEANMASK(ev->state) == 0)
125 return;
126 switch(ev->button) {
127 default:
128 break;
129 case Button1:
130 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
131 restack(c);
132 movemouse(c);
133 }
134 break;
135 case Button2:
136 zoom(NULL);
137 break;
138 case Button3:
139 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
140 restack(c);
141 resizemouse(c);
142 }
143 break;
144 }
145 }
146 }
148 static void
149 configurerequest(XEvent *e)
150 {
151 unsigned long newmask;
152 Client *c;
153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
154 XEvent synev;
155 XWindowChanges wc;
157 if((c = getclient(ev->window))) {
158 gravitate(c, True);
159 if(ev->value_mask & CWX)
160 c->x = ev->x;
161 if(ev->value_mask & CWY)
162 c->y = ev->y;
163 if(ev->value_mask & CWWidth)
164 c->w = ev->width;
165 if(ev->value_mask & CWHeight)
166 c->h = ev->height;
167 if(ev->value_mask & CWBorderWidth)
168 c->border = ev->border_width;
169 gravitate(c, False);
170 wc.x = c->x;
171 wc.y = c->y;
172 wc.width = c->w;
173 wc.height = c->h;
174 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
175 if(newmask)
176 XConfigureWindow(dpy, c->win, newmask, &wc);
177 else {
178 synev.type = ConfigureNotify;
179 synev.xconfigure.display = dpy;
180 synev.xconfigure.event = c->win;
181 synev.xconfigure.window = c->win;
182 synev.xconfigure.x = c->x;
183 synev.xconfigure.y = c->y;
184 synev.xconfigure.width = c->w;
185 synev.xconfigure.height = c->h;
186 synev.xconfigure.border_width = c->border;
187 synev.xconfigure.above = None;
188 /* Send synthetic ConfigureNotify */
189 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
190 }
191 XSync(dpy, False);
192 if(c->isfloat)
193 resize(c, False, TopLeft);
194 else
195 arrange(NULL);
196 }
197 else {
198 wc.x = ev->x;
199 wc.y = ev->y;
200 wc.width = ev->width;
201 wc.height = ev->height;
202 wc.border_width = ev->border_width;
203 wc.sibling = ev->above;
204 wc.stack_mode = ev->detail;
205 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
206 XSync(dpy, False);
207 }
208 }
210 static void
211 destroynotify(XEvent *e)
212 {
213 Client *c;
214 XDestroyWindowEvent *ev = &e->xdestroywindow;
216 if((c = getclient(ev->window)))
217 unmanage(c);
218 }
220 static void
221 enternotify(XEvent *e)
222 {
223 Client *c;
224 XCrossingEvent *ev = &e->xcrossing;
226 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
227 return;
229 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
230 focus(c);
231 else if(ev->window == root) {
232 issel = True;
233 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
234 drawall();
235 }
236 }
238 static void
239 expose(XEvent *e)
240 {
241 Client *c;
242 XExposeEvent *ev = &e->xexpose;
244 if(ev->count == 0) {
245 if(barwin == ev->window)
246 drawstatus();
247 else if((c = getctitle(ev->window)))
248 drawtitle(c);
249 }
250 }
252 static void
253 keypress(XEvent *e)
254 {
255 static unsigned int len = sizeof(key) / sizeof(key[0]);
256 unsigned int i;
257 KeySym keysym;
258 XKeyEvent *ev = &e->xkey;
260 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
261 for(i = 0; i < len; i++) {
262 if(keysym == key[i].keysym &&
263 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
264 {
265 if(key[i].func)
266 key[i].func(&key[i].arg);
267 return;
268 }
269 }
270 }
272 static void
273 leavenotify(XEvent *e)
274 {
275 XCrossingEvent *ev = &e->xcrossing;
277 if((ev->window == root) && !ev->same_screen) {
278 issel = False;
279 drawall();
280 }
281 }
283 static void
284 mappingnotify(XEvent *e)
285 {
286 XMappingEvent *ev = &e->xmapping;
288 XRefreshKeyboardMapping(ev);
289 if(ev->request == MappingKeyboard)
290 grabkeys();
291 }
293 static void
294 maprequest(XEvent *e)
295 {
296 static XWindowAttributes wa;
297 XMapRequestEvent *ev = &e->xmaprequest;
299 if(!XGetWindowAttributes(dpy, ev->window, &wa))
300 return;
302 if(wa.override_redirect) {
303 XSelectInput(dpy, ev->window,
304 (StructureNotifyMask | PropertyChangeMask));
305 return;
306 }
308 if(!getclient(ev->window))
309 manage(ev->window, &wa);
310 }
312 static void
313 propertynotify(XEvent *e)
314 {
315 Client *c;
316 Window trans;
317 XPropertyEvent *ev = &e->xproperty;
319 if(ev->state == PropertyDelete)
320 return; /* ignore */
322 if((c = getclient(ev->window))) {
323 if(ev->atom == wmatom[WMProtocols]) {
324 c->proto = getproto(c->win);
325 return;
326 }
327 switch (ev->atom) {
328 default: break;
329 case XA_WM_TRANSIENT_FOR:
330 XGetTransientForHint(dpy, c->win, &trans);
331 if(!c->isfloat && (c->isfloat = (trans != 0)))
332 arrange(NULL);
333 break;
334 case XA_WM_NORMAL_HINTS:
335 setsize(c);
336 break;
337 }
338 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
339 settitle(c);
340 drawtitle(c);
341 }
342 }
343 }
345 static void
346 unmapnotify(XEvent *e)
347 {
348 Client *c;
349 XUnmapEvent *ev = &e->xunmap;
351 if((c = getclient(ev->window)))
352 unmanage(c);
353 }
355 /* extern */
357 void (*handler[LASTEvent]) (XEvent *) = {
358 [ButtonPress] = buttonpress,
359 [ConfigureRequest] = configurerequest,
360 [DestroyNotify] = destroynotify,
361 [EnterNotify] = enternotify,
362 [LeaveNotify] = leavenotify,
363 [Expose] = expose,
364 [KeyPress] = keypress,
365 [MappingNotify] = mappingnotify,
366 [MapRequest] = maprequest,
367 [PropertyNotify] = propertynotify,
368 [UnmapNotify] = unmapnotify
369 };
371 void
372 grabkeys()
373 {
374 static unsigned int len = sizeof(key) / sizeof(key[0]);
375 unsigned int i;
376 KeyCode code;
378 XUngrabKey(dpy, AnyKey, AnyModifier, root);
379 for(i = 0; i < len; i++) {
380 code = XKeysymToKeycode(dpy, key[i].keysym);
381 XGrabKey(dpy, code, key[i].mod, root, True,
382 GrabModeAsync, GrabModeAsync);
383 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
384 GrabModeAsync, GrabModeAsync);
385 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
386 GrabModeAsync, GrabModeAsync);
387 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
388 GrabModeAsync, GrabModeAsync);
389 }
390 }
392 void
393 procevent()
394 {
395 XEvent ev;
397 while(XPending(dpy)) {
398 XNextEvent(dpy, &ev);
399 if(handler[ev.type])
400 (handler[ev.type])(&ev); /* call handler */
401 }
402 }