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) {
127 if(ev->button == Button1)
131 else if((c = getclient(ev->window))) {
133 if(CLEANMASK(ev->state) != MODKEY)
135 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
139 else if(ev->button == Button2)
141 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
149 configurerequest(XEvent *e) {
150 unsigned long newmask;
152 XConfigureRequestEvent *ev = &e->xconfigurerequest;
155 if((c = getclient(ev->window))) {
158 if(ev->value_mask & CWX)
160 if(ev->value_mask & CWY)
162 if(ev->value_mask & CWWidth)
164 if(ev->value_mask & CWHeight)
166 if(ev->value_mask & CWBorderWidth)
167 c->border = ev->border_width;
173 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
175 XConfigureWindow(dpy, c->win, newmask, &wc);
181 resize(c, False, TopLeft);
189 wc.width = ev->width;
190 wc.height = ev->height;
191 wc.border_width = ev->border_width;
192 wc.sibling = ev->above;
193 wc.stack_mode = ev->detail;
194 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
200 destroynotify(XEvent *e) {
202 XDestroyWindowEvent *ev = &e->xdestroywindow;
204 if((c = getclient(ev->window)))
209 enternotify(XEvent *e) {
211 XCrossingEvent *ev = &e->xcrossing;
213 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
216 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
218 else if(ev->window == root) {
220 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
228 XExposeEvent *ev = &e->xexpose;
231 if(barwin == ev->window)
233 else if((c = getctitle(ev->window)))
239 keypress(XEvent *e) {
240 static unsigned int len = sizeof(key) / sizeof(key[0]);
243 XKeyEvent *ev = &e->xkey;
245 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
246 for(i = 0; i < len; i++) {
247 if(keysym == key[i].keysym
248 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
251 key[i].func(&key[i].arg);
258 leavenotify(XEvent *e) {
259 XCrossingEvent *ev = &e->xcrossing;
261 if((ev->window == root) && !ev->same_screen) {
268 mappingnotify(XEvent *e) {
269 XMappingEvent *ev = &e->xmapping;
271 XRefreshKeyboardMapping(ev);
272 if(ev->request == MappingKeyboard)
277 maprequest(XEvent *e) {
278 static XWindowAttributes wa;
279 XMapRequestEvent *ev = &e->xmaprequest;
281 if(!XGetWindowAttributes(dpy, ev->window, &wa))
284 if(wa.override_redirect) {
285 XSelectInput(dpy, ev->window,
286 (StructureNotifyMask | PropertyChangeMask));
290 if(!getclient(ev->window))
291 manage(ev->window, &wa);
295 propertynotify(XEvent *e) {
298 XPropertyEvent *ev = &e->xproperty;
300 if(ev->state == PropertyDelete)
303 if((c = getclient(ev->window))) {
304 if(ev->atom == wmatom[WMProtocols]) {
305 c->proto = getproto(c->win);
310 case XA_WM_TRANSIENT_FOR:
311 XGetTransientForHint(dpy, c->win, &trans);
312 if(!c->isfloat && (c->isfloat = (trans != 0)))
315 case XA_WM_NORMAL_HINTS:
319 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
328 unmapnotify(XEvent *e) {
330 XUnmapEvent *ev = &e->xunmap;
332 if((c = getclient(ev->window)))
338 void (*handler[LASTEvent]) (XEvent *) = {
339 [ButtonPress] = buttonpress,
340 [ConfigureRequest] = configurerequest,
341 [DestroyNotify] = destroynotify,
342 [EnterNotify] = enternotify,
343 [LeaveNotify] = leavenotify,
345 [KeyPress] = keypress,
346 [MappingNotify] = mappingnotify,
347 [MapRequest] = maprequest,
348 [PropertyNotify] = propertynotify,
349 [UnmapNotify] = unmapnotify
354 static unsigned int len = sizeof(key) / sizeof(key[0]);
358 XUngrabKey(dpy, AnyKey, AnyModifier, root);
359 for(i = 0; i < len; i++) {
360 code = XKeysymToKeycode(dpy, key[i].keysym);
361 XGrabKey(dpy, code, key[i].mod, root, True,
362 GrabModeAsync, GrabModeAsync);
363 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
364 GrabModeAsync, GrabModeAsync);
365 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
366 GrabModeAsync, GrabModeAsync);
367 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
368 GrabModeAsync, GrabModeAsync);
376 while(XPending(dpy)) {
377 XNextEvent(dpy, &ev);
379 (handler[ev.type])(&ev); /* call handler */