dwm-meillo

view event.c @ 107:f5b0c3d22d93

prepared 0.3
author arg@10ksloc.org
date Wed, 19 Jul 2006 14:43:17 +0200
parents 3e74cc981e9b
children 2a1879e05d5a
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 */
15 const char *term[] = { "xterm", NULL };
17 Key key[] = {
18 /* modifier key function arguments */
19 { ControlMask, XK_0, appendtag, { .i = Tscratch } },
20 { ControlMask, XK_1, appendtag, { .i = Tdev } },
21 { ControlMask, XK_2, appendtag, { .i = Twww } },
22 { ControlMask, XK_3, appendtag, { .i = Twork } },
23 { Mod1Mask, XK_0, view, { .i = Tscratch } },
24 { Mod1Mask, XK_1, view, { .i = Tdev } },
25 { Mod1Mask, XK_2, view, { .i = Twww } },
26 { Mod1Mask, XK_3, view, { .i = Twork } },
27 { Mod1Mask, XK_j, focusnext, { 0 } },
28 { Mod1Mask, XK_k, focusprev, { 0 } },
29 { Mod1Mask, XK_m, maximize, { 0 } },
30 { Mod1Mask, XK_space, dotile, { 0 } },
31 { Mod1Mask, XK_Return, zoom, { 0 } },
32 { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
33 { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
34 { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
35 { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
36 { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
37 { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } },
38 { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } },
39 { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } },
40 { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } },
41 { Mod1Mask|ShiftMask, XK_q, quit, { 0 } },
42 { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } },
43 { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } },
44 };
46 /* static */
48 static void
49 movemouse(Client *c)
50 {
51 XEvent ev;
52 int x1, y1, ocx, ocy, di;
53 unsigned int dui;
54 Window dummy;
56 ocx = *c->x;
57 ocy = *c->y;
58 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
59 None, cursor[CurMove], CurrentTime) != GrabSuccess)
60 return;
61 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
62 for(;;) {
63 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
64 switch (ev.type) {
65 default: break;
66 case Expose:
67 handler[Expose](&ev);
68 break;
69 case MotionNotify:
70 XSync(dpy, False);
71 *c->x = ocx + (ev.xmotion.x - x1);
72 *c->y = ocy + (ev.xmotion.y - y1);
73 resize(c, False, TopLeft);
74 break;
75 case ButtonRelease:
76 XUngrabPointer(dpy, CurrentTime);
77 return;
78 }
79 }
80 }
82 static void
83 resizemouse(Client *c)
84 {
85 XEvent ev;
86 int ocx, ocy;
87 Corner sticky;
89 ocx = *c->x;
90 ocy = *c->y;
91 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
92 None, cursor[CurResize], CurrentTime) != GrabSuccess)
93 return;
94 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, *c->w, *c->h);
95 for(;;) {
96 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
97 switch(ev.type) {
98 default: break;
99 case Expose:
100 handler[Expose](&ev);
101 break;
102 case MotionNotify:
103 XSync(dpy, False);
104 *c->w = abs(ocx - ev.xmotion.x);
105 *c->h = abs(ocy - ev.xmotion.y);
106 *c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - *c->w;
107 *c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - *c->h;
108 if(ocx <= ev.xmotion.x)
109 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
110 else
111 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
112 resize(c, True, sticky);
113 break;
114 case ButtonRelease:
115 XUngrabPointer(dpy, CurrentTime);
116 return;
117 }
118 }
119 }
121 static void
122 buttonpress(XEvent *e)
123 {
124 int x;
125 Arg a;
126 XButtonPressedEvent *ev = &e->xbutton;
127 Client *c;
129 if(barwin == ev->window) {
130 switch(ev->button) {
131 default:
132 x = 0;
133 for(a.i = 0; a.i < TLast; a.i++) {
134 x += textw(tags[a.i]);
135 if(ev->x < x) {
136 view(&a);
137 break;
138 }
139 }
140 break;
141 case Button4:
142 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
143 view(&a);
144 break;
145 case Button5:
146 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
147 view(&a);
148 break;
149 }
150 }
151 else if((c = getclient(ev->window))) {
152 switch(ev->button) {
153 default:
154 break;
155 case Button1:
156 if(arrange == dotile && !c->isfloat) {
157 if((ev->state & ControlMask) && (ev->button == Button1))
158 zoom(NULL);
159 }
160 else {
161 higher(c);
162 movemouse(c);
163 }
164 break;
165 case Button2:
166 lower(c);
167 break;
168 case Button3:
169 if(arrange == dofloat || c->isfloat) {
170 higher(c);
171 resizemouse(c);
172 }
173 break;
174 }
175 }
176 }
178 static void
179 configurerequest(XEvent *e)
180 {
181 XConfigureRequestEvent *ev = &e->xconfigurerequest;
182 XWindowChanges wc;
183 Client *c;
185 ev->value_mask &= ~CWSibling;
186 if((c = getclient(ev->window))) {
187 gravitate(c, True);
188 if(ev->value_mask & CWX)
189 *c->x = ev->x;
190 if(ev->value_mask & CWY)
191 *c->y = ev->y;
192 if(ev->value_mask & CWWidth)
193 *c->w = ev->width;
194 if(ev->value_mask & CWHeight)
195 *c->h = ev->height;
196 if(ev->value_mask & CWBorderWidth)
197 c->border = 1;
198 gravitate(c, False);
199 resize(c, True, TopLeft);
200 }
202 wc.x = ev->x;
203 wc.y = ev->y;
204 wc.width = ev->width;
205 wc.height = ev->height;
206 wc.border_width = 1;
207 wc.sibling = None;
208 wc.stack_mode = Above;
209 ev->value_mask &= ~CWStackMode;
210 ev->value_mask |= CWBorderWidth;
211 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
212 XSync(dpy, False);
213 }
215 static void
216 destroynotify(XEvent *e)
217 {
218 Client *c;
219 XDestroyWindowEvent *ev = &e->xdestroywindow;
221 if((c = getclient(ev->window)))
222 unmanage(c);
223 }
225 static void
226 enternotify(XEvent *e)
227 {
228 XCrossingEvent *ev = &e->xcrossing;
229 Client *c;
231 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
232 return;
234 if((c = getclient(ev->window)))
235 focus(c);
236 else if(ev->window == root)
237 issel = True;
238 }
240 static void
241 expose(XEvent *e)
242 {
243 XExposeEvent *ev = &e->xexpose;
244 Client *c;
246 if(ev->count == 0) {
247 if(barwin == ev->window)
248 drawstatus();
249 else if((c = getctitle(ev->window)))
250 drawtitle(c);
251 }
252 }
254 static void
255 keypress(XEvent *e)
256 {
257 XKeyEvent *ev = &e->xkey;
258 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
259 unsigned int i;
260 KeySym keysym;
262 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
263 for(i = 0; i < len; i++)
264 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
265 if(key[i].func)
266 key[i].func(&key[i].arg);
267 return;
268 }
269 }
271 static void
272 leavenotify(XEvent *e)
273 {
274 XCrossingEvent *ev = &e->xcrossing;
276 if((ev->window == root) && !ev->same_screen)
277 issel = True;
278 }
280 static void
281 maprequest(XEvent *e)
282 {
283 XMapRequestEvent *ev = &e->xmaprequest;
284 static XWindowAttributes wa;
286 if(!XGetWindowAttributes(dpy, ev->window, &wa))
287 return;
289 if(wa.override_redirect) {
290 XSelectInput(dpy, ev->window,
291 (StructureNotifyMask | PropertyChangeMask));
292 return;
293 }
295 if(!getclient(ev->window))
296 manage(ev->window, &wa);
297 }
299 static void
300 propertynotify(XEvent *e)
301 {
302 XPropertyEvent *ev = &e->xproperty;
303 Window trans;
304 Client *c;
306 if(ev->state == PropertyDelete)
307 return; /* ignore */
309 if((c = getclient(ev->window))) {
310 if(ev->atom == wmatom[WMProtocols]) {
311 c->proto = getproto(c->win);
312 return;
313 }
314 switch (ev->atom) {
315 default: break;
316 case XA_WM_TRANSIENT_FOR:
317 XGetTransientForHint(dpy, c->win, &trans);
318 if(!c->isfloat && (c->isfloat = (trans != 0)))
319 arrange(NULL);
320 break;
321 case XA_WM_NORMAL_HINTS:
322 setsize(c);
323 break;
324 }
325 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
326 settitle(c);
327 drawtitle(c);
328 }
329 }
330 }
332 static void
333 unmapnotify(XEvent *e)
334 {
335 Client *c;
336 XUnmapEvent *ev = &e->xunmap;
338 if((c = getclient(ev->window)))
339 unmanage(c);
340 }
342 /* extern */
344 void (*handler[LASTEvent]) (XEvent *) = {
345 [ButtonPress] = buttonpress,
346 [ConfigureRequest] = configurerequest,
347 [DestroyNotify] = destroynotify,
348 [EnterNotify] = enternotify,
349 [LeaveNotify] = leavenotify,
350 [Expose] = expose,
351 [KeyPress] = keypress,
352 [MapRequest] = maprequest,
353 [PropertyNotify] = propertynotify,
354 [UnmapNotify] = unmapnotify
355 };
357 void
358 grabkeys()
359 {
360 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
361 unsigned int i;
362 KeyCode code;
364 for(i = 0; i < len; i++) {
365 code = XKeysymToKeycode(dpy, key[i].keysym);
366 XUngrabKey(dpy, code, key[i].mod, root);
367 XGrabKey(dpy, code, key[i].mod, root, True,
368 GrabModeAsync, GrabModeAsync);
369 }
370 }