1 /* $OpenBSD: layout-set.c,v 1.19 2017/11/15 19:59:27 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <string.h> 22 23 #include "tmux.h" 24 25 /* 26 * Set window layouts - predefined methods to arrange windows. These are 27 * one-off and generate a layout tree. 28 */ 29 30 static void layout_set_even_h(struct window *); 31 static void layout_set_even_v(struct window *); 32 static void layout_set_main_h(struct window *); 33 static void layout_set_main_v(struct window *); 34 static void layout_set_tiled(struct window *); 35 36 static const struct { 37 const char *name; 38 void (*arrange)(struct window *); 39 } layout_sets[] = { 40 { "even-horizontal", layout_set_even_h }, 41 { "even-vertical", layout_set_even_v }, 42 { "main-horizontal", layout_set_main_h }, 43 { "main-vertical", layout_set_main_v }, 44 { "tiled", layout_set_tiled }, 45 }; 46 47 int 48 layout_set_lookup(const char *name) 49 { 50 u_int i; 51 int matched = -1; 52 53 for (i = 0; i < nitems(layout_sets); i++) { 54 if (strncmp(layout_sets[i].name, name, strlen(name)) == 0) { 55 if (matched != -1) /* ambiguous */ 56 return (-1); 57 matched = i; 58 } 59 } 60 61 return (matched); 62 } 63 64 u_int 65 layout_set_select(struct window *w, u_int layout) 66 { 67 if (layout > nitems(layout_sets) - 1) 68 layout = nitems(layout_sets) - 1; 69 70 if (layout_sets[layout].arrange != NULL) 71 layout_sets[layout].arrange(w); 72 73 w->lastlayout = layout; 74 return (layout); 75 } 76 77 u_int 78 layout_set_next(struct window *w) 79 { 80 u_int layout; 81 82 if (w->lastlayout == -1) 83 layout = 0; 84 else { 85 layout = w->lastlayout + 1; 86 if (layout > nitems(layout_sets) - 1) 87 layout = 0; 88 } 89 90 if (layout_sets[layout].arrange != NULL) 91 layout_sets[layout].arrange(w); 92 w->lastlayout = layout; 93 return (layout); 94 } 95 96 u_int 97 layout_set_previous(struct window *w) 98 { 99 u_int layout; 100 101 if (w->lastlayout == -1) 102 layout = nitems(layout_sets) - 1; 103 else { 104 layout = w->lastlayout; 105 if (layout == 0) 106 layout = nitems(layout_sets) - 1; 107 else 108 layout--; 109 } 110 111 if (layout_sets[layout].arrange != NULL) 112 layout_sets[layout].arrange(w); 113 w->lastlayout = layout; 114 return (layout); 115 } 116 117 static void 118 layout_set_even(struct window *w, enum layout_type type) 119 { 120 struct window_pane *wp; 121 struct layout_cell *lc, *lcnew; 122 u_int n; 123 124 layout_print_cell(w->layout_root, __func__, 1); 125 126 /* Get number of panes. */ 127 n = window_count_panes(w); 128 if (n <= 1) 129 return; 130 131 /* Free the old root and construct a new. */ 132 layout_free(w); 133 lc = w->layout_root = layout_create_cell(NULL); 134 layout_set_size(lc, w->sx, w->sy, 0, 0); 135 layout_make_node(lc, type); 136 137 /* Build new leaf cells. */ 138 TAILQ_FOREACH(wp, &w->panes, entry) { 139 lcnew = layout_create_cell(lc); 140 layout_make_leaf(lcnew, wp); 141 TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry); 142 } 143 144 /* Spread out cells. */ 145 layout_spread_cell(w, lc); 146 147 /* Fix cell offsets. */ 148 layout_fix_offsets(lc); 149 layout_fix_panes(w, w->sx, w->sy); 150 151 layout_print_cell(w->layout_root, __func__, 1); 152 153 notify_window("window-layout-changed", w); 154 server_redraw_window(w); 155 } 156 157 static void 158 layout_set_even_h(struct window *w) 159 { 160 layout_set_even(w, LAYOUT_LEFTRIGHT); 161 } 162 163 static void 164 layout_set_even_v(struct window *w) 165 { 166 layout_set_even(w, LAYOUT_TOPBOTTOM); 167 } 168 169 static void 170 layout_set_main_h(struct window *w) 171 { 172 struct window_pane *wp; 173 struct layout_cell *lc, *lcmain, *lcrow, *lcchild; 174 u_int n, mainheight, otherheight, width, height; 175 u_int used, i, j, columns, rows, totalrows; 176 177 layout_print_cell(w->layout_root, __func__, 1); 178 179 /* Get number of panes. */ 180 n = window_count_panes(w); 181 if (n <= 1) 182 return; 183 n--; /* take off main pane */ 184 185 /* How many rows and columns will be needed, not counting main? */ 186 columns = (w->sx + 1) / (PANE_MINIMUM + 1); /* maximum columns */ 187 if (columns == 0) 188 columns = 1; 189 rows = 1 + (n - 1) / columns; 190 columns = 1 + (n - 1) / rows; 191 width = (w->sx - (n - 1)) / columns; 192 193 /* Get the main pane height and add one for separator line. */ 194 mainheight = options_get_number(w->options, "main-pane-height") + 1; 195 196 /* Get the optional other pane height and add one for separator line. */ 197 otherheight = options_get_number(w->options, "other-pane-height") + 1; 198 199 /* 200 * If an other pane height was specified, honour it so long as it 201 * doesn't shrink the main height to less than the main-pane-height 202 */ 203 if (otherheight > 1 && w->sy - otherheight > mainheight) 204 mainheight = w->sy - otherheight; 205 if (mainheight < PANE_MINIMUM + 1) 206 mainheight = PANE_MINIMUM + 1; 207 208 /* Try and make everything fit. */ 209 totalrows = rows * (PANE_MINIMUM + 1) - 1; 210 if (mainheight + totalrows > w->sy) { 211 if (totalrows + PANE_MINIMUM + 1 > w->sy) 212 mainheight = PANE_MINIMUM + 2; 213 else 214 mainheight = w->sy - totalrows; 215 height = PANE_MINIMUM; 216 } else 217 height = (w->sy - mainheight - (rows - 1)) / rows; 218 219 /* Free old tree and create a new root. */ 220 layout_free(w); 221 lc = w->layout_root = layout_create_cell(NULL); 222 layout_set_size(lc, w->sx, mainheight + rows * (height + 1) - 1, 0, 0); 223 layout_make_node(lc, LAYOUT_TOPBOTTOM); 224 225 /* Create the main pane. */ 226 lcmain = layout_create_cell(lc); 227 layout_set_size(lcmain, w->sx, mainheight - 1, 0, 0); 228 layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes)); 229 TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry); 230 231 /* Create a grid of the remaining cells. */ 232 wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry); 233 for (j = 0; j < rows; j++) { 234 /* If this is the last cell, all done. */ 235 if (wp == NULL) 236 break; 237 238 /* Create the new row. */ 239 lcrow = layout_create_cell(lc); 240 layout_set_size(lcrow, w->sx, height, 0, 0); 241 TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry); 242 243 /* If only one column, just use the row directly. */ 244 if (columns == 1) { 245 layout_make_leaf(lcrow, wp); 246 wp = TAILQ_NEXT(wp, entry); 247 continue; 248 } 249 250 /* Add in the columns. */ 251 layout_make_node(lcrow, LAYOUT_LEFTRIGHT); 252 for (i = 0; i < columns; i++) { 253 /* Create and add a pane cell. */ 254 lcchild = layout_create_cell(lcrow); 255 layout_set_size(lcchild, width, height, 0, 0); 256 layout_make_leaf(lcchild, wp); 257 TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry); 258 259 /* Move to the next cell. */ 260 if ((wp = TAILQ_NEXT(wp, entry)) == NULL) 261 break; 262 } 263 264 /* Adjust the row to fit the full width if necessary. */ 265 if (i == columns) 266 i--; 267 used = ((i + 1) * (width + 1)) - 1; 268 if (w->sx <= used) 269 continue; 270 lcchild = TAILQ_LAST(&lcrow->cells, layout_cells); 271 layout_resize_adjust(w, lcchild, LAYOUT_LEFTRIGHT, 272 w->sx - used); 273 } 274 275 /* Adjust the last row height to fit if necessary. */ 276 used = mainheight + (rows * height) + rows - 1; 277 if (w->sy > used) { 278 lcrow = TAILQ_LAST(&lc->cells, layout_cells); 279 layout_resize_adjust(w, lcrow, LAYOUT_TOPBOTTOM, 280 w->sy - used); 281 } 282 283 /* Fix cell offsets. */ 284 layout_fix_offsets(lc); 285 layout_fix_panes(w, w->sx, w->sy); 286 287 layout_print_cell(w->layout_root, __func__, 1); 288 289 notify_window("window-layout-changed", w); 290 server_redraw_window(w); 291 } 292 293 static void 294 layout_set_main_v(struct window *w) 295 { 296 struct window_pane *wp; 297 struct layout_cell *lc, *lcmain, *lccolumn, *lcchild; 298 u_int n, mainwidth, otherwidth, width, height; 299 u_int used, i, j, columns, rows, totalcolumns; 300 301 layout_print_cell(w->layout_root, __func__, 1); 302 303 /* Get number of panes. */ 304 n = window_count_panes(w); 305 if (n <= 1) 306 return; 307 n--; /* take off main pane */ 308 309 /* How many rows and columns will be needed, not counting main? */ 310 rows = (w->sy + 1) / (PANE_MINIMUM + 1); /* maximum rows */ 311 if (rows == 0) 312 rows = 1; 313 columns = 1 + (n - 1) / rows; 314 rows = 1 + (n - 1) / columns; 315 height = (w->sy - (n - 1)) / rows; 316 317 /* Get the main pane width and add one for separator line. */ 318 mainwidth = options_get_number(w->options, "main-pane-width") + 1; 319 320 /* Get the optional other pane width and add one for separator line. */ 321 otherwidth = options_get_number(w->options, "other-pane-width") + 1; 322 323 /* 324 * If an other pane width was specified, honour it so long as it 325 * doesn't shrink the main width to less than the main-pane-width 326 */ 327 if (otherwidth > 1 && w->sx - otherwidth > mainwidth) 328 mainwidth = w->sx - otherwidth; 329 if (mainwidth < PANE_MINIMUM + 1) 330 mainwidth = PANE_MINIMUM + 1; 331 332 /* Try and make everything fit. */ 333 totalcolumns = columns * (PANE_MINIMUM + 1) - 1; 334 if (mainwidth + totalcolumns > w->sx) { 335 if (totalcolumns + PANE_MINIMUM + 1 > w->sx) 336 mainwidth = PANE_MINIMUM + 2; 337 else 338 mainwidth = w->sx - totalcolumns; 339 width = PANE_MINIMUM; 340 } else 341 width = (w->sx - mainwidth - (columns - 1)) / columns; 342 343 /* Free old tree and create a new root. */ 344 layout_free(w); 345 lc = w->layout_root = layout_create_cell(NULL); 346 layout_set_size(lc, mainwidth + columns * (width + 1) - 1, w->sy, 0, 0); 347 layout_make_node(lc, LAYOUT_LEFTRIGHT); 348 349 /* Create the main pane. */ 350 lcmain = layout_create_cell(lc); 351 layout_set_size(lcmain, mainwidth - 1, w->sy, 0, 0); 352 layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes)); 353 TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry); 354 355 /* Create a grid of the remaining cells. */ 356 wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry); 357 for (j = 0; j < columns; j++) { 358 /* If this is the last cell, all done. */ 359 if (wp == NULL) 360 break; 361 362 /* Create the new column. */ 363 lccolumn = layout_create_cell(lc); 364 layout_set_size(lccolumn, width, w->sy, 0, 0); 365 TAILQ_INSERT_TAIL(&lc->cells, lccolumn, entry); 366 367 /* If only one row, just use the row directly. */ 368 if (rows == 1) { 369 layout_make_leaf(lccolumn, wp); 370 wp = TAILQ_NEXT(wp, entry); 371 continue; 372 } 373 374 /* Add in the rows. */ 375 layout_make_node(lccolumn, LAYOUT_TOPBOTTOM); 376 for (i = 0; i < rows; i++) { 377 /* Create and add a pane cell. */ 378 lcchild = layout_create_cell(lccolumn); 379 layout_set_size(lcchild, width, height, 0, 0); 380 layout_make_leaf(lcchild, wp); 381 TAILQ_INSERT_TAIL(&lccolumn->cells, lcchild, entry); 382 383 /* Move to the next cell. */ 384 if ((wp = TAILQ_NEXT(wp, entry)) == NULL) 385 break; 386 } 387 388 /* Adjust the column to fit the full height if necessary. */ 389 if (i == rows) 390 i--; 391 used = ((i + 1) * (height + 1)) - 1; 392 if (w->sy <= used) 393 continue; 394 lcchild = TAILQ_LAST(&lccolumn->cells, layout_cells); 395 layout_resize_adjust(w, lcchild, LAYOUT_TOPBOTTOM, 396 w->sy - used); 397 } 398 399 /* Adjust the last column width to fit if necessary. */ 400 used = mainwidth + (columns * width) + columns - 1; 401 if (w->sx > used) { 402 lccolumn = TAILQ_LAST(&lc->cells, layout_cells); 403 layout_resize_adjust(w, lccolumn, LAYOUT_LEFTRIGHT, 404 w->sx - used); 405 } 406 407 /* Fix cell offsets. */ 408 layout_fix_offsets(lc); 409 layout_fix_panes(w, w->sx, w->sy); 410 411 layout_print_cell(w->layout_root, __func__, 1); 412 413 notify_window("window-layout-changed", w); 414 server_redraw_window(w); 415 } 416 417 void 418 layout_set_tiled(struct window *w) 419 { 420 struct window_pane *wp; 421 struct layout_cell *lc, *lcrow, *lcchild; 422 u_int n, width, height, used; 423 u_int i, j, columns, rows; 424 425 layout_print_cell(w->layout_root, __func__, 1); 426 427 /* Get number of panes. */ 428 n = window_count_panes(w); 429 if (n <= 1) 430 return; 431 432 /* How many rows and columns are wanted? */ 433 rows = columns = 1; 434 while (rows * columns < n) { 435 rows++; 436 if (rows * columns < n) 437 columns++; 438 } 439 440 /* What width and height should they be? */ 441 width = (w->sx - (columns - 1)) / columns; 442 if (width < PANE_MINIMUM) 443 width = PANE_MINIMUM; 444 height = (w->sy - (rows - 1)) / rows; 445 if (height < PANE_MINIMUM) 446 height = PANE_MINIMUM; 447 448 /* Free old tree and create a new root. */ 449 layout_free(w); 450 lc = w->layout_root = layout_create_cell(NULL); 451 layout_set_size(lc, (width + 1) * columns - 1, 452 (height + 1) * rows - 1, 0, 0); 453 layout_make_node(lc, LAYOUT_TOPBOTTOM); 454 455 /* Create a grid of the cells. */ 456 wp = TAILQ_FIRST(&w->panes); 457 for (j = 0; j < rows; j++) { 458 /* If this is the last cell, all done. */ 459 if (wp == NULL) 460 break; 461 462 /* Create the new row. */ 463 lcrow = layout_create_cell(lc); 464 layout_set_size(lcrow, w->sx, height, 0, 0); 465 TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry); 466 467 /* If only one column, just use the row directly. */ 468 if (n - (j * columns) == 1 || columns == 1) { 469 layout_make_leaf(lcrow, wp); 470 wp = TAILQ_NEXT(wp, entry); 471 continue; 472 } 473 474 /* Add in the columns. */ 475 layout_make_node(lcrow, LAYOUT_LEFTRIGHT); 476 for (i = 0; i < columns; i++) { 477 /* Create and add a pane cell. */ 478 lcchild = layout_create_cell(lcrow); 479 layout_set_size(lcchild, width, height, 0, 0); 480 layout_make_leaf(lcchild, wp); 481 TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry); 482 483 /* Move to the next cell. */ 484 if ((wp = TAILQ_NEXT(wp, entry)) == NULL) 485 break; 486 } 487 488 /* 489 * Adjust the row and columns to fit the full width if 490 * necessary. 491 */ 492 if (i == columns) 493 i--; 494 used = ((i + 1) * (width + 1)) - 1; 495 if (w->sx <= used) 496 continue; 497 lcchild = TAILQ_LAST(&lcrow->cells, layout_cells); 498 layout_resize_adjust(w, lcchild, LAYOUT_LEFTRIGHT, 499 w->sx - used); 500 } 501 502 /* Adjust the last row height to fit if necessary. */ 503 used = (rows * height) + rows - 1; 504 if (w->sy > used) { 505 lcrow = TAILQ_LAST(&lc->cells, layout_cells); 506 layout_resize_adjust(w, lcrow, LAYOUT_TOPBOTTOM, 507 w->sy - used); 508 } 509 510 /* Fix cell offsets. */ 511 layout_fix_offsets(lc); 512 layout_fix_panes(w, w->sx, w->sy); 513 514 layout_print_cell(w->layout_root, __func__, 1); 515 516 notify_window("window-layout-changed", w); 517 server_redraw_window(w); 518 } 519