garbeam@5: /* garbeam@5: * (C)opyright MMVI Anselm R. Garbe garbeam@5: * See LICENSE file for license details. garbeam@5: */ garbeam@76: #include "dwm.h" garbeam@5: garbeam@10: #include garbeam@5: #include garbeam@5: #include garbeam@32: #include garbeam@5: garbeam@76: /* static functions */ garbeam@50: garbeam@26: static void garbeam@75: resizetitle(Client *c) garbeam@26: { garbeam@31: int i; garbeam@31: arg@115: c->tw = 0; garbeam@31: for(i = 0; i < TLast; i++) garbeam@31: if(c->tags[i]) arg@115: c->tw += textw(c->tags[i]); arg@115: c->tw += textw(c->name); arg@115: if(c->tw > c->w) arg@115: c->tw = c->w + 2; arg@115: c->tx = c->x + c->w - c->tw + 2; arg@115: c->ty = c->y; arg@106: if(c->tags[tsel]) arg@115: XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th); arg@106: else arg@115: XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th); arg@106: garbeam@26: } garbeam@18: garbeam@76: static int garbeam@76: xerrordummy(Display *dsply, XErrorEvent *ee) garbeam@5: { garbeam@76: return 0; garbeam@5: } garbeam@5: garbeam@76: /* extern functions */ garbeam@20: garbeam@20: void garbeam@76: ban(Client *c) garbeam@26: { arg@115: XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); arg@115: XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty); garbeam@26: } garbeam@26: garbeam@26: void garbeam@13: focus(Client *c) garbeam@13: { garbeam@52: Client *old = sel; garbeam@63: XEvent ev; garbeam@52: garbeam@50: sel = c; garbeam@52: if(old && old != c) garbeam@74: drawtitle(old); garbeam@74: drawtitle(c); garbeam@26: XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); garbeam@79: XSync(dpy, False); garbeam@63: while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); garbeam@13: } garbeam@13: garbeam@13: void garbeam@76: focusnext(Arg *arg) garbeam@76: { garbeam@76: Client *c; garbeam@76: garbeam@76: if(!sel) garbeam@76: return; garbeam@76: arg@124: if(sel->ismax) arg@124: togglemax(NULL); arg@124: arg@142: if(!(c = getnext(sel->next))) arg@142: c = getnext(clients); garbeam@76: if(c) { garbeam@76: higher(c); garbeam@76: focus(c); garbeam@76: } garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: focusprev(Arg *arg) garbeam@76: { garbeam@76: Client *c; garbeam@76: garbeam@76: if(!sel) garbeam@76: return; garbeam@76: arg@124: if(sel->ismax) arg@124: togglemax(NULL); arg@124: arg@127: if(!(c = getprev(sel->prev))) { arg@127: for(c = clients; c && c->next; c = c->next); arg@127: c = getprev(c); arg@127: } arg@127: if(c) { garbeam@76: higher(c); garbeam@76: focus(c); garbeam@76: } garbeam@76: } garbeam@76: garbeam@76: Client * garbeam@76: getclient(Window w) garbeam@76: { garbeam@76: Client *c; arg@123: garbeam@76: for(c = clients; c; c = c->next) garbeam@76: if(c->win == w) garbeam@76: return c; garbeam@76: return NULL; garbeam@76: } garbeam@76: garbeam@76: Client * garbeam@76: getctitle(Window w) garbeam@76: { garbeam@76: Client *c; arg@123: garbeam@76: for(c = clients; c; c = c->next) garbeam@76: if(c->title == w) garbeam@76: return c; garbeam@76: return NULL; garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: gravitate(Client *c, Bool invert) garbeam@76: { garbeam@76: int dx = 0, dy = 0; garbeam@76: garbeam@76: switch(c->grav) { arg@127: default: arg@127: break; garbeam@76: case StaticGravity: garbeam@76: case NorthWestGravity: garbeam@76: case NorthGravity: garbeam@76: case NorthEastGravity: garbeam@76: dy = c->border; garbeam@76: break; garbeam@76: case EastGravity: garbeam@76: case CenterGravity: garbeam@76: case WestGravity: arg@115: dy = -(c->h / 2) + c->border; garbeam@76: break; garbeam@76: case SouthEastGravity: garbeam@76: case SouthGravity: garbeam@76: case SouthWestGravity: arg@115: dy = -(c->h); garbeam@76: break; garbeam@76: } garbeam@76: garbeam@76: switch (c->grav) { arg@127: default: arg@127: break; garbeam@76: case StaticGravity: garbeam@76: case NorthWestGravity: garbeam@76: case WestGravity: garbeam@76: case SouthWestGravity: garbeam@76: dx = c->border; garbeam@76: break; garbeam@76: case NorthGravity: garbeam@76: case CenterGravity: garbeam@76: case SouthGravity: arg@115: dx = -(c->w / 2) + c->border; garbeam@76: break; garbeam@76: case NorthEastGravity: garbeam@76: case EastGravity: garbeam@76: case SouthEastGravity: arg@115: dx = -(c->w + c->border); garbeam@76: break; garbeam@76: } garbeam@76: garbeam@76: if(invert) { garbeam@76: dx = -dx; garbeam@76: dy = -dy; garbeam@76: } arg@115: c->x += dx; arg@115: c->y += dy; garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: higher(Client *c) garbeam@76: { garbeam@76: XRaiseWindow(dpy, c->win); garbeam@76: XRaiseWindow(dpy, c->title); garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: killclient(Arg *arg) garbeam@76: { garbeam@76: if(!sel) garbeam@76: return; garbeam@76: if(sel->proto & WM_PROTOCOL_DELWIN) garbeam@77: sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]); garbeam@76: else garbeam@76: XKillClient(dpy, sel->win); garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: lower(Client *c) garbeam@76: { garbeam@76: XLowerWindow(dpy, c->title); garbeam@76: XLowerWindow(dpy, c->win); garbeam@76: } garbeam@76: garbeam@76: void garbeam@10: manage(Window w, XWindowAttributes *wa) garbeam@5: { garbeam@80: Client *c; arg@123: Window trans; garbeam@5: XSetWindowAttributes twa; garbeam@5: garbeam@5: c = emallocz(sizeof(Client)); garbeam@5: c->win = w; arg@115: c->x = c->tx = wa->x; arg@115: c->y = c->ty = wa->y; arg@115: c->w = c->tw = wa->width; arg@115: c->h = wa->height; arg@115: c->th = bh; arg@100: arg@115: if(c->y < bh) arg@115: c->y = c->ty = bh; arg@104: garbeam@29: c->border = 1; garbeam@75: c->proto = getproto(c->win); garbeam@74: setsize(c); garbeam@26: XSelectInput(dpy, c->win, arg@127: StructureNotifyMask | PropertyChangeMask | EnterWindowMask); garbeam@53: XGetTransientForHint(dpy, c->win, &trans); garbeam@5: twa.override_redirect = 1; garbeam@5: twa.background_pixmap = ParentRelative; garbeam@23: twa.event_mask = ExposureMask; garbeam@5: arg@115: c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th, garbeam@20: 0, DefaultDepth(dpy, screen), CopyFromParent, garbeam@5: DefaultVisual(dpy, screen), garbeam@5: CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); garbeam@31: garbeam@75: settags(c); garbeam@49: arg@127: if(clients) arg@127: clients->prev = c; garbeam@80: c->next = clients; garbeam@80: clients = c; garbeam@49: arg@113: XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask, garbeam@19: GrabModeAsync, GrabModeSync, None, None); arg@113: XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask, garbeam@19: GrabModeAsync, GrabModeSync, None, None); arg@113: XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask, garbeam@18: GrabModeAsync, GrabModeSync, None, None); garbeam@53: garbeam@80: if(!c->isfloat) arg@114: c->isfloat = trans || (c->maxw && c->minw && arg@114: (c->maxw == c->minw) && (c->maxh == c->minh)); arg@114: garbeam@53: garbeam@95: settitle(c); garbeam@46: arrange(NULL); garbeam@95: garbeam@60: /* mapping the window now prevents flicker */ arg@142: XMapRaised(dpy, c->win); arg@142: XMapRaised(dpy, c->title); arg@142: if(c->tags[tsel]) garbeam@51: focus(c); garbeam@94: } garbeam@94: garbeam@94: void arg@129: resize(Client *c, Bool sizehints, Corner sticky) garbeam@18: { arg@123: int bottom = c->y + c->h; arg@123: int right = c->x + c->w; garbeam@18: XConfigureEvent e; garbeam@18: arg@129: if(sizehints) { garbeam@52: if(c->incw) arg@115: c->w -= (c->w - c->basew) % c->incw; garbeam@52: if(c->inch) arg@115: c->h -= (c->h - c->baseh) % c->inch; arg@129: if(c->minw && c->w < c->minw) arg@129: c->w = c->minw; arg@129: if(c->minh && c->h < c->minh) arg@129: c->h = c->minh; arg@129: if(c->maxw && c->w > c->maxw) arg@129: c->w = c->maxw; arg@129: if(c->maxh && c->h > c->maxh) arg@129: c->h = c->maxh; garbeam@52: } arg@115: if(c->x > sw) /* might happen on restart */ arg@115: c->x = sw - c->w; arg@115: if(c->y > sh) arg@115: c->y = sh - c->h; arg@105: if(sticky == TopRight || sticky == BotRight) arg@115: c->x = right - c->w; arg@105: if(sticky == BotLeft || sticky == BotRight) arg@115: c->y = bottom - c->h; arg@106: garbeam@75: resizetitle(c); garbeam@55: XSetWindowBorderWidth(dpy, c->win, 1); arg@115: XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); arg@106: garbeam@18: e.type = ConfigureNotify; garbeam@18: e.event = c->win; garbeam@18: e.window = c->win; arg@115: e.x = c->x; arg@115: e.y = c->y; arg@115: e.width = c->w; arg@115: e.height = c->h; garbeam@29: e.border_width = c->border; garbeam@18: e.above = None; garbeam@18: e.override_redirect = False; garbeam@18: XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e); garbeam@79: XSync(dpy, False); garbeam@18: } garbeam@18: garbeam@76: void garbeam@76: setsize(Client *c) garbeam@10: { arg@123: long msize; garbeam@76: XSizeHints size; arg@123: garbeam@76: if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags) garbeam@76: size.flags = PSize; garbeam@76: c->flags = size.flags; garbeam@76: if(c->flags & PBaseSize) { garbeam@76: c->basew = size.base_width; garbeam@76: c->baseh = size.base_height; garbeam@76: } garbeam@76: else garbeam@76: c->basew = c->baseh = 0; garbeam@76: if(c->flags & PResizeInc) { garbeam@76: c->incw = size.width_inc; garbeam@76: c->inch = size.height_inc; garbeam@76: } garbeam@76: else garbeam@76: c->incw = c->inch = 0; garbeam@76: if(c->flags & PMaxSize) { garbeam@76: c->maxw = size.max_width; garbeam@76: c->maxh = size.max_height; garbeam@76: } garbeam@76: else garbeam@76: c->maxw = c->maxh = 0; garbeam@76: if(c->flags & PMinSize) { garbeam@76: c->minw = size.min_width; garbeam@76: c->minh = size.min_height; garbeam@76: } garbeam@76: else garbeam@76: c->minw = c->minh = 0; garbeam@76: if(c->flags & PWinGravity) garbeam@76: c->grav = size.win_gravity; garbeam@76: else garbeam@76: c->grav = NorthWestGravity; garbeam@76: } garbeam@76: garbeam@76: void garbeam@76: settitle(Client *c) garbeam@76: { arg@123: char **list = NULL; arg@123: int n; garbeam@76: XTextProperty name; garbeam@76: garbeam@76: name.nitems = 0; garbeam@76: c->name[0] = 0; garbeam@77: XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]); garbeam@76: if(!name.nitems) garbeam@76: XGetWMName(dpy, c->win, &name); garbeam@76: if(!name.nitems) garbeam@76: return; garbeam@76: if(name.encoding == XA_STRING) garbeam@76: strncpy(c->name, (char *)name.value, sizeof(c->name)); garbeam@76: else { garbeam@76: if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success garbeam@76: && n > 0 && *list) garbeam@76: { garbeam@76: strncpy(c->name, *list, sizeof(c->name)); garbeam@76: XFreeStringList(list); garbeam@76: } garbeam@76: } garbeam@76: XFree(name.value); garbeam@76: resizetitle(c); garbeam@10: } garbeam@10: garbeam@10: void arg@124: togglemax(Arg *arg) arg@124: { arg@124: int ox, oy, ow, oh; arg@124: XEvent ev; arg@124: arg@124: if(!sel) arg@124: return; arg@124: arg@124: if((sel->ismax = !sel->ismax)) { arg@124: ox = sel->x; arg@124: oy = sel->y; arg@124: ow = sel->w; arg@124: oh = sel->h; arg@124: sel->x = sx; arg@124: sel->y = sy + bh; arg@124: sel->w = sw - 2 * sel->border; arg@124: sel->h = sh - 2 * sel->border - bh; arg@124: arg@124: higher(sel); arg@124: resize(sel, False, TopLeft); arg@124: arg@124: sel->x = ox; arg@124: sel->y = oy; arg@124: sel->w = ow; arg@124: sel->h = oh; arg@124: } arg@124: else arg@124: resize(sel, False, TopLeft); arg@124: while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); arg@124: } arg@124: arg@124: void garbeam@10: unmanage(Client *c) garbeam@10: { garbeam@50: Client **l; garbeam@50: garbeam@10: XGrabServer(dpy); garbeam@75: XSetErrorHandler(xerrordummy); garbeam@10: garbeam@18: XUngrabButton(dpy, AnyButton, AnyModifier, c->win); garbeam@10: XDestroyWindow(dpy, c->title); garbeam@10: garbeam@50: for(l = &clients; *l && *l != c; l = &(*l)->next); arg@127: if(c->prev) arg@127: c->prev->next = c->next; arg@127: if(c->next) arg@127: c->next->prev = c->prev; garbeam@50: *l = c->next; arg@127: if(sel == c) { arg@142: sel = getnext(c->next); arg@127: if(!sel) arg@127: sel = getprev(c->prev); arg@127: if(!sel) arg@127: sel = clients; arg@127: } garbeam@10: free(c); garbeam@10: garbeam@79: XSync(dpy, False); garbeam@74: XSetErrorHandler(xerror); garbeam@10: XUngrabServer(dpy); garbeam@46: arrange(NULL); garbeam@50: if(sel) garbeam@50: focus(sel); garbeam@10: } garbeam@10: garbeam@75: void garbeam@75: zoom(Arg *arg) garbeam@75: { arg@130: Client *c, **l; garbeam@75: garbeam@75: if(!sel) garbeam@75: return; garbeam@75: arg@142: if(sel == getnext(clients) && sel->next) { arg@142: if((c = getnext(sel->next))) garbeam@75: sel = c; garbeam@75: } garbeam@75: arg@130: /* pop */ arg@130: for(l = &clients; *l && *l != sel; l = &(*l)->next); arg@130: if(sel->prev) arg@130: sel->prev->next = sel->next; arg@130: if(sel->next) arg@130: sel->next->prev = sel->prev; arg@130: *l = sel->next; arg@130: arg@130: sel->prev = NULL; arg@130: if(clients) arg@130: clients->prev = sel; arg@130: sel->next = clients; arg@130: clients = sel; arg@130: arrange(NULL); garbeam@75: focus(sel); garbeam@75: }