1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
6 #include <X11/keysym.h>
14 void (*func)(Arg *arg);
20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
24 movemouse(Client *c) {
25 int x1, y1, ocx, ocy, di;
32 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
33 None, cursor[CurMove], CurrentTime) != GrabSuccess)
36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
38 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
42 XUngrabPointer(dpy, CurrentTime);
44 case ConfigureRequest:
47 handler[ev.type](&ev);
51 c->x = ocx + (ev.xmotion.x - x1);
52 c->y = ocy + (ev.xmotion.y - y1);
53 if(abs(wax + c->x) < SNAP)
55 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
56 c->x = wax + waw - c->w - 2 * c->border;
57 if(abs(way - c->y) < SNAP)
59 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
60 c->y = way + wah - c->h - 2 * c->border;
68 resizemouse(Client *c) {
75 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
76 None, cursor[CurResize], CurrentTime) != GrabSuccess)
79 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
81 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
85 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
86 c->w + c->border - 1, c->h + c->border - 1);
87 XUngrabPointer(dpy, CurrentTime);
88 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
90 case ConfigureRequest:
93 handler[ev.type](&ev);
97 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
98 c->w = nw > 0 ? nw : 1;
99 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
100 c->h = nh > 0 ? nh : 1;
108 buttonpress(XEvent *e) {
112 XButtonPressedEvent *ev = &e->xbutton;
114 if(barwin == ev->window) {
116 for(a.i = 0; a.i < ntags; a.i++) {
117 x += textw(tags[a.i]);
119 if(ev->button == Button1) {
120 if(ev->state & MODKEY)
125 else if(ev->button == Button3) {
126 if(ev->state & MODKEY)
149 else if((c = getclient(ev->window))) {
151 if(CLEANMASK(ev->state) != MODKEY)
153 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
157 else if(ev->button == Button2)
159 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)
169 configurerequest(XEvent *e) {
171 XConfigureRequestEvent *ev = &e->xconfigurerequest;
174 if((c = getclient(ev->window))) {
176 if(ev->value_mask & CWBorderWidth)
177 c->border = ev->border_width;
178 if(c->isfixed || c->isfloat || (arrange == dofloat)) {
179 if(ev->value_mask & CWX)
181 if(ev->value_mask & CWY)
183 if(ev->value_mask & CWWidth)
185 if(ev->value_mask & CWHeight)
187 if((ev->value_mask & (CWX | CWY)) && !(ev->value_mask & (CWWidth | CWHeight)))
191 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
199 wc.width = ev->width;
200 wc.height = ev->height;
201 wc.border_width = ev->border_width;
202 wc.sibling = ev->above;
203 wc.stack_mode = ev->detail;
204 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
210 destroynotify(XEvent *e) {
212 XDestroyWindowEvent *ev = &e->xdestroywindow;
214 if((c = getclient(ev->window)))
219 enternotify(XEvent *e) {
221 XCrossingEvent *ev = &e->xcrossing;
223 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
225 if((c = getclient(ev->window)) && isvisible(c))
227 else if(ev->window == root) {
229 for(c = stack; c && !isvisible(c); c = c->snext);
236 XExposeEvent *ev = &e->xexpose;
239 if(barwin == ev->window)
245 keypress(XEvent *e) {
246 static unsigned int len = sizeof key / sizeof key[0];
249 XKeyEvent *ev = &e->xkey;
251 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
252 for(i = 0; i < len; i++) {
253 if(keysym == key[i].keysym
254 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
257 key[i].func(&key[i].arg);
263 leavenotify(XEvent *e) {
264 XCrossingEvent *ev = &e->xcrossing;
266 if((ev->window == root) && !ev->same_screen) {
273 mappingnotify(XEvent *e) {
274 XMappingEvent *ev = &e->xmapping;
276 XRefreshKeyboardMapping(ev);
277 if(ev->request == MappingKeyboard)
282 maprequest(XEvent *e) {
283 static XWindowAttributes wa;
284 XMapRequestEvent *ev = &e->xmaprequest;
286 if(!XGetWindowAttributes(dpy, ev->window, &wa))
288 if(wa.override_redirect)
290 if(!getclient(ev->window))
291 manage(ev->window, &wa);
295 propertynotify(XEvent *e) {
298 XPropertyEvent *ev = &e->xproperty;
300 if(ev->state == PropertyDelete)
302 if((c = getclient(ev->window))) {
305 case XA_WM_TRANSIENT_FOR:
306 XGetTransientForHint(dpy, c->win, &trans);
307 if(!c->isfloat && (c->isfloat = (trans != 0)))
310 case XA_WM_NORMAL_HINTS:
314 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
323 unmapnotify(XEvent *e) {
325 XUnmapEvent *ev = &e->xunmap;
327 if((c = getclient(ev->window)))
333 void (*handler[LASTEvent]) (XEvent *) = {
334 [ButtonPress] = buttonpress,
335 [ConfigureRequest] = configurerequest,
336 [DestroyNotify] = destroynotify,
337 [EnterNotify] = enternotify,
338 [LeaveNotify] = leavenotify,
340 [KeyPress] = keypress,
341 [MappingNotify] = mappingnotify,
342 [MapRequest] = maprequest,
343 [PropertyNotify] = propertynotify,
344 [UnmapNotify] = unmapnotify
349 static unsigned int len = sizeof key / sizeof key[0];
353 XUngrabKey(dpy, AnyKey, AnyModifier, root);
354 for(i = 0; i < len; i++) {
355 code = XKeysymToKeycode(dpy, key[i].keysym);
356 XGrabKey(dpy, code, key[i].mod, root, True,
357 GrabModeAsync, GrabModeAsync);
358 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
359 GrabModeAsync, GrabModeAsync);
360 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
361 GrabModeAsync, GrabModeAsync);
362 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
363 GrabModeAsync, GrabModeAsync);
371 while(XPending(dpy)) {
372 XNextEvent(dpy, &ev);
374 (handler[ev.type])(&ev); /* call handler */