1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use crate::defs::{
16     EfiGuid, EfiInputKey, EfiSimpleTextInputProtocol, EFI_STATUS_NOT_FOUND, EFI_STATUS_NOT_READY,
17 };
18 use crate::protocol::{Protocol, ProtocolInfo};
19 use crate::{efi_call, map_efi_err, EfiResult};
20 
21 /// EFI_SIMPLE_TEXT_INPUT_PROTOCOL
22 pub struct SimpleTextInputProtocol;
23 
24 impl ProtocolInfo for SimpleTextInputProtocol {
25     type InterfaceType = EfiSimpleTextInputProtocol;
26 
27     const GUID: EfiGuid =
28         EfiGuid::new(0x387477c1, 0x69c7, 0x11d2, [0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b]);
29 }
30 
31 impl Protocol<'_, SimpleTextInputProtocol> {
32     /// Wrapper of `EFI_SIMPLE_TEXT_INPUT_PROTOCOL.reset()`
reset(&self, extendend_verification: bool) -> EfiResult<()>33     pub fn reset(&self, extendend_verification: bool) -> EfiResult<()> {
34         // SAFETY:
35         // `self.interface()?` guarantees `self.interface` is non-null and points to a valid object
36         // established by `Protocol::new()`.
37         // `self.interface` is input parameter and will not be retained. It outlives the call.
38         unsafe { efi_call!(self.interface()?.reset, self.interface, extendend_verification) }
39     }
40 
41     /// Wrapper of `EFI_SIMPLE_TEXT_INPUT_PROTOCOL.read_key_stroke()`
42     ///
43     /// Returns `Ok(Some(EfiInputKey))` if there is a key stroke, Ok(None) if no key stroke is
44     /// pressed.
read_key_stroke(&self) -> EfiResult<Option<EfiInputKey>>45     pub fn read_key_stroke(&self) -> EfiResult<Option<EfiInputKey>> {
46         let mut key: EfiInputKey = Default::default();
47         // SAFETY:
48         // `self.interface()?` guarantees `self.interface` is non-null and points to a valid object
49         // established by `Protocol::new()`.
50         // `self.interface` is input parameter and will not be retained. It outlives the call.
51         // `key` is an output argument. It outlives the call and will not be taken.
52         match unsafe { efi_call!(self.interface()?.read_key_stroke, self.interface, &mut key) } {
53             Ok(()) => Ok(Some(key)),
54             Err(e) if e.is_efi_err(EFI_STATUS_NOT_READY) => Ok(None),
55             Err(e) => Err(e),
56         }
57     }
58 }
59