xref: /openbsd-src/usr.bin/tmux/cmd-switch-client.c (revision 823b6d6db0ffd5d96b269d4d060f7f6c4d950da6)
1 /* $OpenBSD: cmd-switch-client.c,v 1.64 2020/04/13 14:46:04 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  * Switch client to a different session.
28  */
29 
30 static enum cmd_retval	cmd_switch_client_exec(struct cmd *,
31 			    struct cmdq_item *);
32 
33 const struct cmd_entry cmd_switch_client_entry = {
34 	.name = "switch-client",
35 	.alias = "switchc",
36 
37 	.args = { "lc:Enpt:rT:Z", 0, 0 },
38 	.usage = "[-ElnprZ] [-c target-client] [-t target-session] "
39 		 "[-T key-table]",
40 
41 	/* -t is special */
42 
43 	.flags = CMD_READONLY,
44 	.exec = cmd_switch_client_exec
45 };
46 
47 static enum cmd_retval
48 cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
49 {
50 	struct args		*args = cmd_get_args(self);
51 	struct cmd_find_state	*current = cmdq_get_current(item);
52 	struct cmd_find_state	 target;
53 	const char		*tflag = args_get(args, 't');
54 	enum cmd_find_type	 type;
55 	int			 flags;
56 	struct client		*c;
57 	struct session		*s;
58 	struct winlink		*wl;
59 	struct window		*w;
60 	struct window_pane	*wp;
61 	const char		*tablename;
62 	struct key_table	*table;
63 
64 	if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
65 		return (CMD_RETURN_ERROR);
66 
67 	if (tflag != NULL && tflag[strcspn(tflag, ":.%")] != '\0') {
68 		type = CMD_FIND_PANE;
69 		flags = 0;
70 	} else {
71 		type = CMD_FIND_SESSION;
72 		flags = CMD_FIND_PREFER_UNATTACHED;
73 	}
74 	if (cmd_find_target(&target, item, tflag, type, flags) != 0)
75 		return (CMD_RETURN_ERROR);
76 	s = target.s;
77 	wl = target.wl;
78 	wp = target.wp;
79 
80 	if (args_has(args, 'r'))
81 		c->flags ^= CLIENT_READONLY;
82 
83 	tablename = args_get(args, 'T');
84 	if (tablename != NULL) {
85 		table = key_bindings_get_table(tablename, 0);
86 		if (table == NULL) {
87 			cmdq_error(item, "table %s doesn't exist", tablename);
88 			return (CMD_RETURN_ERROR);
89 		}
90 		table->references++;
91 		key_bindings_unref_table(c->keytable);
92 		c->keytable = table;
93 		return (CMD_RETURN_NORMAL);
94 	}
95 
96 	if (args_has(args, 'n')) {
97 		if ((s = session_next_session(c->session)) == NULL) {
98 			cmdq_error(item, "can't find next session");
99 			return (CMD_RETURN_ERROR);
100 		}
101 	} else if (args_has(args, 'p')) {
102 		if ((s = session_previous_session(c->session)) == NULL) {
103 			cmdq_error(item, "can't find previous session");
104 			return (CMD_RETURN_ERROR);
105 		}
106 	} else if (args_has(args, 'l')) {
107 		if (c->last_session != NULL && session_alive(c->last_session))
108 			s = c->last_session;
109 		else
110 			s = NULL;
111 		if (s == NULL) {
112 			cmdq_error(item, "can't find last session");
113 			return (CMD_RETURN_ERROR);
114 		}
115 	} else {
116 		if (cmdq_get_client(item) == NULL)
117 			return (CMD_RETURN_NORMAL);
118 		if (wl != NULL && wp != NULL) {
119 			w = wl->window;
120 			if (window_push_zoom(w, args_has(args, 'Z')))
121 				server_redraw_window(w);
122 			window_redraw_active_switch(w, wp);
123 			window_set_active_pane(w, wp, 1);
124 			if (window_pop_zoom(w))
125 				server_redraw_window(w);
126 		}
127 		if (wl != NULL) {
128 			session_set_current(s, wl);
129 			cmd_find_from_session(current, s, 0);
130 		}
131 	}
132 
133 	if (!args_has(args, 'E'))
134 		environ_update(s->options, c->environ, s->environ);
135 
136 	if (c->session != NULL && c->session != s)
137 		c->last_session = c->session;
138 	c->session = s;
139 	if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
140 		server_client_set_key_table(c, NULL);
141 	tty_update_client_offset(c);
142 	status_timer_start(c);
143 	notify_client("client-session-changed", c);
144 	session_update_activity(s, NULL);
145 	gettimeofday(&s->last_attached_time, NULL);
146 
147 	server_check_unattached();
148 	server_redraw_client(c);
149 	s->curw->flags &= ~WINLINK_ALERTFLAGS;
150 	s->curw->window->latest = c;
151 	recalculate_sizes();
152 	alerts_check_session(s);
153 
154 	return (CMD_RETURN_NORMAL);
155 }
156