Annotation of src/usr.bin/tmux/cmd-switch-client.c, Revision 1.60
1.60 ! nicm 1: /* $OpenBSD: cmd-switch-client.c,v 1.59 2020/03/06 15:35:03 nicm Exp $ */
1.1 nicm 2:
3: /*
1.42 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.44 nicm 30: static enum cmd_retval cmd_switch_client_exec(struct cmd *,
31: struct cmdq_item *);
1.1 nicm 32:
33: const struct cmd_entry cmd_switch_client_entry = {
1.39 nicm 34: .name = "switch-client",
35: .alias = "switchc",
36:
1.58 nicm 37: .args = { "lc:Enpt:rT:Z", 0, 0 },
38: .usage = "[-ElnprZ] [-c target-client] [-t target-session] "
1.39 nicm 39: "[-T key-table]",
40:
1.51 nicm 41: /* -t is special */
1.40 nicm 42:
43: .flags = CMD_READONLY,
1.39 nicm 44: .exec = cmd_switch_client_exec
1.1 nicm 45: };
1.6 nicm 46:
1.43 nicm 47: static enum cmd_retval
1.44 nicm 48: cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 49: {
1.19 nicm 50: struct args *args = self->args;
1.51 nicm 51: const char *tflag = args_get(args, 't');
52: enum cmd_find_type type;
53: int flags;
54: struct client *c;
55: struct session *s;
56: struct winlink *wl;
1.58 nicm 57: struct window *w;
1.38 nicm 58: struct window_pane *wp;
1.46 nicm 59: const char *tablename;
1.23 nicm 60: struct key_table *table;
1.13 nicm 61:
1.51 nicm 62: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
63: return (CMD_RETURN_ERROR);
64:
1.57 nicm 65: if (tflag != NULL && tflag[strcspn(tflag, ":.%")] != '\0') {
1.51 nicm 66: type = CMD_FIND_PANE;
67: flags = 0;
68: } else {
69: type = CMD_FIND_SESSION;
70: flags = CMD_FIND_PREFER_UNATTACHED;
71: }
72: if (cmd_find_target(&item->target, item, tflag, type, flags) != 0)
73: return (CMD_RETURN_ERROR);
74: s = item->target.s;
75: wl = item->target.wl;
76: wp = item->target.wp;
77:
1.34 mmcc 78: if (args_has(args, 'r'))
79: c->flags ^= CLIENT_READONLY;
1.23 nicm 80:
81: tablename = args_get(args, 'T');
82: if (tablename != NULL) {
83: table = key_bindings_get_table(tablename, 0);
84: if (table == NULL) {
1.44 nicm 85: cmdq_error(item, "table %s doesn't exist", tablename);
1.23 nicm 86: return (CMD_RETURN_ERROR);
87: }
88: table->references++;
89: key_bindings_unref_table(c->keytable);
90: c->keytable = table;
1.36 nicm 91: return (CMD_RETURN_NORMAL);
1.13 nicm 92: }
1.6 nicm 93:
1.11 nicm 94: if (args_has(args, 'n')) {
1.6 nicm 95: if ((s = session_next_session(c->session)) == NULL) {
1.44 nicm 96: cmdq_error(item, "can't find next session");
1.15 nicm 97: return (CMD_RETURN_ERROR);
1.6 nicm 98: }
1.11 nicm 99: } else if (args_has(args, 'p')) {
1.6 nicm 100: if ((s = session_previous_session(c->session)) == NULL) {
1.44 nicm 101: cmdq_error(item, "can't find previous session");
1.15 nicm 102: return (CMD_RETURN_ERROR);
1.6 nicm 103: }
1.11 nicm 104: } else if (args_has(args, 'l')) {
1.9 nicm 105: if (c->last_session != NULL && session_alive(c->last_session))
106: s = c->last_session;
1.38 nicm 107: else
108: s = NULL;
1.8 nicm 109: if (s == NULL) {
1.44 nicm 110: cmdq_error(item, "can't find last session");
1.15 nicm 111: return (CMD_RETURN_ERROR);
1.8 nicm 112: }
1.41 nicm 113: } else {
1.44 nicm 114: if (item->client == NULL)
1.19 nicm 115: return (CMD_RETURN_NORMAL);
1.58 nicm 116: if (wl != NULL && wp != NULL) {
1.60 ! nicm 117: w = wl->window;
1.58 nicm 118: if (window_push_zoom(w, args_has(self->args, 'Z')))
119: server_redraw_window(w);
120: window_redraw_active_switch(w, wp);
121: window_set_active_pane(w, wp, 1);
122: if (window_pop_zoom(w))
123: server_redraw_window(w);
124: }
1.51 nicm 125: if (wl != NULL) {
126: session_set_current(s, wl);
1.53 nicm 127: cmd_find_from_session(&item->shared->current, s, 0);
1.19 nicm 128: }
1.25 nicm 129: }
130:
1.46 nicm 131: if (!args_has(args, 'E'))
132: environ_update(s->options, c->environ, s->environ);
1.8 nicm 133:
1.31 nicm 134: if (c->session != NULL && c->session != s)
1.9 nicm 135: c->last_session = c->session;
1.1 nicm 136: c->session = s;
1.49 nicm 137: if (~item->shared->flags & CMDQ_SHARED_REPEAT)
1.48 nicm 138: server_client_set_key_table(c, NULL);
1.54 nicm 139: tty_update_client_offset(c);
1.28 nicm 140: status_timer_start(c);
1.52 nicm 141: notify_client("client-session-changed", c);
1.29 nicm 142: session_update_activity(s, NULL);
1.30 nicm 143: gettimeofday(&s->last_attached_time, NULL);
1.1 nicm 144:
1.7 nicm 145: server_check_unattached();
1.1 nicm 146: server_redraw_client(c);
1.14 nicm 147: s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.59 nicm 148: s->curw->window->latest = c;
149: recalculate_sizes();
1.35 nicm 150: alerts_check_session(s);
1.1 nicm 151:
1.15 nicm 152: return (CMD_RETURN_NORMAL);
1.1 nicm 153: }