2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
7 #include <X11/keysym.h>
15 void (*func)(Arg *arg);
21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
27 synev.type = ConfigureNotify;
28 synev.xconfigure.display = dpy;
29 synev.xconfigure.event = c->win;
30 synev.xconfigure.window = c->win;
31 synev.xconfigure.x = x;
32 synev.xconfigure.y = y;
33 synev.xconfigure.width = w;
34 synev.xconfigure.height = h;
35 synev.xconfigure.border_width = border;
36 synev.xconfigure.above = None;
37 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
41 movemouse(Client *c) {
42 int x1, y1, ocx, ocy, di;
49 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
50 None, cursor[CurMove], CurrentTime) != GrabSuccess)
53 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
55 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
59 case ConfigureRequest:
60 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
68 c->x = ocx + (ev.xmotion.x - x1);
69 c->y = ocy + (ev.xmotion.y - y1);
70 resize(c, False, TopLeft);
73 XUngrabPointer(dpy, CurrentTime);
77 XUngrabPointer(dpy, CurrentTime);
78 handler[ev.type](&ev);
85 resizemouse(Client *c) {
93 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
94 None, cursor[CurResize], CurrentTime) != GrabSuccess)
97 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
99 XMaskEvent(dpy, MOUSEMASK | ExposureMask | StructureNotifyMask, &ev);
103 case ConfigureRequest:
104 synconfig(c, c->x, c->y, c->w, c->h, ev.xconfigure.border_width);
108 handler[Expose](&ev);
112 if((nw = abs(ocx - ev.xmotion.x)))
114 if((nh = abs(ocy - ev.xmotion.y)))
116 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
117 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
118 if(ocx <= ev.xmotion.x)
119 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
121 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
122 resize(c, True, sticky);
125 XUngrabPointer(dpy, CurrentTime);
129 XUngrabPointer(dpy, CurrentTime);
130 handler[ev.type](&ev);
137 buttonpress(XEvent *e) {
141 XButtonPressedEvent *ev = &e->xbutton;
143 if(barwin == ev->window) {
145 for(a.i = 0; a.i < ntags; a.i++) {
146 x += textw(tags[a.i]);
148 if(ev->button == Button1) {
149 if(ev->state & MODKEY)
154 else if(ev->button == Button3) {
155 if(ev->state & MODKEY)
163 if(ev->x < x + bmw) {
164 if(ev->button == Button1)
168 else if((c = getclient(ev->window))) {
170 if(CLEANMASK(ev->state) != MODKEY)
172 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
176 else if(ev->button == Button2)
178 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
186 configurerequest(XEvent *e) {
187 unsigned long newmask;
189 XConfigureRequestEvent *ev = &e->xconfigurerequest;
192 if((c = getclient(ev->window))) {
193 if((c == sel) && !c->isfloat && (arrange != dofloat)) {
194 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
199 if(ev->value_mask & CWX)
201 if(ev->value_mask & CWY)
203 if(ev->value_mask & CWWidth)
205 if(ev->value_mask & CWHeight)
207 if(ev->value_mask & CWBorderWidth)
208 c->border = ev->border_width;
214 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
216 XConfigureWindow(dpy, c->win, newmask, &wc);
218 synconfig(c, c->x, c->y, c->w, c->h, c->border);
221 resize(c, False, TopLeft);
228 wc.width = ev->width;
229 wc.height = ev->height;
230 wc.border_width = ev->border_width;
231 wc.sibling = ev->above;
232 wc.stack_mode = ev->detail;
233 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
239 destroynotify(XEvent *e) {
241 XDestroyWindowEvent *ev = &e->xdestroywindow;
243 if((c = getclient(ev->window)))
248 enternotify(XEvent *e) {
250 XCrossingEvent *ev = &e->xcrossing;
252 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
255 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
257 else if(ev->window == root) {
259 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
267 XExposeEvent *ev = &e->xexpose;
270 if(barwin == ev->window)
272 else if((c = getctitle(ev->window)))
278 keypress(XEvent *e) {
279 static unsigned int len = sizeof(key) / sizeof(key[0]);
282 XKeyEvent *ev = &e->xkey;
284 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
285 for(i = 0; i < len; i++) {
286 if(keysym == key[i].keysym
287 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
290 key[i].func(&key[i].arg);
297 leavenotify(XEvent *e) {
298 XCrossingEvent *ev = &e->xcrossing;
300 if((ev->window == root) && !ev->same_screen) {
307 mappingnotify(XEvent *e) {
308 XMappingEvent *ev = &e->xmapping;
310 XRefreshKeyboardMapping(ev);
311 if(ev->request == MappingKeyboard)
316 maprequest(XEvent *e) {
317 static XWindowAttributes wa;
318 XMapRequestEvent *ev = &e->xmaprequest;
320 if(!XGetWindowAttributes(dpy, ev->window, &wa))
323 if(wa.override_redirect) {
324 XSelectInput(dpy, ev->window,
325 (StructureNotifyMask | PropertyChangeMask));
329 if(!getclient(ev->window))
330 manage(ev->window, &wa);
334 propertynotify(XEvent *e) {
337 XPropertyEvent *ev = &e->xproperty;
339 if(ev->state == PropertyDelete)
342 if((c = getclient(ev->window))) {
343 if(ev->atom == wmatom[WMProtocols]) {
344 c->proto = getproto(c->win);
349 case XA_WM_TRANSIENT_FOR:
350 XGetTransientForHint(dpy, c->win, &trans);
351 if(!c->isfloat && (c->isfloat = (trans != 0)))
354 case XA_WM_NORMAL_HINTS:
358 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
366 unmapnotify(XEvent *e) {
368 XUnmapEvent *ev = &e->xunmap;
370 if((c = getclient(ev->window)))
376 void (*handler[LASTEvent]) (XEvent *) = {
377 [ButtonPress] = buttonpress,
378 [ConfigureRequest] = configurerequest,
379 [DestroyNotify] = destroynotify,
380 [EnterNotify] = enternotify,
381 [LeaveNotify] = leavenotify,
383 [KeyPress] = keypress,
384 [MappingNotify] = mappingnotify,
385 [MapRequest] = maprequest,
386 [PropertyNotify] = propertynotify,
387 [UnmapNotify] = unmapnotify
392 static unsigned int len = sizeof(key) / sizeof(key[0]);
396 XUngrabKey(dpy, AnyKey, AnyModifier, root);
397 for(i = 0; i < len; i++) {
398 code = XKeysymToKeycode(dpy, key[i].keysym);
399 XGrabKey(dpy, code, key[i].mod, root, True,
400 GrabModeAsync, GrabModeAsync);
401 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
402 GrabModeAsync, GrabModeAsync);
403 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
404 GrabModeAsync, GrabModeAsync);
405 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
406 GrabModeAsync, GrabModeAsync);
414 while(XPending(dpy)) {
415 XNextEvent(dpy, &ev);
417 (handler[ev.type])(&ev); /* call handler */