rev |
line source |
paulo@0
|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
paulo@0
|
2 * See LICENSE file for license details.
|
paulo@0
|
3 */
|
paulo@0
|
4 #include "dwm.h"
|
paulo@0
|
5 #include <stdlib.h>
|
paulo@0
|
6 #include <X11/keysym.h>
|
paulo@0
|
7 #include <X11/Xatom.h>
|
paulo@0
|
8
|
paulo@0
|
9 /* static */
|
paulo@0
|
10
|
paulo@0
|
11 typedef struct {
|
paulo@0
|
12 unsigned long mod;
|
paulo@0
|
13 KeySym keysym;
|
paulo@0
|
14 void (*func)(Arg *arg);
|
paulo@0
|
15 Arg arg;
|
paulo@0
|
16 } Key;
|
paulo@0
|
17
|
paulo@0
|
18 KEYS
|
paulo@0
|
19
|
paulo@0
|
20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
paulo@0
|
21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
|
paulo@0
|
22
|
paulo@0
|
23 static Client *
|
paulo@0
|
24 getclient(Window w) {
|
paulo@0
|
25 Client *c;
|
paulo@0
|
26
|
paulo@0
|
27 for(c = clients; c && c->win != w; c = c->next);
|
paulo@0
|
28 return c;
|
paulo@0
|
29 }
|
paulo@0
|
30
|
paulo@0
|
31 static void
|
paulo@0
|
32 movemouse(Client *c) {
|
paulo@0
|
33 int x1, y1, ocx, ocy, di, nx, ny;
|
paulo@0
|
34 unsigned int dui;
|
paulo@0
|
35 Window dummy;
|
paulo@0
|
36 XEvent ev;
|
paulo@0
|
37
|
paulo@0
|
38 ocx = nx = c->x;
|
paulo@0
|
39 ocy = ny = c->y;
|
paulo@0
|
40 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
paulo@0
|
41 None, cursor[CurMove], CurrentTime) != GrabSuccess)
|
paulo@0
|
42 return;
|
paulo@0
|
43 c->ismax = False;
|
paulo@0
|
44 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
|
paulo@0
|
45 for(;;) {
|
paulo@0
|
46 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
|
paulo@0
|
47 switch (ev.type) {
|
paulo@0
|
48 case ButtonRelease:
|
paulo@0
|
49 XUngrabPointer(dpy, CurrentTime);
|
paulo@0
|
50 return;
|
paulo@0
|
51 case ConfigureRequest:
|
paulo@0
|
52 case Expose:
|
paulo@0
|
53 case MapRequest:
|
paulo@0
|
54 handler[ev.type](&ev);
|
paulo@0
|
55 break;
|
paulo@0
|
56 case MotionNotify:
|
paulo@0
|
57 XSync(dpy, False);
|
paulo@0
|
58 nx = ocx + (ev.xmotion.x - x1);
|
paulo@0
|
59 ny = ocy + (ev.xmotion.y - y1);
|
paulo@0
|
60 if(abs(wax + nx) < SNAP)
|
paulo@0
|
61 nx = wax;
|
paulo@0
|
62 else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
|
paulo@0
|
63 nx = wax + waw - c->w - 2 * c->border;
|
paulo@0
|
64 if(abs(way - ny) < SNAP)
|
paulo@0
|
65 ny = way;
|
paulo@0
|
66 else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
|
paulo@0
|
67 ny = way + wah - c->h - 2 * c->border;
|
paulo@0
|
68 resize(c, nx, ny, c->w, c->h, False);
|
paulo@0
|
69 break;
|
paulo@0
|
70 }
|
paulo@0
|
71 }
|
paulo@0
|
72 }
|
paulo@0
|
73
|
paulo@0
|
74 static void
|
paulo@0
|
75 resizemouse(Client *c) {
|
paulo@0
|
76 int ocx, ocy;
|
paulo@0
|
77 int nw, nh;
|
paulo@0
|
78 XEvent ev;
|
paulo@0
|
79
|
paulo@0
|
80 ocx = c->x;
|
paulo@0
|
81 ocy = c->y;
|
paulo@0
|
82 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
|
paulo@0
|
83 None, cursor[CurResize], CurrentTime) != GrabSuccess)
|
paulo@0
|
84 return;
|
paulo@0
|
85 c->ismax = False;
|
paulo@0
|
86 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
|
paulo@0
|
87 for(;;) {
|
paulo@0
|
88 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
|
paulo@0
|
89 switch(ev.type) {
|
paulo@0
|
90 case ButtonRelease:
|
paulo@0
|
91 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
|
paulo@0
|
92 c->w + c->border - 1, c->h + c->border - 1);
|
paulo@0
|
93 XUngrabPointer(dpy, CurrentTime);
|
paulo@0
|
94 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
|
paulo@0
|
95 return;
|
paulo@0
|
96 case ConfigureRequest:
|
paulo@0
|
97 case Expose:
|
paulo@0
|
98 case MapRequest:
|
paulo@0
|
99 handler[ev.type](&ev);
|
paulo@0
|
100 break;
|
paulo@0
|
101 case MotionNotify:
|
paulo@0
|
102 XSync(dpy, False);
|
paulo@0
|
103 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
|
paulo@0
|
104 nw = 1;
|
paulo@0
|
105 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
|
paulo@0
|
106 nh = 1;
|
paulo@0
|
107 resize(c, c->x, c->y, nw, nh, True);
|
paulo@0
|
108 break;
|
paulo@0
|
109 }
|
paulo@0
|
110 }
|
paulo@0
|
111 }
|
paulo@0
|
112
|
paulo@0
|
113 static void
|
paulo@0
|
114 buttonpress(XEvent *e) {
|
paulo@0
|
115 int x;
|
paulo@0
|
116 Arg a;
|
paulo@0
|
117 Client *c;
|
paulo@0
|
118 XButtonPressedEvent *ev = &e->xbutton;
|
paulo@0
|
119
|
paulo@0
|
120 if(barwin == ev->window) {
|
paulo@0
|
121 x = 0;
|
paulo@0
|
122 for(a.i = 0; a.i < ntags; a.i++) {
|
paulo@0
|
123 x += textw(tags[a.i]);
|
paulo@0
|
124 if(ev->x < x) {
|
paulo@0
|
125 if(ev->button == Button1) {
|
paulo@0
|
126 if(ev->state & MODKEY)
|
paulo@0
|
127 tag(&a);
|
paulo@0
|
128 else
|
paulo@0
|
129 view(&a);
|
paulo@0
|
130 }
|
paulo@0
|
131 else if(ev->button == Button3) {
|
paulo@0
|
132 if(ev->state & MODKEY)
|
paulo@0
|
133 toggletag(&a);
|
paulo@0
|
134 else
|
paulo@0
|
135 toggleview(&a);
|
paulo@0
|
136 }
|
paulo@0
|
137 return;
|
paulo@0
|
138 }
|
paulo@0
|
139 }
|
paulo@0
|
140 if(ev->x < x + blw)
|
paulo@0
|
141 switch(ev->button) {
|
paulo@0
|
142 case Button1:
|
paulo@0
|
143 a.i = -1;
|
paulo@0
|
144 setlayout(&a);
|
paulo@0
|
145 break;
|
pang@2
|
146 case Button3:
|
pang@2
|
147 a.i = -2;
|
pang@2
|
148 setlayout(&a);
|
pang@2
|
149 break;
|
paulo@0
|
150 case Button4:
|
paulo@0
|
151 a.i = 1;
|
paulo@0
|
152 incnmaster(&a);
|
paulo@0
|
153 break;
|
paulo@0
|
154 case Button5:
|
paulo@0
|
155 a.i = -1;
|
paulo@0
|
156 incnmaster(&a);
|
paulo@0
|
157 break;
|
paulo@0
|
158 }
|
paulo@0
|
159 if(ev->x > x + blw)
|
paulo@0
|
160 switch(ev->button) {
|
paulo@0
|
161 case Button1: zoom(NULL); break;
|
paulo@0
|
162 case Button2: toggleversatile(NULL); break;
|
paulo@0
|
163 case Button3: killclient(NULL); break;
|
paulo@0
|
164 case Button4: focusprev(NULL); break;
|
paulo@0
|
165 case Button5: focusnext(NULL); break;
|
paulo@0
|
166 }
|
paulo@0
|
167 }
|
paulo@0
|
168 else if((c = getclient(ev->window))) {
|
paulo@0
|
169 focus(c);
|
paulo@0
|
170 if(CLEANMASK(ev->state) != MODKEY)
|
paulo@0
|
171 return;
|
paulo@0
|
172 if(ev->button == Button1 && (lt->arrange == versatile || c->isversatile)) {
|
paulo@0
|
173 restack();
|
paulo@0
|
174 movemouse(c);
|
paulo@0
|
175 }
|
paulo@0
|
176 else if(ev->button == Button2)
|
paulo@0
|
177 zoom(NULL);
|
paulo@0
|
178 else if(ev->button == Button3
|
paulo@0
|
179 && (lt->arrange == versatile || c->isversatile) && !c->isfixed)
|
paulo@0
|
180 {
|
paulo@0
|
181 restack();
|
paulo@0
|
182 resizemouse(c);
|
paulo@0
|
183 }
|
paulo@0
|
184 }
|
paulo@0
|
185 }
|
paulo@0
|
186
|
paulo@0
|
187 static void
|
paulo@0
|
188 configurerequest(XEvent *e) {
|
paulo@0
|
189 Client *c;
|
paulo@0
|
190 XConfigureRequestEvent *ev = &e->xconfigurerequest;
|
paulo@0
|
191 XWindowChanges wc;
|
paulo@0
|
192
|
paulo@0
|
193 if((c = getclient(ev->window))) {
|
paulo@0
|
194 c->ismax = False;
|
paulo@0
|
195 if(ev->value_mask & CWBorderWidth)
|
paulo@0
|
196 c->border = ev->border_width;
|
paulo@0
|
197 if(c->isfixed || c->isversatile || (lt->arrange == versatile)) {
|
paulo@0
|
198 if(ev->value_mask & CWX)
|
paulo@0
|
199 c->x = ev->x;
|
paulo@0
|
200 if(ev->value_mask & CWY)
|
paulo@0
|
201 c->y = ev->y;
|
paulo@0
|
202 if(ev->value_mask & CWWidth)
|
paulo@0
|
203 c->w = ev->width;
|
paulo@0
|
204 if(ev->value_mask & CWHeight)
|
paulo@0
|
205 c->h = ev->height;
|
paulo@0
|
206 if((ev->value_mask & (CWX | CWY))
|
paulo@0
|
207 && !(ev->value_mask & (CWWidth | CWHeight)))
|
paulo@0
|
208 configure(c);
|
paulo@0
|
209 if(isvisible(c))
|
paulo@0
|
210 /* XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); */
|
paulo@0
|
211 resize(c, c->x, c->y, c->w, c->h, False);
|
paulo@0
|
212 }
|
paulo@0
|
213 else
|
paulo@0
|
214 configure(c);
|
paulo@0
|
215 }
|
paulo@0
|
216 else {
|
paulo@0
|
217 wc.x = ev->x;
|
paulo@0
|
218 wc.y = ev->y;
|
paulo@0
|
219 wc.width = ev->width;
|
paulo@0
|
220 wc.height = ev->height;
|
paulo@0
|
221 wc.border_width = ev->border_width;
|
paulo@0
|
222 wc.sibling = ev->above;
|
paulo@0
|
223 wc.stack_mode = ev->detail;
|
paulo@0
|
224 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
|
paulo@0
|
225 }
|
paulo@0
|
226 XSync(dpy, False);
|
paulo@0
|
227 }
|
paulo@0
|
228
|
paulo@0
|
229 static void
|
paulo@0
|
230 destroynotify(XEvent *e) {
|
paulo@0
|
231 Client *c;
|
paulo@0
|
232 XDestroyWindowEvent *ev = &e->xdestroywindow;
|
paulo@0
|
233
|
paulo@0
|
234 if((c = getclient(ev->window)))
|
paulo@0
|
235 unmanage(c);
|
paulo@0
|
236 }
|
paulo@0
|
237
|
paulo@0
|
238 static void
|
paulo@0
|
239 enternotify(XEvent *e) {
|
paulo@0
|
240 Client *c;
|
paulo@0
|
241 XCrossingEvent *ev = &e->xcrossing;
|
paulo@0
|
242
|
paulo@0
|
243 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
|
paulo@0
|
244 return;
|
paulo@0
|
245 if((c = getclient(ev->window)) && isvisible(c))
|
paulo@0
|
246 focus(c);
|
paulo@0
|
247 else if(ev->window == root) {
|
paulo@0
|
248 selscreen = True;
|
paulo@0
|
249 for(c = stack; c && !isvisible(c); c = c->snext);
|
paulo@0
|
250 focus(c);
|
paulo@0
|
251 }
|
paulo@0
|
252 }
|
paulo@0
|
253
|
paulo@0
|
254 static void
|
paulo@0
|
255 expose(XEvent *e) {
|
paulo@0
|
256 XExposeEvent *ev = &e->xexpose;
|
paulo@0
|
257
|
paulo@0
|
258 if(ev->count == 0) {
|
paulo@0
|
259 if(barwin == ev->window)
|
paulo@0
|
260 drawstatus();
|
paulo@0
|
261 }
|
paulo@0
|
262 }
|
paulo@0
|
263
|
paulo@0
|
264 static void
|
paulo@0
|
265 keypress(XEvent *e) {
|
paulo@0
|
266 static unsigned int len = sizeof key / sizeof key[0];
|
paulo@0
|
267 unsigned int i;
|
paulo@0
|
268 KeySym keysym;
|
paulo@0
|
269 XKeyEvent *ev = &e->xkey;
|
paulo@0
|
270
|
paulo@0
|
271 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
|
paulo@0
|
272 for(i = 0; i < len; i++)
|
paulo@0
|
273 if(keysym == key[i].keysym
|
paulo@0
|
274 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
|
paulo@0
|
275 {
|
paulo@0
|
276 if(key[i].func)
|
paulo@0
|
277 key[i].func(&key[i].arg);
|
paulo@0
|
278 }
|
paulo@0
|
279 }
|
paulo@0
|
280
|
paulo@0
|
281 static void
|
paulo@0
|
282 leavenotify(XEvent *e) {
|
paulo@0
|
283 XCrossingEvent *ev = &e->xcrossing;
|
paulo@0
|
284
|
paulo@0
|
285 if((ev->window == root) && !ev->same_screen) {
|
paulo@0
|
286 selscreen = False;
|
paulo@0
|
287 focus(NULL);
|
paulo@0
|
288 }
|
paulo@0
|
289 }
|
paulo@0
|
290
|
paulo@0
|
291 static void
|
paulo@0
|
292 mappingnotify(XEvent *e) {
|
paulo@0
|
293 XMappingEvent *ev = &e->xmapping;
|
paulo@0
|
294
|
paulo@0
|
295 XRefreshKeyboardMapping(ev);
|
paulo@0
|
296 if(ev->request == MappingKeyboard)
|
paulo@0
|
297 grabkeys();
|
paulo@0
|
298 }
|
paulo@0
|
299
|
paulo@0
|
300 static void
|
paulo@0
|
301 maprequest(XEvent *e) {
|
paulo@0
|
302 static XWindowAttributes wa;
|
paulo@0
|
303 XMapRequestEvent *ev = &e->xmaprequest;
|
paulo@0
|
304
|
paulo@0
|
305 if(!XGetWindowAttributes(dpy, ev->window, &wa))
|
paulo@0
|
306 return;
|
paulo@0
|
307 if(wa.override_redirect)
|
paulo@0
|
308 return;
|
paulo@0
|
309 if(!getclient(ev->window))
|
paulo@0
|
310 manage(ev->window, &wa);
|
paulo@0
|
311 }
|
paulo@0
|
312
|
paulo@0
|
313 static void
|
paulo@0
|
314 propertynotify(XEvent *e) {
|
paulo@0
|
315 Client *c;
|
paulo@0
|
316 Window trans;
|
paulo@0
|
317 XPropertyEvent *ev = &e->xproperty;
|
paulo@0
|
318
|
paulo@0
|
319 if(ev->state == PropertyDelete)
|
paulo@0
|
320 return; /* ignore */
|
paulo@0
|
321 if((c = getclient(ev->window))) {
|
paulo@0
|
322 switch (ev->atom) {
|
paulo@0
|
323 default: break;
|
paulo@0
|
324 case XA_WM_TRANSIENT_FOR:
|
paulo@0
|
325 XGetTransientForHint(dpy, c->win, &trans);
|
paulo@0
|
326 if(!c->isversatile && (c->isversatile = (getclient(trans) != NULL)))
|
paulo@0
|
327 lt->arrange();
|
paulo@0
|
328 break;
|
paulo@0
|
329 case XA_WM_NORMAL_HINTS:
|
paulo@0
|
330 updatesizehints(c);
|
paulo@0
|
331 break;
|
paulo@0
|
332 }
|
paulo@0
|
333 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
|
paulo@0
|
334 updatetitle(c);
|
paulo@0
|
335 if(c == sel)
|
paulo@0
|
336 drawstatus();
|
paulo@0
|
337 }
|
paulo@0
|
338 }
|
paulo@0
|
339 }
|
paulo@0
|
340
|
paulo@0
|
341 static void
|
paulo@0
|
342 unmapnotify(XEvent *e) {
|
paulo@0
|
343 Client *c;
|
paulo@0
|
344 XUnmapEvent *ev = &e->xunmap;
|
paulo@0
|
345
|
paulo@0
|
346 if((c = getclient(ev->window)))
|
paulo@0
|
347 unmanage(c);
|
paulo@0
|
348 }
|
paulo@0
|
349
|
paulo@0
|
350 /* extern */
|
paulo@0
|
351
|
paulo@0
|
352 void (*handler[LASTEvent]) (XEvent *) = {
|
paulo@0
|
353 [ButtonPress] = buttonpress,
|
paulo@0
|
354 [ConfigureRequest] = configurerequest,
|
paulo@0
|
355 [DestroyNotify] = destroynotify,
|
paulo@0
|
356 [EnterNotify] = enternotify,
|
paulo@0
|
357 [LeaveNotify] = leavenotify,
|
paulo@0
|
358 [Expose] = expose,
|
paulo@0
|
359 [KeyPress] = keypress,
|
paulo@0
|
360 [MappingNotify] = mappingnotify,
|
paulo@0
|
361 [MapRequest] = maprequest,
|
paulo@0
|
362 [PropertyNotify] = propertynotify,
|
paulo@0
|
363 [UnmapNotify] = unmapnotify
|
paulo@0
|
364 };
|
paulo@0
|
365
|
paulo@0
|
366 void
|
paulo@0
|
367 grabkeys(void) {
|
paulo@0
|
368 static unsigned int len = sizeof key / sizeof key[0];
|
paulo@0
|
369 unsigned int i;
|
paulo@0
|
370 KeyCode code;
|
paulo@0
|
371
|
paulo@0
|
372 XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
paulo@0
|
373 for(i = 0; i < len; i++) {
|
paulo@0
|
374 code = XKeysymToKeycode(dpy, key[i].keysym);
|
paulo@0
|
375 XGrabKey(dpy, code, key[i].mod, root, True,
|
paulo@0
|
376 GrabModeAsync, GrabModeAsync);
|
paulo@0
|
377 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
|
paulo@0
|
378 GrabModeAsync, GrabModeAsync);
|
paulo@0
|
379 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
|
paulo@0
|
380 GrabModeAsync, GrabModeAsync);
|
paulo@0
|
381 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
|
paulo@0
|
382 GrabModeAsync, GrabModeAsync);
|
paulo@0
|
383 }
|
paulo@0
|
384 }
|