Annotation of src/usr.bin/tmux/cmd-send-keys.c, Revision 1.61
1.61 ! nicm 1: /* $OpenBSD: cmd-send-keys.c,v 1.60 2020/04/13 20:51:57 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.51 nicm 36: .args = { "FHlMN:Rt:X", 0, -1 },
37: .usage = "[-FHlMRX] [-N repeat-count] " CMD_TARGET_PANE_USAGE
38: " key ...",
1.25 nicm 39:
1.39 nicm 40: .target = { 't', CMD_FIND_PANE, 0 },
1.26 nicm 41:
1.31 nicm 42: .flags = CMD_AFTERHOOK,
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:
50: .args = { "2t:", 0, 0 },
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,
61: 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.45 nicm 69: struct key_table *table;
70: struct key_binding *bd;
1.40 nicm 71:
1.60 nicm 72: wme = TAILQ_FIRST(&wp->modes);
1.45 nicm 73: if (wme == NULL || wme->mode->key_table == NULL) {
1.60 nicm 74: if (options_get_number(wp->window->options, "xterm-keys"))
1.42 nicm 75: key |= KEYC_XTERM;
1.60 nicm 76: if (window_pane_key(wp, tc, s, wl, key, NULL) != 0)
1.52 nicm 77: return (NULL);
1.47 nicm 78: return (item);
1.40 nicm 79: }
1.45 nicm 80: table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.40 nicm 81:
1.43 nicm 82: bd = key_bindings_get(table, key & ~KEYC_XTERM);
1.40 nicm 83: if (bd != NULL) {
84: table->references++;
1.60 nicm 85: after = key_bindings_dispatch(bd, after, tc, NULL, target);
1.40 nicm 86: key_bindings_unref_table(table);
87: }
1.60 nicm 88: return (after);
1.40 nicm 89: }
90:
1.49 nicm 91: static struct cmdq_item *
1.60 nicm 92: cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
93: struct args *args, int i)
1.49 nicm 94: {
95: const char *s = args->argv[i];
96: struct utf8_data *ud, *uc;
97: wchar_t wc;
98: key_code key;
99: char *endptr;
100: long n;
101: int literal;
102:
103: if (args_has(args, 'H')) {
104: n = strtol(s, &endptr, 16);
105: if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
106: return (item);
1.60 nicm 107: return (cmd_send_keys_inject_key(item, after, KEYC_LITERAL|n));
1.49 nicm 108: }
109:
110: literal = args_has(args, 'l');
111: if (!literal) {
112: key = key_string_lookup_string(s);
1.52 nicm 113: if (key != KEYC_NONE && key != KEYC_UNKNOWN) {
1.60 nicm 114: after = cmd_send_keys_inject_key(item, after, key);
115: if (after != NULL)
116: return (after);
1.52 nicm 117: }
1.49 nicm 118: literal = 1;
119: }
120: if (literal) {
121: ud = utf8_fromcstr(s);
122: for (uc = ud; uc->size != 0; uc++) {
1.61 ! nicm 123: if (utf8_combine(uc, &wc) != UTF8_DONE)
1.49 nicm 124: continue;
1.60 nicm 125: after = cmd_send_keys_inject_key(item, after, wc);
1.50 nicm 126: }
1.49 nicm 127: free(ud);
128: }
1.60 nicm 129: return (after);
1.49 nicm 130: }
131:
1.29 nicm 132: static enum cmd_retval
1.33 nicm 133: cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 134: {
1.55 nicm 135: struct args *args = cmd_get_args(self);
1.56 nicm 136: struct cmd_find_state *target = cmdq_get_target(item);
1.60 nicm 137: struct client *tc = cmdq_get_target_client(item);
1.56 nicm 138: struct session *s = target->s;
139: struct winlink *wl = target->wl;
1.60 nicm 140: struct window_pane *wp = target->wp;
1.59 nicm 141: struct key_event *event = cmdq_get_event(item);
142: struct mouse_event *m = &event->m;
1.46 nicm 143: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
1.60 nicm 144: struct cmdq_item *after = item;
1.49 nicm 145: int i;
1.45 nicm 146: key_code key;
147: u_int np = 1;
148: char *cause = NULL;
1.30 nicm 149:
150: if (args_has(args, 'N')) {
151: np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
152: if (cause != NULL) {
1.34 nicm 153: cmdq_error(item, "repeat count %s", cause);
1.30 nicm 154: free(cause);
155: return (CMD_RETURN_ERROR);
156: }
1.46 nicm 157: if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) {
1.54 nicm 158: if (wme->mode->command == NULL) {
1.46 nicm 159: cmdq_error(item, "not in a mode");
160: return (CMD_RETURN_ERROR);
161: }
1.45 nicm 162: wme->prefix = np;
1.46 nicm 163: }
1.30 nicm 164: }
165:
166: if (args_has(args, 'X')) {
1.45 nicm 167: if (wme == NULL || wme->mode->command == NULL) {
1.33 nicm 168: cmdq_error(item, "not in a mode");
1.30 nicm 169: return (CMD_RETURN_ERROR);
170: }
171: if (!m->valid)
1.45 nicm 172: m = NULL;
1.60 nicm 173: wme->mode->command(wme, tc, s, wl, args, m);
1.30 nicm 174: return (CMD_RETURN_NORMAL);
175: }
176:
1.19 nicm 177: if (args_has(args, 'M')) {
178: wp = cmd_mouse_pane(m, &s, NULL);
179: if (wp == NULL) {
1.33 nicm 180: cmdq_error(item, "no mouse target");
1.19 nicm 181: return (CMD_RETURN_ERROR);
182: }
1.60 nicm 183: window_pane_key(wp, tc, s, wl, m->key, m);
1.19 nicm 184: return (CMD_RETURN_NORMAL);
185: }
1.14 nicm 186:
1.55 nicm 187: if (cmd_get_entry(self) == &cmd_send_prefix_entry) {
1.14 nicm 188: if (args_has(args, '2'))
1.21 nicm 189: key = options_get_number(s->options, "prefix2");
1.14 nicm 190: else
1.21 nicm 191: key = options_get_number(s->options, "prefix");
1.60 nicm 192: cmd_send_keys_inject_key(item, item, key);
1.14 nicm 193: return (CMD_RETURN_NORMAL);
194: }
1.10 nicm 195:
1.37 nicm 196: if (args_has(args, 'R')) {
197: window_pane_reset_palette(wp);
1.53 nicm 198: input_reset(wp->ictx, 1);
1.37 nicm 199: }
1.1 nicm 200:
1.34 nicm 201: for (; np != 0; np--) {
1.56 nicm 202: for (i = 0; i < args->argc; i++) {
1.60 nicm 203: after = cmd_send_keys_inject_string(item, after, args,
204: i);
1.56 nicm 205: }
1.9 nicm 206: }
1.1 nicm 207:
1.12 nicm 208: return (CMD_RETURN_NORMAL);
1.1 nicm 209: }