aewl

view event.c @ 143:36cabfe408cd

applied Sanders patches
author arg@10ksloc.org
date Tue, 01 Aug 2006 12:32:33 +0200
parents 22213b9a2114
children e61447a7f249
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_j, focusnext, { 0 } },
36 { MODKEY, XK_k, focusprev, { 0 } },
37 { MODKEY, XK_m, togglemax, { 0 } },
38 { MODKEY, XK_space, togglemode, { 0 } },
39 { MODKEY, XK_Return, zoom, { 0 } },
40 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
41 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
42 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
43 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
44 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
45 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
46 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
47 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
48 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
49 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
50 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
51 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
52 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
53 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
54 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
55 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
56 };
58 /* END CUSTOMIZE */
60 /* static */
62 static void
63 movemouse(Client *c)
64 {
65 int x1, y1, ocx, ocy, di;
66 unsigned int dui;
67 Window dummy;
68 XEvent ev;
70 ocx = c->x;
71 ocy = c->y;
72 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
73 None, cursor[CurMove], CurrentTime) != GrabSuccess)
74 return;
75 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
76 for(;;) {
77 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
78 switch (ev.type) {
79 default: break;
80 case Expose:
81 handler[Expose](&ev);
82 break;
83 case MotionNotify:
84 XSync(dpy, False);
85 c->x = ocx + (ev.xmotion.x - x1);
86 c->y = ocy + (ev.xmotion.y - y1);
87 resize(c, False, TopLeft);
88 break;
89 case ButtonRelease:
90 XUngrabPointer(dpy, CurrentTime);
91 return;
92 }
93 }
94 }
96 static void
97 resizemouse(Client *c)
98 {
99 int ocx, ocy;
100 Corner sticky;
101 XEvent ev;
103 ocx = c->x;
104 ocy = c->y;
105 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
106 None, cursor[CurResize], CurrentTime) != GrabSuccess)
107 return;
108 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
109 for(;;) {
110 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
111 switch(ev.type) {
112 default: break;
113 case Expose:
114 handler[Expose](&ev);
115 break;
116 case MotionNotify:
117 XSync(dpy, False);
118 c->w = abs(ocx - ev.xmotion.x);
119 c->h = abs(ocy - ev.xmotion.y);
120 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
121 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
122 if(ocx <= ev.xmotion.x)
123 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
124 else
125 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
126 resize(c, True, sticky);
127 break;
128 case ButtonRelease:
129 XUngrabPointer(dpy, CurrentTime);
130 return;
131 }
132 }
133 }
135 static void
136 buttonpress(XEvent *e)
137 {
138 int x;
139 Arg a;
140 Client *c;
141 XButtonPressedEvent *ev = &e->xbutton;
143 if(barwin == ev->window) {
144 switch(ev->button) {
145 default:
146 x = 0;
147 for(a.i = 0; a.i < TLast; a.i++) {
148 x += textw(tags[a.i]);
149 if(ev->x < x) {
150 view(&a);
151 break;
152 }
153 }
154 break;
155 case Button4:
156 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
157 view(&a);
158 break;
159 case Button5:
160 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
161 view(&a);
162 break;
163 }
164 }
165 else if((c = getclient(ev->window))) {
166 focus(c);
167 switch(ev->button) {
168 default:
169 break;
170 case Button1:
171 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
172 higher(c);
173 movemouse(c);
174 }
175 break;
176 case Button2:
177 lower(c);
178 break;
179 case Button3:
180 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
181 higher(c);
182 resizemouse(c);
183 }
184 break;
185 }
186 }
187 }
189 static void
190 configurerequest(XEvent *e)
191 {
192 Client *c;
193 XConfigureRequestEvent *ev = &e->xconfigurerequest;
194 XWindowChanges wc;
196 ev->value_mask &= ~CWSibling;
197 if((c = getclient(ev->window))) {
198 gravitate(c, True);
199 if(ev->value_mask & CWX)
200 c->x = ev->x;
201 if(ev->value_mask & CWY)
202 c->y = ev->y;
203 if(ev->value_mask & CWWidth)
204 c->w = ev->width;
205 if(ev->value_mask & CWHeight)
206 c->h = ev->height;
207 if(ev->value_mask & CWBorderWidth)
208 c->border = 1;
209 gravitate(c, False);
210 resize(c, True, TopLeft);
211 }
213 wc.x = ev->x;
214 wc.y = ev->y;
215 wc.width = ev->width;
216 wc.height = ev->height;
217 wc.border_width = 1;
218 wc.sibling = None;
219 wc.stack_mode = Above;
220 ev->value_mask &= ~CWStackMode;
221 ev->value_mask |= CWBorderWidth;
222 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
223 XSync(dpy, False);
224 }
226 static void
227 destroynotify(XEvent *e)
228 {
229 Client *c;
230 XDestroyWindowEvent *ev = &e->xdestroywindow;
232 if((c = getclient(ev->window)))
233 unmanage(c);
234 }
236 static void
237 enternotify(XEvent *e)
238 {
239 Client *c;
240 XCrossingEvent *ev = &e->xcrossing;
242 if(ev->detail == NotifyInferior)
243 return;
245 if((c = getclient(ev->window)))
246 focus(c);
247 else if(ev->window == root)
248 issel = True;
249 }
251 static void
252 expose(XEvent *e)
253 {
254 Client *c;
255 XExposeEvent *ev = &e->xexpose;
257 if(ev->count == 0) {
258 if(barwin == ev->window)
259 drawstatus();
260 else if((c = getctitle(ev->window)))
261 drawtitle(c);
262 }
263 }
265 static void
266 keypress(XEvent *e)
267 {
268 static unsigned int len = sizeof(key) / sizeof(key[0]);
269 unsigned int i;
270 KeySym keysym;
271 XKeyEvent *ev = &e->xkey;
273 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
274 for(i = 0; i < len; i++)
275 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
276 if(key[i].func)
277 key[i].func(&key[i].arg);
278 return;
279 }
280 }
282 static void
283 leavenotify(XEvent *e)
284 {
285 XCrossingEvent *ev = &e->xcrossing;
287 if((ev->window == root) && !ev->same_screen)
288 issel = True;
289 }
291 static void
292 maprequest(XEvent *e)
293 {
294 static XWindowAttributes wa;
295 XMapRequestEvent *ev = &e->xmaprequest;
297 if(!XGetWindowAttributes(dpy, ev->window, &wa))
298 return;
300 if(wa.override_redirect) {
301 XSelectInput(dpy, ev->window,
302 (StructureNotifyMask | PropertyChangeMask));
303 return;
304 }
306 if(!getclient(ev->window))
307 manage(ev->window, &wa);
308 }
310 static void
311 propertynotify(XEvent *e)
312 {
313 Client *c;
314 Window trans;
315 XPropertyEvent *ev = &e->xproperty;
317 if(ev->state == PropertyDelete)
318 return; /* ignore */
320 if((c = getclient(ev->window))) {
321 if(ev->atom == wmatom[WMProtocols]) {
322 c->proto = getproto(c->win);
323 return;
324 }
325 switch (ev->atom) {
326 default: break;
327 case XA_WM_TRANSIENT_FOR:
328 XGetTransientForHint(dpy, c->win, &trans);
329 if(!c->isfloat && (c->isfloat = (trans != 0)))
330 arrange(NULL);
331 break;
332 case XA_WM_NORMAL_HINTS:
333 setsize(c);
334 break;
335 }
336 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
337 settitle(c);
338 drawtitle(c);
339 }
340 }
341 }
343 static void
344 unmapnotify(XEvent *e)
345 {
346 Client *c;
347 XUnmapEvent *ev = &e->xunmap;
349 if((c = getclient(ev->window)))
350 unmanage(c);
351 }
353 /* extern */
355 void (*handler[LASTEvent]) (XEvent *) = {
356 [ButtonPress] = buttonpress,
357 [ConfigureRequest] = configurerequest,
358 [DestroyNotify] = destroynotify,
359 [EnterNotify] = enternotify,
360 [LeaveNotify] = leavenotify,
361 [Expose] = expose,
362 [KeyPress] = keypress,
363 [MapRequest] = maprequest,
364 [PropertyNotify] = propertynotify,
365 [UnmapNotify] = unmapnotify
366 };
368 void
369 grabkeys()
370 {
371 static unsigned int len = sizeof(key) / sizeof(key[0]);
372 unsigned int i;
373 KeyCode code;
375 for(i = 0; i < len; i++) {
376 code = XKeysymToKeycode(dpy, key[i].keysym);
377 XUngrabKey(dpy, code, key[i].mod, root);
378 XGrabKey(dpy, code, key[i].mod, root, True,
379 GrabModeAsync, GrabModeAsync);
380 }
381 }