[BACK]Return to cmd-switch-client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-switch-client.c, Revision 1.66

1.66    ! nicm        1: /* $OpenBSD: cmd-switch-client.c,v 1.65 2020/04/13 20:51:57 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.66    ! nicm       37:        .args = { "lc:EFnpt:rT:Z", 0, 0 },
1.58      nicm       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:
1.65      nicm       43:        .flags = CMD_READONLY|CMD_CLIENT_CFLAG,
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.61      nicm       50:        struct args             *args = cmd_get_args(self);
1.64      nicm       51:        struct cmd_find_state   *current = cmdq_get_current(item);
1.62      nicm       52:        struct cmd_find_state    target;
1.51      nicm       53:        const char              *tflag = args_get(args, 't');
                     54:        enum cmd_find_type       type;
                     55:        int                      flags;
1.65      nicm       56:        struct client           *tc = cmdq_get_target_client(item);
1.51      nicm       57:        struct session          *s;
                     58:        struct winlink          *wl;
1.58      nicm       59:        struct window           *w;
1.38      nicm       60:        struct window_pane      *wp;
1.46      nicm       61:        const char              *tablename;
1.23      nicm       62:        struct key_table        *table;
1.13      nicm       63:
1.57      nicm       64:        if (tflag != NULL && tflag[strcspn(tflag, ":.%")] != '\0') {
1.51      nicm       65:                type = CMD_FIND_PANE;
                     66:                flags = 0;
                     67:        } else {
                     68:                type = CMD_FIND_SESSION;
                     69:                flags = CMD_FIND_PREFER_UNATTACHED;
                     70:        }
1.62      nicm       71:        if (cmd_find_target(&target, item, tflag, type, flags) != 0)
1.51      nicm       72:                return (CMD_RETURN_ERROR);
1.62      nicm       73:        s = target.s;
                     74:        wl = target.wl;
                     75:        wp = target.wp;
1.51      nicm       76:
1.66    ! nicm       77:        if (args_has(args, 'r')) {
        !            78:                if (tc->flags & CLIENT_READONLY)
        !            79:                        tc->flags &= ~(CLIENT_READONLY|CLIENT_IGNORESIZE);
        !            80:                else
        !            81:                        tc->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
        !            82:        }
1.23      nicm       83:
                     84:        tablename = args_get(args, 'T');
                     85:        if (tablename != NULL) {
                     86:                table = key_bindings_get_table(tablename, 0);
                     87:                if (table == NULL) {
1.44      nicm       88:                        cmdq_error(item, "table %s doesn't exist", tablename);
1.23      nicm       89:                        return (CMD_RETURN_ERROR);
                     90:                }
                     91:                table->references++;
1.65      nicm       92:                key_bindings_unref_table(tc->keytable);
                     93:                tc->keytable = table;
1.36      nicm       94:                return (CMD_RETURN_NORMAL);
1.13      nicm       95:        }
1.6       nicm       96:
1.11      nicm       97:        if (args_has(args, 'n')) {
1.65      nicm       98:                if ((s = session_next_session(tc->session)) == NULL) {
1.44      nicm       99:                        cmdq_error(item, "can't find next session");
1.15      nicm      100:                        return (CMD_RETURN_ERROR);
1.6       nicm      101:                }
1.11      nicm      102:        } else if (args_has(args, 'p')) {
1.65      nicm      103:                if ((s = session_previous_session(tc->session)) == NULL) {
1.44      nicm      104:                        cmdq_error(item, "can't find previous session");
1.15      nicm      105:                        return (CMD_RETURN_ERROR);
1.6       nicm      106:                }
1.11      nicm      107:        } else if (args_has(args, 'l')) {
1.65      nicm      108:                if (tc->last_session != NULL && session_alive(tc->last_session))
                    109:                        s = tc->last_session;
1.38      nicm      110:                else
                    111:                        s = NULL;
1.8       nicm      112:                if (s == NULL) {
1.44      nicm      113:                        cmdq_error(item, "can't find last session");
1.15      nicm      114:                        return (CMD_RETURN_ERROR);
1.8       nicm      115:                }
1.41      nicm      116:        } else {
1.62      nicm      117:                if (cmdq_get_client(item) == NULL)
1.19      nicm      118:                        return (CMD_RETURN_NORMAL);
1.58      nicm      119:                if (wl != NULL && wp != NULL) {
1.60      nicm      120:                        w = wl->window;
1.61      nicm      121:                        if (window_push_zoom(w, args_has(args, 'Z')))
1.58      nicm      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:                }
1.51      nicm      128:                if (wl != NULL) {
                    129:                        session_set_current(s, wl);
1.62      nicm      130:                        cmd_find_from_session(current, s, 0);
1.19      nicm      131:                }
1.25      nicm      132:        }
                    133:
1.46      nicm      134:        if (!args_has(args, 'E'))
1.65      nicm      135:                environ_update(s->options, tc->environ, s->environ);
1.8       nicm      136:
1.65      nicm      137:        if (tc->session != NULL && tc->session != s)
                    138:                tc->last_session = tc->session;
                    139:        tc->session = s;
1.64      nicm      140:        if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
1.65      nicm      141:                server_client_set_key_table(tc, NULL);
                    142:        tty_update_client_offset(tc);
                    143:        status_timer_start(tc);
                    144:        notify_client("client-session-changed", tc);
1.29      nicm      145:        session_update_activity(s, NULL);
1.30      nicm      146:        gettimeofday(&s->last_attached_time, NULL);
1.1       nicm      147:
1.7       nicm      148:        server_check_unattached();
1.65      nicm      149:        server_redraw_client(tc);
1.14      nicm      150:        s->curw->flags &= ~WINLINK_ALERTFLAGS;
1.65      nicm      151:        s->curw->window->latest = tc;
1.59      nicm      152:        recalculate_sizes();
1.35      nicm      153:        alerts_check_session(s);
1.1       nicm      154:
1.15      nicm      155:        return (CMD_RETURN_NORMAL);
1.1       nicm      156: }