xref: /openbsd-src/usr.bin/tmux/cmd-send-keys.c (revision edafd27b79dc99e9d73299503ce06c69c0b7b01e)
1 /* $OpenBSD: cmd-send-keys.c,v 1.40 2017/05/09 17:56:55 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
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 = { "lXRMN:t:", 0, -1 },
37 	.usage = "[-lXRM] [-N repeat-count] " CMD_TARGET_PANE_USAGE " key ...",
38 
39 	.target = { 't', CMD_FIND_PANE, 0 },
40 
41 	.flags = CMD_AFTERHOOK,
42 	.exec = cmd_send_keys_exec
43 };
44 
45 const struct cmd_entry cmd_send_prefix_entry = {
46 	.name = "send-prefix",
47 	.alias = NULL,
48 
49 	.args = { "2t:", 0, 0 },
50 	.usage = "[-2] " CMD_TARGET_PANE_USAGE,
51 
52 	.target = { 't', CMD_FIND_PANE, 0 },
53 
54 	.flags = CMD_AFTERHOOK,
55 	.exec = cmd_send_keys_exec
56 };
57 
58 static void
59 cmd_send_keys_inject(struct client *c, struct cmdq_item *item, key_code key)
60 {
61 	struct window_pane	*wp = item->target.wp;
62 	struct session		*s = item->target.s;
63 	struct key_table	*table;
64 	struct key_binding	*bd, bd_find;
65 
66 	if (wp->mode == NULL || wp->mode->key_table == NULL) {
67 		window_pane_key(wp, NULL, s, key, NULL);
68 		return;
69 	}
70 	table = key_bindings_get_table(wp->mode->key_table(wp), 1);
71 
72 	bd_find.key = (key & ~KEYC_XTERM);
73 	bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
74 	if (bd != NULL) {
75 		table->references++;
76 		key_bindings_dispatch(bd, c, NULL, &item->target);
77 		key_bindings_unref_table(table);
78 	}
79 }
80 
81 static enum cmd_retval
82 cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
83 {
84 	struct args		*args = self->args;
85 	struct client		*c = cmd_find_client(item, NULL, 1);
86 	struct window_pane	*wp = item->target.wp;
87 	struct session		*s = item->target.s;
88 	struct mouse_event	*m = &item->shared->mouse;
89 	struct utf8_data	*ud, *uc;
90 	wchar_t			 wc;
91 	int			 i, literal;
92 	key_code		 key;
93 	u_int			 np = 1;
94 	char			*cause = NULL;
95 
96 	if (args_has(args, 'N')) {
97 		np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
98 		if (cause != NULL) {
99 			cmdq_error(item, "repeat count %s", cause);
100 			free(cause);
101 			return (CMD_RETURN_ERROR);
102 		}
103 		if (args_has(args, 'X') || args->argc == 0)
104 			wp->modeprefix = np;
105 	}
106 
107 	if (args_has(args, 'X')) {
108 		if (wp->mode == NULL || wp->mode->command == NULL) {
109 			cmdq_error(item, "not in a mode");
110 			return (CMD_RETURN_ERROR);
111 		}
112 		if (!m->valid)
113 			wp->mode->command(wp, c, s, args, NULL);
114 		else
115 			wp->mode->command(wp, c, s, args, m);
116 		return (CMD_RETURN_NORMAL);
117 	}
118 
119 	if (args_has(args, 'M')) {
120 		wp = cmd_mouse_pane(m, &s, NULL);
121 		if (wp == NULL) {
122 			cmdq_error(item, "no mouse target");
123 			return (CMD_RETURN_ERROR);
124 		}
125 		window_pane_key(wp, NULL, s, m->key, m);
126 		return (CMD_RETURN_NORMAL);
127 	}
128 
129 	if (self->entry == &cmd_send_prefix_entry) {
130 		if (args_has(args, '2'))
131 			key = options_get_number(s->options, "prefix2");
132 		else
133 			key = options_get_number(s->options, "prefix");
134 		cmd_send_keys_inject(c, item, key);
135 		return (CMD_RETURN_NORMAL);
136 	}
137 
138 	if (args_has(args, 'R')) {
139 		window_pane_reset_palette(wp);
140 		input_reset(wp, 1);
141 	}
142 
143 	for (; np != 0; np--) {
144 		for (i = 0; i < args->argc; i++) {
145 			literal = args_has(args, 'l');
146 			if (!literal) {
147 				key = key_string_lookup_string(args->argv[i]);
148 				if (key != KEYC_NONE && key != KEYC_UNKNOWN)
149 					cmd_send_keys_inject(c, item, key);
150 				else
151 					literal = 1;
152 			}
153 			if (literal) {
154 				ud = utf8_fromcstr(args->argv[i]);
155 				for (uc = ud; uc->size != 0; uc++) {
156 					if (utf8_combine(uc, &wc) != UTF8_DONE)
157 						continue;
158 					cmd_send_keys_inject(c, item, wc);
159 				}
160 				free(ud);
161 			}
162 		}
163 
164 	}
165 
166 	return (CMD_RETURN_NORMAL);
167 }
168