graphics/input/
input.rs

1use alloc::boxed::Box;
2
3use core::ffi::c_void;
4
5use file_system::Device;
6
7use crate::{Result, lvgl};
8
9use super::{InputKind, UserData, binding_callback_function};
10
11pub struct Input {
12    #[allow(dead_code)]
13    input_device: *mut lvgl::lv_indev_t,
14}
15
16impl Drop for Input {
17    fn drop(&mut self) {
18        unsafe {
19            let _ = Box::from_raw(lvgl::lv_indev_get_user_data(self.input_device) as *mut UserData);
20
21            lvgl::lv_indev_delete(self.input_device);
22
23            // User_data will be dropped here.
24        }
25    }
26}
27
28unsafe impl Send for Input {}
29
30unsafe impl Sync for Input {}
31
32impl Input {
33    pub fn new(device: Device, r#type: InputKind) -> Result<Self> {
34        // User_data is a pinned box, so it's ownership can be transferred to LVGL and will not move or dropper until the Input_device is dropped.
35        let user_data = Box::new(UserData { device });
36
37        let input_device = unsafe {
38            let input_device = lvgl::lv_indev_create();
39            lvgl::lv_indev_set_type(input_device, r#type.into());
40            lvgl::lv_indev_set_read_cb(input_device, Some(binding_callback_function));
41            lvgl::lv_indev_set_user_data(input_device, Box::into_raw(user_data) as *mut c_void);
42
43            if r#type == InputKind::Keypad {
44                let group = lvgl::lv_group_get_default();
45
46                lvgl::lv_indev_set_group(input_device, group);
47            }
48
49            input_device
50        };
51
52        Ok(Self { input_device })
53    }
54}