dwm-meillo
view event.c @ 249:5d7b7ff7a289
added dwm favicon
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Thu, 10 Aug 2006 18:44:19 +0200 |
parents | e5390f8e06b9 |
children | d1630548c3fe |
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 viewprev(&a);
118 break;
119 case Button5:
120 viewnext(&a);
121 break;
122 }
123 }
124 else if((c = getclient(ev->window))) {
125 higher(c);
126 focus(c);
127 switch(ev->button) {
128 default:
129 break;
130 case Button1:
131 if(!c->ismax && (arrange == dofloat || c->isfloat))
132 movemouse(c);
133 break;
134 case Button2:
135 zoom(NULL);
136 break;
137 case Button3:
138 if(!c->ismax && (arrange == dofloat || c->isfloat))
139 resizemouse(c);
140 break;
141 }
142 }
143 }
145 static void
146 configurerequest(XEvent *e)
147 {
148 Client *c;
149 XConfigureRequestEvent *ev = &e->xconfigurerequest;
150 XEvent synev;
151 XWindowChanges wc;
152 unsigned long newmask;
154 if((c = getclient(ev->window))) {
155 gravitate(c, True);
156 if(ev->value_mask & CWX)
157 c->x = ev->x;
158 if(ev->value_mask & CWY)
159 c->y = ev->y;
160 if(ev->value_mask & CWWidth)
161 c->w = ev->width;
162 if(ev->value_mask & CWHeight)
163 c->h = ev->height;
164 if(ev->value_mask & CWBorderWidth)
165 c->border = ev->border_width;
166 gravitate(c, False);
167 wc.x = c->x;
168 wc.y = c->y;
169 wc.width = c->w;
170 wc.height = c->h;
171 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
172 if(newmask)
173 XConfigureWindow(dpy, c->win, newmask, &wc);
174 else {
175 synev.type = ConfigureNotify;
176 synev.xconfigure.display = dpy;
177 synev.xconfigure.event = c->win;
178 synev.xconfigure.window = c->win;
179 synev.xconfigure.x = c->x;
180 synev.xconfigure.y = c->y;
181 synev.xconfigure.width = c->w;
182 synev.xconfigure.height = c->h;
183 synev.xconfigure.border_width = c->border;
184 synev.xconfigure.above = None;
185 /* Send synthetic ConfigureNotify */
186 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
187 }
188 XSync(dpy, False);
189 if(c->isfloat)
190 resize(c, False, TopLeft);
191 else
192 arrange(NULL);
193 }
194 else {
195 wc.x = ev->x;
196 wc.y = ev->y;
197 wc.width = ev->width;
198 wc.height = ev->height;
199 wc.border_width = ev->border_width;
200 wc.sibling = ev->above;
201 wc.stack_mode = ev->detail;
202 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
203 XSync(dpy, False);
204 }
205 }
207 static void
208 destroynotify(XEvent *e)
209 {
210 Client *c;
211 XDestroyWindowEvent *ev = &e->xdestroywindow;
213 if((c = getclient(ev->window)))
214 unmanage(c);
215 }
217 static void
218 enternotify(XEvent *e)
219 {
220 Client *c;
221 XCrossingEvent *ev = &e->xcrossing;
223 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
224 return;
226 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
227 focus(c);
228 else if(ev->window == root) {
229 issel = True;
230 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
231 drawall();
232 }
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 = False;
274 drawall();
275 }
276 }
278 static void
279 maprequest(XEvent *e)
280 {
281 static XWindowAttributes wa;
282 XMapRequestEvent *ev = &e->xmaprequest;
284 if(!XGetWindowAttributes(dpy, ev->window, &wa))
285 return;
287 if(wa.override_redirect) {
288 XSelectInput(dpy, ev->window,
289 (StructureNotifyMask | PropertyChangeMask));
290 return;
291 }
293 if(!getclient(ev->window))
294 manage(ev->window, &wa);
295 }
297 static void
298 propertynotify(XEvent *e)
299 {
300 Client *c;
301 Window trans;
302 XPropertyEvent *ev = &e->xproperty;
304 if(ev->state == PropertyDelete)
305 return; /* ignore */
307 if((c = getclient(ev->window))) {
308 if(ev->atom == wmatom[WMProtocols]) {
309 c->proto = getproto(c->win);
310 return;
311 }
312 switch (ev->atom) {
313 default: break;
314 case XA_WM_TRANSIENT_FOR:
315 XGetTransientForHint(dpy, c->win, &trans);
316 if(!c->isfloat && (c->isfloat = (trans != 0)))
317 arrange(NULL);
318 break;
319 case XA_WM_NORMAL_HINTS:
320 setsize(c);
321 break;
322 }
323 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
324 settitle(c);
325 drawtitle(c);
326 }
327 }
328 }
330 static void
331 unmapnotify(XEvent *e)
332 {
333 Client *c;
334 XUnmapEvent *ev = &e->xunmap;
336 if((c = getclient(ev->window)))
337 unmanage(c);
338 }
340 /* extern */
342 void (*handler[LASTEvent]) (XEvent *) = {
343 [ButtonPress] = buttonpress,
344 [ConfigureRequest] = configurerequest,
345 [DestroyNotify] = destroynotify,
346 [EnterNotify] = enternotify,
347 [LeaveNotify] = leavenotify,
348 [Expose] = expose,
349 [KeyPress] = keypress,
350 [MapRequest] = maprequest,
351 [PropertyNotify] = propertynotify,
352 [UnmapNotify] = unmapnotify
353 };
355 void
356 grabkeys()
357 {
358 static unsigned int len = sizeof(key) / sizeof(key[0]);
359 unsigned int i;
360 KeyCode code;
362 for(i = 0; i < len; i++) {
363 code = XKeysymToKeycode(dpy, key[i].keysym);
364 XGrabKey(dpy, code, key[i].mod, root, True,
365 GrabModeAsync, GrabModeAsync);
366 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
367 GrabModeAsync, GrabModeAsync);
368 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
369 GrabModeAsync, GrabModeAsync);
370 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
371 GrabModeAsync, GrabModeAsync);
372 }
373 }