Annotation of src/usr.bin/tmux/layout-custom.c, Revision 1.21
1.21 ! nicm 1: /* $OpenBSD: layout-custom.c,v 1.20 2021/03/11 06:31:05 nicm Exp $ */
1.1 nicm 2:
3: /*
1.8 nicm 4: * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <ctype.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
1.9 nicm 26: static struct layout_cell *layout_find_bottomright(struct layout_cell *);
27: static u_short layout_checksum(const char *);
28: static int layout_append(struct layout_cell *, char *,
29: size_t);
30: static struct layout_cell *layout_construct(struct layout_cell *,
31: const char **);
32: static void layout_assign(struct window_pane **,
33: struct layout_cell *);
1.2 nicm 34:
35: /* Find the bottom-right cell. */
1.9 nicm 36: static struct layout_cell *
1.2 nicm 37: layout_find_bottomright(struct layout_cell *lc)
38: {
39: if (lc->type == LAYOUT_WINDOWPANE)
40: return (lc);
41: lc = TAILQ_LAST(&lc->cells, layout_cells);
42: return (layout_find_bottomright(lc));
43: }
1.1 nicm 44:
45: /* Calculate layout checksum. */
1.9 nicm 46: static u_short
1.1 nicm 47: layout_checksum(const char *layout)
48: {
49: u_short csum;
50:
51: csum = 0;
52: for (; *layout != '\0'; layout++) {
53: csum = (csum >> 1) + ((csum & 1) << 15);
54: csum += *layout;
55: }
56: return (csum);
57: }
58:
59: /* Dump layout as a string. */
60: char *
1.7 nicm 61: layout_dump(struct layout_cell *root)
1.1 nicm 62: {
1.16 nicm 63: char layout[8192], *out;
1.1 nicm 64:
65: *layout = '\0';
1.7 nicm 66: if (layout_append(root, layout, sizeof layout) != 0)
1.1 nicm 67: return (NULL);
68:
1.9 nicm 69: xasprintf(&out, "%04hx,%s", layout_checksum(layout), layout);
1.1 nicm 70: return (out);
71: }
72:
73: /* Append information for a single cell. */
1.9 nicm 74: static int
1.1 nicm 75: layout_append(struct layout_cell *lc, char *buf, size_t len)
76: {
77: struct layout_cell *lcchild;
78: char tmp[64];
79: size_t tmplen;
80: const char *brackets = "][";
81:
82: if (len == 0)
83: return (-1);
84:
1.3 nicm 85: if (lc->wp != NULL) {
86: tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u,%u",
87: lc->sx, lc->sy, lc->xoff, lc->yoff, lc->wp->id);
88: } else {
89: tmplen = xsnprintf(tmp, sizeof tmp, "%ux%u,%u,%u",
90: lc->sx, lc->sy, lc->xoff, lc->yoff);
91: }
1.1 nicm 92: if (tmplen > (sizeof tmp) - 1)
93: return (-1);
94: if (strlcat(buf, tmp, len) >= len)
95: return (-1);
96:
97: switch (lc->type) {
98: case LAYOUT_LEFTRIGHT:
99: brackets = "}{";
100: /* FALLTHROUGH */
101: case LAYOUT_TOPBOTTOM:
102: if (strlcat(buf, &brackets[1], len) >= len)
103: return (-1);
104: TAILQ_FOREACH(lcchild, &lc->cells, entry) {
105: if (layout_append(lcchild, buf, len) != 0)
106: return (-1);
107: if (strlcat(buf, ",", len) >= len)
108: return (-1);
109: }
110: buf[strlen(buf) - 1] = brackets[0];
111: break;
112: case LAYOUT_WINDOWPANE:
113: break;
114: }
115:
116: return (0);
117: }
118:
1.17 nicm 119: /* Check layout sizes fit. */
120: static int
121: layout_check(struct layout_cell *lc)
122: {
123: struct layout_cell *lcchild;
124: u_int n = 0;
125:
126: switch (lc->type) {
127: case LAYOUT_WINDOWPANE:
128: break;
129: case LAYOUT_LEFTRIGHT:
130: TAILQ_FOREACH(lcchild, &lc->cells, entry) {
131: if (lcchild->sy != lc->sy)
132: return (0);
133: if (!layout_check(lcchild))
134: return (0);
135: n += lcchild->sx + 1;
136: }
137: if (n - 1 != lc->sx)
138: return (0);
139: break;
140: case LAYOUT_TOPBOTTOM:
141: TAILQ_FOREACH(lcchild, &lc->cells, entry) {
142: if (lcchild->sx != lc->sx)
143: return (0);
144: if (!layout_check(lcchild))
145: return (0);
146: n += lcchild->sy + 1;
147: }
148: if (n - 1 != lc->sy)
149: return (0);
150: break;
151: }
152: return (1);
153: }
154:
1.1 nicm 155: /* Parse a layout string and arrange window as layout. */
156: int
1.21 ! nicm 157: layout_parse(struct window *w, const char *layout, char **cause)
1.1 nicm 158: {
159: struct layout_cell *lc, *lcchild;
160: struct window_pane *wp;
1.17 nicm 161: u_int npanes, ncells, sx = 0, sy = 0;
1.1 nicm 162: u_short csum;
163:
164: /* Check validity. */
165: if (sscanf(layout, "%hx,", &csum) != 1)
166: return (-1);
167: layout += 5;
1.21 ! nicm 168: if (csum != layout_checksum(layout)) {
! 169: *cause = xstrdup("invalid layout");
1.1 nicm 170: return (-1);
1.21 ! nicm 171: }
1.1 nicm 172:
173: /* Build the layout. */
174: lc = layout_construct(NULL, &layout);
1.21 ! nicm 175: if (lc == NULL) {
! 176: *cause = xstrdup("invalid layout");
1.1 nicm 177: return (-1);
1.21 ! nicm 178: }
! 179: if (*layout != '\0') {
! 180: *cause = xstrdup("invalid layout");
1.1 nicm 181: goto fail;
1.21 ! nicm 182: }
1.1 nicm 183:
184: /* Check this window will fit into the layout. */
185: for (;;) {
186: npanes = window_count_panes(w);
187: ncells = layout_count_cells(lc);
1.21 ! nicm 188: if (npanes > ncells) {
! 189: xasprintf(cause, "have %u panes but need %u", npanes,
! 190: ncells);
1.1 nicm 191: goto fail;
1.21 ! nicm 192: }
1.1 nicm 193: if (npanes == ncells)
194: break;
195:
196: /* Fewer panes than cells - close the bottom right. */
197: lcchild = layout_find_bottomright(lc);
1.10 nicm 198: layout_destroy_cell(w, lcchild, &lc);
1.1 nicm 199: }
1.17 nicm 200:
201: /*
202: * It appears older versions of tmux were able to generate layouts with
203: * an incorrect top cell size - if it is larger than the top child then
204: * correct that (if this is still wrong the check code will catch it).
205: */
206: switch (lc->type) {
207: case LAYOUT_WINDOWPANE:
208: break;
209: case LAYOUT_LEFTRIGHT:
210: TAILQ_FOREACH(lcchild, &lc->cells, entry) {
211: sy = lcchild->sy + 1;
212: sx += lcchild->sx + 1;
213: }
214: break;
215: case LAYOUT_TOPBOTTOM:
216: TAILQ_FOREACH(lcchild, &lc->cells, entry) {
217: sx = lcchild->sx + 1;
218: sy += lcchild->sy + 1;
219: }
220: break;
221: }
1.18 nicm 222: if (lc->type != LAYOUT_WINDOWPANE && (lc->sx != sx || lc->sy != sy)) {
1.17 nicm 223: log_debug("fix layout %u,%u to %u,%u", lc->sx, lc->sy, sx,sy);
224: layout_print_cell(lc, __func__, 0);
225: lc->sx = sx - 1; lc->sy = sy - 1;
226: }
227:
228: /* Check the new layout. */
1.21 ! nicm 229: if (!layout_check(lc)) {
! 230: *cause = xstrdup("size mismatch after applying layout");
1.17 nicm 231: return (-1);
1.21 ! nicm 232: }
1.1 nicm 233:
1.14 nicm 234: /* Resize to the layout size. */
1.19 nicm 235: window_resize(w, lc->sx, lc->sy, -1, -1);
1.1 nicm 236:
237: /* Destroy the old layout and swap to the new. */
238: layout_free_cell(w->layout_root);
239: w->layout_root = lc;
240:
241: /* Assign the panes into the cells. */
242: wp = TAILQ_FIRST(&w->panes);
243: layout_assign(&wp, lc);
244:
245: /* Update pane offsets and sizes. */
1.15 nicm 246: layout_fix_offsets(w);
1.20 nicm 247: layout_fix_panes(w, NULL);
1.13 nicm 248: recalculate_sizes();
1.1 nicm 249:
250: layout_print_cell(lc, __func__, 0);
1.5 nicm 251:
1.11 nicm 252: notify_window("window-layout-changed", w);
1.1 nicm 253:
254: return (0);
255:
256: fail:
257: layout_free_cell(lc);
258: return (-1);
259: }
260:
261: /* Assign panes into cells. */
1.9 nicm 262: static void
1.1 nicm 263: layout_assign(struct window_pane **wp, struct layout_cell *lc)
264: {
265: struct layout_cell *lcchild;
266:
267: switch (lc->type) {
268: case LAYOUT_WINDOWPANE:
269: layout_make_leaf(lc, *wp);
270: *wp = TAILQ_NEXT(*wp, entry);
271: return;
272: case LAYOUT_LEFTRIGHT:
273: case LAYOUT_TOPBOTTOM:
274: TAILQ_FOREACH(lcchild, &lc->cells, entry)
275: layout_assign(wp, lcchild);
276: return;
277: }
278: }
279:
280: /* Construct a cell from all or part of a layout tree. */
1.9 nicm 281: static struct layout_cell *
1.1 nicm 282: layout_construct(struct layout_cell *lcparent, const char **layout)
283: {
284: struct layout_cell *lc, *lcchild;
285: u_int sx, sy, xoff, yoff;
1.6 nicm 286: const char *saved;
1.1 nicm 287:
288: if (!isdigit((u_char) **layout))
289: return (NULL);
1.6 nicm 290: if (sscanf(*layout, "%ux%u,%u,%u", &sx, &sy, &xoff, &yoff) != 4)
1.1 nicm 291: return (NULL);
292:
293: while (isdigit((u_char) **layout))
294: (*layout)++;
295: if (**layout != 'x')
296: return (NULL);
297: (*layout)++;
298: while (isdigit((u_char) **layout))
299: (*layout)++;
300: if (**layout != ',')
301: return (NULL);
302: (*layout)++;
303: while (isdigit((u_char) **layout))
304: (*layout)++;
305: if (**layout != ',')
306: return (NULL);
307: (*layout)++;
308: while (isdigit((u_char) **layout))
309: (*layout)++;
1.3 nicm 310: if (**layout == ',') {
1.6 nicm 311: saved = *layout;
1.3 nicm 312: (*layout)++;
313: while (isdigit((u_char) **layout))
314: (*layout)++;
1.6 nicm 315: if (**layout == 'x')
316: *layout = saved;
1.3 nicm 317: }
1.1 nicm 318:
319: lc = layout_create_cell(lcparent);
320: lc->sx = sx;
321: lc->sy = sy;
322: lc->xoff = xoff;
323: lc->yoff = yoff;
324:
325: switch (**layout) {
326: case ',':
327: case '}':
328: case ']':
329: case '\0':
330: return (lc);
331: case '{':
332: lc->type = LAYOUT_LEFTRIGHT;
333: break;
334: case '[':
335: lc->type = LAYOUT_TOPBOTTOM;
336: break;
337: default:
338: goto fail;
339: }
340:
341: do {
342: (*layout)++;
343: lcchild = layout_construct(lc, layout);
344: if (lcchild == NULL)
345: goto fail;
346: TAILQ_INSERT_TAIL(&lc->cells, lcchild, entry);
347: } while (**layout == ',');
348:
349: switch (lc->type) {
350: case LAYOUT_LEFTRIGHT:
351: if (**layout != '}')
352: goto fail;
353: break;
354: case LAYOUT_TOPBOTTOM:
355: if (**layout != ']')
356: goto fail;
357: break;
358: default:
359: goto fail;
360: }
361: (*layout)++;
362:
363: return (lc);
364:
365: fail:
366: layout_free_cell(lc);
367: return (NULL);
368: }