aewl
view event.c @ 139:22213b9a2114
prepared 0.5
author | arg@10ksloc.org |
---|---|
date | Fri, 21 Jul 2006 21:15:08 +0200 |
parents | c1185dc7a36e |
children | 36cabfe408cd |
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 "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
30 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
31 };
32 const char *xlock[] = { "xlock", NULL };
33 */
35 static Key key[] = {
36 /* modifier key function arguments */
37 { MODKEY, XK_0, view, { .i = Tfnord } },
38 { MODKEY, XK_1, view, { .i = Tdev } },
39 { MODKEY, XK_2, view, { .i = Tnet } },
40 { MODKEY, XK_3, view, { .i = Twork } },
41 { MODKEY, XK_4, view, { .i = Tmisc} },
42 { MODKEY, XK_j, focusnext, { 0 } },
43 { MODKEY, XK_k, focusprev, { 0 } },
44 { MODKEY, XK_m, togglemax, { 0 } },
45 { MODKEY, XK_space, togglemode, { 0 } },
46 { MODKEY, XK_Return, zoom, { 0 } },
47 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
48 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
49 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
50 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
51 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
52 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
53 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
54 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
55 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
56 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
57 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
58 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
59 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
60 /*
61 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
62 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
63 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
64 */
65 };
67 /* END CUSTOMIZE */
69 /* static */
71 static void
72 movemouse(Client *c)
73 {
74 int x1, y1, ocx, ocy, di;
75 unsigned int dui;
76 Window dummy;
77 XEvent ev;
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 int ocx, ocy;
109 Corner sticky;
110 XEvent ev;
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 Client *c;
150 XButtonPressedEvent *ev = &e->xbutton;
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(!c->ismax && (arrange == dofloat || c->isfloat)) {
180 higher(c);
181 movemouse(c);
182 }
183 break;
184 case Button2:
185 lower(c);
186 break;
187 case Button3:
188 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
189 higher(c);
190 resizemouse(c);
191 }
192 break;
193 }
194 }
195 }
197 static void
198 configurerequest(XEvent *e)
199 {
200 Client *c;
201 XConfigureRequestEvent *ev = &e->xconfigurerequest;
202 XWindowChanges wc;
204 ev->value_mask &= ~CWSibling;
205 if((c = getclient(ev->window))) {
206 gravitate(c, True);
207 if(ev->value_mask & CWX)
208 c->x = ev->x;
209 if(ev->value_mask & CWY)
210 c->y = ev->y;
211 if(ev->value_mask & CWWidth)
212 c->w = ev->width;
213 if(ev->value_mask & CWHeight)
214 c->h = ev->height;
215 if(ev->value_mask & CWBorderWidth)
216 c->border = 1;
217 gravitate(c, False);
218 resize(c, True, TopLeft);
219 }
221 wc.x = ev->x;
222 wc.y = ev->y;
223 wc.width = ev->width;
224 wc.height = ev->height;
225 wc.border_width = 1;
226 wc.sibling = None;
227 wc.stack_mode = Above;
228 ev->value_mask &= ~CWStackMode;
229 ev->value_mask |= CWBorderWidth;
230 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
231 XSync(dpy, False);
232 }
234 static void
235 destroynotify(XEvent *e)
236 {
237 Client *c;
238 XDestroyWindowEvent *ev = &e->xdestroywindow;
240 if((c = getclient(ev->window)))
241 unmanage(c);
242 }
244 static void
245 enternotify(XEvent *e)
246 {
247 Client *c;
248 XCrossingEvent *ev = &e->xcrossing;
250 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
251 return;
253 if((c = getclient(ev->window)))
254 focus(c);
255 else if(ev->window == root)
256 issel = True;
257 }
259 static void
260 expose(XEvent *e)
261 {
262 Client *c;
263 XExposeEvent *ev = &e->xexpose;
265 if(ev->count == 0) {
266 if(barwin == ev->window)
267 drawstatus();
268 else if((c = getctitle(ev->window)))
269 drawtitle(c);
270 }
271 }
273 static void
274 keypress(XEvent *e)
275 {
276 static unsigned int len = sizeof(key) / sizeof(key[0]);
277 unsigned int i;
278 KeySym keysym;
279 XKeyEvent *ev = &e->xkey;
281 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
282 for(i = 0; i < len; i++)
283 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
284 if(key[i].func)
285 key[i].func(&key[i].arg);
286 return;
287 }
288 }
290 static void
291 leavenotify(XEvent *e)
292 {
293 XCrossingEvent *ev = &e->xcrossing;
295 if((ev->window == root) && !ev->same_screen)
296 issel = True;
297 }
299 static void
300 maprequest(XEvent *e)
301 {
302 static XWindowAttributes wa;
303 XMapRequestEvent *ev = &e->xmaprequest;
305 if(!XGetWindowAttributes(dpy, ev->window, &wa))
306 return;
308 if(wa.override_redirect) {
309 XSelectInput(dpy, ev->window,
310 (StructureNotifyMask | PropertyChangeMask));
311 return;
312 }
314 if(!getclient(ev->window))
315 manage(ev->window, &wa);
316 }
318 static void
319 propertynotify(XEvent *e)
320 {
321 Client *c;
322 Window trans;
323 XPropertyEvent *ev = &e->xproperty;
325 if(ev->state == PropertyDelete)
326 return; /* ignore */
328 if((c = getclient(ev->window))) {
329 if(ev->atom == wmatom[WMProtocols]) {
330 c->proto = getproto(c->win);
331 return;
332 }
333 switch (ev->atom) {
334 default: break;
335 case XA_WM_TRANSIENT_FOR:
336 XGetTransientForHint(dpy, c->win, &trans);
337 if(!c->isfloat && (c->isfloat = (trans != 0)))
338 arrange(NULL);
339 break;
340 case XA_WM_NORMAL_HINTS:
341 setsize(c);
342 break;
343 }
344 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
345 settitle(c);
346 drawtitle(c);
347 }
348 }
349 }
351 static void
352 unmapnotify(XEvent *e)
353 {
354 Client *c;
355 XUnmapEvent *ev = &e->xunmap;
357 if((c = getclient(ev->window)))
358 unmanage(c);
359 }
361 /* extern */
363 void (*handler[LASTEvent]) (XEvent *) = {
364 [ButtonPress] = buttonpress,
365 [ConfigureRequest] = configurerequest,
366 [DestroyNotify] = destroynotify,
367 [EnterNotify] = enternotify,
368 [LeaveNotify] = leavenotify,
369 [Expose] = expose,
370 [KeyPress] = keypress,
371 [MapRequest] = maprequest,
372 [PropertyNotify] = propertynotify,
373 [UnmapNotify] = unmapnotify
374 };
376 void
377 grabkeys()
378 {
379 static unsigned int len = sizeof(key) / sizeof(key[0]);
380 unsigned int i;
381 KeyCode code;
383 for(i = 0; i < len; i++) {
384 code = XKeysymToKeycode(dpy, key[i].keysym);
385 XUngrabKey(dpy, code, key[i].mod, root);
386 XGrabKey(dpy, code, key[i].mod, root, True,
387 GrabModeAsync, GrabModeAsync);
388 }
389 }