dwm-meillo

view event.c @ 713:ef2c267884f3

removed a blank line
author Anselm R. Garbe <arg@suckless.org>
date Tue, 23 Jan 2007 12:04:22 +0100
parents 6c2fcf88dd9f
children 7034ee0f48d6
line source
1 /* (C)opyright MMVI-MMVII 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 | SubstructureRedirectMask, &ev);
39 switch (ev.type) {
40 case ButtonRelease:
41 resize(c, True);
42 XUngrabPointer(dpy, CurrentTime);
43 return;
44 case ConfigureRequest:
45 case Expose:
46 case MapRequest:
47 handler[ev.type](&ev);
48 break;
49 case MotionNotify:
50 XSync(dpy, False);
51 c->x = ocx + (ev.xmotion.x - x1);
52 c->y = ocy + (ev.xmotion.y - y1);
53 if(abs(wax + c->x) < SNAP)
54 c->x = wax;
55 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
56 c->x = wax + waw - c->w - 2 * c->border;
57 if(abs(way - c->y) < SNAP)
58 c->y = way;
59 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
60 c->y = way + wah - c->h - 2 * c->border;
61 resize(c, False);
62 break;
63 }
64 }
65 }
67 static void
68 resizemouse(Client *c) {
69 int ocx, ocy;
70 int nw, nh;
71 XEvent ev;
73 ocx = c->x;
74 ocy = c->y;
75 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
76 None, cursor[CurResize], CurrentTime) != GrabSuccess)
77 return;
78 c->ismax = False;
79 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
80 for(;;) {
81 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
82 switch(ev.type) {
83 case ButtonRelease:
84 resize(c, True);
85 XUngrabPointer(dpy, CurrentTime);
86 return;
87 case ConfigureRequest:
88 case Expose:
89 case MapRequest:
90 handler[ev.type](&ev);
91 break;
92 case MotionNotify:
93 XSync(dpy, False);
94 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
95 c->w = nw > 0 ? nw : 1;
96 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
97 c->h = nh > 0 ? nh : 1;
98 resize(c, True);
99 break;
100 }
101 }
102 }
104 static void
105 buttonpress(XEvent *e) {
106 int x;
107 Arg a;
108 Client *c;
109 XButtonPressedEvent *ev = &e->xbutton;
111 if(barwin == ev->window) {
112 x = 0;
113 for(a.i = 0; a.i < ntags; a.i++) {
114 x += textw(tags[a.i]);
115 if(ev->x < x) {
116 if(ev->button == Button1) {
117 if(ev->state & MODKEY)
118 tag(&a);
119 else
120 view(&a);
121 }
122 else if(ev->button == Button3) {
123 if(ev->state & MODKEY)
124 toggletag(&a);
125 else
126 toggleview(&a);
127 }
128 return;
129 }
130 }
131 if(ev->x < x + bmw)
132 switch(ev->button) {
133 case Button1:
134 togglemode(NULL);
135 break;
136 case Button4:
137 a.i = 1;
138 incnmaster(&a);
139 break;
140 case Button5:
141 a.i = -1;
142 incnmaster(&a);
143 break;
144 }
145 }
146 else if((c = getclient(ev->window))) {
147 focus(c);
148 if(CLEANMASK(ev->state) != MODKEY)
149 return;
150 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
151 restack();
152 movemouse(c);
153 }
154 else if(ev->button == Button2)
155 zoom(NULL);
156 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
157 !c->isfixed) {
158 restack();
159 resizemouse(c);
160 }
161 }
162 }
164 static void
165 configurerequest(XEvent *e) {
166 unsigned long newmask;
167 Client *c;
168 XConfigureRequestEvent *ev = &e->xconfigurerequest;
169 XWindowChanges wc;
171 if((c = getclient(ev->window))) {
172 c->ismax = False;
173 if(ev->value_mask & CWX)
174 c->x = ev->x;
175 if(ev->value_mask & CWY)
176 c->y = ev->y;
177 if(ev->value_mask & CWWidth)
178 c->w = ev->width;
179 if(ev->value_mask & CWHeight)
180 c->h = ev->height;
181 if(ev->value_mask & CWBorderWidth)
182 c->border = ev->border_width;
183 wc.x = c->x;
184 wc.y = c->y;
185 wc.width = c->w;
186 wc.height = c->h;
187 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
188 if(newmask)
189 XConfigureWindow(dpy, c->win, newmask, &wc);
190 else
191 configure(c);
192 XSync(dpy, False);
193 if(c->isfloat) {
194 resize(c, False);
195 if(!isvisible(c))
196 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
197 }
198 else
199 arrange();
200 }
201 else {
202 wc.x = ev->x;
203 wc.y = ev->y;
204 wc.width = ev->width;
205 wc.height = ev->height;
206 wc.border_width = ev->border_width;
207 wc.sibling = ev->above;
208 wc.stack_mode = ev->detail;
209 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
210 XSync(dpy, False);
211 }
212 }
214 static void
215 destroynotify(XEvent *e) {
216 Client *c;
217 XDestroyWindowEvent *ev = &e->xdestroywindow;
219 if((c = getclient(ev->window)))
220 unmanage(c);
221 }
223 static void
224 enternotify(XEvent *e) {
225 Client *c;
226 XCrossingEvent *ev = &e->xcrossing;
228 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
229 return;
230 if((c = getclient(ev->window)) && isvisible(c))
231 focus(c);
232 else if(ev->window == root) {
233 issel = True;
234 for(c = stack; c && !isvisible(c); c = c->snext);
235 focus(c);
236 }
237 }
239 static void
240 expose(XEvent *e) {
241 XExposeEvent *ev = &e->xexpose;
243 if(ev->count == 0) {
244 if(barwin == ev->window)
245 drawstatus();
246 }
247 }
249 static void
250 keypress(XEvent *e) {
251 static unsigned int len = sizeof key / sizeof key[0];
252 unsigned int i;
253 KeySym keysym;
254 XKeyEvent *ev = &e->xkey;
256 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
257 for(i = 0; i < len; i++) {
258 if(keysym == key[i].keysym
259 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
260 {
261 if(key[i].func)
262 key[i].func(&key[i].arg);
263 }
264 }
265 }
267 static void
268 leavenotify(XEvent *e) {
269 XCrossingEvent *ev = &e->xcrossing;
271 if((ev->window == root) && !ev->same_screen) {
272 issel = False;
273 focus(NULL);
274 }
275 }
277 static void
278 mappingnotify(XEvent *e) {
279 XMappingEvent *ev = &e->xmapping;
281 XRefreshKeyboardMapping(ev);
282 if(ev->request == MappingKeyboard)
283 grabkeys();
284 }
286 static void
287 maprequest(XEvent *e) {
288 static XWindowAttributes wa;
289 XMapRequestEvent *ev = &e->xmaprequest;
291 if(!XGetWindowAttributes(dpy, ev->window, &wa))
292 return;
293 if(wa.override_redirect) {
294 XSelectInput(dpy, ev->window,
295 (StructureNotifyMask | PropertyChangeMask));
296 return;
297 }
298 if(!getclient(ev->window))
299 manage(ev->window, &wa);
300 }
302 static void
303 propertynotify(XEvent *e) {
304 Client *c;
305 Window trans;
306 XPropertyEvent *ev = &e->xproperty;
308 if(ev->state == PropertyDelete)
309 return; /* ignore */
310 if((c = getclient(ev->window))) {
311 if(ev->atom == wmatom[WMProtocols]) {
312 c->proto = getproto(c->win);
313 return;
314 }
315 switch (ev->atom) {
316 default: break;
317 case XA_WM_TRANSIENT_FOR:
318 XGetTransientForHint(dpy, c->win, &trans);
319 if(!c->isfloat && (c->isfloat = (trans != 0)))
320 arrange();
321 break;
322 case XA_WM_NORMAL_HINTS:
323 updatesizehints(c);
324 break;
325 }
326 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
327 updatetitle(c);
328 if(c == sel)
329 drawstatus();
330 }
331 }
332 }
334 static void
335 unmapnotify(XEvent *e) {
336 Client *c;
337 XUnmapEvent *ev = &e->xunmap;
339 if((c = getclient(ev->window)))
340 unmanage(c);
341 }
343 /* extern */
345 void (*handler[LASTEvent]) (XEvent *) = {
346 [ButtonPress] = buttonpress,
347 [ConfigureRequest] = configurerequest,
348 [DestroyNotify] = destroynotify,
349 [EnterNotify] = enternotify,
350 [LeaveNotify] = leavenotify,
351 [Expose] = expose,
352 [KeyPress] = keypress,
353 [MappingNotify] = mappingnotify,
354 [MapRequest] = maprequest,
355 [PropertyNotify] = propertynotify,
356 [UnmapNotify] = unmapnotify
357 };
359 void
360 grabkeys(void) {
361 static unsigned int len = sizeof key / sizeof key[0];
362 unsigned int i;
363 KeyCode code;
365 XUngrabKey(dpy, AnyKey, AnyModifier, root);
366 for(i = 0; i < len; i++) {
367 code = XKeysymToKeycode(dpy, key[i].keysym);
368 XGrabKey(dpy, code, key[i].mod, root, True,
369 GrabModeAsync, GrabModeAsync);
370 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
371 GrabModeAsync, GrabModeAsync);
372 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
373 GrabModeAsync, GrabModeAsync);
374 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
375 GrabModeAsync, GrabModeAsync);
376 }
377 }
379 void
380 procevent(void) {
381 XEvent ev;
383 while(XPending(dpy)) {
384 XNextEvent(dpy, &ev);
385 if(handler[ev.type])
386 (handler[ev.type])(&ev); /* call handler */
387 }
388 }