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