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//!                 if data.shift() { println!("Shift is being held!") }
36//!
37//!                 match data.key {
38//!                    Printable(c) if u16::from(c) == 0x0D => print!("\r\n"),
39//!                    Printable(c) if u16::from(c) >= 0x20 => print!("{}", c),
40//!                    Special(code) if code == ScanCode::ESCAPE => {
41//!                        println!("Exiting...");
42//!                        return Ok(())
43//!                    },
44//!                    _ => {}
45//!                }
46//!             }
47//!         }
48//!         Ok(())
49//!     }).unwrap();
50//!
51//!     Status::SUCCESS
52//! }
53//! ```
54
55#![no_std]
56
57pub mod simple_text_input_ex;
58pub mod input;
59pub mod key_data;
60
61use uefi::boot::{get_handle_for_protocol, open_protocol_exclusive};
62use uefi::Result;
63use crate::input::Input;
64
65/// it has roughly the same function as `uefi::system::with_stdin`.
66pub fn with_stdin<F, R>(mut f: F) -> Result<R>
67where
68    F: FnMut(&mut Input) -> Result<R>
69{
70    let input = get_handle_for_protocol::<Input>()?;
71    let mut input = open_protocol_exclusive::<Input>(input)?;
72
73    f(&mut *input)
74}