aewl

view event.c @ 195:97960220eb77

fixed xterm font change (all other related apps should work fine with this fix as well)
author arg@10ksloc.org
date Fri, 04 Aug 2006 18:25:40 +0200
parents e848966a1ac6
children d560e796f7fe
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 view(&a);
112 break;
113 }
114 }
115 break;
116 case Button4:
117 viewnext(&a);
118 break;
119 case Button5:
120 viewprev(&a);
121 break;
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 focus(c);
126 switch(ev->button) {
127 default:
128 break;
129 case Button1:
130 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
131 higher(c);
132 movemouse(c);
133 }
134 break;
135 case Button2:
136 lower(c);
137 break;
138 case Button3:
139 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
140 higher(c);
141 resizemouse(c);
142 }
143 break;
144 }
145 }
146 }
148 static void
149 configurerequest(XEvent *e)
150 {
151 Client *c;
152 XConfigureRequestEvent *ev = &e->xconfigurerequest;
153 XEvent synev;
154 XWindowChanges wc;
155 unsigned long newmask;
157 if((c = getclient(ev->window))) {
158 gravitate(c, True);
159 if(ev->value_mask & CWX)
160 c->x = ev->x;
161 if(ev->value_mask & CWY)
162 c->y = ev->y;
163 if(ev->value_mask & CWWidth)
164 c->w = ev->width;
165 if(ev->value_mask & CWHeight)
166 c->h = ev->height;
167 if(ev->value_mask & CWBorderWidth)
168 c->border = ev->border_width;
169 gravitate(c, False);
170 wc.x = c->x;
171 wc.y = c->y;
172 wc.width = c->w;
173 wc.height = c->h;
174 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
175 if(newmask)
176 XConfigureWindow(dpy, c->win, newmask, &wc);
177 else {
178 synev.type = ConfigureNotify;
179 synev.xconfigure.display = dpy;
180 synev.xconfigure.event = c->win;
181 synev.xconfigure.window = c->win;
182 synev.xconfigure.x = c->x;
183 synev.xconfigure.y = c->y;
184 synev.xconfigure.width = c->w;
185 synev.xconfigure.height = c->h;
186 synev.xconfigure.border_width = c->border;
187 synev.xconfigure.above = None;
188 /* Send synthetic ConfigureNotify */
189 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
190 }
191 XSync(dpy, False);
192 arrange(NULL);
193 drawall();
194 }
195 else {
196 wc.x = ev->x;
197 wc.y = ev->y;
198 wc.width = ev->width;
199 wc.height = ev->height;
200 wc.border_width = ev->border_width;
201 wc.sibling = ev->above;
202 wc.stack_mode = ev->detail;
203 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
204 XSync(dpy, False);
205 }
206 }
208 static void
209 destroynotify(XEvent *e)
210 {
211 Client *c;
212 XDestroyWindowEvent *ev = &e->xdestroywindow;
214 if((c = getclient(ev->window)))
215 unmanage(c);
216 }
218 static void
219 enternotify(XEvent *e)
220 {
221 Client *c;
222 XCrossingEvent *ev = &e->xcrossing;
224 if(ev->detail == NotifyInferior)
225 return;
227 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
228 focus(c);
229 else if(ev->window == root)
230 issel = True;
231 }
233 static void
234 expose(XEvent *e)
235 {
236 Client *c;
237 XExposeEvent *ev = &e->xexpose;
239 if(ev->count == 0) {
240 if(barwin == ev->window)
241 drawstatus();
242 else if((c = getctitle(ev->window)))
243 drawtitle(c);
244 }
245 }
247 static void
248 keypress(XEvent *e)
249 {
250 static unsigned int len = sizeof(key) / sizeof(key[0]);
251 unsigned int i;
252 KeySym keysym;
253 XKeyEvent *ev = &e->xkey;
255 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
256 for(i = 0; i < len; i++)
257 if(keysym == key[i].keysym &&
258 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
259 if(key[i].func)
260 key[i].func(&key[i].arg);
261 return;
262 }
263 }
265 static void
266 leavenotify(XEvent *e)
267 {
268 XCrossingEvent *ev = &e->xcrossing;
270 if((ev->window == root) && !ev->same_screen)
271 issel = True;
272 }
274 static void
275 maprequest(XEvent *e)
276 {
277 static XWindowAttributes wa;
278 XMapRequestEvent *ev = &e->xmaprequest;
280 if(!XGetWindowAttributes(dpy, ev->window, &wa))
281 return;
283 if(wa.override_redirect) {
284 XSelectInput(dpy, ev->window,
285 (StructureNotifyMask | PropertyChangeMask));
286 return;
287 }
289 if(!getclient(ev->window))
290 manage(ev->window, &wa);
291 }
293 static void
294 propertynotify(XEvent *e)
295 {
296 Client *c;
297 Window trans;
298 XPropertyEvent *ev = &e->xproperty;
300 if(ev->state == PropertyDelete)
301 return; /* ignore */
303 if((c = getclient(ev->window))) {
304 if(ev->atom == wmatom[WMProtocols]) {
305 c->proto = getproto(c->win);
306 return;
307 }
308 switch (ev->atom) {
309 default: break;
310 case XA_WM_TRANSIENT_FOR:
311 XGetTransientForHint(dpy, c->win, &trans);
312 if(!c->isfloat && (c->isfloat = (trans != 0)))
313 arrange(NULL);
314 break;
315 case XA_WM_NORMAL_HINTS:
316 setsize(c);
317 break;
318 }
319 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
320 settitle(c);
321 drawtitle(c);
322 }
323 }
324 }
326 static void
327 unmapnotify(XEvent *e)
328 {
329 Client *c;
330 XUnmapEvent *ev = &e->xunmap;
332 if((c = getclient(ev->window)))
333 unmanage(c);
334 }
336 /* extern */
338 void (*handler[LASTEvent]) (XEvent *) = {
339 [ButtonPress] = buttonpress,
340 [ConfigureRequest] = configurerequest,
341 [DestroyNotify] = destroynotify,
342 [EnterNotify] = enternotify,
343 [LeaveNotify] = leavenotify,
344 [Expose] = expose,
345 [KeyPress] = keypress,
346 [MapRequest] = maprequest,
347 [PropertyNotify] = propertynotify,
348 [UnmapNotify] = unmapnotify
349 };
351 void
352 grabkeys()
353 {
354 static unsigned int len = sizeof(key) / sizeof(key[0]);
355 unsigned int i;
356 KeyCode code;
358 for(i = 0; i < len; i++) {
359 code = XKeysymToKeycode(dpy, key[i].keysym);
360 /* normal */
361 XUngrabKey(dpy, code, key[i].mod, root);
362 XGrabKey(dpy, code, key[i].mod, root, True,
363 GrabModeAsync, GrabModeAsync);
364 /* capslock */
365 XUngrabKey(dpy, code, key[i].mod | LockMask, root);
366 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
367 GrabModeAsync, GrabModeAsync);
368 /* numlock */
369 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
370 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
371 GrabModeAsync, GrabModeAsync);
372 /* capslock & numlock */
373 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
374 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
375 GrabModeAsync, GrabModeAsync);
376 }
377 }