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))
26 int x1, y1, ocx, ocy, di;
33 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
34 None, cursor[CurMove], CurrentTime) != GrabSuccess)
36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
46 c->x = ocx + (ev.xmotion.x - x1);
47 c->y = ocy + (ev.xmotion.y - y1);
48 resize(c, False, TopLeft);
51 XUngrabPointer(dpy, CurrentTime);
58 resizemouse(Client *c)
67 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
68 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);
80 if((nw = abs(ocx - ev.xmotion.x)))
81 c->w = abs(ocx - ev.xmotion.x);
82 if((nh = abs(ocy - ev.xmotion.y)))
83 c->h = abs(ocy - ev.xmotion.y);
84 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
85 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
86 if(ocx <= ev.xmotion.x)
87 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
89 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
90 resize(c, True, sticky);
93 XUngrabPointer(dpy, CurrentTime);
100 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(maximized || 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 synconfig(Client *c, int x, int y, int w, int h, unsigned int border)
154 synev.type = ConfigureNotify;
155 synev.xconfigure.display = dpy;
156 synev.xconfigure.event = c->win;
157 synev.xconfigure.window = c->win;
158 synev.xconfigure.x = x;
159 synev.xconfigure.y = y;
160 synev.xconfigure.width = w;
161 synev.xconfigure.height = h;
162 synev.xconfigure.border_width = border;
163 synev.xconfigure.above = None;
164 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
168 configurerequest(XEvent *e)
170 unsigned long newmask;
172 XConfigureRequestEvent *ev = &e->xconfigurerequest;
175 if((c = getclient(ev->window))) {
176 if((c == sel) && !c->isfloat && (arrange != dofloat) && maximized) {
177 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
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(ev->value_mask & CWBorderWidth)
191 c->border = ev->border_width;
197 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
199 XConfigureWindow(dpy, c->win, newmask, &wc);
201 synconfig(c, c->x, c->y, c->w, c->h, c->border);
204 resize(c, False, TopLeft);
211 wc.width = ev->width;
212 wc.height = ev->height;
213 wc.border_width = ev->border_width;
214 wc.sibling = ev->above;
215 wc.stack_mode = ev->detail;
216 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
222 destroynotify(XEvent *e)
225 XDestroyWindowEvent *ev = &e->xdestroywindow;
227 if((c = getclient(ev->window)))
232 enternotify(XEvent *e)
235 XCrossingEvent *ev = &e->xcrossing;
237 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
240 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
242 else if(ev->window == root) {
244 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
253 XExposeEvent *ev = &e->xexpose;
256 if(barwin == ev->window)
258 else if((c = getctitle(ev->window)))
266 static unsigned int len = sizeof(key) / sizeof(key[0]);
269 XKeyEvent *ev = &e->xkey;
271 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
272 for(i = 0; i < len; i++) {
273 if(keysym == key[i].keysym &&
274 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
277 key[i].func(&key[i].arg);
284 leavenotify(XEvent *e)
286 XCrossingEvent *ev = &e->xcrossing;
288 if((ev->window == root) && !ev->same_screen) {
295 mappingnotify(XEvent *e)
297 XMappingEvent *ev = &e->xmapping;
299 XRefreshKeyboardMapping(ev);
300 if(ev->request == MappingKeyboard)
305 maprequest(XEvent *e)
307 static XWindowAttributes wa;
308 XMapRequestEvent *ev = &e->xmaprequest;
310 if(!XGetWindowAttributes(dpy, ev->window, &wa))
313 if(wa.override_redirect) {
314 XSelectInput(dpy, ev->window,
315 (StructureNotifyMask | PropertyChangeMask));
319 if(!getclient(ev->window))
320 manage(ev->window, &wa);
324 propertynotify(XEvent *e)
328 XPropertyEvent *ev = &e->xproperty;
330 if(ev->state == PropertyDelete)
333 if((c = getclient(ev->window))) {
334 if(ev->atom == wmatom[WMProtocols]) {
335 c->proto = getproto(c->win);
340 case XA_WM_TRANSIENT_FOR:
341 XGetTransientForHint(dpy, c->win, &trans);
342 if(!c->isfloat && (c->isfloat = (trans != 0)))
345 case XA_WM_NORMAL_HINTS:
349 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
357 unmapnotify(XEvent *e)
360 XUnmapEvent *ev = &e->xunmap;
362 if((c = getclient(ev->window)))
368 void (*handler[LASTEvent]) (XEvent *) = {
369 [ButtonPress] = buttonpress,
370 [ConfigureRequest] = configurerequest,
371 [DestroyNotify] = destroynotify,
372 [EnterNotify] = enternotify,
373 [LeaveNotify] = leavenotify,
375 [KeyPress] = keypress,
376 [MappingNotify] = mappingnotify,
377 [MapRequest] = maprequest,
378 [PropertyNotify] = propertynotify,
379 [UnmapNotify] = unmapnotify
385 static unsigned int len = sizeof(key) / sizeof(key[0]);
389 XUngrabKey(dpy, AnyKey, AnyModifier, root);
390 for(i = 0; i < len; i++) {
391 code = XKeysymToKeycode(dpy, key[i].keysym);
392 XGrabKey(dpy, code, key[i].mod, root, True,
393 GrabModeAsync, GrabModeAsync);
394 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
395 GrabModeAsync, GrabModeAsync);
396 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
397 GrabModeAsync, GrabModeAsync);
398 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
399 GrabModeAsync, GrabModeAsync);
408 while(XPending(dpy)) {
409 XNextEvent(dpy, &ev);
411 (handler[ev.type])(&ev); /* call handler */