aewl

view event.c @ 122:f20cea484900

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