view event.c @ 5:e060ab82b136

gapless grid
author paulo@thepaulopc
date Wed, 27 Jan 2010 23:28:29 -0800
parents faa4cb9d7bd6
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 Client *
24 getclient(Window w) {
25 Client *c;
27 for(c = clients; c && c->win != w; c = c->next);
28 return c;
29 }
31 static void
32 movemouse(Client *c) {
33 int x1, y1, ocx, ocy, di, nx, ny;
34 unsigned int dui;
35 Window dummy;
36 XEvent ev;
38 ocx = nx = c->x;
39 ocy = ny = c->y;
40 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
41 None, cursor[CurMove], CurrentTime) != GrabSuccess)
42 return;
43 c->ismax = False;
44 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
45 for(;;) {
46 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
47 switch (ev.type) {
48 case ButtonRelease:
49 XUngrabPointer(dpy, CurrentTime);
50 return;
51 case ConfigureRequest:
52 case Expose:
53 case MapRequest:
54 handler[ev.type](&ev);
55 break;
56 case MotionNotify:
57 XSync(dpy, False);
58 nx = ocx + (ev.xmotion.x - x1);
59 ny = ocy + (ev.xmotion.y - y1);
60 if(abs(wax + nx) < SNAP)
61 nx = wax;
62 else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
63 nx = wax + waw - c->w - 2 * c->border;
64 if(abs(way - ny) < SNAP)
65 ny = way;
66 else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
67 ny = way + wah - c->h - 2 * c->border;
68 resize(c, nx, ny, c->w, c->h, False);
69 break;
70 }
71 }
72 }
74 static void
75 resizemouse(Client *c) {
76 int ocx, ocy;
77 int nw, nh;
78 XEvent ev;
80 ocx = c->x;
81 ocy = c->y;
82 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
83 None, cursor[CurResize], CurrentTime) != GrabSuccess)
84 return;
85 c->ismax = False;
86 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
87 for(;;) {
88 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
89 switch(ev.type) {
90 case ButtonRelease:
91 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
92 c->w + c->border - 1, c->h + c->border - 1);
93 XUngrabPointer(dpy, CurrentTime);
94 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
95 return;
96 case ConfigureRequest:
97 case Expose:
98 case MapRequest:
99 handler[ev.type](&ev);
100 break;
101 case MotionNotify:
102 XSync(dpy, False);
103 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
104 nw = 1;
105 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
106 nh = 1;
107 resize(c, c->x, c->y, nw, nh, True);
108 break;
109 }
110 }
111 }
113 static void
114 buttonpress(XEvent *e) {
115 int x;
116 Arg a;
117 Client *c;
118 XButtonPressedEvent *ev = &e->xbutton;
120 if(barwin == ev->window) {
121 x = 0;
122 for(a.i = 0; a.i < ntags; a.i++) {
123 x += textw(tags[a.i]);
124 if(ev->x < x) {
125 if(ev->button == Button1) {
126 if(ev->state & MODKEY)
127 tag(&a);
128 else
129 view(&a);
130 }
131 else if(ev->button == Button3) {
132 if(ev->state & MODKEY)
133 toggletag(&a);
134 else
135 toggleview(&a);
136 }
137 return;
138 }
139 }
140 if(ev->x < x + blw)
141 switch(ev->button) {
142 case Button1:
143 a.i = -1;
144 setlayout(&a);
145 break;
146 case Button3:
147 a.i = -2;
148 setlayout(&a);
149 break;
150 case Button4:
151 a.i = 1;
152 incnmaster(&a);
153 break;
154 case Button5:
155 a.i = -1;
156 incnmaster(&a);
157 break;
158 }
159 if(ev->x > x + blw)
160 switch(ev->button) {
161 case Button1: zoom(NULL); break;
162 case Button2: toggleversatile(NULL); break;
163 case Button3: killclient(NULL); break;
164 case Button4: pushup(NULL); break;
165 case Button5: pushdown(NULL); break;
166 }
167 }
168 else if(tbarwin == ev->window) {
169 int i, w;
170 for(i=0, c = clients; c; c = c->next) {
171 if(isvisible(c))
172 i++;
173 }
174 if(!i || i >= MAX_TASKS)
175 return;
176 else
177 w = sw/i;
178 switch(ev->button) {
179 case Button4: focusprev(NULL); return; break;
180 case Button5: focusnext(NULL); return; break;
181 }
182 for(i=1, c = clients; c && i*w<=sw; c = c->next, i++) {
183 for(; c && !isvisible(c); c = c->next);
184 if(i*w > ev->x) {
185 switch(ev->button) {
186 case Button1: focus(c); restack(); break;
187 case Button3: focus(c); zoom(NULL); break;
188 }
189 break;
190 }
191 }
192 }
193 else if((c = getclient(ev->window))) {
194 focus(c);
195 if(CLEANMASK(ev->state) != MODKEY)
196 return;
197 if(ev->button == Button1 && (lt->arrange == versatile || c->isversatile)) {
198 restack();
199 movemouse(c);
200 }
201 else if(ev->button == Button2)
202 zoom(NULL);
203 else if(ev->button == Button3
204 && (lt->arrange == versatile || c->isversatile) && !c->isfixed)
205 {
206 restack();
207 resizemouse(c);
208 }
209 }
210 }
212 static void
213 configurerequest(XEvent *e) {
214 Client *c;
215 XConfigureRequestEvent *ev = &e->xconfigurerequest;
216 XWindowChanges wc;
218 if((c = getclient(ev->window))) {
219 c->ismax = False;
220 if(ev->value_mask & CWBorderWidth)
221 c->border = ev->border_width;
222 if(c->isfixed || c->isversatile || (lt->arrange == versatile)) {
223 if(ev->value_mask & CWX)
224 c->x = ev->x;
225 if(ev->value_mask & CWY)
226 c->y = ev->y;
227 if(ev->value_mask & CWWidth)
228 c->w = ev->width;
229 if(ev->value_mask & CWHeight)
230 c->h = ev->height;
231 if((ev->value_mask & (CWX | CWY))
232 && !(ev->value_mask & (CWWidth | CWHeight)))
233 configure(c);
234 if(isvisible(c))
235 /* XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); */
236 resize(c, c->x, c->y, c->w, c->h, False);
237 }
238 else
239 configure(c);
240 }
241 else {
242 wc.x = ev->x;
243 wc.y = ev->y;
244 wc.width = ev->width;
245 wc.height = ev->height;
246 wc.border_width = ev->border_width;
247 wc.sibling = ev->above;
248 wc.stack_mode = ev->detail;
249 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
250 }
251 XSync(dpy, False);
252 }
254 static void
255 destroynotify(XEvent *e) {
256 Client *c;
257 XDestroyWindowEvent *ev = &e->xdestroywindow;
259 if((c = getclient(ev->window)))
260 unmanage(c);
261 }
263 static void
264 enternotify(XEvent *e) {
265 Client *c;
266 XCrossingEvent *ev = &e->xcrossing;
268 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
269 return;
270 if(!CLICK_TO_FOCUS && (c = getclient(ev->window)) && isvisible(c))
271 focus(c);
272 else if(ev->window == root) {
273 selscreen = True;
274 for(c = stack; c && !isvisible(c); c = c->snext);
275 focus(c);
276 }
277 }
279 static void
280 expose(XEvent *e) {
281 XExposeEvent *ev = &e->xexpose;
283 if(ev->count == 0) {
284 if(barwin == ev->window || tbarwin == ev->window)
285 drawstatus();
286 }
287 }
289 static void
290 keypress(XEvent *e) {
291 static unsigned int len = sizeof key / sizeof key[0];
292 unsigned int i;
293 KeySym keysym;
294 XKeyEvent *ev = &e->xkey;
296 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
297 for(i = 0; i < len; i++)
298 if(keysym == key[i].keysym
299 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
300 {
301 if(key[i].func)
302 key[i].func(&key[i].arg);
303 }
304 }
306 static void
307 leavenotify(XEvent *e) {
308 XCrossingEvent *ev = &e->xcrossing;
310 if((ev->window == root) && !ev->same_screen) {
311 selscreen = False;
312 focus(NULL);
313 }
314 }
316 static void
317 mappingnotify(XEvent *e) {
318 XMappingEvent *ev = &e->xmapping;
320 XRefreshKeyboardMapping(ev);
321 if(ev->request == MappingKeyboard)
322 grabkeys();
323 }
325 static void
326 maprequest(XEvent *e) {
327 static XWindowAttributes wa;
328 XMapRequestEvent *ev = &e->xmaprequest;
330 if(!XGetWindowAttributes(dpy, ev->window, &wa))
331 return;
332 if(wa.override_redirect)
333 return;
334 if(!getclient(ev->window))
335 manage(ev->window, &wa);
336 }
338 static void
339 propertynotify(XEvent *e) {
340 Client *c;
341 Window trans;
342 XPropertyEvent *ev = &e->xproperty;
344 if(ev->state == PropertyDelete)
345 return; /* ignore */
346 if((c = getclient(ev->window))) {
347 switch (ev->atom) {
348 default: break;
349 case XA_WM_TRANSIENT_FOR:
350 XGetTransientForHint(dpy, c->win, &trans);
351 if(!c->isversatile && (c->isversatile = (getclient(trans) != NULL)))
352 lt->arrange();
353 break;
354 case XA_WM_NORMAL_HINTS:
355 updatesizehints(c);
356 break;
357 }
358 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
359 updatetitle(c);
360 if(c == sel)
361 drawstatus();
362 }
363 }
364 }
366 static void
367 unmapnotify(XEvent *e) {
368 Client *c;
369 XUnmapEvent *ev = &e->xunmap;
371 if((c = getclient(ev->window)))
372 unmanage(c);
373 }
375 /* extern */
377 void (*handler[LASTEvent]) (XEvent *) = {
378 [ButtonPress] = buttonpress,
379 [ConfigureRequest] = configurerequest,
380 [DestroyNotify] = destroynotify,
381 [EnterNotify] = enternotify,
382 [LeaveNotify] = leavenotify,
383 [Expose] = expose,
384 [KeyPress] = keypress,
385 [MappingNotify] = mappingnotify,
386 [MapRequest] = maprequest,
387 [PropertyNotify] = propertynotify,
388 [UnmapNotify] = unmapnotify
389 };
391 void
392 grabkeys(void) {
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 }