4 * Copyright (c) 2010 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 u_short
layout_checksum(const char *);
27 int layout_append(struct layout_cell
*, char *, size_t);
28 struct layout_cell
*layout_construct(struct layout_cell
*, const char **);
29 void layout_assign(struct window_pane
**, struct layout_cell
*);
31 /* Calculate layout checksum. */
33 layout_checksum(const char *layout
)
38 for (; *layout
!= '\0'; layout
++) {
39 csum
= (csum
>> 1) + ((csum
& 1) << 15);
45 /* Dump layout as a string. */
47 layout_dump(struct window
*w
)
49 char layout
[BUFSIZ
], *out
;
52 if (layout_append(w
->layout_root
, layout
, sizeof layout
) != 0)
55 xasprintf(&out
, "%4x,%s", layout_checksum(layout
), layout
);
59 /* Append information for a single cell. */
61 layout_append(struct layout_cell
*lc
, char *buf
, size_t len
)
63 struct layout_cell
*lcchild
;
66 const char *brackets
= "][";
71 tmplen
= xsnprintf(tmp
, sizeof tmp
,
72 "%ux%u,%u,%u", lc
->sx
, lc
->sy
, lc
->xoff
, lc
->yoff
);
73 if (tmplen
> (sizeof tmp
) - 1)
75 if (strlcat(buf
, tmp
, len
) >= len
)
79 case LAYOUT_LEFTRIGHT
:
82 case LAYOUT_TOPBOTTOM
:
83 if (strlcat(buf
, &brackets
[1], len
) >= len
)
85 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
86 if (layout_append(lcchild
, buf
, len
) != 0)
88 if (strlcat(buf
, ",", len
) >= len
)
91 buf
[strlen(buf
) - 1] = brackets
[0];
93 case LAYOUT_WINDOWPANE
:
100 /* Parse a layout string and arrange window as layout. */
102 layout_parse(struct window
*w
, const char *layout
)
104 struct layout_cell
*lc
, *lcchild
;
105 struct window_pane
*wp
;
106 u_int npanes
, ncells
, sx
, sy
;
109 /* Check validity. */
110 if (sscanf(layout
, "%hx,", &csum
) != 1)
113 if (csum
!= layout_checksum(layout
))
116 /* Build the layout. */
117 lc
= layout_construct(NULL
, &layout
);
123 /* Check this window will fit into the layout. */
125 npanes
= window_count_panes(w
);
126 ncells
= layout_count_cells(lc
);
129 if (npanes
== ncells
)
132 /* Fewer panes than cells - close the bottom right. */
133 lcchild
= layout_find_bottomright(lc
);
134 layout_destroy_cell(lcchild
, &lc
);
137 /* Save the old window size and resize to the layout size. */
138 sx
= w
->sx
; sy
= w
->sy
;
139 window_resize(w
, lc
->sx
, lc
->sy
);
141 /* Destroy the old layout and swap to the new. */
142 layout_free_cell(w
->layout_root
);
145 /* Assign the panes into the cells. */
146 wp
= TAILQ_FIRST(&w
->panes
);
147 layout_assign(&wp
, lc
);
149 /* Update pane offsets and sizes. */
150 layout_fix_offsets(lc
);
151 layout_fix_panes(w
, lc
->sx
, lc
->sy
);
153 /* Then resize the layout back to the original window size. */
154 layout_resize(w
, sx
, sy
);
155 window_resize(w
, sx
, sy
);
157 layout_print_cell(lc
, __func__
, 0);
162 layout_free_cell(lc
);
166 /* Assign panes into cells. */
168 layout_assign(struct window_pane
**wp
, struct layout_cell
*lc
)
170 struct layout_cell
*lcchild
;
173 case LAYOUT_WINDOWPANE
:
174 layout_make_leaf(lc
, *wp
);
175 *wp
= TAILQ_NEXT(*wp
, entry
);
177 case LAYOUT_LEFTRIGHT
:
178 case LAYOUT_TOPBOTTOM
:
179 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
180 layout_assign(wp
, lcchild
);
185 /* Construct a cell from all or part of a layout tree. */
187 layout_construct(struct layout_cell
*lcparent
, const char **layout
)
189 struct layout_cell
*lc
, *lcchild
;
190 u_int sx
, sy
, xoff
, yoff
;
192 if (!isdigit((u_char
) **layout
))
194 if (sscanf(*layout
, "%ux%u,%u,%u", &sx
, &sy
, &xoff
, &yoff
) != 4)
197 while (isdigit((u_char
) **layout
))
202 while (isdigit((u_char
) **layout
))
207 while (isdigit((u_char
) **layout
))
212 while (isdigit((u_char
) **layout
))
215 lc
= layout_create_cell(lcparent
);
228 lc
->type
= LAYOUT_LEFTRIGHT
;
231 lc
->type
= LAYOUT_TOPBOTTOM
;
239 lcchild
= layout_construct(lc
, layout
);
242 TAILQ_INSERT_TAIL(&lc
->cells
, lcchild
, entry
);
243 } while (**layout
== ',');
246 case LAYOUT_LEFTRIGHT
:
250 case LAYOUT_TOPBOTTOM
:
262 layout_free_cell(lc
);