uefi_input2/
simple_text_input_ex.rs

1// Copyright (c) Bemly, January 2026
2// You may copy and distribute this file freely.  Any queries and
3// complaints should be forwarded to bemly_@petalmail.com.
4// If you make any changes to this file, please do not distribute
5// the results under the name `bemly'.
6
7use core::ffi::c_void;
8pub use uefi_raw::Boolean;
9use uefi_raw::{guid, Event, Guid, Status};
10pub use uefi_raw::protocol::console::InputKey;
11
12pub type KeyToggleState = u8;
13/// Keyboard states:
14/// including Shift/Alt/Ctrl states and Caps Lock flags.
15#[repr(C)]
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub struct KeyState {
18    /// The bitmask here is determined using the SHIFT_STATE constant below.
19    pub key_shift_state: u32,
20    /// TOGGLE_STATE
21    pub key_toggle_state: KeyToggleState,
22}
23
24/// C FFI key data structure
25#[repr(C)]
26#[derive(Clone, Copy, Debug, Default)]
27pub struct RawKeyData {
28    pub key: InputKey,
29    pub key_state: KeyState,
30}
31
32// Protocol constant definition
33// Key combination status mask
34pub const RIGHT_SHIFT_PRESSED: u32   = 0b0000_0000_0000_0000_0000_00__0000000001;
35pub const LEFT_SHIFT_PRESSED: u32    = 0b0000_0000_0000_0000_0000_00__0000000010;
36pub const RIGHT_CONTROL_PRESSED: u32 = 0b0000_0000_0000_0000_0000_00__0000000100;
37pub const LEFT_CONTROL_PRESSED: u32  = 0b0000_0000_0000_0000_0000_00__0000001000;
38pub const RIGHT_ALT_PRESSED: u32     = 0b0000_0000_0000_0000_0000_00__0000010000;
39pub const LEFT_ALT_PRESSED: u32      = 0b0000_0000_0000_0000_0000_00__0000100000;
40pub const RIGHT_LOGO_PRESSED: u32    = 0b0000_0000_0000_0000_0000_00__0001000000;
41pub const LEFT_LOGO_PRESSED: u32     = 0b0000_0000_0000_0000_0000_00__0010000000;
42pub const MENU_KEY_PRESSED: u32      = 0b0000_0000_0000_0000_0000_00__0100000000;
43pub const SYS_REQ_PRESSED: u32       = 0b0000_0000_0000_0000_0000_00__1000000000;
44pub const SHIFT_STATE_VALID: u32     = 0b1000_0000_0000_0000_0000_00__0000000000;
45
46// Toggle state mask
47pub const SCROLL_LOCK_ACTIVE: u8     = 0b0000_0001;
48pub const NUM_LOCK_ACTIVE: u8        = 0b0000_0010;
49pub const CAPS_LOCK_ACTIVE: u8       = 0b0000_0100;
50pub const KEY_STATE_EXPOSED: u8      = 0b0100_0000;
51pub const TOGGLE_STATE_VALID: u8     = 0b1000_0000;
52
53// Protocol interface definition
54/// Key notification callback function type
55pub type KeyNotifyFunction = unsafe extern "efiapi" fn(key_data: *mut RawKeyData) -> Status;
56
57/// EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL:
58/// allows obtaining modifier key (Shift/Alt/Ctrl) states.
59#[derive(Debug)]
60#[repr(C)]
61pub struct SimpleTextInputExProtocol {
62    /// Resets the input device hardware.
63    pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: Boolean) -> Status,
64
65    /// Reads key data (including KeyState).
66    pub read_key_stroke_ex: unsafe extern "efiapi" fn(this: *mut Self, key_data: *mut RawKeyData) -> Status,
67
68    /// Event for waiting for a key press.
69    pub wait_for_key_ex: Event,
70
71    /// Sets the keyboard indicator light state (e.g., CapsLock).
72    pub set_state: unsafe extern "efiapi" fn(this: *mut Self, key_toggle_state: *mut KeyToggleState) -> Status,
73
74    /// Registers a key notification function, triggered when a specific key is pressed.
75    pub register_key_notify: unsafe extern "efiapi" fn(
76        this: *mut Self,
77        key_data: *mut RawKeyData,
78        key_notification_function: KeyNotifyFunction,
79        notify_handle: *mut *mut c_void,
80    ) -> Status,
81
82    /// Unregisters a key notification.
83    pub unregister_key_notify: unsafe extern "efiapi" fn(this: *mut Self, notification_handle: *mut c_void) -> Status,
84}
85
86impl SimpleTextInputExProtocol {
87    pub const GUID: Guid = guid!("dd9e7534-7762-4698-8c14-f58517a625aa");
88}