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