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