dwm-meillo

view event.c @ 260:243bf6783c7c

after some days of experimenting with grey background, I finally give up and switch to white on black ;)
author Anselm R.Garbe <arg@10ksloc.org>
date Fri, 11 Aug 2006 18:11:39 +0200
parents d1630548c3fe
children d6fd632d861c
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 Corner sticky;
62 XEvent ev;
64 ocx = c->x;
65 ocy = c->y;
66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
68 return;
69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
70 for(;;) {
71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
72 switch(ev.type) {
73 default: break;
74 case Expose:
75 handler[Expose](&ev);
76 break;
77 case MotionNotify:
78 XSync(dpy, False);
79 c->w = abs(ocx - ev.xmotion.x);
80 c->h = abs(ocy - ev.xmotion.y);
81 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
82 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
83 if(ocx <= ev.xmotion.x)
84 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
85 else
86 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
87 resize(c, True, sticky);
88 break;
89 case ButtonRelease:
90 XUngrabPointer(dpy, CurrentTime);
91 return;
92 }
93 }
94 }
96 static void
97 buttonpress(XEvent *e)
98 {
99 int x;
100 Arg a;
101 Client *c;
102 XButtonPressedEvent *ev = &e->xbutton;
104 if(barwin == ev->window) {
105 switch(ev->button) {
106 default:
107 x = 0;
108 for(a.i = 0; a.i < ntags; a.i++) {
109 x += textw(tags[a.i]);
110 if(ev->x < x) {
111 view(&a);
112 return;
113 }
114 }
115 if(ev->button == Button1)
116 viewprev(&a);
117 else if(ev->button == Button3)
118 viewnext(&a);
119 break;
120 case Button4:
121 viewprev(&a);
122 break;
123 case Button5:
124 viewnext(&a);
125 break;
126 }
127 }
128 else if((c = getclient(ev->window))) {
129 higher(c);
130 focus(c);
131 switch(ev->button) {
132 default:
133 break;
134 case Button1:
135 if(!c->ismax && (arrange == dofloat || c->isfloat))
136 movemouse(c);
137 break;
138 case Button2:
139 zoom(NULL);
140 break;
141 case Button3:
142 if(!c->ismax && (arrange == dofloat || c->isfloat))
143 resizemouse(c);
144 break;
145 }
146 }
147 }
149 static void
150 configurerequest(XEvent *e)
151 {
152 Client *c;
153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
154 XEvent synev;
155 XWindowChanges wc;
156 unsigned long newmask;
158 if((c = getclient(ev->window))) {
159 gravitate(c, True);
160 if(ev->value_mask & CWX)
161 c->x = ev->x;
162 if(ev->value_mask & CWY)
163 c->y = ev->y;
164 if(ev->value_mask & CWWidth)
165 c->w = ev->width;
166 if(ev->value_mask & CWHeight)
167 c->h = ev->height;
168 if(ev->value_mask & CWBorderWidth)
169 c->border = ev->border_width;
170 gravitate(c, False);
171 wc.x = c->x;
172 wc.y = c->y;
173 wc.width = c->w;
174 wc.height = c->h;
175 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
176 if(newmask)
177 XConfigureWindow(dpy, c->win, newmask, &wc);
178 else {
179 synev.type = ConfigureNotify;
180 synev.xconfigure.display = dpy;
181 synev.xconfigure.event = c->win;
182 synev.xconfigure.window = c->win;
183 synev.xconfigure.x = c->x;
184 synev.xconfigure.y = c->y;
185 synev.xconfigure.width = c->w;
186 synev.xconfigure.height = c->h;
187 synev.xconfigure.border_width = c->border;
188 synev.xconfigure.above = None;
189 /* Send synthetic ConfigureNotify */
190 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
191 }
192 XSync(dpy, False);
193 if(c->isfloat)
194 resize(c, False, TopLeft);
195 else
196 arrange(NULL);
197 }
198 else {
199 wc.x = ev->x;
200 wc.y = ev->y;
201 wc.width = ev->width;
202 wc.height = ev->height;
203 wc.border_width = ev->border_width;
204 wc.sibling = ev->above;
205 wc.stack_mode = ev->detail;
206 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
207 XSync(dpy, False);
208 }
209 }
211 static void
212 destroynotify(XEvent *e)
213 {
214 Client *c;
215 XDestroyWindowEvent *ev = &e->xdestroywindow;
217 if((c = getclient(ev->window)))
218 unmanage(c);
219 }
221 static void
222 enternotify(XEvent *e)
223 {
224 Client *c;
225 XCrossingEvent *ev = &e->xcrossing;
227 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
228 return;
230 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
231 focus(c);
232 else if(ev->window == root) {
233 issel = True;
234 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
235 drawall();
236 }
237 }
239 static void
240 expose(XEvent *e)
241 {
242 Client *c;
243 XExposeEvent *ev = &e->xexpose;
245 if(ev->count == 0) {
246 if(barwin == ev->window)
247 drawstatus();
248 else if((c = getctitle(ev->window)))
249 drawtitle(c);
250 }
251 }
253 static void
254 keypress(XEvent *e)
255 {
256 static unsigned int len = sizeof(key) / sizeof(key[0]);
257 unsigned int i;
258 KeySym keysym;
259 XKeyEvent *ev = &e->xkey;
261 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
262 for(i = 0; i < len; i++)
263 if(keysym == key[i].keysym &&
264 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
265 if(key[i].func)
266 key[i].func(&key[i].arg);
267 return;
268 }
269 }
271 static void
272 leavenotify(XEvent *e)
273 {
274 XCrossingEvent *ev = &e->xcrossing;
276 if((ev->window == root) && !ev->same_screen) {
277 issel = False;
278 drawall();
279 }
280 }
282 static void
283 maprequest(XEvent *e)
284 {
285 static XWindowAttributes wa;
286 XMapRequestEvent *ev = &e->xmaprequest;
288 if(!XGetWindowAttributes(dpy, ev->window, &wa))
289 return;
291 if(wa.override_redirect) {
292 XSelectInput(dpy, ev->window,
293 (StructureNotifyMask | PropertyChangeMask));
294 return;
295 }
297 if(!getclient(ev->window))
298 manage(ev->window, &wa);
299 }
301 static void
302 propertynotify(XEvent *e)
303 {
304 Client *c;
305 Window trans;
306 XPropertyEvent *ev = &e->xproperty;
308 if(ev->state == PropertyDelete)
309 return; /* ignore */
311 if((c = getclient(ev->window))) {
312 if(ev->atom == wmatom[WMProtocols]) {
313 c->proto = getproto(c->win);
314 return;
315 }
316 switch (ev->atom) {
317 default: break;
318 case XA_WM_TRANSIENT_FOR:
319 XGetTransientForHint(dpy, c->win, &trans);
320 if(!c->isfloat && (c->isfloat = (trans != 0)))
321 arrange(NULL);
322 break;
323 case XA_WM_NORMAL_HINTS:
324 setsize(c);
325 break;
326 }
327 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
328 settitle(c);
329 drawtitle(c);
330 }
331 }
332 }
334 static void
335 unmapnotify(XEvent *e)
336 {
337 Client *c;
338 XUnmapEvent *ev = &e->xunmap;
340 if((c = getclient(ev->window)))
341 unmanage(c);
342 }
344 /* extern */
346 void (*handler[LASTEvent]) (XEvent *) = {
347 [ButtonPress] = buttonpress,
348 [ConfigureRequest] = configurerequest,
349 [DestroyNotify] = destroynotify,
350 [EnterNotify] = enternotify,
351 [LeaveNotify] = leavenotify,
352 [Expose] = expose,
353 [KeyPress] = keypress,
354 [MapRequest] = maprequest,
355 [PropertyNotify] = propertynotify,
356 [UnmapNotify] = unmapnotify
357 };
359 void
360 grabkeys()
361 {
362 static unsigned int len = sizeof(key) / sizeof(key[0]);
363 unsigned int i;
364 KeyCode code;
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 }