3 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
4 * See LICENSE file for license details.
8 #include <X11/keysym.h>
16 void (*func)(Arg *arg);
22 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
25 movemouse(Client *c) {
26 int x1, y1, ocx, ocy, di;
33 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
34 None, cursor[CurMove], CurrentTime) != GrabSuccess)
37 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
39 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
42 resize(c, True, TopLeft);
43 XUngrabPointer(dpy, CurrentTime);
50 c->x = ocx + (ev.xmotion.x - x1);
51 c->y = ocy + (ev.xmotion.y - y1);
52 resize(c, False, TopLeft);
59 resizemouse(Client *c) {
67 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
68 None, cursor[CurResize], CurrentTime) != GrabSuccess)
71 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
73 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
76 resize(c, True, TopLeft);
77 XUngrabPointer(dpy, CurrentTime);
84 if((nw = abs(ocx - ev.xmotion.x)))
86 if((nh = abs(ocy - ev.xmotion.y)))
88 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
89 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
90 if(ocx <= ev.xmotion.x)
91 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
93 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
94 resize(c, True, sticky);
101 buttonpress(XEvent *e) {
105 XButtonPressedEvent *ev = &e->xbutton;
107 if(barwin == ev->window) {
109 for(a.i = 0; a.i < ntags; a.i++) {
110 x += textw(tags[a.i]);
112 if(ev->button == Button1) {
113 if(ev->state & MODKEY)
118 else if(ev->button == Button3) {
119 if(ev->state & MODKEY)
127 if(ev->x < x + bmw) {
128 if(ev->button == Button1)
132 else if((c = getclient(ev->window))) {
134 if(CLEANMASK(ev->state) != MODKEY)
136 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
140 else if(ev->button == Button2)
142 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
150 configurerequest(XEvent *e) {
151 unsigned long newmask;
153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
157 fputs("configurerequest\n", stderr);
158 if((c = getclient(ev->window))) {
161 if(ev->value_mask & CWX)
163 if(ev->value_mask & CWY)
165 if(ev->value_mask & CWWidth)
167 if(ev->value_mask & CWHeight)
169 if(ev->value_mask & CWBorderWidth)
170 c->border = ev->border_width;
176 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
178 XConfigureWindow(dpy, c->win, newmask, &wc);
183 resize(c, False, TopLeft);
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);
201 destroynotify(XEvent *e) {
203 XDestroyWindowEvent *ev = &e->xdestroywindow;
205 if((c = getclient(ev->window)))
210 enternotify(XEvent *e) {
212 XCrossingEvent *ev = &e->xcrossing;
214 fputs("enternotify\n", stderr);
215 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
218 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
220 else if(ev->window == root) {
222 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
230 XExposeEvent *ev = &e->xexpose;
233 if(barwin == ev->window)
235 else if((c = getctitle(ev->window)))
241 keypress(XEvent *e) {
242 static unsigned int len = sizeof(key) / sizeof(key[0]);
245 XKeyEvent *ev = &e->xkey;
247 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
248 for(i = 0; i < len; i++) {
249 if(keysym == key[i].keysym
250 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
253 key[i].func(&key[i].arg);
260 leavenotify(XEvent *e) {
261 XCrossingEvent *ev = &e->xcrossing;
263 if((ev->window == root) && !ev->same_screen) {
270 mappingnotify(XEvent *e) {
271 XMappingEvent *ev = &e->xmapping;
273 XRefreshKeyboardMapping(ev);
274 if(ev->request == MappingKeyboard)
279 maprequest(XEvent *e) {
280 static XWindowAttributes wa;
281 XMapRequestEvent *ev = &e->xmaprequest;
283 if(!XGetWindowAttributes(dpy, ev->window, &wa))
286 if(wa.override_redirect) {
287 XSelectInput(dpy, ev->window,
288 (StructureNotifyMask | PropertyChangeMask));
292 if(!getclient(ev->window))
293 manage(ev->window, &wa);
297 propertynotify(XEvent *e) {
300 XPropertyEvent *ev = &e->xproperty;
302 fputs("propertynotify\n", stderr);
303 if(ev->state == PropertyDelete)
306 if((c = getclient(ev->window))) {
307 if(ev->atom == wmatom[WMProtocols]) {
308 c->proto = getproto(c->win);
313 case XA_WM_TRANSIENT_FOR:
314 XGetTransientForHint(dpy, c->win, &trans);
315 if(!c->isfloat && (c->isfloat = (trans != 0)))
318 case XA_WM_NORMAL_HINTS:
322 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
330 unmapnotify(XEvent *e) {
332 XUnmapEvent *ev = &e->xunmap;
334 if((c = getclient(ev->window)))
340 void (*handler[LASTEvent]) (XEvent *) = {
341 [ButtonPress] = buttonpress,
342 [ConfigureRequest] = configurerequest,
343 [DestroyNotify] = destroynotify,
344 [EnterNotify] = enternotify,
345 [LeaveNotify] = leavenotify,
347 [KeyPress] = keypress,
348 [MappingNotify] = mappingnotify,
349 [MapRequest] = maprequest,
350 [PropertyNotify] = propertynotify,
351 [UnmapNotify] = unmapnotify
356 static unsigned int len = sizeof(key) / sizeof(key[0]);
360 XUngrabKey(dpy, AnyKey, AnyModifier, root);
361 for(i = 0; i < len; i++) {
362 code = XKeysymToKeycode(dpy, key[i].keysym);
363 XGrabKey(dpy, code, key[i].mod, root, True,
364 GrabModeAsync, GrabModeAsync);
365 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
368 GrabModeAsync, GrabModeAsync);
369 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
370 GrabModeAsync, GrabModeAsync);
378 while(XPending(dpy)) {
379 XNextEvent(dpy, &ev);
381 (handler[ev.type])(&ev); /* call handler */