aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / console / pointer / mod.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Pointer device access. |
| 4 | |
| 5 | pub use uefi_raw::protocol::console::{ |
| 6 | AbsolutePointerMode, AbsolutePointerModeAttributes, AbsolutePointerState, |
| 7 | SimplePointerMode as PointerMode, SimplePointerState as PointerState, |
| 8 | }; |
| 9 | |
| 10 | use crate::proto::unsafe_protocol; |
| 11 | use crate::{Error, Event, Result, Status, StatusExt}; |
| 12 | use uefi_raw::protocol::console::{AbsolutePointerProtocol, SimplePointerProtocol}; |
| 13 | |
| 14 | /// Simple Pointer [`Protocol`]. Provides information about a pointer device. |
| 15 | /// |
| 16 | /// Pointer devices are report relative movement, such as mice. For absolute |
| 17 | /// pointing devices, such as touchscreens, see [`AbsolutePointer`]. |
| 18 | /// |
| 19 | /// [`Protocol`]: uefi::proto::Protocol |
| 20 | #[derive(Debug)] |
| 21 | #[repr(transparent)] |
| 22 | #[unsafe_protocol(SimplePointerProtocol::GUID)] |
| 23 | pub struct Pointer(SimplePointerProtocol); |
| 24 | |
| 25 | impl Pointer { |
| 26 | /// Resets the pointer device hardware. |
| 27 | /// |
| 28 | /// # Arguments |
| 29 | /// The `extended_verification` parameter is used to request that UEFI |
| 30 | /// performs an extended check and reset of the input device. |
| 31 | /// |
| 32 | /// # Errors |
| 33 | /// - `DeviceError` if the device is malfunctioning and cannot be reset. |
| 34 | pub fn reset(&mut self, extended_verification: bool) -> Result { |
| 35 | // SAFETY: We have an exclusive reference to `self`, and `&mut |
| 36 | // self.0` is a valid protocol pointer. |
| 37 | unsafe { (self.0.reset)(&mut self.0, extended_verification.into()) }.to_result() |
| 38 | } |
| 39 | |
| 40 | /// Retrieves the pointer device's current state, if a state change occurred |
| 41 | /// since the last time this function was called. |
| 42 | /// |
| 43 | /// Use `wait_for_input_event()` with the [`boot::wait_for_event`] |
| 44 | /// interface in order to wait for input from the pointer device. |
| 45 | /// |
| 46 | /// # Errors |
| 47 | /// - `DeviceError` if there was an issue with the pointer device. |
| 48 | /// |
| 49 | /// [`boot::wait_for_event`]: crate::boot::wait_for_event |
| 50 | pub fn read_state(&mut self) -> Result<Option<PointerState>> { |
| 51 | let mut state = PointerState::default(); |
| 52 | |
| 53 | // SAFETY: We have an exclusive reference to `self`, `&mut self.0` is a |
| 54 | // valid protocol pointer and `&mut state` is a valid pointer to stack |
| 55 | // memory initialized to receive the output. |
| 56 | match unsafe { (self.0.get_state)(&mut self.0, &mut state) } { |
| 57 | Status::NOT_READY => Ok(None), |
| 58 | other => other.to_result_with_val(|| Some(state)), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Event to be used with [`boot::wait_for_event`] in order to wait |
| 63 | /// for input from the pointer device |
| 64 | /// |
| 65 | /// [`boot::wait_for_event`]: crate::boot::wait_for_event |
| 66 | pub fn wait_for_input_event(&self) -> Result<Event> { |
| 67 | // SAFETY: |
| 68 | // 1. If null (unsupported), `Event::from_ptr` safely returns `None`. |
| 69 | // 2. If non-null, the UEFI spec guarantees the driver created a valid `EFI_EVENT`. |
| 70 | unsafe { Event::from_ptr(self.0.wait_for_input) }.ok_or(Error::from(Status::UNSUPPORTED)) |
| 71 | } |
| 72 | |
| 73 | /// Returns a reference to the pointer device information. |
| 74 | #[must_use] |
| 75 | pub const fn mode(&self) -> &PointerMode { |
| 76 | // SAFETY: `mode` points to valid, initialized memory for the lifetime |
| 77 | // of the protocol, matching the lifetime of `&self`. |
| 78 | unsafe { &*self.0.mode } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Absolute Pointer [`Protocol`]. Provides coordinate pointing (e.g. touchscreens, tablets). |
| 83 | /// |
| 84 | /// For relative pointing devices such as mice, see [`Pointer`]. |
| 85 | /// |
| 86 | /// [`Protocol`]: uefi::proto::Protocol |
| 87 | #[derive(Debug)] |
| 88 | #[repr(transparent)] |
| 89 | #[unsafe_protocol(AbsolutePointerProtocol::GUID)] |
| 90 | pub struct AbsolutePointer(AbsolutePointerProtocol); |
| 91 | |
| 92 | impl AbsolutePointer { |
| 93 | /// Resets the pointer device hardware. |
| 94 | /// |
| 95 | /// # Arguments |
| 96 | /// The `extended_verification` parameter is used to request that UEFI |
| 97 | /// performs an extended check and reset of the input device. |
| 98 | /// |
| 99 | /// # Errors |
| 100 | /// - `DeviceError` if the device is malfunctioning and cannot be reset. |
| 101 | pub fn reset(&mut self, extended_verification: bool) -> Result { |
| 102 | // SAFETY: We have an exclusive reference to `self`, and `&mut |
| 103 | // self.0` is a valid protocol pointer. |
| 104 | unsafe { (self.0.reset)(&mut self.0, extended_verification.into()) }.to_result() |
| 105 | } |
| 106 | |
| 107 | /// Retrieves the pointer device's current state, if a state change occurred |
| 108 | /// since the last time this function was called. |
| 109 | /// |
| 110 | /// Use `wait_for_input_event()` with the [`Self::wait_for_input_event`] |
| 111 | /// interface in order to wait for input from the pointer device. |
| 112 | /// |
| 113 | /// # Errors |
| 114 | /// - `DeviceError` if there was an issue with the pointer device. |
| 115 | /// |
| 116 | /// [`boot::wait_for_event`]: crate::boot::wait_for_event |
| 117 | pub fn read_state(&mut self) -> Result<Option<AbsolutePointerState>> { |
| 118 | let mut state = AbsolutePointerState::default(); |
| 119 | let state_ptr: *mut _ = &mut state; |
| 120 | |
| 121 | // SAFETY: We have an exclusive reference to `self`, `&mut self.0` is a |
| 122 | // valid protocol pointer and `state_ptr` is a valid pointer to stack |
| 123 | // memory initialized to receive the output. |
| 124 | match unsafe { (self.0.get_state)(&mut self.0, state_ptr) } { |
| 125 | Status::NOT_READY => Ok(None), |
| 126 | other => other.to_result_with_val(|| Some(state)), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Event to be used with [`boot::wait_for_event`] in order to wait |
| 131 | /// for input from the pointer device |
| 132 | /// |
| 133 | /// [`boot::wait_for_event`]: crate::boot::wait_for_event |
| 134 | pub fn wait_for_input_event(&self) -> Result<Event> { |
| 135 | // SAFETY: |
| 136 | // 1. If null (unsupported), `Event::from_ptr` safely returns `None`. |
| 137 | // 2. If non-null, the UEFI spec guarantees the driver created a valid `EFI_EVENT`. |
| 138 | unsafe { Event::from_ptr(self.0.wait_for_input) }.ok_or(Error::from(Status::UNSUPPORTED)) |
| 139 | } |
| 140 | |
| 141 | /// Returns a reference to the pointer device information. |
| 142 | #[must_use] |
| 143 | pub const fn mode(&self) -> &AbsolutePointerMode { |
| 144 | // SAFETY: `mode` points to valid, initialized memory for the lifetime |
| 145 | // of the protocol, matching the lifetime of `&self`. |
| 146 | unsafe { &*self.0.mode } |
| 147 | } |
| 148 | } |