dwm-meillo
view event.c @ 386:58288246d16c
now dwm enforces max screen size also in tiled mode on non-floating clients which are maximized
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Tue, 29 Aug 2006 18:05:02 +0200 |
parents | 37706ba7d492 |
children | dca6aca09653 |
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 == Button1)
113 view(&a);
114 else if(ev->button == Button3)
115 toggleview(&a);
116 return;
117 }
118 }
119 if(ev->x < x + bmw) {
120 if(ev->button == Button1)
121 togglemode(NULL);
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 focus(c);
126 if(CLEANMASK(ev->state) != MODKEY)
127 return;
128 switch(ev->button) {
129 default:
130 break;
131 case Button1:
132 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
133 restack(c);
134 movemouse(c);
135 }
136 break;
137 case Button2:
138 zoom(NULL);
139 break;
140 case Button3:
141 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
142 restack(c);
143 resizemouse(c);
144 }
145 break;
146 }
147 }
148 }
150 static void
151 configurerequest(XEvent *e)
152 {
153 unsigned long newmask;
154 Client *c;
155 XConfigureRequestEvent *ev = &e->xconfigurerequest;
156 XEvent synev;
157 XWindowChanges wc;
159 if((c = getclient(ev->window))) {
160 if(!c->isfloat && (arrange != dofloat) && c->ismax) {
161 synev.type = ConfigureNotify;
162 synev.xconfigure.display = dpy;
163 synev.xconfigure.event = c->win;
164 synev.xconfigure.window = c->win;
165 synev.xconfigure.x = sx;
166 synev.xconfigure.y = sy + bh;
167 synev.xconfigure.width = sw - 2;
168 synev.xconfigure.height = sw - 2 - bh;
169 synev.xconfigure.border_width = ev->border_width;
170 synev.xconfigure.above = None;
171 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
172 return;
173 }
174 gravitate(c, True);
175 if(ev->value_mask & CWX)
176 c->x = ev->x;
177 if(ev->value_mask & CWY)
178 c->y = ev->y;
179 if(ev->value_mask & CWWidth)
180 c->w = ev->width;
181 if(ev->value_mask & CWHeight)
182 c->h = ev->height;
183 if(ev->value_mask & CWBorderWidth)
184 c->border = ev->border_width;
185 gravitate(c, False);
186 wc.x = c->x;
187 wc.y = c->y;
188 wc.width = c->w;
189 wc.height = c->h;
190 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
191 if(newmask)
192 XConfigureWindow(dpy, c->win, newmask, &wc);
193 else {
194 synev.type = ConfigureNotify;
195 synev.xconfigure.display = dpy;
196 synev.xconfigure.event = c->win;
197 synev.xconfigure.window = c->win;
198 synev.xconfigure.x = c->x;
199 synev.xconfigure.y = c->y;
200 synev.xconfigure.width = c->w;
201 synev.xconfigure.height = c->h;
202 synev.xconfigure.border_width = c->border;
203 synev.xconfigure.above = None;
204 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
205 }
206 XSync(dpy, False);
207 if(c->isfloat)
208 resize(c, False, TopLeft);
209 else
210 arrange(NULL);
211 }
212 else {
213 wc.x = ev->x;
214 wc.y = ev->y;
215 wc.width = ev->width;
216 wc.height = ev->height;
217 wc.border_width = ev->border_width;
218 wc.sibling = ev->above;
219 wc.stack_mode = ev->detail;
220 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
221 XSync(dpy, False);
222 }
223 }
225 static void
226 destroynotify(XEvent *e)
227 {
228 Client *c;
229 XDestroyWindowEvent *ev = &e->xdestroywindow;
231 if((c = getclient(ev->window)))
232 unmanage(c);
233 }
235 static void
236 enternotify(XEvent *e)
237 {
238 Client *c;
239 XCrossingEvent *ev = &e->xcrossing;
241 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
242 return;
244 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
245 focus(c);
246 else if(ev->window == root) {
247 issel = True;
248 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
249 drawall();
250 }
251 }
253 static void
254 expose(XEvent *e)
255 {
256 Client *c;
257 XExposeEvent *ev = &e->xexpose;
259 if(ev->count == 0) {
260 if(barwin == ev->window)
261 drawstatus();
262 else if((c = getctitle(ev->window)))
263 drawtitle(c);
264 }
265 }
267 static void
268 keypress(XEvent *e)
269 {
270 static unsigned int len = sizeof(key) / sizeof(key[0]);
271 unsigned int i;
272 KeySym keysym;
273 XKeyEvent *ev = &e->xkey;
275 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
276 for(i = 0; i < len; i++) {
277 if(keysym == key[i].keysym &&
278 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
279 {
280 if(key[i].func)
281 key[i].func(&key[i].arg);
282 return;
283 }
284 }
285 }
287 static void
288 leavenotify(XEvent *e)
289 {
290 XCrossingEvent *ev = &e->xcrossing;
292 if((ev->window == root) && !ev->same_screen) {
293 issel = False;
294 drawall();
295 }
296 }
298 static void
299 mappingnotify(XEvent *e)
300 {
301 XMappingEvent *ev = &e->xmapping;
303 XRefreshKeyboardMapping(ev);
304 if(ev->request == MappingKeyboard)
305 grabkeys();
306 }
308 static void
309 maprequest(XEvent *e)
310 {
311 static XWindowAttributes wa;
312 XMapRequestEvent *ev = &e->xmaprequest;
314 if(!XGetWindowAttributes(dpy, ev->window, &wa))
315 return;
317 if(wa.override_redirect) {
318 XSelectInput(dpy, ev->window,
319 (StructureNotifyMask | PropertyChangeMask));
320 return;
321 }
323 if(!getclient(ev->window))
324 manage(ev->window, &wa);
325 }
327 static void
328 propertynotify(XEvent *e)
329 {
330 Client *c;
331 Window trans;
332 XPropertyEvent *ev = &e->xproperty;
334 if(ev->state == PropertyDelete)
335 return; /* ignore */
337 if((c = getclient(ev->window))) {
338 if(ev->atom == wmatom[WMProtocols]) {
339 c->proto = getproto(c->win);
340 return;
341 }
342 switch (ev->atom) {
343 default: break;
344 case XA_WM_TRANSIENT_FOR:
345 XGetTransientForHint(dpy, c->win, &trans);
346 if(!c->isfloat && (c->isfloat = (trans != 0)))
347 arrange(NULL);
348 break;
349 case XA_WM_NORMAL_HINTS:
350 setsize(c);
351 break;
352 }
353 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
354 settitle(c);
355 drawtitle(c);
356 }
357 }
358 }
360 static void
361 unmapnotify(XEvent *e)
362 {
363 Client *c;
364 XUnmapEvent *ev = &e->xunmap;
366 if((c = getclient(ev->window)))
367 unmanage(c);
368 }
370 /* extern */
372 void (*handler[LASTEvent]) (XEvent *) = {
373 [ButtonPress] = buttonpress,
374 [ConfigureRequest] = configurerequest,
375 [DestroyNotify] = destroynotify,
376 [EnterNotify] = enternotify,
377 [LeaveNotify] = leavenotify,
378 [Expose] = expose,
379 [KeyPress] = keypress,
380 [MappingNotify] = mappingnotify,
381 [MapRequest] = maprequest,
382 [PropertyNotify] = propertynotify,
383 [UnmapNotify] = unmapnotify
384 };
386 void
387 grabkeys()
388 {
389 static unsigned int len = sizeof(key) / sizeof(key[0]);
390 unsigned int i;
391 KeyCode code;
393 XUngrabKey(dpy, AnyKey, AnyModifier, root);
394 for(i = 0; i < len; i++) {
395 code = XKeysymToKeycode(dpy, key[i].keysym);
396 XGrabKey(dpy, code, key[i].mod, root, True,
397 GrabModeAsync, GrabModeAsync);
398 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
399 GrabModeAsync, GrabModeAsync);
400 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
401 GrabModeAsync, GrabModeAsync);
402 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
403 GrabModeAsync, GrabModeAsync);
404 }
405 }
407 void
408 procevent()
409 {
410 XEvent ev;
412 while(XPending(dpy)) {
413 XNextEvent(dpy, &ev);
414 if(handler[ev.type])
415 (handler[ev.type])(&ev); /* call handler */
416 }
417 }