aewl

view event.c @ 117:b819a4ac987e

updated html
author arg@10ksloc.org
date Thu, 20 Jul 2006 10:10:58 +0200
parents dfa5cd0969a6
children eb3165734f00
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 #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
12 #define MouseMask (ButtonMask | PointerMotionMask)
14 /* CUSTOMIZE */
16 typedef struct {
17 unsigned long mod;
18 KeySym keysym;
19 void (*func)(Arg *arg);
20 Arg arg;
21 } Key;
23 const char *browse[] = { "firefox", NULL };
24 const char *gimp[] = { "gimp", NULL };
25 const char *term[] = {
26 "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
27 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
28 };
29 const char *xlock[] = { "xlock", NULL };
31 static Key key[] = {
32 /* modifier key function arguments */
33 { ControlMask, XK_0, appendtag, { .i = Tscratch } },
34 { ControlMask, XK_1, appendtag, { .i = Tdev } },
35 { ControlMask, XK_2, appendtag, { .i = Twww } },
36 { ControlMask, XK_3, appendtag, { .i = Twork } },
37 { MODKEY, XK_0, view, { .i = Tscratch } },
38 { MODKEY, XK_1, view, { .i = Tdev } },
39 { MODKEY, XK_2, view, { .i = Twww } },
40 { MODKEY, XK_3, view, { .i = Twork } },
41 { MODKEY, XK_j, focusnext, { 0 } },
42 { MODKEY, XK_k, focusprev, { 0 } },
43 { MODKEY, XK_m, maximize, { 0 } },
44 { MODKEY, XK_space, dotile, { 0 } },
45 { MODKEY, XK_Return, zoom, { 0 } },
46 { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
47 { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
48 { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
49 { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
50 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
51 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
52 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Twww } },
53 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
54 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
55 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
56 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
57 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
58 { MODKEY|ShiftMask, XK_space, dofloat, { 0 } },
59 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
60 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
61 };
63 /* static */
65 static void
66 movemouse(Client *c)
67 {
68 XEvent ev;
69 int x1, y1, ocx, ocy, di;
70 unsigned int dui;
71 Window dummy;
73 ocx = c->x;
74 ocy = c->y;
75 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
76 None, cursor[CurMove], CurrentTime) != GrabSuccess)
77 return;
78 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
79 for(;;) {
80 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
81 switch (ev.type) {
82 default: break;
83 case Expose:
84 handler[Expose](&ev);
85 break;
86 case MotionNotify:
87 XSync(dpy, False);
88 c->x = ocx + (ev.xmotion.x - x1);
89 c->y = ocy + (ev.xmotion.y - y1);
90 resize(c, False, TopLeft);
91 break;
92 case ButtonRelease:
93 XUngrabPointer(dpy, CurrentTime);
94 return;
95 }
96 }
97 }
99 static void
100 resizemouse(Client *c)
101 {
102 XEvent ev;
103 int ocx, ocy;
104 Corner sticky;
106 ocx = c->x;
107 ocy = c->y;
108 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
109 None, cursor[CurResize], CurrentTime) != GrabSuccess)
110 return;
111 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
112 for(;;) {
113 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
114 switch(ev.type) {
115 default: break;
116 case Expose:
117 handler[Expose](&ev);
118 break;
119 case MotionNotify:
120 XSync(dpy, False);
121 c->w = abs(ocx - ev.xmotion.x);
122 c->h = abs(ocy - ev.xmotion.y);
123 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
124 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
125 if(ocx <= ev.xmotion.x)
126 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
127 else
128 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
129 resize(c, True, sticky);
130 break;
131 case ButtonRelease:
132 XUngrabPointer(dpy, CurrentTime);
133 return;
134 }
135 }
136 }
138 static void
139 buttonpress(XEvent *e)
140 {
141 int x;
142 Arg a;
143 XButtonPressedEvent *ev = &e->xbutton;
144 Client *c;
146 if(barwin == ev->window) {
147 switch(ev->button) {
148 default:
149 x = 0;
150 for(a.i = 0; a.i < TLast; a.i++) {
151 x += textw(tags[a.i]);
152 if(ev->x < x) {
153 view(&a);
154 break;
155 }
156 }
157 break;
158 case Button4:
159 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
160 view(&a);
161 break;
162 case Button5:
163 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
164 view(&a);
165 break;
166 }
167 }
168 else if((c = getclient(ev->window))) {
169 switch(ev->button) {
170 default:
171 break;
172 case Button1:
173 if(arrange == dotile && !c->isfloat) {
174 if((ev->state & ControlMask) && (ev->button == Button1))
175 zoom(NULL);
176 }
177 else {
178 higher(c);
179 movemouse(c);
180 }
181 break;
182 case Button2:
183 lower(c);
184 break;
185 case Button3:
186 if(arrange == dofloat || c->isfloat) {
187 higher(c);
188 resizemouse(c);
189 }
190 break;
191 }
192 }
193 }
195 static void
196 configurerequest(XEvent *e)
197 {
198 XConfigureRequestEvent *ev = &e->xconfigurerequest;
199 XWindowChanges wc;
200 Client *c;
202 ev->value_mask &= ~CWSibling;
203 if((c = getclient(ev->window))) {
204 gravitate(c, True);
205 if(ev->value_mask & CWX)
206 c->x = ev->x;
207 if(ev->value_mask & CWY)
208 c->y = ev->y;
209 if(ev->value_mask & CWWidth)
210 c->w = ev->width;
211 if(ev->value_mask & CWHeight)
212 c->h = ev->height;
213 if(ev->value_mask & CWBorderWidth)
214 c->border = 1;
215 gravitate(c, False);
216 resize(c, True, TopLeft);
217 }
219 wc.x = ev->x;
220 wc.y = ev->y;
221 wc.width = ev->width;
222 wc.height = ev->height;
223 wc.border_width = 1;
224 wc.sibling = None;
225 wc.stack_mode = Above;
226 ev->value_mask &= ~CWStackMode;
227 ev->value_mask |= CWBorderWidth;
228 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
229 XSync(dpy, False);
230 }
232 static void
233 destroynotify(XEvent *e)
234 {
235 Client *c;
236 XDestroyWindowEvent *ev = &e->xdestroywindow;
238 if((c = getclient(ev->window)))
239 unmanage(c);
240 }
242 static void
243 enternotify(XEvent *e)
244 {
245 XCrossingEvent *ev = &e->xcrossing;
246 Client *c;
248 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
249 return;
251 if((c = getclient(ev->window)))
252 focus(c);
253 else if(ev->window == root)
254 issel = True;
255 }
257 static void
258 expose(XEvent *e)
259 {
260 XExposeEvent *ev = &e->xexpose;
261 Client *c;
263 if(ev->count == 0) {
264 if(barwin == ev->window)
265 drawstatus();
266 else if((c = getctitle(ev->window)))
267 drawtitle(c);
268 }
269 }
271 static void
272 keypress(XEvent *e)
273 {
274 XKeyEvent *ev = &e->xkey;
275 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
276 unsigned int i;
277 KeySym keysym;
279 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
280 for(i = 0; i < len; i++)
281 if((keysym == key[i].keysym) && (key[i].mod == 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 XMapRequestEvent *ev = &e->xmaprequest;
301 static XWindowAttributes wa;
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 XPropertyEvent *ev = &e->xproperty;
320 Window trans;
321 Client *c;
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 = key ? sizeof(key) / sizeof(key[0]) : 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 XGrabKey(dpy, code, key[i].mod, root, True,
385 GrabModeAsync, GrabModeAsync);
386 }
387 }