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(&mut self, active: bool) -> Pin<Box<dyn '_ + Future<Output = ()> + Send>>;
51}
52
53/// A resolved [`InputSource`].
54pub struct ResolvedInputSource<T>(pub Box<dyn InputSource<T>>);
55
56impl<T: 'static + InputSource<KeyboardData>> From<T> for ResolvedInputSource<KeyboardData> {
57    fn from(value: T) -> Self {
58        Self(Box::new(value))
59    }
60}
61
62impl<T: 'static + InputSource<MouseData>> From<T> for ResolvedInputSource<MouseData> {
63    fn from(value: T) -> Self {
64        Self(Box::new(value))
65    }
66}
67
68impl CanResolveTo<ResolvedInputSource<KeyboardData>> for KeyboardInputHandleKind {
69    type Input<'a> = &'a str;
70}
71
72impl CanResolveTo<ResolvedInputSource<MouseData>> for MouseInputHandleKind {
73    type Input<'a> = &'a str;
74}
75
76/// An input handle for input multiplexed over an input channel serving multiple
77/// devices.
78#[derive(MeshPayload)]
79pub struct MultiplexedInputHandle {
80    /// The elevation of this device on the input stack. The active device with
81    /// the highest elevation will receive the input.
82    ///
83    /// Each device must have a distinct elevation.
84    pub elevation: usize,
85}
86
87impl ResourceId<KeyboardInputHandleKind> for MultiplexedInputHandle {
88    const ID: &'static str = "keyboard";
89}
90
91impl ResourceId<MouseInputHandleKind> for MultiplexedInputHandle {
92    const ID: &'static str = "mouse";
93}