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