uefi/proto/console/pointer/mod.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Pointer device access.
4
5use crate::proto::unsafe_protocol;
6use crate::{Error, Event, Result, Status, StatusExt};
7use uefi_raw::protocol::console::SimplePointerProtocol;
8
9/// Simple Pointer [`Protocol`]. Provides information about a pointer device.
10///
11/// Pointer devices are mouses, touchpads, and touchscreens.
12///
13/// [`Protocol`]: uefi::proto::Protocol
14#[derive(Debug)]
15#[repr(transparent)]
16#[unsafe_protocol(SimplePointerProtocol::GUID)]
17pub struct Pointer(SimplePointerProtocol);
18
19impl Pointer {
20 /// Resets the pointer device hardware.
21 ///
22 /// # Arguments
23 /// The `extended_verification` parameter is used to request that UEFI
24 /// performs an extended check and reset of the input device.
25 ///
26 /// # Errors
27 /// - `DeviceError` if the device is malfunctioning and cannot be reset.
28 pub fn reset(&mut self, extended_verification: bool) -> Result {
29 unsafe { (self.0.reset)(&mut self.0, extended_verification.into()) }.to_result()
30 }
31
32 /// Retrieves the pointer device's current state, if a state change occurred
33 /// since the last time this function was called.
34 ///
35 /// Use `wait_for_input_event()` with the [`boot::wait_for_event`]
36 /// interface in order to wait for input from the pointer device.
37 ///
38 /// # Errors
39 /// - `DeviceError` if there was an issue with the pointer device.
40 ///
41 /// [`boot::wait_for_event`]: crate::boot::wait_for_event
42 pub fn read_state(&mut self) -> Result<Option<PointerState>> {
43 let mut pointer_state = PointerState::default();
44 let pointer_state_ptr: *mut _ = &mut pointer_state;
45
46 match unsafe { (self.0.get_state)(&mut self.0, pointer_state_ptr.cast()) } {
47 Status::NOT_READY => Ok(None),
48 other => other.to_result_with_val(|| Some(pointer_state)),
49 }
50 }
51
52 /// Event to be used with [`boot::wait_for_event`] in order to wait
53 /// for input from the pointer device
54 ///
55 /// [`boot::wait_for_event`]: crate::boot::wait_for_event
56 pub fn wait_for_input_event(&self) -> Result<Event> {
57 unsafe { Event::from_ptr(self.0.wait_for_input) }.ok_or(Error::from(Status::UNSUPPORTED))
58 }
59
60 /// Returns a reference to the pointer device information.
61 #[must_use]
62 pub const fn mode(&self) -> &PointerMode {
63 unsafe { &*self.0.mode.cast() }
64 }
65}
66
67/// Information about this pointer device.
68#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
69#[repr(C)]
70pub struct PointerMode {
71 /// The pointer device's resolution on the X/Y/Z axis in counts/mm.
72 /// If a value is 0, then the device does _not_ support that axis.
73 pub resolution: [u64; 3],
74 /// Whether the devices has a left button / right button.
75 pub has_button: [bool; 2],
76}
77
78/// The relative change in the pointer's state.
79#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
80#[repr(C)]
81pub struct PointerState {
82 /// The relative movement on the X/Y/Z axis.
83 ///
84 /// If `PointerMode` indicates an axis is not supported, it must be ignored.
85 pub relative_movement: [i32; 3],
86 /// Whether the left / right mouse button is currently pressed.
87 ///
88 /// If `PointerMode` indicates a button is not supported, it must be ignored.
89 pub button: [bool; 2],
90}