uefi_input2/lib.rs
1//! # UEFI Simple Text Input Ex Protocol Wrapper
2//!
3//! This library provides a safe, idiomatic Rust wrapper for the `EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL`.
4//! Unlike the standard `SimpleTextInput`, this protocol allows for advanced key tracking,
5//! including shift state (Ctrl, Alt, Shift) and toggle state (Caps Lock, Num Lock).
6//!
7//! ## Features
8//! - **Seamless Migration**: Designed as a **drop-in, painless replacement**
9//! for the standard `uefi::system::with_stdin`.
10//! - **Safe Resource Management**: Uses the `with_stdin` pattern to ensure the protocol is opened
11//! exclusively and closed automatically.
12//! - **Extended Key Data**: Access to `KeyShiftState` and `KeyToggleState`.
13//! - **No-Std Compatible**: Designed specifically for UEFI environments.
14//!
15//! ## Usage
16//! Simply replace your import and use the same closure-based pattern:
17//!
18//! ```rust,no_run
19//! #![no_main]
20//! #![no_std]
21//! use uefi::prelude::*;
22//! use uefi::{print, println};
23//! use uefi::proto::console::text::Key::{Printable, Special};
24//! use uefi::proto::console::text::ScanCode;
25//! use uefi_input2::with_stdin;
26//! use uefi_input2::simple_text_input_ex::{LEFT_SHIFT_PRESSED, RIGHT_SHIFT_PRESSED};
27//!
28//! #[entry]
29//! fn main() -> Status {
30//! uefi::helpers::init().unwrap();
31//!
32//! with_stdin(|input| {
33//! loop {
34//! if let Some(data) = input.read_key_stroke_ex() {
35//! // Check if any Shift key is held
36//! let is_shift = data.key_state.key_shift_state & (LEFT_SHIFT_PRESSED | RIGHT_SHIFT_PRESSED) != 0;
37//! if is_shift { println!("Shift is being held!") }
38//!
39//! match data.key {
40//! Printable(c) if u16::from(c) == 0x0D => print!("\r\n"),
41//! Printable(c) => print!("{}", c),
42//! Special(code) if code == ScanCode::ESCAPE => {
43//! println!("Exiting...");
44//! return Ok(())
45//! },
46//! _ => {}
47//! }
48//! }
49//! }
50//! Ok(())
51//! }).unwrap();
52//!
53//! Status::SUCCESS
54//! }
55//! ```
56
57#![no_std]
58
59pub mod simple_text_input_ex;
60pub mod input;
61
62use uefi::boot::{get_handle_for_protocol, open_protocol_exclusive};
63use uefi::Result;
64use crate::input::Input;
65
66/// it has roughly the same function as `uefi::system::with_stdin`.
67pub fn with_stdin<F, R>(mut f: F) -> Result<R>
68where
69 F: FnMut(&mut Input) -> Result<R>
70{
71 let input = get_handle_for_protocol::<Input>()?;
72 let mut input = open_protocol_exclusive::<Input>(input)?;
73
74 f(&mut *input)
75}