Mercurial > hg > index.fcgi > dwm > dwm-3.6.1-12pba
view event.c @ 2:de6bb7885c97
add right-click eventon layout icon to go to previous layout
author | pang@hit-nxdomain.opendns.com |
---|---|
date | Tue, 24 Mar 2009 12:52:45 -0700 |
parents | 7024076fa948 |
children | faa4cb9d7bd6 |
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: focusprev(NULL); break;
165 case Button5: focusnext(NULL); break;
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 && (lt->arrange == versatile || c->isversatile)) {
173 restack();
174 movemouse(c);
175 }
176 else if(ev->button == Button2)
177 zoom(NULL);
178 else if(ev->button == Button3
179 && (lt->arrange == versatile || c->isversatile) && !c->isfixed)
180 {
181 restack();
182 resizemouse(c);
183 }
184 }
185 }
187 static void
188 configurerequest(XEvent *e) {
189 Client *c;
190 XConfigureRequestEvent *ev = &e->xconfigurerequest;
191 XWindowChanges wc;
193 if((c = getclient(ev->window))) {
194 c->ismax = False;
195 if(ev->value_mask & CWBorderWidth)
196 c->border = ev->border_width;
197 if(c->isfixed || c->isversatile || (lt->arrange == versatile)) {
198 if(ev->value_mask & CWX)
199 c->x = ev->x;
200 if(ev->value_mask & CWY)
201 c->y = ev->y;
202 if(ev->value_mask & CWWidth)
203 c->w = ev->width;
204 if(ev->value_mask & CWHeight)
205 c->h = ev->height;
206 if((ev->value_mask & (CWX | CWY))
207 && !(ev->value_mask & (CWWidth | CWHeight)))
208 configure(c);
209 if(isvisible(c))
210 /* XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); */
211 resize(c, c->x, c->y, c->w, c->h, False);
212 }
213 else
214 configure(c);
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 }
226 XSync(dpy, False);
227 }
229 static void
230 destroynotify(XEvent *e) {
231 Client *c;
232 XDestroyWindowEvent *ev = &e->xdestroywindow;
234 if((c = getclient(ev->window)))
235 unmanage(c);
236 }
238 static void
239 enternotify(XEvent *e) {
240 Client *c;
241 XCrossingEvent *ev = &e->xcrossing;
243 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
244 return;
245 if((c = getclient(ev->window)) && isvisible(c))
246 focus(c);
247 else if(ev->window == root) {
248 selscreen = True;
249 for(c = stack; c && !isvisible(c); c = c->snext);
250 focus(c);
251 }
252 }
254 static void
255 expose(XEvent *e) {
256 XExposeEvent *ev = &e->xexpose;
258 if(ev->count == 0) {
259 if(barwin == ev->window)
260 drawstatus();
261 }
262 }
264 static void
265 keypress(XEvent *e) {
266 static unsigned int len = sizeof key / sizeof key[0];
267 unsigned int i;
268 KeySym keysym;
269 XKeyEvent *ev = &e->xkey;
271 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
272 for(i = 0; i < len; i++)
273 if(keysym == key[i].keysym
274 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
275 {
276 if(key[i].func)
277 key[i].func(&key[i].arg);
278 }
279 }
281 static void
282 leavenotify(XEvent *e) {
283 XCrossingEvent *ev = &e->xcrossing;
285 if((ev->window == root) && !ev->same_screen) {
286 selscreen = False;
287 focus(NULL);
288 }
289 }
291 static void
292 mappingnotify(XEvent *e) {
293 XMappingEvent *ev = &e->xmapping;
295 XRefreshKeyboardMapping(ev);
296 if(ev->request == MappingKeyboard)
297 grabkeys();
298 }
300 static void
301 maprequest(XEvent *e) {
302 static XWindowAttributes wa;
303 XMapRequestEvent *ev = &e->xmaprequest;
305 if(!XGetWindowAttributes(dpy, ev->window, &wa))
306 return;
307 if(wa.override_redirect)
308 return;
309 if(!getclient(ev->window))
310 manage(ev->window, &wa);
311 }
313 static void
314 propertynotify(XEvent *e) {
315 Client *c;
316 Window trans;
317 XPropertyEvent *ev = &e->xproperty;
319 if(ev->state == PropertyDelete)
320 return; /* ignore */
321 if((c = getclient(ev->window))) {
322 switch (ev->atom) {
323 default: break;
324 case XA_WM_TRANSIENT_FOR:
325 XGetTransientForHint(dpy, c->win, &trans);
326 if(!c->isversatile && (c->isversatile = (getclient(trans) != NULL)))
327 lt->arrange();
328 break;
329 case XA_WM_NORMAL_HINTS:
330 updatesizehints(c);
331 break;
332 }
333 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
334 updatetitle(c);
335 if(c == sel)
336 drawstatus();
337 }
338 }
339 }
341 static void
342 unmapnotify(XEvent *e) {
343 Client *c;
344 XUnmapEvent *ev = &e->xunmap;
346 if((c = getclient(ev->window)))
347 unmanage(c);
348 }
350 /* extern */
352 void (*handler[LASTEvent]) (XEvent *) = {
353 [ButtonPress] = buttonpress,
354 [ConfigureRequest] = configurerequest,
355 [DestroyNotify] = destroynotify,
356 [EnterNotify] = enternotify,
357 [LeaveNotify] = leavenotify,
358 [Expose] = expose,
359 [KeyPress] = keypress,
360 [MappingNotify] = mappingnotify,
361 [MapRequest] = maprequest,
362 [PropertyNotify] = propertynotify,
363 [UnmapNotify] = unmapnotify
364 };
366 void
367 grabkeys(void) {
368 static unsigned int len = sizeof key / sizeof key[0];
369 unsigned int i;
370 KeyCode code;
372 XUngrabKey(dpy, AnyKey, AnyModifier, root);
373 for(i = 0; i < len; i++) {
374 code = XKeysymToKeycode(dpy, key[i].keysym);
375 XGrabKey(dpy, code, key[i].mod, root, True,
376 GrabModeAsync, GrabModeAsync);
377 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
378 GrabModeAsync, GrabModeAsync);
379 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
380 GrabModeAsync, GrabModeAsync);
381 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
382 GrabModeAsync, GrabModeAsync);
383 }
384 }