1 /*
2  * Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
8 #define ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
9 
10 #include <lvgl.h>
11 #include <zephyr/device.h>
12 #include <zephyr/input/input.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct lvgl_common_input_config {
19 	struct k_msgq *event_msgq;
20 	const struct device *display_dev;
21 };
22 
23 struct lvgl_common_input_data {
24 	lv_indev_t *indev;
25 	lv_indev_data_t pending_event;
26 	lv_indev_data_t previous_event;
27 };
28 
29 int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev);
30 int lvgl_init_input_devices(void);
31 
32 #define LVGL_INPUT_EVENT_MSGQ(identifier, type) lvgl_input_msgq_##type##_##identifier
33 #define LVGL_INPUT_DEVICE(inst)                 DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, input))
34 
35 #define LVGL_COORD_VALID(coord) IN_RANGE(coord, LV_COORD_MIN, LV_COORD_MAX)
36 #define LVGL_KEY_VALID(key)     IN_RANGE(key, 0, UINT8_MAX)
37 
38 #define LVGL_INPUT_DEFINE(input_device, lvgl_device, identifier, type, msgq_size, process_evt_cb)  \
39 	INPUT_CALLBACK_DEFINE_NAMED(input_device, process_evt_cb, (void *)lvgl_device,             \
40 				    process_evt_cb_##identifier);                                  \
41 	K_MSGQ_DEFINE(lvgl_input_msgq_##type##_##identifier, sizeof(lv_indev_data_t), msgq_size, 4)
42 
43 #define LVGL_INPUT_INST_DEFINE(inst, type, msgq_size, process_evt_cb)                              \
44 	LVGL_INPUT_DEFINE(LVGL_INPUT_DEVICE(inst), DEVICE_DT_INST_GET(inst), inst, type,           \
45 			  msgq_size, process_evt_cb)
46 
47 #ifdef __cplusplus
48 }
49 #endif
50 
51 /** @} */
52 
53 #endif /* ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_ */
54