Coverage Report

Created: 2025-08-29 06:28

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