dwm-meillo

view event.c @ 276:9a0a351dd910

pplied Sanders appendtag patch
author Anselm R.Garbe <arg@10ksloc.org>
date Mon, 14 Aug 2006 15:31:58 +0200
parents 425cd4490c1e
children c1187ddcd7f7
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 <unistd.h>
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
11 /* static */
13 typedef struct {
14 unsigned long mod;
15 KeySym keysym;
16 void (*func)(Arg *arg);
17 Arg arg;
18 } Key;
20 KEYS
22 #define CLEANMASK(mask) (mask & ~(NUMLOCKMASK | LockMask))
24 static void
25 movemouse(Client *c)
26 {
27 int x1, y1, ocx, ocy, di;
28 unsigned int dui;
29 Window dummy;
30 XEvent ev;
32 ocx = c->x;
33 ocy = c->y;
34 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
35 None, cursor[CurMove], CurrentTime) != GrabSuccess)
36 return;
37 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
38 for(;;) {
39 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
40 switch (ev.type) {
41 default: break;
42 case Expose:
43 handler[Expose](&ev);
44 break;
45 case MotionNotify:
46 XSync(dpy, False);
47 c->x = ocx + (ev.xmotion.x - x1);
48 c->y = ocy + (ev.xmotion.y - y1);
49 resize(c, False, TopLeft);
50 break;
51 case ButtonRelease:
52 XUngrabPointer(dpy, CurrentTime);
53 return;
54 }
55 }
56 }
58 static void
59 resizemouse(Client *c)
60 {
61 int ocx, ocy;
62 int nw, nh;
63 Corner sticky;
64 XEvent ev;
66 ocx = c->x;
67 ocy = c->y;
68 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
69 None, cursor[CurResize], CurrentTime) != GrabSuccess)
70 return;
71 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
72 for(;;) {
73 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
74 switch(ev.type) {
75 default: break;
76 case Expose:
77 handler[Expose](&ev);
78 break;
79 case MotionNotify:
80 XSync(dpy, False);
81 if((nw = abs(ocx - ev.xmotion.x)))
82 c->w = abs(ocx - ev.xmotion.x);
83 if((nh = abs(ocy - ev.xmotion.y)))
84 c->h = abs(ocy - ev.xmotion.y);
85 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
86 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
87 if(ocx <= ev.xmotion.x)
88 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
89 else
90 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
91 resize(c, True, sticky);
92 break;
93 case ButtonRelease:
94 XUngrabPointer(dpy, CurrentTime);
95 return;
96 }
97 }
98 }
100 static void
101 buttonpress(XEvent *e)
102 {
103 int x;
104 Arg a;
105 Client *c;
106 XButtonPressedEvent *ev = &e->xbutton;
108 if(barwin == ev->window) {
109 x = 0;
110 for(a.i = 0; a.i < ntags; a.i++) {
111 x += textw(tags[a.i]);
112 if(ev->x < x) {
113 if(ev->button == Button3)
114 toggleview(&a);
115 else
116 view(&a);
117 return;
118 }
119 }
120 }
121 else if((c = getclient(ev->window))) {
122 focus(c);
123 switch(ev->button) {
124 default:
125 break;
126 case Button1:
127 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
128 restack(c);
129 movemouse(c);
130 }
131 break;
132 case Button2:
133 zoom(NULL);
134 break;
135 case Button3:
136 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
137 restack(c);
138 resizemouse(c);
139 }
140 break;
141 }
142 }
143 }
145 static void
146 configurerequest(XEvent *e)
147 {
148 Client *c;
149 XConfigureRequestEvent *ev = &e->xconfigurerequest;
150 XEvent synev;
151 XWindowChanges wc;
152 unsigned long newmask;
154 if((c = getclient(ev->window))) {
155 gravitate(c, True);
156 if(ev->value_mask & CWX)
157 c->x = ev->x;
158 if(ev->value_mask & CWY)
159 c->y = ev->y;
160 if(ev->value_mask & CWWidth)
161 c->w = ev->width;
162 if(ev->value_mask & CWHeight)
163 c->h = ev->height;
164 if(ev->value_mask & CWBorderWidth)
165 c->border = ev->border_width;
166 gravitate(c, False);
167 wc.x = c->x;
168 wc.y = c->y;
169 wc.width = c->w;
170 wc.height = c->h;
171 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
172 if(newmask)
173 XConfigureWindow(dpy, c->win, newmask, &wc);
174 else {
175 synev.type = ConfigureNotify;
176 synev.xconfigure.display = dpy;
177 synev.xconfigure.event = c->win;
178 synev.xconfigure.window = c->win;
179 synev.xconfigure.x = c->x;
180 synev.xconfigure.y = c->y;
181 synev.xconfigure.width = c->w;
182 synev.xconfigure.height = c->h;
183 synev.xconfigure.border_width = c->border;
184 synev.xconfigure.above = None;
185 /* Send synthetic ConfigureNotify */
186 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
187 }
188 XSync(dpy, False);
189 if(c->isfloat)
190 resize(c, False, TopLeft);
191 else
192 arrange(NULL);
193 }
194 else {
195 wc.x = ev->x;
196 wc.y = ev->y;
197 wc.width = ev->width;
198 wc.height = ev->height;
199 wc.border_width = ev->border_width;
200 wc.sibling = ev->above;
201 wc.stack_mode = ev->detail;
202 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
203 XSync(dpy, False);
204 }
205 }
207 static void
208 destroynotify(XEvent *e)
209 {
210 Client *c;
211 XDestroyWindowEvent *ev = &e->xdestroywindow;
213 if((c = getclient(ev->window)))
214 unmanage(c);
215 }
217 static void
218 enternotify(XEvent *e)
219 {
220 Client *c;
221 XCrossingEvent *ev = &e->xcrossing;
223 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
224 return;
226 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
227 focus(c);
228 else if(ev->window == root) {
229 issel = True;
230 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
231 drawall();
232 }
233 }
235 static void
236 expose(XEvent *e)
237 {
238 Client *c;
239 XExposeEvent *ev = &e->xexpose;
241 if(ev->count == 0) {
242 if(barwin == ev->window)
243 drawstatus();
244 else if((c = getctitle(ev->window)))
245 drawtitle(c);
246 }
247 }
249 static void
250 keypress(XEvent *e)
251 {
252 static unsigned int len = sizeof(key) / sizeof(key[0]);
253 unsigned int i;
254 KeySym keysym;
255 XKeyEvent *ev = &e->xkey;
257 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
258 for(i = 0; i < len; i++) {
259 if(keysym == key[i].keysym &&
260 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
261 {
262 if(key[i].func)
263 key[i].func(&key[i].arg);
264 return;
265 }
266 }
267 }
269 static void
270 leavenotify(XEvent *e)
271 {
272 XCrossingEvent *ev = &e->xcrossing;
274 if((ev->window == root) && !ev->same_screen) {
275 issel = False;
276 drawall();
277 }
278 }
280 static void
281 maprequest(XEvent *e)
282 {
283 static XWindowAttributes wa;
284 XMapRequestEvent *ev = &e->xmaprequest;
286 if(!XGetWindowAttributes(dpy, ev->window, &wa))
287 return;
289 if(wa.override_redirect) {
290 XSelectInput(dpy, ev->window,
291 (StructureNotifyMask | PropertyChangeMask));
292 return;
293 }
295 if(!getclient(ev->window))
296 manage(ev->window, &wa);
297 }
299 static void
300 propertynotify(XEvent *e)
301 {
302 Client *c;
303 Window trans;
304 XPropertyEvent *ev = &e->xproperty;
306 if(ev->state == PropertyDelete)
307 return; /* ignore */
309 if((c = getclient(ev->window))) {
310 if(ev->atom == wmatom[WMProtocols]) {
311 c->proto = getproto(c->win);
312 return;
313 }
314 switch (ev->atom) {
315 default: break;
316 case XA_WM_TRANSIENT_FOR:
317 XGetTransientForHint(dpy, c->win, &trans);
318 if(!c->isfloat && (c->isfloat = (trans != 0)))
319 arrange(NULL);
320 break;
321 case XA_WM_NORMAL_HINTS:
322 setsize(c);
323 break;
324 }
325 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
326 settitle(c);
327 drawtitle(c);
328 }
329 }
330 }
332 static void
333 unmapnotify(XEvent *e)
334 {
335 Client *c;
336 XUnmapEvent *ev = &e->xunmap;
338 if((c = getclient(ev->window)))
339 unmanage(c);
340 }
342 /* extern */
344 void (*handler[LASTEvent]) (XEvent *) = {
345 [ButtonPress] = buttonpress,
346 [ConfigureRequest] = configurerequest,
347 [DestroyNotify] = destroynotify,
348 [EnterNotify] = enternotify,
349 [LeaveNotify] = leavenotify,
350 [Expose] = expose,
351 [KeyPress] = keypress,
352 [MapRequest] = maprequest,
353 [PropertyNotify] = propertynotify,
354 [UnmapNotify] = unmapnotify
355 };
357 void
358 grabkeys()
359 {
360 static unsigned int len = sizeof(key) / sizeof(key[0]);
361 unsigned int i;
362 KeyCode code;
364 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
365 GrabModeAsync, CurrentTime) != GrabSuccess)
366 usleep(1000);
367 XUngrabKeyboard(dpy, CurrentTime);
369 for(i = 0; i < len; i++) {
370 code = XKeysymToKeycode(dpy, key[i].keysym);
371 XGrabKey(dpy, code, key[i].mod, root, True,
372 GrabModeAsync, GrabModeAsync);
373 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
374 GrabModeAsync, GrabModeAsync);
375 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
376 GrabModeAsync, GrabModeAsync);
377 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
378 GrabModeAsync, GrabModeAsync);
379 }
380 }