Coverage Report

Created: 2026-10-05 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/layout.c
Line
Count
Source
1
/* $OpenBSD: layout.c,v 1.102 2026/10/02 12:48:52 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 * Copyright (c) 2016 Stephen Kent <smkent@smkent.net>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/types.h>
21
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "tmux.h"
26
27
/*
28
 * The window layout is a tree of cells each of which can be one of: a
29
 * left-right container for a list of cells, a top-bottom container for a list
30
 * of cells, or a container for a window pane. 'Node' will be used to refer to
31
 * a cell which contains a list of cells, and 'leaf' to refer to a cell that
32
 * contains a window pane. A leaf is considered to be 'tiled' if it is to be
33
 * drawn as a part of the tiled layout. A 'neighbour' is a sibling that is also
34
 * tiled or a node that contains a tiled leaf in a subtree. A cell's 'split'
35
 * size refers to the side that is shortened when splitting it, determined by
36
 * the parent's type.
37
 *
38
 * Each window has a pointer to the root of its layout tree (containing its
39
 * panes), every pane has a pointer back to the cell containing it, and each
40
 * cell a pointer to its parent cell. Every cell has a position in the root
41
 * layout tree. This position is retained through cell state changes such as
42
 * floating or hiding.
43
 */
44
45
static u_int  layout_resize_check(struct window *, struct layout_cell *,
46
        enum layout_type);
47
static int  layout_resize_pane_grow(struct window *, struct layout_cell *,
48
        enum layout_type, int, int);
49
static int  layout_resize_pane_shrink(struct window *, struct layout_cell *,
50
        enum layout_type, int);
51
static u_int  layout_new_pane_size(struct window *, u_int,
52
        struct layout_cell *, enum layout_type, u_int, u_int,
53
        u_int);
54
static int  layout_set_size_check(struct window *, struct layout_cell *,
55
        enum layout_type, int);
56
static void layout_resize_child_cells(struct window *,
57
        struct layout_cell *);
58
59
/* Initializes cell geometry to sentinel values. */
60
static void
61
layout_geometry_init(struct layout_geometry *lg)
62
0
{
63
0
  lg->sx = UINT_MAX;
64
0
  lg->sy = UINT_MAX;
65
0
  lg->xoff = INT_MAX;
66
0
  lg->yoff = INT_MAX;
67
0
}
68
69
/* Create a new layout cell. */
70
struct layout_cell *
71
layout_create_cell(struct layout_cell *lcparent)
72
0
{
73
0
  struct layout_cell  *lc;
74
75
0
  lc = xcalloc(1, sizeof *lc);
76
0
  lc->type = LAYOUT_WINDOWPANE;
77
0
  lc->parent = lcparent;
78
0
  TAILQ_INIT(&lc->cells);
79
80
0
  layout_geometry_init(&lc->g);
81
0
  layout_geometry_init(&lc->fg);
82
83
0
  return (lc);
84
0
}
85
86
/* Free a layout cell. */
87
void
88
layout_free_cell(struct layout_cell *lc, int only_nodes)
89
0
{
90
0
  struct layout_cell  *lcchild, *lcnext;
91
92
0
  if (lc == NULL || (only_nodes && lc->type == LAYOUT_WINDOWPANE))
93
0
    return;
94
95
0
  switch (lc->type) {
96
0
  case LAYOUT_LEFTRIGHT:
97
0
  case LAYOUT_TOPBOTTOM:
98
0
    lcchild = TAILQ_FIRST(&lc->cells);
99
0
    while (lcchild != NULL) {
100
0
      lcnext = TAILQ_NEXT(lcchild, entry);
101
0
      if (!only_nodes || lcchild->type != LAYOUT_WINDOWPANE) {
102
0
        TAILQ_REMOVE(&lc->cells, lcchild, entry);
103
0
        layout_free_cell(lcchild, only_nodes);
104
0
      }
105
0
      lcchild = lcnext;
106
0
    }
107
0
    break;
108
0
  case LAYOUT_WINDOWPANE:
109
0
    if (lc->wp != NULL && lc->wp->layout_cell != NULL) {
110
0
      lc->wp->layout_cell->parent = NULL;
111
0
      lc->wp->layout_cell = NULL;
112
0
    }
113
0
    break;
114
0
  }
115
116
0
  free(lc);
117
0
}
118
119
/* Log a cell. */
120
void
121
layout_print_cell(struct layout_cell *lc, const char *hdr, u_int n)
122
0
{
123
0
  struct layout_cell  *lcchild;
124
0
  const char    *type;
125
126
0
  if (lc == NULL)
127
0
    return;
128
129
0
  switch (lc->type) {
130
0
  case LAYOUT_LEFTRIGHT:
131
0
    type = "LEFTRIGHT";
132
0
    break;
133
0
  case LAYOUT_TOPBOTTOM:
134
0
    type = "TOPBOTTOM";
135
0
    break;
136
0
  case LAYOUT_WINDOWPANE:
137
0
    type = "WINDOWPANE";
138
0
    break;
139
0
  default:
140
0
    type = "UNKNOWN";
141
0
    break;
142
0
  }
143
0
  log_debug("%s:%*s%p type %s [parent %p] wp=%p [%d,%d %ux%u]", hdr, n,
144
0
      " ", lc, type, lc->parent, lc->wp, lc->g.xoff, lc->g.yoff, lc->g.sx,
145
0
      lc->g.sy);
146
0
  switch (lc->type) {
147
0
  case LAYOUT_LEFTRIGHT:
148
0
  case LAYOUT_TOPBOTTOM:
149
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
150
0
      layout_print_cell(lcchild, hdr, n + 1);
151
0
    break;
152
0
  case LAYOUT_WINDOWPANE:
153
0
    break;
154
0
  }
155
0
}
156
157
/* Search for a cell by the border position. */
158
struct layout_cell *
159
layout_search_by_border(struct layout_cell *lc, u_int x, u_int y)
160
0
{
161
0
  struct layout_cell  *lcchild, *last = NULL;
162
163
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
164
0
    if ((int)x >= lcchild->g.xoff &&
165
0
        (int)x < lcchild->g.xoff + (int)lcchild->g.sx &&
166
0
        (int)y >= lcchild->g.yoff &&
167
0
        (int)y < lcchild->g.yoff + (int)lcchild->g.sy) {
168
      /* Inside the cell - recurse. */
169
0
      return (layout_search_by_border(lcchild, x, y));
170
0
    }
171
172
0
    if (last == NULL) {
173
0
      last = lcchild;
174
0
      continue;
175
0
    }
176
177
0
    switch (lc->type) {
178
0
    case LAYOUT_LEFTRIGHT:
179
0
      if ((int)x < lcchild->g.xoff &&
180
0
          (int)x >= last->g.xoff + (int)last->g.sx)
181
0
        return (last);
182
0
      break;
183
0
    case LAYOUT_TOPBOTTOM:
184
0
      if ((int)y < lcchild->g.yoff &&
185
0
          (int)y >= last->g.yoff + (int)last->g.sy)
186
0
        return (last);
187
0
      break;
188
0
    case LAYOUT_WINDOWPANE:
189
0
      break;
190
0
    }
191
192
0
    last = lcchild;
193
0
  }
194
195
0
  return (NULL);
196
0
}
197
198
/* Set cell size. */
199
void
200
layout_set_size(struct layout_cell *lc, u_int sx, u_int sy, int xoff, int yoff)
201
0
{
202
0
  lc->g.sx = sx;
203
0
  lc->g.sy = sy;
204
205
0
  lc->g.xoff = xoff;
206
0
  lc->g.yoff = yoff;
207
0
}
208
209
/* Make a cell a leaf cell. */
210
void
211
layout_make_leaf(struct layout_cell *lc, struct window_pane *wp)
212
0
{
213
0
  lc->type = LAYOUT_WINDOWPANE;
214
215
0
  TAILQ_INIT(&lc->cells);
216
217
0
  wp->layout_cell = lc;
218
0
  lc->wp = wp;
219
0
}
220
221
/* Make a cell a node cell. */
222
void
223
layout_make_node(struct layout_cell *lc, enum layout_type type)
224
0
{
225
0
  if (type == LAYOUT_WINDOWPANE)
226
0
    fatalx("bad layout type");
227
0
  lc->type = type;
228
229
0
  TAILQ_INIT(&lc->cells);
230
231
0
  if (lc->wp != NULL)
232
0
    lc->wp->layout_cell = NULL;
233
0
  lc->wp = NULL;
234
0
}
235
236
int
237
layout_cell_is_tiled(struct layout_cell *lc)
238
0
{
239
0
  int is_leaf = lc->type == LAYOUT_WINDOWPANE;
240
0
  int is_floating = lc->flags & LAYOUT_CELL_FLOATING;
241
242
0
  return is_leaf && !is_floating;
243
0
}
244
245
int
246
layout_cell_has_tiled_child(struct layout_cell *lc)
247
0
{
248
0
  struct layout_cell      *lcchild;
249
250
0
  if (lc->type == LAYOUT_WINDOWPANE)
251
0
    return (0);
252
253
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
254
0
    if (layout_cell_is_tiled(lcchild) ||
255
0
        layout_cell_has_tiled_child(lcchild))
256
0
      return (1);
257
0
  }
258
0
  return (0);
259
0
}
260
261
static int
262
layout_cell_is_first_tiled(struct layout_cell *lc)
263
0
{
264
0
  struct layout_cell      *lcchild, *lcparent = lc->parent;
265
266
0
  if (lcparent == NULL)
267
0
    return (layout_cell_is_tiled(lc));
268
269
0
  TAILQ_FOREACH(lcchild, &lcparent->cells, entry) {
270
0
    if (layout_cell_is_tiled(lcchild) ||
271
0
        layout_cell_has_tiled_child(lcchild))
272
0
      break;
273
0
  }
274
275
0
  return (lcchild == lc);
276
0
}
277
278
static struct layout_cell *
279
layout_cell_get_first_tiled(struct layout_cell *lc)
280
0
{
281
0
  struct layout_cell  *lcchild, *lcchild2;
282
283
0
  if (layout_cell_is_tiled(lc))
284
0
    return (lc);
285
0
  if (lc->type == LAYOUT_WINDOWPANE)
286
0
    return (NULL);
287
288
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
289
0
    if (layout_cell_is_tiled(lcchild))
290
0
      return (lcchild);
291
0
    if (lcchild->type != LAYOUT_WINDOWPANE) {
292
0
      lcchild2 = layout_cell_get_first_tiled(lcchild);
293
0
      if (lcchild2 != NULL)
294
0
        return (lcchild2);
295
0
    }
296
0
  }
297
0
  return (NULL);
298
0
}
299
300
/* Fix cell offsets for a child cell. */
301
static void
302
layout_fix_offsets1(struct layout_cell *lc)
303
0
{
304
0
  struct layout_cell  *lcchild;
305
0
  int      xoff, yoff;
306
307
0
  if (lc->type == LAYOUT_LEFTRIGHT) {
308
0
    xoff = lc->g.xoff;
309
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
310
0
      if (!layout_cell_is_tiled(lcchild) &&
311
0
          !layout_cell_has_tiled_child(lcchild))
312
0
        continue;
313
0
      lcchild->g.xoff = xoff;
314
0
      lcchild->g.yoff = lc->g.yoff;
315
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
316
0
        layout_fix_offsets1(lcchild);
317
0
      xoff += lcchild->g.sx + 1;
318
0
    }
319
0
  } else {
320
0
    yoff = lc->g.yoff;
321
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
322
0
      if (!layout_cell_is_tiled(lcchild) &&
323
0
          !layout_cell_has_tiled_child(lcchild))
324
0
        continue;
325
0
      lcchild->g.xoff = lc->g.xoff;
326
0
      lcchild->g.yoff = yoff;
327
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
328
0
        layout_fix_offsets1(lcchild);
329
0
      yoff += lcchild->g.sy + 1;
330
0
    }
331
0
  }
332
0
}
333
334
/* Update cell offsets based on their sizes. */
335
void
336
layout_fix_offsets(struct window *w)
337
0
{
338
0
  struct layout_cell  *lc = w->layout_root;
339
340
  /* Root consists of a single floating cell */
341
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
342
0
    return;
343
344
0
  lc->g.xoff = 0;
345
0
  lc->g.yoff = 0;
346
347
0
  layout_fix_offsets1(lc);
348
0
}
349
350
static int
351
layout_cell_is_last_tiled(struct layout_cell *lc)
352
0
{
353
0
  struct layout_cell      *lcchild, *lcparent = lc->parent;
354
355
0
  if (lcparent == NULL)
356
0
    return (layout_cell_is_tiled(lc));
357
358
0
  TAILQ_FOREACH_REVERSE(lcchild, &lcparent->cells, layout_cells, entry) {
359
0
    if (layout_cell_is_tiled(lcchild) ||
360
0
        layout_cell_has_tiled_child(lcchild))
361
0
      break;
362
0
  }
363
364
0
  return (lcchild == lc);
365
0
}
366
367
/* Is this a top cell? */
368
static int
369
layout_cell_is_top(struct layout_cell *root, struct layout_cell *lc)
370
0
{
371
0
  struct layout_cell  *next;
372
373
0
  while (lc != root) {
374
0
    next = lc->parent;
375
0
    if (next == NULL)
376
0
      return (0);
377
0
    if (next->type == LAYOUT_TOPBOTTOM &&
378
0
        !layout_cell_is_first_tiled(lc))
379
0
      return (0);
380
0
    lc = next;
381
0
  }
382
0
  return (1);
383
0
}
384
385
/* Is this a bottom cell? */
386
static int
387
layout_cell_is_bottom(struct layout_cell *root, struct layout_cell *lc)
388
0
{
389
0
  struct layout_cell  *next;
390
391
0
  while (lc != root) {
392
0
    next = lc->parent;
393
0
    if (next == NULL)
394
0
      return (0);
395
0
    if (next->type == LAYOUT_TOPBOTTOM &&
396
0
        !layout_cell_is_last_tiled(lc))
397
0
      return (0);
398
0
    lc = next;
399
0
  }
400
0
  return (1);
401
0
}
402
403
/*
404
 * Returns 1 if we need to add an extra line for the pane status line. This is
405
 * the case for the most upper or lower panes only.
406
 */
407
int
408
layout_add_horizontal_border(struct layout_cell *root, struct layout_cell *lc,
409
    int status)
410
0
{
411
0
  if (status == PANE_STATUS_TOP)
412
0
    return (layout_cell_is_top(root, lc));
413
0
  if (status == PANE_STATUS_BOTTOM)
414
0
    return (layout_cell_is_bottom(root, lc));
415
0
  return (0);
416
0
}
417
418
/* Update pane offsets and sizes based on their cells. */
419
void
420
layout_fix_panes(struct window *w, struct window_pane *skip)
421
0
{
422
0
  struct window_pane  *wp;
423
0
  struct layout_cell  *lc, *root = w->layout_root;
424
0
  int      status, sb_w, sb_pad;
425
0
  int      old_xoff, old_yoff, changed = 0;
426
0
  u_int      sx, sy, old_sx, old_sy;
427
428
0
  TAILQ_FOREACH(wp, &w->panes, entry) {
429
0
    if ((lc = wp->layout_cell) == NULL || wp == skip)
430
0
      continue;
431
432
0
    old_xoff = wp->xoff;
433
0
    old_yoff = wp->yoff;
434
0
    old_sx = wp->sx;
435
0
    old_sy = wp->sy;
436
437
0
    wp->xoff = lc->g.xoff;
438
0
    wp->yoff = lc->g.yoff;
439
0
    sx = lc->g.sx;
440
0
    sy = lc->g.sy;
441
442
0
    status = window_pane_get_pane_status(wp);
443
0
    if (!window_pane_is_floating(wp) &&
444
0
        layout_add_horizontal_border(root, lc, status)) {
445
0
      if (status == PANE_STATUS_TOP)
446
0
        wp->yoff++;
447
0
      if (sy > 1)
448
0
        sy--;
449
0
    }
450
451
0
    if (window_pane_scrollbar_reserve(wp)) {
452
0
      sb_w = wp->scrollbar_style.width;
453
0
      sb_pad = wp->scrollbar_style.pad;
454
0
      if (sb_w < 1)
455
0
        sb_w = 1;
456
0
      if (sb_pad < 0)
457
0
        sb_pad = 0;
458
0
      if (w->sb_pos == PANE_SCROLLBARS_LEFT) {
459
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM) {
460
0
          wp->xoff = wp->xoff +
461
0
              (int)sx - PANE_MINIMUM;
462
0
          sx = PANE_MINIMUM;
463
0
        } else {
464
0
          sx = sx - sb_w - sb_pad;
465
0
          wp->xoff = wp->xoff + sb_w + sb_pad;
466
0
        }
467
0
      } else /* sb_pos == PANE_SCROLLBARS_RIGHT */
468
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM)
469
0
          sx = PANE_MINIMUM;
470
0
        else
471
0
          sx = sx - sb_w - sb_pad;
472
0
    }
473
474
0
    window_pane_resize(wp, sx, sy);
475
476
0
    if (wp->xoff != old_xoff ||
477
0
        wp->yoff != old_yoff ||
478
0
        wp->sx != old_sx ||
479
0
        wp->sy != old_sy) {
480
0
      if (window_pane_scrollbar_reserve(wp))
481
0
        wp->flags |= PANE_REDRAWSCROLLBAR;
482
0
      changed = 1;
483
0
    }
484
0
  }
485
0
  if (changed)
486
0
    redraw_invalidate_scene(w);
487
0
}
488
489
/* Count the number of available cells in a layout. */
490
u_int
491
layout_count_cells(struct layout_cell *lc, int with_floating)
492
0
{
493
0
  struct layout_cell  *lcchild;
494
0
  u_int      count = 0;
495
496
0
  switch (lc->type) {
497
0
  case LAYOUT_WINDOWPANE:
498
0
    if (lc->flags & LAYOUT_CELL_FLOATING && !with_floating)
499
0
      return 0;
500
0
    return (1);
501
0
  case LAYOUT_LEFTRIGHT:
502
0
  case LAYOUT_TOPBOTTOM:
503
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
504
0
      count += layout_count_cells(lcchild, with_floating);
505
0
    return (count);
506
0
  default:
507
0
    fatalx("bad layout type");
508
0
  }
509
0
}
510
511
/* Calculate how much size is available to be removed from a cell. */
512
static u_int
513
layout_resize_check(struct window *w, struct layout_cell *lc,
514
    enum layout_type type)
515
0
{
516
0
  struct layout_cell  *lcchild, *root = w->layout_root;
517
0
  struct style    *sb_style = &w->active->scrollbar_style;
518
0
  u_int      available, minimum;
519
0
  int      status;
520
521
0
  status = window_get_pane_status(w);
522
523
  /* Floating cells do not take space from the tiled layout. */
524
0
  if (!layout_cell_is_tiled(lc) && !layout_cell_has_tiled_child(lc))
525
0
    return (0);
526
527
0
  if (lc->type == LAYOUT_WINDOWPANE) {
528
    /* Space available in this cell only. */
529
0
    if (type == LAYOUT_LEFTRIGHT) {
530
0
      available = lc->g.sx;
531
0
      if (w->sb == PANE_SCROLLBARS_ALWAYS)
532
0
        minimum = PANE_MINIMUM + sb_style->width +
533
0
            sb_style->pad;
534
0
      else
535
0
        minimum = PANE_MINIMUM;
536
0
    } else {
537
0
      available = lc->g.sy;
538
0
      if (layout_add_horizontal_border(root, lc, status))
539
0
        minimum = PANE_MINIMUM + 1;
540
0
      else
541
0
        minimum = PANE_MINIMUM;
542
0
    }
543
0
    if (available > minimum)
544
0
      available -= minimum;
545
0
    else
546
0
      available = 0;
547
0
  } else if (lc->type == type) {
548
    /* Same type: total of available space in all child cells. */
549
0
    available = 0;
550
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
551
0
      available += layout_resize_check(w, lcchild, type);
552
0
  } else {
553
    /* Different type: minimum of available space in child cells. */
554
0
    minimum = UINT_MAX;
555
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
556
0
      if (!layout_cell_is_tiled(lcchild) &&
557
0
          !layout_cell_has_tiled_child(lcchild))
558
0
        continue;
559
0
      available = layout_resize_check(w, lcchild, type);
560
0
      if (available < minimum)
561
0
        minimum = available;
562
0
    }
563
0
    available = minimum;
564
0
  }
565
566
0
  return (available);
567
0
}
568
569
/*
570
 * Adjust cell size evenly, including altering its children. This function
571
 * expects the change to have already been bounded to the space available.
572
 */
573
void
574
layout_resize_adjust(struct window *w, struct layout_cell *lc,
575
    enum layout_type type, int change)
576
0
{
577
0
  struct layout_cell  *lcchild;
578
0
  int      changed;
579
580
  /* Adjust the cell size. */
581
0
  if (type == LAYOUT_LEFTRIGHT)
582
0
    lc->g.sx += change;
583
0
  else
584
0
    lc->g.sy += change;
585
586
  /* If this is a leaf cell, that is all that is necessary. */
587
0
  if (type == LAYOUT_WINDOWPANE)
588
0
    return;
589
590
  /* Child cell runs in a different direction. */
591
0
  if (lc->type != type) {
592
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
593
0
      if (!layout_cell_is_tiled(lcchild) &&
594
0
          !layout_cell_has_tiled_child(lcchild))
595
0
        continue;
596
0
      layout_resize_adjust(w, lcchild, type, change);
597
0
    }
598
0
    return;
599
0
  }
600
601
  /*
602
   * If a node doesn't contain any tiled cells, there is nothing to do.
603
   */
604
0
  if (!layout_cell_has_tiled_child(lc))
605
0
    return;
606
607
  /*
608
   * Child cell runs in the same direction. Adjust each child equally
609
   * until no further change is possible.
610
   */
611
0
  while (change != 0) {
612
0
    changed = 0;
613
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
614
0
      if (change == 0)
615
0
        break;
616
0
      if (!layout_cell_is_tiled(lcchild) &&
617
0
          !layout_cell_has_tiled_child(lcchild))
618
0
        continue;
619
0
      if (change > 0) {
620
0
        layout_resize_adjust(w, lcchild, type, 1);
621
0
        change--;
622
0
        changed = 1;
623
0
        continue;
624
0
      }
625
0
      if (layout_resize_check(w, lcchild, type) > 0) {
626
0
        layout_resize_adjust(w, lcchild, type, -1);
627
0
        change++;
628
0
        changed = 1;
629
0
      }
630
0
    }
631
0
    if (!changed)
632
0
      break;
633
0
  }
634
0
}
635
636
/* Resizes a cell to a specified size */
637
void
638
layout_resize_set_size(struct window *w, struct layout_cell *lc,
639
    enum layout_type type, u_int size)
640
0
{
641
0
  int change;
642
643
0
  if (type == LAYOUT_LEFTRIGHT)
644
0
    change = size - lc->g.sx;
645
0
  else
646
0
    change = size - lc->g.sy;
647
0
  layout_resize_adjust(w, lc, type, change);
648
0
}
649
650
/* Find and return the nearest neighbour to a cell in a specific direction. */
651
static struct layout_cell *
652
layout_cell_get_neighbour_dir(struct layout_cell *lc, int direction)
653
0
{
654
0
  struct layout_cell  *lcn = lc;
655
656
0
  while (1) {
657
0
    if (direction)
658
0
      lcn = TAILQ_NEXT(lcn, entry);
659
0
    else
660
0
      lcn = TAILQ_PREV(lcn, layout_cells, entry);
661
662
0
    if (lcn == NULL ||
663
0
        layout_cell_is_tiled(lcn) ||
664
0
        layout_cell_has_tiled_child(lcn))
665
0
      return (lcn);
666
0
  }
667
0
}
668
669
/*
670
 * Find and return the nearest neighbour. Prefers cells "after" the specified
671
 * cell. This behavior defines how cell dimensions are redistributed when a cell
672
 * is hidden/shown and floated/tiled.
673
 */
674
struct layout_cell *
675
layout_cell_get_neighbour(struct layout_cell *lc)
676
0
{
677
0
  struct layout_cell  *lcother, *lcparent = lc->parent;
678
0
  int      direction = 1;
679
680
0
  if (lcparent == NULL)
681
0
    return (NULL);
682
683
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
684
0
    direction = !direction;
685
686
0
  lcother = layout_cell_get_neighbour_dir(lc, direction);
687
0
  if (lcother == NULL)
688
0
    lcother = layout_cell_get_neighbour_dir(lc, !direction);
689
690
0
  return (lcother);
691
0
}
692
693
694
/* Destroy a cell and redistribute the space. */
695
void
696
layout_destroy_cell(struct window *w, struct layout_cell *lc,
697
    struct layout_cell **lcroot)
698
0
{
699
0
  struct layout_cell  *lcother = NULL, *lcparent;
700
0
  int      change;
701
702
  /* If no parent, this is the last pane in a window. */
703
0
  lcparent = lc->parent;
704
0
  if (lcparent == NULL) {
705
0
    if (*lcroot == lc)
706
0
      *lcroot = NULL;
707
0
    layout_free_cell(lc, 0);
708
0
    return;
709
0
  }
710
711
0
  if (!layout_cell_is_tiled(lc)) {
712
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
713
0
    layout_free_cell(lc, 0);
714
0
    goto out;
715
0
  }
716
717
0
  lcother = layout_cell_get_neighbour(lc);
718
0
  if (lcother != NULL) {
719
0
    if (lcparent->type == LAYOUT_LEFTRIGHT)
720
0
      change = lc->g.sx + 1;
721
0
    else
722
0
      change = lc->g.sy + 1;
723
0
    layout_resize_adjust(w, lcother, lcparent->type, change);
724
0
  } else
725
0
    layout_remove_tile(w, lcparent);
726
727
  /* Remove this from the parent's list. */
728
0
  TAILQ_REMOVE(&lcparent->cells, lc, entry);
729
0
  layout_free_cell(lc, 0);
730
731
0
out:
732
  /*
733
   * If the parent now has one cell, remove the parent from the tree and
734
   * replace it by that cell.
735
   */
736
0
  lc = TAILQ_FIRST(&lcparent->cells);
737
0
  if (lc != NULL && TAILQ_NEXT(lc, entry) == NULL) {
738
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
739
740
0
    lc->parent = lcparent->parent;
741
0
    if (lc->parent == NULL) {
742
0
      if (layout_cell_is_tiled(lc)) {
743
0
        lc->g.xoff = 0;
744
0
        lc->g.yoff = 0;
745
0
      }
746
0
      *lcroot = lc;
747
0
    } else
748
0
      TAILQ_REPLACE(&lc->parent->cells, lcparent, lc, entry);
749
750
0
    layout_free_cell(lcparent, 0);
751
0
  }
752
0
}
753
754
/* Initialize layout for pane. */
755
void
756
layout_init(struct window *w, struct window_pane *wp)
757
0
{
758
0
  struct layout_cell  *lc;
759
760
0
  lc = w->layout_root = layout_create_cell(NULL);
761
0
  layout_set_size(lc, w->sx, w->sy, 0, 0);
762
0
  layout_make_leaf(lc, wp);
763
0
  layout_fix_panes(w, NULL);
764
0
}
765
766
/* Free layout for pane. */
767
void
768
layout_free(struct window *w, int only_nodes)
769
0
{
770
0
  layout_free_cell(w->layout_root, only_nodes);
771
0
}
772
773
/* Move and resize floating panes so they stay inside the window. */
774
static void
775
layout_clamp_floating_panes(struct window *w, u_int sx, u_int sy)
776
0
{
777
0
  struct window_pane  *wp;
778
0
  struct layout_cell  *lc;
779
0
  u_int      pad, avail, csx, csy;
780
781
0
  TAILQ_FOREACH(wp, &w->z_index, zentry) {
782
0
    lc = wp->layout_cell;
783
0
    if (lc == NULL || (~lc->flags & LAYOUT_CELL_FLOATING))
784
0
      continue;
785
0
    if (window_pane_get_pane_lines(wp) == PANE_LINES_NONE)
786
0
      pad = 0;
787
0
    else
788
0
      pad = 1;
789
790
0
    csx = lc->g.sx;
791
0
    avail = (sx > 2 * pad) ? sx - 2 * pad : 0;
792
0
    if (csx > avail)
793
0
      csx = (avail > PANE_MINIMUM) ? avail : PANE_MINIMUM;
794
0
    csy = lc->g.sy;
795
0
    avail = (sy > 2 * pad) ? sy - 2 * pad : 0;
796
0
    if (csy > avail)
797
0
      csy = (avail > PANE_MINIMUM) ? avail : PANE_MINIMUM;
798
0
    if (csx != lc->g.sx || csy != lc->g.sy)
799
0
      layout_set_size(lc, csx, csy, lc->g.xoff, lc->g.yoff);
800
801
0
    if (lc->g.xoff + lc->g.sx + pad > sx) {
802
0
      if (lc->g.sx + 2 * pad >= sx)
803
0
        lc->g.xoff = pad;
804
0
      else
805
0
        lc->g.xoff = sx - lc->g.sx - pad;
806
0
    }
807
0
    if (lc->g.yoff + lc->g.sy + pad > sy) {
808
0
      if (lc->g.sy + 2 * pad >= sy)
809
0
        lc->g.yoff = pad;
810
0
      else
811
0
        lc->g.yoff = sy - lc->g.sy - pad;
812
0
    }
813
0
  }
814
0
}
815
816
/* Resize the entire layout after window resize. */
817
void
818
layout_resize(struct window *w, u_int sx, u_int sy)
819
0
{
820
0
  struct layout_cell  *lc = w->layout_root;
821
0
  int      xlimit, ylimit, xchange, ychange;
822
823
  /*
824
   * Adjust horizontally. Do not attempt to reduce the layout lower than
825
   * the minimum (more than the amount returned by layout_resize_check).
826
   *
827
   * This can mean that the window size is smaller than the total layout
828
   * size: redrawing this is handled at a higher level, but it does leave
829
   * a problem with growing the window size here: if the current size is
830
   * < the minimum, growing proportionately by adding to each pane is
831
   * wrong as it would keep the layout size larger than the window size.
832
   * Instead, spread the difference between the minimum and the new size
833
   * out proportionately - this should leave the layout fitting the new
834
   * window size.
835
   */
836
0
  if (lc->type == LAYOUT_WINDOWPANE && (lc->flags & LAYOUT_CELL_FLOATING)) {
837
0
    layout_clamp_floating_panes(w, sx, sy);
838
0
    layout_fix_panes(w, NULL);
839
0
    return;
840
0
  }
841
0
  xchange = sx - lc->g.sx;
842
0
  xlimit = layout_resize_check(w, lc, LAYOUT_LEFTRIGHT);
843
0
  if (xchange < 0 && xchange < -xlimit)
844
0
    xchange = -xlimit;
845
0
  if (xlimit == 0) {
846
0
    if (sx <= lc->g.sx) /* lc->g.sx is minimum possible */
847
0
      xchange = 0;
848
0
    else
849
0
      xchange = sx - lc->g.sx;
850
0
  }
851
0
  if (xchange != 0)
852
0
    layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, xchange);
853
854
  /* Adjust vertically in a similar fashion. */
855
0
  ychange = sy - lc->g.sy;
856
0
  ylimit = layout_resize_check(w, lc, LAYOUT_TOPBOTTOM);
857
0
  if (ychange < 0 && ychange < -ylimit)
858
0
    ychange = -ylimit;
859
0
  if (ylimit == 0) {
860
0
    if (sy <= lc->g.sy) /* lc->g.sy is minimum possible */
861
0
      ychange = 0;
862
0
    else
863
0
      ychange = sy - lc->g.sy;
864
0
  }
865
0
  if (ychange != 0)
866
0
    layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, ychange);
867
868
  /* Fix cell offsets. */
869
0
  layout_fix_offsets(w);
870
0
  layout_clamp_floating_panes(w, sx, sy);
871
0
  layout_fix_panes(w, NULL);
872
0
}
873
874
/* Resize a pane to an absolute size. */
875
void
876
layout_resize_pane_to(struct window_pane *wp, enum layout_type type,
877
    u_int new_size)
878
0
{
879
0
  struct layout_cell     *lc, *lcparent;
880
0
  int     change, size;
881
882
0
  lc = wp->layout_cell;
883
884
  /* Find next parent of the same type. */
885
0
  lcparent = lc->parent;
886
0
  while (lcparent != NULL && lcparent->type != type) {
887
0
    lc = lcparent;
888
0
    lcparent = lc->parent;
889
0
  }
890
0
  if (lcparent == NULL)
891
0
    return;
892
893
  /* Work out the size adjustment. */
894
0
  if (type == LAYOUT_LEFTRIGHT)
895
0
    size = lc->g.sx;
896
0
  else
897
0
    size = lc->g.sy;
898
0
  if (layout_cell_is_last_tiled(lc))
899
0
    change = size - new_size;
900
0
  else
901
0
    change = new_size - size;
902
903
  /* Resize the pane. */
904
0
  layout_resize_pane(wp, type, change, 1);
905
0
}
906
907
/* Resize a floating pane to an absolute size. */
908
int
909
layout_resize_floating_pane_to(struct window_pane *wp, enum layout_type type,
910
    u_int size, char **cause)
911
0
{
912
0
  struct layout_cell  *lc = wp->layout_cell;
913
914
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
915
0
    *cause = xstrdup("pane is not floating");
916
0
    return (-1);
917
0
  }
918
919
0
  if (window_pane_get_pane_lines(wp) != PANE_LINES_NONE &&
920
0
      size >= PANE_MINIMUM + 2)
921
0
    size -= 2;
922
0
  if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
923
0
    *cause = xstrdup("size is too big or too small");
924
0
    return (-1);
925
0
  }
926
927
0
  if (type == LAYOUT_TOPBOTTOM) {
928
0
    if (lc->g.sy == size)
929
0
      return (0);
930
0
    lc->g.sy = size;
931
0
  } else {
932
0
    if (lc->g.sx == size)
933
0
      return (0);
934
0
    lc->g.sx = size;
935
0
  }
936
0
  redraw_invalidate_scene(wp->window);
937
0
  return (0);
938
0
}
939
940
/* Resize a floating pane relative to its current size. */
941
int
942
layout_resize_floating_pane(struct window_pane *wp, enum layout_type type,
943
    int change, int opposite, char **cause)
944
0
{
945
0
  struct layout_cell  *lc = wp->layout_cell;
946
0
  u_int      size;
947
948
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
949
0
    *cause = xstrdup("pane is not floating");
950
0
    return (-1);
951
0
  }
952
0
  if (change == 0)
953
0
    return (0);
954
955
0
  if (type == LAYOUT_TOPBOTTOM) {
956
0
    size = lc->g.sy + change;
957
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
958
0
      *cause = xstrdup("change is too big or too small");
959
0
      return (-1);
960
0
    }
961
0
    lc->g.sy = size;
962
0
    if (opposite)
963
0
      lc->g.yoff -= change;
964
0
  } else {
965
0
    size = lc->g.sx + change;
966
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
967
0
      *cause = xstrdup("change is too big or too small");
968
0
      return (-1);
969
0
    }
970
0
    lc->g.sx = size;
971
0
    if (opposite)
972
0
      lc->g.xoff -= change;
973
0
  }
974
0
  redraw_invalidate_scene(wp->window);
975
0
  return (0);
976
0
}
977
978
/* Resize a layout cell. */
979
void
980
layout_resize_layout(struct window *w, struct layout_cell *lc,
981
    enum layout_type type, int change, int opposite)
982
0
{
983
0
  int needed, size;
984
985
  /* Grow or shrink the cell. */
986
0
  needed = change;
987
0
  while (needed != 0) {
988
0
    if (change > 0) {
989
0
      size = layout_resize_pane_grow(w, lc, type, needed,
990
0
          opposite);
991
0
      needed -= size;
992
0
    } else {
993
0
      size = layout_resize_pane_shrink(w, lc, type, needed);
994
0
      needed += size;
995
0
    }
996
997
0
    if (size == 0) /* no more change possible */
998
0
      break;
999
0
  }
1000
1001
  /* Fix cell offsets. */
1002
0
  layout_fix_offsets(w);
1003
0
  layout_fix_panes(w, NULL);
1004
0
  events_fire_window("window-layout-changed", w);
1005
0
}
1006
1007
/* Resize a single pane within the layout. */
1008
void
1009
layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
1010
    int opposite)
1011
0
{
1012
0
  struct layout_cell  *lc = wp->layout_cell, *lcparent;
1013
1014
  /* Find next parent of the same type. */
1015
0
  lcparent = lc->parent;
1016
0
  while (lcparent != NULL && lcparent->type != type) {
1017
0
    lc = lcparent;
1018
0
    lcparent = lc->parent;
1019
0
  }
1020
0
  if (lcparent == NULL)
1021
0
    return;
1022
1023
  /* If this is the last tiled cell, move back one. */
1024
0
  if (layout_cell_is_last_tiled(lc)) {
1025
0
    lc = layout_cell_get_neighbour_dir(lc, 0);
1026
0
    if (lc == NULL)
1027
0
      return;
1028
0
  }
1029
1030
0
  layout_resize_layout(wp->window, lc, type, change, opposite);
1031
0
}
1032
1033
/* Helper function to grow pane. */
1034
static int
1035
layout_resize_pane_grow(struct window *w, struct layout_cell *lc,
1036
    enum layout_type type, int needed, int opposite)
1037
0
{
1038
0
  struct layout_cell  *lcadd, *lcremove;
1039
0
  u_int      size = 0;
1040
1041
  /* Growing. Always add to the current cell. */
1042
0
  lcadd = lc;
1043
1044
  /* Look towards the tail for a suitable cell for reduction. */
1045
0
  lcremove = layout_cell_get_neighbour_dir(lc, 1);
1046
0
  while (lcremove != NULL) {
1047
0
    size = layout_resize_check(w, lcremove, type);
1048
0
    if (size > 0)
1049
0
      break;
1050
0
    lcremove = layout_cell_get_neighbour_dir(lcremove, 1);
1051
0
  }
1052
1053
  /* If none found, look towards the head. */
1054
0
  if (opposite && lcremove == NULL) {
1055
0
    lcremove = layout_cell_get_neighbour_dir(lc, 0);
1056
0
    while (lcremove != NULL) {
1057
0
      size = layout_resize_check(w, lcremove, type);
1058
0
      if (size > 0)
1059
0
        break;
1060
0
      lcremove = layout_cell_get_neighbour_dir(lcremove, 0);
1061
0
    }
1062
0
  }
1063
0
  if (lcremove == NULL)
1064
0
    return (0);
1065
1066
  /* Change the cells. */
1067
0
  if (size > (u_int) needed)
1068
0
    size = needed;
1069
0
  layout_resize_adjust(w, lcadd, type, size);
1070
0
  layout_resize_adjust(w, lcremove, type, -size);
1071
0
  return (size);
1072
0
}
1073
1074
/* Helper function to shrink pane. */
1075
static int
1076
layout_resize_pane_shrink(struct window *w, struct layout_cell *lc,
1077
    enum layout_type type, int needed)
1078
0
{
1079
0
  struct layout_cell  *lcadd, *lcremove;
1080
0
  u_int      size;
1081
1082
  /* Shrinking. Find cell to remove from by walking towards head. */
1083
0
  lcremove = lc;
1084
0
  do {
1085
0
    size = layout_resize_check(w, lcremove, type);
1086
0
    if (size != 0)
1087
0
      break;
1088
0
    lcremove = layout_cell_get_neighbour_dir(lcremove, 0);
1089
0
  } while (lcremove != NULL);
1090
0
  if (lcremove == NULL)
1091
0
    return (0);
1092
1093
  /* And add onto the next cell (from the original cell). */
1094
0
  lcadd = layout_cell_get_neighbour_dir(lc, 1);
1095
0
  if (lcadd == NULL)
1096
0
    return (0);
1097
1098
  /* Change the cells. */
1099
0
  if (size > (u_int) -needed)
1100
0
    size = -needed;
1101
0
  layout_resize_adjust(w, lcadd, type, size);
1102
0
  layout_resize_adjust(w, lcremove, type, -size);
1103
0
  return (size);
1104
0
}
1105
1106
/* Assign window pane to new cell. */
1107
void
1108
layout_assign_pane(struct layout_cell *lc, struct window_pane *wp,
1109
    int do_not_resize)
1110
0
{
1111
0
  layout_make_leaf(lc, wp);
1112
0
  if (do_not_resize)
1113
0
    layout_fix_panes(wp->window, wp);
1114
0
  else
1115
0
    layout_fix_panes(wp->window, NULL);
1116
0
}
1117
1118
/* Calculate the new pane size for resized parent. */
1119
static u_int
1120
layout_new_pane_size(struct window *w, u_int previous, struct layout_cell *lc,
1121
    enum layout_type type, u_int size, u_int count_left, u_int size_left)
1122
0
{
1123
0
  u_int new_size, min, max, available;
1124
1125
  /* If this is the last cell, it can take all of the remaining size. */
1126
0
  if (count_left == 1)
1127
0
    return (size_left);
1128
1129
  /* How much is available in this parent? */
1130
0
  available = layout_resize_check(w, lc, type);
1131
1132
  /*
1133
   * Work out the minimum size of this cell and the new size
1134
   * proportionate to the previous size.
1135
   */
1136
0
  min = (PANE_MINIMUM + 1) * (count_left - 1);
1137
0
  if (type == LAYOUT_LEFTRIGHT) {
1138
0
    if (lc->g.sx - available > min)
1139
0
      min = lc->g.sx - available;
1140
0
    new_size = (lc->g.sx * size) / previous;
1141
0
  } else {
1142
0
    if (lc->g.sy - available > min)
1143
0
      min = lc->g.sy - available;
1144
0
    new_size = (lc->g.sy * size) / previous;
1145
0
  }
1146
1147
  /* Check against the maximum and minimum size. */
1148
0
  max = size_left - min;
1149
0
  if (new_size > max)
1150
0
    new_size = max;
1151
0
  if (new_size < PANE_MINIMUM)
1152
0
    new_size = PANE_MINIMUM;
1153
0
  return (new_size);
1154
0
}
1155
1156
/* Check if the cell and all its children can be resized to a specific size. */
1157
static int
1158
layout_set_size_check(struct window *w, struct layout_cell *lc,
1159
    enum layout_type type, int size)
1160
0
{
1161
0
  struct layout_cell  *lcchild;
1162
0
  u_int      new_size, available, previous, count, idx;
1163
1164
  /* Cells with no children must just be bigger than minimum. */
1165
0
  if (lc->type == LAYOUT_WINDOWPANE)
1166
0
    return (size >= PANE_MINIMUM);
1167
0
  available = size;
1168
1169
  /* Count number of children. */
1170
0
  count = 0;
1171
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry)
1172
0
    count++;
1173
1174
  /* Check new size will work for each child. */
1175
0
  if (lc->type == type) {
1176
0
    if (available < (count * 2) - 1)
1177
0
      return (0);
1178
1179
0
    if (type == LAYOUT_LEFTRIGHT)
1180
0
      previous = lc->g.sx;
1181
0
    else
1182
0
      previous = lc->g.sy;
1183
1184
0
    idx = 0;
1185
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1186
0
      new_size = layout_new_pane_size(w, previous, lcchild,
1187
0
          type, size, count - idx, available);
1188
0
      if (idx == count - 1) {
1189
0
        if (new_size > available)
1190
0
          return (0);
1191
0
        available -= new_size;
1192
0
      } else {
1193
0
        if (new_size + 1 > available)
1194
0
          return (0);
1195
0
        available -= new_size + 1;
1196
0
      }
1197
0
      if (!layout_set_size_check(w, lcchild, type, new_size))
1198
0
        return (0);
1199
0
      idx++;
1200
0
    }
1201
0
  } else {
1202
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1203
0
      if (lcchild->type == LAYOUT_WINDOWPANE)
1204
0
        continue;
1205
0
      if (!layout_set_size_check(w, lcchild, type, size))
1206
0
        return (0);
1207
0
    }
1208
0
  }
1209
1210
0
  return (1);
1211
0
}
1212
1213
/* Resize all child cells to fit within the current cell. */
1214
static void
1215
layout_resize_child_cells(struct window *w, struct layout_cell *lc)
1216
0
{
1217
0
  struct layout_cell  *lcchild;
1218
0
  u_int      prev, available, count, idx;
1219
1220
0
  if (lc->type == LAYOUT_WINDOWPANE)
1221
0
    return;
1222
1223
  /* What is the current size used? */
1224
0
  count = 0;
1225
0
  prev = 0;
1226
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1227
0
    if (!layout_cell_is_tiled(lcchild) &&
1228
0
        !layout_cell_has_tiled_child(lcchild))
1229
0
      continue;
1230
0
    count++;
1231
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1232
0
      prev += lcchild->g.sx;
1233
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1234
0
      prev += lcchild->g.sy;
1235
0
  }
1236
0
  prev += (count - 1);
1237
1238
  /* And how much is available? */
1239
0
  available = 0;
1240
0
  if (lc->type == LAYOUT_LEFTRIGHT)
1241
0
    available = lc->g.sx;
1242
0
  else if (lc->type == LAYOUT_TOPBOTTOM)
1243
0
    available = lc->g.sy;
1244
1245
  /* Resize children into the new size. */
1246
0
  idx = 0;
1247
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1248
0
    if (!layout_cell_is_tiled(lcchild) &&
1249
0
        !layout_cell_has_tiled_child(lcchild))
1250
0
      continue;
1251
0
    if (lc->type == LAYOUT_TOPBOTTOM) {
1252
0
      lcchild->g.sx = lc->g.sx;
1253
0
      lcchild->g.xoff = lc->g.xoff;
1254
0
    } else {
1255
0
      lcchild->g.sx = layout_new_pane_size(w, prev, lcchild,
1256
0
          lc->type, lc->g.sx, count - idx, available);
1257
0
      available -= (lcchild->g.sx + 1);
1258
0
    }
1259
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1260
0
      lcchild->g.sy = lc->g.sy;
1261
0
      lcchild->g.yoff = lc->g.yoff;
1262
0
    } else {
1263
0
      lcchild->g.sy = layout_new_pane_size(w, prev, lcchild,
1264
0
          lc->type, lc->g.sy, count - idx, available);
1265
0
      available -= (lcchild->g.sy + 1);
1266
0
    }
1267
0
    layout_resize_child_cells(w, lcchild);
1268
0
    idx++;
1269
0
  }
1270
0
}
1271
1272
/*
1273
 * Replaces the provided layout cell with a new node of the specified type and
1274
 * inserts the cell into it. Used when creating new cells requires a different
1275
 * layout type, or when the root layout is a window pane.
1276
 */
1277
struct layout_cell *
1278
layout_replace_with_node(struct window *w, struct layout_cell *lc,
1279
    enum layout_type type)
1280
0
{
1281
0
  struct layout_cell  *lcparent;
1282
1283
0
  lcparent = layout_create_cell(lc->parent);
1284
0
  layout_make_node(lcparent, type);
1285
0
  layout_set_size(lcparent, lc->g.sx, lc->g.sy, lc->g.xoff, lc->g.yoff);
1286
0
  if (lc->parent == NULL)
1287
0
    w->layout_root = lcparent;
1288
0
  else
1289
0
    TAILQ_REPLACE(&lc->parent->cells, lc, lcparent, entry);
1290
1291
  /* Insert the old cell. */
1292
0
  lc->parent = lcparent;
1293
0
  TAILQ_INSERT_HEAD(&lcparent->cells, lc, entry);
1294
1295
0
  return (lcparent);
1296
0
}
1297
1298
/* Checks if there is enough space for two new panes. */
1299
int
1300
layout_split_check_space(struct window_pane *wp, struct layout_cell *lc,
1301
   enum layout_type type)
1302
0
{
1303
0
  struct layout_cell  *root = wp->window->layout_root;
1304
0
  struct style    *sb_style = &wp->scrollbar_style;
1305
0
  u_int      minimum, sx = lc->g.sx, sy = lc->g.sy;
1306
0
  int      status;
1307
1308
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1309
0
    fatalx("floating cells cannot be split");
1310
1311
0
  status = window_get_pane_status(wp->window);
1312
1313
0
  switch (type) {
1314
0
  case LAYOUT_LEFTRIGHT:
1315
0
    if (wp->window->sb == PANE_SCROLLBARS_ALWAYS) {
1316
0
      minimum = PANE_MINIMUM * 2 + sb_style->width +
1317
0
          sb_style->pad;
1318
0
    } else
1319
0
      minimum = PANE_MINIMUM * 2 + 1;
1320
0
    if (sx < minimum)
1321
0
      return (0);
1322
0
    break;
1323
0
  case LAYOUT_TOPBOTTOM:
1324
0
    if (layout_add_horizontal_border(root, lc, status))
1325
0
      minimum = PANE_MINIMUM * 2 + 2;
1326
0
    else
1327
0
      minimum = PANE_MINIMUM * 2 + 1;
1328
0
    if (sy < minimum)
1329
0
      return (0);
1330
0
    break;
1331
0
  default:
1332
0
    fatalx("bad layout type");
1333
0
  }
1334
1335
0
  return (1);
1336
0
}
1337
1338
/* Calculates the new cell sizes when splitting a pane. */
1339
void
1340
layout_split_sizes(struct layout_cell *lc, int size, int before,
1341
    enum layout_type type, u_int *size1, u_int *size2, u_int *saved_size)
1342
0
{
1343
0
  u_int s1, s2, ss;
1344
0
  u_int sx = lc->g.sx, sy = lc->g.sy;
1345
1346
0
  if (type == LAYOUT_LEFTRIGHT)
1347
0
    ss = sx;
1348
0
  else
1349
0
    ss = sy;
1350
0
  if (size < 0)
1351
0
    s2 = ((ss + 1) / 2) - 1;
1352
0
  else if (before)
1353
0
    s2 = ss - size - 1;
1354
0
  else
1355
0
    s2 = size;
1356
0
  if (s2 < PANE_MINIMUM)
1357
0
    s2 = PANE_MINIMUM;
1358
0
  else if (s2 > ss - 2)
1359
0
    s2 = ss - 2;
1360
0
  s1 = ss - 1 - s2;
1361
1362
0
  *size1 = s1;
1363
0
  *size2 = s2;
1364
0
  *saved_size = ss;
1365
0
}
1366
1367
/*
1368
 * Split a pane into two. size is a hint, or -1 for default half/half
1369
 * split. This must be followed by layout_assign_pane before much else happens!
1370
 */
1371
struct layout_cell *
1372
layout_split_pane(struct window_pane *wp, enum layout_type type, int size,
1373
    int flags)
1374
0
{
1375
0
  struct layout_cell  *lc, *lcparent, *lcnew, *lc1, *lc2;
1376
0
  u_int      sx, sy, xoff, yoff, size1, size2;
1377
0
  u_int      new_size, saved_size, resize_first = 0;
1378
0
  int      full_size = (flags & SPAWN_FULLSIZE);
1379
0
  int      before = (flags & SPAWN_BEFORE);
1380
1381
  /*
1382
   * If full_size is specified, add a new cell at the top of the window
1383
   * layout. Otherwise, split the cell for the current pane.
1384
   */
1385
0
  if (full_size)
1386
0
    lc = wp->window->layout_root;
1387
0
  else
1388
0
    lc = wp->layout_cell;
1389
1390
  /* Copy the old cell size. */
1391
0
  sx = lc->g.sx;
1392
0
  sy = lc->g.sy;
1393
0
  xoff = lc->g.xoff;
1394
0
  yoff = lc->g.yoff;
1395
1396
  /* Check there is enough space for the two new panes. */
1397
0
  if (!layout_split_check_space(wp, lc, type))
1398
0
    return (NULL);
1399
1400
  /*
1401
   * Calculate new cell sizes. size is the target size or -1 for middle
1402
   * split, size1 is the size of the top/left and size2 the bottom/right.
1403
   */
1404
0
  layout_split_sizes(lc, size, before, type, &size1, &size2, &saved_size);
1405
1406
  /* Which size are we using? */
1407
0
  if (flags & SPAWN_BEFORE)
1408
0
    new_size = size2;
1409
0
  else
1410
0
    new_size = size1;
1411
1412
  /* Confirm there is enough space for full size pane. */
1413
0
  if (full_size && !layout_set_size_check(wp->window, lc, type, new_size))
1414
0
    return (NULL);
1415
1416
0
  if (lc->parent != NULL && lc->parent->type == type) {
1417
    /*
1418
     * If the parent exists and is of the same type as the split,
1419
     * create a new cell and insert it after this one.
1420
     */
1421
0
    lcparent = lc->parent;
1422
0
    lcnew = layout_create_cell(lcparent);
1423
0
    if (flags & SPAWN_BEFORE)
1424
0
      TAILQ_INSERT_BEFORE(lc, lcnew, entry);
1425
0
    else
1426
0
      TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1427
0
  } else if (full_size && lc->parent == NULL && lc->type == type) {
1428
    /*
1429
     * If the new full size pane is the same type as the root
1430
     * split, insert the new pane under the existing root cell
1431
     * instead of creating a new root cell. The existing layout
1432
     * must be resized before inserting the new cell.
1433
     */
1434
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1435
0
      lc->g.sx = new_size;
1436
0
      layout_resize_child_cells(wp->window, lc);
1437
0
      lc->g.sx = saved_size;
1438
0
    } else if (lc->type == LAYOUT_TOPBOTTOM) {
1439
0
      lc->g.sy = new_size;
1440
0
      layout_resize_child_cells(wp->window, lc);
1441
0
      lc->g.sy = saved_size;
1442
0
    }
1443
0
    resize_first = 1;
1444
1445
    /* Create the new cell. */
1446
0
    lcnew = layout_create_cell(lc);
1447
0
    size = saved_size - 1 - new_size;
1448
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1449
0
      layout_set_size(lcnew, size, sy, 0, 0);
1450
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1451
0
      layout_set_size(lcnew, sx, size, 0, 0);
1452
0
    if (flags & SPAWN_BEFORE)
1453
0
      TAILQ_INSERT_HEAD(&lc->cells, lcnew, entry);
1454
0
    else
1455
0
      TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
1456
0
  } else {
1457
    /*
1458
     * Otherwise create a new parent and insert it.
1459
     */
1460
1461
    /* Create and insert the replacement parent. */
1462
0
    lcparent = layout_replace_with_node(wp->window, lc, type);
1463
1464
    /* Create the new child cell. */
1465
0
    lcnew = layout_create_cell(lcparent);
1466
0
    if (flags & SPAWN_BEFORE)
1467
0
      TAILQ_INSERT_HEAD(&lcparent->cells, lcnew, entry);
1468
0
    else
1469
0
      TAILQ_INSERT_TAIL(&lcparent->cells, lcnew, entry);
1470
0
  }
1471
0
  if (flags & SPAWN_BEFORE) {
1472
0
    lc1 = lcnew;
1473
0
    lc2 = lc;
1474
0
  } else {
1475
0
    lc1 = lc;
1476
0
    lc2 = lcnew;
1477
0
  }
1478
1479
  /*
1480
   * Set new cell sizes. size1 is the size of the top/left and size2 the
1481
   * bottom/right.
1482
   */
1483
0
  if (!resize_first && type == LAYOUT_LEFTRIGHT) {
1484
0
    layout_set_size(lc1, size1, sy, xoff, yoff);
1485
0
    layout_set_size(lc2, size2, sy, xoff + lc1->g.sx + 1, yoff);
1486
0
  } else if (!resize_first && type == LAYOUT_TOPBOTTOM) {
1487
0
    layout_set_size(lc1, sx, size1, xoff, yoff);
1488
0
    layout_set_size(lc2, sx, size2, xoff, yoff + lc1->g.sy + 1);
1489
0
  }
1490
0
  if (full_size) {
1491
0
    if (!resize_first)
1492
0
      layout_resize_child_cells(wp->window, lc);
1493
0
    layout_fix_offsets(wp->window);
1494
0
  } else
1495
0
    layout_make_leaf(lc, wp);
1496
1497
0
  return (lcnew);
1498
0
}
1499
1500
/*
1501
 * Creates a cell for a new floating pane. This must be followed by
1502
 * layout_assign_pane before much else happens!
1503
 */
1504
struct layout_cell *
1505
layout_floating_pane(struct window *w, struct window_pane *wp,
1506
    struct layout_geometry *lg)
1507
0
{
1508
0
  struct layout_cell  *lc, *lcnew, *lcparent;
1509
1510
0
  if (wp == NULL)
1511
0
    lc = w->layout_root;
1512
0
  else
1513
0
    lc = wp->layout_cell;
1514
0
  lcparent = lc->parent;
1515
1516
0
  if (lcparent == NULL) {
1517
    /*
1518
     * Adding a pane to a root that isn't a node. Must create and
1519
     * insert a new root.
1520
     */
1521
0
    lcparent = layout_replace_with_node(w, lc, LAYOUT_TOPBOTTOM);
1522
0
  }
1523
1524
0
  lcnew = layout_create_cell(lcparent);
1525
0
  TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1526
0
  lcnew->flags |= LAYOUT_CELL_FLOATING;
1527
0
  layout_set_size(lcnew, lg->sx, lg->sy, lg->xoff, lg->yoff);
1528
1529
0
  return (lcnew);
1530
0
}
1531
1532
/* Destroy the cell associated with a pane. */
1533
void
1534
layout_close_pane(struct window_pane *wp)
1535
0
{
1536
0
  struct window *w = wp->window;
1537
1538
0
  if (wp->layout_cell == NULL)
1539
0
    return;
1540
1541
  /* Remove the cell. */
1542
0
  layout_destroy_cell(w, wp->layout_cell, &w->layout_root);
1543
0
  wp->layout_cell = NULL;
1544
1545
  /* Fix pane offsets and sizes. */
1546
0
  if (w->layout_root != NULL) {
1547
0
    layout_fix_offsets(w);
1548
0
    layout_fix_panes(w, NULL);
1549
0
  }
1550
0
  events_fire_window("window-layout-changed", w);
1551
0
}
1552
1553
/* Spread out cells inside a parent cell. */
1554
int
1555
layout_spread_cell(struct window *w, struct layout_cell *parent)
1556
0
{
1557
0
  struct layout_cell  *lc, *root = w->layout_root;
1558
0
  u_int      number, each, size, this, remainder;
1559
0
  int      change, changed, status;
1560
1561
0
  number = 0;
1562
0
  TAILQ_FOREACH (lc, &parent->cells, entry)
1563
0
    if (layout_cell_is_tiled(lc))
1564
0
      number++;
1565
0
  if (number <= 1)
1566
0
    return (0);
1567
0
  status = window_get_pane_status(w);
1568
1569
0
  if (parent->type == LAYOUT_LEFTRIGHT)
1570
0
    size = parent->g.sx;
1571
0
  else if (parent->type == LAYOUT_TOPBOTTOM) {
1572
0
    if (layout_add_horizontal_border(root, parent, status))
1573
0
      size = parent->g.sy - 1;
1574
0
    else
1575
0
      size = parent->g.sy;
1576
0
  } else
1577
0
    return (0);
1578
0
  if (size < number - 1)
1579
0
    return (0);
1580
0
  each = (size - (number - 1)) / number;
1581
0
  if (each == 0)
1582
0
    return (0);
1583
1584
  /*
1585
   * Remaining space after assigning that which can be evenly
1586
   * distributed.
1587
   */
1588
0
  remainder = size - (number * (each + 1)) + 1;
1589
1590
0
  changed = 0;
1591
0
  TAILQ_FOREACH (lc, &parent->cells, entry) {
1592
0
    if (!layout_cell_is_tiled(lc))
1593
0
      continue;
1594
0
    change = 0;
1595
0
    if (parent->type == LAYOUT_LEFTRIGHT) {
1596
0
      change = each - (int)lc->g.sx;
1597
0
      if (remainder > 0) {
1598
0
        change++;
1599
0
        remainder--;
1600
0
      }
1601
0
      layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, change);
1602
0
    } else if (parent->type == LAYOUT_TOPBOTTOM) {
1603
0
      if (layout_add_horizontal_border(root, lc, status))
1604
0
        this = each + 1;
1605
0
      else
1606
0
        this = each;
1607
0
      if (remainder > 0) {
1608
0
        this++;
1609
0
        remainder--;
1610
0
      }
1611
0
      change = this - (int)lc->g.sy;
1612
0
      layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, change);
1613
0
    }
1614
0
    if (change != 0)
1615
0
      changed = 1;
1616
0
  }
1617
0
  return (changed);
1618
0
}
1619
1620
/* Spread out cells evenly. */
1621
void
1622
layout_spread_out(struct window_pane *wp)
1623
0
{
1624
0
  struct layout_cell  *parent;
1625
0
  struct window   *w = wp->window;
1626
1627
0
  parent = wp->layout_cell->parent;
1628
0
  if (parent == NULL)
1629
0
    return;
1630
1631
0
  do {
1632
0
    if (layout_spread_cell(w, parent)) {
1633
0
      layout_fix_offsets(w);
1634
0
      layout_fix_panes(w, NULL);
1635
0
      break;
1636
0
    }
1637
0
  } while ((parent = parent->parent) != NULL);
1638
0
}
1639
1640
/* Get a new tiled cell. */
1641
struct layout_cell *
1642
layout_get_tiled_cell(struct cmdq_item *item, struct args *args,
1643
    struct window *w, struct window_pane *wp, int flags, char **cause)
1644
0
{
1645
0
  struct layout_cell  *lc;
1646
0
  enum layout_type   type = LAYOUT_TOPBOTTOM;
1647
0
  u_int      curval;
1648
0
  int      size = -1;
1649
0
  char      *error = NULL;
1650
1651
0
  if (window_pane_is_floating(wp)) {
1652
0
    *cause = xstrdup("can't split a floating pane");
1653
0
    return (NULL);
1654
0
  }
1655
1656
0
  if (flags & SPAWN_HORIZONTAL)
1657
0
    type = LAYOUT_LEFTRIGHT;
1658
1659
0
  if (args_has(args, 'l') || args_has(args, 'p')) {
1660
0
    if (flags & SPAWN_FULLSIZE) {
1661
0
      if (type == LAYOUT_TOPBOTTOM)
1662
0
        curval = w->sy;
1663
0
      else
1664
0
        curval = w->sx;
1665
0
    } else {
1666
0
      if (type == LAYOUT_TOPBOTTOM)
1667
0
        curval = wp->sy;
1668
0
      else
1669
0
        curval = wp->sx;
1670
0
    }
1671
0
  }
1672
1673
0
  if (args_has(args, 'l')) {
1674
0
    size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval,
1675
0
        item, &error);
1676
0
  } else if (args_has(args, 'p')) {
1677
0
    size = args_strtonum_and_expand(args, 'p', 0, 100, item,
1678
0
        &error);
1679
0
    if (error == NULL)
1680
0
      size = curval * size / 100;
1681
0
  }
1682
0
  if (error != NULL) {
1683
0
    xasprintf(cause, "invalid tiled geometry %s", error);
1684
0
    free(error);
1685
0
    return (NULL);
1686
0
  }
1687
1688
0
  if (window_active_pane_is_over_zoom(w))
1689
0
    window_push_zoom(w, 0, 1);
1690
0
  else
1691
0
    window_push_zoom(w, 1, (flags & SPAWN_ZOOM));
1692
0
  lc = layout_split_pane(wp, type, size, flags);
1693
0
  if (lc == NULL)
1694
0
    *cause = xstrdup("no space for a new pane");
1695
1696
0
  return (lc);
1697
0
}
1698
1699
struct layout_cell *
1700
layout_get_floating_cell(struct cmdq_item *item, struct args *args,
1701
    enum pane_lines lines, struct window *w, struct window_pane *wp, int flags,
1702
    char **cause)
1703
0
{
1704
0
  struct layout_cell  *lcnew, *lc = wp->layout_cell;
1705
0
  struct layout_geometry   fg;
1706
1707
0
  layout_geometry_init(&fg);
1708
0
  if (flags & SPAWN_SPLIT) {
1709
0
    if (layout_split_floating_cell(lc, w, &fg, lines, flags, cause)
1710
0
        != 0)
1711
0
      return (NULL);
1712
0
  } else {
1713
0
    if (layout_floating_args_parse(item, args, lines, w, &fg, cause)
1714
0
        != 0)
1715
0
      return (NULL);
1716
0
  }
1717
1718
0
  if (flags & SPAWN_FLOATOVERZOOM)
1719
0
    window_push_zoom(wp->window, 0, 1);
1720
0
  else if (window_active_pane_is_over_zoom(w))
1721
0
    window_push_zoom(wp->window, 0, 1);
1722
0
  else
1723
0
    window_push_zoom(wp->window, 1, (flags & SPAWN_ZOOM));
1724
0
  lcnew = layout_floating_pane(w, wp, &fg);
1725
0
  return (lcnew);
1726
0
}
1727
1728
int
1729
layout_floating_args_parse(struct cmdq_item *item, struct args *args,
1730
    enum pane_lines lines, struct window *w, struct layout_geometry *lg,
1731
    char **cause)
1732
0
{
1733
0
  int  sx, sy, ox, oy, pad;
1734
0
  char  *error = NULL;
1735
1736
0
  sx = lg->sx == UINT_MAX ? w->sx / 2 : lg->sx;
1737
0
  sy = lg->sy == UINT_MAX ? w->sy / 4 : lg->sy;
1738
0
  ox = lg->xoff;
1739
0
  oy = lg->yoff;
1740
1741
0
  if (args_has(args, 'x')) {
1742
0
    sx = args_percentage_and_expand(args, 'x', 0, PANE_MAXIMUM,
1743
0
        w->sx, item, &error);
1744
0
    if (error != NULL) {
1745
0
      xasprintf(cause, "position %s", error);
1746
0
      free(error);
1747
0
      return (-1);
1748
0
    }
1749
0
    if (lines != PANE_LINES_NONE)
1750
0
      sx -= 2;
1751
0
  }
1752
0
  if (args_has(args, 'y')) {
1753
0
    sy = args_percentage_and_expand(args, 'y', 0, PANE_MAXIMUM,
1754
0
        w->sy, item, &error);
1755
0
    if (error != NULL) {
1756
0
      xasprintf(cause, "position %s", error);
1757
0
      free(error);
1758
0
      return (-1);
1759
0
    }
1760
0
    if (lines != PANE_LINES_NONE)
1761
0
      sy -= 2;
1762
0
  }
1763
0
  if (args_has(args, 'X')) {
1764
0
    ox = args_percentage_and_expand(args, 'X', -sx, w->sx,
1765
0
        w->sx, item, &error);
1766
0
    if (error != NULL) {
1767
0
      xasprintf(cause, "position %s", error);
1768
0
      free(error);
1769
0
      return (-1);
1770
0
    }
1771
0
  }
1772
0
  if (args_has(args, 'Y')) {
1773
0
    oy = args_percentage_and_expand(args, 'Y', -sy, w->sy,
1774
0
        w->sy, item, &error);
1775
0
    if (error != NULL) {
1776
0
      xasprintf(cause, "position %s", error);
1777
0
      free(error);
1778
0
      return (-1);
1779
0
    }
1780
0
  }
1781
1782
0
  if (!window_has_floating_panes(w)) {
1783
0
    w->last_new_pane_x = 0;
1784
0
    w->last_new_pane_y = 0;
1785
0
  }
1786
0
  if (ox == INT_MAX) {
1787
0
    if (w->last_new_pane_x == 0)
1788
0
      ox = 4;
1789
0
    else {
1790
0
      if (lines != PANE_LINES_NONE)
1791
0
        pad = 1;
1792
0
      else
1793
0
        pad = 0;
1794
0
      ox = w->last_new_pane_x + 4;
1795
0
      if (ox + sx + pad > (int)w->sx)
1796
0
        ox = 4;
1797
0
    }
1798
0
    w->last_new_pane_x = ox;
1799
0
  } else if (args_has(args, 'X'))
1800
0
    if (lines != PANE_LINES_NONE)
1801
0
      ox += 1;
1802
0
  if (oy == INT_MAX) {
1803
0
    if (w->last_new_pane_y == 0)
1804
0
      oy = 2;
1805
0
    else {
1806
0
      if (lines != PANE_LINES_NONE)
1807
0
        pad = 1;
1808
0
      else
1809
0
        pad = 0;
1810
0
      oy = w->last_new_pane_y + 2;
1811
0
      if (oy + sy + pad > (int)w->sy)
1812
0
        oy = 2;
1813
0
    }
1814
0
    w->last_new_pane_y = oy;
1815
0
  } else if (args_has(args, 'Y'))
1816
0
    if (lines != PANE_LINES_NONE)
1817
0
      oy += 1;
1818
1819
0
  if (sx < PANE_MINIMUM || sx > PANE_MAXIMUM) {
1820
0
    *cause = xstrdup("invalid width");
1821
0
    return (-1);
1822
0
  }
1823
0
  if (sy < PANE_MINIMUM || sy > PANE_MAXIMUM) {
1824
0
    *cause = xstrdup("invalid height");
1825
0
    return (-1);
1826
0
  }
1827
1828
0
  lg->sx = sx;
1829
0
  lg->sy = sy;
1830
0
  lg->xoff = ox;
1831
0
  lg->yoff = oy;
1832
0
  return (0);
1833
0
}
1834
1835
int
1836
layout_split_floating_cell(struct layout_cell *lc, struct window *w,
1837
    struct layout_geometry *out, enum pane_lines lines, int flags,
1838
    char **cause)
1839
0
{
1840
0
  struct layout_geometry   old, new;
1841
0
  int      tborder = 1, bborder = w->sy - 1;
1842
0
  int      lborder = 3, rborder = w->sx - 3;
1843
0
  int      border = lines != PANE_LINES_NONE ? 1 : 0;
1844
0
  int      size, space;
1845
1846
  /* First, move the target cell in-bounds. */
1847
0
  memcpy(&old, &lc->g, sizeof old);
1848
0
  if (lborder > old.xoff - border)
1849
0
    old.xoff = lborder + border;
1850
0
  if (rborder < old.xoff + (int)old.sx + border)
1851
0
    old.xoff = rborder - (int)old.sx - border;
1852
0
  if (tborder > old.yoff - border)
1853
0
    old.yoff = tborder + border;
1854
0
  if (bborder < old.yoff + (int)old.sy + border)
1855
0
    old.yoff = bborder - (int)old.sy - border;
1856
1857
  /* Move the new cell to its ideal position. */
1858
0
  memcpy(&new, &old, sizeof new);
1859
0
  if (flags & SPAWN_HORIZONTAL) {
1860
0
    if (flags & SPAWN_BEFORE)
1861
0
      new.xoff -= old.sx + 2 * border;
1862
0
    else
1863
0
      new.xoff += old.sx + 2 * border;
1864
0
  } else {
1865
0
    if (flags & SPAWN_BEFORE)
1866
0
      new.yoff -= old.sy + 2 * border;
1867
0
    else
1868
0
      new.yoff += old.sy + 2 * border;
1869
0
  }
1870
1871
  /*
1872
   * The position of the new cell is checked to see if it is in bounds.
1873
   * If it isn't, the availible space is split and equally given to both
1874
   * cells. Only one border is check because the target cell is in bounds
1875
   * already.
1876
   */
1877
0
  if (lborder > new.xoff - border) {
1878
    /*
1879
     * The space for both panes is calculated. Since the offsets are
1880
     * associated to where pane contents start, we remove pane
1881
     * borders from the space. '1' is added in case the space is
1882
     * odd.
1883
     */
1884
0
    space = old.xoff + old.sx - lborder - 3 * border + 1;
1885
0
    size = space / 2;
1886
0
    new.sx = size;
1887
0
    old.sx = size;
1888
0
    new.xoff = lborder + border;
1889
0
    old.xoff = new.xoff + new.sx + 2 * border;
1890
    /*
1891
     * If the original space was to be odd (now even), subtract 1
1892
     * from the rightmost cell
1893
     */
1894
0
    if (space % 2 == 0)
1895
0
      old.sx -= 1;
1896
0
  } else if (rborder < new.xoff + (int)new.sx + border) {
1897
0
    space = rborder - old.xoff - 3 * border + 1;
1898
0
    size = space / 2;
1899
0
    new.sx = size;
1900
0
    old.sx = size;
1901
0
    new.xoff = old.xoff + old.sx + 2 * border;
1902
0
    if (space % 2 == 0)
1903
0
      new.sx -= 1;
1904
0
  } else if (tborder > new.yoff - border) {
1905
0
    space = old.sy + old.yoff - tborder - 3 * border + 1;
1906
0
    size = space / 2;
1907
0
    new.sy = size;
1908
0
    old.sy = size;
1909
0
    new.yoff = tborder + border;
1910
0
    old.yoff = new.yoff + new.sy + 2 * border;
1911
0
    if (space % 2 == 0)
1912
0
      old.sy -= 1;
1913
0
  } else if (bborder < new.yoff + (int)new.sy + border) {
1914
0
    space = bborder - old.yoff - 3 * border + 1;
1915
0
    size = space / 2;
1916
0
    new.sy = size;
1917
0
    old.sy = size;
1918
0
    new.yoff = old.yoff + old.sy + 2 * border;
1919
0
    if (space % 2 == 0)
1920
0
      new.sy -= 1;
1921
0
  }
1922
1923
  /*
1924
   * Expand the cell to occupy the whole availible space where it was
1925
   * spawned.
1926
   */
1927
0
  if (flags & SPAWN_FULLSIZE) {
1928
0
    if (flags & SPAWN_HORIZONTAL) {
1929
0
      new.yoff = tborder + border;
1930
0
      new.sy = bborder - tborder - 2 * border;
1931
0
      if (flags & SPAWN_BEFORE) {
1932
0
        new.xoff = lborder + border;
1933
0
        new.sx = old.xoff - new.xoff - 2 * border;
1934
0
      } else {
1935
0
        new.sx = rborder - new.xoff - border;
1936
0
      }
1937
0
    } else {
1938
0
      new.xoff = lborder + border;
1939
0
      new.sx = rborder - lborder - 2 * border;
1940
0
      if (flags & SPAWN_BEFORE) {
1941
0
        new.yoff = tborder + border;
1942
0
        new.sy = old.yoff - new.yoff - 2 * border;
1943
0
      } else {
1944
0
        new.sy = bborder - new.yoff - border;
1945
0
      }
1946
0
    }
1947
0
  }
1948
1949
0
  if (new.sx < PANE_MINIMUM || new.sy < PANE_MINIMUM ||
1950
0
      old.sx < PANE_MINIMUM || old.sy < PANE_MINIMUM) {
1951
0
    *cause = xstrdup("no space for a new pane");
1952
0
    return (-1);
1953
0
  }
1954
1955
0
  layout_set_size(lc, old.sx, old.sy, old.xoff, old.yoff);
1956
0
  memcpy(out, &new, sizeof *out);
1957
0
  return (0);
1958
0
}
1959
1960
/*
1961
 * Removes a cell from the tiled layout by giving the cell's space to the
1962
 * nearest neighbour.
1963
 */
1964
int
1965
layout_remove_tile(struct window *w, struct layout_cell *lc)
1966
0
{
1967
0
  struct layout_cell  *lcneighbour, *lcparent;
1968
0
  enum layout_type   type;
1969
0
  int      change;
1970
1971
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1972
0
    return (-1);
1973
1974
0
  lcneighbour = layout_cell_get_neighbour(lc);
1975
0
  if (lcneighbour == NULL) {
1976
0
    if (lc->parent != NULL)
1977
0
      layout_remove_tile(w, lc->parent);
1978
0
  } else if ((lcparent = lcneighbour->parent) != NULL) {
1979
0
    type = lcparent->type;
1980
    /*
1981
     * Adding the size of the layout cell plus its border to the
1982
     * neighbour.
1983
     */
1984
0
    if (type == LAYOUT_TOPBOTTOM)
1985
0
      change = lc->g.sy + 1;
1986
0
    else
1987
0
      change = lc->g.sx + 1;
1988
0
    layout_resize_adjust(w, lcneighbour, type, change);
1989
0
  }
1990
1991
  /*
1992
   * Zeroing out the cell geometry until the cell is retiled unless this
1993
   * is the top level node.
1994
   */
1995
0
  if (lc->parent != NULL)
1996
0
    layout_set_size(lc, 0, 0, 0, 0);
1997
0
  return (0);
1998
0
}
1999
2000
/*
2001
 * Inserts a cell back into the tiled layout by taking half the space from its
2002
 * nearest neighbour.
2003
 */
2004
int
2005
layout_insert_tile(struct window *w, struct layout_cell *lc)
2006
0
{
2007
0
  struct layout_cell  *lcneighbour, *lctiled, *lcparent;
2008
0
  enum layout_type   type;
2009
0
  u_int      size1, size2, saved_size;
2010
2011
0
  if (lc == NULL)
2012
0
    fatalx("layout cell cannot be null when tiling");
2013
2014
0
  if (layout_cell_is_tiled(lc))
2015
0
    return (-1);
2016
2017
0
  lcparent = lc->parent;
2018
0
  if (lcparent == NULL) {
2019
    /* Only pane in the layout. */
2020
0
    layout_set_size(lc, w->sx, w->sy, 0, 0);
2021
0
    return (0);
2022
0
  }
2023
2024
0
  type = lcparent->type;
2025
0
  lcneighbour = layout_cell_get_neighbour(lc);
2026
0
  if (lcneighbour == NULL) {
2027
    /*
2028
     * This will become the only visible cell in the parent.
2029
     * Tile the parent, then set the child's 'split' size.
2030
     */
2031
0
    layout_insert_tile(w, lcparent);
2032
0
    if (type == LAYOUT_LEFTRIGHT)
2033
0
      size1 = lcparent->g.sx;
2034
0
    else
2035
0
      size1 = lcparent->g.sy;
2036
0
    layout_resize_set_size(w, lc, type, size1);
2037
0
  } else {
2038
    /*
2039
     * If the neighbour is a node, a tiled child in the subtree of
2040
     * the neighbour is needed to check for space.
2041
     */
2042
0
    lctiled = layout_cell_get_first_tiled(lcneighbour);
2043
0
    if (!layout_split_check_space(lctiled->wp, lcneighbour, type))
2044
0
      return (-1);
2045
0
    layout_split_sizes(lcneighbour, -1, 0, type, &size1, &size2,
2046
0
        &saved_size);
2047
0
    layout_resize_set_size(w, lc, type, size1);
2048
0
    layout_resize_set_size(w, lcneighbour, type, size2);
2049
0
  }
2050
2051
  /* Setting opposite of the 'split' size to that of the parent. */
2052
0
  if (lcparent->type == LAYOUT_LEFTRIGHT) {
2053
0
    size1 = lcparent->g.sy;
2054
0
    type = LAYOUT_TOPBOTTOM;
2055
0
  } else {
2056
0
    size1 = lcparent->g.sx;
2057
0
    type = LAYOUT_LEFTRIGHT;
2058
0
  }
2059
0
  layout_resize_set_size(w, lc, type, size1);
2060
2061
0
  return (0);
2062
0
}