Coverage Report

Created: 2025-04-11 06:41

/src/tmux/cmd-refresh-client.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 <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Refresh client.
28
 */
29
30
static enum cmd_retval  cmd_refresh_client_exec(struct cmd *,
31
          struct cmdq_item *);
32
33
const struct cmd_entry cmd_refresh_client_entry = {
34
  .name = "refresh-client",
35
  .alias = "refresh",
36
37
  .args = { "A:B:cC:Df:r:F:l::LRSt:U", 0, 1, NULL },
38
  .usage = "[-cDlLRSU] [-A pane:state] [-B name:what:format] "
39
     "[-C XxY] [-f flags] [-r pane:report] " CMD_TARGET_CLIENT_USAGE
40
     " [adjustment]",
41
42
  .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
43
  .exec = cmd_refresh_client_exec
44
};
45
46
static void
47
cmd_refresh_client_update_subscription(struct client *tc, const char *value)
48
0
{
49
0
  char      *copy, *split, *name, *what;
50
0
  enum control_sub_type  subtype;
51
0
  int      subid = -1;
52
53
0
  copy = name = xstrdup(value);
54
0
  if ((split = strchr(copy, ':')) == NULL) {
55
0
    control_remove_sub(tc, copy);
56
0
    goto out;
57
0
  }
58
0
  *split++ = '\0';
59
60
0
  what = split;
61
0
  if ((split = strchr(what, ':')) == NULL)
62
0
    goto out;
63
0
  *split++ = '\0';
64
65
0
  if (strcmp(what, "%*") == 0)
66
0
    subtype = CONTROL_SUB_ALL_PANES;
67
0
  else if (sscanf(what, "%%%d", &subid) == 1 && subid >= 0)
68
0
    subtype = CONTROL_SUB_PANE;
69
0
  else if (strcmp(what, "@*") == 0)
70
0
    subtype = CONTROL_SUB_ALL_WINDOWS;
71
0
  else if (sscanf(what, "@%d", &subid) == 1 && subid >= 0)
72
0
    subtype = CONTROL_SUB_WINDOW;
73
0
  else
74
0
    subtype = CONTROL_SUB_SESSION;
75
0
  control_add_sub(tc, name, subtype, subid, split);
76
77
0
out:
78
0
  free(copy);
79
0
}
80
81
static enum cmd_retval
82
cmd_refresh_client_control_client_size(struct cmd *self, struct cmdq_item *item)
83
0
{
84
0
  struct args   *args = cmd_get_args(self);
85
0
  struct client   *tc = cmdq_get_target_client(item);
86
0
  const char    *size = args_get(args, 'C');
87
0
  u_int      w, x, y;
88
0
  struct client_window  *cw;
89
90
0
  if (sscanf(size, "@%u:%ux%u", &w, &x, &y) == 3) {
91
0
    if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM ||
92
0
        y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) {
93
0
      cmdq_error(item, "size too small or too big");
94
0
      return (CMD_RETURN_ERROR);
95
0
    }
96
0
    log_debug("%s: client %s window @%u: size %ux%u", __func__,
97
0
        tc->name, w, x, y);
98
0
    cw = server_client_add_client_window(tc, w);
99
0
    cw->sx = x;
100
0
    cw->sy = y;
101
0
    tc->flags |= CLIENT_WINDOWSIZECHANGED;
102
0
    recalculate_sizes_now(1);
103
0
    return (CMD_RETURN_NORMAL);
104
0
  }
105
0
  if (sscanf(size, "@%u:", &w) == 1) {
106
0
    cw = server_client_get_client_window(tc, w);
107
0
    if (cw != NULL) {
108
0
      log_debug("%s: client %s window @%u: no size", __func__,
109
0
          tc->name, w);
110
0
      cw->sx = 0;
111
0
      cw->sy = 0;
112
0
      recalculate_sizes_now(1);
113
0
    }
114
0
    return (CMD_RETURN_NORMAL);
115
0
  }
116
117
0
  if (sscanf(size, "%u,%u", &x, &y) != 2 &&
118
0
      sscanf(size, "%ux%u", &x, &y) != 2) {
119
0
    cmdq_error(item, "bad size argument");
120
0
    return (CMD_RETURN_ERROR);
121
0
  }
122
0
  if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM ||
123
0
      y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) {
124
0
    cmdq_error(item, "size too small or too big");
125
0
    return (CMD_RETURN_ERROR);
126
0
  }
127
0
  tty_set_size(&tc->tty, x, y, 0, 0);
128
0
  tc->flags |= CLIENT_SIZECHANGED;
129
0
  recalculate_sizes_now(1);
130
0
  return (CMD_RETURN_NORMAL);
131
0
}
132
133
static void
134
cmd_refresh_client_update_offset(struct client *tc, const char *value)
135
0
{
136
0
  struct window_pane  *wp;
137
0
  char      *copy, *split;
138
0
  u_int      pane;
139
140
0
  if (*value != '%')
141
0
    return;
142
0
  copy = xstrdup(value);
143
0
  if ((split = strchr(copy, ':')) == NULL)
144
0
    goto out;
145
0
  *split++ = '\0';
146
147
0
  if (sscanf(copy, "%%%u", &pane) != 1)
148
0
    goto out;
149
0
  wp = window_pane_find_by_id(pane);
150
0
  if (wp == NULL)
151
0
    goto out;
152
153
0
  if (strcmp(split, "on") == 0)
154
0
    control_set_pane_on(tc, wp);
155
0
  else if (strcmp(split, "off") == 0)
156
0
    control_set_pane_off(tc, wp);
157
0
  else if (strcmp(split, "continue") == 0)
158
0
    control_continue_pane(tc, wp);
159
0
  else if (strcmp(split, "pause") == 0)
160
0
    control_pause_pane(tc, wp);
161
162
0
out:
163
0
  free(copy);
164
0
}
165
166
static enum cmd_retval
167
cmd_refresh_client_clipboard(struct cmd *self, struct cmdq_item *item)
168
0
{
169
0
  struct args   *args = cmd_get_args(self);
170
0
  struct client   *tc = cmdq_get_target_client(item);
171
0
  const char    *p;
172
0
  u_int      i;
173
0
  struct cmd_find_state  fs;
174
175
0
  p = args_get(args, 'l');
176
0
  if (p == NULL) {
177
0
    if (tc->flags & CLIENT_CLIPBOARDBUFFER)
178
0
      return (CMD_RETURN_NORMAL);
179
0
    tc->flags |= CLIENT_CLIPBOARDBUFFER;
180
0
  } else {
181
0
    if (cmd_find_target(&fs, item, p, CMD_FIND_PANE, 0) != 0)
182
0
      return (CMD_RETURN_ERROR);
183
0
    for (i = 0; i < tc->clipboard_npanes; i++) {
184
0
      if (tc->clipboard_panes[i] == fs.wp->id)
185
0
        break;
186
0
    }
187
0
    if (i != tc->clipboard_npanes)
188
0
      return (CMD_RETURN_NORMAL);
189
0
    tc->clipboard_panes = xreallocarray(tc->clipboard_panes,
190
0
        tc->clipboard_npanes + 1, sizeof *tc->clipboard_panes);
191
0
    tc->clipboard_panes[tc->clipboard_npanes++] = fs.wp->id;
192
0
  }
193
0
  tty_clipboard_query(&tc->tty);
194
0
  return (CMD_RETURN_NORMAL);
195
0
}
196
197
static void
198
cmd_refresh_report(struct tty *tty, const char *value)
199
0
{
200
0
  struct window_pane  *wp;
201
0
  u_int      pane;
202
0
  size_t       size = 0;
203
0
  char      *copy, *split;
204
205
0
  if (*value != '%')
206
0
    return;
207
0
  copy = xstrdup(value);
208
0
  if ((split = strchr(copy, ':')) == NULL)
209
0
    goto out;
210
0
  *split++ = '\0';
211
212
0
  if (sscanf(copy, "%%%u", &pane) != 1)
213
0
    goto out;
214
0
  wp = window_pane_find_by_id(pane);
215
0
  if (wp == NULL)
216
0
    goto out;
217
218
0
  tty_keys_colours(tty, split, strlen(split), &size, &wp->control_fg,
219
0
      &wp->control_bg);
220
221
0
out:
222
0
  free(copy);
223
0
}
224
225
static enum cmd_retval
226
cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item)
227
0
{
228
0
  struct args   *args = cmd_get_args(self);
229
0
  struct client   *tc = cmdq_get_target_client(item);
230
0
  struct tty    *tty = &tc->tty;
231
0
  struct window   *w;
232
0
  const char    *errstr;
233
0
  u_int      adjust;
234
0
  struct args_value *av;
235
236
0
  if (args_has(args, 'c') ||
237
0
      args_has(args, 'L') ||
238
0
      args_has(args, 'R') ||
239
0
      args_has(args, 'U') ||
240
0
      args_has(args, 'D'))
241
0
  {
242
0
    if (args_count(args) == 0)
243
0
      adjust = 1;
244
0
    else {
245
0
      adjust = strtonum(args_string(args, 0), 1, INT_MAX,
246
0
          &errstr);
247
0
      if (errstr != NULL) {
248
0
        cmdq_error(item, "adjustment %s", errstr);
249
0
        return (CMD_RETURN_ERROR);
250
0
      }
251
0
    }
252
253
0
    if (args_has(args, 'c'))
254
0
      tc->pan_window = NULL;
255
0
    else {
256
0
      w = tc->session->curw->window;
257
0
      if (tc->pan_window != w) {
258
0
        tc->pan_window = w;
259
0
        tc->pan_ox = tty->oox;
260
0
        tc->pan_oy = tty->ooy;
261
0
      }
262
0
      if (args_has(args, 'L')) {
263
0
        if (tc->pan_ox > adjust)
264
0
          tc->pan_ox -= adjust;
265
0
        else
266
0
          tc->pan_ox = 0;
267
0
      } else if (args_has(args, 'R')) {
268
0
        tc->pan_ox += adjust;
269
0
        if (tc->pan_ox > w->sx - tty->osx)
270
0
          tc->pan_ox = w->sx - tty->osx;
271
0
      } else if (args_has(args, 'U')) {
272
0
        if (tc->pan_oy > adjust)
273
0
          tc->pan_oy -= adjust;
274
0
        else
275
0
          tc->pan_oy = 0;
276
0
      } else if (args_has(args, 'D')) {
277
0
        tc->pan_oy += adjust;
278
0
        if (tc->pan_oy > w->sy - tty->osy)
279
0
          tc->pan_oy = w->sy - tty->osy;
280
0
      }
281
0
    }
282
0
    tty_update_client_offset(tc);
283
0
    server_redraw_client(tc);
284
0
    return (CMD_RETURN_NORMAL);
285
0
  }
286
287
0
  if (args_has(args, 'l'))
288
0
    return (cmd_refresh_client_clipboard(self, item));
289
290
0
  if (args_has(args, 'F')) /* -F is an alias for -f */
291
0
    server_client_set_flags(tc, args_get(args, 'F'));
292
0
  if (args_has(args, 'f'))
293
0
    server_client_set_flags(tc, args_get(args, 'f'));
294
0
  if (args_has(args, 'r'))
295
0
    cmd_refresh_report(tty, args_get(args, 'r'));
296
297
0
  if (args_has(args, 'A')) {
298
0
    if (~tc->flags & CLIENT_CONTROL)
299
0
      goto not_control_client;
300
0
    av = args_first_value(args, 'A');
301
0
    while (av != NULL) {
302
0
      cmd_refresh_client_update_offset(tc, av->string);
303
0
      av = args_next_value(av);
304
0
    }
305
0
    return (CMD_RETURN_NORMAL);
306
0
  }
307
0
  if (args_has(args, 'B')) {
308
0
    if (~tc->flags & CLIENT_CONTROL)
309
0
      goto not_control_client;
310
0
    av = args_first_value(args, 'B');
311
0
    while (av != NULL) {
312
0
      cmd_refresh_client_update_subscription(tc, av->string);
313
0
      av = args_next_value(av);
314
0
    }
315
0
    return (CMD_RETURN_NORMAL);
316
0
  }
317
0
  if (args_has(args, 'C')) {
318
0
    if (~tc->flags & CLIENT_CONTROL)
319
0
      goto not_control_client;
320
0
    return (cmd_refresh_client_control_client_size(self, item));
321
0
  }
322
323
0
  if (args_has(args, 'S')) {
324
0
    tc->flags |= CLIENT_STATUSFORCE;
325
0
    server_status_client(tc);
326
0
  } else {
327
0
    tc->flags |= CLIENT_STATUSFORCE;
328
0
    server_redraw_client(tc);
329
0
  }
330
0
  return (CMD_RETURN_NORMAL);
331
332
0
not_control_client:
333
0
  cmdq_error(item, "not a control client");
334
0
  return (CMD_RETURN_ERROR);
335
0
}