dwm-meillo
view event.c @ 144:e61447a7f249
applied Jukkas prev/next patch with XK_{h,l}
author | arg@10ksloc.org |
---|---|
date | Tue, 01 Aug 2006 12:39:14 +0200 |
parents | 36cabfe408cd |
children | 774754477c35 |
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 /* CUSTOMIZE */
13 typedef struct {
14 unsigned long mod;
15 KeySym keysym;
16 void (*func)(Arg *arg);
17 Arg arg;
18 } Key;
20 const char *browse[] = { "firefox", NULL };
21 const char *gimp[] = { "gimp", NULL };
22 const char *term[] = { /*"xterm", NULL };*/
23 "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
24 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
25 };
26 const char *xlock[] = { "xlock", NULL };
28 static Key key[] = {
29 /* modifier key function arguments */
30 { MODKEY, XK_0, view, { .i = Tfnord } },
31 { MODKEY, XK_1, view, { .i = Tdev } },
32 { MODKEY, XK_2, view, { .i = Tnet } },
33 { MODKEY, XK_3, view, { .i = Twork } },
34 { MODKEY, XK_4, view, { .i = Tmisc} },
35 { MODKEY, XK_h, viewprev, { 0 } },
36 { MODKEY, XK_j, focusnext, { 0 } },
37 { MODKEY, XK_k, focusprev, { 0 } },
38 { MODKEY, XK_l, viewnext, { 0 } },
39 { MODKEY, XK_m, togglemax, { 0 } },
40 { MODKEY, XK_space, togglemode, { 0 } },
41 { MODKEY, XK_Return, zoom, { 0 } },
42 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
43 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
44 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
45 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
46 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
47 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
48 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
49 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
50 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
51 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
52 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
53 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
54 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
55 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
56 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
57 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
58 };
60 /* END CUSTOMIZE */
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 focus(c);
169 switch(ev->button) {
170 default:
171 break;
172 case Button1:
173 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
174 higher(c);
175 movemouse(c);
176 }
177 break;
178 case Button2:
179 lower(c);
180 break;
181 case Button3:
182 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
183 higher(c);
184 resizemouse(c);
185 }
186 break;
187 }
188 }
189 }
191 static void
192 configurerequest(XEvent *e)
193 {
194 Client *c;
195 XConfigureRequestEvent *ev = &e->xconfigurerequest;
196 XWindowChanges wc;
198 ev->value_mask &= ~CWSibling;
199 if((c = getclient(ev->window))) {
200 gravitate(c, True);
201 if(ev->value_mask & CWX)
202 c->x = ev->x;
203 if(ev->value_mask & CWY)
204 c->y = ev->y;
205 if(ev->value_mask & CWWidth)
206 c->w = ev->width;
207 if(ev->value_mask & CWHeight)
208 c->h = ev->height;
209 if(ev->value_mask & CWBorderWidth)
210 c->border = 1;
211 gravitate(c, False);
212 resize(c, True, TopLeft);
213 }
215 wc.x = ev->x;
216 wc.y = ev->y;
217 wc.width = ev->width;
218 wc.height = ev->height;
219 wc.border_width = 1;
220 wc.sibling = None;
221 wc.stack_mode = Above;
222 ev->value_mask &= ~CWStackMode;
223 ev->value_mask |= CWBorderWidth;
224 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
225 XSync(dpy, False);
226 }
228 static void
229 destroynotify(XEvent *e)
230 {
231 Client *c;
232 XDestroyWindowEvent *ev = &e->xdestroywindow;
234 if((c = getclient(ev->window)))
235 unmanage(c);
236 }
238 static void
239 enternotify(XEvent *e)
240 {
241 Client *c;
242 XCrossingEvent *ev = &e->xcrossing;
244 if(ev->detail == NotifyInferior)
245 return;
247 if((c = getclient(ev->window)))
248 focus(c);
249 else if(ev->window == root)
250 issel = True;
251 }
253 static void
254 expose(XEvent *e)
255 {
256 Client *c;
257 XExposeEvent *ev = &e->xexpose;
259 if(ev->count == 0) {
260 if(barwin == ev->window)
261 drawstatus();
262 else if((c = getctitle(ev->window)))
263 drawtitle(c);
264 }
265 }
267 static void
268 keypress(XEvent *e)
269 {
270 static unsigned int len = sizeof(key) / sizeof(key[0]);
271 unsigned int i;
272 KeySym keysym;
273 XKeyEvent *ev = &e->xkey;
275 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
276 for(i = 0; i < len; i++)
277 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
278 if(key[i].func)
279 key[i].func(&key[i].arg);
280 return;
281 }
282 }
284 static void
285 leavenotify(XEvent *e)
286 {
287 XCrossingEvent *ev = &e->xcrossing;
289 if((ev->window == root) && !ev->same_screen)
290 issel = True;
291 }
293 static void
294 maprequest(XEvent *e)
295 {
296 static XWindowAttributes wa;
297 XMapRequestEvent *ev = &e->xmaprequest;
299 if(!XGetWindowAttributes(dpy, ev->window, &wa))
300 return;
302 if(wa.override_redirect) {
303 XSelectInput(dpy, ev->window,
304 (StructureNotifyMask | PropertyChangeMask));
305 return;
306 }
308 if(!getclient(ev->window))
309 manage(ev->window, &wa);
310 }
312 static void
313 propertynotify(XEvent *e)
314 {
315 Client *c;
316 Window trans;
317 XPropertyEvent *ev = &e->xproperty;
319 if(ev->state == PropertyDelete)
320 return; /* ignore */
322 if((c = getclient(ev->window))) {
323 if(ev->atom == wmatom[WMProtocols]) {
324 c->proto = getproto(c->win);
325 return;
326 }
327 switch (ev->atom) {
328 default: break;
329 case XA_WM_TRANSIENT_FOR:
330 XGetTransientForHint(dpy, c->win, &trans);
331 if(!c->isfloat && (c->isfloat = (trans != 0)))
332 arrange(NULL);
333 break;
334 case XA_WM_NORMAL_HINTS:
335 setsize(c);
336 break;
337 }
338 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
339 settitle(c);
340 drawtitle(c);
341 }
342 }
343 }
345 static void
346 unmapnotify(XEvent *e)
347 {
348 Client *c;
349 XUnmapEvent *ev = &e->xunmap;
351 if((c = getclient(ev->window)))
352 unmanage(c);
353 }
355 /* extern */
357 void (*handler[LASTEvent]) (XEvent *) = {
358 [ButtonPress] = buttonpress,
359 [ConfigureRequest] = configurerequest,
360 [DestroyNotify] = destroynotify,
361 [EnterNotify] = enternotify,
362 [LeaveNotify] = leavenotify,
363 [Expose] = expose,
364 [KeyPress] = keypress,
365 [MapRequest] = maprequest,
366 [PropertyNotify] = propertynotify,
367 [UnmapNotify] = unmapnotify
368 };
370 void
371 grabkeys()
372 {
373 static unsigned int len = sizeof(key) / sizeof(key[0]);
374 unsigned int i;
375 KeyCode code;
377 for(i = 0; i < len; i++) {
378 code = XKeysymToKeycode(dpy, key[i].keysym);
379 XUngrabKey(dpy, code, key[i].mod, root);
380 XGrabKey(dpy, code, key[i].mod, root, True,
381 GrabModeAsync, GrabModeAsync);
382 }
383 }