1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use {
fuchsia_async as fasync, input_synthesis,
std::{env, time::Duration},
structopt::StructOpt,
};
#[derive(Debug, StructOpt)]
#[structopt(name = "input")]
struct Opt {
#[structopt(subcommand)]
command: Command,
}
const KEYEVENT_HELP: &str = r#"Injects a single key event.
This command simulates a single key down + up sequence. The argument is a decimal HID usage
`code`, prior to any remapping the IME may do.
Common usage codes:
key | code
----------|-----
enter | 40
escape | 41
backspace | 42
tab | 43
The time between the down and up report is `--duration`."#;
#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "text")]
Text {
#[structopt(long = "key_event_duration", default_value = "0")]
key_event_duration_msec: u32,
input: String,
},
#[structopt(name = "keyevent", raw(help = "KEYEVENT_HELP"))]
KeyboardEvent {
#[structopt(short = "d", long = "duration", default_value = "0")]
duration_msec: u32,
usage: u32,
},
#[structopt(name = "tap")]
Tap {
#[structopt(short = "w", long = "width", default_value = "1000")]
width: u32,
#[structopt(short = "h", long = "height", default_value = "1000")]
height: u32,
#[structopt(short = "tc", long = "tap_event_count", default_value = "1")]
tap_event_count: usize,
#[structopt(short = "d", long = "duration", default_value = "0")]
duration_msec: u32,
x: u32,
y: u32,
},
#[structopt(name = "swipe")]
Swipe {
#[structopt(short = "w", long = "width", default_value = "1000")]
width: u32,
#[structopt(short = "h", long = "height", default_value = "1000")]
height: u32,
#[structopt(short = "mc", long = "move_event_count", default_value = "100")]
move_event_count: usize,
#[structopt(short = "d", long = "duration", default_value = "0")]
duration_msec: u32,
x0: u32,
y0: u32,
x1: u32,
y1: u32,
},
#[structopt(name = "media_button")]
MediaButton {
#[structopt(parse(try_from_str = "parse_bool"))]
mic_mute: bool,
#[structopt(parse(try_from_str = "parse_bool"))]
volume_up: bool,
#[structopt(parse(try_from_str = "parse_bool"))]
volume_down: bool,
#[structopt(parse(try_from_str = "parse_bool"))]
reset: bool,
#[structopt(parse(try_from_str = "parse_bool"))]
pause: bool,
#[structopt(parse(try_from_str = "parse_bool"))]
camera_disable: bool,
},
}
fn parse_bool(src: &str) -> Result<bool, String> {
match src {
"0" | "false" => Ok(false),
"1" | "true" => Ok(true),
_ => Err(format!("cannot parse {:?} to bool", src)),
}
}
#[fasync::run_singlethreaded]
async fn main() {
let (mut args, options): (Vec<_>, Vec<_>) = env::args_os().partition(|s| match s.to_str() {
Some(s) => s.get(0..2) != Some("--"),
_ => false,
});
args.extend(options);
let opt = Opt::from_iter(args.into_iter());
match opt.command {
Command::Text { input, key_event_duration_msec } => {
input_synthesis::text_command(
input,
Duration::from_millis(key_event_duration_msec as u64),
)
.await
}
Command::KeyboardEvent { usage, duration_msec } => {
input_synthesis::keyboard_event_command(
usage,
Duration::from_millis(duration_msec as u64),
)
.await
}
Command::Tap { width, height, tap_event_count, duration_msec, x, y } => {
input_synthesis::tap_event_command(
x,
y,
width,
height,
tap_event_count,
Duration::from_millis(duration_msec as u64),
)
.await
}
Command::Swipe { width, height, move_event_count, duration_msec, x0, y0, x1, y1 } => {
input_synthesis::swipe_command(
x0,
y0,
x1,
y1,
width,
height,
move_event_count,
Duration::from_millis(duration_msec as u64),
)
.await
}
Command::MediaButton { mic_mute, volume_up, volume_down, reset, pause, camera_disable } => {
input_synthesis::media_button_event_command(
mic_mute,
volume_up,
volume_down,
reset,
pause,
camera_disable,
)
.await
}
}
.expect("failed to run command");
}