xref: /openbsd-src/usr.bin/tmux/cmd-switch-client.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: cmd-switch-client.c,v 1.42 2016/01/19 15:59:12 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 enum cmd_retval	 cmd_switch_client_exec(struct cmd *, struct cmd_q *);
31 
32 const struct cmd_entry cmd_switch_client_entry = {
33 	.name = "switch-client",
34 	.alias = "switchc",
35 
36 	.args = { "lc:Enpt:rT:", 0, 0 },
37 	.usage = "[-Elnpr] [-c target-client] [-t target-session] "
38 		 "[-T key-table]",
39 
40 	.cflag = CMD_CLIENT,
41 	.tflag = CMD_SESSION_WITHPANE,
42 
43 	.flags = CMD_READONLY,
44 	.exec = cmd_switch_client_exec
45 };
46 
47 enum cmd_retval
48 cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq)
49 {
50 	struct args		*args = self->args;
51 	struct cmd_state	*state = &cmdq->state;
52 	struct client		*c = state->c;
53 	struct session		*s = cmdq->state.tflag.s;
54 	struct window_pane	*wp;
55 	const char		*tablename, *update;
56 	struct key_table	*table;
57 
58 	if (args_has(args, 'r'))
59 		c->flags ^= CLIENT_READONLY;
60 
61 	tablename = args_get(args, 'T');
62 	if (tablename != NULL) {
63 		table = key_bindings_get_table(tablename, 0);
64 		if (table == NULL) {
65 			cmdq_error(cmdq, "table %s doesn't exist", tablename);
66 			return (CMD_RETURN_ERROR);
67 		}
68 		table->references++;
69 		key_bindings_unref_table(c->keytable);
70 		c->keytable = table;
71 		return (CMD_RETURN_NORMAL);
72 	}
73 
74 	if (args_has(args, 'n')) {
75 		if ((s = session_next_session(c->session)) == NULL) {
76 			cmdq_error(cmdq, "can't find next session");
77 			return (CMD_RETURN_ERROR);
78 		}
79 	} else if (args_has(args, 'p')) {
80 		if ((s = session_previous_session(c->session)) == NULL) {
81 			cmdq_error(cmdq, "can't find previous session");
82 			return (CMD_RETURN_ERROR);
83 		}
84 	} else if (args_has(args, 'l')) {
85 		if (c->last_session != NULL && session_alive(c->last_session))
86 			s = c->last_session;
87 		else
88 			s = NULL;
89 		if (s == NULL) {
90 			cmdq_error(cmdq, "can't find last session");
91 			return (CMD_RETURN_ERROR);
92 		}
93 	} else {
94 		if (cmdq->client == NULL)
95 			return (CMD_RETURN_NORMAL);
96 		if (state->tflag.wl != NULL) {
97 			wp = state->tflag.wp;
98 			if (wp != NULL)
99 				window_set_active_pane(wp->window, wp);
100 			session_set_current(s, state->tflag.wl);
101 		}
102 	}
103 
104 	if (c != NULL && !args_has(args, 'E')) {
105 		update = options_get_string(s->options, "update-environment");
106 		environ_update(update, c->environ, s->environ);
107 	}
108 
109 	if (c->session != NULL && c->session != s)
110 		c->last_session = c->session;
111 	c->session = s;
112 	server_client_set_key_table(c, NULL);
113 	status_timer_start(c);
114 	session_update_activity(s, NULL);
115 	gettimeofday(&s->last_attached_time, NULL);
116 
117 	recalculate_sizes();
118 	server_check_unattached();
119 	server_redraw_client(c);
120 	s->curw->flags &= ~WINLINK_ALERTFLAGS;
121 	alerts_check_session(s);
122 
123 	return (CMD_RETURN_NORMAL);
124 }
125