mirror of
https://github.com/tmux/tmux.git
synced 2026-05-30 22:26:18 +00:00
Bring over some of layout and positioning code for floating panes, by
Michael Grant.
This commit is contained in:
176
layout-custom.c
176
layout-custom.c
@@ -27,10 +27,11 @@ static struct layout_cell *layout_find_bottomright(struct layout_cell *);
|
||||
static u_short layout_checksum(const char *);
|
||||
static int layout_append(struct layout_cell *, char *,
|
||||
size_t);
|
||||
static struct layout_cell *layout_construct(struct layout_cell *,
|
||||
const char **);
|
||||
static int layout_construct(struct layout_cell *,
|
||||
const char **, struct layout_cell **,
|
||||
struct layout_cell **);
|
||||
static void layout_assign(struct window_pane **,
|
||||
struct layout_cell *);
|
||||
struct layout_cell *, int);
|
||||
|
||||
/* Find the bottom-right cell. */
|
||||
static struct layout_cell *
|
||||
@@ -58,14 +59,30 @@ layout_checksum(const char *layout)
|
||||
|
||||
/* Dump layout as a string. */
|
||||
char *
|
||||
layout_dump(__unused struct window *w, struct layout_cell *root)
|
||||
layout_dump(struct window *w, struct layout_cell *root)
|
||||
{
|
||||
char layout[8192], *out;
|
||||
char layout[8192], *out;
|
||||
int bracket = 0;
|
||||
struct window_pane *wp;
|
||||
|
||||
*layout = '\0';
|
||||
if (layout_append(root, layout, sizeof layout) != 0)
|
||||
return (NULL);
|
||||
|
||||
TAILQ_FOREACH(wp, &w->z_index, zentry) {
|
||||
if (~wp->flags & PANE_FLOATING)
|
||||
break;
|
||||
if (!bracket) {
|
||||
strlcat(layout, "<", sizeof layout);
|
||||
bracket = 1;
|
||||
}
|
||||
if (layout_append(wp->layout_cell, layout, sizeof layout) != 0)
|
||||
return (NULL);
|
||||
strlcat(layout, ",", sizeof layout);
|
||||
}
|
||||
if (bracket)
|
||||
layout[strlen(layout) - 1] = '>';
|
||||
|
||||
xasprintf(&out, "%04hx,%s", layout_checksum(layout), layout);
|
||||
return (out);
|
||||
}
|
||||
@@ -81,12 +98,13 @@ layout_append(struct layout_cell *lc, char *buf, size_t len)
|
||||
|
||||
if (len == 0)
|
||||
return (-1);
|
||||
|
||||
if (lc == NULL)
|
||||
return (0);
|
||||
if (lc->wp != NULL) {
|
||||
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u,%u",
|
||||
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%d,%d,%u",
|
||||
lc->sx, lc->sy, lc->xoff, lc->yoff, lc->wp->id);
|
||||
} else {
|
||||
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u",
|
||||
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%d,%d",
|
||||
lc->sx, lc->sy, lc->xoff, lc->yoff);
|
||||
}
|
||||
if (tmplen > (sizeof tmp) - 1)
|
||||
@@ -109,6 +127,7 @@ layout_append(struct layout_cell *lc, char *buf, size_t len)
|
||||
}
|
||||
buf[strlen(buf) - 1] = brackets[0];
|
||||
break;
|
||||
case LAYOUT_FLOATING:
|
||||
case LAYOUT_WINDOWPANE:
|
||||
break;
|
||||
}
|
||||
@@ -124,6 +143,7 @@ layout_check(struct layout_cell *lc)
|
||||
u_int n = 0;
|
||||
|
||||
switch (lc->type) {
|
||||
case LAYOUT_FLOATING:
|
||||
case LAYOUT_WINDOWPANE:
|
||||
break;
|
||||
case LAYOUT_LEFTRIGHT:
|
||||
@@ -156,7 +176,7 @@ layout_check(struct layout_cell *lc)
|
||||
int
|
||||
layout_parse(struct window *w, const char *layout, char **cause)
|
||||
{
|
||||
struct layout_cell *lc, *lcchild;
|
||||
struct layout_cell *lcchild, *tiled_lc = NULL, *floating_lc = NULL;
|
||||
struct window_pane *wp;
|
||||
u_int npanes, ncells, sx = 0, sy = 0;
|
||||
u_short csum;
|
||||
@@ -174,20 +194,27 @@ layout_parse(struct window *w, const char *layout, char **cause)
|
||||
}
|
||||
|
||||
/* Build the layout. */
|
||||
lc = layout_construct(NULL, &layout);
|
||||
if (lc == NULL) {
|
||||
if (layout_construct(NULL, &layout, &tiled_lc, &floating_lc) != 0) {
|
||||
*cause = xstrdup("invalid layout");
|
||||
return (-1);
|
||||
}
|
||||
if (tiled_lc == NULL) {
|
||||
/* A stub layout cell for an empty window. */
|
||||
tiled_lc = layout_create_cell(NULL);
|
||||
tiled_lc->type = LAYOUT_LEFTRIGHT;
|
||||
layout_set_size(tiled_lc, w->sx, w->sy, 0, 0);
|
||||
}
|
||||
if (*layout != '\0') {
|
||||
*cause = xstrdup("invalid layout");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Check this window will fit into the layout. */
|
||||
npanes = window_count_panes(w, 1);
|
||||
for (;;) {
|
||||
npanes = window_count_panes(w);
|
||||
ncells = layout_count_cells(lc);
|
||||
ncells = layout_count_cells(tiled_lc);
|
||||
if (floating_lc != NULL)
|
||||
ncells += layout_count_cells(floating_lc);
|
||||
if (npanes > ncells) {
|
||||
xasprintf(cause, "have %u panes but need %u", npanes,
|
||||
ncells);
|
||||
@@ -196,9 +223,17 @@ layout_parse(struct window *w, const char *layout, char **cause)
|
||||
if (npanes == ncells)
|
||||
break;
|
||||
|
||||
/* Fewer panes than cells - close the bottom right. */
|
||||
lcchild = layout_find_bottomright(lc);
|
||||
layout_destroy_cell(w, lcchild, &lc);
|
||||
/*
|
||||
* Fewer panes than cells - close floating panes first
|
||||
* then close the bottom right until none remain.
|
||||
*/
|
||||
if (floating_lc != NULL && !TAILQ_EMPTY(&floating_lc->cells)) {
|
||||
lcchild = TAILQ_FIRST(&floating_lc->cells);
|
||||
layout_destroy_cell(w, lcchild, &floating_lc);
|
||||
} else {
|
||||
lcchild = layout_find_bottomright(tiled_lc);
|
||||
layout_destroy_cell(w, lcchild, &tiled_lc);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -206,91 +241,108 @@ layout_parse(struct window *w, const char *layout, char **cause)
|
||||
* an incorrect top cell size - if it is larger than the top child then
|
||||
* correct that (if this is still wrong the check code will catch it).
|
||||
*/
|
||||
switch (lc->type) {
|
||||
|
||||
switch (tiled_lc->type) {
|
||||
case LAYOUT_WINDOWPANE:
|
||||
break;
|
||||
case LAYOUT_LEFTRIGHT:
|
||||
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
|
||||
TAILQ_FOREACH(lcchild, &tiled_lc->cells, entry) {
|
||||
sy = lcchild->sy + 1;
|
||||
sx += lcchild->sx + 1;
|
||||
}
|
||||
break;
|
||||
case LAYOUT_TOPBOTTOM:
|
||||
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
|
||||
TAILQ_FOREACH(lcchild, &tiled_lc->cells, entry) {
|
||||
sx = lcchild->sx + 1;
|
||||
sy += lcchild->sy + 1;
|
||||
}
|
||||
break;
|
||||
case LAYOUT_FLOATING:
|
||||
*cause = xstrdup("invalid layout");
|
||||
goto fail;
|
||||
}
|
||||
if (lc->type != LAYOUT_WINDOWPANE && (lc->sx != sx || lc->sy != sy)) {
|
||||
log_debug("fix layout %u,%u to %u,%u", lc->sx, lc->sy, sx,sy);
|
||||
layout_print_cell(lc, __func__, 0);
|
||||
lc->sx = sx - 1; lc->sy = sy - 1;
|
||||
if (tiled_lc->type != LAYOUT_WINDOWPANE &&
|
||||
(tiled_lc->sx != sx || tiled_lc->sy != sy)) {
|
||||
layout_print_cell(tiled_lc, __func__, 0);
|
||||
tiled_lc->sx = sx - 1; tiled_lc->sy = sy - 1;
|
||||
}
|
||||
|
||||
/* Check the new layout. */
|
||||
if (!layout_check(lc)) {
|
||||
if (!layout_check(tiled_lc)) {
|
||||
*cause = xstrdup("size mismatch after applying layout");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Resize to the layout size. */
|
||||
window_resize(w, lc->sx, lc->sy, -1, -1);
|
||||
/* Resize window to the layout size. */
|
||||
if (sx != 0 && sy != 0)
|
||||
window_resize(w, tiled_lc->sx, tiled_lc->sy, -1, -1);
|
||||
|
||||
/* Destroy the old layout and swap to the new. */
|
||||
layout_free_cell(w->layout_root);
|
||||
w->layout_root = lc;
|
||||
w->layout_root = tiled_lc;
|
||||
|
||||
/* Assign the panes into the cells. */
|
||||
wp = TAILQ_FIRST(&w->panes);
|
||||
layout_assign(&wp, lc);
|
||||
if (tiled_lc != NULL)
|
||||
layout_assign(&wp, tiled_lc, 0);
|
||||
if (floating_lc != NULL)
|
||||
layout_assign(&wp, floating_lc, PANE_FLOATING);
|
||||
|
||||
/* Update pane offsets and sizes. */
|
||||
layout_fix_offsets(w);
|
||||
layout_fix_panes(w, NULL);
|
||||
recalculate_sizes();
|
||||
layout_print_cell(tiled_lc, __func__, 0);
|
||||
|
||||
layout_print_cell(lc, __func__, 0);
|
||||
/* Free the floating layout cell, no longer needed. */
|
||||
if (floating_lc != NULL)
|
||||
layout_free_cell(floating_lc);
|
||||
|
||||
notify_window("window-layout-changed", w);
|
||||
|
||||
return (0);
|
||||
|
||||
fail:
|
||||
layout_free_cell(lc);
|
||||
layout_free_cell(tiled_lc);
|
||||
layout_free_cell(floating_lc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Assign panes into cells. */
|
||||
static void
|
||||
layout_assign(struct window_pane **wp, struct layout_cell *lc)
|
||||
layout_assign(struct window_pane **wp, struct layout_cell *lc, int flags)
|
||||
{
|
||||
struct layout_cell *lcchild;
|
||||
|
||||
if (lc == NULL)
|
||||
return;
|
||||
|
||||
switch (lc->type) {
|
||||
case LAYOUT_WINDOWPANE:
|
||||
layout_make_leaf(lc, *wp);
|
||||
(*wp)->flags |= flags;
|
||||
*wp = TAILQ_NEXT(*wp, entry);
|
||||
return;
|
||||
case LAYOUT_LEFTRIGHT:
|
||||
case LAYOUT_TOPBOTTOM:
|
||||
case LAYOUT_FLOATING:
|
||||
TAILQ_FOREACH(lcchild, &lc->cells, entry)
|
||||
layout_assign(wp, lcchild);
|
||||
layout_assign(wp, lcchild, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Construct a cell from all or part of a layout tree. */
|
||||
static struct layout_cell *
|
||||
layout_construct(struct layout_cell *lcparent, const char **layout)
|
||||
layout_construct_cell(struct layout_cell *lcparent, const char **layout)
|
||||
{
|
||||
struct layout_cell *lc, *lcchild;
|
||||
u_int sx, sy, xoff, yoff;
|
||||
struct layout_cell *lc;
|
||||
u_int sx, sy;
|
||||
int xoff, yoff;
|
||||
const char *saved;
|
||||
|
||||
if (!isdigit((u_char) **layout))
|
||||
return (NULL);
|
||||
if (sscanf(*layout, "%ux%u,%u,%u", &sx, &sy, &xoff, &yoff) != 4)
|
||||
if (sscanf(*layout, "%ux%u,%d,%d", &sx, &sy, &xoff, &yoff) != 4)
|
||||
return (NULL);
|
||||
|
||||
while (isdigit((u_char) **layout))
|
||||
@@ -325,17 +377,41 @@ layout_construct(struct layout_cell *lcparent, const char **layout)
|
||||
lc->xoff = xoff;
|
||||
lc->yoff = yoff;
|
||||
|
||||
return (lc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a character string layout, recursively construct cells.
|
||||
* Possible return values:
|
||||
* lc LAYOUT_WINDOWPANE, no children
|
||||
* lc LAYOUT_LEFTRIGHT or LAYOUT_TOPBOTTOM, with children
|
||||
* floating_lc LAYOUT_FLOATING, with children
|
||||
*/
|
||||
static int
|
||||
layout_construct(struct layout_cell *lcparent, const char **layout,
|
||||
struct layout_cell **lc, struct layout_cell **floating_lc)
|
||||
{
|
||||
struct layout_cell *lcchild, *saved_lc;
|
||||
|
||||
*lc = layout_construct_cell(lcparent, layout);
|
||||
|
||||
switch (**layout) {
|
||||
case ',':
|
||||
case '}':
|
||||
case ']':
|
||||
case '>':
|
||||
case '\0':
|
||||
return (lc);
|
||||
return (0);
|
||||
case '{':
|
||||
lc->type = LAYOUT_LEFTRIGHT;
|
||||
(*lc)->type = LAYOUT_LEFTRIGHT;
|
||||
break;
|
||||
case '[':
|
||||
lc->type = LAYOUT_TOPBOTTOM;
|
||||
(*lc)->type = LAYOUT_TOPBOTTOM;
|
||||
break;
|
||||
case '<':
|
||||
saved_lc = *lc;
|
||||
*lc = layout_create_cell(lcparent);
|
||||
(*lc)->type = LAYOUT_FLOATING;
|
||||
break;
|
||||
default:
|
||||
goto fail;
|
||||
@@ -343,13 +419,12 @@ layout_construct(struct layout_cell *lcparent, const char **layout)
|
||||
|
||||
do {
|
||||
(*layout)++;
|
||||
lcchild = layout_construct(lc, layout);
|
||||
if (lcchild == NULL)
|
||||
if (layout_construct(*lc, layout, &lcchild, floating_lc) != 0)
|
||||
goto fail;
|
||||
TAILQ_INSERT_TAIL(&lc->cells, lcchild, entry);
|
||||
TAILQ_INSERT_TAIL(&(*lc)->cells, lcchild, entry);
|
||||
} while (**layout == ',');
|
||||
|
||||
switch (lc->type) {
|
||||
switch ((*lc)->type) {
|
||||
case LAYOUT_LEFTRIGHT:
|
||||
if (**layout != '}')
|
||||
goto fail;
|
||||
@@ -358,14 +433,21 @@ layout_construct(struct layout_cell *lcparent, const char **layout)
|
||||
if (**layout != ']')
|
||||
goto fail;
|
||||
break;
|
||||
case LAYOUT_FLOATING:
|
||||
if (**layout != '>')
|
||||
goto fail;
|
||||
*floating_lc = *lc;
|
||||
*lc = saved_lc;
|
||||
break;
|
||||
default:
|
||||
goto fail;
|
||||
}
|
||||
(*layout)++;
|
||||
|
||||
return (lc);
|
||||
return (0);
|
||||
|
||||
fail:
|
||||
layout_free_cell(lc);
|
||||
return (NULL);
|
||||
layout_free_cell(*lc);
|
||||
layout_free_cell(*floating_lc);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user