aewl

view event.c @ 244:2d4f2bacbbc4

applied grammar correction by ILF
author Anselm R.Garbe <arg@10ksloc.org>
date Thu, 10 Aug 2006 15:35:02 +0200
parents 969fb6be25e1
children 1227c21588e2
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 break;
113 }
114 }
115 break;
116 case Button4:
117 viewprev(&a);
118 break;
119 case Button5:
120 viewnext(&a);
121 break;
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 higher(c);
126 focus(c);
127 switch(ev->button) {
128 default:
129 break;
130 case Button1:
131 if(!c->ismax && (arrange == dofloat || c->isfloat))
132 movemouse(c);
133 break;
134 case Button2:
135 if(!c->ismax && arrange != dofloat && !c->isfloat)
136 zoom(NULL);
137 break;
138 case Button3:
139 if(!c->ismax && (arrange == dofloat || c->isfloat))
140 resizemouse(c);
141 break;
142 }
143 }
144 }
146 static void
147 configurerequest(XEvent *e)
148 {
149 Client *c;
150 XConfigureRequestEvent *ev = &e->xconfigurerequest;
151 XEvent synev;
152 XWindowChanges wc;
153 unsigned long newmask;
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 if(key[i].func)
263 key[i].func(&key[i].arg);
264 return;
265 }
266 }
268 static void
269 leavenotify(XEvent *e)
270 {
271 XCrossingEvent *ev = &e->xcrossing;
273 if((ev->window == root) && !ev->same_screen) {
274 issel = False;
275 drawall();
276 }
277 }
279 static void
280 maprequest(XEvent *e)
281 {
282 static XWindowAttributes wa;
283 XMapRequestEvent *ev = &e->xmaprequest;
285 if(!XGetWindowAttributes(dpy, ev->window, &wa))
286 return;
288 if(wa.override_redirect) {
289 XSelectInput(dpy, ev->window,
290 (StructureNotifyMask | PropertyChangeMask));
291 return;
292 }
294 if(!getclient(ev->window))
295 manage(ev->window, &wa);
296 }
298 static void
299 propertynotify(XEvent *e)
300 {
301 Client *c;
302 Window trans;
303 XPropertyEvent *ev = &e->xproperty;
305 if(ev->state == PropertyDelete)
306 return; /* ignore */
308 if((c = getclient(ev->window))) {
309 if(ev->atom == wmatom[WMProtocols]) {
310 c->proto = getproto(c->win);
311 return;
312 }
313 switch (ev->atom) {
314 default: break;
315 case XA_WM_TRANSIENT_FOR:
316 XGetTransientForHint(dpy, c->win, &trans);
317 if(!c->isfloat && (c->isfloat = (trans != 0)))
318 arrange(NULL);
319 break;
320 case XA_WM_NORMAL_HINTS:
321 setsize(c);
322 break;
323 }
324 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
325 settitle(c);
326 drawtitle(c);
327 }
328 }
329 }
331 static void
332 unmapnotify(XEvent *e)
333 {
334 Client *c;
335 XUnmapEvent *ev = &e->xunmap;
337 if((c = getclient(ev->window)))
338 unmanage(c);
339 }
341 /* extern */
343 void (*handler[LASTEvent]) (XEvent *) = {
344 [ButtonPress] = buttonpress,
345 [ConfigureRequest] = configurerequest,
346 [DestroyNotify] = destroynotify,
347 [EnterNotify] = enternotify,
348 [LeaveNotify] = leavenotify,
349 [Expose] = expose,
350 [KeyPress] = keypress,
351 [MapRequest] = maprequest,
352 [PropertyNotify] = propertynotify,
353 [UnmapNotify] = unmapnotify
354 };
356 void
357 grabkeys()
358 {
359 static unsigned int len = sizeof(key) / sizeof(key[0]);
360 unsigned int i;
361 KeyCode code;
363 for(i = 0; i < len; i++) {
364 code = XKeysymToKeycode(dpy, key[i].keysym);
365 XGrabKey(dpy, code, key[i].mod, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
368 GrabModeAsync, GrabModeAsync);
369 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
370 GrabModeAsync, GrabModeAsync);
371 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
372 GrabModeAsync, GrabModeAsync);
373 }
374 }