dwm-meillo
view event.c @ 473:2d8af0d7920d
implemented the maximization as I described on the mailinglist, this feels better to me.
author | arg@mmvi |
---|---|
date | Fri, 22 Sep 2006 07:37:56 +0200 |
parents | ad3fa2d18542 |
children | 3e4596240abb |
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 int x1, y1, ocx, ocy, di;
26 unsigned int dui;
27 Window dummy;
28 XEvent ev;
30 ocx = c->x;
31 ocy = c->y;
32 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
33 None, cursor[CurMove], CurrentTime) != GrabSuccess)
34 return;
35 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
36 for(;;) {
37 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
38 switch (ev.type) {
39 default:
40 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 int ocx, ocy;
60 int nw, nh;
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:
74 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 int x;
102 Arg a;
103 Client *c;
104 XButtonPressedEvent *ev = &e->xbutton;
106 if(barwin == ev->window) {
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 if(ev->button == Button1) {
112 if(ev->state & MODKEY)
113 tag(&a);
114 else
115 view(&a);
116 }
117 else if(ev->button == Button3) {
118 if(ev->state & MODKEY)
119 toggletag(&a);
120 else
121 toggleview(&a);
122 }
123 return;
124 }
125 }
126 if(ev->x < x + bmw) {
127 if(ev->button == Button1)
128 togglemode(NULL);
129 }
130 }
131 else if((c = getclient(ev->window))) {
132 focus(c);
133 if(CLEANMASK(ev->state) != MODKEY)
134 return;
135 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
136 restack(c);
137 movemouse(c);
138 }
139 else if(ev->button == Button2)
140 zoom(NULL);
141 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
142 restack(c);
143 resizemouse(c);
144 }
145 }
146 }
148 static void
149 synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
150 XEvent synev;
152 synev.type = ConfigureNotify;
153 synev.xconfigure.display = dpy;
154 synev.xconfigure.event = c->win;
155 synev.xconfigure.window = c->win;
156 synev.xconfigure.x = x;
157 synev.xconfigure.y = y;
158 synev.xconfigure.width = w;
159 synev.xconfigure.height = h;
160 synev.xconfigure.border_width = border;
161 synev.xconfigure.above = None;
162 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
163 }
165 static void
166 configurerequest(XEvent *e) {
167 unsigned long newmask;
168 Client *c;
169 XConfigureRequestEvent *ev = &e->xconfigurerequest;
170 XWindowChanges wc;
172 if((c = getclient(ev->window))) {
173 if((c == sel) && !c->isfloat && (arrange != dofloat)) {
174 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
175 XSync(dpy, False);
176 return;
177 }
178 gravitate(c, True);
179 if(ev->value_mask & CWX)
180 c->x = ev->x;
181 if(ev->value_mask & CWY)
182 c->y = ev->y;
183 if(ev->value_mask & CWWidth)
184 c->w = ev->width;
185 if(ev->value_mask & CWHeight)
186 c->h = ev->height;
187 if(ev->value_mask & CWBorderWidth)
188 c->border = ev->border_width;
189 gravitate(c, False);
190 wc.x = c->x;
191 wc.y = c->y;
192 wc.width = c->w;
193 wc.height = c->h;
194 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
195 if(newmask)
196 XConfigureWindow(dpy, c->win, newmask, &wc);
197 else
198 synconfig(c, c->x, c->y, c->w, c->h, c->border);
199 XSync(dpy, False);
200 if(c->isfloat)
201 resize(c, False, TopLeft);
202 else
203 arrange(NULL);
204 }
205 else {
206 wc.x = ev->x;
207 wc.y = ev->y;
208 wc.width = ev->width;
209 wc.height = ev->height;
210 wc.border_width = ev->border_width;
211 wc.sibling = ev->above;
212 wc.stack_mode = ev->detail;
213 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
214 XSync(dpy, False);
215 }
216 }
218 static void
219 destroynotify(XEvent *e) {
220 Client *c;
221 XDestroyWindowEvent *ev = &e->xdestroywindow;
223 if((c = getclient(ev->window)))
224 unmanage(c);
225 }
227 static void
228 enternotify(XEvent *e) {
229 Client *c;
230 XCrossingEvent *ev = &e->xcrossing;
232 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
233 return;
235 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
236 focus(c);
237 else if(ev->window == root) {
238 issel = True;
239 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
240 drawall();
241 }
242 }
244 static void
245 expose(XEvent *e) {
246 Client *c;
247 XExposeEvent *ev = &e->xexpose;
249 if(ev->count == 0) {
250 if(barwin == ev->window)
251 drawstatus();
252 else if((c = getctitle(ev->window)))
253 drawtitle(c);
254 }
255 }
257 static void
258 keypress(XEvent *e) {
259 static unsigned int len = sizeof(key) / sizeof(key[0]);
260 unsigned int i;
261 KeySym keysym;
262 XKeyEvent *ev = &e->xkey;
264 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
265 for(i = 0; i < len; i++) {
266 if(keysym == key[i].keysym
267 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
268 {
269 if(key[i].func)
270 key[i].func(&key[i].arg);
271 return;
272 }
273 }
274 }
276 static void
277 leavenotify(XEvent *e) {
278 XCrossingEvent *ev = &e->xcrossing;
280 if((ev->window == root) && !ev->same_screen) {
281 issel = False;
282 drawall();
283 }
284 }
286 static void
287 mappingnotify(XEvent *e) {
288 XMappingEvent *ev = &e->xmapping;
290 XRefreshKeyboardMapping(ev);
291 if(ev->request == MappingKeyboard)
292 grabkeys();
293 }
295 static void
296 maprequest(XEvent *e) {
297 static XWindowAttributes wa;
298 XMapRequestEvent *ev = &e->xmaprequest;
300 if(!XGetWindowAttributes(dpy, ev->window, &wa))
301 return;
303 if(wa.override_redirect) {
304 XSelectInput(dpy, ev->window,
305 (StructureNotifyMask | PropertyChangeMask));
306 return;
307 }
309 if(!getclient(ev->window))
310 manage(ev->window, &wa);
311 }
313 static void
314 propertynotify(XEvent *e) {
315 Client *c;
316 Window trans;
317 XPropertyEvent *ev = &e->xproperty;
319 if(ev->state == PropertyDelete)
320 return; /* ignore */
322 if((c = getclient(ev->window))) {
323 if(ev->atom == wmatom[WMProtocols]) {
324 c->proto = getproto(c->win);
325 return;
326 }
327 switch (ev->atom) {
328 default: break;
329 case XA_WM_TRANSIENT_FOR:
330 XGetTransientForHint(dpy, c->win, &trans);
331 if(!c->isfloat && (c->isfloat = (trans != 0)))
332 arrange(NULL);
333 break;
334 case XA_WM_NORMAL_HINTS:
335 updatesize(c);
336 break;
337 }
338 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
339 updatetitle(c);
340 drawtitle(c);
341 }
342 }
343 }
345 static void
346 unmapnotify(XEvent *e) {
347 Client *c;
348 XUnmapEvent *ev = &e->xunmap;
350 if((c = getclient(ev->window)))
351 unmanage(c);
352 }
354 /* extern */
356 void (*handler[LASTEvent]) (XEvent *) = {
357 [ButtonPress] = buttonpress,
358 [ConfigureRequest] = configurerequest,
359 [DestroyNotify] = destroynotify,
360 [EnterNotify] = enternotify,
361 [LeaveNotify] = leavenotify,
362 [Expose] = expose,
363 [KeyPress] = keypress,
364 [MappingNotify] = mappingnotify,
365 [MapRequest] = maprequest,
366 [PropertyNotify] = propertynotify,
367 [UnmapNotify] = unmapnotify
368 };
370 void
371 grabkeys() {
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 XEvent ev;
394 while(XPending(dpy)) {
395 XNextEvent(dpy, &ev);
396 if(handler[ev.type])
397 (handler[ev.type])(&ev); /* call handler */
398 }
399 }