sl4f_lib/input/
types.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use serde::{Deserialize, Deserializer, Serialize};
6
7use fidl_fuchsia_ui_input::Touch;
8use input_synthesis as is;
9
10/// Enum for supported Input commands.
11pub enum InputMethod {
12    Tap,
13    MultiFingerTap,
14    Swipe,
15    MultiFingerSwipe,
16    Text,
17    KeyPress,
18    /// Arbitrary sequence of key events.
19    KeyEvents,
20}
21
22impl std::str::FromStr for InputMethod {
23    type Err = anyhow::Error;
24    fn from_str(method: &str) -> Result<Self, Self::Err> {
25        match method {
26            "Tap" => Ok(InputMethod::Tap),
27            "MultiFingerTap" => Ok(InputMethod::MultiFingerTap),
28            "Swipe" => Ok(InputMethod::Swipe),
29            "MultiFingerSwipe" => Ok(InputMethod::MultiFingerSwipe),
30            "Text" => Ok(InputMethod::Text),
31            "KeyPress" => Ok(InputMethod::KeyPress),
32            "KeyEvents" => Ok(InputMethod::KeyEvents),
33            _ => return Err(format_err!("Invalid Input Facade method: {}", method)),
34        }
35    }
36}
37
38#[derive(Deserialize, Debug)]
39pub struct MultiFingerTapRequest {
40    pub width: Option<u32>,
41    pub height: Option<u32>,
42    pub tap_event_count: Option<usize>,
43    pub duration: Option<u64>,
44
45    #[serde(default, deserialize_with = "TouchDef::vec")]
46    pub fingers: Vec<Touch>,
47}
48
49#[derive(Deserialize, Debug)]
50pub struct TapRequest {
51    pub x: u32,
52    pub y: u32,
53    pub width: Option<u32>,
54    pub height: Option<u32>,
55    pub tap_event_count: Option<usize>,
56    pub duration: Option<u64>,
57}
58
59#[derive(Deserialize, Debug)]
60pub struct SwipeRequest {
61    pub x0: u32,
62    pub y0: u32,
63    pub x1: u32,
64    pub y1: u32,
65    pub width: Option<u32>,
66    pub height: Option<u32>,
67    pub tap_event_count: Option<usize>,
68    pub duration: Option<u64>,
69}
70
71#[derive(Deserialize, Debug)]
72/// Describes a start and end position for a single finger.
73pub struct FingerSwipe {
74    pub x0: u32,
75    pub y0: u32,
76    pub x1: u32,
77    pub y1: u32,
78}
79
80#[derive(Deserialize, Debug)]
81pub struct MultiFingerSwipeRequest {
82    pub fingers: Vec<FingerSwipe>,
83    pub width: Option<u32>,
84    pub height: Option<u32>,
85    pub move_event_count: Option<usize>,
86    pub duration: Option<u64>,
87}
88
89#[derive(Deserialize, Debug)]
90pub struct TextRequest {
91    pub text: String,
92    pub key_event_duration: Option<u64>,
93}
94
95#[derive(Deserialize, Debug)]
96pub struct KeyPressRequest {
97    pub hid_usage_id: u16,
98    pub key_press_duration: Option<u64>,
99}
100
101#[derive(Deserialize, Debug)]
102pub struct KeyEventsRequest {
103    #[serde(default, deserialize_with = "is::synthesizer::TimedKeyEvent::vec")]
104    pub key_events: Vec<is::synthesizer::TimedKeyEvent>,
105}
106
107#[derive(Serialize, Deserialize, Debug)]
108pub enum ActionResult {
109    Success,
110}
111
112/// This matches the FIDL struct `Touch` defined at
113/// sdk/fidl/fuchsia.ui.input/input_reports.fidl
114/// and is enforced by the build system.
115#[derive(Serialize, Deserialize, Debug)]
116#[serde(remote = "Touch")]
117pub struct TouchDef {
118    pub finger_id: u32,
119    pub x: i32,
120    pub y: i32,
121    pub width: u32,
122    pub height: u32,
123}
124
125/// serde does not work with Vec.
126/// This requires us to write our own Deserializer for Vec<Touch>.
127/// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277
128impl TouchDef {
129    fn vec<'de, D>(deserializer: D) -> Result<Vec<Touch>, D::Error>
130    where
131        D: Deserializer<'de>,
132    {
133        #[derive(Deserialize)]
134        struct Wrapper(#[serde(deserialize_with = "deserialize_element")] Touch);
135
136        fn deserialize_element<'de, D>(deserializer: D) -> Result<Touch, D::Error>
137        where
138            D: Deserializer<'de>,
139        {
140            TouchDef::deserialize(deserializer)
141        }
142
143        let v = Vec::deserialize(deserializer)?;
144        Ok(v.into_iter().map(|Wrapper(a)| a).collect())
145    }
146}