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 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 resize(c, False, TopLeft);
58 resizemouse(Client *c) {
66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
70 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
72 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
75 resize(c, True, TopLeft);
76 XUngrabPointer(dpy, CurrentTime);
83 if((nw = abs(ocx - ev.xmotion.x)))
85 if((nh = abs(ocy - ev.xmotion.y)))
87 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
88 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
89 if(ocx <= ev.xmotion.x)
90 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
92 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
93 resize(c, True, sticky);
100 buttonpress(XEvent *e) {
104 XButtonPressedEvent *ev = &e->xbutton;
106 if(barwin == ev->window) {
108 for(a.i = 0; a.i < ntags; a.i++) {
109 x += textw(tags[a.i]);
111 if(ev->button == Button1) {
112 if(ev->state & MODKEY)
117 else if(ev->button == Button3) {
118 if(ev->state & MODKEY)
126 if((ev->x < x + bmw) && (ev->button == Button1))
129 else if((c = getclient(ev->window))) {
131 if(CLEANMASK(ev->state) != MODKEY)
133 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
137 else if(ev->button == Button2)
139 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
147 configurerequest(XEvent *e) {
148 unsigned long newmask;
150 XConfigureRequestEvent *ev = &e->xconfigurerequest;
153 if((c = getclient(ev->window))) {
156 if(ev->value_mask & CWX)
158 if(ev->value_mask & CWY)
160 if(ev->value_mask & CWWidth)
162 if(ev->value_mask & CWHeight)
164 if(ev->value_mask & CWBorderWidth)
165 c->border = ev->border_width;
171 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
173 XConfigureWindow(dpy, c->win, newmask, &wc);
178 resize(c, False, TopLeft);
188 wc.width = ev->width;
189 wc.height = ev->height;
190 wc.border_width = ev->border_width;
191 wc.sibling = ev->above;
192 wc.stack_mode = ev->detail;
193 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
199 destroynotify(XEvent *e) {
201 XDestroyWindowEvent *ev = &e->xdestroywindow;
203 if((c = getclient(ev->window)))
208 enternotify(XEvent *e) {
210 XCrossingEvent *ev = &e->xcrossing;
212 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
215 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
217 else if(ev->window == root) {
219 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
227 XExposeEvent *ev = &e->xexpose;
230 if(barwin == ev->window)
232 else if((c = getctitle(ev->window)))
238 keypress(XEvent *e) {
239 static unsigned int len = sizeof(key) / sizeof(key[0]);
242 XKeyEvent *ev = &e->xkey;
244 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
245 for(i = 0; i < len; i++) {
246 if(keysym == key[i].keysym
247 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
250 key[i].func(&key[i].arg);
257 leavenotify(XEvent *e) {
258 XCrossingEvent *ev = &e->xcrossing;
260 if((ev->window == root) && !ev->same_screen) {
267 mappingnotify(XEvent *e) {
268 XMappingEvent *ev = &e->xmapping;
270 XRefreshKeyboardMapping(ev);
271 if(ev->request == MappingKeyboard)
276 maprequest(XEvent *e) {
277 static XWindowAttributes wa;
278 XMapRequestEvent *ev = &e->xmaprequest;
280 if(!XGetWindowAttributes(dpy, ev->window, &wa))
283 if(wa.override_redirect) {
284 XSelectInput(dpy, ev->window,
285 (StructureNotifyMask | PropertyChangeMask));
289 if(!getclient(ev->window))
290 manage(ev->window, &wa);
294 propertynotify(XEvent *e) {
297 XPropertyEvent *ev = &e->xproperty;
299 if(ev->state == PropertyDelete)
302 if((c = getclient(ev->window))) {
303 if(ev->atom == wmatom[WMProtocols]) {
304 c->proto = getproto(c->win);
309 case XA_WM_TRANSIENT_FOR:
310 XGetTransientForHint(dpy, c->win, &trans);
311 if(!c->isfloat && (c->isfloat = (trans != 0)))
314 case XA_WM_NORMAL_HINTS:
318 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
327 unmapnotify(XEvent *e) {
329 XUnmapEvent *ev = &e->xunmap;
331 if((c = getclient(ev->window)))
337 void (*handler[LASTEvent]) (XEvent *) = {
338 [ButtonPress] = buttonpress,
339 [ConfigureRequest] = configurerequest,
340 [DestroyNotify] = destroynotify,
341 [EnterNotify] = enternotify,
342 [LeaveNotify] = leavenotify,
344 [KeyPress] = keypress,
345 [MappingNotify] = mappingnotify,
346 [MapRequest] = maprequest,
347 [PropertyNotify] = propertynotify,
348 [UnmapNotify] = unmapnotify
353 static unsigned int len = sizeof(key) / sizeof(key[0]);
357 XUngrabKey(dpy, AnyKey, AnyModifier, root);
358 for(i = 0; i < len; i++) {
359 code = XKeysymToKeycode(dpy, key[i].keysym);
360 XGrabKey(dpy, code, key[i].mod, root, True,
361 GrabModeAsync, GrabModeAsync);
362 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
363 GrabModeAsync, GrabModeAsync);
364 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
365 GrabModeAsync, GrabModeAsync);
366 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
367 GrabModeAsync, GrabModeAsync);
375 while(XPending(dpy)) {
376 XNextEvent(dpy, &ev);
378 (handler[ev.type])(&ev); /* call handler */