kernel/device/input/
mod.rs

1//! Input device module
2//!
3//! This module provides input event handling for devices like keyboards, mice,
4//! touchscreens, etc. It follows the Linux input event model conceptually but
5//! uses Rust-friendly types (u64 timestamps instead of timeval).
6
7use core::mem::size_of;
8
9pub mod event_device;
10
11/// Input event structure
12///
13/// Conceptually similar to Linux's input_event, but with a Rust-friendly
14/// u64 timestamp (nanoseconds since boot) instead of timeval.
15/// The type/code/value model is preserved for compatibility.
16#[repr(C)]
17#[derive(Debug, Clone, Copy, Default)]
18pub struct InputEvent {
19    /// Timestamp in nanoseconds since boot
20    pub time: u64,
21    /// Event type (EV_KEY, EV_REL, etc.)
22    pub type_: u16,
23    /// Event code (KEY_A, REL_X, etc.)
24    pub code: u16,
25    /// Event value (1/0 for keys, movement delta for relative axes, etc.)
26    pub value: i32,
27}
28
29impl InputEvent {
30    /// Get the size of InputEvent structure for read operations
31    pub const fn size() -> usize {
32        size_of::<Self>()
33    }
34
35    /// Create a new input event with the current timestamp
36    pub fn new(type_: u16, code: u16, value: i32) -> Self {
37        Self {
38            time: crate::time::current_time_ns(),
39            type_,
40            code,
41            value,
42        }
43    }
44}
45
46/// Event type constants
47///
48/// These values match Linux's input event types for compatibility.
49pub mod event_types {
50    /// Synchronization events
51    pub const EV_SYN: u16 = 0x00;
52    /// Key/button press and release events
53    pub const EV_KEY: u16 = 0x01;
54    /// Relative axis movement (e.g., mouse movement)
55    pub const EV_REL: u16 = 0x02;
56    /// Absolute axis position (e.g., touchscreen)
57    pub const EV_ABS: u16 = 0x03;
58    /// Miscellaneous events
59    pub const EV_MSC: u16 = 0x04;
60    /// LED state changes
61    pub const EV_LED: u16 = 0x11;
62    /// Sound events
63    pub const EV_SND: u16 = 0x12;
64}
65
66/// Synchronization event codes
67pub mod syn_codes {
68    /// Synchronization marker - separates event packets
69    pub const SYN_REPORT: u16 = 0;
70}
71
72/// Relative axis codes
73pub mod rel_codes {
74    /// Relative X axis (horizontal movement)
75    pub const REL_X: u16 = 0x00;
76    /// Relative Y axis (vertical movement)
77    pub const REL_Y: u16 = 0x01;
78    /// Relative Z axis
79    pub const REL_Z: u16 = 0x02;
80    /// Mouse wheel
81    pub const REL_WHEEL: u16 = 0x08;
82}
83
84/// Key/button codes (selected common ones)
85///
86/// For a complete list, refer to Linux's input-event-codes.h
87pub mod key_codes {
88    // Mouse buttons
89    pub const BTN_LEFT: u16 = 0x110;
90    pub const BTN_RIGHT: u16 = 0x111;
91    pub const BTN_MIDDLE: u16 = 0x112;
92
93    // Keyboard keys (examples)
94    pub const KEY_ESC: u16 = 1;
95    pub const KEY_1: u16 = 2;
96    pub const KEY_2: u16 = 3;
97    pub const KEY_A: u16 = 30;
98    pub const KEY_B: u16 = 48;
99    pub const KEY_SPACE: u16 = 57;
100    pub const KEY_ENTER: u16 = 28;
101    pub const KEY_LEFTSHIFT: u16 = 42;
102    pub const KEY_RIGHTSHIFT: u16 = 54;
103}
104
105/// Key/button state values
106pub mod key_values {
107    /// Key released
108    pub const KEY_RELEASE: i32 = 0;
109    /// Key pressed
110    pub const KEY_PRESS: i32 = 1;
111    /// Key held (auto-repeat)
112    pub const KEY_REPEAT: i32 = 2;
113}