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