dwm-meillo
view event.c @ 342:a1901753deef
updated man page
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Wed, 23 Aug 2006 18:50:46 +0200 |
parents | 4aa632b6ba66 |
children | 8a06efe5b563 |
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 }
120 else if((c = getclient(ev->window))) {
121 focus(c);
122 if(CLEANMASK(ev->state) == 0)
123 return;
124 switch(ev->button) {
125 default:
126 break;
127 case Button1:
128 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
129 restack(c);
130 movemouse(c);
131 }
132 break;
133 case Button2:
134 zoom(NULL);
135 break;
136 case Button3:
137 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
138 restack(c);
139 resizemouse(c);
140 }
141 break;
142 }
143 }
144 }
146 static void
147 configurerequest(XEvent *e)
148 {
149 unsigned long newmask;
150 Client *c;
151 XConfigureRequestEvent *ev = &e->xconfigurerequest;
152 XEvent synev;
153 XWindowChanges wc;
155 if((c = getclient(ev->window))) {
156 gravitate(c, True);
157 if(ev->value_mask & CWX)
158 c->x = ev->x;
159 if(ev->value_mask & CWY)
160 c->y = ev->y;
161 if(ev->value_mask & CWWidth)
162 c->w = ev->width;
163 if(ev->value_mask & CWHeight)
164 c->h = ev->height;
165 if(ev->value_mask & CWBorderWidth)
166 c->border = ev->border_width;
167 gravitate(c, False);
168 wc.x = c->x;
169 wc.y = c->y;
170 wc.width = c->w;
171 wc.height = c->h;
172 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
173 if(newmask)
174 XConfigureWindow(dpy, c->win, newmask, &wc);
175 else {
176 synev.type = ConfigureNotify;
177 synev.xconfigure.display = dpy;
178 synev.xconfigure.event = c->win;
179 synev.xconfigure.window = c->win;
180 synev.xconfigure.x = c->x;
181 synev.xconfigure.y = c->y;
182 synev.xconfigure.width = c->w;
183 synev.xconfigure.height = c->h;
184 synev.xconfigure.border_width = c->border;
185 synev.xconfigure.above = None;
186 /* Send synthetic ConfigureNotify */
187 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
188 }
189 XSync(dpy, False);
190 if(c->isfloat)
191 resize(c, False, TopLeft);
192 else
193 arrange(NULL);
194 }
195 else {
196 wc.x = ev->x;
197 wc.y = ev->y;
198 wc.width = ev->width;
199 wc.height = ev->height;
200 wc.border_width = ev->border_width;
201 wc.sibling = ev->above;
202 wc.stack_mode = ev->detail;
203 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
204 XSync(dpy, False);
205 }
206 }
208 static void
209 destroynotify(XEvent *e)
210 {
211 Client *c;
212 XDestroyWindowEvent *ev = &e->xdestroywindow;
214 if((c = getclient(ev->window)))
215 unmanage(c);
216 }
218 static void
219 enternotify(XEvent *e)
220 {
221 Client *c;
222 XCrossingEvent *ev = &e->xcrossing;
224 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
225 return;
227 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
228 focus(c);
229 else if(ev->window == root) {
230 issel = True;
231 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
232 drawall();
233 }
234 }
236 static void
237 expose(XEvent *e)
238 {
239 Client *c;
240 XExposeEvent *ev = &e->xexpose;
242 if(ev->count == 0) {
243 if(barwin == ev->window)
244 drawstatus();
245 else if((c = getctitle(ev->window)))
246 drawtitle(c);
247 }
248 }
250 static void
251 keypress(XEvent *e)
252 {
253 static unsigned int len = sizeof(key) / sizeof(key[0]);
254 unsigned int i;
255 KeySym keysym;
256 XKeyEvent *ev = &e->xkey;
258 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
259 for(i = 0; i < len; i++) {
260 if(keysym == key[i].keysym &&
261 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
262 {
263 if(key[i].func)
264 key[i].func(&key[i].arg);
265 return;
266 }
267 }
268 }
270 static void
271 leavenotify(XEvent *e)
272 {
273 XCrossingEvent *ev = &e->xcrossing;
275 if((ev->window == root) && !ev->same_screen) {
276 issel = False;
277 drawall();
278 }
279 }
281 static void
282 mappingnotify(XEvent *e)
283 {
284 XMappingEvent *ev = &e->xmapping;
286 XRefreshKeyboardMapping(ev);
287 if(ev->request == MappingKeyboard)
288 grabkeys();
289 }
291 static void
292 maprequest(XEvent *e)
293 {
294 static XWindowAttributes wa;
295 XMapRequestEvent *ev = &e->xmaprequest;
297 if(!XGetWindowAttributes(dpy, ev->window, &wa))
298 return;
300 if(wa.override_redirect) {
301 XSelectInput(dpy, ev->window,
302 (StructureNotifyMask | PropertyChangeMask));
303 return;
304 }
306 if(!getclient(ev->window))
307 manage(ev->window, &wa);
308 }
310 static void
311 propertynotify(XEvent *e)
312 {
313 Client *c;
314 Window trans;
315 XPropertyEvent *ev = &e->xproperty;
317 if(ev->state == PropertyDelete)
318 return; /* ignore */
320 if((c = getclient(ev->window))) {
321 if(ev->atom == wmatom[WMProtocols]) {
322 c->proto = getproto(c->win);
323 return;
324 }
325 switch (ev->atom) {
326 default: break;
327 case XA_WM_TRANSIENT_FOR:
328 XGetTransientForHint(dpy, c->win, &trans);
329 if(!c->isfloat && (c->isfloat = (trans != 0)))
330 arrange(NULL);
331 break;
332 case XA_WM_NORMAL_HINTS:
333 setsize(c);
334 break;
335 }
336 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
337 settitle(c);
338 drawtitle(c);
339 }
340 }
341 }
343 static void
344 unmapnotify(XEvent *e)
345 {
346 Client *c;
347 XUnmapEvent *ev = &e->xunmap;
349 if((c = getclient(ev->window)))
350 unmanage(c);
351 }
353 /* extern */
355 void (*handler[LASTEvent]) (XEvent *) = {
356 [ButtonPress] = buttonpress,
357 [ConfigureRequest] = configurerequest,
358 [DestroyNotify] = destroynotify,
359 [EnterNotify] = enternotify,
360 [LeaveNotify] = leavenotify,
361 [Expose] = expose,
362 [KeyPress] = keypress,
363 [MappingNotify] = mappingnotify,
364 [MapRequest] = maprequest,
365 [PropertyNotify] = propertynotify,
366 [UnmapNotify] = unmapnotify
367 };
369 void
370 grabkeys()
371 {
372 static unsigned int len = sizeof(key) / sizeof(key[0]);
373 unsigned int i;
374 KeyCode code;
376 XUngrabKey(dpy, AnyKey, AnyModifier, root);
377 for(i = 0; i < len; i++) {
378 code = XKeysymToKeycode(dpy, key[i].keysym);
379 XGrabKey(dpy, code, key[i].mod, root, True,
380 GrabModeAsync, GrabModeAsync);
381 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
382 GrabModeAsync, GrabModeAsync);
383 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
384 GrabModeAsync, GrabModeAsync);
385 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
386 GrabModeAsync, GrabModeAsync);
387 }
388 }
390 void
391 procevent()
392 {
393 XEvent ev;
395 while(XPending(dpy)) {
396 XNextEvent(dpy, &ev);
397 if(handler[ev.type])
398 (handler[ev.type])(&ev); /* call handler */
399 }
400 }