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