/* * (C)opyright MMVI Anselm R. Garbe * See LICENSE file for license details. */ #include "dwm.h" #include #include #include #include /* static functions */ static void resizetitle(Client *c) { int i; c->tw = 0; for(i = 0; i < TLast; i++) if(c->tags[i]) c->tw += textw(c->tags[i]); c->tw += textw(c->name); if(c->tw > c->w) c->tw = c->w + 2; c->tx = c->x + c->w - c->tw + 2; c->ty = c->y; XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th); } static int xerrordummy(Display *dsply, XErrorEvent *ee) { return 0; } /* extern functions */ void ban(Client *c) { XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty); } void focus(Client *c) { Client *old = sel; XEvent ev; sel = c; if(old && old != c) drawtitle(old); drawtitle(c); XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); XSync(dpy, False); while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); } void focusnext(Arg *arg) { Client *c; if(!sel) return; if(!(c = getnext(sel->next, tsel))) c = getnext(clients, tsel); if(c) { higher(c); c->revert = sel; focus(c); } } void focusprev(Arg *arg) { Client *c; if(!sel) return; if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) { higher(c); focus(c); } } Client * getclient(Window w) { Client *c; for(c = clients; c; c = c->next) if(c->win == w) return c; return NULL; } Client * getctitle(Window w) { Client *c; for(c = clients; c; c = c->next) if(c->title == w) return c; return NULL; } void gravitate(Client *c, Bool invert) { int dx = 0, dy = 0; switch(c->grav) { case StaticGravity: case NorthWestGravity: case NorthGravity: case NorthEastGravity: dy = c->border; break; case EastGravity: case CenterGravity: case WestGravity: dy = -(c->h / 2) + c->border; break; case SouthEastGravity: case SouthGravity: case SouthWestGravity: dy = -c->h; break; default: break; } switch (c->grav) { case StaticGravity: case NorthWestGravity: case WestGravity: case SouthWestGravity: dx = c->border; break; case NorthGravity: case CenterGravity: case SouthGravity: dx = -(c->w / 2) + c->border; break; case NorthEastGravity: case EastGravity: case SouthEastGravity: dx = -(c->w + c->border); break; default: break; } if(invert) { dx = -dx; dy = -dy; } c->x += dx; c->y += dy; } void higher(Client *c) { XRaiseWindow(dpy, c->win); XRaiseWindow(dpy, c->title); } void killclient(Arg *arg) { if(!sel) return; if(sel->proto & WM_PROTOCOL_DELWIN) sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]); else XKillClient(dpy, sel->win); } void lower(Client *c) { XLowerWindow(dpy, c->title); XLowerWindow(dpy, c->win); } void manage(Window w, XWindowAttributes *wa) { Client *c; XSetWindowAttributes twa; Window trans; c = emallocz(sizeof(Client)); c->win = w; c->tx = c->x = wa->x; c->ty = c->y = wa->y; if(c->y < bh) c->ty = c->y += bh; c->tw = c->w = wa->width; c->h = wa->height; c->th = bh; c->border = 1; c->proto = getproto(c->win); setsize(c); XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | EnterWindowMask); XGetTransientForHint(dpy, c->win, &trans); twa.override_redirect = 1; twa.background_pixmap = ParentRelative; twa.event_mask = ExposureMask; c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th, 0, DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); settitle(c); settags(c); c->next = clients; clients = c; XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask, GrabModeAsync, GrabModeSync, None, None); XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask, GrabModeAsync, GrabModeSync, None, None); XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask, GrabModeAsync, GrabModeSync, None, None); XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask, GrabModeAsync, GrabModeSync, None, None); if(!c->isfloat) c->isfloat = trans || ((c->maxw == c->minw) && (c->maxh == c->minh)); arrange(NULL); /* mapping the window now prevents flicker */ if(c->tags[tsel]) { XMapRaised(dpy, c->win); XMapRaised(dpy, c->title); focus(c); } else { ban(c); XMapRaised(dpy, c->win); XMapRaised(dpy, c->title); XSync(dpy, False); } } void maximize(Arg *arg) { if(!sel) return; sel->x = sx; sel->y = sy + bh; sel->w = sw - 2 * sel->border; sel->h = sh - 2 * sel->border - bh; higher(sel); resize(sel, False); } void resize(Client *c, Bool inc) { XConfigureEvent e; if(inc) { if(c->incw) c->w -= (c->w - c->basew) % c->incw; if(c->inch) c->h -= (c->h - c->baseh) % c->inch; } if(c->x > sw) /* might happen on restart */ c->x = sw - c->w;