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

Annotation of src/usr.bin/tmux/cmd-send-keys.c, Revision 1.74

1.74    ! nicm        1: /* $OpenBSD: cmd-send-keys.c,v 1.73 2022/12/16 08:13:40 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.28      nicm        4:  * Copyright (c) 2008 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>
1.10      nicm       22: #include <string.h>
1.1       nicm       23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Send keys to client.
                     28:  */
                     29:
1.33      nicm       30: static enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       31:
                     32: const struct cmd_entry cmd_send_keys_entry = {
1.25      nicm       33:        .name = "send-keys",
                     34:        .alias = "send",
                     35:
1.73      nicm       36:        .args = { "c:FHKlMN:Rt:X", 0, -1, NULL },
                     37:        .usage = "[-FHKlMRX] [-c target-client] [-N repeat-count] "
                     38:                 CMD_TARGET_PANE_USAGE " key ...",
1.25      nicm       39:
1.39      nicm       40:        .target = { 't', CMD_FIND_PANE, 0 },
1.26      nicm       41:
1.74    ! nicm       42:        .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL,
1.25      nicm       43:        .exec = cmd_send_keys_exec
1.1       nicm       44: };
                     45:
1.14      nicm       46: const struct cmd_entry cmd_send_prefix_entry = {
1.25      nicm       47:        .name = "send-prefix",
                     48:        .alias = NULL,
                     49:
1.68      nicm       50:        .args = { "2t:", 0, 0, NULL },
1.25      nicm       51:        .usage = "[-2] " CMD_TARGET_PANE_USAGE,
                     52:
1.39      nicm       53:        .target = { 't', CMD_FIND_PANE, 0 },
1.26      nicm       54:
1.31      nicm       55:        .flags = CMD_AFTERHOOK,
1.25      nicm       56:        .exec = cmd_send_keys_exec
1.14      nicm       57: };
                     58:
1.47      nicm       59: static struct cmdq_item *
1.60      nicm       60: cmd_send_keys_inject_key(struct cmdq_item *item, struct cmdq_item *after,
1.73      nicm       61:     struct args *args, key_code key)
1.40      nicm       62: {
1.56      nicm       63:        struct cmd_find_state           *target = cmdq_get_target(item);
1.60      nicm       64:        struct client                   *tc = cmdq_get_target_client(item);
                     65:        struct session                  *s = target->s;
                     66:        struct winlink                  *wl = target->wl;
                     67:        struct window_pane              *wp = target->wp;
1.46      nicm       68:        struct window_mode_entry        *wme;
1.73      nicm       69:        struct key_table                *table = NULL;
1.45      nicm       70:        struct key_binding              *bd;
1.73      nicm       71:        struct key_event                *event;
                     72:
                     73:        if (args_has(args, 'K')) {
1.74    ! nicm       74:                if (tc == NULL)
        !            75:                        return (item);
1.73      nicm       76:                event = xmalloc(sizeof *event);
                     77:                event->key = key;
                     78:                memset(&event->m, 0, sizeof event->m);
                     79:                if (server_client_handle_key(tc, event) == 0)
                     80:                        free(event);
                     81:                return (item);
                     82:        }
1.40      nicm       83:
1.60      nicm       84:        wme = TAILQ_FIRST(&wp->modes);
1.45      nicm       85:        if (wme == NULL || wme->mode->key_table == NULL) {
1.63      nicm       86:                if (window_pane_key(wp, tc, s, wl, key, NULL) != 0)
1.52      nicm       87:                        return (NULL);
1.47      nicm       88:                return (item);
1.40      nicm       89:        }
1.45      nicm       90:        table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.40      nicm       91:
1.63      nicm       92:        bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
1.40      nicm       93:        if (bd != NULL) {
                     94:                table->references++;
1.60      nicm       95:                after = key_bindings_dispatch(bd, after, tc, NULL, target);
1.40      nicm       96:                key_bindings_unref_table(table);
                     97:        }
1.60      nicm       98:        return (after);
1.40      nicm       99: }
                    100:
1.49      nicm      101: static struct cmdq_item *
1.60      nicm      102: cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
                    103:     struct args *args, int i)
1.49      nicm      104: {
1.67      nicm      105:        const char              *s = args_string(args, i);
1.64      nicm      106:        struct utf8_data        *ud, *loop;
                    107:        utf8_char                uc;
1.49      nicm      108:        key_code                 key;
                    109:        char                    *endptr;
                    110:        long                     n;
                    111:        int                      literal;
                    112:
                    113:        if (args_has(args, 'H')) {
                    114:                n = strtol(s, &endptr, 16);
                    115:                if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
                    116:                        return (item);
1.73      nicm      117:                return (cmd_send_keys_inject_key(item, after, args,
                    118:                    KEYC_LITERAL|n));
1.49      nicm      119:        }
                    120:
                    121:        literal = args_has(args, 'l');
                    122:        if (!literal) {
                    123:                key = key_string_lookup_string(s);
1.52      nicm      124:                if (key != KEYC_NONE && key != KEYC_UNKNOWN) {
1.73      nicm      125:                        after = cmd_send_keys_inject_key(item, after, args,
                    126:                            key);
1.60      nicm      127:                        if (after != NULL)
                    128:                                return (after);
1.52      nicm      129:                }
1.49      nicm      130:                literal = 1;
                    131:        }
                    132:        if (literal) {
                    133:                ud = utf8_fromcstr(s);
1.64      nicm      134:                for (loop = ud; loop->size != 0; loop++) {
1.65      nicm      135:                        if (loop->size == 1 && loop->data[0] <= 0x7f)
                    136:                                key = loop->data[0];
                    137:                        else {
                    138:                                if (utf8_from_data(loop, &uc) != UTF8_DONE)
                    139:                                        continue;
                    140:                                key = uc;
                    141:                        }
1.73      nicm      142:                        after = cmd_send_keys_inject_key(item, after, args,
                    143:                            key);
1.50      nicm      144:                }
1.49      nicm      145:                free(ud);
                    146:        }
1.60      nicm      147:        return (after);
1.49      nicm      148: }
                    149:
1.29      nicm      150: static enum cmd_retval
1.33      nicm      151: cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm      152: {
1.55      nicm      153:        struct args                     *args = cmd_get_args(self);
1.56      nicm      154:        struct cmd_find_state           *target = cmdq_get_target(item);
1.60      nicm      155:        struct client                   *tc = cmdq_get_target_client(item);
1.56      nicm      156:        struct session                  *s = target->s;
                    157:        struct winlink                  *wl = target->wl;
1.60      nicm      158:        struct window_pane              *wp = target->wp;
1.59      nicm      159:        struct key_event                *event = cmdq_get_event(item);
                    160:        struct mouse_event              *m = &event->m;
1.46      nicm      161:        struct window_mode_entry        *wme = TAILQ_FIRST(&wp->modes);
1.60      nicm      162:        struct cmdq_item                *after = item;
1.45      nicm      163:        key_code                         key;
1.67      nicm      164:        u_int                            i, np = 1;
                    165:        u_int                            count = args_count(args);
1.45      nicm      166:        char                            *cause = NULL;
1.30      nicm      167:
                    168:        if (args_has(args, 'N')) {
1.72      nicm      169:                np = args_strtonum_and_expand(args, 'N', 1, UINT_MAX, item,
                    170:                         &cause);
1.30      nicm      171:                if (cause != NULL) {
1.34      nicm      172:                        cmdq_error(item, "repeat count %s", cause);
1.30      nicm      173:                        free(cause);
                    174:                        return (CMD_RETURN_ERROR);
                    175:                }
1.67      nicm      176:                if (wme != NULL && (args_has(args, 'X') || count == 0)) {
1.54      nicm      177:                        if (wme->mode->command == NULL) {
1.46      nicm      178:                                cmdq_error(item, "not in a mode");
                    179:                                return (CMD_RETURN_ERROR);
                    180:                        }
1.45      nicm      181:                        wme->prefix = np;
1.46      nicm      182:                }
1.30      nicm      183:        }
                    184:
                    185:        if (args_has(args, 'X')) {
1.45      nicm      186:                if (wme == NULL || wme->mode->command == NULL) {
1.33      nicm      187:                        cmdq_error(item, "not in a mode");
1.30      nicm      188:                        return (CMD_RETURN_ERROR);
                    189:                }
                    190:                if (!m->valid)
1.45      nicm      191:                        m = NULL;
1.60      nicm      192:                wme->mode->command(wme, tc, s, wl, args, m);
1.30      nicm      193:                return (CMD_RETURN_NORMAL);
                    194:        }
                    195:
1.19      nicm      196:        if (args_has(args, 'M')) {
                    197:                wp = cmd_mouse_pane(m, &s, NULL);
                    198:                if (wp == NULL) {
1.33      nicm      199:                        cmdq_error(item, "no mouse target");
1.19      nicm      200:                        return (CMD_RETURN_ERROR);
                    201:                }
1.60      nicm      202:                window_pane_key(wp, tc, s, wl, m->key, m);
1.19      nicm      203:                return (CMD_RETURN_NORMAL);
                    204:        }
1.14      nicm      205:
1.55      nicm      206:        if (cmd_get_entry(self) == &cmd_send_prefix_entry) {
1.14      nicm      207:                if (args_has(args, '2'))
1.21      nicm      208:                        key = options_get_number(s->options, "prefix2");
1.14      nicm      209:                else
1.21      nicm      210:                        key = options_get_number(s->options, "prefix");
1.73      nicm      211:                cmd_send_keys_inject_key(item, item, args, key);
1.14      nicm      212:                return (CMD_RETURN_NORMAL);
                    213:        }
1.10      nicm      214:
1.37      nicm      215:        if (args_has(args, 'R')) {
1.66      nicm      216:                colour_palette_clear(&wp->palette);
1.53      nicm      217:                input_reset(wp->ictx, 1);
1.66      nicm      218:                wp->flags |= (PANE_STYLECHANGED|PANE_REDRAW);
1.69      nicm      219:        }
                    220:
                    221:        if (count == 0) {
1.71      nicm      222:                if (args_has(args, 'N') || args_has(args, 'R'))
1.70      nicm      223:                        return (CMD_RETURN_NORMAL);
1.69      nicm      224:                for (; np != 0; np--)
1.73      nicm      225:                        cmd_send_keys_inject_key(item, NULL, args, event->key);
1.69      nicm      226:                return (CMD_RETURN_NORMAL);
1.37      nicm      227:        }
1.1       nicm      228:
1.34      nicm      229:        for (; np != 0; np--) {
1.67      nicm      230:                for (i = 0; i < count; i++) {
1.60      nicm      231:                        after = cmd_send_keys_inject_string(item, after, args,
                    232:                            i);
1.56      nicm      233:                }
1.9       nicm      234:        }
1.1       nicm      235:
1.12      nicm      236:        return (CMD_RETURN_NORMAL);
1.1       nicm      237: }