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

Annotation of src/usr.bin/tmux/cmd-choose-client.c, Revision 1.6

1.6     ! nicm        1: /* $OpenBSD: cmd-choose-client.c,v 1.5 2011/01/04 00:42:46 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      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>
1.2       nicm       20:
                     21: #include <ctype.h>
1.1       nicm       22:
                     23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Enter choice mode to choose a client.
                     27:  */
                     28:
                     29: int    cmd_choose_client_exec(struct cmd *, struct cmd_ctx *);
                     30:
                     31: void   cmd_choose_client_callback(void *, int);
                     32: void   cmd_choose_client_free(void *);
                     33:
                     34: const struct cmd_entry cmd_choose_client_entry = {
                     35:        "choose-client", NULL,
1.5       nicm       36:        "t:", 0, 1,
1.1       nicm       37:        CMD_TARGET_WINDOW_USAGE " [template]",
1.5       nicm       38:        0,
                     39:        NULL,
                     40:        NULL,
                     41:        cmd_choose_client_exec
1.1       nicm       42: };
                     43:
                     44: struct cmd_choose_client_data {
1.3       nicm       45:        struct client   *client;
1.1       nicm       46:        char            *template;
                     47: };
                     48:
                     49: int
                     50: cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
                     51: {
1.5       nicm       52:        struct args                     *args = self->args;
1.1       nicm       53:        struct cmd_choose_client_data   *cdata;
                     54:        struct winlink                  *wl;
                     55:        struct client                   *c;
                     56:        u_int                            i, idx, cur;
                     57:
                     58:        if (ctx->curclient == NULL) {
                     59:                ctx->error(ctx, "must be run interactively");
                     60:                return (-1);
                     61:        }
                     62:
1.5       nicm       63:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.1       nicm       64:                return (-1);
                     65:
                     66:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     67:                return (0);
                     68:
                     69:        cur = idx = 0;
                     70:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     71:                c = ARRAY_ITEM(&clients, i);
                     72:                if (c == NULL || c->session == NULL)
                     73:                        continue;
                     74:                if (c == ctx->curclient)
                     75:                        cur = idx;
                     76:                idx++;
                     77:
                     78:                window_choose_add(wl->window->active, i,
1.6     ! nicm       79:                    "%s: %s [%ux%u %s]%s%s", c->tty.path,
1.1       nicm       80:                    c->session->name, c->tty.sx, c->tty.sy,
1.6     ! nicm       81:                    c->tty.termname,
        !            82:                    c->tty.flags & TTY_UTF8 ? " (utf8)" : "",
        !            83:                    c->flags & CLIENT_READONLY ? " (ro)" : "");
1.1       nicm       84:        }
                     85:
                     86:        cdata = xmalloc(sizeof *cdata);
1.5       nicm       87:        if (args->argc != 0)
                     88:                cdata->template = xstrdup(args->argv[0]);
1.1       nicm       89:        else
                     90:                cdata->template = xstrdup("detach-client -t '%%'");
1.3       nicm       91:        cdata->client = ctx->curclient;
                     92:        cdata->client->references++;
1.1       nicm       93:
                     94:        window_choose_ready(wl->window->active,
                     95:            cur, cmd_choose_client_callback, cmd_choose_client_free, cdata);
                     96:
                     97:        return (0);
                     98: }
                     99:
                    100: void
                    101: cmd_choose_client_callback(void *data, int idx)
                    102: {
                    103:        struct cmd_choose_client_data   *cdata = data;
1.3       nicm      104:        struct client                   *c;
1.1       nicm      105:        struct cmd_list                 *cmdlist;
                    106:        struct cmd_ctx                   ctx;
                    107:        char                            *template, *cause;
                    108:
                    109:        if (idx == -1)
                    110:                return;
1.3       nicm      111:        if (cdata->client->flags & CLIENT_DEAD)
1.1       nicm      112:                return;
                    113:
                    114:        if ((u_int) idx > ARRAY_LENGTH(&clients) - 1)
                    115:                return;
1.3       nicm      116:        c = ARRAY_ITEM(&clients, idx);
                    117:        if (c == NULL || c->session == NULL)
1.1       nicm      118:                return;
1.3       nicm      119:        template = cmd_template_replace(cdata->template, c->tty.path, 1);
1.1       nicm      120:
                    121:        if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
                    122:                if (cause != NULL) {
                    123:                        *cause = toupper((u_char) *cause);
                    124:                        status_message_set(c, "%s", cause);
                    125:                        xfree(cause);
                    126:                }
                    127:                xfree(template);
                    128:                return;
                    129:        }
                    130:        xfree(template);
                    131:
                    132:        ctx.msgdata = NULL;
1.3       nicm      133:        ctx.curclient = cdata->client;
1.1       nicm      134:
                    135:        ctx.error = key_bindings_error;
                    136:        ctx.print = key_bindings_print;
                    137:        ctx.info = key_bindings_info;
                    138:
                    139:        ctx.cmdclient = NULL;
                    140:
                    141:        cmd_list_exec(cmdlist, &ctx);
                    142:        cmd_list_free(cmdlist);
                    143: }
                    144:
                    145: void
                    146: cmd_choose_client_free(void *data)
                    147: {
                    148:        struct cmd_choose_client_data   *cdata = data;
                    149:
1.3       nicm      150:        cdata->client->references--;
1.1       nicm      151:        xfree(cdata->template);
                    152:        xfree(cdata);
                    153: }