implemented setlayout in the way proposed on the ml, split tile() into two functions, a third will follow soon

This commit is contained in:
Anselm R Garbe 2008-03-06 18:53:15 +00:00
parent f7a45ff28b
commit c9170189bf
2 changed files with 109 additions and 103 deletions

View File

@ -13,31 +13,31 @@
/* bar position */ /* bar position */
#define BX sx #define BX sx
#define BY sy #define BY sy
#define BW MW #define BW sw
/* window area */ /* window area */
#define WX 0 #define WX 0
#define WY bh #define WY bh
#define WW sw #define WW sw
#define WH sh #define WH sh - bh
/* master area */ /* master area */
#define MX sx #define MX WX
#define MY sy + bh #define MY WY
#define MW ((int)(((float)sw) * 0.6)) #define MW ((int)(((float)sw) * 0.6))
#define MH sh - bh #define MH WH
/* tile area, might be on a different screen */ /* tile area, might be on a different screen */
#define TX sx + MW #define TX MX + MW
#define TY sy #define TY WY
#define TW sw - MW #define TW WW - MW
#define TH sh #define TH WH
/* monocle area, might be restricted to a specific screen */ /* monocle area, might be restricted to a specific screen */
#define MOX sx #define MOX WX
#define MOY MY #define MOY WY
#define MOW sw #define MOW WW
#define MOH MH #define MOH WH
/* tagging */ /* tagging */
const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
@ -55,10 +55,10 @@ Rule rules[] = {
#define SNAP 32 /* snap pixel */ #define SNAP 32 /* snap pixel */
Layout layouts[] = { Layout layouts[] = {
/* symbol function */ /* symbol function isfloating */
{ "[]=", tile }, /* first entry is default */ { "[]=", tile, False }, /* first entry is default */
{ "><>", floating }, { "><>", floating True },
{ "[M]", monocle }, { "[M]", monocle True },
}; };
/* key definitions */ /* key definitions */

178
dwm.c
View File

@ -1,9 +1,4 @@
/** /**
* TODO
* - treat monocle as floating layout, actually otherwise certain monocled windows don't get raised
* - use WX, WY, WW, WH for window snapping/resizing/mouse
* - MOX, MOY, MOW, MOH should only be used in the case of monocle layout and of n==1 in tiled
* - simplify tile()
* - allow for vstack * - allow for vstack
*/ */
/* See LICENSE file for copyright and license details. /* See LICENSE file for copyright and license details.
@ -67,7 +62,6 @@ typedef struct Client Client;
struct Client { struct Client {
char name[256]; char name[256];
int x, y, w, h; int x, y, w, h;
int rx, ry, rw, rh;
int basew, baseh, incw, inch, maxw, maxh, minw, minh; int basew, baseh, incw, inch, maxw, maxh, minw, minh;
int minax, maxax, minay, maxay; int minax, maxax, minay, maxay;
long flags; long flags;
@ -105,6 +99,7 @@ typedef struct {
typedef struct { typedef struct {
const char *symbol; const char *symbol;
void (*arrange)(void); void (*arrange)(void);
Bool isfloating;
} Layout; } Layout;
typedef struct { typedef struct {
@ -176,6 +171,8 @@ void tag(const char *arg);
unsigned int textnw(const char *text, unsigned int len); unsigned int textnw(const char *text, unsigned int len);
unsigned int textw(const char *text); unsigned int textw(const char *text);
void tile(void); void tile(void);
unsigned int tilemaster(void);
void tilevstack(unsigned int n);
void togglefloating(const char *arg); void togglefloating(const char *arg);
void toggletag(const char *arg); void toggletag(const char *arg);
void toggleview(const char *arg); void toggleview(const char *arg);
@ -213,7 +210,6 @@ void (*handler[LASTEvent]) (XEvent *) = {
[UnmapNotify] = unmapnotify [UnmapNotify] = unmapnotify
}; };
Atom wmatom[WMLast], netatom[NetLast]; Atom wmatom[WMLast], netatom[NetLast];
Bool dozoom = True;
Bool otherwm, readin; Bool otherwm, readin;
Bool running = True; Bool running = True;
Bool *prevtags; Bool *prevtags;
@ -297,7 +293,7 @@ void
ban(Client *c) { ban(Client *c) {
if(c->isbanned) if(c->isbanned)
return; return;
XMoveWindow(dpy, c->win, c->x + 3 * sw, c->y); XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
c->isbanned = True; c->isbanned = True;
} }
@ -428,7 +424,7 @@ configurerequest(XEvent *e) {
if((c = getclient(ev->window))) { if((c = getclient(ev->window))) {
if(ev->value_mask & CWBorderWidth) if(ev->value_mask & CWBorderWidth)
c->border = ev->border_width; c->border = ev->border_width;
if(c->isfixed || c->isfloating || (floating == lt->arrange)) { if(c->isfixed || c->isfloating || lt->isfloating) {
if(ev->value_mask & CWX) if(ev->value_mask & CWX)
c->x = sx + ev->x; c->x = sx + ev->x;
if(ev->value_mask & CWY) if(ev->value_mask & CWY)
@ -637,7 +633,6 @@ void
floating(void) { /* default floating layout */ floating(void) { /* default floating layout */
Client *c; Client *c;
dozoom = False;
for(c = clients; c; c = c->next) for(c = clients; c; c = c->next)
if(isvisible(c)) if(isvisible(c))
resize(c, c->x, c->y, c->w, c->h, True); resize(c, c->x, c->y, c->w, c->h, True);
@ -650,22 +645,11 @@ focus(Client *c) {
if(sel && sel != c) { if(sel && sel != c) {
grabbuttons(sel, False); grabbuttons(sel, False);
XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]); XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
if(lt->arrange == monocle)
resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
} }
if(c) { if(c) {
detachstack(c); detachstack(c);
attachstack(c); attachstack(c);
grabbuttons(c, True); grabbuttons(c, True);
if(lt->arrange == monocle) {
if(sel != c) {
c->rx = c->x;
c->ry = c->y;
c->rw = c->w;
c->rh = c->h;
}
resize(c, MOX, MOY, MOW, MOH, RESIZEHINTS);
}
} }
sel = c; sel = c;
if(c) { if(c) {
@ -987,28 +971,29 @@ manage(Window w, XWindowAttributes *wa) {
c->tags = emallocz(TAGSZ); c->tags = emallocz(TAGSZ);
c->win = w; c->win = w;
c->x = c->rx = wa->x + sx; /* geometry */
c->y = c->ry = wa->y + sy; c->x = wa->x;
c->w = c->rw = wa->width; c->y = wa->y;
c->h = c->rh = wa->height; c->w = wa->width;
c->h = wa->height;
c->oldborder = wa->border_width; c->oldborder = wa->border_width;
if(c->w == sw && c->h == sh) { if(c->w == sw && c->h == sh) {
c->x = sx; c->x = sx;
c->y = sy; c->y = sy;
c->border = wa->border_width; c->border = wa->border_width;
} }
else { else {
if(c->x + c->w + 2 * c->border > sx + sw) if(c->x + c->w + 2 * c->border > WX + WW)
c->x = sx + sw - c->w - 2 * c->border; c->x = WX + WW - c->w - 2 * c->border;
if(c->y + c->h + 2 * c->border > sy + sh) if(c->y + c->h + 2 * c->border > WY + WH)
c->y = sy + sh - c->h - 2 * c->border; c->y = WY + WH - c->h - 2 * c->border;
if(c->x < sx) if(c->x < WX)
c->x = sx; c->x = WX;
if(c->y < sy) if(c->y < WY)
c->y = sy; c->y = WY;
c->border = BORDERPX; c->border = BORDERPX;
} }
wc.border_width = c->border; wc.border_width = c->border;
XConfigureWindow(dpy, w, CWBorderWidth, &wc); XConfigureWindow(dpy, w, CWBorderWidth, &wc);
XSetWindowBorder(dpy, w, dc.norm[ColBorder]); XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
@ -1057,8 +1042,12 @@ maprequest(XEvent *e) {
} }
void void
monocle(void) { monocle(void) {
dozoom = False; Client *c;
for(c = clients; c; c = c->next)
if(isvisible(c))
resize(c, MOX, MOY, MOW, MOH, RESIZEHINTS);
} }
void void
@ -1089,17 +1078,17 @@ movemouse(Client *c) {
XSync(dpy, False); XSync(dpy, False);
nx = ocx + (ev.xmotion.x - x1); nx = ocx + (ev.xmotion.x - x1);
ny = ocy + (ev.xmotion.y - y1); ny = ocy + (ev.xmotion.y - y1);
if(abs(sx - nx) < SNAP) if(abs(WX - nx) < SNAP)
nx = sx; nx = WX;
else if(abs((sx + sw) - (nx + c->w + 2 * c->border)) < SNAP) else if(abs((WX + WW) - (nx + c->w + 2 * c->border)) < SNAP)
nx = sx + sw - c->w - 2 * c->border; nx = WX + WW - c->w - 2 * c->border;
if(abs(sy - ny) < SNAP) if(abs(WY - ny) < SNAP)
ny = sy; ny = WY;
else if(abs((sy + sh) - (ny + c->h + 2 * c->border)) < SNAP) else if(abs((WY + WH) - (ny + c->h + 2 * c->border)) < SNAP)
ny = sy + sh - c->h - 2 * c->border; ny = WY + WH - c->h - 2 * c->border;
if(!c->isfloating && (lt->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP)) if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
togglefloating(NULL); togglefloating(NULL);
if((lt->arrange == floating) || c->isfloating) if((lt->isfloating) || c->isfloating)
resize(c, nx, ny, c->w, c->h, False); resize(c, nx, ny, c->w, c->h, False);
break; break;
} }
@ -1258,9 +1247,9 @@ resizemouse(Client *c) {
nw = 1; nw = 1;
if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0) if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
nh = 1; nh = 1;
if(!c->isfloating && (lt->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP)) if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
togglefloating(NULL); togglefloating(NULL);
if((lt->arrange == floating) || c->isfloating) if((lt->isfloating) || c->isfloating)
resize(c, c->x, c->y, nw, nh, True); resize(c, c->x, c->y, nw, nh, True);
break; break;
} }
@ -1276,9 +1265,9 @@ restack(void) {
drawbar(); drawbar();
if(!sel) if(!sel)
return; return;
if(sel->isfloating || (lt->arrange == floating)) if(sel->isfloating || lt->isfloating)
XRaiseWindow(dpy, sel->win); XRaiseWindow(dpy, sel->win);
if(lt->arrange != floating) { if(!lt->isfloating) {
wc.stack_mode = Below; wc.stack_mode = Below;
wc.sibling = barwin; wc.sibling = barwin;
if(!sel->isfloating) { if(!sel->isfloating) {
@ -1393,6 +1382,7 @@ setclientstate(Client *c, long state) {
void void
setlayout(const char *arg) { setlayout(const char *arg) {
static Layout *revert = 0;
unsigned int i; unsigned int i;
if(!arg) if(!arg)
@ -1402,7 +1392,12 @@ setlayout(const char *arg) {
break; break;
if(i == LENGTH(layouts)) if(i == LENGTH(layouts))
return; return;
lt = &layouts[i]; if(revert && &layouts[i] == lt)
lt = revert;
else {
revert = lt;
lt = &layouts[i];
}
if(sel) if(sel)
arrange(); arrange();
else else
@ -1545,47 +1540,58 @@ textw(const char *text) {
} }
void void
tile(void) { tileresize(Client *c, int x, int y, int w, int h) {
unsigned int i, n, nx, ny, nw, nh, mw, th; resize(c, x, y, w, h, RESIZEHINTS);
if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
/* client doesn't accept size constraints */
resize(c, x, y, w, h, False);
}
unsigned int
tilemaster(void) {
unsigned int n;
Client *c, *mc; Client *c, *mc;
dozoom = True; for(n = 0, mc = c = nexttiled(clients); c; c = nexttiled(c->next))
nx = MX;
ny = MY;
nw = 0;
for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
n++; n++;
if(n == 0)
return 0;
if(n == 1)
tileresize(mc, MOX, MOY, (MOW) - 2 * mc->border, (MOH) - 2 * mc->border);
else
tileresize(mc, MX, MY, (MW) - 2 * mc->border, (MH) - 2 * mc->border);
return n - 1;
}
/* window geoms */ void
mw = (n == 1) ? MOW : MW; tilevstack(unsigned int n) {
th = (n > 1) ? TH / (n - 1) : 0; int i, y, h;
if(n > 1 && th < bh) Client *c;
th = TH;
for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next)) { if(n == 0)
if(i == 0) { /* master */ return;
nw = mw - 2 * c->border;
nh = MH - 2 * c->border; y = TY;
} h = (TH) / n;
else { /* tile window */ if(h < bh)
if(i == 1) { h = TH;
ny = TY;
nx = TX; for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++)
nw = TW - 2 * c->border; if(i > 0) {
} if(i > 1 && i == n) /* remainder */
if(i + 1 == n) /* remainder */ tileresize(c, TX, y, (TW) - 2 * c->border,
nh = (TY + TH) - ny - 2 * c->border; ((TY) + (TH)) - y - 2 * c->border);
else else
nh = th - 2 * c->border; tileresize(c, TX, y, (TW) - 2 * c->border,
h - 2 * c->border);
if(h != TH)
y = c->y + c->h + 2 * c->border;
} }
resize(c, nx, ny, nw, nh, RESIZEHINTS); }
if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
/* client doesn't accept size constraints */ void
resize(c, nx, ny, nw, nh, False); tile(void) {
if(n > 1 && th != TH) tilevstack(tilemaster());
ny = c->y + c->h + 2 * c->border;
i++;
}
} }
void void
@ -1796,7 +1802,7 @@ void
zoom(const char *arg) { zoom(const char *arg) {
Client *c = sel; Client *c = sel;
if(!sel || !dozoom || sel->isfloating) if(!sel || lt->isfloating || sel->isfloating)
return; return;
if(c == nexttiled(clients)) if(c == nexttiled(clients))
if(!(c = nexttiled(c->next))) if(!(c = nexttiled(c->next)))