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
|
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <libevdev/libevdev.h>
#include "error.h"
#include "axis.h"
#include "config.h"
#include "input.h"
/*
* Ensures that the input device has enabled the EV_REL axes mentioned
* in the configuration file.
*
* Returns -1 on (fatal) error,
* 0 all configured axes are accounted for.
*/
static int check_for_axes
(
const struct libevdev * const dev,
const struct relabsd_config * const conf
)
{
int i, device_is_valid;
unsigned int rel_code;
device_is_valid = 1;
for (i = RELABSD_VALID_AXES_COUNT; i --> 0;)
{
if (conf->axis[i].enabled)
{
rel_code = relabsd_axis_to_rel((enum relabsd_axis) i);
if (!libevdev_has_event_code(dev, EV_REL, rel_code))
{
_FATAL
(
"Input device has no relative %s axis, yet the configuration "
"file asks to convert it.",
relabsd_axis_to_name((enum relabsd_axis) i)
);
device_is_valid = 0;
}
}
}
return (device_is_valid - 1);
}
/*
* Ensures that the input device is compatible with the config file.
*
* Returns -1 on (fatal) error,
* 0 is the device is compatible.
*/
static int device_is_compatible
(
const struct libevdev * const dev,
const struct relabsd_config * const conf
)
{
if (!libevdev_has_event_type(dev, EV_REL))
{
_S_FATAL("Input device has no relative axis.");
return -1;
}
if (check_for_axes(dev, conf) < 0)
{
return -1;
}
return 0;
}
int relabsd_input_open
(
struct relabsd_input * const input,
const struct relabsd_config * const conf
)
{
input->fd = open(conf->input_file, O_RDONLY);
if (input->fd < 0)
{
_FATAL
(
"Could not open device '%s' in read only mode: %s.",
conf->input_file,
strerror(errno)
);
return -1;
}
if
(
libevdev_new_from_fd(input->fd, &(input->dev)) < 0
)
{
_FATAL
(
"libevdev could not open '%s': %s.",
conf->input_file,
strerror(errno)
);
close(input->fd);
return -1;
}
if (device_is_compatible(input->dev, conf) < 0)
{
return -1;
}
return 0;
}
void relabsd_input_close (const struct relabsd_input * const input)
{
libevdev_free(input->dev);
close(input->fd);
}
int relabsd_input_read
(
const struct relabsd_input * const input,
unsigned int * const input_type,
unsigned int * const input_code,
int * const input_value
)
{
int rc;
struct input_event event;
rc =
libevdev_next_event
(
input->dev,
(LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_BLOCKING),
&event
);
/* TODO: Look into LIBEVDEV_READ_STATUS_SYNC, handle it. */
/*
if (rc == LIBEVDEV_READ_STATUS_SYNC)
{
handle_syn_dropped(input->dev);
}
else
*/
if (rc != LIBEVDEV_READ_STATUS_SUCCESS)
{
_WARNING("[INPUT] Could not get next event: %s.", strerror(-rc));
return -1;
}
_DEBUG
(
90,
"[INPUT] Valid event received: {type = %s; code = %s; value = %d}.",
libevdev_event_type_get_name(event.type),
libevdev_event_code_get_name(event.type, event.code),
event.value
);
*input_type = event.type;
*input_code = event.code;
*input_value = event.value;
return 0;
}
|