1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * © 2007 Premysl Hruby <dfenze at gmail dot com>
4 * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
5 * See LICENSE file for license details. */
9 #include <X11/keysym.h>
10 #include <X11/Xatom.h>
17 void (*func)(const char *arg);
23 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
24 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
30 for(c = clients; c && c->win != w; c = c->next);
35 movemouse(Client *c) {
36 int x1, y1, ocx, ocy, di, nx, ny;
43 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
44 None, cursor[CurMove], CurrentTime) != GrabSuccess)
47 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
49 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
52 XUngrabPointer(dpy, CurrentTime);
54 case ConfigureRequest:
57 handler[ev.type](&ev);
61 nx = ocx + (ev.xmotion.x - x1);
62 ny = ocy + (ev.xmotion.y - y1);
63 if(abs(wax + nx) < SNAP)
65 else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
66 nx = wax + waw - c->w - 2 * c->border;
67 if(abs(way - ny) < SNAP)
69 else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
70 ny = way + wah - c->h - 2 * c->border;
71 resize(c, nx, ny, c->w, c->h, False);
78 resizemouse(Client *c) {
85 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
86 None, cursor[CurResize], CurrentTime) != GrabSuccess)
89 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
91 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
94 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
95 c->w + c->border - 1, c->h + c->border - 1);
96 XUngrabPointer(dpy, CurrentTime);
97 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
99 case ConfigureRequest:
102 handler[ev.type](&ev);
106 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
108 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
110 resize(c, c->x, c->y, nw, nh, True);
117 buttonpress(XEvent *e) {
121 XButtonPressedEvent *ev = &e->xbutton;
124 if(barwin == ev->window) {
126 for(i = 0; i < ntags; i++) {
129 snprintf(buf, sizeof buf, "%d", i);
130 if(ev->button == Button1) {
131 if(ev->state & MODKEY)
136 else if(ev->button == Button3) {
137 if(ev->state & MODKEY)
152 else if((c = getclient(ev->window))) {
154 if(CLEANMASK(ev->state) != MODKEY)
156 if(ev->button == Button1 && (lt->arrange == floating || c->isfloating)) {
160 else if(ev->button == Button2)
162 else if(ev->button == Button3
163 && (lt->arrange == floating || c->isfloating) && !c->isfixed)
172 configurerequest(XEvent *e) {
174 XConfigureRequestEvent *ev = &e->xconfigurerequest;
177 if((c = getclient(ev->window))) {
179 if(ev->value_mask & CWBorderWidth)
180 c->border = ev->border_width;
181 if(c->isfixed || c->isfloating || (lt->arrange == floating)) {
182 if(ev->value_mask & CWX)
184 if(ev->value_mask & CWY)
186 if(ev->value_mask & CWWidth)
188 if(ev->value_mask & CWHeight)
190 if((c->x + c->w) > sw && c->isfloating)
191 c->x = sw / 2 - c->w / 2; /* center in x direction */
192 if((c->y + c->h) > sh && c->isfloating)
193 c->y = sh / 2 - c->h / 2; /* center in y direction */
194 if((ev->value_mask & (CWX | CWY))
195 && !(ev->value_mask & (CWWidth | CWHeight)))
198 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
206 wc.width = ev->width;
207 wc.height = ev->height;
208 wc.border_width = ev->border_width;
209 wc.sibling = ev->above;
210 wc.stack_mode = ev->detail;
211 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
217 configurenotify(XEvent *e) {
218 XConfigureEvent *ev = &e->xconfigure;
220 if (ev->window == root && (ev->width != sw || ev->height != sh)) {
223 XFreePixmap(dpy, dc.drawable);
224 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
225 XResizeWindow(dpy, barwin, sw, bh);
232 destroynotify(XEvent *e) {
234 XDestroyWindowEvent *ev = &e->xdestroywindow;
236 if((c = getclient(ev->window)))
241 enternotify(XEvent *e) {
243 XCrossingEvent *ev = &e->xcrossing;
245 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
247 if(c = getclient(ev->window))
249 else if(ev->window == root) {
257 XExposeEvent *ev = &e->xexpose;
260 if(barwin == ev->window)
266 keypress(XEvent *e) {
267 static unsigned int len = sizeof key / sizeof key[0];
270 XKeyEvent *ev = &e->xkey;
272 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
273 for(i = 0; i < len; i++)
274 if(keysym == key[i].keysym
275 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
278 key[i].func(key[i].arg);
283 leavenotify(XEvent *e) {
284 XCrossingEvent *ev = &e->xcrossing;
286 if((ev->window == root) && !ev->same_screen) {
293 mappingnotify(XEvent *e) {
294 XMappingEvent *ev = &e->xmapping;
296 XRefreshKeyboardMapping(ev);
297 if(ev->request == MappingKeyboard)
302 maprequest(XEvent *e) {
303 static XWindowAttributes wa;
304 XMapRequestEvent *ev = &e->xmaprequest;
306 if(!XGetWindowAttributes(dpy, ev->window, &wa))
308 if(wa.override_redirect)
310 if(!getclient(ev->window))
311 manage(ev->window, &wa);
315 propertynotify(XEvent *e) {
318 XPropertyEvent *ev = &e->xproperty;
320 if(ev->state == PropertyDelete)
322 if((c = getclient(ev->window))) {
325 case XA_WM_TRANSIENT_FOR:
326 XGetTransientForHint(dpy, c->win, &trans);
327 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
330 case XA_WM_NORMAL_HINTS:
334 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
343 unmapnotify(XEvent *e) {
345 XUnmapEvent *ev = &e->xunmap;
347 if((c = getclient(ev->window)))
353 void (*handler[LASTEvent]) (XEvent *) = {
354 [ButtonPress] = buttonpress,
355 [ConfigureRequest] = configurerequest,
356 [ConfigureNotify] = configurenotify,
357 [DestroyNotify] = destroynotify,
358 [EnterNotify] = enternotify,
359 [LeaveNotify] = leavenotify,
361 [KeyPress] = keypress,
362 [MappingNotify] = mappingnotify,
363 [MapRequest] = maprequest,
364 [PropertyNotify] = propertynotify,
365 [UnmapNotify] = unmapnotify
370 static unsigned int len = sizeof key / sizeof key[0];
374 XUngrabKey(dpy, AnyKey, AnyModifier, root);
375 for(i = 0; i < len; i++) {
376 code = XKeysymToKeycode(dpy, key[i].keysym);
377 XGrabKey(dpy, code, key[i].mod, root, True,
378 GrabModeAsync, GrabModeAsync);
379 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
380 GrabModeAsync, GrabModeAsync);
381 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
382 GrabModeAsync, GrabModeAsync);
383 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
384 GrabModeAsync, GrabModeAsync);