dwm-meillo

view event.c @ 379:fc279cd6c7be

fix client position in list also on tag and toggletag
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 29 Aug 2006 09:25:14 +0200
parents fc9d35252ab4
children 126e78129f1d
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 int nw, nh;
62 Corner sticky;
63 XEvent ev;
65 ocx = c->x;
66 ocy = c->y;
67 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
68 None, cursor[CurResize], CurrentTime) != GrabSuccess)
69 return;
70 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
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 if((nw = abs(ocx - ev.xmotion.x)))
81 c->w = abs(ocx - ev.xmotion.x);
82 if((nh = abs(ocy - ev.xmotion.y)))
83 c->h = abs(ocy - ev.xmotion.y);
84 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
85 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
86 if(ocx <= ev.xmotion.x)
87 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
88 else
89 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
90 resize(c, True, sticky);
91 break;
92 case ButtonRelease:
93 XUngrabPointer(dpy, CurrentTime);
94 return;
95 }
96 }
97 }
99 static void
100 buttonpress(XEvent *e)
101 {
102 int x;
103 Arg a;
104 Client *c;
105 XButtonPressedEvent *ev = &e->xbutton;
107 if(barwin == ev->window) {
108 x = 0;
109 for(a.i = 0; a.i < ntags; a.i++) {
110 x += textw(tags[a.i]);
111 if(ev->x < x) {
112 if(ev->button == Button1)
113 view(&a);
114 else if(ev->button == Button3)
115 toggleview(&a);
116 return;
117 }
118 }
119 if(ev->x < x + bmw) {
120 if(ev->button == Button1)
121 togglemode(NULL);
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 focus(c);
126 if(CLEANMASK(ev->state) != MODKEY)
127 return;
128 switch(ev->button) {
129 default:
130 break;
131 case Button1:
132 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
133 restack(c);
134 movemouse(c);
135 }
136 break;
137 case Button2:
138 zoom(NULL);
139 break;
140 case Button3:
141 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
142 restack(c);
143 resizemouse(c);
144 }
145 break;
146 }
147 }
148 }
150 static void
151 configurerequest(XEvent *e)
152 {
153 unsigned long newmask;
154 Client *c;
155 XConfigureRequestEvent *ev = &e->xconfigurerequest;
156 XEvent synev;
157 XWindowChanges wc;
159 if((c = getclient(ev->window))) {
160 gravitate(c, True);
161 if(ev->value_mask & CWX)
162 c->x = ev->x;
163 if(ev->value_mask & CWY)
164 c->y = ev->y;
165 if(ev->value_mask & CWWidth)
166 c->w = ev->width;
167 if(ev->value_mask & CWHeight)
168 c->h = ev->height;
169 if(ev->value_mask & CWBorderWidth)
170 c->border = ev->border_width;
171 gravitate(c, False);
172 wc.x = c->x;
173 wc.y = c->y;
174 wc.width = c->w;
175 wc.height = c->h;
176 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
177 if(newmask)
178 XConfigureWindow(dpy, c->win, newmask, &wc);
179 else {
180 synev.type = ConfigureNotify;
181 synev.xconfigure.display = dpy;
182 synev.xconfigure.event = c->win;
183 synev.xconfigure.window = c->win;
184 synev.xconfigure.x = c->x;
185 synev.xconfigure.y = c->y;
186 synev.xconfigure.width = c->w;
187 synev.xconfigure.height = c->h;
188 synev.xconfigure.border_width = c->border;
189 synev.xconfigure.above = None;
190 /* Send synthetic ConfigureNotify */
191 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
192 }
193 XSync(dpy, False);
194 if(c->isfloat)
195 resize(c, False, TopLeft);
196 else
197 arrange(NULL);
198 }
199 else {
200 wc.x = ev->x;
201 wc.y = ev->y;
202 wc.width = ev->width;
203 wc.height = ev->height;
204 wc.border_width = ev->border_width;
205 wc.sibling = ev->above;
206 wc.stack_mode = ev->detail;
207 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
208 XSync(dpy, False);
209 }
210 }
212 static void
213 destroynotify(XEvent *e)
214 {
215 Client *c;
216 XDestroyWindowEvent *ev = &e->xdestroywindow;
218 if((c = getclient(ev->window)))
219 unmanage(c);
220 }
222 static void
223 enternotify(XEvent *e)
224 {
225 Client *c;
226 XCrossingEvent *ev = &e->xcrossing;
228 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
229 return;
231 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
232 focus(c);
233 else if(ev->window == root) {
234 issel = True;
235 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
236 drawall();
237 }
238 }
240 static void
241 expose(XEvent *e)
242 {
243 Client *c;
244 XExposeEvent *ev = &e->xexpose;
246 if(ev->count == 0) {
247 if(barwin == ev->window)
248 drawstatus();
249 else if((c = getctitle(ev->window)))
250 drawtitle(c);
251 }
252 }
254 static void
255 keypress(XEvent *e)
256 {
257 static unsigned int len = sizeof(key) / sizeof(key[0]);
258 unsigned int i;
259 KeySym keysym;
260 XKeyEvent *ev = &e->xkey;
262 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
263 for(i = 0; i < len; i++) {
264 if(keysym == key[i].keysym &&
265 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
266 {
267 if(key[i].func)
268 key[i].func(&key[i].arg);
269 return;
270 }
271 }
272 }
274 static void
275 leavenotify(XEvent *e)
276 {
277 XCrossingEvent *ev = &e->xcrossing;
279 if((ev->window == root) && !ev->same_screen) {
280 issel = False;
281 drawall();
282 }
283 }
285 static void
286 mappingnotify(XEvent *e)
287 {
288 XMappingEvent *ev = &e->xmapping;
290 XRefreshKeyboardMapping(ev);
291 if(ev->request == MappingKeyboard)
292 grabkeys();
293 }
295 static void
296 maprequest(XEvent *e)
297 {
298 static XWindowAttributes wa;
299 XMapRequestEvent *ev = &e->xmaprequest;
301 if(!XGetWindowAttributes(dpy, ev->window, &wa))
302 return;
304 if(wa.override_redirect) {
305 XSelectInput(dpy, ev->window,
306 (StructureNotifyMask | PropertyChangeMask));
307 return;
308 }
310 if(!getclient(ev->window))
311 manage(ev->window, &wa);
312 }
314 static void
315 propertynotify(XEvent *e)
316 {
317 Client *c;
318 Window trans;
319 XPropertyEvent *ev = &e->xproperty;
321 if(ev->state == PropertyDelete)
322 return; /* ignore */
324 if((c = getclient(ev->window))) {
325 if(ev->atom == wmatom[WMProtocols]) {
326 c->proto = getproto(c->win);
327 return;
328 }
329 switch (ev->atom) {
330 default: break;
331 case XA_WM_TRANSIENT_FOR:
332 XGetTransientForHint(dpy, c->win, &trans);
333 if(!c->isfloat && (c->isfloat = (trans != 0)))
334 arrange(NULL);
335 break;
336 case XA_WM_NORMAL_HINTS:
337 setsize(c);
338 break;
339 }
340 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
341 settitle(c);
342 drawtitle(c);
343 }
344 }
345 }
347 static void
348 unmapnotify(XEvent *e)
349 {
350 Client *c;
351 XUnmapEvent *ev = &e->xunmap;
353 if((c = getclient(ev->window)))
354 unmanage(c);
355 }
357 /* extern */
359 void (*handler[LASTEvent]) (XEvent *) = {
360 [ButtonPress] = buttonpress,
361 [ConfigureRequest] = configurerequest,
362 [DestroyNotify] = destroynotify,
363 [EnterNotify] = enternotify,
364 [LeaveNotify] = leavenotify,
365 [Expose] = expose,
366 [KeyPress] = keypress,
367 [MappingNotify] = mappingnotify,
368 [MapRequest] = maprequest,
369 [PropertyNotify] = propertynotify,
370 [UnmapNotify] = unmapnotify
371 };
373 void
374 grabkeys()
375 {
376 static unsigned int len = sizeof(key) / sizeof(key[0]);
377 unsigned int i;
378 KeyCode code;
380 XUngrabKey(dpy, AnyKey, AnyModifier, root);
381 for(i = 0; i < len; i++) {
382 code = XKeysymToKeycode(dpy, key[i].keysym);
383 XGrabKey(dpy, code, key[i].mod, root, True,
384 GrabModeAsync, GrabModeAsync);
385 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
386 GrabModeAsync, GrabModeAsync);
387 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
388 GrabModeAsync, GrabModeAsync);
389 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
390 GrabModeAsync, GrabModeAsync);
391 }
392 }
394 void
395 procevent()
396 {
397 XEvent ev;
399 while(XPending(dpy)) {
400 XNextEvent(dpy, &ev);
401 if(handler[ev.type])
402 (handler[ev.type])(&ev); /* call handler */
403 }
404 }