dwm-meillo

view event.c @ 533:a5567a0d3011

do* has no Arg arument anymore (never called directly)
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 06 Oct 2006 13:06:37 +0200
parents 651f2c868b31
children 00ccae001069
line source
1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <X11/keysym.h>
7 #include <X11/Xatom.h>
9 /* static */
11 typedef struct {
12 unsigned long mod;
13 KeySym keysym;
14 void (*func)(Arg *arg);
15 Arg arg;
16 } Key;
18 KEYS
20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
22 static void
23 movemouse(Client *c) {
24 int x1, y1, ocx, ocy, di;
25 unsigned int dui;
26 Window dummy;
27 XEvent ev;
29 ocx = c->x;
30 ocy = c->y;
31 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
32 None, cursor[CurMove], CurrentTime) != GrabSuccess)
33 return;
34 c->ismax = False;
35 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
36 for(;;) {
37 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
38 switch (ev.type) {
39 case ButtonRelease:
40 resize(c, True, TopLeft);
41 XUngrabPointer(dpy, CurrentTime);
42 return;
43 case Expose:
44 handler[Expose](&ev);
45 break;
46 case MotionNotify:
47 XSync(dpy, False);
48 c->x = ocx + (ev.xmotion.x - x1);
49 c->y = ocy + (ev.xmotion.y - y1);
50 resize(c, False, TopLeft);
51 break;
52 }
53 }
54 }
56 static void
57 resizemouse(Client *c) {
58 int ocx, ocy;
59 int nw, nh;
60 Corner sticky;
61 XEvent ev;
63 ocx = c->x;
64 ocy = c->y;
65 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
66 None, cursor[CurResize], CurrentTime) != GrabSuccess)
67 return;
68 c->ismax = False;
69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
70 for(;;) {
71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
72 switch(ev.type) {
73 case ButtonRelease:
74 resize(c, True, TopLeft);
75 XUngrabPointer(dpy, CurrentTime);
76 return;
77 case Expose:
78 handler[Expose](&ev);
79 break;
80 case MotionNotify:
81 XSync(dpy, False);
82 if((nw = abs(ocx - ev.xmotion.x)))
83 c->w = nw;
84 if((nh = abs(ocy - ev.xmotion.y)))
85 c->h = nh;
86 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
87 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
88 if(ocx <= ev.xmotion.x)
89 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
90 else
91 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
92 resize(c, True, sticky);
93 break;
94 }
95 }
96 }
98 static void
99 buttonpress(XEvent *e) {
100 int x;
101 Arg a;
102 Client *c;
103 XButtonPressedEvent *ev = &e->xbutton;
105 if(barwin == ev->window) {
106 x = 0;
107 for(a.i = 0; a.i < ntags; a.i++) {
108 x += textw(tags[a.i]);
109 if(ev->x < x) {
110 if(ev->button == Button1) {
111 if(ev->state & MODKEY)
112 tag(&a);
113 else
114 view(&a);
115 }
116 else if(ev->button == Button3) {
117 if(ev->state & MODKEY)
118 toggletag(&a);
119 else
120 toggleview(&a);
121 }
122 return;
123 }
124 }
125 if((ev->x < x + bmw) && (ev->button == Button1))
126 togglemode(NULL);
127 }
128 else if((c = getclient(ev->window))) {
129 focus(c);
130 if(CLEANMASK(ev->state) != MODKEY)
131 return;
132 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
133 restack();
134 movemouse(c);
135 }
136 else if(ev->button == Button2)
137 zoom(NULL);
138 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
139 restack();
140 resizemouse(c);
141 }
142 }
143 }
145 static void
146 configurerequest(XEvent *e) {
147 unsigned long newmask;
148 Client *c;
149 XConfigureRequestEvent *ev = &e->xconfigurerequest;
150 XWindowChanges wc;
152 if((c = getclient(ev->window))) {
153 c->ismax = False;
154 gravitate(c, True);
155 if(ev->value_mask & CWX)
156 c->x = ev->x;
157 if(ev->value_mask & CWY)
158 c->y = ev->y;
159 if(ev->value_mask & CWWidth)
160 c->w = ev->width;
161 if(ev->value_mask & CWHeight)
162 c->h = ev->height;
163 if(ev->value_mask & CWBorderWidth)
164 c->border = ev->border_width;
165 gravitate(c, False);
166 wc.x = c->x;
167 wc.y = c->y;
168 wc.width = c->w;
169 wc.height = c->h;
170 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
171 if(newmask)
172 XConfigureWindow(dpy, c->win, newmask, &wc);
173 else
174 configure(c);
175 XSync(dpy, False);
176 if(c->isfloat) {
177 resize(c, False, TopLeft);
178 if(!isvisible(c))
179 ban(c);
180 }
181 else
182 arrange();
183 }
184 else {
185 wc.x = ev->x;
186 wc.y = ev->y;
187 wc.width = ev->width;
188 wc.height = ev->height;
189 wc.border_width = ev->border_width;
190 wc.sibling = ev->above;
191 wc.stack_mode = ev->detail;
192 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
193 XSync(dpy, False);
194 }
195 }
197 static void
198 destroynotify(XEvent *e) {
199 Client *c;
200 XDestroyWindowEvent *ev = &e->xdestroywindow;
202 if((c = getclient(ev->window)))
203 unmanage(c);
204 }
206 static void
207 enternotify(XEvent *e) {
208 Client *c;
209 XCrossingEvent *ev = &e->xcrossing;
211 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
212 return;
213 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
214 focus(c);
215 else if(ev->window == root) {
216 issel = True;
217 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
218 drawall();
219 }
220 }
222 static void
223 expose(XEvent *e) {
224 Client *c;
225 XExposeEvent *ev = &e->xexpose;
227 if(ev->count == 0) {
228 if(barwin == ev->window)
229 drawstatus();
230 else if((c = getctitle(ev->window)))
231 drawtitle(c);
232 }
233 }
235 static void
236 keypress(XEvent *e) {
237 static unsigned int len = sizeof(key) / sizeof(key[0]);
238 unsigned int i;
239 KeySym keysym;
240 XKeyEvent *ev = &e->xkey;
242 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
243 for(i = 0; i < len; i++) {
244 if(keysym == key[i].keysym
245 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
246 {
247 if(key[i].func)
248 key[i].func(&key[i].arg);
249 return;
250 }
251 }
252 }
254 static void
255 leavenotify(XEvent *e) {
256 XCrossingEvent *ev = &e->xcrossing;
258 if((ev->window == root) && !ev->same_screen) {
259 issel = False;
260 drawall();
261 }
262 }
264 static void
265 mappingnotify(XEvent *e) {
266 XMappingEvent *ev = &e->xmapping;
268 XRefreshKeyboardMapping(ev);
269 if(ev->request == MappingKeyboard)
270 grabkeys();
271 }
273 static void
274 maprequest(XEvent *e) {
275 static XWindowAttributes wa;
276 XMapRequestEvent *ev = &e->xmaprequest;
278 if(!XGetWindowAttributes(dpy, ev->window, &wa))
279 return;
280 if(wa.override_redirect) {
281 XSelectInput(dpy, ev->window,
282 (StructureNotifyMask | PropertyChangeMask));
283 return;
284 }
285 if(!getclient(ev->window))
286 manage(ev->window, &wa);
287 }
289 static void
290 propertynotify(XEvent *e) {
291 Client *c;
292 Window trans;
293 XPropertyEvent *ev = &e->xproperty;
295 if(ev->state == PropertyDelete)
296 return; /* ignore */
297 if((c = getclient(ev->window))) {
298 if(ev->atom == wmatom[WMProtocols]) {
299 c->proto = getproto(c->win);
300 return;
301 }
302 switch (ev->atom) {
303 default: break;
304 case XA_WM_TRANSIENT_FOR:
305 XGetTransientForHint(dpy, c->win, &trans);
306 if(!c->isfloat && (c->isfloat = (trans != 0)))
307 arrange();
308 break;
309 case XA_WM_NORMAL_HINTS:
310 updatesize(c);
311 break;
312 }
313 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
314 updatetitle(c);
315 resizetitle(c);
316 drawtitle(c);
317 }
318 }
319 }
321 static void
322 unmapnotify(XEvent *e) {
323 Client *c;
324 XUnmapEvent *ev = &e->xunmap;
326 if((c = getclient(ev->window)))
327 unmanage(c);
328 }
330 /* extern */
332 void (*handler[LASTEvent]) (XEvent *) = {
333 [ButtonPress] = buttonpress,
334 [ConfigureRequest] = configurerequest,
335 [DestroyNotify] = destroynotify,
336 [EnterNotify] = enternotify,
337 [LeaveNotify] = leavenotify,
338 [Expose] = expose,
339 [KeyPress] = keypress,
340 [MappingNotify] = mappingnotify,
341 [MapRequest] = maprequest,
342 [PropertyNotify] = propertynotify,
343 [UnmapNotify] = unmapnotify
344 };
346 void
347 grabkeys(void) {
348 static unsigned int len = sizeof(key) / sizeof(key[0]);
349 unsigned int i;
350 KeyCode code;
352 XUngrabKey(dpy, AnyKey, AnyModifier, root);
353 for(i = 0; i < len; i++) {
354 code = XKeysymToKeycode(dpy, key[i].keysym);
355 XGrabKey(dpy, code, key[i].mod, root, True,
356 GrabModeAsync, GrabModeAsync);
357 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
358 GrabModeAsync, GrabModeAsync);
359 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
360 GrabModeAsync, GrabModeAsync);
361 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
362 GrabModeAsync, GrabModeAsync);
363 }
364 }
366 void
367 procevent(void) {
368 XEvent ev;
370 while(XPending(dpy)) {
371 XNextEvent(dpy, &ev);
372 if(handler[ev.type])
373 (handler[ev.type])(&ev); /* call handler */
374 }
375 }