Coverage Report

Created: 2026-07-09 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/layout.c
Line
Count
Source
1
/* $OpenBSD$ */
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
22.2k
{
90
22.2k
  struct layout_cell  *lcchild, *lcnext;
91
92
22.2k
  if (lc == NULL || (only_nodes && lc->type == LAYOUT_WINDOWPANE))
93
22.2k
    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) {
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
/* Fix z-indexes. */
237
void
238
layout_fix_zindexes(struct window *w, struct layout_cell *lc)
239
0
{
240
0
  struct layout_cell  *lcchild;
241
242
0
  if (lc == NULL)
243
0
    return;
244
245
0
  switch (lc->type) {
246
0
  case LAYOUT_WINDOWPANE:
247
0
    TAILQ_INSERT_TAIL(&w->z_index, lc->wp, zentry);
248
0
    break;
249
0
  case LAYOUT_LEFTRIGHT:
250
0
  case LAYOUT_TOPBOTTOM:
251
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
252
0
      layout_fix_zindexes(w, lcchild);
253
0
    return;
254
0
  default:
255
0
    fatalx("bad layout type");
256
0
  }
257
0
}
258
259
int
260
layout_cell_is_tiled(struct layout_cell *lc)
261
0
{
262
0
  int is_leaf = lc->type == LAYOUT_WINDOWPANE;
263
0
  int is_floating = lc->flags & LAYOUT_CELL_FLOATING;
264
265
0
  return is_leaf && !is_floating;
266
0
}
267
268
static int
269
layout_cell_has_tiled_child(struct layout_cell *lc)
270
0
{
271
0
  struct layout_cell      *lcchild;
272
273
0
  if (lc->type == LAYOUT_WINDOWPANE)
274
0
    return (0);
275
276
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
277
0
    if (layout_cell_is_tiled(lcchild) ||
278
0
        layout_cell_has_tiled_child(lcchild))
279
0
      return (1);
280
0
  }
281
0
  return (0);
282
0
}
283
284
static int
285
layout_cell_is_first_tiled(struct layout_cell *lc)
286
0
{
287
0
  struct layout_cell      *lcchild, *lcparent = lc->parent;
288
289
0
  if (lcparent == NULL)
290
0
    return (layout_cell_is_tiled(lc));
291
292
0
  TAILQ_FOREACH(lcchild, &lcparent->cells, entry) {
293
0
    if (layout_cell_is_tiled(lcchild) ||
294
0
        layout_cell_has_tiled_child(lcchild))
295
0
      break;
296
0
  }
297
298
0
  return (lcchild == lc);
299
0
}
300
301
static struct layout_cell *
302
layout_cell_get_first_tiled(struct layout_cell *lc)
303
0
{
304
0
  struct layout_cell  *lcchild, *lcchild2;
305
306
0
  if (layout_cell_is_tiled(lc))
307
0
    return (lc);
308
0
  if (lc->type == LAYOUT_WINDOWPANE)
309
0
    return (NULL);
310
311
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
312
0
    if (layout_cell_is_tiled(lcchild))
313
0
      return (lcchild);
314
0
    if (lcchild->type != LAYOUT_WINDOWPANE) {
315
0
      lcchild2 = layout_cell_get_first_tiled(lcchild);
316
0
      if (lcchild2 != NULL)
317
0
        return (lcchild2);
318
0
    }
319
0
  }
320
0
  return (NULL);
321
0
}
322
323
/* Fix cell offsets for a child cell. */
324
static void
325
layout_fix_offsets1(struct layout_cell *lc)
326
0
{
327
0
  struct layout_cell  *lcchild;
328
0
  int      xoff, yoff;
329
330
0
  if (lc->type == LAYOUT_LEFTRIGHT) {
331
0
    xoff = lc->g.xoff;
332
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
333
0
      if (!layout_cell_is_tiled(lcchild) &&
334
0
          !layout_cell_has_tiled_child(lcchild))
335
0
        continue;
336
0
      lcchild->g.xoff = xoff;
337
0
      lcchild->g.yoff = lc->g.yoff;
338
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
339
0
        layout_fix_offsets1(lcchild);
340
0
      xoff += lcchild->g.sx + 1;
341
0
    }
342
0
  } else {
343
0
    yoff = lc->g.yoff;
344
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
345
0
      if (!layout_cell_is_tiled(lcchild) &&
346
0
          !layout_cell_has_tiled_child(lcchild))
347
0
        continue;
348
0
      lcchild->g.xoff = lc->g.xoff;
349
0
      lcchild->g.yoff = yoff;
350
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
351
0
        layout_fix_offsets1(lcchild);
352
0
      yoff += lcchild->g.sy + 1;
353
0
    }
354
0
  }
355
0
}
356
357
/* Update cell offsets based on their sizes. */
358
void
359
layout_fix_offsets(struct window *w)
360
0
{
361
0
  struct layout_cell  *lc = w->layout_root;
362
363
  /* Root consists of a single floating cell */
364
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
365
0
    return;
366
367
0
  lc->g.xoff = 0;
368
0
  lc->g.yoff = 0;
369
370
0
  layout_fix_offsets1(lc);
371
0
}
372
373
/* Is this a top cell? */
374
static int
375
layout_cell_is_top(struct window *w, struct layout_cell *lc)
376
0
{
377
0
  struct layout_cell  *next;
378
379
0
  while (lc != w->layout_root) {
380
0
    next = lc->parent;
381
0
    if (next == NULL)
382
0
      return (0);
383
0
    if (next->type == LAYOUT_TOPBOTTOM &&
384
0
        !layout_cell_is_first_tiled(lc))
385
0
      return (0);
386
0
    lc = next;
387
0
  }
388
0
  return (1);
389
0
}
390
391
/* Is this a bottom cell? */
392
static int
393
layout_cell_is_bottom(struct window *w, struct layout_cell *lc)
394
0
{
395
0
  struct layout_cell  *next, *edge;
396
397
0
  while (lc != w->layout_root) {
398
0
    next = lc->parent;
399
0
    if (next == NULL)
400
0
      return (0);
401
0
    if (next->type == LAYOUT_TOPBOTTOM) {
402
0
      edge = TAILQ_LAST(&next->cells, layout_cells);
403
0
      while (edge != NULL) {
404
0
        if (~edge->flags & LAYOUT_CELL_FLOATING)
405
0
          break;
406
0
        edge = TAILQ_PREV(edge, layout_cells, entry);
407
0
      }
408
0
      if (lc != edge)
409
0
        return (0);
410
0
    }
411
0
    lc = next;
412
0
  }
413
0
  return (1);
414
0
}
415
416
/*
417
 * Returns 1 if we need to add an extra line for the pane status line. This is
418
 * the case for the most upper or lower panes only.
419
 */
420
static int
421
layout_add_horizontal_border(struct window *w, struct layout_cell *lc,
422
    int status)
423
0
{
424
0
  if (status == PANE_STATUS_TOP)
425
0
    return (layout_cell_is_top(w, lc));
426
0
  if (status == PANE_STATUS_BOTTOM)
427
0
    return (layout_cell_is_bottom(w, lc));
428
0
  return (0);
429
0
}
430
431
/* Update pane offsets and sizes based on their cells. */
432
void
433
layout_fix_panes(struct window *w, struct window_pane *skip)
434
768
{
435
768
  struct window_pane  *wp;
436
768
  struct layout_cell  *lc;
437
768
  int      status, sb_w, sb_pad;
438
768
  int      old_xoff, old_yoff, changed = 0;
439
768
  u_int      sx, sy, old_sx, old_sy;
440
441
768
  status = window_get_pane_status(w);
442
443
768
  TAILQ_FOREACH(wp, &w->panes, entry) {
444
768
    if ((lc = wp->layout_cell) == NULL || wp == skip)
445
768
      continue;
446
447
0
    old_xoff = wp->xoff;
448
0
    old_yoff = wp->yoff;
449
0
    old_sx = wp->sx;
450
0
    old_sy = wp->sy;
451
452
0
    wp->xoff = lc->g.xoff;
453
0
    wp->yoff = lc->g.yoff;
454
0
    sx = lc->g.sx;
455
0
    sy = lc->g.sy;
456
457
0
    if (!window_pane_is_floating(wp) &&
458
0
        layout_add_horizontal_border(w, lc, status)) {
459
0
      if (status == PANE_STATUS_TOP)
460
0
        wp->yoff++;
461
0
      if (sy > 1)
462
0
        sy--;
463
0
    }
464
465
0
    if (window_pane_scrollbar_reserve(wp)) {
466
0
      sb_w = wp->scrollbar_style.width;
467
0
      sb_pad = wp->scrollbar_style.pad;
468
0
      if (sb_w < 1)
469
0
        sb_w = 1;
470
0
      if (sb_pad < 0)
471
0
        sb_pad = 0;
472
0
      if (w->sb_pos == PANE_SCROLLBARS_LEFT) {
473
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM) {
474
0
          wp->xoff = wp->xoff +
475
0
              (int)sx - PANE_MINIMUM;
476
0
          sx = PANE_MINIMUM;
477
0
        } else {
478
0
          sx = sx - sb_w - sb_pad;
479
0
          wp->xoff = wp->xoff + sb_w + sb_pad;
480
0
        }
481
0
      } else /* sb_pos == PANE_SCROLLBARS_RIGHT */
482
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM)
483
0
          sx = PANE_MINIMUM;
484
0
        else
485
0
          sx = sx - sb_w - sb_pad;
486
0
      wp->flags |= PANE_REDRAWSCROLLBAR;
487
0
    }
488
489
0
    window_pane_resize(wp, sx, sy);
490
491
0
    if (wp->xoff != old_xoff ||
492
0
        wp->yoff != old_yoff ||
493
0
        wp->sx != old_sx ||
494
0
        wp->sy != old_sy)
495
0
      changed = 1;
496
0
  }
497
768
  if (changed)
498
0
    redraw_invalidate_scene(w);
499
768
}
500
501
/* Count the number of available cells in a layout. */
502
u_int
503
layout_count_cells(struct layout_cell *lc)
504
0
{
505
0
  struct layout_cell  *lcchild;
506
0
  u_int      count = 0;
507
508
0
  switch (lc->type) {
509
0
  case LAYOUT_WINDOWPANE:
510
0
    return (1);
511
0
  case LAYOUT_LEFTRIGHT:
512
0
  case LAYOUT_TOPBOTTOM:
513
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
514
0
      count += layout_count_cells(lcchild);
515
0
    return (count);
516
0
  default:
517
0
    fatalx("bad layout type");
518
0
  }
519
0
}
520
521
/* Calculate how much size is available to be removed from a cell. */
522
static u_int
523
layout_resize_check(struct window *w, struct layout_cell *lc,
524
    enum layout_type type)
525
0
{
526
0
  struct layout_cell  *lcchild;
527
0
  struct style    *sb_style = &w->active->scrollbar_style;
528
0
  u_int      available, minimum;
529
0
  int      status;
530
531
0
  status = window_get_pane_status(w);
532
533
0
  if (lc->type == LAYOUT_WINDOWPANE) {
534
    /* Space available in this cell only. */
535
0
    if (type == LAYOUT_LEFTRIGHT) {
536
0
      available = lc->g.sx;
537
0
      if (w->sb == PANE_SCROLLBARS_ALWAYS)
538
0
        minimum = PANE_MINIMUM + sb_style->width +
539
0
            sb_style->pad;
540
0
      else
541
0
        minimum = PANE_MINIMUM;
542
0
    } else {
543
0
      available = lc->g.sy;
544
0
      if (layout_add_horizontal_border(w, lc, status))
545
0
        minimum = PANE_MINIMUM + 1;
546
0
      else
547
0
        minimum = PANE_MINIMUM;
548
0
    }
549
0
    if (available > minimum)
550
0
      available -= minimum;
551
0
    else
552
0
      available = 0;
553
0
  } else if (lc->type == type) {
554
    /* Same type: total of available space in all child cells. */
555
0
    available = 0;
556
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
557
0
      available += layout_resize_check(w, lcchild, type);
558
0
  } else {
559
    /* Different type: minimum of available space in child cells. */
560
0
    minimum = UINT_MAX;
561
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
562
0
      available = layout_resize_check(w, lcchild, type);
563
0
      if (available < minimum)
564
0
        minimum = available;
565
0
    }
566
0
    available = minimum;
567
0
  }
568
569
0
  return (available);
570
0
}
571
572
/*
573
 * Adjust cell size evenly, including altering its children. This function
574
 * expects the change to have already been bounded to the space available.
575
 */
576
void
577
layout_resize_adjust(struct window *w, struct layout_cell *lc,
578
    enum layout_type type, int change)
579
0
{
580
0
  struct layout_cell  *lcchild;
581
0
  int      changed;
582
583
  /* Adjust the cell size. */
584
0
  if (type == LAYOUT_LEFTRIGHT)
585
0
    lc->g.sx += change;
586
0
  else
587
0
    lc->g.sy += change;
588
589
  /* If this is a leaf cell, that is all that is necessary. */
590
0
  if (type == LAYOUT_WINDOWPANE)
591
0
    return;
592
593
  /* Child cell runs in a different direction. */
594
0
  if (lc->type != type) {
595
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
596
0
      if (!layout_cell_is_tiled(lcchild) &&
597
0
          !layout_cell_has_tiled_child(lcchild))
598
0
        continue;
599
0
      layout_resize_adjust(w, lcchild, type, change);
600
0
    }
601
0
    return;
602
0
  }
603
604
  /*
605
   * If a node doesn't contain any tiled cells, there is nothing to do.
606
   */
607
0
  if (!layout_cell_has_tiled_child(lc))
608
0
    return;
609
610
  /*
611
   * Child cell runs in the same direction. Adjust each child equally
612
   * until no further change is possible.
613
   */
614
0
  while (change != 0) {
615
0
    changed = 0;
616
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
617
0
      if (change == 0)
618
0
        break;
619
0
      if (!layout_cell_is_tiled(lcchild) &&
620
0
          !layout_cell_has_tiled_child(lcchild))
621
0
        continue;
622
0
      if (change > 0) {
623
0
        layout_resize_adjust(w, lcchild, type, 1);
624
0
        change--;
625
0
        changed = 1;
626
0
        continue;
627
0
      }
628
0
      if (layout_resize_check(w, lcchild, type) > 0) {
629
0
        layout_resize_adjust(w, lcchild, type, -1);
630
0
        change++;
631
0
        changed = 1;
632
0
      }
633
0
    }
634
0
    if (!changed)
635
0
      break;
636
0
  }
637
0
}
638
639
/* Resizes a cell to a specified size */
640
void
641
layout_resize_set_size(struct window *w, struct layout_cell *lc,
642
    enum layout_type type, u_int size)
643
0
{
644
0
  int change;
645
646
0
  if (type == LAYOUT_LEFTRIGHT)
647
0
    change = size - lc->g.sx;
648
0
  else
649
0
    change = size - lc->g.sy;
650
0
  layout_resize_adjust(w, lc, type, change);
651
0
}
652
653
/* Find and return the nearest neighbour to a cell in a specific direction. */
654
static struct layout_cell *
655
layout_cell_get_neighbour_direction(struct layout_cell *lc, int direction)
656
0
{
657
0
  struct layout_cell  *lcn = lc;
658
659
0
  while (1) {
660
0
    if (direction)
661
0
      lcn = TAILQ_NEXT(lcn, entry);
662
0
    else
663
0
      lcn = TAILQ_PREV(lcn, layout_cells, entry);
664
665
0
    if (lcn == NULL ||
666
0
        layout_cell_is_tiled(lcn) ||
667
0
        layout_cell_has_tiled_child(lcn))
668
0
      return (lcn);
669
0
  }
670
0
}
671
672
/*
673
 * Find and return the nearest neighbour. Prefers cells "after" the specified
674
 * cell. This behavior defines how cell dimensions are redistributed when a cell
675
 * is hidden/shown and floated/tiled.
676
 */
677
struct layout_cell *
678
layout_cell_get_neighbour(struct layout_cell *lc)
679
0
{
680
0
  struct layout_cell  *lcother, *lcparent = lc->parent;
681
0
  int      direction = 1;
682
683
0
  if (lcparent == NULL)
684
0
    return (NULL);
685
686
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
687
0
    direction = !direction;
688
689
0
  lcother = layout_cell_get_neighbour_direction(lc, direction);
690
0
  if (lcother == NULL)
691
0
    lcother = layout_cell_get_neighbour_direction(lc, !direction);
692
693
0
  return (lcother);
694
0
}
695
696
697
/* Destroy a cell and redistribute the space. */
698
void
699
layout_destroy_cell(struct window *w, struct layout_cell *lc,
700
    struct layout_cell **lcroot)
701
0
{
702
0
  struct layout_cell  *lcother = NULL, *lcparent;
703
0
  int      change;
704
705
  /* If no parent, this is the last pane in a window. */
706
0
  lcparent = lc->parent;
707
0
  if (lcparent == NULL) {
708
0
    if (lc->wp != NULL)
709
0
      *lcroot = NULL;
710
0
    layout_free_cell(lc, 0);
711
0
    return;
712
0
  }
713
714
0
  if (!layout_cell_is_tiled(lc)) {
715
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
716
0
    layout_free_cell(lc, 0);
717
0
    goto out;
718
0
  }
719
720
0
  lcother = layout_cell_get_neighbour(lc);
721
0
  if (lcother != NULL) {
722
0
    if (lcparent->type == LAYOUT_LEFTRIGHT)
723
0
      change = lc->g.sx + 1;
724
0
    else
725
0
      change = lc->g.sy + 1;
726
0
    layout_resize_adjust(w, lcother, lcparent->type, change);
727
0
  } else
728
0
    layout_remove_tile(w, lcparent);
729
730
  /* Remove this from the parent's list. */
731
0
  TAILQ_REMOVE(&lcparent->cells, lc, entry);
732
0
  layout_free_cell(lc, 0);
733
734
0
out:
735
  /*
736
   * If the parent now has one cell, remove the parent from the tree and
737
   * replace it by that cell.
738
   */
739
0
  lc = TAILQ_FIRST(&lcparent->cells);
740
0
  if (lc != NULL && TAILQ_NEXT(lc, entry) == NULL) {
741
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
742
743
0
    lc->parent = lcparent->parent;
744
0
    if (lc->parent == NULL) {
745
0
      if (layout_cell_is_tiled(lc)) {
746
0
        lc->g.xoff = 0;
747
0
        lc->g.yoff = 0;
748
0
      }
749
0
      *lcroot = lc;
750
0
    } else
751
0
      TAILQ_REPLACE(&lc->parent->cells, lcparent, lc, entry);
752
753
0
    layout_free_cell(lcparent, 0);
754
0
  }
755
0
}
756
757
/* Initialize layout for pane. */
758
void
759
layout_init(struct window *w, struct window_pane *wp)
760
0
{
761
0
  struct layout_cell  *lc;
762
763
0
  lc = w->layout_root = layout_create_cell(NULL);
764
0
  layout_set_size(lc, w->sx, w->sy, 0, 0);
765
0
  layout_make_leaf(lc, wp);
766
0
  layout_fix_panes(w, NULL);
767
0
}
768
769
/* Free layout for pane. */
770
void
771
layout_free(struct window *w, int only_nodes)
772
0
{
773
0
  layout_free_cell(w->layout_root, only_nodes);
774
0
}
775
776
/* Resize the entire layout after window resize. */
777
void
778
layout_resize(struct window *w, u_int sx, u_int sy)
779
0
{
780
0
  struct layout_cell  *lc = w->layout_root;
781
0
  int      xlimit, ylimit, xchange, ychange;
782
783
  /*
784
   * Adjust horizontally. Do not attempt to reduce the layout lower than
785
   * the minimum (more than the amount returned by layout_resize_check).
786
   *
787
   * This can mean that the window size is smaller than the total layout
788
   * size: redrawing this is handled at a higher level, but it does leave
789
   * a problem with growing the window size here: if the current size is
790
   * < the minimum, growing proportionately by adding to each pane is
791
   * wrong as it would keep the layout size larger than the window size.
792
   * Instead, spread the difference between the minimum and the new size
793
   * out proportionately - this should leave the layout fitting the new
794
   * window size.
795
   */
796
0
  if (lc->type == LAYOUT_WINDOWPANE && (lc->flags & LAYOUT_CELL_FLOATING))
797
0
    return;
798
0
  xchange = sx - lc->g.sx;
799
0
  xlimit = layout_resize_check(w, lc, LAYOUT_LEFTRIGHT);
800
0
  if (xchange < 0 && xchange < -xlimit)
801
0
    xchange = -xlimit;
802
0
  if (xlimit == 0) {
803
0
    if (sx <= lc->g.sx) /* lc->g.sx is minimum possible */
804
0
      xchange = 0;
805
0
    else
806
0
      xchange = sx - lc->g.sx;
807
0
  }
808
0
  if (xchange != 0)
809
0
    layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, xchange);
810
811
  /* Adjust vertically in a similar fashion. */
812
0
  ychange = sy - lc->g.sy;
813
0
  ylimit = layout_resize_check(w, lc, LAYOUT_TOPBOTTOM);
814
0
  if (ychange < 0 && ychange < -ylimit)
815
0
    ychange = -ylimit;
816
0
  if (ylimit == 0) {
817
0
    if (sy <= lc->g.sy) /* lc->g.sy is minimum possible */
818
0
      ychange = 0;
819
0
    else
820
0
      ychange = sy - lc->g.sy;
821
0
  }
822
0
  if (ychange != 0)
823
0
    layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, ychange);
824
825
  /* Fix cell offsets. */
826
0
  layout_fix_offsets(w);
827
0
  layout_fix_panes(w, NULL);
828
0
}
829
830
/* Resize a pane to an absolute size. */
831
void
832
layout_resize_pane_to(struct window_pane *wp, enum layout_type type,
833
    u_int new_size)
834
0
{
835
0
  struct layout_cell     *lc, *lcparent;
836
0
  int     change, size;
837
838
0
  lc = wp->layout_cell;
839
840
  /* Find next parent of the same type. */
841
0
  lcparent = lc->parent;
842
0
  while (lcparent != NULL && lcparent->type != type) {
843
0
    lc = lcparent;
844
0
    lcparent = lc->parent;
845
0
  }
846
0
  if (lcparent == NULL)
847
0
    return;
848
849
  /* Work out the size adjustment. */
850
0
  if (type == LAYOUT_LEFTRIGHT)
851
0
    size = lc->g.sx;
852
0
  else
853
0
    size = lc->g.sy;
854
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
855
0
    change = size - new_size;
856
0
  else
857
0
    change = new_size - size;
858
859
  /* Resize the pane. */
860
0
  layout_resize_pane(wp, type, change, 1);
861
0
}
862
863
/* Resize a floating pane to an absolute size. */
864
int
865
layout_resize_floating_pane_to(struct window_pane *wp, enum layout_type type,
866
    u_int size, char **cause)
867
0
{
868
0
  struct layout_cell  *lc = wp->layout_cell;
869
870
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
871
0
    *cause = xstrdup("pane is not floating");
872
0
    return (-1);
873
0
  }
874
875
0
  if (window_pane_get_pane_lines(wp) != PANE_LINES_NONE &&
876
0
      size >= PANE_MINIMUM + 2)
877
0
    size -= 2;
878
0
  if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
879
0
    *cause = xstrdup("size is too big or too small");
880
0
    return (-1);
881
0
  }
882
883
0
  if (type == LAYOUT_TOPBOTTOM) {
884
0
    if (lc->g.sy == size)
885
0
      return (0);
886
0
    lc->g.sy = size;
887
0
  } else {
888
0
    if (lc->g.sx == size)
889
0
      return (0);
890
0
    lc->g.sx = size;
891
0
  }
892
0
  redraw_invalidate_scene(wp->window);
893
0
  return (0);
894
0
}
895
896
/* Resize a floating pane relative to its current size. */
897
int
898
layout_resize_floating_pane(struct window_pane *wp, enum layout_type type,
899
    int change, int opposite, char **cause)
900
0
{
901
0
  struct layout_cell  *lc = wp->layout_cell;
902
0
  u_int      size;
903
904
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
905
0
    *cause = xstrdup("pane is not floating");
906
0
    return (-1);
907
0
  }
908
0
  if (change == 0)
909
0
    return (0);
910
911
0
  if (type == LAYOUT_TOPBOTTOM) {
912
0
    size = lc->g.sy + change;
913
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
914
0
      *cause = xstrdup("change is too big or too small");
915
0
      return (-1);
916
0
    }
917
0
    lc->g.sy = size;
918
0
    if (opposite)
919
0
      lc->g.yoff -= change;
920
0
  } else {
921
0
    size = lc->g.sx + change;
922
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
923
0
      *cause = xstrdup("change is too big or too small");
924
0
      return (-1);
925
0
    }
926
0
    lc->g.sx = size;
927
0
    if (opposite)
928
0
      lc->g.xoff -= change;
929
0
  }
930
0
  redraw_invalidate_scene(wp->window);
931
0
  return (0);
932
0
}
933
934
/* Resize a layout cell. */
935
void
936
layout_resize_layout(struct window *w, struct layout_cell *lc,
937
    enum layout_type type, int change, int opposite)
938
0
{
939
0
  int needed, size;
940
941
  /* Grow or shrink the cell. */
942
0
  needed = change;
943
0
  while (needed != 0) {
944
0
    if (change > 0) {
945
0
      size = layout_resize_pane_grow(w, lc, type, needed,
946
0
          opposite);
947
0
      needed -= size;
948
0
    } else {
949
0
      size = layout_resize_pane_shrink(w, lc, type, needed);
950
0
      needed += size;
951
0
    }
952
953
0
    if (size == 0) /* no more change possible */
954
0
      break;
955
0
  }
956
957
  /* Fix cell offsets. */
958
0
  layout_fix_offsets(w);
959
0
  layout_fix_panes(w, NULL);
960
0
  notify_window("window-layout-changed", w);
961
0
}
962
963
/* Resize a single pane within the layout. */
964
void
965
layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
966
    int opposite)
967
0
{
968
0
  struct layout_cell  *lc = wp->layout_cell, *lcparent;
969
970
  /* Find next parent of the same type. */
971
0
  lcparent = lc->parent;
972
0
  while (lcparent != NULL && lcparent->type != type) {
973
0
    lc = lcparent;
974
0
    lcparent = lc->parent;
975
0
  }
976
0
  if (lcparent == NULL)
977
0
    return;
978
979
  /* If this is the last cell, move back one. */
980
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells)) {
981
0
    do
982
0
      lc = TAILQ_PREV(lc, layout_cells, entry);
983
0
    while (lc->flags & LAYOUT_CELL_FLOATING);
984
0
  }
985
986
0
  layout_resize_layout(wp->window, lc, type, change, opposite);
987
0
}
988
989
/* Helper function to grow pane. */
990
static int
991
layout_resize_pane_grow(struct window *w, struct layout_cell *lc,
992
    enum layout_type type, int needed, int opposite)
993
0
{
994
0
  struct layout_cell  *lcadd, *lcremove;
995
0
  u_int      size = 0;
996
997
  /* Growing. Always add to the current cell. */
998
0
  lcadd = lc;
999
1000
  /* Look towards the tail for a suitable cell for reduction. */
1001
0
  lcremove = TAILQ_NEXT(lc, entry);
1002
0
  while (lcremove != NULL) {
1003
0
    size = layout_resize_check(w, lcremove, type);
1004
0
    if (size > 0)
1005
0
      break;
1006
0
    lcremove = TAILQ_NEXT(lcremove, entry);
1007
0
  }
1008
1009
  /* If none found, look towards the head. */
1010
0
  if (opposite && lcremove == NULL) {
1011
0
    lcremove = TAILQ_PREV(lc, layout_cells, entry);
1012
0
    while (lcremove != NULL) {
1013
0
      size = layout_resize_check(w, lcremove, type);
1014
0
      if (size > 0)
1015
0
        break;
1016
0
      lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
1017
0
    }
1018
0
  }
1019
0
  if (lcremove == NULL)
1020
0
    return (0);
1021
1022
  /* Change the cells. */
1023
0
  if (size > (u_int) needed)
1024
0
    size = needed;
1025
0
  layout_resize_adjust(w, lcadd, type, size);
1026
0
  layout_resize_adjust(w, lcremove, type, -size);
1027
0
  return (size);
1028
0
}
1029
1030
/* Helper function to shrink pane. */
1031
static int
1032
layout_resize_pane_shrink(struct window *w, struct layout_cell *lc,
1033
    enum layout_type type, int needed)
1034
0
{
1035
0
  struct layout_cell  *lcadd, *lcremove;
1036
0
  u_int      size;
1037
1038
  /* Shrinking. Find cell to remove from by walking towards head. */
1039
0
  lcremove = lc;
1040
0
  do {
1041
0
    size = layout_resize_check(w, lcremove, type);
1042
0
    if (size != 0)
1043
0
      break;
1044
0
    lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
1045
0
  } while (lcremove != NULL);
1046
0
  if (lcremove == NULL)
1047
0
    return (0);
1048
1049
  /* And add onto the next cell (from the original cell). */
1050
0
  lcadd = TAILQ_NEXT(lc, entry);
1051
0
  if (lcadd == NULL)
1052
0
    return (0);
1053
1054
  /* Change the cells. */
1055
0
  if (size > (u_int) -needed)
1056
0
    size = -needed;
1057
0
  layout_resize_adjust(w, lcadd, type, size);
1058
0
  layout_resize_adjust(w, lcremove, type, -size);
1059
0
  return (size);
1060
0
}
1061
1062
/* Assign window pane to new cell. */
1063
void
1064
layout_assign_pane(struct layout_cell *lc, struct window_pane *wp,
1065
    int do_not_resize)
1066
0
{
1067
0
  layout_make_leaf(lc, wp);
1068
0
  if (do_not_resize)
1069
0
    layout_fix_panes(wp->window, wp);
1070
0
  else
1071
0
    layout_fix_panes(wp->window, NULL);
1072
0
}
1073
1074
/* Calculate the new pane size for resized parent. */
1075
static u_int
1076
layout_new_pane_size(struct window *w, u_int previous, struct layout_cell *lc,
1077
    enum layout_type type, u_int size, u_int count_left, u_int size_left)
1078
0
{
1079
0
  u_int new_size, min, max, available;
1080
1081
  /* If this is the last cell, it can take all of the remaining size. */
1082
0
  if (count_left == 1)
1083
0
    return (size_left);
1084
1085
  /* How much is available in this parent? */
1086
0
  available = layout_resize_check(w, lc, type);
1087
1088
  /*
1089
   * Work out the minimum size of this cell and the new size
1090
   * proportionate to the previous size.
1091
   */
1092
0
  min = (PANE_MINIMUM + 1) * (count_left - 1);
1093
0
  if (type == LAYOUT_LEFTRIGHT) {
1094
0
    if (lc->g.sx - available > min)
1095
0
      min = lc->g.sx - available;
1096
0
    new_size = (lc->g.sx * size) / previous;
1097
0
  } else {
1098
0
    if (lc->g.sy - available > min)
1099
0
      min = lc->g.sy - available;
1100
0
    new_size = (lc->g.sy * size) / previous;
1101
0
  }
1102
1103
  /* Check against the maximum and minimum size. */
1104
0
  max = size_left - min;
1105
0
  if (new_size > max)
1106
0
    new_size = max;
1107
0
  if (new_size < PANE_MINIMUM)
1108
0
    new_size = PANE_MINIMUM;
1109
0
  return (new_size);
1110
0
}
1111
1112
/* Check if the cell and all its children can be resized to a specific size. */
1113
static int
1114
layout_set_size_check(struct window *w, struct layout_cell *lc,
1115
    enum layout_type type, int size)
1116
0
{
1117
0
  struct layout_cell  *lcchild;
1118
0
  u_int      new_size, available, previous, count, idx;
1119
1120
  /* Cells with no children must just be bigger than minimum. */
1121
0
  if (lc->type == LAYOUT_WINDOWPANE)
1122
0
    return (size >= PANE_MINIMUM);
1123
0
  available = size;
1124
1125
  /* Count number of children. */
1126
0
  count = 0;
1127
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry)
1128
0
    count++;
1129
1130
  /* Check new size will work for each child. */
1131
0
  if (lc->type == type) {
1132
0
    if (available < (count * 2) - 1)
1133
0
      return (0);
1134
1135
0
    if (type == LAYOUT_LEFTRIGHT)
1136
0
      previous = lc->g.sx;
1137
0
    else
1138
0
      previous = lc->g.sy;
1139
1140
0
    idx = 0;
1141
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1142
0
      new_size = layout_new_pane_size(w, previous, lcchild,
1143
0
          type, size, count - idx, available);
1144
0
      if (idx == count - 1) {
1145
0
        if (new_size > available)
1146
0
          return (0);
1147
0
        available -= new_size;
1148
0
      } else {
1149
0
        if (new_size + 1 > available)
1150
0
          return (0);
1151
0
        available -= new_size + 1;
1152
0
      }
1153
0
      if (!layout_set_size_check(w, lcchild, type, new_size))
1154
0
        return (0);
1155
0
      idx++;
1156
0
    }
1157
0
  } else {
1158
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1159
0
      if (lcchild->type == LAYOUT_WINDOWPANE)
1160
0
        continue;
1161
0
      if (!layout_set_size_check(w, lcchild, type, size))
1162
0
        return (0);
1163
0
    }
1164
0
  }
1165
1166
0
  return (1);
1167
0
}
1168
1169
/* Resize all child cells to fit within the current cell. */
1170
static void
1171
layout_resize_child_cells(struct window *w, struct layout_cell *lc)
1172
0
{
1173
0
  struct layout_cell  *lcchild;
1174
0
  u_int      prev, available, count, idx;
1175
1176
0
  if (lc->type == LAYOUT_WINDOWPANE)
1177
0
    return;
1178
1179
  /* What is the current size used? */
1180
0
  count = 0;
1181
0
  prev = 0;
1182
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1183
0
    if (!layout_cell_is_tiled(lcchild) &&
1184
0
        !layout_cell_has_tiled_child(lcchild))
1185
0
      continue;
1186
0
    count++;
1187
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1188
0
      prev += lcchild->g.sx;
1189
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1190
0
      prev += lcchild->g.sy;
1191
0
  }
1192
0
  prev += (count - 1);
1193
1194
  /* And how much is available? */
1195
0
  available = 0;
1196
0
  if (lc->type == LAYOUT_LEFTRIGHT)
1197
0
    available = lc->g.sx;
1198
0
  else if (lc->type == LAYOUT_TOPBOTTOM)
1199
0
    available = lc->g.sy;
1200
1201
  /* Resize children into the new size. */
1202
0
  idx = 0;
1203
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1204
0
    if (!layout_cell_is_tiled(lcchild) &&
1205
0
        !layout_cell_has_tiled_child(lcchild))
1206
0
      continue;
1207
0
    if (lc->type == LAYOUT_TOPBOTTOM) {
1208
0
      lcchild->g.sx = lc->g.sx;
1209
0
      lcchild->g.xoff = lc->g.xoff;
1210
0
    } else {
1211
0
      lcchild->g.sx = layout_new_pane_size(w, prev, lcchild,
1212
0
          lc->type, lc->g.sx, count - idx, available);
1213
0
      available -= (lcchild->g.sx + 1);
1214
0
    }
1215
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1216
0
      lcchild->g.sy = lc->g.sy;
1217
0
      lcchild->g.yoff = lc->g.yoff;
1218
0
    } else {
1219
0
      lcchild->g.sy = layout_new_pane_size(w, prev, lcchild,
1220
0
          lc->type, lc->g.sy, count - idx, available);
1221
0
      available -= (lcchild->g.sy + 1);
1222
0
    }
1223
0
    layout_resize_child_cells(w, lcchild);
1224
0
    idx++;
1225
0
  }
1226
0
}
1227
1228
/*
1229
 * Replaces the provided layout cell with a new node of the specified type and
1230
 * inserts the cell into it. Used when creating new cells requires a different
1231
 * layout type, or when the root layout is a window pane.
1232
 */
1233
struct layout_cell *
1234
layout_replace_with_node(struct window *w, struct layout_cell *lc,
1235
    enum layout_type type)
1236
0
{
1237
0
  struct layout_cell  *lcparent;
1238
1239
0
  lcparent = layout_create_cell(lc->parent);
1240
0
  layout_make_node(lcparent, type);
1241
0
  layout_set_size(lcparent, lc->g.sx, lc->g.sy, lc->g.xoff, lc->g.yoff);
1242
0
  if (lc->parent == NULL)
1243
0
    w->layout_root = lcparent;
1244
0
  else
1245
0
    TAILQ_REPLACE(&lc->parent->cells, lc, lcparent, entry);
1246
1247
  /* Insert the old cell. */
1248
0
  lc->parent = lcparent;
1249
0
  TAILQ_INSERT_HEAD(&lcparent->cells, lc, entry);
1250
1251
0
  return (lcparent);
1252
0
}
1253
1254
/* Checks if there is enough space for two new panes. */
1255
int
1256
layout_split_check_space(struct window_pane *wp, struct layout_cell *lc,
1257
   enum layout_type type)
1258
0
{
1259
0
  struct style  *sb_style = &wp->scrollbar_style;
1260
0
  u_int    minimum, sx = lc->g.sx, sy = lc->g.sy;
1261
0
  int    status;
1262
1263
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1264
0
    fatalx("floating cells cannot be split");
1265
1266
0
  status = window_get_pane_status(wp->window);
1267
1268
0
  switch (type) {
1269
0
  case LAYOUT_LEFTRIGHT:
1270
0
    if (wp->window->sb == PANE_SCROLLBARS_ALWAYS) {
1271
0
      minimum = PANE_MINIMUM * 2 + sb_style->width +
1272
0
          sb_style->pad;
1273
0
    } else
1274
0
      minimum = PANE_MINIMUM * 2 + 1;
1275
0
    if (sx < minimum)
1276
0
      return (0);
1277
0
    break;
1278
0
  case LAYOUT_TOPBOTTOM:
1279
0
    if (layout_add_horizontal_border(wp->window, lc, status))
1280
0
      minimum = PANE_MINIMUM * 2 + 2;
1281
0
    else
1282
0
      minimum = PANE_MINIMUM * 2 + 1;
1283
0
    if (sy < minimum)
1284
0
      return (0);
1285
0
    break;
1286
0
  default:
1287
0
    fatalx("bad layout type");
1288
0
  }
1289
1290
0
  return (1);
1291
0
}
1292
1293
/* Calculates the new cell sizes when splitting a pane. */
1294
void
1295
layout_split_sizes(struct layout_cell *lc, int size, int before,
1296
    enum layout_type type, u_int *size1, u_int *size2, u_int *saved_size)
1297
0
{
1298
0
  u_int s1, s2, ss;
1299
0
  u_int sx = lc->g.sx, sy = lc->g.sy;
1300
1301
0
  if (type == LAYOUT_LEFTRIGHT)
1302
0
    ss = sx;
1303
0
  else
1304
0
    ss = sy;
1305
0
  if (size < 0)
1306
0
    s2 = ((ss + 1) / 2) - 1;
1307
0
  else if (before)
1308
0
    s2 = ss - size - 1;
1309
0
  else
1310
0
    s2 = size;
1311
0
  if (s2 < PANE_MINIMUM)
1312
0
    s2 = PANE_MINIMUM;
1313
0
  else if (s2 > ss - 2)
1314
0
    s2 = ss - 2;
1315
0
  s1 = ss - 1 - s2;
1316
1317
0
  *size1 = s1;
1318
0
  *size2 = s2;
1319
0
  *saved_size = ss;
1320
0
}
1321
1322
/*
1323
 * Split a pane into two. size is a hint, or -1 for default half/half
1324
 * split. This must be followed by layout_assign_pane before much else happens!
1325
 */
1326
struct layout_cell *
1327
layout_split_pane(struct window_pane *wp, enum layout_type type, int size,
1328
    int flags)
1329
0
{
1330
0
  struct layout_cell  *lc, *lcparent, *lcnew, *lc1, *lc2;
1331
0
  u_int      sx, sy, xoff, yoff, size1, size2;
1332
0
  u_int      new_size, saved_size, resize_first = 0;
1333
0
  int      full_size = (flags & SPAWN_FULLSIZE);
1334
0
  int      before = (flags & SPAWN_BEFORE);
1335
1336
  /*
1337
   * If full_size is specified, add a new cell at the top of the window
1338
   * layout. Otherwise, split the cell for the current pane.
1339
   */
1340
0
  if (full_size)
1341
0
    lc = wp->window->layout_root;
1342
0
  else
1343
0
    lc = wp->layout_cell;
1344
1345
  /* Copy the old cell size. */
1346
0
  sx = lc->g.sx;
1347
0
  sy = lc->g.sy;
1348
0
  xoff = lc->g.xoff;
1349
0
  yoff = lc->g.yoff;
1350
1351
  /* Check there is enough space for the two new panes. */
1352
0
  if (!layout_split_check_space(wp, lc, type))
1353
0
    return (NULL);
1354
1355
  /*
1356
   * Calculate new cell sizes. size is the target size or -1 for middle
1357
   * split, size1 is the size of the top/left and size2 the bottom/right.
1358
   */
1359
0
  layout_split_sizes(lc, size, before, type, &size1, &size2, &saved_size);
1360
1361
  /* Which size are we using? */
1362
0
  if (flags & SPAWN_BEFORE)
1363
0
    new_size = size2;
1364
0
  else
1365
0
    new_size = size1;
1366
1367
  /* Confirm there is enough space for full size pane. */
1368
0
  if (full_size && !layout_set_size_check(wp->window, lc, type, new_size))
1369
0
    return (NULL);
1370
1371
0
  if (lc->parent != NULL && lc->parent->type == type) {
1372
    /*
1373
     * If the parent exists and is of the same type as the split,
1374
     * create a new cell and insert it after this one.
1375
     */
1376
0
    lcparent = lc->parent;
1377
0
    lcnew = layout_create_cell(lcparent);
1378
0
    if (flags & SPAWN_BEFORE)
1379
0
      TAILQ_INSERT_BEFORE(lc, lcnew, entry);
1380
0
    else
1381
0
      TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1382
0
  } else if (full_size && lc->parent == NULL && lc->type == type) {
1383
    /*
1384
     * If the new full size pane is the same type as the root
1385
     * split, insert the new pane under the existing root cell
1386
     * instead of creating a new root cell. The existing layout
1387
     * must be resized before inserting the new cell.
1388
     */
1389
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1390
0
      lc->g.sx = new_size;
1391
0
      layout_resize_child_cells(wp->window, lc);
1392
0
      lc->g.sx = saved_size;
1393
0
    } else if (lc->type == LAYOUT_TOPBOTTOM) {
1394
0
      lc->g.sy = new_size;
1395
0
      layout_resize_child_cells(wp->window, lc);
1396
0
      lc->g.sy = saved_size;
1397
0
    }
1398
0
    resize_first = 1;
1399
1400
    /* Create the new cell. */
1401
0
    lcnew = layout_create_cell(lc);
1402
0
    size = saved_size - 1 - new_size;
1403
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1404
0
      layout_set_size(lcnew, size, sy, 0, 0);
1405
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1406
0
      layout_set_size(lcnew, sx, size, 0, 0);
1407
0
    if (flags & SPAWN_BEFORE)
1408
0
      TAILQ_INSERT_HEAD(&lc->cells, lcnew, entry);
1409
0
    else
1410
0
      TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
1411
0
  } else {
1412
    /*
1413
     * Otherwise create a new parent and insert it.
1414
     */
1415
1416
    /* Create and insert the replacement parent. */
1417
0
    lcparent = layout_replace_with_node(wp->window, lc, type);
1418
1419
    /* Create the new child cell. */
1420
0
    lcnew = layout_create_cell(lcparent);
1421
0
    if (flags & SPAWN_BEFORE)
1422
0
      TAILQ_INSERT_HEAD(&lcparent->cells, lcnew, entry);
1423
0
    else
1424
0
      TAILQ_INSERT_TAIL(&lcparent->cells, lcnew, entry);
1425
0
  }
1426
0
  if (flags & SPAWN_BEFORE) {
1427
0
    lc1 = lcnew;
1428
0
    lc2 = lc;
1429
0
  } else {
1430
0
    lc1 = lc;
1431
0
    lc2 = lcnew;
1432
0
  }
1433
1434
  /*
1435
   * Set new cell sizes. size1 is the size of the top/left and size2 the
1436
   * bottom/right.
1437
   */
1438
0
  if (!resize_first && type == LAYOUT_LEFTRIGHT) {
1439
0
    layout_set_size(lc1, size1, sy, xoff, yoff);
1440
0
    layout_set_size(lc2, size2, sy, xoff + lc1->g.sx + 1, yoff);
1441
0
  } else if (!resize_first && type == LAYOUT_TOPBOTTOM) {
1442
0
    layout_set_size(lc1, sx, size1, xoff, yoff);
1443
0
    layout_set_size(lc2, sx, size2, xoff, yoff + lc1->g.sy + 1);
1444
0
  }
1445
0
  if (full_size) {
1446
0
    if (!resize_first)
1447
0
      layout_resize_child_cells(wp->window, lc);
1448
0
    layout_fix_offsets(wp->window);
1449
0
  } else
1450
0
    layout_make_leaf(lc, wp);
1451
1452
0
  return (lcnew);
1453
0
}
1454
1455
/*
1456
 * Creates a cell for a new floating pane. This must be followed by
1457
 * layout_assign_pane before much else happens!
1458
 */
1459
struct layout_cell *
1460
layout_floating_pane(struct window *w, struct window_pane *wp,
1461
    struct layout_geometry *lg)
1462
0
{
1463
0
  struct layout_cell  *lc, *lcnew, *lcparent;
1464
1465
0
  if (wp == NULL)
1466
0
    lc = w->layout_root;
1467
0
  else
1468
0
    lc = wp->layout_cell;
1469
0
  lcparent = lc->parent;
1470
1471
0
  if (lcparent == NULL) {
1472
    /*
1473
     * Adding a pane to a root that isn't a node. Must create and
1474
     * insert a new root.
1475
     */
1476
0
    lcparent = layout_replace_with_node(w, lc, LAYOUT_TOPBOTTOM);
1477
0
  }
1478
1479
0
  lcnew = layout_create_cell(lcparent);
1480
0
  TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1481
0
  lcnew->flags |= LAYOUT_CELL_FLOATING;
1482
0
  layout_set_size(lcnew, lg->sx, lg->sy, lg->xoff, lg->yoff);
1483
1484
0
  return (lcnew);
1485
0
}
1486
1487
/* Destroy the cell associated with a pane. */
1488
void
1489
layout_close_pane(struct window_pane *wp)
1490
0
{
1491
0
  struct window *w = wp->window;
1492
1493
0
  if (wp->layout_cell == NULL)
1494
0
    return;
1495
1496
  /* Remove the cell. */
1497
0
  layout_destroy_cell(w, wp->layout_cell, &w->layout_root);
1498
0
  wp->layout_cell = NULL;
1499
1500
  /* Fix pane offsets and sizes. */
1501
0
  if (w->layout_root != NULL) {
1502
0
    layout_fix_offsets(w);
1503
0
    layout_fix_panes(w, NULL);
1504
0
  }
1505
0
  notify_window("window-layout-changed", w);
1506
0
}
1507
1508
/* Spread out cells inside a parent cell. */
1509
int
1510
layout_spread_cell(struct window *w, struct layout_cell *parent)
1511
0
{
1512
0
  struct layout_cell  *lc;
1513
0
  u_int      number, each, size, this, remainder;
1514
0
  int      change, changed, status;
1515
1516
0
  number = 0;
1517
0
  TAILQ_FOREACH (lc, &parent->cells, entry)
1518
0
    if (layout_cell_is_tiled(lc))
1519
0
      number++;
1520
0
  if (number <= 1)
1521
0
    return (0);
1522
0
  status = window_get_pane_status(w);
1523
1524
0
  if (parent->type == LAYOUT_LEFTRIGHT)
1525
0
    size = parent->g.sx;
1526
0
  else if (parent->type == LAYOUT_TOPBOTTOM) {
1527
0
    if (layout_add_horizontal_border(w, parent, status))
1528
0
      size = parent->g.sy - 1;
1529
0
    else
1530
0
      size = parent->g.sy;
1531
0
  } else
1532
0
    return (0);
1533
0
  if (size < number - 1)
1534
0
    return (0);
1535
0
  each = (size - (number - 1)) / number;
1536
0
  if (each == 0)
1537
0
    return (0);
1538
1539
  /*
1540
   * Remaining space after assigning that which can be evenly
1541
   * distributed.
1542
   */
1543
0
  remainder = size - (number * (each + 1)) + 1;
1544
1545
0
  changed = 0;
1546
0
  TAILQ_FOREACH (lc, &parent->cells, entry) {
1547
0
    if (!layout_cell_is_tiled(lc))
1548
0
      continue;
1549
0
    change = 0;
1550
0
    if (parent->type == LAYOUT_LEFTRIGHT) {
1551
0
      change = each - (int)lc->g.sx;
1552
0
      if (remainder > 0) {
1553
0
        change++;
1554
0
        remainder--;
1555
0
      }
1556
0
      layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, change);
1557
0
    } else if (parent->type == LAYOUT_TOPBOTTOM) {
1558
0
      if (layout_add_horizontal_border(w, lc, status))
1559
0
        this = each + 1;
1560
0
      else
1561
0
        this = each;
1562
0
      if (remainder > 0) {
1563
0
        this++;
1564
0
        remainder--;
1565
0
      }
1566
0
      change = this - (int)lc->g.sy;
1567
0
      layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, change);
1568
0
    }
1569
0
    if (change != 0)
1570
0
      changed = 1;
1571
0
  }
1572
0
  return (changed);
1573
0
}
1574
1575
/* Spread out cells evenly. */
1576
void
1577
layout_spread_out(struct window_pane *wp)
1578
0
{
1579
0
  struct layout_cell  *parent;
1580
0
  struct window   *w = wp->window;
1581
1582
0
  parent = wp->layout_cell->parent;
1583
0
  if (parent == NULL)
1584
0
    return;
1585
1586
0
  do {
1587
0
    if (layout_spread_cell(w, parent)) {
1588
0
      layout_fix_offsets(w);
1589
0
      layout_fix_panes(w, NULL);
1590
0
      break;
1591
0
    }
1592
0
  } while ((parent = parent->parent) != NULL);
1593
0
}
1594
1595
/* Get a new tiled cell. */
1596
struct layout_cell *
1597
layout_get_tiled_cell(struct cmdq_item *item, struct args *args,
1598
    struct window *w, struct window_pane *wp, int flags, char **cause)
1599
0
{
1600
0
  struct layout_cell  *lc;
1601
0
  enum layout_type   type;
1602
0
  u_int      curval;
1603
0
  int      size = -1;
1604
0
  char      *error = NULL;
1605
1606
0
  if (window_pane_is_floating(wp)) {
1607
0
    *cause = xstrdup("can't split a floating pane");
1608
0
    return (NULL);
1609
0
  }
1610
1611
0
  type = LAYOUT_TOPBOTTOM;
1612
0
  if (args_has(args, 'h'))
1613
0
    type = LAYOUT_LEFTRIGHT;
1614
1615
0
  if (args_has(args, 'l') || args_has(args, 'p')) {
1616
0
    if (args_has(args, 'f')) {
1617
0
      if (type == LAYOUT_TOPBOTTOM)
1618
0
        curval = w->sy;
1619
0
      else
1620
0
        curval = w->sx;
1621
0
    } else {
1622
0
      if (type == LAYOUT_TOPBOTTOM)
1623
0
        curval = wp->sy;
1624
0
      else
1625
0
        curval = wp->sx;
1626
0
    }
1627
0
  }
1628
1629
0
  if (args_has(args, 'l')) {
1630
0
    size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval,
1631
0
        item, &error);
1632
0
  } else if (args_has(args, 'p')) {
1633
0
    size = args_strtonum_and_expand(args, 'p', 0, 100, item,
1634
0
        &error);
1635
0
    if (error == NULL)
1636
0
      size = curval * size / 100;
1637
0
  }
1638
0
  if (error != NULL) {
1639
0
    xasprintf(cause, "invalid tiled geometry %s", error);
1640
0
    free(error);
1641
0
    return (NULL);
1642
0
  }
1643
1644
0
  if (args_has(args, 'b'))
1645
0
    flags |= SPAWN_BEFORE;
1646
0
  if (args_has(args, 'f'))
1647
0
    flags |= SPAWN_FULLSIZE;
1648
1649
0
  window_push_zoom(wp->window, 1, args_has(args, 'Z'));
1650
0
  lc = layout_split_pane(wp, type, size, flags);
1651
0
  if (lc == NULL)
1652
0
    *cause = xstrdup("no space for a new pane");
1653
1654
0
  return (lc);
1655
0
}
1656
1657
struct layout_cell *
1658
layout_get_floating_cell(struct cmdq_item *item, struct args *args,
1659
    enum pane_lines lines, struct window *w, struct window_pane *wp,
1660
    char **cause)
1661
0
{
1662
0
  struct layout_cell  *lcnew;
1663
0
  struct layout_geometry   fg;
1664
1665
0
  layout_geometry_init(&fg);
1666
0
  if (layout_floating_args_parse(item, args, lines, w, &fg, cause) != 0)
1667
0
    return (NULL);
1668
1669
0
  window_push_zoom(wp->window, 1, args_has(args, 'Z'));
1670
0
  lcnew = layout_floating_pane(w, wp, &fg);
1671
0
  return (lcnew);
1672
0
}
1673
1674
int
1675
layout_floating_args_parse(struct cmdq_item *item, struct args *args,
1676
    enum pane_lines lines, struct window *w, struct layout_geometry *lg,
1677
    char **cause)
1678
0
{
1679
0
  int  sx, sy, ox, oy;
1680
0
  char  *error = NULL;
1681
1682
0
  sx = lg->sx == UINT_MAX ? w->sx / 2 : lg->sx;
1683
0
  sy = lg->sy == UINT_MAX ? w->sy / 4 : lg->sy;
1684
0
  ox = lg->xoff;
1685
0
  oy = lg->yoff;
1686
1687
0
  if (args_has(args, 'x')) {
1688
0
    sx = args_percentage_and_expand(args, 'x', 0, PANE_MAXIMUM,
1689
0
        w->sx, item, &error);
1690
0
    if (error != NULL) {
1691
0
      xasprintf(cause, "position %s", error);
1692
0
      free(error);
1693
0
      return (-1);
1694
0
    }
1695
0
    if (lines != PANE_LINES_NONE)
1696
0
      sx -= 2;
1697
0
  }
1698
0
  if (args_has(args, 'y')) {
1699
0
    sy = args_percentage_and_expand(args, 'y', 0, PANE_MAXIMUM,
1700
0
        w->sy, item, &error);
1701
0
    if (error != NULL) {
1702
0
      xasprintf(cause, "position %s", error);
1703
0
      free(error);
1704
0
      return (-1);
1705
0
    }
1706
0
    if (lines != PANE_LINES_NONE)
1707
0
      sy -= 2;
1708
0
  }
1709
0
  if (args_has(args, 'X')) {
1710
0
    ox = args_percentage_and_expand(args, 'X', -sx, w->sx,
1711
0
        w->sx, item, &error);
1712
0
    if (error != NULL) {
1713
0
      xasprintf(cause, "position %s", error);
1714
0
      free(error);
1715
0
      return (-1);
1716
0
    }
1717
0
  }
1718
0
  if (args_has(args, 'Y')) {
1719
0
    oy = args_percentage_and_expand(args, 'Y', -sy, w->sy,
1720
0
        w->sy, item, &error);
1721
0
    if (error != NULL) {
1722
0
      xasprintf(cause, "position %s", error);
1723
0
      free(error);
1724
0
      return (-1);
1725
0
    }
1726
0
  }
1727
1728
0
  if (ox == INT_MAX) {
1729
0
    if (w->last_new_pane_x == 0)
1730
0
      ox = 4;
1731
0
    else {
1732
0
      ox = w->last_new_pane_x + 4;
1733
0
      if (w->last_new_pane_x > w->sx)
1734
0
        ox = 4;
1735
0
    }
1736
0
    w->last_new_pane_x = ox;
1737
0
  } else if (args_has(args, 'X'))
1738
0
    if (lines != PANE_LINES_NONE)
1739
0
      ox += 1;
1740
0
  if (oy == INT_MAX) {
1741
0
    if (w->last_new_pane_y == 0)
1742
0
      oy = 2;
1743
0
    else {
1744
0
      oy = w->last_new_pane_y + 2;
1745
0
      if (w->last_new_pane_y > w->sy)
1746
0
        oy = 2;
1747
0
    }
1748
0
    w->last_new_pane_y = oy;
1749
0
  } else if (args_has(args, 'Y'))
1750
0
    if (lines != PANE_LINES_NONE)
1751
0
      oy += 1;
1752
1753
0
  if (sx < PANE_MINIMUM || sx > PANE_MAXIMUM) {
1754
0
    *cause = xstrdup("invalid width");
1755
0
    return (-1);
1756
0
  }
1757
0
  if (sy < PANE_MINIMUM || sy > PANE_MAXIMUM) {
1758
0
    *cause = xstrdup("invalid height");
1759
0
    return (-1);
1760
0
  }
1761
1762
0
  lg->sx = sx;
1763
0
  lg->sy = sy;
1764
0
  lg->xoff = ox;
1765
0
  lg->yoff = oy;
1766
0
  return (0);
1767
0
}
1768
1769
/*
1770
 * Removes a cell from the tiled layout by giving the cell's space to the
1771
 * nearest neighbour.
1772
 */
1773
int
1774
layout_remove_tile(struct window *w, struct layout_cell *lc)
1775
0
{
1776
0
  struct layout_cell  *lcneighbour, *lcparent;
1777
0
  enum layout_type   type;
1778
0
  int      change;
1779
1780
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1781
0
    return (-1);
1782
1783
0
  lcneighbour = layout_cell_get_neighbour(lc);
1784
0
  if (lcneighbour == NULL) {
1785
0
    if (lc->parent != NULL)
1786
0
      layout_remove_tile(w, lc->parent);
1787
0
  } else if ((lcparent = lcneighbour->parent) != NULL) {
1788
0
    type = lcparent->type;
1789
    /*
1790
     * Adding the size of the layout cell plus its border to the
1791
     * neighbour.
1792
     */
1793
0
    if (type == LAYOUT_TOPBOTTOM)
1794
0
      change = lc->g.sy + 1;
1795
0
    else
1796
0
      change = lc->g.sx + 1;
1797
0
    layout_resize_adjust(w, lcneighbour, type, change);
1798
0
  }
1799
1800
  /*
1801
   * Zeroing out the cell geometry until the cell is retiled unless this
1802
   * is the top level node.
1803
   */
1804
0
  if (lc->parent != NULL)
1805
0
    layout_set_size(lc, 0, 0, 0, 0);
1806
0
  return (0);
1807
0
}
1808
1809
/*
1810
 * Inserts a cell back into the tiled layout by taking half the space from its
1811
 * nearest neighbour.
1812
 */
1813
int
1814
layout_insert_tile(struct window *w, struct layout_cell *lc)
1815
0
{
1816
0
  struct layout_cell  *lcneighbour, *lctiled, *lcparent;
1817
0
  enum layout_type   type;
1818
0
  u_int      size1, size2, saved_size;
1819
1820
0
  if (lc == NULL)
1821
0
    fatalx("layout cell cannot be null when tiling");
1822
1823
0
  if (layout_cell_is_tiled(lc))
1824
0
    return (-1);
1825
1826
0
  lcparent = lc->parent;
1827
0
  if (lcparent == NULL) {
1828
    /* Only pane in the layout. */
1829
0
    layout_set_size(lc, w->sx, w->sy, 0, 0);
1830
0
    return (0);
1831
0
  }
1832
1833
0
  type = lcparent->type;
1834
0
  lcneighbour = layout_cell_get_neighbour(lc);
1835
0
  if (lcneighbour == NULL) {
1836
    /*
1837
     * This will become the only visible cell in the parent.
1838
     * Tile the parent, then set the child's 'split' size.
1839
     */
1840
0
    layout_insert_tile(w, lcparent);
1841
0
    if (type == LAYOUT_LEFTRIGHT)
1842
0
      size1 = lcparent->g.sx;
1843
0
    else
1844
0
      size1 = lcparent->g.sy;
1845
0
    layout_resize_set_size(w, lc, type, size1);
1846
0
  } else {
1847
    /*
1848
     * If the neighbour is a node, a tiled child in the subtree of
1849
     * the neighbour is needed to check for space.
1850
     */
1851
0
    lctiled = layout_cell_get_first_tiled(lcneighbour);
1852
0
    if (!layout_split_check_space(lctiled->wp, lcneighbour, type))
1853
0
      return (-1);
1854
0
    layout_split_sizes(lcneighbour, -1, 0, type, &size1, &size2,
1855
0
        &saved_size);
1856
0
    layout_resize_set_size(w, lc, type, size1);
1857
0
    layout_resize_set_size(w, lcneighbour, type, size2);
1858
0
  }
1859
1860
  /* Setting opposite of the 'split' size to that of the parent. */
1861
0
  if (lcparent->type == LAYOUT_LEFTRIGHT) {
1862
0
    size1 = lcparent->g.sy;
1863
0
    type = LAYOUT_TOPBOTTOM;
1864
0
  } else {
1865
0
    size1 = lcparent->g.sx;
1866
0
    type = LAYOUT_LEFTRIGHT;
1867
0
  }
1868
0
  layout_resize_set_size(w, lc, type, size1);
1869
1870
0
  return (0);
1871
0
}