xref: /netbsd-src/external/bsd/tmux/dist/cmd-refresh-client.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv 
3698d5317Sjmmv /*
4ed4e6cd4Schristos  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv  *
6698d5317Sjmmv  * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv  * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv  * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv  *
10698d5317Sjmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv  */
18698d5317Sjmmv 
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv 
21ef36e747Schristos #include <stdlib.h>
226483eba0Schristos #include <string.h>
23ef36e747Schristos 
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv 
26698d5317Sjmmv /*
27698d5317Sjmmv  * Refresh client.
28698d5317Sjmmv  */
29698d5317Sjmmv 
304e179ddaSchristos static enum cmd_retval	cmd_refresh_client_exec(struct cmd *,
314e179ddaSchristos 			    struct cmdq_item *);
32698d5317Sjmmv 
33698d5317Sjmmv const struct cmd_entry cmd_refresh_client_entry = {
34ed4e6cd4Schristos 	.name = "refresh-client",
35ed4e6cd4Schristos 	.alias = "refresh",
36ed4e6cd4Schristos 
37*f8cf1a91Swiz 	.args = { "A:B:cC:Df:r:F:l::LRSt:U", 0, 1, NULL },
389fb66d81Schristos 	.usage = "[-cDlLRSU] [-A pane:state] [-B name:what:format] "
39*f8cf1a91Swiz 		 "[-C XxY] [-f flags] [-r pane:report]" CMD_TARGET_CLIENT_USAGE
40*f8cf1a91Swiz 		 " [adjustment]",
41ed4e6cd4Schristos 
429fb66d81Schristos 	.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
43ed4e6cd4Schristos 	.exec = cmd_refresh_client_exec
44698d5317Sjmmv };
45698d5317Sjmmv 
469fb66d81Schristos static void
479fb66d81Schristos cmd_refresh_client_update_subscription(struct client *tc, const char *value)
489fb66d81Schristos {
499fb66d81Schristos 	char			*copy, *split, *name, *what;
509fb66d81Schristos 	enum control_sub_type	 subtype;
519fb66d81Schristos 	int			 subid = -1;
529fb66d81Schristos 
539fb66d81Schristos 	copy = name = xstrdup(value);
549fb66d81Schristos 	if ((split = strchr(copy, ':')) == NULL) {
559fb66d81Schristos 		control_remove_sub(tc, copy);
569fb66d81Schristos 		goto out;
579fb66d81Schristos 	}
589fb66d81Schristos 	*split++ = '\0';
599fb66d81Schristos 
609fb66d81Schristos 	what = split;
619fb66d81Schristos 	if ((split = strchr(what, ':')) == NULL)
629fb66d81Schristos 		goto out;
639fb66d81Schristos 	*split++ = '\0';
649fb66d81Schristos 
659fb66d81Schristos 	if (strcmp(what, "%*") == 0)
669fb66d81Schristos 		subtype = CONTROL_SUB_ALL_PANES;
679fb66d81Schristos 	else if (sscanf(what, "%%%d", &subid) == 1 && subid >= 0)
689fb66d81Schristos 		subtype = CONTROL_SUB_PANE;
699fb66d81Schristos 	else if (strcmp(what, "@*") == 0)
709fb66d81Schristos 		subtype = CONTROL_SUB_ALL_WINDOWS;
719fb66d81Schristos 	else if (sscanf(what, "@%d", &subid) == 1 && subid >= 0)
729fb66d81Schristos 		subtype = CONTROL_SUB_WINDOW;
739fb66d81Schristos 	else
749fb66d81Schristos 		subtype = CONTROL_SUB_SESSION;
759fb66d81Schristos 	control_add_sub(tc, name, subtype, subid, split);
769fb66d81Schristos 
779fb66d81Schristos out:
789fb66d81Schristos 	free(copy);
799fb66d81Schristos }
809fb66d81Schristos 
816db26757Swiz static enum cmd_retval
826db26757Swiz cmd_refresh_client_control_client_size(struct cmd *self, struct cmdq_item *item)
836db26757Swiz {
846db26757Swiz 	struct args		*args = cmd_get_args(self);
856db26757Swiz 	struct client		*tc = cmdq_get_target_client(item);
866db26757Swiz 	const char		*size = args_get(args, 'C');
876db26757Swiz 	u_int			 w, x, y;
886db26757Swiz 	struct client_window	*cw;
896db26757Swiz 
906db26757Swiz 	if (sscanf(size, "@%u:%ux%u", &w, &x, &y) == 3) {
916db26757Swiz 		if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM ||
926db26757Swiz 		    y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) {
936db26757Swiz 			cmdq_error(item, "size too small or too big");
946db26757Swiz 			return (CMD_RETURN_ERROR);
956db26757Swiz 		}
966db26757Swiz 		log_debug("%s: client %s window @%u: size %ux%u", __func__,
976db26757Swiz 		    tc->name, w, x, y);
986db26757Swiz 		cw = server_client_add_client_window(tc, w);
996db26757Swiz 		cw->sx = x;
1006db26757Swiz 		cw->sy = y;
1016db26757Swiz 		tc->flags |= CLIENT_WINDOWSIZECHANGED;
1026db26757Swiz 		recalculate_sizes_now(1);
1036db26757Swiz 		return (CMD_RETURN_NORMAL);
1046db26757Swiz 	}
1056db26757Swiz 	if (sscanf(size, "@%u:", &w) == 1) {
1066db26757Swiz 		cw = server_client_get_client_window(tc, w);
1076db26757Swiz 		if (cw != NULL) {
1086db26757Swiz 			log_debug("%s: client %s window @%u: no size", __func__,
1096db26757Swiz 			    tc->name, w);
1106db26757Swiz 			cw->sx = 0;
1116db26757Swiz 			cw->sy = 0;
1126db26757Swiz 			recalculate_sizes_now(1);
1136db26757Swiz 		}
1146db26757Swiz 		return (CMD_RETURN_NORMAL);
1156db26757Swiz 	}
1166db26757Swiz 
1176db26757Swiz 	if (sscanf(size, "%u,%u", &x, &y) != 2 &&
1186db26757Swiz 	    sscanf(size, "%ux%u", &x, &y) != 2) {
1196db26757Swiz 		cmdq_error(item, "bad size argument");
1206db26757Swiz 		return (CMD_RETURN_ERROR);
1216db26757Swiz 	}
1226db26757Swiz 	if (x < WINDOW_MINIMUM || x > WINDOW_MAXIMUM ||
1236db26757Swiz 	    y < WINDOW_MINIMUM || y > WINDOW_MAXIMUM) {
1246db26757Swiz 		cmdq_error(item, "size too small or too big");
1256db26757Swiz 		return (CMD_RETURN_ERROR);
1266db26757Swiz 	}
1276db26757Swiz 	tty_set_size(&tc->tty, x, y, 0, 0);
1286db26757Swiz 	tc->flags |= CLIENT_SIZECHANGED;
1296db26757Swiz 	recalculate_sizes_now(1);
1306db26757Swiz 	return (CMD_RETURN_NORMAL);
1316db26757Swiz }
1326db26757Swiz 
1339fb66d81Schristos static void
1349fb66d81Schristos cmd_refresh_client_update_offset(struct client *tc, const char *value)
1359fb66d81Schristos {
1369fb66d81Schristos 	struct window_pane	*wp;
1379fb66d81Schristos 	char			*copy, *split;
1389fb66d81Schristos 	u_int			 pane;
1399fb66d81Schristos 
1409fb66d81Schristos 	if (*value != '%')
1419fb66d81Schristos 		return;
1429fb66d81Schristos 	copy = xstrdup(value);
1439fb66d81Schristos 	if ((split = strchr(copy, ':')) == NULL)
1449fb66d81Schristos 		goto out;
1459fb66d81Schristos 	*split++ = '\0';
1469fb66d81Schristos 
1479fb66d81Schristos 	if (sscanf(copy, "%%%u", &pane) != 1)
1489fb66d81Schristos 		goto out;
1499fb66d81Schristos 	wp = window_pane_find_by_id(pane);
1509fb66d81Schristos 	if (wp == NULL)
1519fb66d81Schristos 		goto out;
1529fb66d81Schristos 
1539fb66d81Schristos 	if (strcmp(split, "on") == 0)
1549fb66d81Schristos 		control_set_pane_on(tc, wp);
1559fb66d81Schristos 	else if (strcmp(split, "off") == 0)
1569fb66d81Schristos 		control_set_pane_off(tc, wp);
1579fb66d81Schristos 	else if (strcmp(split, "continue") == 0)
1589fb66d81Schristos 		control_continue_pane(tc, wp);
1599fb66d81Schristos 	else if (strcmp(split, "pause") == 0)
1609fb66d81Schristos 		control_pause_pane(tc, wp);
1619fb66d81Schristos 
1629fb66d81Schristos out:
1639fb66d81Schristos 	free(copy);
1649fb66d81Schristos }
1659fb66d81Schristos 
1664e179ddaSchristos static enum cmd_retval
1676db26757Swiz cmd_refresh_client_clipboard(struct cmd *self, struct cmdq_item *item)
1686db26757Swiz {
1696db26757Swiz 	struct args		*args = cmd_get_args(self);
1706db26757Swiz 	struct client		*tc = cmdq_get_target_client(item);
1716db26757Swiz 	const char		*p;
1726db26757Swiz 	u_int			 i;
1736db26757Swiz 	struct cmd_find_state	 fs;
1746db26757Swiz 
1756db26757Swiz 	p = args_get(args, 'l');
1766db26757Swiz 	if (p == NULL) {
1776db26757Swiz 		if (tc->flags & CLIENT_CLIPBOARDBUFFER)
1786db26757Swiz 			return (CMD_RETURN_NORMAL);
1796db26757Swiz 		tc->flags |= CLIENT_CLIPBOARDBUFFER;
1806db26757Swiz 	} else {
1816db26757Swiz 		if (cmd_find_target(&fs, item, p, CMD_FIND_PANE, 0) != 0)
1826db26757Swiz 			return (CMD_RETURN_ERROR);
1836db26757Swiz 		for (i = 0; i < tc->clipboard_npanes; i++) {
1846db26757Swiz 			if (tc->clipboard_panes[i] == fs.wp->id)
1856db26757Swiz 				break;
1866db26757Swiz 		}
1876db26757Swiz 		if (i != tc->clipboard_npanes)
1886db26757Swiz 			return (CMD_RETURN_NORMAL);
1896db26757Swiz 		tc->clipboard_panes = xreallocarray(tc->clipboard_panes,
1906db26757Swiz 		    tc->clipboard_npanes + 1, sizeof *tc->clipboard_panes);
1916db26757Swiz 		tc->clipboard_panes[tc->clipboard_npanes++] = fs.wp->id;
1926db26757Swiz 	}
1936db26757Swiz 	tty_clipboard_query(&tc->tty);
1946db26757Swiz 	return (CMD_RETURN_NORMAL);
1956db26757Swiz }
1966db26757Swiz 
197*f8cf1a91Swiz static void
198*f8cf1a91Swiz cmd_refresh_report(struct tty *tty, const char *value)
199*f8cf1a91Swiz {
200*f8cf1a91Swiz 	struct window_pane	*wp;
201*f8cf1a91Swiz 	u_int			 pane;
202*f8cf1a91Swiz 	size_t			 size = 0;
203*f8cf1a91Swiz 	char			*copy, *split;
204*f8cf1a91Swiz 
205*f8cf1a91Swiz 	if (*value != '%')
206*f8cf1a91Swiz 		return;
207*f8cf1a91Swiz 	copy = xstrdup(value);
208*f8cf1a91Swiz 	if ((split = strchr(copy, ':')) == NULL)
209*f8cf1a91Swiz 		goto out;
210*f8cf1a91Swiz 	*split++ = '\0';
211*f8cf1a91Swiz 
212*f8cf1a91Swiz 	if (sscanf(copy, "%%%u", &pane) != 1)
213*f8cf1a91Swiz 		goto out;
214*f8cf1a91Swiz 	wp = window_pane_find_by_id(pane);
215*f8cf1a91Swiz 	if (wp == NULL)
216*f8cf1a91Swiz 		goto out;
217*f8cf1a91Swiz 
218*f8cf1a91Swiz 	tty_keys_colours(tty, split, strlen(split), &size, &wp->control_fg,
219*f8cf1a91Swiz 	    &wp->control_bg);
220*f8cf1a91Swiz 
221*f8cf1a91Swiz out:
222*f8cf1a91Swiz 	free(copy);
223*f8cf1a91Swiz }
224*f8cf1a91Swiz 
2256db26757Swiz static enum cmd_retval
2264e179ddaSchristos cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item)
227698d5317Sjmmv {
2289fb66d81Schristos 	struct args		*args = cmd_get_args(self);
2299fb66d81Schristos 	struct client		*tc = cmdq_get_target_client(item);
2309fb66d81Schristos 	struct tty		*tty = &tc->tty;
231ef36e747Schristos 	struct window		*w;
2326db26757Swiz 	const char		*errstr;
2336db26757Swiz 	u_int			 adjust;
2349fb66d81Schristos 	struct args_value	*av;
235c9ad075bSchristos 
236ef36e747Schristos 	if (args_has(args, 'c') ||
237ef36e747Schristos 	    args_has(args, 'L') ||
238ef36e747Schristos 	    args_has(args, 'R') ||
239ef36e747Schristos 	    args_has(args, 'U') ||
240ef36e747Schristos 	    args_has(args, 'D'))
241ef36e747Schristos 	{
2426db26757Swiz 		if (args_count(args) == 0)
243ef36e747Schristos 			adjust = 1;
244ef36e747Schristos 		else {
2456db26757Swiz 			adjust = strtonum(args_string(args, 0), 1, INT_MAX,
2466db26757Swiz 			    &errstr);
247ef36e747Schristos 			if (errstr != NULL) {
248ef36e747Schristos 				cmdq_error(item, "adjustment %s", errstr);
249ef36e747Schristos 				return (CMD_RETURN_ERROR);
250ef36e747Schristos 			}
251ef36e747Schristos 		}
252ef36e747Schristos 
253ef36e747Schristos 		if (args_has(args, 'c'))
2549fb66d81Schristos 			tc->pan_window = NULL;
255ef36e747Schristos 		else {
2569fb66d81Schristos 			w = tc->session->curw->window;
2579fb66d81Schristos 			if (tc->pan_window != w) {
2589fb66d81Schristos 				tc->pan_window = w;
2599fb66d81Schristos 				tc->pan_ox = tty->oox;
2609fb66d81Schristos 				tc->pan_oy = tty->ooy;
261ef36e747Schristos 			}
262ef36e747Schristos 			if (args_has(args, 'L')) {
2639fb66d81Schristos 				if (tc->pan_ox > adjust)
2649fb66d81Schristos 					tc->pan_ox -= adjust;
265ef36e747Schristos 				else
2669fb66d81Schristos 					tc->pan_ox = 0;
267ef36e747Schristos 			} else if (args_has(args, 'R')) {
2689fb66d81Schristos 				tc->pan_ox += adjust;
2699fb66d81Schristos 				if (tc->pan_ox > w->sx - tty->osx)
2709fb66d81Schristos 					tc->pan_ox = w->sx - tty->osx;
271ef36e747Schristos 			} else if (args_has(args, 'U')) {
2729fb66d81Schristos 				if (tc->pan_oy > adjust)
2739fb66d81Schristos 					tc->pan_oy -= adjust;
274ef36e747Schristos 				else
2759fb66d81Schristos 					tc->pan_oy = 0;
276ef36e747Schristos 			} else if (args_has(args, 'D')) {
2779fb66d81Schristos 				tc->pan_oy += adjust;
2789fb66d81Schristos 				if (tc->pan_oy > w->sy - tty->osy)
2799fb66d81Schristos 					tc->pan_oy = w->sy - tty->osy;
280ef36e747Schristos 			}
281ef36e747Schristos 		}
2829fb66d81Schristos 		tty_update_client_offset(tc);
2839fb66d81Schristos 		server_redraw_client(tc);
284ef36e747Schristos 		return (CMD_RETURN_NORMAL);
285ef36e747Schristos 	}
286ef36e747Schristos 
2876db26757Swiz 	if (args_has(args, 'l'))
2886db26757Swiz 		return (cmd_refresh_client_clipboard(self, item));
2896483eba0Schristos 
2909fb66d81Schristos 	if (args_has(args, 'F')) /* -F is an alias for -f */
2919fb66d81Schristos 		server_client_set_flags(tc, args_get(args, 'F'));
2929fb66d81Schristos 	if (args_has(args, 'f'))
2939fb66d81Schristos 		server_client_set_flags(tc, args_get(args, 'f'));
294*f8cf1a91Swiz 	if (args_has(args, 'r'))
295*f8cf1a91Swiz 		cmd_refresh_report(tty, args_get(args, 'r'));
2969fb66d81Schristos 
2979fb66d81Schristos 	if (args_has(args, 'A')) {
2989fb66d81Schristos 		if (~tc->flags & CLIENT_CONTROL)
2999fb66d81Schristos 			goto not_control_client;
3006db26757Swiz 		av = args_first_value(args, 'A');
3016db26757Swiz 		while (av != NULL) {
3026db26757Swiz 			cmd_refresh_client_update_offset(tc, av->string);
3036db26757Swiz 			av = args_next_value(av);
304928fc495Schristos 		}
3059fb66d81Schristos 		return (CMD_RETURN_NORMAL);
3069fb66d81Schristos 	}
3079fb66d81Schristos 	if (args_has(args, 'B')) {
3089fb66d81Schristos 		if (~tc->flags & CLIENT_CONTROL)
3099fb66d81Schristos 			goto not_control_client;
3106db26757Swiz 		av = args_first_value(args, 'B');
3116db26757Swiz 		while (av != NULL) {
3126db26757Swiz 			cmd_refresh_client_update_subscription(tc, av->string);
3136db26757Swiz 			av = args_next_value(av);
3149fb66d81Schristos 		}
3159fb66d81Schristos 		return (CMD_RETURN_NORMAL);
3169fb66d81Schristos 	}
3179fb66d81Schristos 	if (args_has(args, 'C')) {
3189fb66d81Schristos 		if (~tc->flags & CLIENT_CONTROL)
3199fb66d81Schristos 			goto not_control_client;
3206db26757Swiz 		return (cmd_refresh_client_control_client_size(self, item));
321ef36e747Schristos 	}
322ef36e747Schristos 
323ef36e747Schristos 	if (args_has(args, 'S')) {
3249fb66d81Schristos 		tc->flags |= CLIENT_STATUSFORCE;
3259fb66d81Schristos 		server_status_client(tc);
3265494e770Schristos 	} else {
3279fb66d81Schristos 		tc->flags |= CLIENT_STATUSFORCE;
3289fb66d81Schristos 		server_redraw_client(tc);
3295494e770Schristos 	}
330928fc495Schristos 	return (CMD_RETURN_NORMAL);
3319fb66d81Schristos 
3329fb66d81Schristos not_control_client:
3339fb66d81Schristos 	cmdq_error(item, "not a control client");
3349fb66d81Schristos 	return (CMD_RETURN_ERROR);
335698d5317Sjmmv }
336