dwm-meillo

view event.c @ 483:0db173d7e59c

man page fix
author arg@mmvi
date Fri, 22 Sep 2006 14:02:41 +0200
parents 635b58a87d20
children be4f90c03582
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 synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
25 XEvent synev;
27 synev.type = ConfigureNotify;
28 synev.xconfigure.display = dpy;
29 synev.xconfigure.event = c->win;
30 synev.xconfigure.window = c->win;
31 synev.xconfigure.x = x;
32 synev.xconfigure.y = y;
33 synev.xconfigure.width = w;
34 synev.xconfigure.height = h;
35 synev.xconfigure.border_width = border;
36 synev.xconfigure.above = None;
37 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
38 }
40 static void
41 movemouse(Client *c) {
42 int x1, y1, ocx, ocy, di;
43 unsigned int dui;
44 Window dummy;
45 XEvent ev;
47 ocx = c->x;
48 ocy = c->y;
49 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
50 None, cursor[CurMove], CurrentTime) != GrabSuccess)
51 return;
52 c->ismax = False;
53 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
54 for(;;) {
55 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
56 switch (ev.type) {
57 default:
58 break;
59 case ConfigureRequest:
60 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
61 XSync(dpy, False);
62 break;
63 case Expose:
64 handler[Expose](&ev);
65 break;
66 case MotionNotify:
67 XSync(dpy, False);
68 c->x = ocx + (ev.xmotion.x - x1);
69 c->y = ocy + (ev.xmotion.y - y1);
70 resize(c, False, TopLeft);
71 break;
72 case ButtonRelease:
73 XUngrabPointer(dpy, CurrentTime);
74 return;
75 case DestroyNotify:
76 case UnmapNotify:
77 XUngrabPointer(dpy, CurrentTime);
78 handler[ev.type](&ev);
79 return;
80 }
81 }
82 }
84 static void
85 resizemouse(Client *c) {
86 int ocx, ocy;
87 int nw, nh;
88 Corner sticky;
89 XEvent ev;
91 ocx = c->x;
92 ocy = c->y;
93 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
94 None, cursor[CurResize], CurrentTime) != GrabSuccess)
95 return;
96 c->ismax = False;
97 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
98 for(;;) {
99 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
100 switch(ev.type) {
101 default:
102 break;
103 case ConfigureRequest:
104 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
105 XSync(dpy, False);
106 break;
107 case Expose:
108 handler[Expose](&ev);
109 break;
110 case MotionNotify:
111 XSync(dpy, False);
112 if((nw = abs(ocx - ev.xmotion.x)))
113 c->w = nw;
114 if((nh = abs(ocy - ev.xmotion.y)))
115 c->h = nh;
116 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
117 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
118 if(ocx <= ev.xmotion.x)
119 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
120 else
121 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
122 resize(c, True, sticky);
123 break;
124 case ButtonRelease:
125 XUngrabPointer(dpy, CurrentTime);
126 return;
127 case DestroyNotify:
128 case UnmapNotify:
129 XUngrabPointer(dpy, CurrentTime);
130 handler[ev.type](&ev);
131 return;
132 }
133 }
134 }
136 static void
137 buttonpress(XEvent *e) {
138 int x;
139 Arg a;
140 Client *c;
141 XButtonPressedEvent *ev = &e->xbutton;
143 if(barwin == ev->window) {
144 x = 0;
145 for(a.i = 0; a.i < ntags; a.i++) {
146 x += textw(tags[a.i]);
147 if(ev->x < x) {
148 if(ev->button == Button1) {
149 if(ev->state & MODKEY)
150 tag(&a);
151 else
152 view(&a);
153 }
154 else if(ev->button == Button3) {
155 if(ev->state & MODKEY)
156 toggletag(&a);
157 else
158 toggleview(&a);
159 }
160 return;
161 }
162 }
163 if(ev->x < x + bmw) {
164 if(ev->button == Button1)
165 togglemode(NULL);
166 }
167 }
168 else if((c = getclient(ev->window))) {
169 focus(c);
170 if(CLEANMASK(ev->state) != MODKEY)
171 return;
172 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
173 restack(c);
174 movemouse(c);
175 }
176 else if(ev->button == Button2)
177 zoom(NULL);
178 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
179 restack(c);
180 resizemouse(c);
181 }
182 }
183 }
185 static void
186 configurerequest(XEvent *e) {
187 unsigned long newmask;
188 Client *c;
189 XConfigureRequestEvent *ev = &e->xconfigurerequest;
190 XWindowChanges wc;
192 if((c = getclient(ev->window))) {
193 if((c == sel) && !c->isfloat && (arrange != dofloat)) {
194 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
195 XSync(dpy, False);
196 return;
197 }
198 gravitate(c, True);
199 if(ev->value_mask & CWX)
200 c->x = ev->x;
201 if(ev->value_mask & CWY)
202 c->y = ev->y;
203 if(ev->value_mask & CWWidth)
204 c->w = ev->width;
205 if(ev->value_mask & CWHeight)
206 c->h = ev->height;
207 if(ev->value_mask & CWBorderWidth)
208 c->border = ev->border_width;
209 gravitate(c, False);
210 wc.x = c->x;
211 wc.y = c->y;
212 wc.width = c->w;
213 wc.height = c->h;
214 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
215 if(newmask)
216 XConfigureWindow(dpy, c->win, newmask, &wc);
217 else
218 synconfig(c, c->x, c->y, c->w, c->h, c->border);
219 XSync(dpy, False);
220 if(c->isfloat)
221 resize(c, False, TopLeft);
222 else
223 arrange(NULL);
224 }
225 else {
226 wc.x = ev->x;
227 wc.y = ev->y;
228 wc.width = ev->width;
229 wc.height = ev->height;
230 wc.border_width = ev->border_width;
231 wc.sibling = ev->above;
232 wc.stack_mode = ev->detail;
233 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
234 XSync(dpy, False);
235 }
236 }
238 static void
239 destroynotify(XEvent *e) {
240 Client *c;
241 XDestroyWindowEvent *ev = &e->xdestroywindow;
243 if((c = getclient(ev->window)))
244 unmanage(c);
245 }
247 static void
248 enternotify(XEvent *e) {
249 Client *c;
250 XCrossingEvent *ev = &e->xcrossing;
252 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
253 return;
255 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
256 focus(c);
257 else if(ev->window == root) {
258 issel = True;
259 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
260 drawall();
261 }
262 }
264 static void
265 expose(XEvent *e) {
266 Client *c;
267 XExposeEvent *ev = &e->xexpose;
269 if(ev->count == 0) {
270 if(barwin == ev->window)
271 drawstatus();
272 else if((c = getctitle(ev->window)))
273 drawtitle(c);
274 }
275 }
277 static void
278 keypress(XEvent *e) {
279 static unsigned int len = sizeof(key) / sizeof(key[0]);
280 unsigned int i;
281 KeySym keysym;
282 XKeyEvent *ev = &e->xkey;
284 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
285 for(i = 0; i < len; i++) {
286 if(keysym == key[i].keysym
287 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
288 {
289 if(key[i].func)
290 key[i].func(&key[i].arg);
291 return;
292 }
293 }
294 }
296 static void
297 leavenotify(XEvent *e) {
298 XCrossingEvent *ev = &e->xcrossing;
300 if((ev->window == root) && !ev->same_screen) {
301 issel = False;
302 drawall();
303 }
304 }
306 static void
307 mappingnotify(XEvent *e) {
308 XMappingEvent *ev = &e->xmapping;
310 XRefreshKeyboardMapping(ev);
311 if(ev->request == MappingKeyboard)
312 grabkeys();
313 }
315 static void
316 maprequest(XEvent *e) {
317 static XWindowAttributes wa;
318 XMapRequestEvent *ev = &e->xmaprequest;
320 if(!XGetWindowAttributes(dpy, ev->window, &wa))
321 return;
323 if(wa.override_redirect) {
324 XSelectInput(dpy, ev->window,
325 (StructureNotifyMask | PropertyChangeMask));
326 return;
327 }
329 if(!getclient(ev->window))
330 manage(ev->window, &wa);
331 }
333 static void
334 propertynotify(XEvent *e) {
335 Client *c;
336 Window trans;
337 XPropertyEvent *ev = &e->xproperty;
339 if(ev->state == PropertyDelete)
340 return; /* ignore */
342 if((c = getclient(ev->window))) {
343 if(ev->atom == wmatom[WMProtocols]) {
344 c->proto = getproto(c->win);
345 return;
346 }
347 switch (ev->atom) {
348 default: break;
349 case XA_WM_TRANSIENT_FOR:
350 XGetTransientForHint(dpy, c->win, &trans);
351 if(!c->isfloat && (c->isfloat = (trans != 0)))
352 arrange(NULL);
353 break;
354 case XA_WM_NORMAL_HINTS:
355 updatesize(c);
356 break;
357 }
358 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
359 updatetitle(c);
360 drawtitle(c);
361 }
362 }
363 }
365 static void
366 unmapnotify(XEvent *e) {
367 Client *c;
368 XUnmapEvent *ev = &e->xunmap;
370 if((c = getclient(ev->window)))
371 unmanage(c);
372 }
374 /* extern */
376 void (*handler[LASTEvent]) (XEvent *) = {
377 [ButtonPress] = buttonpress,
378 [ConfigureRequest] = configurerequest,
379 [DestroyNotify] = destroynotify,
380 [EnterNotify] = enternotify,
381 [LeaveNotify] = leavenotify,
382 [Expose] = expose,
383 [KeyPress] = keypress,
384 [MappingNotify] = mappingnotify,
385 [MapRequest] = maprequest,
386 [PropertyNotify] = propertynotify,
387 [UnmapNotify] = unmapnotify
388 };
390 void
391 grabkeys() {
392 static unsigned int len = sizeof(key) / sizeof(key[0]);
393 unsigned int i;
394 KeyCode code;
396 XUngrabKey(dpy, AnyKey, AnyModifier, root);
397 for(i = 0; i < len; i++) {
398 code = XKeysymToKeycode(dpy, key[i].keysym);
399 XGrabKey(dpy, code, key[i].mod, root, True,
400 GrabModeAsync, GrabModeAsync);
401 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
402 GrabModeAsync, GrabModeAsync);
403 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
404 GrabModeAsync, GrabModeAsync);
405 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
406 GrabModeAsync, GrabModeAsync);
407 }
408 }
410 void
411 procevent() {
412 XEvent ev;
414 while(XPending(dpy)) {
415 XNextEvent(dpy, &ev);
416 if(handler[ev.type])
417 (handler[ev.type])(&ev); /* call handler */
418 }
419 }