1 | /* $OpenBSD$ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2008 Nicholas Marriott <[email protected]> |
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 | * Send keys to client. |
28 | */ |
29 | |
30 | static enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmdq_item *); |
31 | |
32 | const struct cmd_entry cmd_send_keys_entry = { |
33 | .name = "send-keys" , |
34 | .alias = "send" , |
35 | |
36 | .args = { "FHlMN:Rt:X" , 0, -1 }, |
37 | .usage = "[-FHlMRX] [-N repeat-count] " CMD_TARGET_PANE_USAGE |
38 | " key ..." , |
39 | |
40 | .target = { 't', CMD_FIND_PANE, 0 }, |
41 | |
42 | .flags = CMD_AFTERHOOK, |
43 | .exec = cmd_send_keys_exec |
44 | }; |
45 | |
46 | const struct cmd_entry cmd_send_prefix_entry = { |
47 | .name = "send-prefix" , |
48 | .alias = NULL, |
49 | |
50 | .args = { "2t:" , 0, 0 }, |
51 | .usage = "[-2] " CMD_TARGET_PANE_USAGE, |
52 | |
53 | .target = { 't', CMD_FIND_PANE, 0 }, |
54 | |
55 | .flags = CMD_AFTERHOOK, |
56 | .exec = cmd_send_keys_exec |
57 | }; |
58 | |
59 | static struct cmdq_item * |
60 | cmd_send_keys_inject_key(struct client *c, struct cmd_find_state *fs, |
61 | struct cmdq_item *item, key_code key) |
62 | { |
63 | struct session *s = fs->s; |
64 | struct winlink *wl = fs->wl; |
65 | struct window_pane *wp = fs->wp; |
66 | struct window_mode_entry *wme; |
67 | struct key_table *table; |
68 | struct key_binding *bd; |
69 | |
70 | wme = TAILQ_FIRST(&fs->wp->modes); |
71 | if (wme == NULL || wme->mode->key_table == NULL) { |
72 | if (options_get_number(fs->wp->window->options, "xterm-keys" )) |
73 | key |= KEYC_XTERM; |
74 | if (window_pane_key(wp, item->client, s, wl, key, NULL) != 0) |
75 | return (NULL); |
76 | return (item); |
77 | } |
78 | table = key_bindings_get_table(wme->mode->key_table(wme), 1); |
79 | |
80 | bd = key_bindings_get(table, key & ~KEYC_XTERM); |
81 | if (bd != NULL) { |
82 | table->references++; |
83 | item = key_bindings_dispatch(bd, item, c, NULL, &item->target); |
84 | key_bindings_unref_table(table); |
85 | } |
86 | return (item); |
87 | } |
88 | |
89 | static struct cmdq_item * |
90 | cmd_send_keys_inject_string(struct client *c, struct cmd_find_state *fs, |
91 | struct cmdq_item *item, struct args *args, int i) |
92 | { |
93 | const char *s = args->argv[i]; |
94 | struct cmdq_item *new_item; |
95 | struct utf8_data *ud, *uc; |
96 | wchar_t wc; |
97 | key_code key; |
98 | char *endptr; |
99 | long n; |
100 | int literal; |
101 | |
102 | if (args_has(args, 'H')) { |
103 | n = strtol(s, &endptr, 16); |
104 | if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0') |
105 | return (item); |
106 | return (cmd_send_keys_inject_key(c, fs, item, KEYC_LITERAL|n)); |
107 | } |
108 | |
109 | literal = args_has(args, 'l'); |
110 | if (!literal) { |
111 | key = key_string_lookup_string(s); |
112 | if (key != KEYC_NONE && key != KEYC_UNKNOWN) { |
113 | new_item = cmd_send_keys_inject_key(c, fs, item, key); |
114 | if (new_item != NULL) |
115 | return (new_item); |
116 | } |
117 | literal = 1; |
118 | } |
119 | if (literal) { |
120 | ud = utf8_fromcstr(s); |
121 | for (uc = ud; uc->size != 0; uc++) { |
122 | if (utf8_combine(uc, &wc) != UTF8_DONE) |
123 | continue; |
124 | item = cmd_send_keys_inject_key(c, fs, item, wc); |
125 | } |
126 | free(ud); |
127 | } |
128 | return (item); |
129 | } |
130 | |
131 | static enum cmd_retval |
132 | cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item) |
133 | { |
134 | struct args *args = self->args; |
135 | struct client *c = cmd_find_client(item, NULL, 1); |
136 | struct cmd_find_state *fs = &item->target; |
137 | struct window_pane *wp = item->target.wp; |
138 | struct session *s = item->target.s; |
139 | struct winlink *wl = item->target.wl; |
140 | struct mouse_event *m = &item->shared->mouse; |
141 | struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); |
142 | int i; |
143 | key_code key; |
144 | u_int np = 1; |
145 | char *cause = NULL; |
146 | |
147 | if (args_has(args, 'N')) { |
148 | np = args_strtonum(args, 'N', 1, UINT_MAX, &cause); |
149 | if (cause != NULL) { |
150 | cmdq_error(item, "repeat count %s" , cause); |
151 | free(cause); |
152 | return (CMD_RETURN_ERROR); |
153 | } |
154 | if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) { |
155 | if (wme == NULL || wme->mode->command == NULL) { |
156 | cmdq_error(item, "not in a mode" ); |
157 | return (CMD_RETURN_ERROR); |
158 | } |
159 | wme->prefix = np; |
160 | } |
161 | } |
162 | |
163 | if (args_has(args, 'X')) { |
164 | if (wme == NULL || wme->mode->command == NULL) { |
165 | cmdq_error(item, "not in a mode" ); |
166 | return (CMD_RETURN_ERROR); |
167 | } |
168 | if (!m->valid) |
169 | m = NULL; |
170 | wme->mode->command(wme, c, s, wl, args, m); |
171 | return (CMD_RETURN_NORMAL); |
172 | } |
173 | |
174 | if (args_has(args, 'M')) { |
175 | wp = cmd_mouse_pane(m, &s, NULL); |
176 | if (wp == NULL) { |
177 | cmdq_error(item, "no mouse target" ); |
178 | return (CMD_RETURN_ERROR); |
179 | } |
180 | window_pane_key(wp, item->client, s, wl, m->key, m); |
181 | return (CMD_RETURN_NORMAL); |
182 | } |
183 | |
184 | if (self->entry == &cmd_send_prefix_entry) { |
185 | if (args_has(args, '2')) |
186 | key = options_get_number(s->options, "prefix2" ); |
187 | else |
188 | key = options_get_number(s->options, "prefix" ); |
189 | cmd_send_keys_inject_key(c, fs, item, key); |
190 | return (CMD_RETURN_NORMAL); |
191 | } |
192 | |
193 | if (args_has(args, 'R')) { |
194 | window_pane_reset_palette(wp); |
195 | input_reset(wp, 1); |
196 | } |
197 | |
198 | for (; np != 0; np--) { |
199 | for (i = 0; i < args->argc; i++) |
200 | item = cmd_send_keys_inject_string(c, fs, item, args, i); |
201 | } |
202 | |
203 | return (CMD_RETURN_NORMAL); |
204 | } |
205 | |