aewl

view event.c @ 77:38c8f7f7d401

sanitized other stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Sat, 15 Jul 2006 17:19:19 +0200
parents 4bd49f404f10
children 0d71fb80b592
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 const char *term[] = {
17 "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn",
18 "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL
19 };
20 const char *browse[] = { "firefox", NULL };
21 const char *xlock[] = { "xlock", NULL };
23 Key key[] = {
24 /* modifier key function arguments */
25 { Mod1Mask, XK_Return, zoom, { 0 } },
26 { Mod1Mask, XK_k, focusprev, { 0 } },
27 { Mod1Mask, XK_j, focusnext, { 0 } },
28 { Mod1Mask, XK_m, maximize, { 0 } },
29 { Mod1Mask, XK_0, view, { .i = Tscratch } },
30 { Mod1Mask, XK_1, view, { .i = Tdev } },
31 { Mod1Mask, XK_2, view, { .i = Twww } },
32 { Mod1Mask, XK_3, view, { .i = Twork } },
33 { Mod1Mask, XK_space, dotile, { 0 } },
34 { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } },
35 { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
36 { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } },
37 { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } },
38 { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } },
39 { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } },
40 { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
41 { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
42 { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } },
43 { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } },
44 { ControlMask, XK_0, appendtag, { .i = Tscratch } },
45 { ControlMask, XK_1, appendtag, { .i = Tdev } },
46 { ControlMask, XK_2, appendtag, { .i = Twww } },
47 { ControlMask, XK_3, appendtag, { .i = Twork } },
48 };
50 /********** CUSTOMIZE **********/
52 /* static functions */
54 static void
55 movemouse(Client *c)
56 {
57 XEvent ev;
58 int x1, y1, ocx, ocy, di;
59 unsigned int dui;
60 Window dummy;
62 ocx = c->x;
63 ocy = c->y;
64 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
65 None, cursor[CurMove], CurrentTime) != GrabSuccess)
66 return;
67 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
68 for(;;) {
69 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
70 switch (ev.type) {
71 default: break;
72 case Expose:
73 handler[Expose](&ev);
74 break;
75 case MotionNotify:
76 XFlush(dpy);
77 c->x = ocx + (ev.xmotion.x - x1);
78 c->y = ocy + (ev.xmotion.y - y1);
79 resize(c, False);
80 break;
81 case ButtonRelease:
82 XUngrabPointer(dpy, CurrentTime);
83 return;
84 }
85 }
86 }
88 static void
89 resizemouse(Client *c)
90 {
91 XEvent ev;
92 int ocx, ocy;
94 ocx = c->x;
95 ocy = c->y;
96 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
97 None, cursor[CurResize], CurrentTime) != GrabSuccess)
98 return;
99 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
100 for(;;) {
101 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
102 switch(ev.type) {
103 default: break;
104 case Expose:
105 handler[Expose](&ev);
106 break;
107 case MotionNotify:
108 XFlush(dpy);
109 c->w = abs(ocx - ev.xmotion.x);
110 c->h = abs(ocy - ev.xmotion.y);
111 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
112 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
113 resize(c, True);
114 break;
115 case ButtonRelease:
116 XUngrabPointer(dpy, CurrentTime);
117 return;
118 }
119 }
120 }
122 static void
123 buttonpress(XEvent *e)
124 {
125 int x;
126 Arg a;
127 XButtonPressedEvent *ev = &e->xbutton;
128 Client *c;
130 if(barwin == ev->window) {
131 x = (arrange == dofloat) ? textw("~") : 0;
132 for(a.i = 0; a.i < TLast; a.i++) {
133 x += textw(tags[a.i]);
134 if(ev->x < x) {
135 view(&a);
136 break;
137 }
138 }
139 }
140 else if((c = getclient(ev->window))) {
141 if(arrange == dotile && !c->dofloat)
142 return;
143 higher(c);
144 switch(ev->button) {
145 default:
146 break;
147 case Button1:
148 movemouse(c);
149 break;
150 case Button2:
151 lower(c);
152 break;
153 case Button3:
154 resizemouse(c);
155 break;
156 }
157 }
158 }
160 static void
161 configurerequest(XEvent *e)
162 {
163 XConfigureRequestEvent *ev = &e->xconfigurerequest;
164 XWindowChanges wc;
165 Client *c;
167 ev->value_mask &= ~CWSibling;
168 if((c = getclient(ev->window))) {
169 gravitate(c, True);
170 if(ev->value_mask & CWX)
171 c->x = ev->x;
172 if(ev->value_mask & CWY)
173 c->y = ev->y;
174 if(ev->value_mask & CWWidth)
175 c->w = ev->width;
176 if(ev->value_mask & CWHeight)
177 c->h = ev->height;
178 if(ev->value_mask & CWBorderWidth)
179 c->border = 1;
180 gravitate(c, False);
181 resize(c, True);
182 }
184 wc.x = ev->x;
185 wc.y = ev->y;
186 wc.width = ev->width;
187 wc.height = ev->height;
188 wc.border_width = 1;
189 wc.sibling = None;
190 wc.stack_mode = Above;
191 ev->value_mask &= ~CWStackMode;
192 ev->value_mask |= CWBorderWidth;
193 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
194 XFlush(dpy);
195 }
197 static void
198 destroynotify(XEvent *e)
199 {
200 Client *c;
201 XDestroyWindowEvent *ev = &e->xdestroywindow;
203 if((c = getclient(ev->window)))
204 unmanage(c);
205 }
207 static void
208 enternotify(XEvent *e)
209 {
210 XCrossingEvent *ev = &e->xcrossing;
211 Client *c;
213 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
214 return;
216 if((c = getclient(ev->window)))
217 focus(c);
218 else if(ev->window == root)
219 issel = True;
220 }
222 static void
223 expose(XEvent *e)
224 {
225 XExposeEvent *ev = &e->xexpose;
226 Client *c;
228 if(ev->count == 0) {
229 if(barwin == ev->window)
230 drawstatus();
231 else if((c = getctitle(ev->window)))
232 drawtitle(c);
233 }
234 }
236 static void
237 keypress(XEvent *e)
238 {
239 XKeyEvent *ev = &e->xkey;
240 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
241 unsigned int i;
242 KeySym keysym;
244 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
245 for(i = 0; i < len; i++)
246 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
247 if(key[i].func)
248 key[i].func(&key[i].arg);
249 return;
250 }
251 }
253 static void
254 leavenotify(XEvent *e)
255 {
256 XCrossingEvent *ev = &e->xcrossing;
258 if((ev->window == root) && !ev->same_screen)
259 issel = True;
260 }
262 static void
263 maprequest(XEvent *e)
264 {
265 XMapRequestEvent *ev = &e->xmaprequest;
266 static XWindowAttributes wa;
268 if(!XGetWindowAttributes(dpy, ev->window, &wa))
269 return;
271 if(wa.override_redirect) {
272 XSelectInput(dpy, ev->window,
273 (StructureNotifyMask | PropertyChangeMask));
274 return;
275 }
277 if(!getclient(ev->window))
278 manage(ev->window, &wa);
279 }
281 static void
282 propertynotify(XEvent *e)
283 {
284 XPropertyEvent *ev = &e->xproperty;
285 Window trans;
286 Client *c;
288 if(ev->state == PropertyDelete)
289 return; /* ignore */
291 if((c = getclient(ev->window))) {
292 if(ev->atom == wmatom[WMProtocols]) {
293 c->proto = getproto(c->win);
294 return;
295 }
296 switch (ev->atom) {
297 default: break;
298 case XA_WM_TRANSIENT_FOR:
299 XGetTransientForHint(dpy, c->win, &trans);
300 if(!c->dofloat && (c->dofloat = (trans != 0)))
301 arrange(NULL);
302 break;
303 case XA_WM_NORMAL_HINTS:
304 setsize(c);
305 break;
306 }
307 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
308 settitle(c);
309 drawtitle(c);
310 }
311 }
312 }
314 static void
315 unmapnotify(XEvent *e)
316 {
317 Client *c;
318 XUnmapEvent *ev = &e->xunmap;
320 if((c = getclient(ev->window)))
321 unmanage(c);
322 }
324 /* extern functions */
326 void (*handler[LASTEvent]) (XEvent *) = {
327 [ButtonPress] = buttonpress,
328 [ConfigureRequest] = configurerequest,
329 [DestroyNotify] = destroynotify,
330 [EnterNotify] = enternotify,
331 [LeaveNotify] = leavenotify,
332 [Expose] = expose,
333 [KeyPress] = keypress,
334 [MapRequest] = maprequest,
335 [PropertyNotify] = propertynotify,
336 [UnmapNotify] = unmapnotify
337 };
339 void
340 grabkeys()
341 {
342 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
343 unsigned int i;
344 KeyCode code;
346 for(i = 0; i < len; i++) {
347 code = XKeysymToKeycode(dpy, key[i].keysym);
348 XUngrabKey(dpy, code, key[i].mod, root);
349 XGrabKey(dpy, code, key[i].mod, root, True,
350 GrabModeAsync, GrabModeAsync);
351 }
352 }