arg@532: /* (C)opyright MMVI Anselm R. Garbe garbeam@5: * See LICENSE file for license details. garbeam@5: */ garbeam@76: #include "dwm.h" garbeam@10: #include garbeam@5: #include garbeam@5: #include garbeam@32: #include garbeam@5: garbeam@76: /* static functions */ garbeam@50: garbeam@26: static void arg@461: detachstack(Client *c) { arg@446: Client **tc; arg@446: for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext); arg@446: *tc = c->snext; arg@446: } arg@446: arg@446: static void arg@461: grabbuttons(Client *c, Bool focused) { arg@372: XUngrabButton(dpy, AnyButton, AnyModifier, c->win); arg@372: arg@452: if(focused) { arg@372: XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: arg@372: XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: arg@372: XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@372: } arg@372: else arg@372: XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK, arg@372: GrabModeAsync, GrabModeSync, None, None); arg@318: } arg@318: garbeam@76: static int arg@461: xerrordummy(Display *dsply, XErrorEvent *ee) { garbeam@76: return 0; garbeam@5: } garbeam@5: garbeam@76: /* extern functions */ garbeam@20: garbeam@20: void arg@461: ban(Client *c) { arg@115: XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); arg@342: XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty); garbeam@26: } garbeam@26: garbeam@26: void arg@491: configure(Client *c) { arg@491: XEvent synev; arg@491: arg@491: synev.type = ConfigureNotify; arg@491: synev.xconfigure.display = dpy; arg@491: synev.xconfigure.event = c->win; arg@491: synev.xconfigure.window = c->win; arg@491: synev.xconfigure.x = c->x; arg@491: synev.xconfigure.y = c->y; arg@491: synev.xconfigure.width = c->w; arg@491: synev.xconfigure.height = c->h; arg@491: synev.xconfigure.border_width = c->border; arg@491: synev.xconfigure.above = None; arg@491: XSendEvent(dpy, c->win, True, NoEventMask, &synev); arg@491: } arg@491: arg@491: void arg@461: focus(Client *c) { arg@400: Client *old; arg@286: arg@464: if(!issel || (c && !isvisible(c))) arg@239: return; arg@319: if(!sel) arg@319: sel = c; arg@319: else if(sel != c) { arg@400: old = sel; arg@319: sel = c; arg@400: if(old) { arg@400: grabbuttons(old, False); arg@400: drawtitle(old); arg@400: } arg@318: } arg@400: if(c) { arg@446: detachstack(c); arg@446: c->snext = stack; arg@446: stack = c; arg@400: grabbuttons(c, True); arg@400: drawtitle(c); arg@400: XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); arg@400: } arg@400: else arg@400: XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); garbeam@13: } garbeam@13: garbeam@76: Client * arg@461: getclient(Window w) { 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 * arg@461: getctitle(Window w) { garbeam@76: Client *c; arg@123: garbeam@76: for(c = clients; c; c = c->next) arg@342: if(c->twin == w) garbeam@76: return c; garbeam@76: return NULL; garbeam@76: } garbeam@76: garbeam@76: void arg@461: gravitate(Client *c, Bool invert) { garbeam@76: int dx = 0, dy = 0; garbeam@76: arg@628: return; 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: 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: 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 arg@461: killclient(Arg *arg) { garbeam@76: if(!sel) garbeam@76: return; arg@157: if(sel->proto & PROTODELWIN) 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 arg@461: manage(Window w, XWindowAttributes *wa) { arg@431: Client *c; arg@123: Window trans; garbeam@5: XSetWindowAttributes twa; garbeam@5: garbeam@5: c = emallocz(sizeof(Client)); arg@178: c->tags = emallocz(ntags * sizeof(Bool)); garbeam@5: c->win = w; arg@546: c->border = 0; arg@544: c->x = c->tx = wa->x; arg@544: 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@454: updatesize(c); arg@502: if(c->x + c->w + 2 * BORDERPX > sw) arg@502: c->x = sw - c->w - 2 * BORDERPX; arg@517: if(c->x < sx) arg@517: c->x = sx; arg@502: if(c->y + c->h + 2 * BORDERPX > sh) arg@517: c->y = sh - c->h - 2 * BORDERPX; arg@163: if(c->h != sh && c->y < bh) arg@314: c->y = bh; garbeam@75: c->proto = getproto(c->win); 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; arg@161: twa.event_mask = ExposureMask | EnterWindowMask; arg@342: c->twin = 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); arg@372: grabbuttons(c, False); arg@500: updatetitle(c); arg@431: settags(c, getclient(trans)); garbeam@80: if(!c->isfloat) arg@549: c->isfloat = trans || c->isfixed; arg@500: resizetitle(c); arg@381: if(clients) arg@381: clients->prev = c; arg@381: c->next = clients; arg@446: c->snext = stack; arg@446: stack = clients = c; arg@408: ban(c); arg@270: XMapWindow(dpy, c->win); arg@342: XMapWindow(dpy, c->twin); arg@261: if(isvisible(c)) garbeam@51: focus(c); arg@533: arrange(); garbeam@94: } garbeam@94: garbeam@94: void arg@461: resize(Client *c, Bool sizehints, Corner sticky) { arg@123: int bottom = c->y + c->h; arg@123: int right = c->x + c->w; arg@163: XWindowChanges wc; 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@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@465: /* offscreen appearance fixes */ arg@517: if(c->x + c->w < sx) arg@517: c->x = sx; arg@465: if(c->y + c->h < bh) arg@465: c->y = bh; arg@465: if(c->x > sw) arg@465: c->x = sw - c->w; arg@465: if(c->y > sh) arg@465: c->y = sh - c->h; garbeam@75: resizetitle(c); arg@163: wc.x = c->x; arg@163: wc.y = c->y; arg@163: wc.width = c->w; arg@163: wc.height = c->h; arg@164: if(c->w == sw && c->h == sh) arg@164: wc.border_width = 0; arg@164: else arg@502: wc.border_width = BORDERPX; arg@452: XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc); arg@491: configure(c); garbeam@79: XSync(dpy, False); garbeam@18: } garbeam@18: garbeam@76: void arg@500: resizetitle(Client *c) { arg@500: c->tw = textw(c->name); arg@500: if(c->tw > c->w) arg@502: c->tw = c->w + 2 * BORDERPX; arg@502: c->tx = c->x + c->w - c->tw + 2 * BORDERPX; arg@500: c->ty = c->y; arg@500: if(isvisible(c)) arg@500: XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th); arg@500: else arg@500: XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th); arg@500: } arg@500: arg@500: void arg@461: updatesize(Client *c) { 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; arg@550: c->isfixed = (c->maxw && c->minw && c->maxh && c->minh && arg@550: c->maxw == c->minw && c->maxh == c->minh); 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 arg@461: updatetitle(Client *c) { arg@123: char **list = NULL; arg@377: 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) arg@581: 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: { arg@581: strncpy(c->name, *list, sizeof c->name); garbeam@76: XFreeStringList(list); garbeam@76: } garbeam@76: } garbeam@76: XFree(name.value); garbeam@10: } garbeam@10: garbeam@10: void arg@461: unmanage(Client *c) { arg@450: Client *nc; arg@450: arg@472: /* The server grab construct avoids race conditions. */ garbeam@10: XGrabServer(dpy); garbeam@75: XSetErrorHandler(xerrordummy); arg@400: detach(c); arg@448: detachstack(c); arg@400: if(sel == c) { arg@450: for(nc = stack; nc && !isvisible(nc); nc = nc->snext); arg@450: focus(nc); arg@400: } garbeam@18: XUngrabButton(dpy, AnyButton, AnyModifier, c->win); arg@342: XDestroyWindow(dpy, c->twin); arg@178: free(c->tags); garbeam@10: free(c); garbeam@79: XSync(dpy, False); garbeam@74: XSetErrorHandler(xerror); garbeam@10: XUngrabServer(dpy); arg@533: arrange(); garbeam@10: }