Skip to main content

graphics/input/
data.rs

1use core::mem::transmute;
2
3use crate::{Point, lvgl};
4
5use super::{Key, State};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[repr(C)]
9pub struct InputData {
10    /// Set after consuming an event only if another queued event is ready to read.
11    pub r#continue: bool,
12    pub point: Point,
13    pub state: State,
14    pub key: Key,
15}
16
17impl Default for InputData {
18    fn default() -> Self {
19        Self {
20            point: Point::default(),
21            state: State::default(),
22            key: Key::Character(0),
23            r#continue: false,
24        }
25    }
26}
27
28impl InputData {
29    pub const fn new(point: Point, state: State, key: Key, r#continue: bool) -> Self {
30        Self {
31            point,
32            state,
33            key,
34            r#continue,
35        }
36    }
37
38    pub const fn default_constant() -> Self {
39        Self {
40            point: Point::new(0, 0),
41            state: State::Released,
42            key: Key::Character(0),
43            r#continue: false,
44        }
45    }
46
47    pub const fn get_continue(&self) -> bool {
48        self.r#continue
49    }
50
51    pub const fn get_point(&self) -> &Point {
52        &self.point
53    }
54
55    pub const fn get_touch(&self) -> &State {
56        &self.state
57    }
58
59    pub const fn get_key(&self) -> Key {
60        self.key
61    }
62
63    pub const fn set_continue(&mut self, r#continue: bool) {
64        self.r#continue = r#continue;
65    }
66
67    pub fn set_point(&mut self, point: Point) {
68        self.point = point;
69    }
70
71    pub fn set_state(&mut self, touch: State) {
72        self.state = touch;
73    }
74
75    pub fn set_key(&mut self, key: Key) {
76        self.key = key;
77    }
78
79    pub fn set(&mut self, point: Point, touch: State) {
80        self.point = point;
81        self.state = touch;
82    }
83}
84
85impl TryFrom<&mut [u8]> for &mut InputData {
86    type Error = ();
87
88    fn try_from(value: &mut [u8]) -> Result<Self, Self::Error> {
89        if value.len() != size_of::<InputData>() {
90            return Err(());
91        }
92        if !(value.as_ptr() as usize).is_multiple_of(core::mem::align_of::<InputData>()) {
93            return Err(());
94        }
95
96        #[allow(clippy::transmute_ptr_to_ref)]
97        Ok(unsafe { transmute::<*mut u8, Self>(value.as_mut_ptr()) })
98    }
99}
100
101impl AsMut<[u8]> for InputData {
102    fn as_mut(&mut self) -> &mut [u8] {
103        unsafe { core::slice::from_raw_parts_mut(self as *mut _ as *mut u8, size_of::<Self>()) }
104    }
105}
106
107impl From<InputData> for lvgl::lv_indev_data_t {
108    fn from(value: InputData) -> lvgl::lv_indev_data_t {
109        let mut input_device_data = lvgl::lv_indev_data_t::default();
110
111        let state = value.get_touch();
112
113        let point = value.get_point();
114        input_device_data.point = lvgl::lv_point_t {
115            x: point.x as i32,
116            y: point.y as i32,
117        };
118
119        input_device_data.state = (*state).into();
120        input_device_data.key = value.key.into();
121        input_device_data.continue_reading = value.get_continue();
122
123        input_device_data
124    }
125}