/src/tmux/cmd-refresh-client.c
Line | Count | Source |
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: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 *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 void |
167 | | cmd_refresh_report(struct tty *tty, const char *value) |
168 | 0 | { |
169 | 0 | struct window_pane *wp; |
170 | 0 | u_int pane; |
171 | 0 | size_t size = 0; |
172 | 0 | char *copy, *split; |
173 | |
|
174 | 0 | if (*value != '%') |
175 | 0 | return; |
176 | 0 | copy = xstrdup(value); |
177 | 0 | if ((split = strchr(copy, ':')) == NULL) |
178 | 0 | goto out; |
179 | 0 | *split++ = '\0'; |
180 | |
|
181 | 0 | if (sscanf(copy, "%%%u", &pane) != 1) |
182 | 0 | goto out; |
183 | 0 | wp = window_pane_find_by_id(pane); |
184 | 0 | if (wp == NULL) |
185 | 0 | goto out; |
186 | | |
187 | 0 | tty_keys_colours(tty, split, strlen(split), &size, &wp->control_fg, |
188 | 0 | &wp->control_bg); |
189 | |
|
190 | 0 | out: |
191 | 0 | free(copy); |
192 | 0 | } |
193 | | |
194 | | static enum cmd_retval |
195 | | cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item) |
196 | 0 | { |
197 | 0 | struct args *args = cmd_get_args(self); |
198 | 0 | struct client *tc = cmdq_get_target_client(item); |
199 | 0 | struct tty *tty = &tc->tty; |
200 | 0 | struct window *w; |
201 | 0 | const char *errstr; |
202 | 0 | u_int adjust; |
203 | 0 | struct args_value *av; |
204 | |
|
205 | 0 | if (args_has(args, 'c') || |
206 | 0 | args_has(args, 'L') || |
207 | 0 | args_has(args, 'R') || |
208 | 0 | args_has(args, 'U') || |
209 | 0 | args_has(args, 'D')) |
210 | 0 | { |
211 | 0 | if (args_count(args) == 0) |
212 | 0 | adjust = 1; |
213 | 0 | else { |
214 | 0 | adjust = strtonum(args_string(args, 0), 1, INT_MAX, |
215 | 0 | &errstr); |
216 | 0 | if (errstr != NULL) { |
217 | 0 | cmdq_error(item, "adjustment %s", errstr); |
218 | 0 | return (CMD_RETURN_ERROR); |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | if (args_has(args, 'c')) |
223 | 0 | tc->pan_window = NULL; |
224 | 0 | else { |
225 | 0 | w = tc->session->curw->window; |
226 | 0 | if (tc->pan_window != w) { |
227 | 0 | tc->pan_window = w; |
228 | 0 | tc->pan_ox = tty->oox; |
229 | 0 | tc->pan_oy = tty->ooy; |
230 | 0 | } |
231 | 0 | if (args_has(args, 'L')) { |
232 | 0 | if (tc->pan_ox > adjust) |
233 | 0 | tc->pan_ox -= adjust; |
234 | 0 | else |
235 | 0 | tc->pan_ox = 0; |
236 | 0 | } else if (args_has(args, 'R')) { |
237 | 0 | tc->pan_ox += adjust; |
238 | 0 | if (tc->pan_ox > w->sx - tty->osx) |
239 | 0 | tc->pan_ox = w->sx - tty->osx; |
240 | 0 | } else if (args_has(args, 'U')) { |
241 | 0 | if (tc->pan_oy > adjust) |
242 | 0 | tc->pan_oy -= adjust; |
243 | 0 | else |
244 | 0 | tc->pan_oy = 0; |
245 | 0 | } else if (args_has(args, 'D')) { |
246 | 0 | tc->pan_oy += adjust; |
247 | 0 | if (tc->pan_oy > w->sy - tty->osy) |
248 | 0 | tc->pan_oy = w->sy - tty->osy; |
249 | 0 | } |
250 | 0 | } |
251 | 0 | tty_update_client_offset(tc); |
252 | 0 | server_redraw_client(tc); |
253 | 0 | return (CMD_RETURN_NORMAL); |
254 | 0 | } |
255 | | |
256 | 0 | if (args_has(args, 'l')) { |
257 | 0 | tty_clipboard_query(&tc->tty); |
258 | 0 | return (CMD_RETURN_NORMAL); |
259 | 0 | } |
260 | | |
261 | 0 | if (args_has(args, 'F')) /* -F is an alias for -f */ |
262 | 0 | server_client_set_flags(tc, args_get(args, 'F')); |
263 | 0 | if (args_has(args, 'f')) |
264 | 0 | server_client_set_flags(tc, args_get(args, 'f')); |
265 | 0 | if (args_has(args, 'r')) |
266 | 0 | cmd_refresh_report(tty, args_get(args, 'r')); |
267 | |
|
268 | 0 | if (args_has(args, 'A')) { |
269 | 0 | if (~tc->flags & CLIENT_CONTROL) |
270 | 0 | goto not_control_client; |
271 | 0 | av = args_first_value(args, 'A'); |
272 | 0 | while (av != NULL) { |
273 | 0 | cmd_refresh_client_update_offset(tc, av->string); |
274 | 0 | av = args_next_value(av); |
275 | 0 | } |
276 | 0 | return (CMD_RETURN_NORMAL); |
277 | 0 | } |
278 | 0 | if (args_has(args, 'B')) { |
279 | 0 | if (~tc->flags & CLIENT_CONTROL) |
280 | 0 | goto not_control_client; |
281 | 0 | av = args_first_value(args, 'B'); |
282 | 0 | while (av != NULL) { |
283 | 0 | cmd_refresh_client_update_subscription(tc, av->string); |
284 | 0 | av = args_next_value(av); |
285 | 0 | } |
286 | 0 | return (CMD_RETURN_NORMAL); |
287 | 0 | } |
288 | 0 | if (args_has(args, 'C')) { |
289 | 0 | if (~tc->flags & CLIENT_CONTROL) |
290 | 0 | goto not_control_client; |
291 | 0 | return (cmd_refresh_client_control_client_size(self, item)); |
292 | 0 | } |
293 | | |
294 | 0 | if (args_has(args, 'S')) { |
295 | 0 | tc->flags |= CLIENT_STATUSFORCE; |
296 | 0 | server_status_client(tc); |
297 | 0 | } else { |
298 | 0 | tc->flags |= CLIENT_STATUSFORCE; |
299 | 0 | server_redraw_client(tc); |
300 | 0 | } |
301 | 0 | return (CMD_RETURN_NORMAL); |
302 | | |
303 | 0 | not_control_client: |
304 | 0 | cmdq_error(item, "not a control client"); |
305 | 0 | return (CMD_RETURN_ERROR); |
306 | 0 | } |