aewl
view event.c @ 751:743061dbe7f2
made Mod-F4 (shutdown) work on notebook
author | meillo@marmaro.de |
---|---|
date | Sat, 16 Jun 2007 21:44:00 +0200 |
parents | 6283adb1fcf2 |
children |
line source
1 /* (C)opyright MMVI-MMVII 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))
21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
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 | SubstructureRedirectMask, &ev);
39 switch (ev.type) {
40 case ButtonRelease:
41 resize(c, True);
42 XUngrabPointer(dpy, CurrentTime);
43 return;
44 case ConfigureRequest:
45 case Expose:
46 case MapRequest:
47 handler[ev.type](&ev);
48 break;
49 case MotionNotify:
50 XSync(dpy, False);
51 c->x = ocx + (ev.xmotion.x - x1);
52 c->y = ocy + (ev.xmotion.y - y1);
53 if(abs(wax + c->x) < SNAP)
54 c->x = wax;
55 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
56 c->x = wax + waw - c->w - 2 * c->border;
57 if(abs(way - c->y) < SNAP)
58 c->y = way;
59 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
60 c->y = way + wah - c->h - 2 * c->border;
61 resize(c, False);
62 break;
63 }
64 }
65 }
67 static void
68 resizemouse(Client *c) {
69 int ocx, ocy;
70 int nw, nh;
71 XEvent ev;
73 ocx = c->x;
74 ocy = c->y;
75 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
76 None, cursor[CurResize], CurrentTime) != GrabSuccess)
77 return;
78 c->ismax = False;
79 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
80 for(;;) {
81 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
82 switch(ev.type) {
83 case ButtonRelease:
84 resize(c, True);
85 XUngrabPointer(dpy, CurrentTime);
86 return;
87 case ConfigureRequest:
88 case Expose:
89 case MapRequest:
90 handler[ev.type](&ev);
91 break;
92 case MotionNotify:
93 XSync(dpy, False);
94 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
95 c->w = nw > 0 ? nw : 1;
96 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
97 c->h = nh > 0 ? nh : 1;
98 resize(c, True);
99 break;
100 }
101 }
102 }
104 static void
105 buttonpress(XEvent *e) {
106 int x;
107 Arg a;
108 Client *c;
109 XButtonPressedEvent *ev = &e->xbutton;
111 if(barwin == ev->window) {
112 x = 0;
113 for(a.i = 0; a.i < ntags; a.i++) {
114 x += textw(tags[a.i]);
115 if(ev->x < x) {
116 if(ev->button == Button1) {
117 if(ev->state & MODKEY)
118 tag(&a);
119 else
120 view(&a);
121 }
122 else if(ev->button == Button3) {
123 if(ev->state & MODKEY)
124 toggletag(&a);
125 else
126 toggleview(&a);
127 }
128 return;
129 }
130 }
131 if(ev->x < x + bmw)
132 switch(ev->button) {
133 case Button1:
134 togglemode(NULL);
135 break;
136 case Button4:
137 a.i = 1;
138 incnmaster(&a);
139 break;
140 case Button5:
141 a.i = -1;
142 incnmaster(&a);
143 break;
144 }
145 }
146 else if((c = getclient(ev->window))) {
147 focus(c);
148 if(CLEANMASK(ev->state) != MODKEY)
149 return;
150 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
151 restack();
152 movemouse(c);
153 }
154 else if(ev->button == Button2)
155 zoom(NULL);
156 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
157 !c->isfixed) {
158 restack();
159 resizemouse(c);
160 }
161 }
162 }
164 static void
165 configurerequest(XEvent *e) {
166 unsigned long newmask;
167 Client *c;
168 XConfigureRequestEvent *ev = &e->xconfigurerequest;
169 XWindowChanges wc;
171 if((c = getclient(ev->window))) {
172 c->ismax = False;
173 if(ev->value_mask & CWX)
174 c->x = ev->x;
175 if(ev->value_mask & CWY)
176 c->y = ev->y;
177 if(ev->value_mask & CWWidth)
178 c->w = ev->width;
179 if(ev->value_mask & CWHeight)
180 c->h = ev->height;
181 if(ev->value_mask & CWBorderWidth)
182 c->border = ev->border_width;
183 wc.x = c->x;
184 wc.y = c->y;
185 wc.width = c->w;
186 wc.height = c->h;
187 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
188 if(newmask)
189 XConfigureWindow(dpy, c->win, newmask, &wc);
190 else
191 configure(c);
192 XSync(dpy, False);
193 if(c->isfloat) {
194 resize(c, False);
195 if(!isvisible(c))
196 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
197 }
198 else
199 arrange();
200 }
201 else {
202 wc.x = ev->x;
203 wc.y = ev->y;
204 wc.width = ev->width;
205 wc.height = ev->height;
206 wc.border_width = ev->border_width;
207 wc.sibling = ev->above;
208 wc.stack_mode = ev->detail;
209 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
210 XSync(dpy, False);
211 }
212 }
214 static void
215 destroynotify(XEvent *e) {
216 Client *c;
217 XDestroyWindowEvent *ev = &e->xdestroywindow;
219 if((c = getclient(ev->window)))
220 unmanage(c);
221 }
223 static void
224 enternotify(XEvent *e) {
225 Client *c;
226 XCrossingEvent *ev = &e->xcrossing;
228 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
229 return;
230 if((c = getclient(ev->window)) && isvisible(c))
231 focus(c);
232 else if(ev->window == root) {
233 selscreen = True;
234 for(c = stack; c && !isvisible(c); c = c->snext);
235 focus(c);
236 }
237 }
239 static void
240 expose(XEvent *e) {
241 XExposeEvent *ev = &e->xexpose;
243 if(ev->count == 0) {
244 if(barwin == ev->window)
245 drawstatus();
246 }
247 }
249 static void
250 keypress(XEvent *e) {
251 static unsigned int len = sizeof key / sizeof key[0];
252 unsigned int i;
253 KeySym keysym;
254 XKeyEvent *ev = &e->xkey;
256 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
257 for(i = 0; i < len; i++) {
258 if(keysym == key[i].keysym
259 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
260 {
261 if(key[i].func)
262 key[i].func(&key[i].arg);
263 }
264 }
265 }
267 static void
268 leavenotify(XEvent *e) {
269 XCrossingEvent *ev = &e->xcrossing;
271 if((ev->window == root) && !ev->same_screen) {
272 selscreen = False;
273 focus(NULL);
274 }
275 }
277 static void
278 mappingnotify(XEvent *e) {
279 XMappingEvent *ev = &e->xmapping;
281 XRefreshKeyboardMapping(ev);
282 if(ev->request == MappingKeyboard)
283 grabkeys();
284 }
286 static void
287 maprequest(XEvent *e) {
288 static XWindowAttributes wa;
289 XMapRequestEvent *ev = &e->xmaprequest;
291 if(!XGetWindowAttributes(dpy, ev->window, &wa))
292 return;
293 if(wa.override_redirect) {
294 XSelectInput(dpy, ev->window,
295 (StructureNotifyMask | PropertyChangeMask));
296 return;
297 }
298 if(!getclient(ev->window))
299 manage(ev->window, &wa);
300 }
302 static void
303 propertynotify(XEvent *e) {
304 Client *c;
305 Window trans;
306 XPropertyEvent *ev = &e->xproperty;
308 if(ev->state == PropertyDelete)
309 return; /* ignore */
310 if((c = getclient(ev->window))) {
311 switch (ev->atom) {
312 default: break;
313 case XA_WM_TRANSIENT_FOR:
314 XGetTransientForHint(dpy, c->win, &trans);
315 if(!c->isfloat && (c->isfloat = (trans != 0)))
316 arrange();
317 break;
318 case XA_WM_NORMAL_HINTS:
319 updatesizehints(c);
320 break;
321 }
322 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
323 updatetitle(c);
324 if(c == sel)
325 drawstatus();
326 }
327 }
328 }
330 static void
331 unmapnotify(XEvent *e) {
332 Client *c;
333 XUnmapEvent *ev = &e->xunmap;
335 if((c = getclient(ev->window)))
336 unmanage(c);
337 }
339 /* extern */
341 void (*handler[LASTEvent]) (XEvent *) = {
342 [ButtonPress] = buttonpress,
343 [ConfigureRequest] = configurerequest,
344 [DestroyNotify] = destroynotify,
345 [EnterNotify] = enternotify,
346 [LeaveNotify] = leavenotify,
347 [Expose] = expose,
348 [KeyPress] = keypress,
349 [MappingNotify] = mappingnotify,
350 [MapRequest] = maprequest,
351 [PropertyNotify] = propertynotify,
352 [UnmapNotify] = unmapnotify
353 };
355 void
356 grabkeys(void) {
357 static unsigned int len = sizeof key / sizeof key[0];
358 unsigned int i;
359 KeyCode code;
361 XUngrabKey(dpy, AnyKey, AnyModifier, root);
362 for(i = 0; i < len; i++) {
363 code = XKeysymToKeycode(dpy, key[i].keysym);
364 XGrabKey(dpy, code, key[i].mod, root, True,
365 GrabModeAsync, GrabModeAsync);
366 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
367 GrabModeAsync, GrabModeAsync);
368 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
369 GrabModeAsync, GrabModeAsync);
370 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
371 GrabModeAsync, GrabModeAsync);
372 }
373 }
375 void
376 procevent(void) {
377 XEvent ev;
379 while(XPending(dpy)) {
380 XNextEvent(dpy, &ev);
381 if(handler[ev.type])
382 (handler[ev.type])(&ev); /* call handler */
383 }
384 }