dwm-meillo
view event.c @ 267:8a8ea74e1b87
removed viewnext/viewprev
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Mon, 14 Aug 2006 08:38:26 +0200 |
parents | 118d3e010e5e |
children | a47b3b0d7bf4 |
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 x = 0;
106 for(a.i = 0; a.i < ntags; a.i++) {
107 x += textw(tags[a.i]);
108 if(ev->x < x) {
109 if(ev->button == Button3)
110 toggleview(&a);
111 else
112 view(&a);
113 return;
114 }
115 }
116 }
117 else if((c = getclient(ev->window))) {
118 higher(c);
119 focus(c);
120 switch(ev->button) {
121 default:
122 break;
123 case Button1:
124 if(!c->ismax && (arrange == dofloat || c->isfloat))
125 movemouse(c);
126 break;
127 case Button2:
128 zoom(NULL);
129 break;
130 case Button3:
131 if(!c->ismax && (arrange == dofloat || c->isfloat))
132 resizemouse(c);
133 break;
134 }
135 }
136 }
138 static void
139 configurerequest(XEvent *e)
140 {
141 Client *c;
142 XConfigureRequestEvent *ev = &e->xconfigurerequest;
143 XEvent synev;
144 XWindowChanges wc;
145 unsigned long newmask;
147 if((c = getclient(ev->window))) {
148 gravitate(c, True);
149 if(ev->value_mask & CWX)
150 c->x = ev->x;
151 if(ev->value_mask & CWY)
152 c->y = ev->y;
153 if(ev->value_mask & CWWidth)
154 c->w = ev->width;
155 if(ev->value_mask & CWHeight)
156 c->h = ev->height;
157 if(ev->value_mask & CWBorderWidth)
158 c->border = ev->border_width;
159 gravitate(c, False);
160 wc.x = c->x;
161 wc.y = c->y;
162 wc.width = c->w;
163 wc.height = c->h;
164 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
165 if(newmask)
166 XConfigureWindow(dpy, c->win, newmask, &wc);
167 else {
168 synev.type = ConfigureNotify;
169 synev.xconfigure.display = dpy;
170 synev.xconfigure.event = c->win;
171 synev.xconfigure.window = c->win;
172 synev.xconfigure.x = c->x;
173 synev.xconfigure.y = c->y;
174 synev.xconfigure.width = c->w;
175 synev.xconfigure.height = c->h;
176 synev.xconfigure.border_width = c->border;
177 synev.xconfigure.above = None;
178 /* Send synthetic ConfigureNotify */
179 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
180 }
181 XSync(dpy, False);
182 if(c->isfloat)
183 resize(c, False, TopLeft);
184 else
185 arrange(NULL);
186 }
187 else {
188 wc.x = ev->x;
189 wc.y = ev->y;
190 wc.width = ev->width;
191 wc.height = ev->height;
192 wc.border_width = ev->border_width;
193 wc.sibling = ev->above;
194 wc.stack_mode = ev->detail;
195 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
196 XSync(dpy, False);
197 }
198 }
200 static void
201 destroynotify(XEvent *e)
202 {
203 Client *c;
204 XDestroyWindowEvent *ev = &e->xdestroywindow;
206 if((c = getclient(ev->window)))
207 unmanage(c);
208 }
210 static void
211 enternotify(XEvent *e)
212 {
213 Client *c;
214 XCrossingEvent *ev = &e->xcrossing;
216 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
217 return;
219 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
220 focus(c);
221 else if(ev->window == root) {
222 issel = True;
223 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
224 drawall();
225 }
226 }
228 static void
229 expose(XEvent *e)
230 {
231 Client *c;
232 XExposeEvent *ev = &e->xexpose;
234 if(ev->count == 0) {
235 if(barwin == ev->window)
236 drawstatus();
237 else if((c = getctitle(ev->window)))
238 drawtitle(c);
239 }
240 }
242 static void
243 keypress(XEvent *e)
244 {
245 static unsigned int len = sizeof(key) / sizeof(key[0]);
246 unsigned int i;
247 KeySym keysym;
248 XKeyEvent *ev = &e->xkey;
250 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
251 for(i = 0; i < len; i++)
252 if(keysym == key[i].keysym &&
253 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
254 if(key[i].func)
255 key[i].func(&key[i].arg);
256 return;
257 }
258 }
260 static void
261 leavenotify(XEvent *e)
262 {
263 XCrossingEvent *ev = &e->xcrossing;
265 if((ev->window == root) && !ev->same_screen) {
266 issel = False;
267 drawall();
268 }
269 }
271 static void
272 maprequest(XEvent *e)
273 {
274 static XWindowAttributes wa;
275 XMapRequestEvent *ev = &e->xmaprequest;
277 if(!XGetWindowAttributes(dpy, ev->window, &wa))
278 return;
280 if(wa.override_redirect) {
281 XSelectInput(dpy, ev->window,
282 (StructureNotifyMask | PropertyChangeMask));
283 return;
284 }
286 if(!getclient(ev->window))
287 manage(ev->window, &wa);
288 }
290 static void
291 propertynotify(XEvent *e)
292 {
293 Client *c;
294 Window trans;
295 XPropertyEvent *ev = &e->xproperty;
297 if(ev->state == PropertyDelete)
298 return; /* ignore */
300 if((c = getclient(ev->window))) {
301 if(ev->atom == wmatom[WMProtocols]) {
302 c->proto = getproto(c->win);
303 return;
304 }
305 switch (ev->atom) {
306 default: break;
307 case XA_WM_TRANSIENT_FOR:
308 XGetTransientForHint(dpy, c->win, &trans);
309 if(!c->isfloat && (c->isfloat = (trans != 0)))
310 arrange(NULL);
311 break;
312 case XA_WM_NORMAL_HINTS:
313 setsize(c);
314 break;
315 }
316 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
317 settitle(c);
318 drawtitle(c);
319 }
320 }
321 }
323 static void
324 unmapnotify(XEvent *e)
325 {
326 Client *c;
327 XUnmapEvent *ev = &e->xunmap;
329 if((c = getclient(ev->window)))
330 unmanage(c);
331 }
333 /* extern */
335 void (*handler[LASTEvent]) (XEvent *) = {
336 [ButtonPress] = buttonpress,
337 [ConfigureRequest] = configurerequest,
338 [DestroyNotify] = destroynotify,
339 [EnterNotify] = enternotify,
340 [LeaveNotify] = leavenotify,
341 [Expose] = expose,
342 [KeyPress] = keypress,
343 [MapRequest] = maprequest,
344 [PropertyNotify] = propertynotify,
345 [UnmapNotify] = unmapnotify
346 };
348 void
349 grabkeys()
350 {
351 static unsigned int len = sizeof(key) / sizeof(key[0]);
352 unsigned int i;
353 KeyCode code;
355 for(i = 0; i < len; i++) {
356 code = XKeysymToKeycode(dpy, key[i].keysym);
357 XGrabKey(dpy, code, key[i].mod, root, True,
358 GrabModeAsync, GrabModeAsync);
359 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
360 GrabModeAsync, GrabModeAsync);
361 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
362 GrabModeAsync, GrabModeAsync);
363 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
364 GrabModeAsync, GrabModeAsync);
365 }
366 }