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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    fuchsia_async as fasync, input_synthesis,
    std::{env, time::Duration},
    structopt::StructOpt,
};

#[derive(Debug, StructOpt)]
#[structopt(name = "input")]
/// simple tool to inject input events into Scenic
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")]
    /// Injects text using QWERTY keystrokes
    ///
    /// Text is injected by translating a string into keystrokes using a QWERTY keymap. This
    /// facility is intended for end-to-end and input testing purposes only.
    ///
    /// Only printable ASCII characters are mapped. Tab, newline, and other control characters are
    /// not supported, and `keyevent` should be used instead.
    ///
    /// The events simulated consist of a series of keyboard reports, ending in a report with no
    /// keys. The number of reports is near the lower bound of reports needed to produce the
    /// requested string, minimizing pauses and shift-state transitions.
    ///
    /// The `--key_event_duration` is the time between each report. Care should be taken not to
    /// provide so long a duration that key repeat kicks in.
    ///
    /// Note: when using through `fx shell` with quotes, you may need to surround the invocation in
    /// strong quotes, e.g.:
    ///
    /// fx shell 'input text "Hello, world!"'
    Text {
        #[structopt(long = "key_event_duration", default_value = "0")]
        /// Duration of the each event in milliseconds
        key_event_duration_msec: u32,
        /// Input text to inject
        input: String,
    },
    #[structopt(name = "keyevent", raw(help = "KEYEVENT_HELP"))]
    KeyboardEvent {
        #[structopt(short = "d", long = "duration", default_value = "0")]
        /// Duration of the event(s) in milliseconds
        duration_msec: u32,
        /// HID usage code
        usage: u32,
    },
    #[structopt(name = "tap")]
    /// Injects a single tap event
    ///
    /// This command simulates a single touch down + up sequence. By default, the `x` and `y`
    /// coordinates are in the range 0 to 1000 and will be proportionally transformed to the
    /// current display, but you can specify a virtual range for the input with the `--width` and
    /// `--height` options.
    ///
    /// The time between the down and up report is `--duration`.
    Tap {
        #[structopt(short = "w", long = "width", default_value = "1000")]
        /// Width of the display
        width: u32,
        #[structopt(short = "h", long = "height", default_value = "1000")]
        /// Height of the display
        height: u32,
        #[structopt(short = "tc", long = "tap_event_count", default_value = "1")]
        /// Number of tap events to send (`--duration` is divided over the tap events)
        tap_event_count: usize,
        #[structopt(short = "d", long = "duration", default_value = "0")]
        /// Duration of the event(s) in milliseconds
        duration_msec: u32,
        /// X axis coordinate
        x: u32,
        /// Y axis coordinate
        y: u32,
    },
    #[structopt(name = "swipe")]
    /// Injects a swipe event
    ///
    /// This command simulates a touch down, a series of touch move, followed up a touch up event.
    /// By default, the `x` and `y` coordinates are in the range 0 to 1000 and will be
    /// proportionally transformed to the current display, but you can specify a virtual range for
    /// the input with the `--width` and `--height` options.
    ///
    /// The time between the down and up report is `--duration`.
    Swipe {
        #[structopt(short = "w", long = "width", default_value = "1000")]
        /// Width of the display
        width: u32,
        #[structopt(short = "h", long = "height", default_value = "1000")]
        /// Height of the display
        height: u32,
        #[structopt(short = "mc", long = "move_event_count", default_value = "100")]
        /// Number of move events to send in between the down and up events of the swipe
        move_event_count: usize,
        #[structopt(short = "d", long = "duration", default_value = "0")]
        /// Duration of the event(s) in milliseconds
        duration_msec: u32,
        /// X axis start coordinate
        x0: u32,
        /// Y axis start coordinate
        y0: u32,
        /// X axis end coordinate
        x1: u32,
        /// Y axis end coordinate
        y1: u32,
    },
    #[structopt(name = "media_button")]
    /// Injects a MediaButton event
    MediaButton {
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        mic_mute: bool,
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        volume_up: bool,
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        volume_down: bool,
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        reset: bool,
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        pause: bool,
        #[structopt(parse(try_from_str = "parse_bool"))]
        /// 0 or 1
        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() {
    // TODO: Remove this workaround once topaz tests have been updated.
    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");
}