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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/**
* @file lv_evdev.c
*
*/
/**********************
* INCLUDES
**********************/
#include "lv_evdev.h"
#if LV_USE_EVDEV
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/param.h> /*To detect BSD*/
#ifdef BSD
#include <dev/evdev/input.h>
#else
#include <linux/input.h>
#endif /*BSD*/
#include "../../misc/lv_assert.h"
#include "../../misc/lv_math.h"
#include "../../stdlib/lv_mem.h"
#include "../../stdlib/lv_string.h"
#include "../../display/lv_display.h"
/**********************
* TYPEDEFS
**********************/
typedef struct {
/*Device*/
int fd;
/*Config*/
bool swap_axes;
int min_x;
int min_y;
int max_x;
int max_y;
/*State*/
int root_x;
int root_y;
int key;
lv_indev_state_t state;
} lv_evdev_t;
/**********************
* STATIC FUNCTIONS
**********************/
static int _evdev_process_key(uint16_t code)
{
switch(code) {
case KEY_UP:
return LV_KEY_UP;
case KEY_DOWN:
return LV_KEY_DOWN;
case KEY_RIGHT:
return LV_KEY_RIGHT;
case KEY_LEFT:
return LV_KEY_LEFT;
case KEY_ESC:
return LV_KEY_ESC;
case KEY_DELETE:
return LV_KEY_DEL;
case KEY_BACKSPACE:
return LV_KEY_BACKSPACE;
case KEY_ENTER:
return LV_KEY_ENTER;
case KEY_NEXT:
case KEY_TAB:
return LV_KEY_NEXT;
case KEY_PREVIOUS:
return LV_KEY_PREV;
case KEY_HOME:
return LV_KEY_HOME;
case KEY_END:
return LV_KEY_END;
default:
return 0;
}
}
static int _evdev_calibrate(int v, int in_min, int in_max, int out_min, int out_max)
{
if(in_min != in_max) v = (v - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return LV_CLAMP(out_min, v, out_max);
}
static lv_point_t _evdev_process_pointer(lv_indev_t * indev, int x, int y)
{
lv_display_t * disp = lv_indev_get_display(indev);
lv_evdev_t * dsc = lv_indev_get_driver_data(indev);
LV_ASSERT_NULL(dsc);
int swapped_x = dsc->swap_axes ? y : x;
int swapped_y = dsc->swap_axes ? x : y;
int offset_x = lv_display_get_offset_x(disp);
int offset_y = lv_display_get_offset_y(disp);
int width = lv_display_get_horizontal_resolution(disp);
int height = lv_display_get_vertical_resolution(disp);
lv_point_t p;
p.x = _evdev_calibrate(swapped_x, dsc->min_x, dsc->max_x, offset_x, offset_x + width - 1);
p.y = _evdev_calibrate(swapped_y, dsc->min_y, dsc->max_y, offset_y, offset_y + height - 1);
return p;
}
static void _evdev_read(lv_indev_t * indev, lv_indev_data_t * data)
{
lv_evdev_t * dsc = lv_indev_get_driver_data(indev);
LV_ASSERT_NULL(dsc);
/*Update dsc with buffered events*/
struct input_event in = { 0 };
while(read(dsc->fd, &in, sizeof(in)) > 0) {
if(in.type == EV_REL) {
if(in.code == REL_X) dsc->root_x += in.value;
else if(in.code == REL_Y) dsc->root_y += in.value;
}
else if(in.type == EV_ABS) {
if(in.code == ABS_X || in.code == ABS_MT_POSITION_X) dsc->root_x = in.value;
else if(in.code == ABS_Y || in.code == ABS_MT_POSITION_Y) dsc->root_y = in.value;
else if(in.code == ABS_MT_TRACKING_ID) {
if(in.value == -1) dsc->state = LV_INDEV_STATE_RELEASED;
else if(in.value == 0) dsc->state = LV_INDEV_STATE_PRESSED;
}
}
else if(in.type == EV_KEY) {
if(in.code == BTN_MOUSE || in.code == BTN_TOUCH) {
if(in.value == 0) dsc->state = LV_INDEV_STATE_RELEASED;
else if(in.value == 1) dsc->state = LV_INDEV_STATE_PRESSED;
}
else {
dsc->key = _evdev_process_key(in.code);
if(dsc->key) {
dsc->state = in.value ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
data->continue_reading = true; /*Keep following events in buffer for now*/
break;
}
}
}
}
/*Process and store in data*/
switch(lv_indev_get_type(indev)) {
case LV_INDEV_TYPE_KEYPAD:
data->state = dsc->state;
data->key = dsc->key;
break;
case LV_INDEV_TYPE_POINTER:
data->state = dsc->state;
data->point = _evdev_process_pointer(indev, dsc->root_x, dsc->root_y);
break;
default:
break;
}
}
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_indev_t * lv_evdev_create(lv_indev_type_t indev_type, const char * dev_path)
{
lv_evdev_t * dsc = lv_malloc_zeroed(sizeof(lv_evdev_t));
LV_ASSERT_MALLOC(dsc);
if(dsc == NULL) return NULL;
dsc->fd = open(dev_path, O_RDONLY | O_NOCTTY | O_CLOEXEC);
if(dsc->fd < 0) {
LV_LOG_ERROR("open failed: %s", strerror(errno));
goto err_after_malloc;
}
if(fcntl(dsc->fd, F_SETFL, O_NONBLOCK) < 0) {
LV_LOG_ERROR("fcntl failed: %s", strerror(errno));
goto err_after_open;
}
lv_indev_t * indev = lv_indev_create();
if(indev == NULL) goto err_after_open;
lv_indev_set_type(indev, indev_type);
lv_indev_set_read_cb(indev, _evdev_read);
lv_indev_set_driver_data(indev, dsc);
return indev;
err_after_open:
close(dsc->fd);
err_after_malloc:
lv_free(dsc);
return NULL;
}
void lv_evdev_set_swap_axes(lv_indev_t * indev, bool swap_axes)
{
lv_evdev_t * dsc = lv_indev_get_driver_data(indev);
LV_ASSERT_NULL(dsc);
dsc->swap_axes = swap_axes;
}
void lv_evdev_set_calibration(lv_indev_t * indev, int min_x, int min_y, int max_x, int max_y)
{
lv_evdev_t * dsc = lv_indev_get_driver_data(indev);
LV_ASSERT_NULL(dsc);
dsc->min_x = min_x;
dsc->min_y = min_y;
dsc->max_x = max_x;
dsc->max_y = max_y;
}
void lv_evdev_delete(lv_indev_t * indev)
{
lv_evdev_t * dsc = lv_indev_get_driver_data(indev);
LV_ASSERT_NULL(dsc);
close(dsc->fd);
lv_free(dsc);
lv_indev_delete(indev);
}
#endif /*LV_USE_EVDEV*/
|