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