aewl

view event.c @ 237:7f8f7f14e9cd

readded border color, this sucks least
author Anselm R.Garbe <arg@10ksloc.org>
date Thu, 10 Aug 2006 11:12:15 +0200
parents f7e87c894b5e
children 969fb6be25e1
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) {
131 if(arrange == dofloat || c->isfloat) {
132 higher(c);
133 movemouse(c);
134 }
135 else
136 zoom(NULL);
137 }
138 break;
139 case Button3:
140 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
141 higher(c);
142 resizemouse(c);
143 }
144 break;
145 }
146 }
147 }
149 static void
150 configurerequest(XEvent *e)
151 {
152 Client *c;
153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
154 XEvent synev;
155 XWindowChanges wc;
156 unsigned long newmask;
158 if((c = getclient(ev->window))) {
159 gravitate(c, True);
160 if(ev->value_mask & CWX)
161 c->x = ev->x;
162 if(ev->value_mask & CWY)
163 c->y = ev->y;
164 if(ev->value_mask & CWWidth)
165 c->w = ev->width;
166 if(ev->value_mask & CWHeight)
167 c->h = ev->height;
168 if(ev->value_mask & CWBorderWidth)
169 c->border = ev->border_width;
170 gravitate(c, False);
171 wc.x = c->x;
172 wc.y = c->y;
173 wc.width = c->w;
174 wc.height = c->h;
175 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
176 if(newmask)
177 XConfigureWindow(dpy, c->win, newmask, &wc);
178 else {
179 synev.type = ConfigureNotify;
180 synev.xconfigure.display = dpy;
181 synev.xconfigure.event = c->win;
182 synev.xconfigure.window = c->win;
183 synev.xconfigure.x = c->x;
184 synev.xconfigure.y = c->y;
185 synev.xconfigure.width = c->w;
186 synev.xconfigure.height = c->h;
187 synev.xconfigure.border_width = c->border;
188 synev.xconfigure.above = None;
189 /* Send synthetic ConfigureNotify */
190 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
191 }
192 XSync(dpy, False);
193 if(c->isfloat)
194 resize(c, False, TopLeft);
195 else
196 arrange(NULL);
197 }
198 else {
199 wc.x = ev->x;
200 wc.y = ev->y;
201 wc.width = ev->width;
202 wc.height = ev->height;
203 wc.border_width = ev->border_width;
204 wc.sibling = ev->above;
205 wc.stack_mode = ev->detail;
206 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
207 XSync(dpy, False);
208 }
209 }
211 static void
212 destroynotify(XEvent *e)
213 {
214 Client *c;
215 XDestroyWindowEvent *ev = &e->xdestroywindow;
217 if((c = getclient(ev->window)))
218 unmanage(c);
219 }
221 static void
222 enternotify(XEvent *e)
223 {
224 Client *c;
225 XCrossingEvent *ev = &e->xcrossing;
227 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
228 return;
230 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
231 focus(c);
232 else if(ev->window == root)
233 issel = True;
234 }
236 static void
237 expose(XEvent *e)
238 {
239 Client *c;
240 XExposeEvent *ev = &e->xexpose;
242 if(ev->count == 0) {
243 if(barwin == ev->window)
244 drawstatus();
245 else if((c = getctitle(ev->window)))
246 drawtitle(c);
247 }
248 }
250 static void
251 keypress(XEvent *e)
252 {
253 static unsigned int len = sizeof(key) / sizeof(key[0]);
254 unsigned int i;
255 KeySym keysym;
256 XKeyEvent *ev = &e->xkey;
258 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
259 for(i = 0; i < len; i++)
260 if(keysym == key[i].keysym &&
261 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
262 if(key[i].func)
263 key[i].func(&key[i].arg);
264 return;
265 }
266 }
268 static void
269 leavenotify(XEvent *e)
270 {
271 XCrossingEvent *ev = &e->xcrossing;
273 if((ev->window == root) && !ev->same_screen)
274 issel = True;
275 }
277 static void
278 maprequest(XEvent *e)
279 {
280 static XWindowAttributes wa;
281 XMapRequestEvent *ev = &e->xmaprequest;
283 if(!XGetWindowAttributes(dpy, ev->window, &wa))
284 return;
286 if(wa.override_redirect) {
287 XSelectInput(dpy, ev->window,
288 (StructureNotifyMask | PropertyChangeMask));
289 return;
290 }
292 if(!getclient(ev->window))
293 manage(ev->window, &wa);
294 }
296 static void
297 propertynotify(XEvent *e)
298 {
299 Client *c;
300 Window trans;
301 XPropertyEvent *ev = &e->xproperty;
303 if(ev->state == PropertyDelete)
304 return; /* ignore */
306 if((c = getclient(ev->window))) {
307 if(ev->atom == wmatom[WMProtocols]) {
308 c->proto = getproto(c->win);
309 return;
310 }
311 switch (ev->atom) {
312 default: break;
313 case XA_WM_TRANSIENT_FOR:
314 XGetTransientForHint(dpy, c->win, &trans);
315 if(!c->isfloat && (c->isfloat = (trans != 0)))
316 arrange(NULL);
317 break;
318 case XA_WM_NORMAL_HINTS:
319 setsize(c);
320 break;
321 }
322 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
323 settitle(c);
324 drawtitle(c);
325 }
326 }
327 }
329 static void
330 unmapnotify(XEvent *e)
331 {
332 Client *c;
333 XUnmapEvent *ev = &e->xunmap;
335 if((c = getclient(ev->window)))
336 unmanage(c);
337 }
339 /* extern */
341 void (*handler[LASTEvent]) (XEvent *) = {
342 [ButtonPress] = buttonpress,
343 [ConfigureRequest] = configurerequest,
344 [DestroyNotify] = destroynotify,
345 [EnterNotify] = enternotify,
346 [LeaveNotify] = leavenotify,
347 [Expose] = expose,
348 [KeyPress] = keypress,
349 [MapRequest] = maprequest,
350 [PropertyNotify] = propertynotify,
351 [UnmapNotify] = unmapnotify
352 };
354 void
355 grabkeys()
356 {
357 static unsigned int len = sizeof(key) / sizeof(key[0]);
358 unsigned int i;
359 KeyCode code;
361 for(i = 0; i < len; i++) {
362 code = XKeysymToKeycode(dpy, key[i].keysym);
363 XGrabKey(dpy, code, key[i].mod, root, True,
364 GrabModeAsync, GrabModeAsync);
365 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
368 GrabModeAsync, GrabModeAsync);
369 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
370 GrabModeAsync, GrabModeAsync);
371 }
372 }