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