1 /* $OpenBSD$ */
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 enum cmd_retval	 cmd_send_keys_exec(struct cmd *, struct cmd_q *);
31 
32 const struct cmd_entry cmd_send_keys_entry = {
33 	.name = "send-keys",
34 	.alias = "send",
35 
36 	.args = { "lRMt:", 0, -1 },
37 	.usage = "[-lRM] " CMD_TARGET_PANE_USAGE " key ...",
38 
39 	.tflag = CMD_PANE,
40 
41 	.flags = 0,
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 	.tflag = CMD_PANE,
53 
54 	.flags = 0,
55 	.exec = cmd_send_keys_exec
56 };
57 
58 enum cmd_retval
cmd_send_keys_exec(struct cmd * self,struct cmd_q * cmdq)59 cmd_send_keys_exec(struct cmd *self, struct cmd_q *cmdq)
60 {
61 #ifdef TMATE
62 	return (CMD_RETURN_ERROR);
63 #else
64 	struct args		*args = self->args;
65 	struct window_pane	*wp = cmdq->state.tflag.wp;
66 	struct session		*s = cmdq->state.tflag.s;
67 	struct mouse_event	*m = &cmdq->item->mouse;
68 	const u_char		*keystr;
69 	int			 i, literal;
70 	key_code		 key;
71 
72 	if (args_has(args, 'M')) {
73 		wp = cmd_mouse_pane(m, &s, NULL);
74 		if (wp == NULL) {
75 			cmdq_error(cmdq, "no mouse target");
76 			return (CMD_RETURN_ERROR);
77 		}
78 		window_pane_key(wp, NULL, s, m->key, m);
79 		return (CMD_RETURN_NORMAL);
80 	}
81 
82 	if (self->entry == &cmd_send_prefix_entry) {
83 		if (args_has(args, '2'))
84 			key = options_get_number(s->options, "prefix2");
85 		else
86 			key = options_get_number(s->options, "prefix");
87 		window_pane_key(wp, NULL, s, key, NULL);
88 		return (CMD_RETURN_NORMAL);
89 	}
90 
91 	if (args_has(args, 'R'))
92 		input_reset(wp, 1);
93 
94 	for (i = 0; i < args->argc; i++) {
95 		literal = args_has(args, 'l');
96 		if (!literal) {
97 			key = key_string_lookup_string(args->argv[i]);
98 			if (key != KEYC_NONE && key != KEYC_UNKNOWN)
99 				window_pane_key(wp, NULL, s, key, NULL);
100 			else
101 				literal = 1;
102 		}
103 		if (literal) {
104 			for (keystr = args->argv[i]; *keystr != '\0'; keystr++)
105 				window_pane_key(wp, NULL, s, *keystr, NULL);
106 		}
107 	}
108 
109 	return (CMD_RETURN_NORMAL);
110 #endif
111 }
112