/src/tmux/cmd-refresh-client.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-refresh-client.c,v 1.56 2026/10/09 13:12:14 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 <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 | | .description = "Refresh or resize a client.", |
36 | | .alias = "refresh", |
37 | | |
38 | | .args = { "A:B:cC:Df:r:F:lLRSt:U", 0, 1, NULL }, |
39 | | .usage = "[-cDlLRSU] [-A pane:state] [-B name:what:format] " |
40 | | "[-C XxY] [-f flags] [-r pane:report] " CMD_TARGET_CLIENT_USAGE |
41 | | " [adjustment]", |
42 | | |
43 | | .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG, |
44 | | .exec = cmd_refresh_client_exec |
45 | | }; |
46 | | |
47 | | static void |
48 | | cmd_refresh_client_update_subscription(struct client *tc, const char *value) |
49 | 0 | { |
50 | 0 | char *name, *format; |
51 | 0 | enum monitor_type type; |
52 | 0 | int id; |
53 | |
|
54 | 0 | if (monitor_parse(value, &name, &type, &id, &format) != 0) { |
55 | 0 | control_remove_sub(tc, value); |
56 | 0 | return; |
57 | 0 | } |
58 | 0 | control_add_sub(tc, name, type, id, format); |
59 | 0 | free(name); |
60 | 0 | free(format); |
61 | 0 | } |
62 | | |
63 | | static enum cmd_retval |
64 | | cmd_refresh_client_control_client_size(struct cmd *self, struct cmdq_item *item) |
65 | 0 | { |
66 | 0 | struct args *args = cmd_get_args(self); |
67 | 0 | struct client *tc = cmdq_get_target_client(item); |
68 | 0 | const char *size = args_get(args, 'C'); |
69 | 0 | u_int w, x, y; |
70 | |
|
71 | 0 | if (sscanf(size, "@%u:%ux%u", &w, &x, &y) == 3) { |
72 | 0 | if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM || |
73 | 0 | y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) { |
74 | 0 | cmdq_error(item, "size too small or too big"); |
75 | 0 | return (CMD_RETURN_ERROR); |
76 | 0 | } |
77 | 0 | log_debug("%s: client %s window @%u: size %ux%u", __func__, |
78 | 0 | tc->name, w, x, y); |
79 | 0 | control_set_window_size(tc, w, x, y); |
80 | 0 | tc->flags |= CLIENT_WINDOWSIZECHANGED; |
81 | 0 | recalculate_sizes_now(1); |
82 | 0 | return (CMD_RETURN_NORMAL); |
83 | 0 | } |
84 | 0 | if (sscanf(size, "@%u:", &w) == 1) { |
85 | 0 | log_debug("%s: client %s window @%u: no size", __func__, |
86 | 0 | tc->name, w); |
87 | 0 | control_clear_window_size(tc, w); |
88 | 0 | recalculate_sizes_now(1); |
89 | 0 | return (CMD_RETURN_NORMAL); |
90 | 0 | } |
91 | | |
92 | 0 | if (sscanf(size, "%u,%u", &x, &y) != 2 && |
93 | 0 | sscanf(size, "%ux%u", &x, &y) != 2) { |
94 | 0 | cmdq_error(item, "bad size argument"); |
95 | 0 | return (CMD_RETURN_ERROR); |
96 | 0 | } |
97 | 0 | if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM || |
98 | 0 | y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) { |
99 | 0 | cmdq_error(item, "size too small or too big"); |
100 | 0 | return (CMD_RETURN_ERROR); |
101 | 0 | } |
102 | 0 | tty_set_size(&tc->tty, x, y, 0, 0); |
103 | 0 | tc->flags |= CLIENT_SIZECHANGED; |
104 | 0 | recalculate_sizes_now(1); |
105 | 0 | return (CMD_RETURN_NORMAL); |
106 | 0 | } |
107 | | |
108 | | static void |
109 | | cmd_refresh_client_update_offset(struct client *tc, const char *value) |
110 | 0 | { |
111 | 0 | struct window_pane *wp; |
112 | 0 | char *copy, *split; |
113 | 0 | u_int pane; |
114 | |
|
115 | 0 | if (*value != '%') |
116 | 0 | return; |
117 | 0 | copy = xstrdup(value); |
118 | 0 | if ((split = strchr(copy, ':')) == NULL) |
119 | 0 | goto out; |
120 | 0 | *split++ = '\0'; |
121 | |
|
122 | 0 | if (sscanf(copy, "%%%u", &pane) != 1) |
123 | 0 | goto out; |
124 | 0 | wp = window_pane_find_by_id(pane); |
125 | 0 | if (wp == NULL) |
126 | 0 | goto out; |
127 | | |
128 | 0 | if (strcmp(split, "on") == 0) |
129 | 0 | control_set_pane_on(tc, wp); |
130 | 0 | else if (strcmp(split, "off") == 0) |
131 | 0 | control_set_pane_off(tc, wp); |
132 | 0 | else if (strcmp(split, "continue") == 0) |
133 | 0 | control_continue_pane(tc, wp); |
134 | 0 | else if (strcmp(split, "pause") == 0) |
135 | 0 | control_pause_pane(tc, wp); |
136 | |
|
137 | 0 | out: |
138 | 0 | free(copy); |
139 | 0 | } |
140 | | |
141 | | static void |
142 | | cmd_refresh_report(struct tty *tty, const char *value) |
143 | 0 | { |
144 | 0 | struct window_pane *wp; |
145 | 0 | u_int pane; |
146 | 0 | int fg, bg; |
147 | 0 | size_t size = 0; |
148 | 0 | char *copy, *split; |
149 | |
|
150 | 0 | if (*value != '%') |
151 | 0 | return; |
152 | 0 | copy = xstrdup(value); |
153 | 0 | if ((split = strchr(copy, ':')) == NULL) |
154 | 0 | goto out; |
155 | 0 | *split++ = '\0'; |
156 | |
|
157 | 0 | if (sscanf(copy, "%%%u", &pane) != 1) |
158 | 0 | goto out; |
159 | 0 | wp = window_pane_find_by_id(pane); |
160 | 0 | if (wp == NULL) |
161 | 0 | goto out; |
162 | | |
163 | 0 | fg = wp->control_fg; |
164 | 0 | bg = wp->control_bg; |
165 | 0 | if (tty_keys_colours(tty, split, strlen(split), &size, &fg, &bg) == 0) { |
166 | 0 | if (bg != wp->control_bg) |
167 | 0 | wp->flags |= PANE_THEMECHANGED; |
168 | 0 | wp->control_fg = fg; |
169 | 0 | wp->control_bg = bg; |
170 | 0 | } |
171 | |
|
172 | 0 | out: |
173 | 0 | free(copy); |
174 | 0 | } |
175 | | |
176 | | static enum cmd_retval |
177 | | cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item) |
178 | 0 | { |
179 | 0 | struct args *args = cmd_get_args(self); |
180 | 0 | struct client *tc = cmdq_get_target_client(item); |
181 | 0 | struct tty *tty = &tc->tty; |
182 | 0 | struct window *w; |
183 | 0 | const char *errstr; |
184 | 0 | u_int adjust; |
185 | 0 | struct args_value *av; |
186 | |
|
187 | 0 | if (args_has(args, 'c') || |
188 | 0 | args_has(args, 'L') || |
189 | 0 | args_has(args, 'R') || |
190 | 0 | args_has(args, 'U') || |
191 | 0 | args_has(args, 'D')) |
192 | 0 | { |
193 | 0 | if (args_count(args) == 0) |
194 | 0 | adjust = 1; |
195 | 0 | else { |
196 | 0 | adjust = strtonum(args_string(args, 0), 1, INT_MAX, |
197 | 0 | &errstr); |
198 | 0 | if (errstr != NULL) { |
199 | 0 | cmdq_error(item, "adjustment %s", errstr); |
200 | 0 | return (CMD_RETURN_ERROR); |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | 0 | if (args_has(args, 'c')) |
205 | 0 | tc->pan_window = NULL; |
206 | 0 | else { |
207 | 0 | w = tc->session->curw->window; |
208 | 0 | if (tc->pan_window != w) { |
209 | 0 | tc->pan_window = w; |
210 | 0 | tc->pan_ox = tty->oox; |
211 | 0 | tc->pan_oy = tty->ooy; |
212 | 0 | } |
213 | 0 | if (args_has(args, 'L')) { |
214 | 0 | if (tc->pan_ox > adjust) |
215 | 0 | tc->pan_ox -= adjust; |
216 | 0 | else |
217 | 0 | tc->pan_ox = 0; |
218 | 0 | } else if (args_has(args, 'R')) { |
219 | 0 | tc->pan_ox += adjust; |
220 | 0 | if (tc->pan_ox > w->sx - tty->osx) |
221 | 0 | tc->pan_ox = w->sx - tty->osx; |
222 | 0 | } else if (args_has(args, 'U')) { |
223 | 0 | if (tc->pan_oy > adjust) |
224 | 0 | tc->pan_oy -= adjust; |
225 | 0 | else |
226 | 0 | tc->pan_oy = 0; |
227 | 0 | } else if (args_has(args, 'D')) { |
228 | 0 | tc->pan_oy += adjust; |
229 | 0 | if (tc->pan_oy > w->sy - tty->osy) |
230 | 0 | tc->pan_oy = w->sy - tty->osy; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | tty_update_client_offset(tc); |
234 | 0 | server_redraw_client(tc); |
235 | 0 | return (CMD_RETURN_NORMAL); |
236 | 0 | } |
237 | | |
238 | 0 | if (args_has(args, 'l')) { |
239 | 0 | tty_clipboard_query(&tc->tty); |
240 | 0 | return (CMD_RETURN_NORMAL); |
241 | 0 | } |
242 | | |
243 | 0 | if (args_has(args, 'F')) /* -F is an alias for -f */ |
244 | 0 | server_client_set_flags(tc, args_get(args, 'F')); |
245 | 0 | if (args_has(args, 'f')) |
246 | 0 | server_client_set_flags(tc, args_get(args, 'f')); |
247 | 0 | if (args_has(args, 'r')) |
248 | 0 | cmd_refresh_report(tty, args_get(args, 'r')); |
249 | |
|
250 | 0 | if (args_has(args, 'A')) { |
251 | 0 | if (~tc->flags & CLIENT_CONTROL) |
252 | 0 | goto not_control_client; |
253 | 0 | av = args_first_value(args, 'A'); |
254 | 0 | while (av != NULL) { |
255 | 0 | cmd_refresh_client_update_offset(tc, av->string); |
256 | 0 | av = args_next_value(av); |
257 | 0 | } |
258 | 0 | return (CMD_RETURN_NORMAL); |
259 | 0 | } |
260 | 0 | if (args_has(args, 'B')) { |
261 | 0 | if (~tc->flags & CLIENT_CONTROL) |
262 | 0 | goto not_control_client; |
263 | 0 | av = args_first_value(args, 'B'); |
264 | 0 | while (av != NULL) { |
265 | 0 | cmd_refresh_client_update_subscription(tc, av->string); |
266 | 0 | av = args_next_value(av); |
267 | 0 | } |
268 | 0 | return (CMD_RETURN_NORMAL); |
269 | 0 | } |
270 | 0 | if (args_has(args, 'C')) { |
271 | 0 | if (~tc->flags & CLIENT_CONTROL) |
272 | 0 | goto not_control_client; |
273 | 0 | return (cmd_refresh_client_control_client_size(self, item)); |
274 | 0 | } |
275 | | |
276 | 0 | if (args_has(args, 'S')) { |
277 | 0 | tc->flags |= CLIENT_STATUSFORCE; |
278 | 0 | server_status_client(tc); |
279 | 0 | } else { |
280 | 0 | tc->flags |= CLIENT_STATUSFORCE; |
281 | 0 | server_redraw_client(tc); |
282 | 0 | } |
283 | 0 | return (CMD_RETURN_NORMAL); |
284 | | |
285 | 0 | not_control_client: |
286 | 0 | cmdq_error(item, "not a control client"); |
287 | 0 | return (CMD_RETURN_ERROR); |
288 | 0 | } |