input_core/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Common input device-related definitions.
5
6#![forbid(unsafe_code)]
7
8pub mod mesh_input;
9
10use mesh::MeshPayload;
11use std::pin::Pin;
12use vm_resource::CanResolveTo;
13use vm_resource::ResourceId;
14use vm_resource::kind::KeyboardInputHandleKind;
15use vm_resource::kind::MouseInputHandleKind;
16
17/// Keyboard or mouse input data.
18#[derive(Debug, Copy, Clone, MeshPayload)]
19pub enum InputData {
20    /// A keystoke.
21    Keyboard(KeyboardData),
22    /// A mouse move or click.
23    Mouse(MouseData),
24}
25
26/// A mouse input event.
27#[derive(Debug, Copy, Clone, MeshPayload)]
28pub struct MouseData {
29    /// A bitmask of the buttons that are pressed.
30    pub button_mask: u8,
31    /// The absolute X location.
32    pub x: u16,
33    /// The absolute Y location.
34    pub y: u16,
35}
36
37/// A keyboard input event.
38#[derive(Debug, Copy, Clone, MeshPayload)]
39pub struct KeyboardData {
40    /// Keyboard code.
41    pub code: u16,
42    /// True if this is a "make", false if it is a "break".
43    pub make: bool,
44}
45
46/// Trait implemented by input sources.
47pub trait InputSource<T>: futures::Stream<Item = T> + Unpin + Send {
48    /// Sets this input source active, so that the sending side can choose which
49    /// device to send input to.
50    fn set_active(
51        &mut self,
52        active: bool,
53    ) -> Pin<Box<dyn '_ + std::future::Future<Output = ()> + Send>>;
54}
55
56/// A resolved [`InputSource`].
57pub struct ResolvedInputSource<T>(pub Box<dyn InputSource<T>>);
58
59impl<T: 'static + InputSource<KeyboardData>> From<T> for ResolvedInputSource<KeyboardData> {
60    fn from(value: T) -> Self {
61        Self(Box::new(value))
62    }
63}
64
65impl<T: 'static + InputSource<MouseData>> From<T> for ResolvedInputSource<MouseData> {
66    fn from(value: T) -> Self {
67        Self(Box::new(value))
68    }
69}
70
71impl CanResolveTo<ResolvedInputSource<KeyboardData>> for KeyboardInputHandleKind {
72    type Input<'a> = &'a str;
73}
74
75impl CanResolveTo<ResolvedInputSource<MouseData>> for MouseInputHandleKind {
76    type Input<'a> = &'a str;
77}
78
79/// An input handle for input multiplexed over an input channel serving multiple
80/// devices.
81#[derive(MeshPayload)]
82pub struct MultiplexedInputHandle {
83    /// The elevation of this device on the input stack. The active device with
84    /// the highest elevation will receive the input.
85    ///
86    /// Each device must have a distinct elevation.
87    pub elevation: usize,
88}
89
90impl ResourceId<KeyboardInputHandleKind> for MultiplexedInputHandle {
91    const ID: &'static str = "keyboard";
92}
93
94impl ResourceId<MouseInputHandleKind> for MultiplexedInputHandle {
95    const ID: &'static str = "mouse";
96}