199e242abSchristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4f26e8bc9Schristos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
21698d5317Sjmmv #include <stdlib.h>
22928fc495Schristos #include <string.h>
23698d5317Sjmmv
24698d5317Sjmmv #include "tmux.h"
25698d5317Sjmmv
26698d5317Sjmmv /*
27698d5317Sjmmv * Send keys to client.
28698d5317Sjmmv */
29698d5317Sjmmv
30e9a2d6faSchristos static enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmdq_item *);
31698d5317Sjmmv
32698d5317Sjmmv const struct cmd_entry cmd_send_keys_entry = {
33f26e8bc9Schristos .name = "send-keys",
34f26e8bc9Schristos .alias = "send",
35f26e8bc9Schristos
36*f844e94eSwiz .args = { "c:FHKlMN:Rt:X", 0, -1, NULL },
37*f844e94eSwiz .usage = "[-FHKlMRX] [-c target-client] [-N repeat-count] "
38*f844e94eSwiz CMD_TARGET_PANE_USAGE " key ...",
39f26e8bc9Schristos
40fe99a117Schristos .target = { 't', CMD_FIND_PANE, 0 },
41f26e8bc9Schristos
42*f844e94eSwiz .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL,
43f26e8bc9Schristos .exec = cmd_send_keys_exec
44698d5317Sjmmv };
45698d5317Sjmmv
46928fc495Schristos const struct cmd_entry cmd_send_prefix_entry = {
47f26e8bc9Schristos .name = "send-prefix",
48f26e8bc9Schristos .alias = NULL,
49f26e8bc9Schristos
5046548964Swiz .args = { "2t:", 0, 0, NULL },
51f26e8bc9Schristos .usage = "[-2] " CMD_TARGET_PANE_USAGE,
52f26e8bc9Schristos
53fe99a117Schristos .target = { 't', CMD_FIND_PANE, 0 },
54f26e8bc9Schristos
55e9a2d6faSchristos .flags = CMD_AFTERHOOK,
56f26e8bc9Schristos .exec = cmd_send_keys_exec
57928fc495Schristos };
58928fc495Schristos
5930744affSchristos static struct cmdq_item *
cmd_send_keys_inject_key(struct cmdq_item * item,struct cmdq_item * after,struct args * args,key_code key)60e271dbb8Schristos cmd_send_keys_inject_key(struct cmdq_item *item, struct cmdq_item *after,
61*f844e94eSwiz struct args *args, key_code key)
62fe99a117Schristos {
63e271dbb8Schristos struct cmd_find_state *target = cmdq_get_target(item);
64e271dbb8Schristos struct client *tc = cmdq_get_target_client(item);
65e271dbb8Schristos struct session *s = target->s;
66e271dbb8Schristos struct winlink *wl = target->wl;
67e271dbb8Schristos struct window_pane *wp = target->wp;
680a274e86Schristos struct window_mode_entry *wme;
69*f844e94eSwiz struct key_table *table = NULL;
70c7e17de0Schristos struct key_binding *bd;
71*f844e94eSwiz struct key_event *event;
72*f844e94eSwiz
73*f844e94eSwiz if (args_has(args, 'K')) {
74*f844e94eSwiz if (tc == NULL)
75*f844e94eSwiz return (item);
76*f844e94eSwiz event = xmalloc(sizeof *event);
77*f844e94eSwiz event->key = key|KEYC_SENT;
78*f844e94eSwiz memset(&event->m, 0, sizeof event->m);
79*f844e94eSwiz if (server_client_handle_key(tc, event) == 0)
80*f844e94eSwiz free(event);
81*f844e94eSwiz return (item);
82*f844e94eSwiz }
83fe99a117Schristos
84e271dbb8Schristos wme = TAILQ_FIRST(&wp->modes);
850a274e86Schristos if (wme == NULL || wme->mode->key_table == NULL) {
86e271dbb8Schristos if (window_pane_key(wp, tc, s, wl, key, NULL) != 0)
8768e6ba84Schristos return (NULL);
8830744affSchristos return (item);
89fe99a117Schristos }
900a274e86Schristos table = key_bindings_get_table(wme->mode->key_table(wme), 1);
91fe99a117Schristos
92e271dbb8Schristos bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
93fe99a117Schristos if (bd != NULL) {
94fe99a117Schristos table->references++;
95e271dbb8Schristos after = key_bindings_dispatch(bd, after, tc, NULL, target);
96fe99a117Schristos key_bindings_unref_table(table);
97fe99a117Schristos }
98e271dbb8Schristos return (after);
9930744affSchristos }
10030744affSchristos
10130744affSchristos static struct cmdq_item *
cmd_send_keys_inject_string(struct cmdq_item * item,struct cmdq_item * after,struct args * args,int i)102e271dbb8Schristos cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
103e271dbb8Schristos struct args *args, int i)
10430744affSchristos {
10546548964Swiz const char *s = args_string(args, i);
106e271dbb8Schristos struct utf8_data *ud, *loop;
107e271dbb8Schristos utf8_char uc;
10830744affSchristos key_code key;
10930744affSchristos char *endptr;
11030744affSchristos long n;
11130744affSchristos int literal;
11230744affSchristos
11330744affSchristos if (args_has(args, 'H')) {
11430744affSchristos n = strtol(s, &endptr, 16);
11530744affSchristos if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
11630744affSchristos return (item);
117*f844e94eSwiz return (cmd_send_keys_inject_key(item, after, args,
118*f844e94eSwiz KEYC_LITERAL|n));
11930744affSchristos }
12030744affSchristos
12130744affSchristos literal = args_has(args, 'l');
12230744affSchristos if (!literal) {
12330744affSchristos key = key_string_lookup_string(s);
12468e6ba84Schristos if (key != KEYC_NONE && key != KEYC_UNKNOWN) {
125*f844e94eSwiz after = cmd_send_keys_inject_key(item, after, args,
126*f844e94eSwiz key);
127e271dbb8Schristos if (after != NULL)
128e271dbb8Schristos return (after);
12968e6ba84Schristos }
13030744affSchristos literal = 1;
13130744affSchristos }
13230744affSchristos if (literal) {
13330744affSchristos ud = utf8_fromcstr(s);
134e271dbb8Schristos for (loop = ud; loop->size != 0; loop++) {
135e271dbb8Schristos if (loop->size == 1 && loop->data[0] <= 0x7f)
136e271dbb8Schristos key = loop->data[0];
137e271dbb8Schristos else {
138e271dbb8Schristos if (utf8_from_data(loop, &uc) != UTF8_DONE)
13930744affSchristos continue;
140e271dbb8Schristos key = uc;
141e271dbb8Schristos }
142*f844e94eSwiz after = cmd_send_keys_inject_key(item, after, args,
143*f844e94eSwiz key);
14430744affSchristos }
14530744affSchristos free(ud);
14630744affSchristos }
147e271dbb8Schristos return (after);
148fe99a117Schristos }
149fe99a117Schristos
150e9a2d6faSchristos static enum cmd_retval
cmd_send_keys_exec(struct cmd * self,struct cmdq_item * item)151e9a2d6faSchristos cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
152698d5317Sjmmv {
153e271dbb8Schristos struct args *args = cmd_get_args(self);
154e271dbb8Schristos struct cmd_find_state *target = cmdq_get_target(item);
155e271dbb8Schristos struct client *tc = cmdq_get_target_client(item);
156e271dbb8Schristos struct session *s = target->s;
157e271dbb8Schristos struct winlink *wl = target->wl;
158e271dbb8Schristos struct window_pane *wp = target->wp;
159e271dbb8Schristos struct key_event *event = cmdq_get_event(item);
160e271dbb8Schristos struct mouse_event *m = &event->m;
1610a274e86Schristos struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
162e271dbb8Schristos struct cmdq_item *after = item;
163f26e8bc9Schristos key_code key;
16446548964Swiz u_int i, np = 1;
16546548964Swiz u_int count = args_count(args);
166e9a2d6faSchristos char *cause = NULL;
167e9a2d6faSchristos
168e9a2d6faSchristos if (args_has(args, 'N')) {
169*f844e94eSwiz np = args_strtonum_and_expand(args, 'N', 1, UINT_MAX, item,
170*f844e94eSwiz &cause);
171e9a2d6faSchristos if (cause != NULL) {
172e9a2d6faSchristos cmdq_error(item, "repeat count %s", cause);
173e9a2d6faSchristos free(cause);
174e9a2d6faSchristos return (CMD_RETURN_ERROR);
175e9a2d6faSchristos }
17646548964Swiz if (wme != NULL && (args_has(args, 'X') || count == 0)) {
177e271dbb8Schristos if (wme->mode->command == NULL) {
1780a274e86Schristos cmdq_error(item, "not in a mode");
1790a274e86Schristos return (CMD_RETURN_ERROR);
1800a274e86Schristos }
1810a274e86Schristos wme->prefix = np;
1820a274e86Schristos }
183e9a2d6faSchristos }
184e9a2d6faSchristos
185e9a2d6faSchristos if (args_has(args, 'X')) {
1860a274e86Schristos if (wme == NULL || wme->mode->command == NULL) {
187e9a2d6faSchristos cmdq_error(item, "not in a mode");
188e9a2d6faSchristos return (CMD_RETURN_ERROR);
189e9a2d6faSchristos }
190e9a2d6faSchristos if (!m->valid)
1910a274e86Schristos m = NULL;
192e271dbb8Schristos wme->mode->command(wme, tc, s, wl, args, m);
193e9a2d6faSchristos return (CMD_RETURN_NORMAL);
194e9a2d6faSchristos }
195698d5317Sjmmv
19699e242abSchristos if (args_has(args, 'M')) {
19799e242abSchristos wp = cmd_mouse_pane(m, &s, NULL);
19899e242abSchristos if (wp == NULL) {
199e9a2d6faSchristos cmdq_error(item, "no mouse target");
20099e242abSchristos return (CMD_RETURN_ERROR);
20199e242abSchristos }
202e271dbb8Schristos window_pane_key(wp, tc, s, wl, m->key, m);
20399e242abSchristos return (CMD_RETURN_NORMAL);
20499e242abSchristos }
20599e242abSchristos
206e271dbb8Schristos if (cmd_get_entry(self) == &cmd_send_prefix_entry) {
207928fc495Schristos if (args_has(args, '2'))
208f26e8bc9Schristos key = options_get_number(s->options, "prefix2");
209928fc495Schristos else
210f26e8bc9Schristos key = options_get_number(s->options, "prefix");
211*f844e94eSwiz cmd_send_keys_inject_key(item, item, args, key);
212928fc495Schristos return (CMD_RETURN_NORMAL);
213928fc495Schristos }
214928fc495Schristos
215e9a2d6faSchristos if (args_has(args, 'R')) {
21646548964Swiz colour_palette_clear(&wp->palette);
217e271dbb8Schristos input_reset(wp->ictx, 1);
21846548964Swiz wp->flags |= (PANE_STYLECHANGED|PANE_REDRAW);
21946548964Swiz }
22046548964Swiz
22146548964Swiz if (count == 0) {
22246548964Swiz if (args_has(args, 'N') || args_has(args, 'R'))
22346548964Swiz return (CMD_RETURN_NORMAL);
22446548964Swiz for (; np != 0; np--)
225*f844e94eSwiz cmd_send_keys_inject_key(item, NULL, args, event->key);
22646548964Swiz return (CMD_RETURN_NORMAL);
227e9a2d6faSchristos }
228698d5317Sjmmv
229e9a2d6faSchristos for (; np != 0; np--) {
23046548964Swiz for (i = 0; i < count; i++) {
231e271dbb8Schristos after = cmd_send_keys_inject_string(item, after, args,
232e271dbb8Schristos i);
233e271dbb8Schristos }
234d530c4d0Sjmmv }
235698d5317Sjmmv
236928fc495Schristos return (CMD_RETURN_NORMAL);
237698d5317Sjmmv }
238