aewl
view event.c @ 124:75576e44c1d8
made status bar drawing more robust, implemented togglemax and togglemode, works quite well
author | arg@10ksloc.org |
---|---|
date | Thu, 20 Jul 2006 15:07:35 +0200 |
parents | 61490330e90a |
children | b4b8b4236599 |
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"
7 #include <stdlib.h>
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
11 #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
12 #define MouseMask (ButtonMask | PointerMotionMask)
14 /* CUSTOMIZE */
16 typedef struct {
17 unsigned long mod;
18 KeySym keysym;
19 void (*func)(Arg *arg);
20 Arg arg;
21 } Key;
23 const char *browse[] = { "firefox", NULL };
24 const char *gimp[] = { "gimp", NULL };
25 const char *term[] = {
26 "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
27 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
28 };
29 const char *xlock[] = { "xlock", NULL };
31 static Key key[] = {
32 /* modifier key function arguments */
33 { ControlMask, XK_0, appendtag, { .i = Tscratch } },
34 { ControlMask, XK_1, appendtag, { .i = Tdev } },
35 { ControlMask, XK_2, appendtag, { .i = Twww } },
36 { ControlMask, XK_3, appendtag, { .i = Twork } },
37 { MODKEY, XK_0, view, { .i = Tscratch } },
38 { MODKEY, XK_1, view, { .i = Tdev } },
39 { MODKEY, XK_2, view, { .i = Twww } },
40 { MODKEY, XK_3, view, { .i = Twork } },
41 { MODKEY, XK_j, focusnext, { 0 } },
42 { MODKEY, XK_k, focusprev, { 0 } },
43 { MODKEY, XK_m, togglemax, { 0 } },
44 { MODKEY, XK_space, togglemode, { 0 } },
45 { MODKEY, XK_Return, zoom, { 0 } },
46 { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
47 { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
48 { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
49 { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
50 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
51 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
52 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Twww } },
53 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
54 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
55 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
56 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
57 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
58 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
59 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
60 };
62 /* static */
64 static void
65 movemouse(Client *c)
66 {
67 int x1, y1, ocx, ocy, di;
68 unsigned int dui;
69 Window dummy;
70 XEvent ev;
72 ocx = c->x;
73 ocy = c->y;
74 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
75 None, cursor[CurMove], CurrentTime) != GrabSuccess)
76 return;
77 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
78 for(;;) {
79 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
80 switch (ev.type) {
81 default: break;
82 case Expose:
83 handler[Expose](&ev);
84 break;
85 case MotionNotify:
86 XSync(dpy, False);
87 c->x = ocx + (ev.xmotion.x - x1);
88 c->y = ocy + (ev.xmotion.y - y1);
89 resize(c, False, TopLeft);
90 break;
91 case ButtonRelease:
92 XUngrabPointer(dpy, CurrentTime);
93 return;
94 }
95 }
96 }
98 static void
99 resizemouse(Client *c)
100 {
101 int ocx, ocy;
102 Corner sticky;
103 XEvent ev;
105 ocx = c->x;
106 ocy = c->y;
107 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
108 None, cursor[CurResize], CurrentTime) != GrabSuccess)
109 return;
110 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
111 for(;;) {
112 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
113 switch(ev.type) {
114 default: break;
115 case Expose:
116 handler[Expose](&ev);
117 break;
118 case MotionNotify:
119 XSync(dpy, False);
120 c->w = abs(ocx - ev.xmotion.x);
121 c->h = abs(ocy - ev.xmotion.y);
122 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
123 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
124 if(ocx <= ev.xmotion.x)
125 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
126 else
127 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
128 resize(c, True, sticky);
129 break;
130 case ButtonRelease:
131 XUngrabPointer(dpy, CurrentTime);
132 return;
133 }
134 }
135 }
137 static void
138 buttonpress(XEvent *e)
139 {
140 int x;
141 Arg a;
142 Client *c;
143 XButtonPressedEvent *ev = &e->xbutton;
145 if(barwin == ev->window) {
146 switch(ev->button) {
147 default:
148 x = 0;
149 for(a.i = 0; a.i < TLast; a.i++) {
150 x += textw(tags[a.i]);
151 if(ev->x < x) {
152 view(&a);
153 break;
154 }
155 }
156 break;
157 case Button4:
158 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
159 view(&a);
160 break;
161 case Button5:
162 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
163 view(&a);
164 break;
165 }
166 }
167 else if((c = getclient(ev->window))) {
168 switch(ev->button) {
169 default:
170 break;
171 case Button1:
172 if(arrange == dofloat || c->isfloat) {
173 higher(c);
174 movemouse(c);
175 }
176 break;
177 case Button2:
178 lower(c);
179 break;
180 case Button3:
181 if(arrange == dofloat || c->isfloat) {
182 higher(c);
183 resizemouse(c);
184 }
185 break;
186 }
187 }
188 }
190 static void
191 configurerequest(XEvent *e)
192 {
193 Client *c;
194 XConfigureRequestEvent *ev = &e->xconfigurerequest;
195 XWindowChanges wc;
197 ev->value_mask &= ~CWSibling;
198 if((c = getclient(ev->window))) {
199 gravitate(c, True);
200 if(ev->value_mask & CWX)
201 c->x = ev->x;
202 if(ev->value_mask & CWY)
203 c->y = ev->y;
204 if(ev->value_mask & CWWidth)
205 c->w = ev->width;
206 if(ev->value_mask & CWHeight)
207 c->h = ev->height;
208 if(ev->value_mask & CWBorderWidth)
209 c->border = 1;
210 gravitate(c, False);
211 resize(c, True, TopLeft);
212 }
214 wc.x = ev->x;
215 wc.y = ev->y;
216 wc.width = ev->width;
217 wc.height = ev->height;
218 wc.border_width = 1;
219 wc.sibling = None;
220 wc.stack_mode = Above;
221 ev->value_mask &= ~CWStackMode;
222 ev->value_mask |= CWBorderWidth;
223 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
224 XSync(dpy, False);
225 }
227 static void
228 destroynotify(XEvent *e)
229 {
230 Client *c;
231 XDestroyWindowEvent *ev = &e->xdestroywindow;
233 if((c = getclient(ev->window)))
234 unmanage(c);
235 }
237 static void
238 enternotify(XEvent *e)
239 {
240 Client *c;
241 XCrossingEvent *ev = &e->xcrossing;
243 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
244 return;
246 if((c = getclient(ev->window)))
247 focus(c);
248 else if(ev->window == root)
249 issel = True;
250 }
252 static void
253 expose(XEvent *e)
254 {
255 Client *c;
256 XExposeEvent *ev = &e->xexpose;
258 if(ev->count == 0) {
259 if(barwin == ev->window)
260 drawstatus();
261 else if((c = getctitle(ev->window)))
262 drawtitle(c);
263 }
264 }
266 static void
267 keypress(XEvent *e)
268 {
269 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
270 unsigned int i;
271 KeySym keysym;
272 XKeyEvent *ev = &e->xkey;
274 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
275 for(i = 0; i < len; i++)
276 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
277 if(key[i].func)
278 key[i].func(&key[i].arg);
279 return;
280 }
281 }
283 static void
284 leavenotify(XEvent *e)
285 {
286 XCrossingEvent *ev = &e->xcrossing;
288 if((ev->window == root) && !ev->same_screen)
289 issel = True;
290 }
292 static void
293 maprequest(XEvent *e)
294 {
295 static XWindowAttributes wa;
296 XMapRequestEvent *ev = &e->xmaprequest;
298 if(!XGetWindowAttributes(dpy, ev->window, &wa))
299 return;
301 if(wa.override_redirect) {
302 XSelectInput(dpy, ev->window,
303 (StructureNotifyMask | PropertyChangeMask));
304 return;
305 }
307 if(!getclient(ev->window))
308 manage(ev->window, &wa);
309 }
311 static void
312 propertynotify(XEvent *e)
313 {
314 Client *c;
315 Window trans;
316 XPropertyEvent *ev = &e->xproperty;
318 if(ev->state == PropertyDelete)
319 return; /* ignore */
321 if((c = getclient(ev->window))) {
322 if(ev->atom == wmatom[WMProtocols]) {
323 c->proto = getproto(c->win);
324 return;
325 }
326 switch (ev->atom) {
327 default: break;
328 case XA_WM_TRANSIENT_FOR:
329 XGetTransientForHint(dpy, c->win, &trans);
330 if(!c->isfloat && (c->isfloat = (trans != 0)))
331 arrange(NULL);
332 break;
333 case XA_WM_NORMAL_HINTS:
334 setsize(c);
335 break;
336 }
337 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
338 settitle(c);
339 drawtitle(c);
340 }
341 }
342 }
344 static void
345 unmapnotify(XEvent *e)
346 {
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 [MapRequest] = maprequest,
365 [PropertyNotify] = propertynotify,
366 [UnmapNotify] = unmapnotify
367 };
369 void
370 grabkeys()
371 {
372 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
373 unsigned int i;
374 KeyCode code;
376 for(i = 0; i < len; i++) {
377 code = XKeysymToKeycode(dpy, key[i].keysym);
378 XUngrabKey(dpy, code, key[i].mod, root);
379 XGrabKey(dpy, code, key[i].mod, root, True,
380 GrabModeAsync, GrabModeAsync);
381 }
382 }