arg@644: /* (C)opyright MMVI-MMVII Anselm R. Garbe garbeam@5: * See LICENSE file for license details. garbeam@5: */ garbeam@76: #include "dwm.h" garbeam@5: #include garbeam@5: #include garbeam@13: #include garbeam@5: arg@146: /* static */ arg@114: arg@114: typedef struct { arg@114: unsigned long mod; arg@114: KeySym keysym; arg@589: void (*func)(Arg *arg); arg@114: Arg arg; arg@114: } Key; arg@114: arg@146: KEYS garbeam@75: arg@291: #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) arg@538: #define MOUSEMASK (BUTTONMASK | PointerMotionMask) garbeam@5: garbeam@77: static void arg@461: movemouse(Client *c) { garbeam@77: int x1, y1, ocx, ocy, di; garbeam@77: unsigned int dui; garbeam@77: Window dummy; arg@123: XEvent ev; garbeam@77: arg@115: ocx = c->x; arg@115: ocy = c->y; arg@148: if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, arg@123: None, cursor[CurMove], CurrentTime) != GrabSuccess) garbeam@77: return; arg@482: c->ismax = False; garbeam@77: XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); garbeam@77: for(;;) { arg@489: XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); garbeam@77: switch (ev.type) { arg@490: case ButtonRelease: arg@491: resize(c, True, TopLeft); arg@490: XUngrabPointer(dpy, CurrentTime); arg@490: return; garbeam@77: case Expose: garbeam@77: handler[Expose](&ev); garbeam@77: break; garbeam@77: case MotionNotify: garbeam@79: XSync(dpy, False); arg@115: c->x = ocx + (ev.xmotion.x - x1); arg@115: c->y = ocy + (ev.xmotion.y - y1); arg@565: if(abs(wax + c->x) < SNAP) arg@565: c->x = wax; arg@565: else if(abs((wax + waw) - (c->x + c->w)) < SNAP) arg@565: c->x = wax + waw - c->w - 2 * BORDERPX; arg@565: if(abs(way - c->y) < SNAP) arg@565: c->y = way; arg@565: else if(abs((way + wah) - (c->y + c->h)) < SNAP) arg@565: c->y = way + wah - c->h - 2 * BORDERPX; arg@99: resize(c, False, TopLeft); garbeam@77: break; garbeam@77: } garbeam@77: } garbeam@77: } garbeam@77: garbeam@77: static void arg@461: resizemouse(Client *c) { garbeam@77: int ocx, ocy; arg@268: int nw, nh; arg@99: Corner sticky; arg@123: XEvent ev; garbeam@77: arg@115: ocx = c->x; arg@115: ocy = c->y; arg@148: if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, arg@532: None, cursor[CurResize], CurrentTime) != GrabSuccess) garbeam@77: return; arg@482: c->ismax = False; arg@115: XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h); garbeam@77: for(;;) { arg@489: XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); garbeam@77: switch(ev.type) { arg@490: case ButtonRelease: arg@491: resize(c, True, TopLeft); arg@490: XUngrabPointer(dpy, CurrentTime); arg@490: return; garbeam@77: case Expose: garbeam@77: handler[Expose](&ev); garbeam@77: break; garbeam@77: case MotionNotify: garbeam@79: XSync(dpy, False); arg@268: if((nw = abs(ocx - ev.xmotion.x))) arg@478: c->w = nw; arg@268: if((nh = abs(ocy - ev.xmotion.y))) arg@478: c->h = nh; arg@115: c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w; arg@115: c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h; arg@105: if(ocx <= ev.xmotion.x) arg@105: sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft; arg@105: else arg@105: sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight; arg@99: resize(c, True, sticky); garbeam@77: break; garbeam@77: } garbeam@77: } garbeam@77: } garbeam@73: garbeam@73: static void arg@461: buttonpress(XEvent *e) { garbeam@73: int x; garbeam@73: Arg a; arg@123: Client *c; garbeam@18: XButtonPressedEvent *ev = &e->xbutton; garbeam@18: garbeam@73: if(barwin == ev->window) { arg@362: x = 0; arg@362: for(a.i = 0; a.i < ntags; a.i++) { arg@362: x += textw(tags[a.i]); arg@362: if(ev->x < x) { arg@399: if(ev->button == Button1) { arg@398: if(ev->state & MODKEY) arg@398: tag(&a); arg@398: else arg@398: view(&a); arg@399: } arg@399: else if(ev->button == Button3) { arg@398: if(ev->state & MODKEY) arg@398: toggletag(&a); arg@398: else arg@398: toggleview(&a); arg@394: } arg@362: return; garbeam@73: } garbeam@73: } arg@676: if(ev->x < x + bmw) arg@676: switch(ev->button) { arg@676: case Button1: arg@676: togglemode(NULL); arg@676: break; arg@676: case Button4: arg@676: a.i = 1; arg@676: incnmaster(&a); arg@676: break; arg@676: case Button5: arg@676: a.i = -1; arg@676: incnmaster(&a); arg@676: break; arg@676: } garbeam@73: } garbeam@58: else if((c = getclient(ev->window))) { arg@143: focus(c); arg@473: if(CLEANMASK(ev->state) != MODKEY) arg@318: return; arg@400: if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) { arg@487: restack(); arg@399: movemouse(c); arg@399: } arg@399: else if(ev->button == Button2) arg@248: zoom(NULL); arg@550: else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) && arg@550: !c->isfixed) { arg@487: restack(); arg@399: resizemouse(c); garbeam@18: } garbeam@18: } garbeam@18: } garbeam@18: garbeam@18: static void arg@461: configurerequest(XEvent *e) { arg@286: unsigned long newmask; arg@123: Client *c; garbeam@5: XConfigureRequestEvent *ev = &e->xconfigurerequest; garbeam@5: XWindowChanges wc; garbeam@5: garbeam@18: if((c = getclient(ev->window))) { arg@488: c->ismax = False; arg@195: if(ev->value_mask & CWX) arg@195: c->x = ev->x; arg@195: if(ev->value_mask & CWY) arg@195: c->y = ev->y; arg@195: if(ev->value_mask & CWWidth) arg@195: c->w = ev->width; arg@195: if(ev->value_mask & CWHeight) arg@195: c->h = ev->height; garbeam@29: if(ev->value_mask & CWBorderWidth) arg@164: c->border = ev->border_width; arg@164: wc.x = c->x; arg@164: wc.y = c->y; arg@164: wc.width = c->w; arg@164: wc.height = c->h; arg@164: newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth)); arg@164: if(newmask) arg@164: XConfigureWindow(dpy, c->win, newmask, &wc); arg@491: else arg@491: configure(c); arg@195: XSync(dpy, False); arg@505: if(c->isfloat) { arg@508: resize(c, False, TopLeft); arg@508: if(!isvisible(c)) arg@687: XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); arg@505: } arg@196: else arg@533: arrange(); garbeam@5: } arg@164: else { arg@164: wc.x = ev->x; arg@164: wc.y = ev->y; arg@164: wc.width = ev->width; arg@164: wc.height = ev->height; arg@164: wc.border_width = ev->border_width; arg@164: wc.sibling = ev->above; arg@164: wc.stack_mode = ev->detail; arg@164: XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); arg@195: XSync(dpy, False); arg@164: } garbeam@5: } garbeam@5: garbeam@5: static void arg@461: destroynotify(XEvent *e) { garbeam@5: Client *c; garbeam@5: XDestroyWindowEvent *ev = &e->xdestroywindow; garbeam@5: garbeam@11: if((c = getclient(ev->window))) garbeam@11: unmanage(c); garbeam@5: } garbeam@5: garbeam@5: static void arg@461: enternotify(XEvent *e) { arg@123: Client *c; garbeam@5: XCrossingEvent *ev = &e->xcrossing; garbeam@5: arg@232: if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) garbeam@5: return; arg@687: if((c = getclient(ev->window)) && isvisible(c)) garbeam@13: focus(c); arg@239: else if(ev->window == root) { garbeam@31: issel = True; arg@707: focus(sel); arg@239: } garbeam@5: } garbeam@5: garbeam@5: static void arg@461: expose(XEvent *e) { garbeam@5: XExposeEvent *ev = &e->xexpose; garbeam@5: garbeam@5: if(ev->count == 0) { garbeam@70: if(barwin == ev->window) garbeam@74: drawstatus(); garbeam@5: } garbeam@5: } garbeam@5: garbeam@5: static void arg@461: keypress(XEvent *e) { arg@581: static unsigned int len = sizeof key / sizeof key[0]; arg@589: unsigned int i; garbeam@76: KeySym keysym; arg@123: XKeyEvent *ev = &e->xkey; garbeam@76: garbeam@76: keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); arg@275: for(i = 0; i < len; i++) { arg@461: if(keysym == key[i].keysym arg@461: && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) arg@275: { arg@589: if(key[i].func) arg@589: key[i].func(&key[i].arg); garbeam@76: } arg@275: } garbeam@76: } garbeam@76: garbeam@76: static void arg@461: leavenotify(XEvent *e) { garbeam@76: XCrossingEvent *ev = &e->xcrossing; garbeam@76: arg@701: if((ev->window == root) && !ev->same_screen) { arg@707: focus(NULL); arg@239: issel = False; arg@701: } garbeam@76: } garbeam@76: garbeam@76: static void arg@461: mappingnotify(XEvent *e) { arg@279: XMappingEvent *ev = &e->xmapping; arg@279: arg@279: XRefreshKeyboardMapping(ev); arg@279: if(ev->request == MappingKeyboard) arg@279: grabkeys(); arg@279: } arg@279: arg@279: static void arg@461: maprequest(XEvent *e) { arg@123: static XWindowAttributes wa; garbeam@5: XMapRequestEvent *ev = &e->xmaprequest; garbeam@5: garbeam@5: if(!XGetWindowAttributes(dpy, ev->window, &wa)) garbeam@5: return; garbeam@5: if(wa.override_redirect) { garbeam@5: XSelectInput(dpy, ev->window, garbeam@5: (StructureNotifyMask | PropertyChangeMask)); garbeam@5: return; garbeam@5: } garbeam@10: if(!getclient(ev->window)) garbeam@10: manage(ev->window, &wa); garbeam@5: } garbeam@5: garbeam@5: static void arg@461: propertynotify(XEvent *e) { arg@123: Client *c; arg@123: Window trans; garbeam@5: XPropertyEvent *ev = &e->xproperty; garbeam@5: garbeam@5: if(ev->state == PropertyDelete) garbeam@5: return; /* ignore */ garbeam@13: if((c = getclient(ev->window))) { garbeam@77: if(ev->atom == wmatom[WMProtocols]) { garbeam@75: c->proto = getproto(c->win); garbeam@30: return; garbeam@30: } garbeam@13: switch (ev->atom) { garbeam@13: default: break; garbeam@13: case XA_WM_TRANSIENT_FOR: garbeam@53: XGetTransientForHint(dpy, c->win, &trans); garbeam@80: if(!c->isfloat && (c->isfloat = (trans != 0))) arg@533: arrange(); garbeam@13: break; garbeam@13: case XA_WM_NORMAL_HINTS: arg@639: updatesizehints(c); garbeam@13: break; garbeam@13: } garbeam@77: if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { arg@454: updatetitle(c); arg@690: if(c == sel) arg@690: drawstatus(); garbeam@13: } garbeam@13: } garbeam@5: } garbeam@5: garbeam@5: static void arg@461: unmapnotify(XEvent *e) { garbeam@5: Client *c; garbeam@5: XUnmapEvent *ev = &e->xunmap; garbeam@5: garbeam@10: if((c = getclient(ev->window))) garbeam@10: unmanage(c); garbeam@5: } garbeam@76: garbeam@84: /* extern */ garbeam@76: garbeam@76: void (*handler[LASTEvent]) (XEvent *) = { garbeam@76: [ButtonPress] = buttonpress, garbeam@76: [ConfigureRequest] = configurerequest, garbeam@76: [DestroyNotify] = destroynotify, garbeam@76: [EnterNotify] = enternotify, garbeam@76: [LeaveNotify] = leavenotify, garbeam@76: [Expose] = expose, garbeam@76: [KeyPress] = keypress, arg@279: [MappingNotify] = mappingnotify, garbeam@76: [MapRequest] = maprequest, garbeam@76: [PropertyNotify] = propertynotify, garbeam@76: [UnmapNotify] = unmapnotify garbeam@76: }; garbeam@76: garbeam@76: void arg@487: grabkeys(void) { arg@581: static unsigned int len = sizeof key / sizeof key[0]; garbeam@76: unsigned int i; garbeam@76: KeyCode code; garbeam@76: arg@279: XUngrabKey(dpy, AnyKey, AnyModifier, root); garbeam@76: for(i = 0; i < len; i++) { garbeam@76: code = XKeysymToKeycode(dpy, key[i].keysym); garbeam@76: XGrabKey(dpy, code, key[i].mod, root, True, garbeam@76: GrabModeAsync, GrabModeAsync); arg@160: XGrabKey(dpy, code, key[i].mod | LockMask, root, True, arg@160: GrabModeAsync, GrabModeAsync); arg@291: XGrabKey(dpy, code, key[i].mod | numlockmask, root, True, arg@146: GrabModeAsync, GrabModeAsync); arg@291: XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True, arg@146: GrabModeAsync, GrabModeAsync); garbeam@76: } garbeam@76: } arg@292: arg@292: void arg@487: procevent(void) { arg@292: XEvent ev; arg@292: arg@292: while(XPending(dpy)) { arg@292: XNextEvent(dpy, &ev); arg@292: if(handler[ev.type]) arg@292: (handler[ev.type])(&ev); /* call handler */ arg@292: } arg@292: }