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