1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Common input device-related definitions.

#![forbid(unsafe_code)]
#![warn(missing_docs)]

pub mod mesh_input;

use mesh::MeshPayload;
use std::pin::Pin;
use vm_resource::kind::KeyboardInputHandleKind;
use vm_resource::kind::MouseInputHandleKind;
use vm_resource::CanResolveTo;
use vm_resource::ResourceId;

/// Keyboard or mouse input data.
#[derive(Debug, Copy, Clone, MeshPayload)]
pub enum InputData {
    /// A keystoke.
    Keyboard(KeyboardData),
    /// A mouse move or click.
    Mouse(MouseData),
}

/// A mouse input event.
#[derive(Debug, Copy, Clone, MeshPayload)]
pub struct MouseData {
    /// A bitmask of the buttons that are pressed.
    pub button_mask: u8,
    /// The absolute X location.
    pub x: u16,
    /// The absolute Y location.
    pub y: u16,
}

/// A keyboard input event.
#[derive(Debug, Copy, Clone, MeshPayload)]
pub struct KeyboardData {
    /// Keyboard code.
    pub code: u16,
    /// True if this is a "make", false if it is a "break".
    pub make: bool,
}

/// Trait implemented by input sources.
pub trait InputSource<T>: futures::Stream<Item = T> + Unpin + Send {
    /// Sets this input source active, so that the sending side can choose which
    /// device to send input to.
    fn set_active(
        &mut self,
        active: bool,
    ) -> Pin<Box<dyn '_ + std::future::Future<Output = ()> + Send>>;
}

/// A resolved [`InputSource`].
pub struct ResolvedInputSource<T>(pub Box<dyn InputSource<T>>);

impl<T: 'static + InputSource<KeyboardData>> From<T> for ResolvedInputSource<KeyboardData> {
    fn from(value: T) -> Self {
        Self(Box::new(value))
    }
}

impl<T: 'static + InputSource<MouseData>> From<T> for ResolvedInputSource<MouseData> {
    fn from(value: T) -> Self {
        Self(Box::new(value))
    }
}

impl CanResolveTo<ResolvedInputSource<KeyboardData>> for KeyboardInputHandleKind {
    type Input<'a> = &'a str;
}

impl CanResolveTo<ResolvedInputSource<MouseData>> for MouseInputHandleKind {
    type Input<'a> = &'a str;
}

/// An input handle for input multiplexed over an input channel serving multiple
/// devices.
#[derive(MeshPayload)]
pub struct MultiplexedInputHandle {
    /// The elevation of this device on the input stack. The active device with
    /// the highest elevation will receive the input.
    ///
    /// Each device must have a distinct elevation.
    pub elevation: usize,
}

impl ResourceId<KeyboardInputHandleKind> for MultiplexedInputHandle {
    const ID: &'static str = "keyboard";
}

impl ResourceId<MouseInputHandleKind> for MultiplexedInputHandle {
    const ID: &'static str = "mouse";
}