2010-06-29 03:30:13 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2010-06-29 03:30:13 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2016-07-15 09:27:35 +00:00
|
|
|
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 void layout_assign(struct window_pane **,
|
|
|
|
struct layout_cell *);
|
2010-06-29 03:30:13 +00:00
|
|
|
|
2011-06-05 10:53:05 +00:00
|
|
|
/* Find the bottom-right cell. */
|
2016-07-15 09:27:35 +00:00
|
|
|
static struct layout_cell *
|
2011-06-05 10:53:05 +00:00
|
|
|
layout_find_bottomright(struct layout_cell *lc)
|
|
|
|
{
|
|
|
|
if (lc->type == LAYOUT_WINDOWPANE)
|
|
|
|
return (lc);
|
|
|
|
lc = TAILQ_LAST(&lc->cells, layout_cells);
|
|
|
|
return (layout_find_bottomright(lc));
|
|
|
|
}
|
|
|
|
|
2010-06-29 03:30:13 +00:00
|
|
|
/* Calculate layout checksum. */
|
2016-07-15 09:27:35 +00:00
|
|
|
static u_short
|
2010-06-29 03:30:13 +00:00
|
|
|
layout_checksum(const char *layout)
|
|
|
|
{
|
|
|
|
u_short csum;
|
|
|
|
|
|
|
|
csum = 0;
|
|
|
|
for (; *layout != '\0'; layout++) {
|
|
|
|
csum = (csum >> 1) + ((csum & 1) << 15);
|
|
|
|
csum += *layout;
|
|
|
|
}
|
|
|
|
return (csum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump layout as a string. */
|
|
|
|
char *
|
2015-04-21 22:38:49 +00:00
|
|
|
layout_dump(struct layout_cell *root)
|
2010-06-29 03:30:13 +00:00
|
|
|
{
|
|
|
|
char layout[BUFSIZ], *out;
|
|
|
|
|
|
|
|
*layout = '\0';
|
2015-04-21 22:38:49 +00:00
|
|
|
if (layout_append(root, layout, sizeof layout) != 0)
|
2010-06-29 03:30:13 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2016-07-15 09:27:35 +00:00
|
|
|
xasprintf(&out, "%04hx,%s", layout_checksum(layout), layout);
|
2010-06-29 03:30:13 +00:00
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append information for a single cell. */
|
2016-07-15 09:27:35 +00:00
|
|
|
static int
|
2010-06-29 03:30:13 +00:00
|
|
|
layout_append(struct layout_cell *lc, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct layout_cell *lcchild;
|
|
|
|
char tmp[64];
|
|
|
|
size_t tmplen;
|
|
|
|
const char *brackets = "][";
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return (-1);
|
|
|
|
|
2012-01-30 20:57:02 +00:00
|
|
|
if (lc->wp != NULL) {
|
|
|
|
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u,%u",
|
|
|
|
lc->sx, lc->sy, lc->xoff, lc->yoff, lc->wp->id);
|
|
|
|
} else {
|
|
|
|
tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u",
|
|
|
|
lc->sx, lc->sy, lc->xoff, lc->yoff);
|
|
|
|
}
|
2010-06-29 03:30:13 +00:00
|
|
|
if (tmplen > (sizeof tmp) - 1)
|
|
|
|
return (-1);
|
|
|
|
if (strlcat(buf, tmp, len) >= len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
switch (lc->type) {
|
|
|
|
case LAYOUT_LEFTRIGHT:
|
|
|
|
brackets = "}{";
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case LAYOUT_TOPBOTTOM:
|
|
|
|
if (strlcat(buf, &brackets[1], len) >= len)
|
|
|
|
return (-1);
|
|
|
|
TAILQ_FOREACH(lcchild, &lc->cells, entry) {
|
|
|
|
if (layout_append(lcchild, buf, len) != 0)
|
|
|
|
return (-1);
|
|
|
|
if (strlcat(buf, ",", len) >= len)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
buf[strlen(buf) - 1] = brackets[0];
|
|
|
|
break;
|
|
|
|
case LAYOUT_WINDOWPANE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a layout string and arrange window as layout. */
|
|
|
|
int
|
|
|
|
layout_parse(struct window *w, const char *layout)
|
|
|
|
{
|
|
|
|
struct layout_cell *lc, *lcchild;
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int npanes, ncells, sx, sy;
|
|
|
|
u_short csum;
|
|
|
|
|
|
|
|
/* Check validity. */
|
|
|
|
if (sscanf(layout, "%hx,", &csum) != 1)
|
|
|
|
return (-1);
|
|
|
|
layout += 5;
|
|
|
|
if (csum != layout_checksum(layout))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Build the layout. */
|
|
|
|
lc = layout_construct(NULL, &layout);
|
|
|
|
if (lc == NULL)
|
|
|
|
return (-1);
|
|
|
|
if (*layout != '\0')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Check this window will fit into the layout. */
|
|
|
|
for (;;) {
|
|
|
|
npanes = window_count_panes(w);
|
|
|
|
ncells = layout_count_cells(lc);
|
|
|
|
if (npanes > ncells)
|
|
|
|
goto fail;
|
|
|
|
if (npanes == ncells)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Fewer panes than cells - close the bottom right. */
|
|
|
|
lcchild = layout_find_bottomright(lc);
|
2016-08-03 09:07:02 +00:00
|
|
|
layout_destroy_cell(w, lcchild, &lc);
|
2010-06-29 03:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the old window size and resize to the layout size. */
|
|
|
|
sx = w->sx; sy = w->sy;
|
|
|
|
window_resize(w, lc->sx, lc->sy);
|
|
|
|
|
|
|
|
/* Destroy the old layout and swap to the new. */
|
|
|
|
layout_free_cell(w->layout_root);
|
|
|
|
w->layout_root = lc;
|
|
|
|
|
|
|
|
/* Assign the panes into the cells. */
|
|
|
|
wp = TAILQ_FIRST(&w->panes);
|
|
|
|
layout_assign(&wp, lc);
|
|
|
|
|
|
|
|
/* Update pane offsets and sizes. */
|
|
|
|
layout_fix_offsets(lc);
|
Support for windows larger than the client.
This adds two new options, window-size and default-size, and a new
command, resize-window.
The force-width and force-height options, and the session_width and
session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and
manual means that it does not automatically resize
windows. aggressive-resize modifies the choice of session for largest
and smallest as it did before.
If a window is in a session attached to a client that is too small,
only part of the window is shown. tmux attempts to keep the cursor
visible, so the part of the window displayed is changed as the cursor
moves (with a small delay, to try and avoid excess redrawing when
applications redraw status lines or similar that are not currently
visible).
Drawing windows which are larger than the client is not as efficient
as those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If
it is used, the window-size option is automatically set to manual for
the window (undo this with "setw -u window-size"). resize-window works
in a similar way to resize-pane (-U -D -L -R -x -y flags) but also has
-a and -A flags. -a sets the window to the size of the smallest client
(what it would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use
resize-width -x or -y.
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications
to complain and higher memory use if you make a window that big. The
minimum size is the size required for the current layout including
borders.
This change allows some code improvements, most notably that since
windows can now never be cropped, that code can be removed from the
layout code, and since panes can now never be outside the size of the
window, window_pane_visible can be removed.
2018-08-20 14:22:14 +00:00
|
|
|
layout_fix_panes(w);
|
2010-06-29 03:30:13 +00:00
|
|
|
|
|
|
|
/* Then resize the layout back to the original window size. */
|
|
|
|
layout_resize(w, sx, sy);
|
|
|
|
window_resize(w, sx, sy);
|
|
|
|
|
|
|
|
layout_print_cell(lc, __func__, 0);
|
|
|
|
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_window("window-layout-changed", w);
|
2012-03-17 22:35:09 +00:00
|
|
|
|
2010-06-29 03:30:13 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
layout_free_cell(lc);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign panes into cells. */
|
2016-07-15 09:27:35 +00:00
|
|
|
static void
|
2010-06-29 03:30:13 +00:00
|
|
|
layout_assign(struct window_pane **wp, struct layout_cell *lc)
|
|
|
|
{
|
|
|
|
struct layout_cell *lcchild;
|
|
|
|
|
|
|
|
switch (lc->type) {
|
|
|
|
case LAYOUT_WINDOWPANE:
|
|
|
|
layout_make_leaf(lc, *wp);
|
|
|
|
*wp = TAILQ_NEXT(*wp, entry);
|
|
|
|
return;
|
|
|
|
case LAYOUT_LEFTRIGHT:
|
|
|
|
case LAYOUT_TOPBOTTOM:
|
|
|
|
TAILQ_FOREACH(lcchild, &lc->cells, entry)
|
|
|
|
layout_assign(wp, lcchild);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct a cell from all or part of a layout tree. */
|
2016-07-15 09:27:35 +00:00
|
|
|
static struct layout_cell *
|
2010-06-29 03:30:13 +00:00
|
|
|
layout_construct(struct layout_cell *lcparent, const char **layout)
|
|
|
|
{
|
|
|
|
struct layout_cell *lc, *lcchild;
|
|
|
|
u_int sx, sy, xoff, yoff;
|
2013-03-25 11:35:30 +00:00
|
|
|
const char *saved;
|
2010-06-29 03:30:13 +00:00
|
|
|
|
|
|
|
if (!isdigit((u_char) **layout))
|
|
|
|
return (NULL);
|
2013-03-25 11:35:30 +00:00
|
|
|
if (sscanf(*layout, "%ux%u,%u,%u", &sx, &sy, &xoff, &yoff) != 4)
|
2010-06-29 03:30:13 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
while (isdigit((u_char) **layout))
|
|
|
|
(*layout)++;
|
|
|
|
if (**layout != 'x')
|
|
|
|
return (NULL);
|
|
|
|
(*layout)++;
|
|
|
|
while (isdigit((u_char) **layout))
|
|
|
|
(*layout)++;
|
|
|
|
if (**layout != ',')
|
|
|
|
return (NULL);
|
|
|
|
(*layout)++;
|
|
|
|
while (isdigit((u_char) **layout))
|
|
|
|
(*layout)++;
|
|
|
|
if (**layout != ',')
|
|
|
|
return (NULL);
|
|
|
|
(*layout)++;
|
|
|
|
while (isdigit((u_char) **layout))
|
|
|
|
(*layout)++;
|
2012-01-30 20:57:02 +00:00
|
|
|
if (**layout == ',') {
|
2013-03-25 11:35:30 +00:00
|
|
|
saved = *layout;
|
2012-01-30 20:57:02 +00:00
|
|
|
(*layout)++;
|
|
|
|
while (isdigit((u_char) **layout))
|
|
|
|
(*layout)++;
|
2013-03-25 11:35:30 +00:00
|
|
|
if (**layout == 'x')
|
|
|
|
*layout = saved;
|
2012-01-30 20:57:02 +00:00
|
|
|
}
|
2010-06-29 03:30:13 +00:00
|
|
|
|
|
|
|
lc = layout_create_cell(lcparent);
|
|
|
|
lc->sx = sx;
|
|
|
|
lc->sy = sy;
|
|
|
|
lc->xoff = xoff;
|
|
|
|
lc->yoff = yoff;
|
|
|
|
|
|
|
|
switch (**layout) {
|
|
|
|
case ',':
|
|
|
|
case '}':
|
|
|
|
case ']':
|
|
|
|
case '\0':
|
|
|
|
return (lc);
|
|
|
|
case '{':
|
|
|
|
lc->type = LAYOUT_LEFTRIGHT;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
lc->type = LAYOUT_TOPBOTTOM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
(*layout)++;
|
|
|
|
lcchild = layout_construct(lc, layout);
|
|
|
|
if (lcchild == NULL)
|
|
|
|
goto fail;
|
|
|
|
TAILQ_INSERT_TAIL(&lc->cells, lcchild, entry);
|
|
|
|
} while (**layout == ',');
|
|
|
|
|
|
|
|
switch (lc->type) {
|
|
|
|
case LAYOUT_LEFTRIGHT:
|
|
|
|
if (**layout != '}')
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
case LAYOUT_TOPBOTTOM:
|
|
|
|
if (**layout != ']')
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
(*layout)++;
|
|
|
|
|
|
|
|
return (lc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
layout_free_cell(lc);
|
|
|
|
return (NULL);
|
|
|
|
}
|