aewl

view event.c @ 399:74739798b0b2

simplified buttonpress
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 01 Sep 2006 15:31:59 +0200
parents 9c703e528e58
children 052657ff2e7b
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 if(ev->state & MODKEY)
114 tag(&a);
115 else
116 view(&a);
117 }
118 else if(ev->button == Button3) {
119 if(ev->state & MODKEY)
120 toggletag(&a);
121 else
122 toggleview(&a);
123 }
124 return;
125 }
126 }
127 if(ev->x < x + bmw) {
128 if(ev->button == Button1)
129 togglemode(NULL);
130 }
131 }
132 else if((c = getclient(ev->window))) {
133 focus(c);
134 if(c->ismax || CLEANMASK(ev->state) != MODKEY)
135 return;
136 if((ev->button == Button1) && ((arrange == dofloat) || c->isfloat)) {
137 restack(c);
138 movemouse(c);
139 }
140 else if(ev->button == Button2)
141 zoom(NULL);
142 else if(ev->button == Button3 && ((arrange == dofloat) || c->isfloat)) {
143 restack(c);
144 resizemouse(c);
145 }
146 }
147 }
149 static void
150 synconfig(Client *c, int x, int y, int w, int h, unsigned int border)
151 {
152 XEvent synev;
154 synev.type = ConfigureNotify;
155 synev.xconfigure.display = dpy;
156 synev.xconfigure.event = c->win;
157 synev.xconfigure.window = c->win;
158 synev.xconfigure.x = x;
159 synev.xconfigure.y = y;
160 synev.xconfigure.width = w;
161 synev.xconfigure.height = h;
162 synev.xconfigure.border_width = border;
163 synev.xconfigure.above = None;
164 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
165 }
167 static void
168 configurerequest(XEvent *e)
169 {
170 unsigned long newmask;
171 Client *c;
172 XConfigureRequestEvent *ev = &e->xconfigurerequest;
173 XWindowChanges wc;
175 if((c = getclient(ev->window))) {
176 if(!c->isfloat && (arrange != dofloat) && c->ismax) {
177 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
178 XSync(dpy, False);
179 return;
180 }
181 gravitate(c, True);
182 if(ev->value_mask & CWX)
183 c->x = ev->x;
184 if(ev->value_mask & CWY)
185 c->y = ev->y;
186 if(ev->value_mask & CWWidth)
187 c->w = ev->width;
188 if(ev->value_mask & CWHeight)
189 c->h = ev->height;
190 if(ev->value_mask & CWBorderWidth)
191 c->border = ev->border_width;
192 gravitate(c, False);
193 wc.x = c->x;
194 wc.y = c->y;
195 wc.width = c->w;
196 wc.height = c->h;
197 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
198 if(newmask)
199 XConfigureWindow(dpy, c->win, newmask, &wc);
200 else
201 synconfig(c, c->x, c->y, c->w, c->h, c->border);
202 XSync(dpy, False);
203 if(c->isfloat)
204 resize(c, False, TopLeft);
205 else
206 arrange(NULL);
207 }
208 else {
209 wc.x = ev->x;
210 wc.y = ev->y;
211 wc.width = ev->width;
212 wc.height = ev->height;
213 wc.border_width = ev->border_width;
214 wc.sibling = ev->above;
215 wc.stack_mode = ev->detail;
216 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
217 XSync(dpy, False);
218 }
219 }
221 static void
222 destroynotify(XEvent *e)
223 {
224 Client *c;
225 XDestroyWindowEvent *ev = &e->xdestroywindow;
227 if((c = getclient(ev->window)))
228 unmanage(c);
229 }
231 static void
232 enternotify(XEvent *e)
233 {
234 Client *c;
235 XCrossingEvent *ev = &e->xcrossing;
237 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
238 return;
240 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
241 focus(c);
242 else if(ev->window == root) {
243 issel = True;
244 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
245 drawall();
246 }
247 }
249 static void
250 expose(XEvent *e)
251 {
252 Client *c;
253 XExposeEvent *ev = &e->xexpose;
255 if(ev->count == 0) {
256 if(barwin == ev->window)
257 drawstatus();
258 else if((c = getctitle(ev->window)))
259 drawtitle(c);
260 }
261 }
263 static void
264 keypress(XEvent *e)
265 {
266 static unsigned int len = sizeof(key) / sizeof(key[0]);
267 unsigned int i;
268 KeySym keysym;
269 XKeyEvent *ev = &e->xkey;
271 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
272 for(i = 0; i < len; i++) {
273 if(keysym == key[i].keysym &&
274 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
275 {
276 if(key[i].func)
277 key[i].func(&key[i].arg);
278 return;
279 }
280 }
281 }
283 static void
284 leavenotify(XEvent *e)
285 {
286 XCrossingEvent *ev = &e->xcrossing;
288 if((ev->window == root) && !ev->same_screen) {
289 issel = False;
290 drawall();
291 }
292 }
294 static void
295 mappingnotify(XEvent *e)
296 {
297 XMappingEvent *ev = &e->xmapping;
299 XRefreshKeyboardMapping(ev);
300 if(ev->request == MappingKeyboard)
301 grabkeys();
302 }
304 static void
305 maprequest(XEvent *e)
306 {
307 static XWindowAttributes wa;
308 XMapRequestEvent *ev = &e->xmaprequest;
310 if(!XGetWindowAttributes(dpy, ev->window, &wa))
311 return;
313 if(wa.override_redirect) {
314 XSelectInput(dpy, ev->window,
315 (StructureNotifyMask | PropertyChangeMask));
316 return;
317 }
319 if(!getclient(ev->window))
320 manage(ev->window, &wa);
321 }
323 static void
324 propertynotify(XEvent *e)
325 {
326 Client *c;
327 Window trans;
328 XPropertyEvent *ev = &e->xproperty;
330 if(ev->state == PropertyDelete)
331 return; /* ignore */
333 if((c = getclient(ev->window))) {
334 if(ev->atom == wmatom[WMProtocols]) {
335 c->proto = getproto(c->win);
336 return;
337 }
338 switch (ev->atom) {
339 default: break;
340 case XA_WM_TRANSIENT_FOR:
341 XGetTransientForHint(dpy, c->win, &trans);
342 if(!c->isfloat && (c->isfloat = (trans != 0)))
343 arrange(NULL);
344 break;
345 case XA_WM_NORMAL_HINTS:
346 setsize(c);
347 break;
348 }
349 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
350 settitle(c);
351 drawtitle(c);
352 }
353 }
354 }
356 static void
357 unmapnotify(XEvent *e)
358 {
359 Client *c;
360 XUnmapEvent *ev = &e->xunmap;
362 if((c = getclient(ev->window)))
363 unmanage(c);
364 }
366 /* extern */
368 void (*handler[LASTEvent]) (XEvent *) = {
369 [ButtonPress] = buttonpress,
370 [ConfigureRequest] = configurerequest,
371 [DestroyNotify] = destroynotify,
372 [EnterNotify] = enternotify,
373 [LeaveNotify] = leavenotify,
374 [Expose] = expose,
375 [KeyPress] = keypress,
376 [MappingNotify] = mappingnotify,
377 [MapRequest] = maprequest,
378 [PropertyNotify] = propertynotify,
379 [UnmapNotify] = unmapnotify
380 };
382 void
383 grabkeys()
384 {
385 static unsigned int len = sizeof(key) / sizeof(key[0]);
386 unsigned int i;
387 KeyCode code;
389 XUngrabKey(dpy, AnyKey, AnyModifier, root);
390 for(i = 0; i < len; i++) {
391 code = XKeysymToKeycode(dpy, key[i].keysym);
392 XGrabKey(dpy, code, key[i].mod, root, True,
393 GrabModeAsync, GrabModeAsync);
394 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
395 GrabModeAsync, GrabModeAsync);
396 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
397 GrabModeAsync, GrabModeAsync);
398 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
399 GrabModeAsync, GrabModeAsync);
400 }
401 }
403 void
404 procevent()
405 {
406 XEvent ev;
408 while(XPending(dpy)) {
409 XNextEvent(dpy, &ev);
410 if(handler[ev.type])
411 (handler[ev.type])(&ev); /* call handler */
412 }
413 }