1 /* (C)opyright MMVI 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(sx + c->x) < SNAP)
53 else if(abs((sx + sw) - (c->x + c->w)) < SNAP)
54 c->x = sw - c->w - 2 * BORDERPX;
55 if(abs((sy + bh) - c->y) < SNAP)
57 else if(abs((sy + sh) - (c->y + c->h)) < SNAP)
58 c->y = sh - 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))) {
165 if(ev->value_mask & CWX)
167 if(ev->value_mask & CWY)
169 if(ev->value_mask & CWWidth)
171 if(ev->value_mask & CWHeight)
173 if(ev->value_mask & CWBorderWidth)
174 c->border = ev->border_width;
180 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
182 XConfigureWindow(dpy, c->win, newmask, &wc);
187 resize(c, False, TopLeft);
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);
208 destroynotify(XEvent *e) {
210 XDestroyWindowEvent *ev = &e->xdestroywindow;
212 if((c = getclient(ev->window)))
217 enternotify(XEvent *e) {
219 XCrossingEvent *ev = &e->xcrossing;
221 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
223 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
225 else if(ev->window == root) {
227 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
235 XExposeEvent *ev = &e->xexpose;
238 if(barwin == ev->window)
240 else if((c = getctitle(ev->window)))
246 keypress(XEvent *e) {
247 static unsigned int len = sizeof(key) / sizeof(key[0]);
250 XKeyEvent *ev = &e->xkey;
252 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
253 for(i = 0; i < len; i++) {
254 if(keysym == key[i].keysym
255 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
258 key[i].func(&key[i].arg);
265 leavenotify(XEvent *e) {
266 XCrossingEvent *ev = &e->xcrossing;
268 if((ev->window == root) && !ev->same_screen) {
275 mappingnotify(XEvent *e) {
276 XMappingEvent *ev = &e->xmapping;
278 XRefreshKeyboardMapping(ev);
279 if(ev->request == MappingKeyboard)
284 maprequest(XEvent *e) {
285 static XWindowAttributes wa;
286 XMapRequestEvent *ev = &e->xmaprequest;
288 if(!XGetWindowAttributes(dpy, ev->window, &wa))
290 if(wa.override_redirect) {
291 XSelectInput(dpy, ev->window,
292 (StructureNotifyMask | PropertyChangeMask));
295 if(!getclient(ev->window))
296 manage(ev->window, &wa);
300 propertynotify(XEvent *e) {
303 XPropertyEvent *ev = &e->xproperty;
305 if(ev->state == PropertyDelete)
307 if((c = getclient(ev->window))) {
308 if(ev->atom == wmatom[WMProtocols]) {
309 c->proto = getproto(c->win);
314 case XA_WM_TRANSIENT_FOR:
315 XGetTransientForHint(dpy, c->win, &trans);
316 if(!c->isfloat && (c->isfloat = (trans != 0)))
319 case XA_WM_NORMAL_HINTS:
323 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
332 unmapnotify(XEvent *e) {
334 XUnmapEvent *ev = &e->xunmap;
336 if((c = getclient(ev->window)))
342 void (*handler[LASTEvent]) (XEvent *) = {
343 [ButtonPress] = buttonpress,
344 [ConfigureRequest] = configurerequest,
345 [DestroyNotify] = destroynotify,
346 [EnterNotify] = enternotify,
347 [LeaveNotify] = leavenotify,
349 [KeyPress] = keypress,
350 [MappingNotify] = mappingnotify,
351 [MapRequest] = maprequest,
352 [PropertyNotify] = propertynotify,
353 [UnmapNotify] = unmapnotify
358 static unsigned int len = sizeof(key) / sizeof(key[0]);
362 XUngrabKey(dpy, AnyKey, AnyModifier, root);
363 for(i = 0; i < len; i++) {
364 code = XKeysymToKeycode(dpy, key[i].keysym);
365 XGrabKey(dpy, code, key[i].mod, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
368 GrabModeAsync, GrabModeAsync);
369 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
370 GrabModeAsync, GrabModeAsync);
371 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
372 GrabModeAsync, GrabModeAsync);
380 while(XPending(dpy)) {
381 XNextEvent(dpy, &ev);
383 (handler[ev.type])(&ev); /* call handler */