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, &ev);
41 resize(c, True, TopLeft);
42 XUngrabPointer(dpy, CurrentTime);
49 c->x = ocx + (ev.xmotion.x - x1);
50 c->y = ocy + (ev.xmotion.y - y1);
51 if(abs(wax + c->x) < SNAP)
53 else if(abs((wax + waw) - (c->x + c->w)) < SNAP)
54 c->x = wax + waw - c->w - 2 * BORDERPX;
55 if(abs(way - c->y) < SNAP)
57 else if(abs((way + wah) - (c->y + c->h)) < SNAP)
58 c->y = way + wah - c->h - 2 * BORDERPX;
59 resize(c, False, TopLeft);
66 resizemouse(Client *c) {
74 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
75 None, cursor[CurResize], CurrentTime) != GrabSuccess)
78 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
80 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
83 resize(c, True, TopLeft);
84 XUngrabPointer(dpy, CurrentTime);
91 if((nw = abs(ocx - ev.xmotion.x)))
93 if((nh = abs(ocy - ev.xmotion.y)))
95 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
96 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
97 if(ocx <= ev.xmotion.x)
98 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
100 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
101 resize(c, True, sticky);
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)
134 if((ev->x < x + bmw) && (ev->button == Button1))
137 else if((c = getclient(ev->window))) {
139 if(CLEANMASK(ev->state) != MODKEY)
141 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
145 else if(ev->button == Button2)
147 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
156 configurerequest(XEvent *e) {
157 unsigned long newmask;
159 XConfigureRequestEvent *ev = &e->xconfigurerequest;
162 if((c = getclient(ev->window))) {
164 if(ev->value_mask & CWX)
166 if(ev->value_mask & CWY)
168 if(ev->value_mask & CWWidth)
170 if(ev->value_mask & CWHeight)
172 if(ev->value_mask & CWBorderWidth)
173 c->border = ev->border_width;
178 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
180 XConfigureWindow(dpy, c->win, newmask, &wc);
185 resize(c, False, TopLeft);
195 wc.width = ev->width;
196 wc.height = ev->height;
197 wc.border_width = ev->border_width;
198 wc.sibling = ev->above;
199 wc.stack_mode = ev->detail;
200 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
206 destroynotify(XEvent *e) {
208 XDestroyWindowEvent *ev = &e->xdestroywindow;
210 if((c = getclient(ev->window)))
215 enternotify(XEvent *e) {
217 XCrossingEvent *ev = &e->xcrossing;
219 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
221 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
223 else if(ev->window == root) {
225 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
233 XExposeEvent *ev = &e->xexpose;
236 if(barwin == ev->window)
238 else if((c = getctitle(ev->window)))
244 keypress(XEvent *e) {
245 static unsigned int len = sizeof key / sizeof key[0];
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))
256 key[i].func(&key[i].arg);
262 leavenotify(XEvent *e) {
263 XCrossingEvent *ev = &e->xcrossing;
265 if((ev->window == root) && !ev->same_screen) {
272 mappingnotify(XEvent *e) {
273 XMappingEvent *ev = &e->xmapping;
275 XRefreshKeyboardMapping(ev);
276 if(ev->request == MappingKeyboard)
281 maprequest(XEvent *e) {
282 static XWindowAttributes wa;
283 XMapRequestEvent *ev = &e->xmaprequest;
285 if(!XGetWindowAttributes(dpy, ev->window, &wa))
287 if(wa.override_redirect) {
288 XSelectInput(dpy, ev->window,
289 (StructureNotifyMask | PropertyChangeMask));
292 if(!getclient(ev->window))
293 manage(ev->window, &wa);
297 propertynotify(XEvent *e) {
300 XPropertyEvent *ev = &e->xproperty;
302 if(ev->state == PropertyDelete)
304 if((c = getclient(ev->window))) {
305 if(ev->atom == wmatom[WMProtocols]) {
306 c->proto = getproto(c->win);
311 case XA_WM_TRANSIENT_FOR:
312 XGetTransientForHint(dpy, c->win, &trans);
313 if(!c->isfloat && (c->isfloat = (trans != 0)))
316 case XA_WM_NORMAL_HINTS:
320 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
329 unmapnotify(XEvent *e) {
331 XUnmapEvent *ev = &e->xunmap;
333 if((c = getclient(ev->window)))
339 void (*handler[LASTEvent]) (XEvent *) = {
340 [ButtonPress] = buttonpress,
341 [ConfigureRequest] = configurerequest,
342 [DestroyNotify] = destroynotify,
343 [EnterNotify] = enternotify,
344 [LeaveNotify] = leavenotify,
346 [KeyPress] = keypress,
347 [MappingNotify] = mappingnotify,
348 [MapRequest] = maprequest,
349 [PropertyNotify] = propertynotify,
350 [UnmapNotify] = unmapnotify
355 static unsigned int len = sizeof key / sizeof key[0];
359 XUngrabKey(dpy, AnyKey, AnyModifier, root);
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);
377 while(XPending(dpy)) {
378 XNextEvent(dpy, &ev);
380 (handler[ev.type])(&ev); /* call handler */