aewl

view event.c @ 225:40432020c0e8

without borders it looks cleaner
author Anselm R.Garbe <arg@10ksloc.org>
date Tue, 08 Aug 2006 11:47:29 +0200
parents d560e796f7fe
children f4f5d000ce7a
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 if(c->isfloat)
193 resize(c, False, TopLeft);
194 else
195 arrange(NULL);
196 }
197 else {
198 wc.x = ev->x;
199 wc.y = ev->y;
200 wc.width = ev->width;
201 wc.height = ev->height;
202 wc.border_width = ev->border_width;
203 wc.sibling = ev->above;
204 wc.stack_mode = ev->detail;
205 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
206 XSync(dpy, False);
207 }
208 }
210 static void
211 destroynotify(XEvent *e)
212 {
213 Client *c;
214 XDestroyWindowEvent *ev = &e->xdestroywindow;
216 if((c = getclient(ev->window)))
217 unmanage(c);
218 }
220 static void
221 enternotify(XEvent *e)
222 {
223 Client *c;
224 XCrossingEvent *ev = &e->xcrossing;
226 if(ev->detail == NotifyInferior)
227 return;
229 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
230 focus(c);
231 else if(ev->window == root)
232 issel = True;
233 }
235 static void
236 expose(XEvent *e)
237 {
238 Client *c;
239 XExposeEvent *ev = &e->xexpose;
241 if(ev->count == 0) {
242 if(barwin == ev->window)
243 drawstatus();
244 else if((c = getctitle(ev->window)))
245 drawtitle(c);
246 }
247 }
249 static void
250 keypress(XEvent *e)
251 {
252 static unsigned int len = sizeof(key) / sizeof(key[0]);
253 unsigned int i;
254 KeySym keysym;
255 XKeyEvent *ev = &e->xkey;
257 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
258 for(i = 0; i < len; i++)
259 if(keysym == key[i].keysym &&
260 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
261 if(key[i].func)
262 key[i].func(&key[i].arg);
263 return;
264 }
265 }
267 static void
268 leavenotify(XEvent *e)
269 {
270 XCrossingEvent *ev = &e->xcrossing;
272 if((ev->window == root) && !ev->same_screen)
273 issel = True;
274 }
276 static void
277 maprequest(XEvent *e)
278 {
279 static XWindowAttributes wa;
280 XMapRequestEvent *ev = &e->xmaprequest;
282 if(!XGetWindowAttributes(dpy, ev->window, &wa))
283 return;
285 if(wa.override_redirect) {
286 XSelectInput(dpy, ev->window,
287 (StructureNotifyMask | PropertyChangeMask));
288 return;
289 }
291 if(!getclient(ev->window))
292 manage(ev->window, &wa);
293 }
295 static void
296 propertynotify(XEvent *e)
297 {
298 Client *c;
299 Window trans;
300 XPropertyEvent *ev = &e->xproperty;
302 if(ev->state == PropertyDelete)
303 return; /* ignore */
305 if((c = getclient(ev->window))) {
306 if(ev->atom == wmatom[WMProtocols]) {
307 c->proto = getproto(c->win);
308 return;
309 }
310 switch (ev->atom) {
311 default: break;
312 case XA_WM_TRANSIENT_FOR:
313 XGetTransientForHint(dpy, c->win, &trans);
314 if(!c->isfloat && (c->isfloat = (trans != 0)))
315 arrange(NULL);
316 break;
317 case XA_WM_NORMAL_HINTS:
318 setsize(c);
319 break;
320 }
321 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
322 settitle(c);
323 drawtitle(c);
324 }
325 }
326 }
328 static void
329 unmapnotify(XEvent *e)
330 {
331 Client *c;
332 XUnmapEvent *ev = &e->xunmap;
334 if((c = getclient(ev->window)))
335 unmanage(c);
336 }
338 /* extern */
340 void (*handler[LASTEvent]) (XEvent *) = {
341 [ButtonPress] = buttonpress,
342 [ConfigureRequest] = configurerequest,
343 [DestroyNotify] = destroynotify,
344 [EnterNotify] = enternotify,
345 [LeaveNotify] = leavenotify,
346 [Expose] = expose,
347 [KeyPress] = keypress,
348 [MapRequest] = maprequest,
349 [PropertyNotify] = propertynotify,
350 [UnmapNotify] = unmapnotify
351 };
353 void
354 grabkeys()
355 {
356 static unsigned int len = sizeof(key) / sizeof(key[0]);
357 unsigned int i;
358 KeyCode code;
360 for(i = 0; i < len; i++) {
361 code = XKeysymToKeycode(dpy, key[i].keysym);
362 XGrabKey(dpy, code, key[i].mod, root, True,
363 GrabModeAsync, GrabModeAsync);
364 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
365 GrabModeAsync, GrabModeAsync);
366 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
367 GrabModeAsync, GrabModeAsync);
368 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
369 GrabModeAsync, GrabModeAsync);
370 }
371 }