X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=client.c;h=f4ed1a067f860bc95c215919cc82a35a7d2cdbf2;hb=dc5c070c44d9af434405c8d6538abd06299d3298;hp=a5141ea64b7ec6b394ed1d555bee97dda7aff063;hpb=439e15d09f6fa9271d3b49ef97194f0c80ebe161;p=dwm.git diff --git a/client.c b/client.c index a5141ea..f4ed1a0 100644 --- a/client.c +++ b/client.c @@ -2,23 +2,361 @@ * (C)opyright MMVI Anselm R. Garbe * See LICENSE file for license details. */ - +#include "dwm.h" +#include #include #include +#include -#include "util.h" -#include "wm.h" +/* static functions */ static void -update_client_name(Client *c) +resizetitle(Client *c) { - XTextProperty name; + int i; + + c->tw = 0; + for(i = 0; i < TLast; i++) + if(c->tags[i]) + c->tw += textw(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; + if(c->tags[tsel]) + XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th); + else + XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, 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(sel->ismax) + togglemax(NULL); + + if(!(c = getnext(sel->next))) + c = getnext(clients); + if(c) { + higher(c); + focus(c); + } +} + +void +focusprev(Arg *arg) +{ + Client *c; + + if(!sel) + return; + + if(sel->ismax) + togglemax(NULL); + + if(!(c = getprev(sel->prev))) { + for(c = clients; c && c->next; c = c->next); + c = getprev(c); + } + if(c) { + 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) { + default: + break; + 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; + } + + switch (c->grav) { + default: + break; + 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; + } + + 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 & PROTODELWIN) + 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; + Window trans; + XSetWindowAttributes twa; + + c = emallocz(sizeof(Client)); + c->win = w; + c->x = c->tx = wa->x; + c->y = c->ty = wa->y; + c->w = c->tw = wa->width; + c->h = wa->height; + c->th = bh; + + c->border = 0; + setsize(c); + + if(c->h != sh && c->y < bh) + c->y = c->ty = bh; + + c->proto = getproto(c->win); + XSelectInput(dpy, c->win, + StructureNotifyMask | PropertyChangeMask | EnterWindowMask); + XGetTransientForHint(dpy, c->win, &trans); + twa.override_redirect = 1; + twa.background_pixmap = ParentRelative; + twa.event_mask = ExposureMask | EnterWindowMask; + + 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); + + if(clients) + clients->prev = c; + c->next = clients; + clients = c; + + XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK, + GrabModeAsync, GrabModeSync, None, None); + XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK, + GrabModeAsync, GrabModeSync, None, None); + XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK, + GrabModeAsync, GrabModeSync, None, None); + + settags(c); + if(!c->isfloat) + c->isfloat = trans + || (c->maxw && c->minw && + c->maxw == c->minw && c->maxh == c->minh); + settitle(c); + arrange(NULL); + + /* mapping the window now prevents flicker */ + XMapRaised(dpy, c->win); + XMapRaised(dpy, c->title); + if(c->tags[tsel]) + focus(c); +} + +void +resize(Client *c, Bool sizehints, Corner sticky) +{ + int bottom = c->y + c->h; + int right = c->x + c->w; + /*XConfigureEvent e;*/ + XWindowChanges wc; + + if(sizehints) { + if(c->incw) + c->w -= (c->w - c->basew) % c->incw; + if(c->inch) + c->h -= (c->h - c->baseh) % c->inch; + if(c->minw && c->w < c->minw) + c->w = c->minw; + if(c->minh && c->h < c->minh) + c->h = c->minh; + if(c->maxw && c->w > c->maxw) + c->w = c->maxw; + if(c->maxh && c->h > c->maxh) + c->h = c->maxh; + } + if(c->x > right) /* might happen on restart */ + c->x = right - c->w; + if(c->y > bottom) + c->y = bottom - c->h; + if(sticky == TopRight || sticky == BotRight) + c->x = right - c->w; + if(sticky == BotLeft || sticky == BotRight) + c->y = bottom - c->h; + + resizetitle(c); + wc.x = c->x; + wc.y = c->y; + wc.width = c->w; + wc.height = c->h; + if(c->w == sw && c->h == sh) + wc.border_width = 0; + else + wc.border_width = 1; + XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); + XSync(dpy, False); +} + +void +setsize(Client *c) +{ + long msize; + XSizeHints size; + + if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags) + size.flags = PSize; + c->flags = size.flags; + if(c->flags & PBaseSize) { + c->basew = size.base_width; + c->baseh = size.base_height; + } + else + c->basew = c->baseh = 0; + if(c->flags & PResizeInc) { + c->incw = size.width_inc; + c->inch = size.height_inc; + } + else + c->incw = c->inch = 0; + if(c->flags & PMaxSize) { + c->maxw = size.max_width; + c->maxh = size.max_height; + } + else + c->maxw = c->maxh = 0; + if(c->flags & PMinSize) { + c->minw = size.min_width; + c->minh = size.min_height; + } + else + c->minw = c->minh = 0; + if(c->flags & PWinGravity) + c->grav = size.win_gravity; + else + c->grav = NorthWestGravity; +} + +void +settitle(Client *c) +{ + char **list = NULL; int n; - char **list = 0; + XTextProperty name; name.nitems = 0; c->name[0] = 0; - XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]); + XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]); if(!name.nitems) XGetWMName(dpy, c->win, &name); if(!name.nitems) @@ -34,56 +372,96 @@ update_client_name(Client *c) } } XFree(name.value); + resizetitle(c); } -Client * -create_client(Window w, XWindowAttributes *wa) +void +togglemax(Arg *arg) { - Client *c; - XSetWindowAttributes twa; - long msize; + int ox, oy, ow, oh; + XEvent ev; - c = emallocz(sizeof(Client)); - c->win = w; - c->r[RFloat].x = wa->x; - c->r[RFloat].y = wa->y; - c->r[RFloat].width = wa->width; - c->r[RFloat].height = wa->height; - c->border = wa->border_width; - XSetWindowBorderWidth(dpy, c->win, 0); - c->proto = win_proto(c->win); - XGetTransientForHint(dpy, c->win, &c->trans); - if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags) - c->size.flags = PSize; - c->fixedsize = - (c->size.flags & PMinSize && c->size.flags & PMaxSize - && c->size.min_width == c->size.max_width - && c->size.min_height == c->size.max_height); - XAddToSaveSet(dpy, c->win); - update_client_name(c); - twa.override_redirect = 1; - twa.background_pixmap = ParentRelative; - twa.event_mask = ExposureMask; + if(!sel) + return; - c->title = XCreateWindow(dpy, root, c->r[RFloat].x, c->r[RFloat].y, - c->r[RFloat].width, barrect.height, 0, - DefaultDepth(dpy, screen), CopyFromParent, - DefaultVisual(dpy, screen), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); - XFlush(dpy); + if((sel->ismax = !sel->ismax)) { + ox = sel->x; + oy = sel->y; + ow = sel->w; + oh = sel->h; + sel->x = sx; + sel->y = sy + bh; + sel->w = sw - 2; + sel->h = sh - 2 - bh; -#if 0 - for(t=&client, i=0; *t; t=&(*t)->next, i++); - c->next = *t; /* *t == nil */ - *t = c; -#endif - return c; + higher(sel); + resize(sel, False, TopLeft); + + sel->x = ox; + sel->y = oy; + sel->w = ow; + sel->h = oh; + } + else + resize(sel, False, TopLeft); + while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); } void -manage(Client *c) +unmanage(Client *c) { - XMapRaised(dpy, c->win); - XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); - XFlush(dpy); + XGrabServer(dpy); + XSetErrorHandler(xerrordummy); + + XUngrabButton(dpy, AnyButton, AnyModifier, c->win); + XDestroyWindow(dpy, c->title); + + if(c->prev) + c->prev->next = c->next; + if(c->next) + c->next->prev = c->prev; + if(c == clients) + clients = c->next; + if(sel == c) { + sel = getnext(c->next); + if(!sel) + sel = getprev(c->prev); + if(!sel) + sel = clients; + } + free(c); + + XSync(dpy, False); + XSetErrorHandler(xerror); + XUngrabServer(dpy); + arrange(NULL); + if(sel) + focus(sel); +} + +void +zoom(Arg *arg) +{ + Client *c; + + if(!sel) + return; + + if(sel == getnext(clients) && sel->next) { + if((c = getnext(sel->next))) + sel = c; + } + + /* pop */ + if(sel->prev) + sel->prev->next = sel->next; + if(sel->next) + sel->next->prev = sel->prev; + sel->prev = NULL; + if(clients) + clients->prev = sel; + sel->next = clients; + clients = sel; + arrange(NULL); + focus(sel); }