dwm-meillo
view event.c @ 145:774754477c35
applied Cedric's NumLock patch
author | arg@10ksloc.org |
---|---|
date | Tue, 01 Aug 2006 12:41:38 +0200 |
parents | e61447a7f249 |
children | f328ce9c558c |
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"
7 #include <stdlib.h>
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
11 /* CUSTOMIZE */
13 typedef struct {
14 unsigned long mod;
15 KeySym keysym;
16 void (*func)(Arg *arg);
17 Arg arg;
18 } Key;
20 const char *browse[] = { "firefox", NULL };
21 const char *gimp[] = { "gimp", NULL };
22 const char *term[] = { /*"xterm", NULL };*/
23 "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
24 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
25 };
26 const char *xlock[] = { "xlock", NULL };
28 static Key key[] = {
29 /* modifier key function arguments */
30 { MODKEY, XK_0, view, { .i = Tfnord } },
31 { MODKEY, XK_1, view, { .i = Tdev } },
32 { MODKEY, XK_2, view, { .i = Tnet } },
33 { MODKEY, XK_3, view, { .i = Twork } },
34 { MODKEY, XK_4, view, { .i = Tmisc} },
35 { MODKEY, XK_h, viewprev, { 0 } },
36 { MODKEY, XK_j, focusnext, { 0 } },
37 { MODKEY, XK_k, focusprev, { 0 } },
38 { MODKEY, XK_l, viewnext, { 0 } },
39 { MODKEY, XK_m, togglemax, { 0 } },
40 { MODKEY, XK_space, togglemode, { 0 } },
41 { MODKEY, XK_Return, zoom, { 0 } },
42 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
43 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
44 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
45 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
46 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
47 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
48 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
49 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
50 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
51 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
52 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
53 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
54 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
55 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
56 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
57 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
58 };
60 #define NumLockMask Mod2Mask
61 unsigned int valid_mask = 255 & ~( NumLockMask | LockMask);
63 /* END CUSTOMIZE */
65 /* static */
67 static void
68 movemouse(Client *c)
69 {
70 int x1, y1, ocx, ocy, di;
71 unsigned int dui;
72 Window dummy;
73 XEvent ev;
75 ocx = c->x;
76 ocy = c->y;
77 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
78 None, cursor[CurMove], CurrentTime) != GrabSuccess)
79 return;
80 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
81 for(;;) {
82 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
83 switch (ev.type) {
84 default: break;
85 case Expose:
86 handler[Expose](&ev);
87 break;
88 case MotionNotify:
89 XSync(dpy, False);
90 c->x = ocx + (ev.xmotion.x - x1);
91 c->y = ocy + (ev.xmotion.y - y1);
92 resize(c, False, TopLeft);
93 break;
94 case ButtonRelease:
95 XUngrabPointer(dpy, CurrentTime);
96 return;
97 }
98 }
99 }
101 static void
102 resizemouse(Client *c)
103 {
104 int ocx, ocy;
105 Corner sticky;
106 XEvent ev;
108 ocx = c->x;
109 ocy = c->y;
110 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
111 None, cursor[CurResize], CurrentTime) != GrabSuccess)
112 return;
113 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
114 for(;;) {
115 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
116 switch(ev.type) {
117 default: break;
118 case Expose:
119 handler[Expose](&ev);
120 break;
121 case MotionNotify:
122 XSync(dpy, False);
123 c->w = abs(ocx - ev.xmotion.x);
124 c->h = abs(ocy - ev.xmotion.y);
125 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
126 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
127 if(ocx <= ev.xmotion.x)
128 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
129 else
130 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
131 resize(c, True, sticky);
132 break;
133 case ButtonRelease:
134 XUngrabPointer(dpy, CurrentTime);
135 return;
136 }
137 }
138 }
140 static void
141 buttonpress(XEvent *e)
142 {
143 int x;
144 Arg a;
145 Client *c;
146 XButtonPressedEvent *ev = &e->xbutton;
148 if(barwin == ev->window) {
149 switch(ev->button) {
150 default:
151 x = 0;
152 for(a.i = 0; a.i < TLast; a.i++) {
153 x += textw(tags[a.i]);
154 if(ev->x < x) {
155 view(&a);
156 break;
157 }
158 }
159 break;
160 case Button4:
161 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
162 view(&a);
163 break;
164 case Button5:
165 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
166 view(&a);
167 break;
168 }
169 }
170 else if((c = getclient(ev->window))) {
171 focus(c);
172 switch(ev->button) {
173 default:
174 break;
175 case Button1:
176 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
177 higher(c);
178 movemouse(c);
179 }
180 break;
181 case Button2:
182 lower(c);
183 break;
184 case Button3:
185 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
186 higher(c);
187 resizemouse(c);
188 }
189 break;
190 }
191 }
192 }
194 static void
195 configurerequest(XEvent *e)
196 {
197 Client *c;
198 XConfigureRequestEvent *ev = &e->xconfigurerequest;
199 XWindowChanges wc;
201 ev->value_mask &= ~CWSibling;
202 if((c = getclient(ev->window))) {
203 gravitate(c, True);
204 if(ev->value_mask & CWX)
205 c->x = ev->x;
206 if(ev->value_mask & CWY)
207 c->y = ev->y;
208 if(ev->value_mask & CWWidth)
209 c->w = ev->width;
210 if(ev->value_mask & CWHeight)
211 c->h = ev->height;
212 if(ev->value_mask & CWBorderWidth)
213 c->border = 1;
214 gravitate(c, False);
215 resize(c, True, TopLeft);
216 }
218 wc.x = ev->x;
219 wc.y = ev->y;
220 wc.width = ev->width;
221 wc.height = ev->height;
222 wc.border_width = 1;
223 wc.sibling = None;
224 wc.stack_mode = Above;
225 ev->value_mask &= ~CWStackMode;
226 ev->value_mask |= CWBorderWidth;
227 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
228 XSync(dpy, False);
229 }
231 static void
232 destroynotify(XEvent *e)
233 {
234 Client *c;
235 XDestroyWindowEvent *ev = &e->xdestroywindow;
237 if((c = getclient(ev->window)))
238 unmanage(c);
239 }
241 static void
242 enternotify(XEvent *e)
243 {
244 Client *c;
245 XCrossingEvent *ev = &e->xcrossing;
247 if(ev->detail == NotifyInferior)
248 return;
250 if((c = getclient(ev->window)))
251 focus(c);
252 else if(ev->window == root)
253 issel = True;
254 }
256 static void
257 expose(XEvent *e)
258 {
259 Client *c;
260 XExposeEvent *ev = &e->xexpose;
262 if(ev->count == 0) {
263 if(barwin == ev->window)
264 drawstatus();
265 else if((c = getctitle(ev->window)))
266 drawtitle(c);
267 }
268 }
270 static void
271 keypress(XEvent *e)
272 {
273 static unsigned int len = sizeof(key) / sizeof(key[0]);
274 unsigned int i;
275 KeySym keysym;
276 XKeyEvent *ev = &e->xkey;
277 ev->state &= valid_mask;
279 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
280 for(i = 0; i < len; i++)
281 if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
282 if(key[i].func)
283 key[i].func(&key[i].arg);
284 return;
285 }
286 }
288 static void
289 leavenotify(XEvent *e)
290 {
291 XCrossingEvent *ev = &e->xcrossing;
293 if((ev->window == root) && !ev->same_screen)
294 issel = True;
295 }
297 static void
298 maprequest(XEvent *e)
299 {
300 static XWindowAttributes wa;
301 XMapRequestEvent *ev = &e->xmaprequest;
303 if(!XGetWindowAttributes(dpy, ev->window, &wa))
304 return;
306 if(wa.override_redirect) {
307 XSelectInput(dpy, ev->window,
308 (StructureNotifyMask | PropertyChangeMask));
309 return;
310 }
312 if(!getclient(ev->window))
313 manage(ev->window, &wa);
314 }
316 static void
317 propertynotify(XEvent *e)
318 {
319 Client *c;
320 Window trans;
321 XPropertyEvent *ev = &e->xproperty;
323 if(ev->state == PropertyDelete)
324 return; /* ignore */
326 if((c = getclient(ev->window))) {
327 if(ev->atom == wmatom[WMProtocols]) {
328 c->proto = getproto(c->win);
329 return;
330 }
331 switch (ev->atom) {
332 default: break;
333 case XA_WM_TRANSIENT_FOR:
334 XGetTransientForHint(dpy, c->win, &trans);
335 if(!c->isfloat && (c->isfloat = (trans != 0)))
336 arrange(NULL);
337 break;
338 case XA_WM_NORMAL_HINTS:
339 setsize(c);
340 break;
341 }
342 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
343 settitle(c);
344 drawtitle(c);
345 }
346 }
347 }
349 static void
350 unmapnotify(XEvent *e)
351 {
352 Client *c;
353 XUnmapEvent *ev = &e->xunmap;
355 if((c = getclient(ev->window)))
356 unmanage(c);
357 }
359 /* extern */
361 void (*handler[LASTEvent]) (XEvent *) = {
362 [ButtonPress] = buttonpress,
363 [ConfigureRequest] = configurerequest,
364 [DestroyNotify] = destroynotify,
365 [EnterNotify] = enternotify,
366 [LeaveNotify] = leavenotify,
367 [Expose] = expose,
368 [KeyPress] = keypress,
369 [MapRequest] = maprequest,
370 [PropertyNotify] = propertynotify,
371 [UnmapNotify] = unmapnotify
372 };
374 void
375 grabkeys()
376 {
377 static unsigned int len = sizeof(key) / sizeof(key[0]);
378 unsigned int i;
379 KeyCode code;
381 for(i = 0; i < len; i++) {
382 code = XKeysymToKeycode(dpy, key[i].keysym);
383 XUngrabKey(dpy, code, key[i].mod, root);
384 if (NumLockMask)
385 {
386 XUngrabKey(dpy, code, key[i].mod | NumLockMask, root);
387 XUngrabKey(dpy, code, key[i].mod | NumLockMask | LockMask, root);
388 }
389 XGrabKey(dpy, code, key[i].mod, root, True,
390 GrabModeAsync, GrabModeAsync);
391 if (NumLockMask)
392 {
393 XGrabKey(dpy, code, key[i].mod | NumLockMask, root, True,
394 GrabModeAsync, GrabModeAsync);
395 XGrabKey(dpy, code, key[i].mod | NumLockMask | LockMask, root, True,
396 GrabModeAsync, GrabModeAsync);
397 }
398 }
399 }