dwm-meillo

view event.c @ 395:7528080beb0e

added viewall to mainstream (only Ross Mohns version, not the toggle)
author Anselm R. Garbe <arg@10kloc.org>
date Thu, 31 Aug 2006 18:02:38 +0200
parents dca6aca09653
children 9c703e528e58
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 switch(ev->button) {
113 case Button1:
114 view(&a);
115 break;
116 case Button2:
117 toggletag(&a);
118 break;
119 case Button3:
120 toggleview(&a);
121 break;
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 switch(ev->button) {
136 default:
137 break;
138 case Button1:
139 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
140 restack(c);
141 movemouse(c);
142 }
143 break;
144 case Button2:
145 zoom(NULL);
146 break;
147 case Button3:
148 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
149 restack(c);
150 resizemouse(c);
151 }
152 break;
153 }
154 }
155 }
157 static void
158 synconfig(Client *c, int x, int y, int w, int h, unsigned int border)
159 {
160 XEvent synev;
162 synev.type = ConfigureNotify;
163 synev.xconfigure.display = dpy;
164 synev.xconfigure.event = c->win;
165 synev.xconfigure.window = c->win;
166 synev.xconfigure.x = x;
167 synev.xconfigure.y = y;
168 synev.xconfigure.width = w;
169 synev.xconfigure.height = h;
170 synev.xconfigure.border_width = border;
171 synev.xconfigure.above = None;
172 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
173 }
175 static void
176 configurerequest(XEvent *e)
177 {
178 unsigned long newmask;
179 Client *c;
180 XConfigureRequestEvent *ev = &e->xconfigurerequest;
181 XWindowChanges wc;
183 if((c = getclient(ev->window))) {
184 if(!c->isfloat && (arrange != dofloat) && c->ismax) {
185 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
186 XSync(dpy, False);
187 return;
188 }
189 gravitate(c, True);
190 if(ev->value_mask & CWX)
191 c->x = ev->x;
192 if(ev->value_mask & CWY)
193 c->y = ev->y;
194 if(ev->value_mask & CWWidth)
195 c->w = ev->width;
196 if(ev->value_mask & CWHeight)
197 c->h = ev->height;
198 if(ev->value_mask & CWBorderWidth)
199 c->border = ev->border_width;
200 gravitate(c, False);
201 wc.x = c->x;
202 wc.y = c->y;
203 wc.width = c->w;
204 wc.height = c->h;
205 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
206 if(newmask)
207 XConfigureWindow(dpy, c->win, newmask, &wc);
208 else
209 synconfig(c, c->x, c->y, c->w, c->h, c->border);
210 XSync(dpy, False);
211 if(c->isfloat)
212 resize(c, False, TopLeft);
213 else
214 arrange(NULL);
215 }
216 else {
217 wc.x = ev->x;
218 wc.y = ev->y;
219 wc.width = ev->width;
220 wc.height = ev->height;
221 wc.border_width = ev->border_width;
222 wc.sibling = ev->above;
223 wc.stack_mode = ev->detail;
224 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
225 XSync(dpy, False);
226 }
227 }
229 static void
230 destroynotify(XEvent *e)
231 {
232 Client *c;
233 XDestroyWindowEvent *ev = &e->xdestroywindow;
235 if((c = getclient(ev->window)))
236 unmanage(c);
237 }
239 static void
240 enternotify(XEvent *e)
241 {
242 Client *c;
243 XCrossingEvent *ev = &e->xcrossing;
245 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
246 return;
248 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
249 focus(c);
250 else if(ev->window == root) {
251 issel = True;
252 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
253 drawall();
254 }
255 }
257 static void
258 expose(XEvent *e)
259 {
260 Client *c;
261 XExposeEvent *ev = &e->xexpose;
263 if(ev->count == 0) {
264 if(barwin == ev->window)
265 drawstatus();
266 else if((c = getctitle(ev->window)))
267 drawtitle(c);
268 }
269 }
271 static void
272 keypress(XEvent *e)
273 {
274 static unsigned int len = sizeof(key) / sizeof(key[0]);
275 unsigned int i;
276 KeySym keysym;
277 XKeyEvent *ev = &e->xkey;
279 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
280 for(i = 0; i < len; i++) {
281 if(keysym == key[i].keysym &&
282 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
283 {
284 if(key[i].func)
285 key[i].func(&key[i].arg);
286 return;
287 }
288 }
289 }
291 static void
292 leavenotify(XEvent *e)
293 {
294 XCrossingEvent *ev = &e->xcrossing;
296 if((ev->window == root) && !ev->same_screen) {
297 issel = False;
298 drawall();
299 }
300 }
302 static void
303 mappingnotify(XEvent *e)
304 {
305 XMappingEvent *ev = &e->xmapping;
307 XRefreshKeyboardMapping(ev);
308 if(ev->request == MappingKeyboard)
309 grabkeys();
310 }
312 static void
313 maprequest(XEvent *e)
314 {
315 static XWindowAttributes wa;
316 XMapRequestEvent *ev = &e->xmaprequest;
318 if(!XGetWindowAttributes(dpy, ev->window, &wa))
319 return;
321 if(wa.override_redirect) {
322 XSelectInput(dpy, ev->window,
323 (StructureNotifyMask | PropertyChangeMask));
324 return;
325 }
327 if(!getclient(ev->window))
328 manage(ev->window, &wa);
329 }
331 static void
332 propertynotify(XEvent *e)
333 {
334 Client *c;
335 Window trans;
336 XPropertyEvent *ev = &e->xproperty;
338 if(ev->state == PropertyDelete)
339 return; /* ignore */
341 if((c = getclient(ev->window))) {
342 if(ev->atom == wmatom[WMProtocols]) {
343 c->proto = getproto(c->win);
344 return;
345 }
346 switch (ev->atom) {
347 default: break;
348 case XA_WM_TRANSIENT_FOR:
349 XGetTransientForHint(dpy, c->win, &trans);
350 if(!c->isfloat && (c->isfloat = (trans != 0)))
351 arrange(NULL);
352 break;
353 case XA_WM_NORMAL_HINTS:
354 setsize(c);
355 break;
356 }
357 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
358 settitle(c);
359 drawtitle(c);
360 }
361 }
362 }
364 static void
365 unmapnotify(XEvent *e)
366 {
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 {
393 static unsigned int len = sizeof(key) / sizeof(key[0]);
394 unsigned int i;
395 KeyCode code;
397 XUngrabKey(dpy, AnyKey, AnyModifier, root);
398 for(i = 0; i < len; i++) {
399 code = XKeysymToKeycode(dpy, key[i].keysym);
400 XGrabKey(dpy, code, key[i].mod, root, True,
401 GrabModeAsync, GrabModeAsync);
402 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
403 GrabModeAsync, GrabModeAsync);
404 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
405 GrabModeAsync, GrabModeAsync);
406 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
407 GrabModeAsync, GrabModeAsync);
408 }
409 }
411 void
412 procevent()
413 {
414 XEvent ev;
416 while(XPending(dpy)) {
417 XNextEvent(dpy, &ev);
418 if(handler[ev.type])
419 (handler[ev.type])(&ev); /* call handler */
420 }
421 }