-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathevdev.c
More file actions
176 lines (147 loc) · 4.42 KB
/
Copy pathevdev.c
File metadata and controls
176 lines (147 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
*
* @file evdev.c
*
* The lib evdev driver
*
* Based on the original file from the repository
*
* - Move the driver to a separate file to avoid excessive conditional
* compilation
* Author: EDGEMTech Ltd, Erik Tagirov (erik.tagirov@edgemtech.ch)
*
* Copyright (c) 2025 EDGEMTech Ltd.
*
*/
/*********************
* INCLUDES
*********************/
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "lvgl/lvgl.h"
#if LV_USE_EVDEV
#include <lvgl_private/lvgl_private.h>
#include "../backends.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void indev_deleted_cb(lv_event_t * e);
static void discovery_cb(lv_indev_t * indev, lv_evdev_type_t type, void * user_data);
static void set_mouse_cursor_icon(lv_indev_t * indev, lv_display_t * display);
static lv_indev_t * init_pointer_evdev(lv_display_t * display);
/**********************
* STATIC VARIABLES
**********************/
static char * backend_name = "EVDEV";
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/*
* Initialize the evdev driver
*
* @param backend the backend descriptor
*/
int backend_init_evdev(backend_t * backend)
{
LV_ASSERT_NULL(backend);
backend->handle->indev = malloc(sizeof(indev_backend_t));
LV_ASSERT_NULL(backend->handle->indev);
backend->handle->indev->init_indev = init_pointer_evdev;
backend->name = backend_name;
backend->type = BACKEND_INDEV;
return 0;
}
/**********************
* STATIC FUNCTIONS
**********************/
/*
* Remove cursor icon
*
* @description When the indev is deleted remove the mouse cursor icon
* @note called by the LVGL evdev driver
* @param e the deletion event
*/
static void indev_deleted_cb(lv_event_t * e)
{
if(LV_GLOBAL_DEFAULT()->deinit_in_progress) return;
lv_obj_t * cursor_obj = lv_event_get_user_data(e);
lv_obj_delete(cursor_obj);
}
/*
* Set cursor icon
*
* @description Once the input device is discovered set the mouse cursor icon
* @note called by the LVGL evdev driver
* @param indev the input device
* @param type the type of the input device
* @param user_data the user data
*/
static void discovery_cb(lv_indev_t * indev, lv_evdev_type_t type, void * user_data)
{
LV_LOG_USER("new '%s' device discovered", type == LV_EVDEV_TYPE_REL ? "REL" :
type == LV_EVDEV_TYPE_ABS ? "ABS" :
type == LV_EVDEV_TYPE_KEY ? "KEY" :
"unknown");
lv_display_t * disp = user_data;
lv_indev_set_display(indev, disp);
if(type == LV_EVDEV_TYPE_REL) {
set_mouse_cursor_icon(indev, disp);
}
}
/*
* Set cursor icon
*
* @description Enables a pointer (touchscreen/mouse) input device
* @param display the display on which to create
* @param indev the input device to set the cursor on
*/
static void set_mouse_cursor_icon(lv_indev_t * indev, lv_display_t * display)
{
/* Set the cursor icon */
LV_IMAGE_DECLARE(mouse_cursor_icon);
lv_obj_t * cursor_obj = lv_image_create(lv_display_get_screen_active(display));
lv_image_set_src(cursor_obj, &mouse_cursor_icon);
lv_indev_set_cursor(indev, cursor_obj);
/* delete the mouse cursor icon if the device is removed */
lv_indev_add_event_cb(indev, indev_deleted_cb, LV_EVENT_DELETE, cursor_obj);
}
/*
* Initialize a mouse pointer device
*
* Enables a pointer (touchscreen/mouse) input device
* Use 'evtest' to find the correct input device. /dev/input/by-id/ is recommended if possible
* Use /dev/input/by-id/my-mouse-or-touchscreen or /dev/input/eventX
*
* If LV_LINUX_EVDEV_POINTER_DEVICE is not set, automatic evdev discovery will start
*
* @param display the LVGL display
*
* @return input device
*/
static lv_indev_t * init_pointer_evdev(lv_display_t * display)
{
const char * input_device = getenv("LV_LINUX_EVDEV_POINTER_DEVICE");
if(input_device == NULL) {
LV_LOG_USER("Using evdev automatic discovery.");
lv_evdev_discovery_start(discovery_cb, display);
return NULL;
}
lv_indev_t * indev = lv_evdev_create(LV_INDEV_TYPE_POINTER, input_device);
if(indev == NULL) {
return NULL;
}
lv_indev_set_display(indev, display);
set_mouse_cursor_icon(indev, display);
return indev;
}
#endif /*#if LV_USE_EVDEV*/