aewl

view event.c @ 229:f4f5d000ce7a

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