• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium OS 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 
5 use std::collections::BTreeMap;
6 
7 use super::constants::*;
8 use super::virtio_input_absinfo;
9 use super::virtio_input_bitmap;
10 use super::virtio_input_device_ids;
11 use super::VirtioInputConfig;
12 
13 /// Instantiates a VirtioInputConfig object with the default configuration for a trackpad. It
14 /// supports touch, left button and right button events, as well as X and Y axis.
new_trackpad_config(width: u32, height: u32) -> VirtioInputConfig15 pub fn new_trackpad_config(width: u32, height: u32) -> VirtioInputConfig {
16     VirtioInputConfig::new(
17         virtio_input_device_ids::new(0, 0, 0, 0),
18         b"Crosvm Virtio Trackpad".to_vec(),
19         b"virtio-trackpad".to_vec(),
20         virtio_input_bitmap::new([0u8; 128]),
21         default_trackpad_events(),
22         default_trackpad_absinfo(width, height),
23     )
24 }
25 
26 /// Instantiates a VirtioInputConfig object with the default configuration for a mouse.
27 /// It supports left, right and middle buttons, as wel as X, Y and wheel relative axes.
new_mouse_config() -> VirtioInputConfig28 pub fn new_mouse_config() -> VirtioInputConfig {
29     VirtioInputConfig::new(
30         virtio_input_device_ids::new(0, 0, 0, 0),
31         b"Crosvm Virtio Mouse".to_vec(),
32         b"virtio-mouse".to_vec(),
33         virtio_input_bitmap::new([0u8; 128]),
34         default_mouse_events(),
35         BTreeMap::new(),
36     )
37 }
38 
39 /// Instantiates a VirtioInputConfig object with the default configuration for a keyboard.
40 /// It supports the same keys as a en-us keyboard and the CAPSLOCK, NUMLOCK and SCROLLLOCK leds.
new_keyboard_config() -> VirtioInputConfig41 pub fn new_keyboard_config() -> VirtioInputConfig {
42     VirtioInputConfig::new(
43         virtio_input_device_ids::new(0, 0, 0, 0),
44         b"Crosvm Virtio Keyboard".to_vec(),
45         b"virtio-keyboard".to_vec(),
46         virtio_input_bitmap::new([0u8; 128]),
47         default_keyboard_events(),
48         BTreeMap::new(),
49     )
50 }
51 
52 /// Instantiates a VirtioInputConfig object with the default configuration for a collection of switches.
new_switches_config() -> VirtioInputConfig53 pub fn new_switches_config() -> VirtioInputConfig {
54     VirtioInputConfig::new(
55         virtio_input_device_ids::new(0, 0, 0, 0),
56         b"Crosvm Virtio Switches".to_vec(),
57         b"virtio-switches".to_vec(),
58         virtio_input_bitmap::new([0u8; 128]),
59         default_switch_events(),
60         BTreeMap::new(),
61     )
62 }
63 
64 /// Instantiates a VirtioInputConfig object with the default configuration for a touchscreen (no
65 /// multitouch support).
new_single_touch_config(width: u32, height: u32) -> VirtioInputConfig66 pub fn new_single_touch_config(width: u32, height: u32) -> VirtioInputConfig {
67     VirtioInputConfig::new(
68         virtio_input_device_ids::new(0, 0, 0, 0),
69         b"Crosvm Virtio Touchscreen".to_vec(),
70         b"virtio-touchscreen".to_vec(),
71         virtio_input_bitmap::from_bits(&[INPUT_PROP_DIRECT]),
72         default_touchscreen_events(),
73         default_touchscreen_absinfo(width, height),
74     )
75 }
76 
77 /// Instantiates a VirtioInputConfig object with the default configuration for a multitouch
78 /// touchscreen.
new_multi_touch_config(width: u32, height: u32) -> VirtioInputConfig79 pub fn new_multi_touch_config(width: u32, height: u32) -> VirtioInputConfig {
80     VirtioInputConfig::new(
81         virtio_input_device_ids::new(0, 0, 0, 0),
82         b"Crosvm Virtio Multitouch Touchscreen".to_vec(),
83         b"virtio-touchscreen".to_vec(),
84         virtio_input_bitmap::from_bits(&[INPUT_PROP_DIRECT]),
85         default_multitouchscreen_events(),
86         default_multitouchscreen_absinfo(width, height, 10, 10),
87     )
88 }
89 
default_touchscreen_absinfo(width: u32, height: u32) -> BTreeMap<u16, virtio_input_absinfo>90 fn default_touchscreen_absinfo(width: u32, height: u32) -> BTreeMap<u16, virtio_input_absinfo> {
91     let mut absinfo: BTreeMap<u16, virtio_input_absinfo> = BTreeMap::new();
92     absinfo.insert(ABS_X, virtio_input_absinfo::new(0, width, 0, 0));
93     absinfo.insert(ABS_Y, virtio_input_absinfo::new(0, height, 0, 0));
94     absinfo
95 }
96 
default_touchscreen_events() -> BTreeMap<u16, virtio_input_bitmap>97 fn default_touchscreen_events() -> BTreeMap<u16, virtio_input_bitmap> {
98     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
99     supported_events.insert(EV_KEY, virtio_input_bitmap::from_bits(&[BTN_TOUCH]));
100     supported_events.insert(EV_ABS, virtio_input_bitmap::from_bits(&[ABS_X, ABS_Y]));
101     supported_events
102 }
103 
default_multitouchscreen_absinfo( width: u32, height: u32, slot: u32, id: u32, ) -> BTreeMap<u16, virtio_input_absinfo>104 fn default_multitouchscreen_absinfo(
105     width: u32,
106     height: u32,
107     slot: u32,
108     id: u32,
109 ) -> BTreeMap<u16, virtio_input_absinfo> {
110     let mut absinfo: BTreeMap<u16, virtio_input_absinfo> = BTreeMap::new();
111     absinfo.insert(ABS_MT_SLOT, virtio_input_absinfo::new(0, slot, 0, 0));
112     absinfo.insert(ABS_MT_TRACKING_ID, virtio_input_absinfo::new(0, id, 0, 0));
113     absinfo.insert(ABS_MT_POSITION_X, virtio_input_absinfo::new(0, width, 0, 0));
114     absinfo.insert(
115         ABS_MT_POSITION_Y,
116         virtio_input_absinfo::new(0, height, 0, 0),
117     );
118     absinfo
119 }
120 
default_multitouchscreen_events() -> BTreeMap<u16, virtio_input_bitmap>121 fn default_multitouchscreen_events() -> BTreeMap<u16, virtio_input_bitmap> {
122     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
123     supported_events.insert(EV_KEY, virtio_input_bitmap::from_bits(&[BTN_TOUCH]));
124     supported_events.insert(
125         EV_ABS,
126         virtio_input_bitmap::from_bits(&[
127             ABS_MT_SLOT,
128             ABS_MT_TRACKING_ID,
129             ABS_MT_POSITION_X,
130             ABS_MT_POSITION_Y,
131         ]),
132     );
133     supported_events
134 }
135 
default_trackpad_absinfo(width: u32, height: u32) -> BTreeMap<u16, virtio_input_absinfo>136 fn default_trackpad_absinfo(width: u32, height: u32) -> BTreeMap<u16, virtio_input_absinfo> {
137     let mut absinfo: BTreeMap<u16, virtio_input_absinfo> = BTreeMap::new();
138     absinfo.insert(ABS_X, virtio_input_absinfo::new(0, width, 0, 0));
139     absinfo.insert(ABS_Y, virtio_input_absinfo::new(0, height, 0, 0));
140     absinfo
141 }
142 
default_trackpad_events() -> BTreeMap<u16, virtio_input_bitmap>143 fn default_trackpad_events() -> BTreeMap<u16, virtio_input_bitmap> {
144     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
145     supported_events.insert(
146         EV_KEY,
147         virtio_input_bitmap::from_bits(&[BTN_TOOL_FINGER, BTN_TOUCH, BTN_LEFT, BTN_RIGHT]),
148     );
149     supported_events.insert(EV_ABS, virtio_input_bitmap::from_bits(&[ABS_X, ABS_Y]));
150     supported_events
151 }
152 
default_mouse_events() -> BTreeMap<u16, virtio_input_bitmap>153 fn default_mouse_events() -> BTreeMap<u16, virtio_input_bitmap> {
154     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
155     supported_events.insert(
156         EV_KEY,
157         virtio_input_bitmap::from_bits(&[BTN_LEFT, BTN_RIGHT, BTN_MIDDLE]),
158     );
159     supported_events.insert(
160         EV_REL,
161         virtio_input_bitmap::from_bits(&[REL_X, REL_Y, REL_WHEEL]),
162     );
163     supported_events
164 }
165 
default_keyboard_events() -> BTreeMap<u16, virtio_input_bitmap>166 fn default_keyboard_events() -> BTreeMap<u16, virtio_input_bitmap> {
167     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
168     supported_events.insert(
169         EV_KEY,
170         virtio_input_bitmap::from_bits(&[
171             KEY_ESC,
172             KEY_1,
173             KEY_2,
174             KEY_3,
175             KEY_4,
176             KEY_5,
177             KEY_6,
178             KEY_7,
179             KEY_8,
180             KEY_9,
181             KEY_0,
182             KEY_MINUS,
183             KEY_EQUAL,
184             KEY_BACKSPACE,
185             KEY_TAB,
186             KEY_Q,
187             KEY_W,
188             KEY_E,
189             KEY_R,
190             KEY_T,
191             KEY_Y,
192             KEY_U,
193             KEY_I,
194             KEY_O,
195             KEY_P,
196             KEY_LEFTBRACE,
197             KEY_RIGHTBRACE,
198             KEY_ENTER,
199             KEY_LEFTCTRL,
200             KEY_A,
201             KEY_S,
202             KEY_D,
203             KEY_F,
204             KEY_G,
205             KEY_H,
206             KEY_J,
207             KEY_K,
208             KEY_L,
209             KEY_SEMICOLON,
210             KEY_APOSTROPHE,
211             KEY_GRAVE,
212             KEY_LEFTSHIFT,
213             KEY_BACKSLASH,
214             KEY_Z,
215             KEY_X,
216             KEY_C,
217             KEY_V,
218             KEY_B,
219             KEY_N,
220             KEY_M,
221             KEY_COMMA,
222             KEY_DOT,
223             KEY_SLASH,
224             KEY_RIGHTSHIFT,
225             KEY_KPASTERISK,
226             KEY_LEFTALT,
227             KEY_SPACE,
228             KEY_CAPSLOCK,
229             KEY_F1,
230             KEY_F2,
231             KEY_F3,
232             KEY_F4,
233             KEY_F5,
234             KEY_F6,
235             KEY_F7,
236             KEY_F8,
237             KEY_F9,
238             KEY_F10,
239             KEY_NUMLOCK,
240             KEY_SCROLLLOCK,
241             KEY_KP7,
242             KEY_KP8,
243             KEY_KP9,
244             KEY_KPMINUS,
245             KEY_KP4,
246             KEY_KP5,
247             KEY_KP6,
248             KEY_KPPLUS,
249             KEY_KP1,
250             KEY_KP2,
251             KEY_KP3,
252             KEY_KP0,
253             KEY_KPDOT,
254             KEY_F11,
255             KEY_F12,
256             KEY_KPENTER,
257             KEY_RIGHTCTRL,
258             KEY_KPSLASH,
259             KEY_SYSRQ,
260             KEY_RIGHTALT,
261             KEY_HOME,
262             KEY_UP,
263             KEY_PAGEUP,
264             KEY_LEFT,
265             KEY_RIGHT,
266             KEY_END,
267             KEY_DOWN,
268             KEY_PAGEDOWN,
269             KEY_INSERT,
270             KEY_DELETE,
271             KEY_PAUSE,
272             KEY_MENU,
273             KEY_PRINT,
274             KEY_POWER,
275             KEY_HOMEPAGE,
276             KEY_MUTE,
277             KEY_VOLUMEDOWN,
278             KEY_VOLUMEUP,
279         ]),
280     );
281     supported_events.insert(
282         EV_REP,
283         virtio_input_bitmap::from_bits(&[REP_DELAY, REP_PERIOD]),
284     );
285     supported_events.insert(
286         EV_LED,
287         virtio_input_bitmap::from_bits(&[LED_CAPSL, LED_NUML, LED_SCROLLL]),
288     );
289     supported_events
290 }
291 
default_switch_events() -> BTreeMap<u16, virtio_input_bitmap>292 fn default_switch_events() -> BTreeMap<u16, virtio_input_bitmap> {
293     let mut supported_events: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();
294     supported_events.insert(
295         EV_SW,
296         virtio_input_bitmap::from_bits(&[
297             SW_LID,
298             SW_TABLET_MODE,
299             SW_HEADPHONE_INSERT,
300             SW_RFKILL_ALL,
301             SW_MICROPHONE_INSERT,
302             SW_DOCK,
303             SW_LINEOUT_INSERT,
304             SW_JACK_PHYSICAL_INSERT,
305             SW_VIDEOOUT_INSERT,
306             SW_CAMERA_LENS_COVER,
307             SW_KEYPAD_SLIDE,
308             SW_FRONT_PROXIMITY,
309             SW_ROTATE_LOCK,
310             SW_LINEIN_INSERT,
311             SW_MUTE_DEVICE,
312             SW_PEN_INSERTED,
313             SW_MACHINE_COVER,
314         ]),
315     );
316     supported_events
317 }
318 
319 #[cfg(test)]
320 mod tests {
321     use super::*;
322 
323     #[test]
test_new_switches_config()324     fn test_new_switches_config() {
325         let config = new_switches_config();
326         assert_eq!(config.serial_name, b"virtio-switches".to_vec());
327 
328         let events = config.supported_events;
329         assert_eq!(events.len(), 1);
330         assert_eq!(events.contains_key(&EV_SW), true);
331 
332         // The bitmap should contain SW_CNT=0x10+1=17 ones,
333         // where each one is packed into the u8 bitmap.
334         let mut expected_bitmap = [0 as u8; 128];
335         expected_bitmap[0] = 0b11111111u8;
336         expected_bitmap[1] = 0b11111111u8;
337         expected_bitmap[2] = 0b1u8;
338         assert_eq!(events[&EV_SW].bitmap, expected_bitmap);
339     }
340 }
341