dwm-meillo
view event.c @ 268:a47b3b0d7bf4
applied Sanders LD and resize patches
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 14 Aug 2006 08:43:19 +0200 |
parents | 8a8ea74e1b87 |
children | dacd3f3c5823 |
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 == Button3)
113 toggleview(&a);
114 else
115 view(&a);
116 return;
117 }
118 }
119 }
120 else if((c = getclient(ev->window))) {
121 higher(c);
122 focus(c);
123 switch(ev->button) {
124 default:
125 break;
126 case Button1:
127 if(!c->ismax && (arrange == dofloat || c->isfloat))
128 movemouse(c);
129 break;
130 case Button2:
131 zoom(NULL);
132 break;
133 case Button3:
134 if(!c->ismax && (arrange == dofloat || c->isfloat))
135 resizemouse(c);
136 break;
137 }
138 }
139 }
141 static void
142 configurerequest(XEvent *e)
143 {
144 Client *c;
145 XConfigureRequestEvent *ev = &e->xconfigurerequest;
146 XEvent synev;
147 XWindowChanges wc;
148 unsigned long newmask;
150 if((c = getclient(ev->window))) {
151 gravitate(c, True);
152 if(ev->value_mask & CWX)
153 c->x = ev->x;
154 if(ev->value_mask & CWY)
155 c->y = ev->y;
156 if(ev->value_mask & CWWidth)
157 c->w = ev->width;
158 if(ev->value_mask & CWHeight)
159 c->h = ev->height;
160 if(ev->value_mask & CWBorderWidth)
161 c->border = ev->border_width;
162 gravitate(c, False);
163 wc.x = c->x;
164 wc.y = c->y;
165 wc.width = c->w;
166 wc.height = c->h;
167 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
168 if(newmask)
169 XConfigureWindow(dpy, c->win, newmask, &wc);
170 else {
171 synev.type = ConfigureNotify;
172 synev.xconfigure.display = dpy;
173 synev.xconfigure.event = c->win;
174 synev.xconfigure.window = c->win;
175 synev.xconfigure.x = c->x;
176 synev.xconfigure.y = c->y;
177 synev.xconfigure.width = c->w;
178 synev.xconfigure.height = c->h;
179 synev.xconfigure.border_width = c->border;
180 synev.xconfigure.above = None;
181 /* Send synthetic ConfigureNotify */
182 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
183 }
184 XSync(dpy, False);
185 if(c->isfloat)
186 resize(c, False, TopLeft);
187 else
188 arrange(NULL);
189 }
190 else {
191 wc.x = ev->x;
192 wc.y = ev->y;
193 wc.width = ev->width;
194 wc.height = ev->height;
195 wc.border_width = ev->border_width;
196 wc.sibling = ev->above;
197 wc.stack_mode = ev->detail;
198 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
199 XSync(dpy, False);
200 }
201 }
203 static void
204 destroynotify(XEvent *e)
205 {
206 Client *c;
207 XDestroyWindowEvent *ev = &e->xdestroywindow;
209 if((c = getclient(ev->window)))
210 unmanage(c);
211 }
213 static void
214 enternotify(XEvent *e)
215 {
216 Client *c;
217 XCrossingEvent *ev = &e->xcrossing;
219 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
220 return;
222 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
223 focus(c);
224 else if(ev->window == root) {
225 issel = True;
226 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
227 drawall();
228 }
229 }
231 static void
232 expose(XEvent *e)
233 {
234 Client *c;
235 XExposeEvent *ev = &e->xexpose;
237 if(ev->count == 0) {
238 if(barwin == ev->window)
239 drawstatus();
240 else if((c = getctitle(ev->window)))
241 drawtitle(c);
242 }
243 }
245 static void
246 keypress(XEvent *e)
247 {
248 static unsigned int len = sizeof(key) / sizeof(key[0]);
249 unsigned int i;
250 KeySym keysym;
251 XKeyEvent *ev = &e->xkey;
253 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
254 for(i = 0; i < len; i++)
255 if(keysym == key[i].keysym &&
256 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
257 if(key[i].func)
258 key[i].func(&key[i].arg);
259 return;
260 }
261 }
263 static void
264 leavenotify(XEvent *e)
265 {
266 XCrossingEvent *ev = &e->xcrossing;
268 if((ev->window == root) && !ev->same_screen) {
269 issel = False;
270 drawall();
271 }
272 }
274 static void
275 maprequest(XEvent *e)
276 {
277 static XWindowAttributes wa;
278 XMapRequestEvent *ev = &e->xmaprequest;
280 if(!XGetWindowAttributes(dpy, ev->window, &wa))
281 return;
283 if(wa.override_redirect) {
284 XSelectInput(dpy, ev->window,
285 (StructureNotifyMask | PropertyChangeMask));
286 return;
287 }
289 if(!getclient(ev->window))
290 manage(ev->window, &wa);
291 }
293 static void
294 propertynotify(XEvent *e)
295 {
296 Client *c;
297 Window trans;
298 XPropertyEvent *ev = &e->xproperty;
300 if(ev->state == PropertyDelete)
301 return; /* ignore */
303 if((c = getclient(ev->window))) {
304 if(ev->atom == wmatom[WMProtocols]) {
305 c->proto = getproto(c->win);
306 return;
307 }
308 switch (ev->atom) {
309 default: break;
310 case XA_WM_TRANSIENT_FOR:
311 XGetTransientForHint(dpy, c->win, &trans);
312 if(!c->isfloat && (c->isfloat = (trans != 0)))
313 arrange(NULL);
314 break;
315 case XA_WM_NORMAL_HINTS:
316 setsize(c);
317 break;
318 }
319 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
320 settitle(c);
321 drawtitle(c);
322 }
323 }
324 }
326 static void
327 unmapnotify(XEvent *e)
328 {
329 Client *c;
330 XUnmapEvent *ev = &e->xunmap;
332 if((c = getclient(ev->window)))
333 unmanage(c);
334 }
336 /* extern */
338 void (*handler[LASTEvent]) (XEvent *) = {
339 [ButtonPress] = buttonpress,
340 [ConfigureRequest] = configurerequest,
341 [DestroyNotify] = destroynotify,
342 [EnterNotify] = enternotify,
343 [LeaveNotify] = leavenotify,
344 [Expose] = expose,
345 [KeyPress] = keypress,
346 [MapRequest] = maprequest,
347 [PropertyNotify] = propertynotify,
348 [UnmapNotify] = unmapnotify
349 };
351 void
352 grabkeys()
353 {
354 static unsigned int len = sizeof(key) / sizeof(key[0]);
355 unsigned int i;
356 KeyCode code;
358 for(i = 0; i < len; i++) {
359 code = XKeysymToKeycode(dpy, key[i].keysym);
360 XGrabKey(dpy, code, key[i].mod, root, True,
361 GrabModeAsync, GrabModeAsync);
362 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
363 GrabModeAsync, GrabModeAsync);
364 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
365 GrabModeAsync, GrabModeAsync);
366 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
367 GrabModeAsync, GrabModeAsync);
368 }
369 }