1 /* $OpenBSD: cmd-switch-client.c,v 1.62 2020/04/13 10:59:58 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 cmdq_shared *shared = cmdq_get_shared(item); 52 struct cmd_find_state *current = &shared->current; 53 struct cmd_find_state target; 54 const char *tflag = args_get(args, 't'); 55 enum cmd_find_type type; 56 int flags; 57 struct client *c; 58 struct session *s; 59 struct winlink *wl; 60 struct window *w; 61 struct window_pane *wp; 62 const char *tablename; 63 struct key_table *table; 64 65 if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL) 66 return (CMD_RETURN_ERROR); 67 68 if (tflag != NULL && tflag[strcspn(tflag, ":.%")] != '\0') { 69 type = CMD_FIND_PANE; 70 flags = 0; 71 } else { 72 type = CMD_FIND_SESSION; 73 flags = CMD_FIND_PREFER_UNATTACHED; 74 } 75 if (cmd_find_target(&target, item, tflag, type, flags) != 0) 76 return (CMD_RETURN_ERROR); 77 s = target.s; 78 wl = target.wl; 79 wp = target.wp; 80 81 if (args_has(args, 'r')) 82 c->flags ^= CLIENT_READONLY; 83 84 tablename = args_get(args, 'T'); 85 if (tablename != NULL) { 86 table = key_bindings_get_table(tablename, 0); 87 if (table == NULL) { 88 cmdq_error(item, "table %s doesn't exist", tablename); 89 return (CMD_RETURN_ERROR); 90 } 91 table->references++; 92 key_bindings_unref_table(c->keytable); 93 c->keytable = table; 94 return (CMD_RETURN_NORMAL); 95 } 96 97 if (args_has(args, 'n')) { 98 if ((s = session_next_session(c->session)) == NULL) { 99 cmdq_error(item, "can't find next session"); 100 return (CMD_RETURN_ERROR); 101 } 102 } else if (args_has(args, 'p')) { 103 if ((s = session_previous_session(c->session)) == NULL) { 104 cmdq_error(item, "can't find previous session"); 105 return (CMD_RETURN_ERROR); 106 } 107 } else if (args_has(args, 'l')) { 108 if (c->last_session != NULL && session_alive(c->last_session)) 109 s = c->last_session; 110 else 111 s = NULL; 112 if (s == NULL) { 113 cmdq_error(item, "can't find last session"); 114 return (CMD_RETURN_ERROR); 115 } 116 } else { 117 if (cmdq_get_client(item) == NULL) 118 return (CMD_RETURN_NORMAL); 119 if (wl != NULL && wp != NULL) { 120 w = wl->window; 121 if (window_push_zoom(w, args_has(args, 'Z'))) 122 server_redraw_window(w); 123 window_redraw_active_switch(w, wp); 124 window_set_active_pane(w, wp, 1); 125 if (window_pop_zoom(w)) 126 server_redraw_window(w); 127 } 128 if (wl != NULL) { 129 session_set_current(s, wl); 130 cmd_find_from_session(current, s, 0); 131 } 132 } 133 134 if (!args_has(args, 'E')) 135 environ_update(s->options, c->environ, s->environ); 136 137 if (c->session != NULL && c->session != s) 138 c->last_session = c->session; 139 c->session = s; 140 if (~shared->flags & CMDQ_SHARED_REPEAT) 141 server_client_set_key_table(c, NULL); 142 tty_update_client_offset(c); 143 status_timer_start(c); 144 notify_client("client-session-changed", c); 145 session_update_activity(s, NULL); 146 gettimeofday(&s->last_attached_time, NULL); 147 148 server_check_unattached(); 149 server_redraw_client(c); 150 s->curw->flags &= ~WINLINK_ALERTFLAGS; 151 s->curw->window->latest = c; 152 recalculate_sizes(); 153 alerts_check_session(s); 154 155 return (CMD_RETURN_NORMAL); 156 } 157