Coverage Report

Created: 2026-07-30 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/resize.c
Line
Count
Source
1
/* $OpenBSD: resize.c,v 1.58 2026/07/17 08:37:29 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
21
#include <string.h>
22
23
#include "tmux.h"
24
25
static void
26
resize_fire_window_resized(struct window *w, u_int old_sx, u_int old_sy)
27
0
{
28
0
  struct event_payload  *ep;
29
0
  struct cmd_find_state  fs;
30
31
0
  ep = event_payload_create();
32
0
  cmd_find_from_window(&fs, w, 0);
33
0
  event_payload_set_target(ep, &fs);
34
0
  event_payload_set_window(ep, "window", w);
35
0
  event_payload_set_uint(ep, "width", w->sx);
36
0
  event_payload_set_uint(ep, "height", w->sy);
37
0
  event_payload_set_uint(ep, "old_width", old_sx);
38
0
  event_payload_set_uint(ep, "old_height", old_sy);
39
0
  events_fire("window-resized", ep);
40
0
}
41
42
void
43
resize_window(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
44
0
{
45
0
  u_int old_sx = w->sx, old_sy = w->sy;
46
0
  int zoomed;
47
48
  /* Check size limits. */
49
0
  if (sx < WINDOW_MINIMUM)
50
0
    sx = WINDOW_MINIMUM;
51
0
  if (sx > WINDOW_MAXIMUM)
52
0
    sx = WINDOW_MAXIMUM;
53
0
  if (sy < WINDOW_MINIMUM)
54
0
    sy = WINDOW_MINIMUM;
55
0
  if (sy > WINDOW_MAXIMUM)
56
0
    sy = WINDOW_MAXIMUM;
57
58
  /* If the window is zoomed, unzoom. */
59
0
  zoomed = w->flags & WINDOW_ZOOMED;
60
0
  if (zoomed)
61
0
    window_unzoom(w, 1);
62
63
  /* Resize the layout first. */
64
0
  layout_resize(w, sx, sy);
65
66
  /* Resize the window, it can be no smaller than the layout. */
67
0
  if (sx < w->layout_root->g.sx)
68
0
    sx = w->layout_root->g.sx;
69
0
  if (sy < w->layout_root->g.sy)
70
0
    sy = w->layout_root->g.sy;
71
0
  window_resize(w, sx, sy, xpixel, ypixel);
72
0
  log_debug("%s: @%u resized to %ux%u; layout %ux%u", __func__, w->id,
73
0
      sx, sy, w->layout_root->g.sx, w->layout_root->g.sy);
74
75
  /* Restore the window zoom state. */
76
0
  if (zoomed)
77
0
    window_zoom(w->active);
78
79
0
  tty_update_window_offset(w);
80
0
  server_redraw_window(w);
81
0
  events_fire_window("window-layout-changed", w);
82
0
  resize_fire_window_resized(w, old_sx, old_sy);
83
0
  w->flags &= ~WINDOW_RESIZE;
84
0
}
85
86
static int
87
ignore_client_size(struct client *c)
88
0
{
89
0
  struct client *loop;
90
91
0
  if (c->session == NULL)
92
0
    return (1);
93
0
  if (c->flags & CLIENT_NOSIZEFLAGS)
94
0
    return (1);
95
0
  if (c->flags & CLIENT_IGNORESIZE) {
96
    /*
97
     * Ignore flagged clients if there are any attached clients
98
     * that aren't flagged.
99
     */
100
0
    TAILQ_FOREACH (loop, &clients, entry) {
101
0
      if (loop->session == NULL)
102
0
        continue;
103
0
      if (loop->flags & CLIENT_NOSIZEFLAGS)
104
0
        continue;
105
0
      if (~loop->flags & CLIENT_IGNORESIZE)
106
0
        return (1);
107
0
    }
108
0
  }
109
0
  if ((c->flags & CLIENT_CONTROL) &&
110
0
      (~c->flags & CLIENT_SIZECHANGED) &&
111
0
      (~c->flags & CLIENT_WINDOWSIZECHANGED))
112
0
    return (1);
113
0
  return (0);
114
0
}
115
116
static u_int
117
clients_with_window(struct window *w)
118
0
{
119
0
  struct client *loop;
120
0
  u_int    n = 0;
121
122
0
  TAILQ_FOREACH(loop, &clients, entry) {
123
0
    if (ignore_client_size(loop) || !session_has(loop->session, w))
124
0
      continue;
125
0
    if (++n > 1)
126
0
      break;
127
0
  }
128
0
  return (n);
129
0
}
130
131
static int
132
clients_calculate_size(int type, int current, struct client *c,
133
    struct session *s, struct window *w, int (*skip_client)(struct client *,
134
    int, int, struct session *, struct window *), u_int *sx, u_int *sy,
135
    u_int *xpixel, u_int *ypixel)
136
0
{
137
0
  struct client *loop;
138
0
  u_int    cx, cy, n = 0;
139
140
  /*
141
   * Start comparing with 0 for largest and UINT_MAX for smallest or
142
   * latest.
143
   */
144
0
  if (type == WINDOW_SIZE_LARGEST) {
145
0
    *sx = 0;
146
0
    *sy = 0;
147
0
  } else if (w != NULL && type == WINDOW_SIZE_MANUAL) {
148
0
    *sx = w->manual_sx;
149
0
    *sy = w->manual_sy;
150
0
    log_debug("%s: manual size %ux%u", __func__, *sx, *sy);
151
0
  } else {
152
0
    *sx = UINT_MAX;
153
0
    *sy = UINT_MAX;
154
0
  }
155
0
  *xpixel = *ypixel = 0;
156
157
  /*
158
   * For latest, count the number of clients with this window. We only
159
   * care if there is more than one.
160
   */
161
0
  if (type == WINDOW_SIZE_LATEST && w != NULL)
162
0
    n = clients_with_window(w);
163
164
  /* Skip setting the size if manual */
165
0
  if (type == WINDOW_SIZE_MANUAL)
166
0
    goto skip;
167
168
  /* Loop over the clients and work out the size. */
169
0
  TAILQ_FOREACH(loop, &clients, entry) {
170
0
    if (loop != c && ignore_client_size(loop)) {
171
0
      log_debug("%s: ignoring %s (1)", __func__, loop->name);
172
0
      continue;
173
0
    }
174
0
    if (loop != c && skip_client(loop, type, current, s, w)) {
175
0
      log_debug("%s: skipping %s (1)", __func__, loop->name);
176
0
      continue;
177
0
    }
178
179
    /*
180
     * If there are multiple clients attached, only accept the
181
     * latest client; otherwise let the only client be chosen as
182
     * for smallest.
183
     */
184
0
    if (type == WINDOW_SIZE_LATEST && n > 1 && loop != w->latest) {
185
0
      log_debug("%s: %s is not latest", __func__, loop->name);
186
0
      continue;
187
0
    }
188
189
    /* Work out this client's size. */
190
0
    if (w == NULL ||
191
0
        !control_get_window_size(loop, w->id, &cx, &cy) ||
192
0
        cx == 0 || cy == 0) {
193
0
      cx = loop->tty.sx;
194
0
      cy = loop->tty.sy - status_line_size(loop);
195
0
    }
196
197
    /*
198
     * If it is larger or smaller than the best so far, update the
199
     * new size.
200
     */
201
0
    if (type == WINDOW_SIZE_LARGEST) {
202
0
      if (cx > *sx)
203
0
        *sx = cx;
204
0
      if (cy > *sy)
205
0
        *sy = cy;
206
0
    } else {
207
0
      if (cx < *sx)
208
0
        *sx = cx;
209
0
      if (cy < *sy)
210
0
        *sy = cy;
211
0
    }
212
0
    if (loop->tty.xpixel > *xpixel && loop->tty.ypixel > *ypixel) {
213
0
      *xpixel = loop->tty.xpixel;
214
0
      *ypixel = loop->tty.ypixel;
215
0
    }
216
0
    log_debug("%s: after %s (%ux%u), size is %ux%u", __func__,
217
0
        loop->name, cx, cy, *sx, *sy);
218
0
  }
219
0
  if (*sx != UINT_MAX && *sy != UINT_MAX)
220
0
    log_debug("%s: calculated size %ux%u", __func__, *sx, *sy);
221
0
  else
222
0
    log_debug("%s: no calculated size", __func__);
223
224
0
skip:
225
  /*
226
   * Do not allow any size to be larger than the per-client window size
227
   * if one exists.
228
   */
229
0
  if (w != NULL) {
230
0
    TAILQ_FOREACH(loop, &clients, entry) {
231
0
      if (loop != c && ignore_client_size(loop))
232
0
        continue;
233
0
      if (loop != c && skip_client(loop, type, current, s, w))
234
0
        continue;
235
236
      /* Look up per-window size if any. */
237
0
      if (~loop->flags & CLIENT_WINDOWSIZECHANGED)
238
0
        continue;
239
0
      if (!control_get_window_size(loop, w->id, &cx, &cy))
240
0
        continue;
241
242
      /* Clamp the size. */
243
0
      log_debug("%s: %s size for @%u is %ux%u", __func__,
244
0
          loop->name, w->id, cx, cy);
245
0
      if (cx != 0 && *sx > cx)
246
0
        *sx = cx;
247
0
      if (cy != 0 && *sy > cy)
248
0
        *sy = cy;
249
0
    }
250
0
  }
251
0
  if (*sx != UINT_MAX && *sy != UINT_MAX)
252
0
    log_debug("%s: calculated size %ux%u", __func__, *sx, *sy);
253
0
  else
254
0
    log_debug("%s: no calculated size", __func__);
255
256
  /* Return whether a suitable size was found. */
257
0
  if (type == WINDOW_SIZE_MANUAL) {
258
0
    log_debug("%s: type is manual", __func__);
259
0
    return (w != NULL);
260
0
  }
261
0
  if (type == WINDOW_SIZE_LARGEST) {
262
0
    log_debug("%s: type is largest", __func__);
263
0
    return (*sx != 0 && *sy != 0);
264
0
  }
265
0
  if (type == WINDOW_SIZE_LATEST)
266
0
    log_debug("%s: type is latest", __func__);
267
0
  else
268
0
    log_debug("%s: type is smallest", __func__);
269
0
  return (*sx != UINT_MAX && *sy != UINT_MAX);
270
0
}
271
272
static int
273
default_window_size_skip_client(struct client *loop, __unused int type,
274
    __unused int current, struct session *s, struct window *w)
275
0
{
276
0
  if (w != NULL && !session_has(loop->session, w))
277
0
    return (1);
278
0
  if (w == NULL && loop->session != s)
279
0
    return (1);
280
0
  return (0);
281
0
}
282
283
void
284
default_window_size(struct client *c, struct session *s, struct window *w,
285
  u_int *sx, u_int *sy, u_int *xpixel, u_int *ypixel, int type)
286
0
{
287
0
  const char  *value;
288
289
  /* Get type if not provided. */
290
0
  if (type == -1)
291
0
    type = options_get_number(global_w_options, "window-size");
292
293
  /*
294
   * Latest clients can use the given client if suitable. If there is no
295
   * client and no window, use the default size as for manual type.
296
   */
297
0
  if (type == WINDOW_SIZE_LATEST && c != NULL && !ignore_client_size(c)) {
298
0
    *sx = c->tty.sx;
299
0
    *sy = c->tty.sy - status_line_size(c);
300
0
    *xpixel = c->tty.xpixel;
301
0
    *ypixel = c->tty.ypixel;
302
0
    log_debug("%s: using %ux%u from %s", __func__, *sx, *sy,
303
0
        c->name);
304
0
    goto done;
305
0
  }
306
307
  /*
308
   * Ignore the given client if it is a control client - the creating
309
   * client should only affect the size if it is not a control client.
310
   */
311
0
  if (c != NULL && (c->flags & CLIENT_CONTROL))
312
0
    c = NULL;
313
314
  /*
315
   * Look for a client to base the size on. If none exists (or the type
316
   * is manual), use the default-size option.
317
   */
318
0
  if (!clients_calculate_size(type, 0, c, s, w,
319
0
      default_window_size_skip_client, sx, sy, xpixel, ypixel)) {
320
0
    value = options_get_string(s->options, "default-size");
321
0
    if (sscanf(value, "%ux%u", sx, sy) != 2) {
322
0
      *sx = 80;
323
0
      *sy = 24;
324
0
    }
325
0
    log_debug("%s: using %ux%u from default-size", __func__, *sx,
326
0
        *sy);
327
0
  }
328
329
0
done:
330
  /* Make sure the limits are enforced. */
331
0
  if (*sx < WINDOW_MINIMUM)
332
0
    *sx = WINDOW_MINIMUM;
333
0
  if (*sx > WINDOW_MAXIMUM)
334
0
    *sx = WINDOW_MAXIMUM;
335
0
  if (*sy < WINDOW_MINIMUM)
336
0
    *sy = WINDOW_MINIMUM;
337
0
  if (*sy > WINDOW_MAXIMUM)
338
0
    *sy = WINDOW_MAXIMUM;
339
0
  log_debug("%s: resulting size is %ux%u", __func__, *sx, *sy);
340
0
}
341
342
static int
343
recalculate_size_skip_client(struct client *loop, __unused int type,
344
    int current, __unused struct session *s, struct window *w)
345
0
{
346
  /*
347
   * If the current flag is set, then skip any client where this window
348
   * is not the current window - this is used for aggressive-resize.
349
   * Otherwise skip any session that doesn't contain the window.
350
   */
351
0
  if (loop->session->curw == NULL)
352
0
    return (1);
353
0
  if (current)
354
0
    return (loop->session->curw->window != w);
355
0
  return (session_has(loop->session, w) == 0);
356
0
}
357
358
void
359
recalculate_size(struct window *w, int now)
360
0
{
361
0
  u_int sx, sy, xpixel = 0, ypixel = 0;
362
0
  int type, current, changed;
363
364
  /*
365
   * Do not attempt to resize windows which have no pane, they must be on
366
   * the way to destruction.
367
   */
368
0
  if (w->active == NULL)
369
0
    return;
370
0
  log_debug("%s: @%u is %ux%u", __func__, w->id, w->sx, w->sy);
371
372
  /*
373
   * Type is manual, smallest, largest, latest. Current is the
374
   * aggressive-resize option (do not resize based on clients where the
375
   * window is not the current window).
376
   */
377
0
  type = options_get_number(w->options, "window-size");
378
0
  current = options_get_number(w->options, "aggressive-resize");
379
380
  /* Look for a suitable client and get the new size. */
381
0
  changed = clients_calculate_size(type, current, NULL, NULL, w,
382
0
      recalculate_size_skip_client, &sx, &sy, &xpixel, &ypixel);
383
384
  /*
385
   * Make sure the size has actually changed. If the window has already
386
   * got a resize scheduled, then use the new size; otherwise the old.
387
   */
388
0
  if (w->flags & WINDOW_RESIZE) {
389
0
    if (!now && changed && w->new_sx == sx && w->new_sy == sy)
390
0
      changed = 0;
391
0
  } else {
392
0
    if (!now && changed && w->sx == sx && w->sy == sy)
393
0
      changed = 0;
394
0
  }
395
396
  /*
397
   * If the size hasn't changed, update the window offset but not the
398
   * size.
399
   */
400
0
  if (!changed) {
401
0
    log_debug("%s: @%u no size change", __func__, w->id);
402
0
    tty_update_window_offset(w);
403
0
    return;
404
0
  }
405
406
  /*
407
   * If the now flag is set or if the window is sized manually, change
408
   * the size immediately. Otherwise set the flag and it will be done
409
   * later.
410
   */
411
0
  log_debug("%s: @%u new size %ux%u", __func__, w->id, sx, sy);
412
0
  if (now || type == WINDOW_SIZE_MANUAL)
413
0
    resize_window(w, sx, sy, xpixel, ypixel);
414
0
  else {
415
0
    w->new_sx = sx;
416
0
    w->new_sy = sy;
417
0
    w->new_xpixel = xpixel;
418
0
    w->new_ypixel = ypixel;
419
420
0
    w->flags |= WINDOW_RESIZE;
421
0
    tty_update_window_offset(w);
422
0
  }
423
0
}
424
425
void
426
recalculate_sizes(void)
427
0
{
428
0
  recalculate_sizes_now(0);
429
0
}
430
431
void
432
recalculate_sizes_now(int now)
433
0
{
434
0
  struct session  *s;
435
0
  struct client *c;
436
0
  struct window *w;
437
438
  /*
439
   * Clear attached count and update saved status line information for
440
   * each session.
441
   */
442
0
  RB_FOREACH(s, sessions, &sessions) {
443
0
    s->attached = 0;
444
0
    status_update_cache(s);
445
0
  }
446
447
  /*
448
   * Increment attached count and check the status line size for each
449
   * client.
450
   */
451
0
  TAILQ_FOREACH(c, &clients, entry) {
452
0
    s = c->session;
453
0
    if (s != NULL && !(c->flags & CLIENT_UNATTACHEDFLAGS))
454
0
      s->attached++;
455
0
    if (ignore_client_size(c))
456
0
      continue;
457
0
    if (c->tty.sy <= s->statuslines || (c->flags & CLIENT_CONTROL))
458
0
      c->flags |= CLIENT_STATUSOFF;
459
0
    else
460
0
      c->flags &= ~CLIENT_STATUSOFF;
461
0
  }
462
463
  /* Walk each window and adjust the size. */
464
0
  RB_FOREACH(w, windows, &windows)
465
0
    recalculate_size(w, now);
466
0
}