aewl
view event.c @ 104:3a708f113f55
implemented fallback for too many clients in stacked mode
author | arg@10ksloc.org |
---|---|
date | Wed, 19 Jul 2006 13:36:04 +0200 |
parents | 5d88952cbf96 |
children | 3e74cc981e9b |
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 if(ocy <= ev.xmotion.y)
119 sticky = TopLeft;
120 else
121 sticky = BottomLeft;
122 } else {
123 if(ocy <= ev.xmotion.y)
124 sticky = TopRight;
125 else
126 sticky = BottomRight;
127 }
128 resize(c, True, sticky);
129 break;
130 case ButtonRelease:
131 XUngrabPointer(dpy, CurrentTime);
132 return;
133 }
134 }
135 }
137 static void
138 buttonpress(XEvent *e)
139 {
140 int x;
141 Arg a;
142 XButtonPressedEvent *ev = &e->xbutton;
143 Client *c;
145 if(barwin == ev->window) {
146 switch(ev->button) {
147 default:
148 x = 0;
149 for(a.i = 0; a.i < TLast; a.i++) {
150 x += textw(tags[a.i]);
151 if(ev->x < x) {
152 view(&a);
153 break;
154 }
155 }
156 break;
157 case Button4:
158 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
159 view(&a);
160 break;
161 case Button5:
162 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
163 view(&a);
164 break;
165 }
166 }
167 else if((c = getclient(ev->window))) {
168 switch(ev->button) {
169 default:
170 break;
171 case Button1:
172 if(arrange == dotile && !c->isfloat) {
173 if((ev->state & ControlMask) && (ev->button == Button1))
174 zoom(NULL);
175 }
176 else {
177 higher(c);
178 movemouse(c);
179 }
180 break;
181 case Button2:
182 lower(c);
183 break;
184 case Button3:
185 if(arrange == dofloat || c->isfloat) {
186 higher(c);
187 resizemouse(c);
188 }
189 break;
190 }
191 }
192 }
194 static void
195 configurerequest(XEvent *e)
196 {
197 XConfigureRequestEvent *ev = &e->xconfigurerequest;
198 XWindowChanges wc;
199 Client *c;
201 ev->value_mask &= ~CWSibling;
202 if((c = getclient(ev->window))) {
203 gravitate(c, True);
204 if(ev->value_mask & CWX)
205 *c->x = ev->x;
206 if(ev->value_mask & CWY)
207 *c->y = ev->y;
208 if(ev->value_mask & CWWidth)
209 *c->w = ev->width;
210 if(ev->value_mask & CWHeight)
211 *c->h = ev->height;
212 if(ev->value_mask & CWBorderWidth)
213 c->border = 1;
214 gravitate(c, False);
215 resize(c, True, TopLeft);
216 }
218 wc.x = ev->x;
219 wc.y = ev->y;
220 wc.width = ev->width;
221 wc.height = ev->height;
222 wc.border_width = 1;
223 wc.sibling = None;
224 wc.stack_mode = Above;
225 ev->value_mask &= ~CWStackMode;
226 ev->value_mask |= CWBorderWidth;
227 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
228 XSync(dpy, False);
229 }
231 static void
232 destroynotify(XEvent *e)
233 {
234 Client *c;
235 XDestroyWindowEvent *ev = &e->xdestroywindow;
237 if((c = getclient(ev->window)))
238 unmanage(c);
239 }
241 static void
242 enternotify(XEvent *e)
243 {
244 XCrossingEvent *ev = &e->xcrossing;
245 Client *c;
247 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
248 return;
250 if((c = getclient(ev->window)))
251 focus(c);
252 else if(ev->window == root)
253 issel = True;
254 }
256 static void
257 expose(XEvent *e)
258 {
259 XExposeEvent *ev = &e->xexpose;
260 Client *c;
262 if(ev->count == 0) {
263 if(barwin == ev->window)
264 drawstatus();
265 else if((c = getctitle(ev->window)))
266 drawtitle(c);
267 }
268 }
270 static void
271 keypress(XEvent *e)
272 {
273 XKeyEvent *ev = &e->xkey;
274 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
275 unsigned int i;
276 KeySym keysym;
278 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
279 for(i = 0; i < len; i++)
280 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
281 if(key[i].func)
282 key[i].func(&key[i].arg);
283 return;
284 }
285 }
287 static void
288 leavenotify(XEvent *e)
289 {
290 XCrossingEvent *ev = &e->xcrossing;
292 if((ev->window == root) && !ev->same_screen)
293 issel = True;
294 }
296 static void
297 maprequest(XEvent *e)
298 {
299 XMapRequestEvent *ev = &e->xmaprequest;
300 static XWindowAttributes wa;
302 if(!XGetWindowAttributes(dpy, ev->window, &wa))
303 return;
305 if(wa.override_redirect) {
306 XSelectInput(dpy, ev->window,
307 (StructureNotifyMask | PropertyChangeMask));
308 return;
309 }
311 if(!getclient(ev->window))
312 manage(ev->window, &wa);
313 }
315 static void
316 propertynotify(XEvent *e)
317 {
318 XPropertyEvent *ev = &e->xproperty;
319 Window trans;
320 Client *c;
322 if(ev->state == PropertyDelete)
323 return; /* ignore */
325 if((c = getclient(ev->window))) {
326 if(ev->atom == wmatom[WMProtocols]) {
327 c->proto = getproto(c->win);
328 return;
329 }
330 switch (ev->atom) {
331 default: break;
332 case XA_WM_TRANSIENT_FOR:
333 XGetTransientForHint(dpy, c->win, &trans);
334 if(!c->isfloat && (c->isfloat = (trans != 0)))
335 arrange(NULL);
336 break;
337 case XA_WM_NORMAL_HINTS:
338 setsize(c);
339 break;
340 }
341 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
342 settitle(c);
343 drawtitle(c);
344 }
345 }
346 }
348 static void
349 unmapnotify(XEvent *e)
350 {
351 Client *c;
352 XUnmapEvent *ev = &e->xunmap;
354 if((c = getclient(ev->window)))
355 unmanage(c);
356 }
358 /* extern */
360 void (*handler[LASTEvent]) (XEvent *) = {
361 [ButtonPress] = buttonpress,
362 [ConfigureRequest] = configurerequest,
363 [DestroyNotify] = destroynotify,
364 [EnterNotify] = enternotify,
365 [LeaveNotify] = leavenotify,
366 [Expose] = expose,
367 [KeyPress] = keypress,
368 [MapRequest] = maprequest,
369 [PropertyNotify] = propertynotify,
370 [UnmapNotify] = unmapnotify
371 };
373 void
374 grabkeys()
375 {
376 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
377 unsigned int i;
378 KeyCode code;
380 for(i = 0; i < len; i++) {
381 code = XKeysymToKeycode(dpy, key[i].keysym);
382 XUngrabKey(dpy, code, key[i].mod, root);
383 XGrabKey(dpy, code, key[i].mod, root, True,
384 GrabModeAsync, GrabModeAsync);
385 }
386 }