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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <libevdev/libevdev-uinput.h>
#include "error.h"
#include "axis.h"
#include "config.h"
#include "relabsd_device.h"
/*
LIBEVDEV_UINPUT_OPEN_MANAGED is not defined on my machines.
It is not my place to define it, so I'll avoid the issue by defining my own
constant.
*/
#ifndef LIBEVDEV_UINPUT_OPEN_MANAGED
#pragma message "[WARNING] libevdev did not define "\
"LIBEVDEV_UINPUT_OPEN_MANAGED, using value '-2' instead."
#define RELABSD_UINPUT_OPEN_MANAGED -2
#else
#define RELABSD_UINPUT_OPEN_MANAGED LIBEVDEV_UINPUT_OPEN_MANAGED
#endif
static void replace_rel_axes
(
struct relabsd_device * const dev,
const struct relabsd_config * const config
)
{
int i;
struct input_absinfo absinfo;
unsigned int abs_code, rel_code;
for (i = RELABSD_VALID_AXES_COUNT; i --> 0;)
{
if (config->axis[i].enabled)
{
rel_code = relabsd_axis_to_rel((enum relabsd_axis) i);
abs_code = relabsd_axis_to_abs((enum relabsd_axis) i);
relabsd_config_get_absinfo(config, (enum relabsd_axis) i, &absinfo);
libevdev_disable_event_code(dev->dev, EV_REL, rel_code);
libevdev_enable_event_code(dev->dev, EV_ABS, abs_code, &absinfo);
}
}
}
static int rename_device
(
struct libevdev * const dev,
const struct relabsd_config * const config
)
{
size_t new_name_size;
char * new_name;
const char * real_name;
/* +2: One for the \0, one for the space between prefix and 'real_name'. */
new_name_size = strlen(RELABSD_DEVICE_PREFIX) + 2;
if (config->device_name == (char *) NULL)
{
/* XXX
* "The name is never NULL but it may be the empty string."
* I'm assuming that since they use the term 'string', it is \0
* terminated.
*/
real_name = libevdev_get_name(dev);
}
else
{
real_name = config->device_name;
}
new_name_size += strlen(real_name);
new_name = (char *) calloc(new_name_size, sizeof(char));
if (new_name == (char *) NULL)
{
RELABSD_ERROR
(
"Attempt at allocating memory to create the virtual device's name "
"failed: %s.",
strerror(errno)
);
/* This frees whatever came from 'libevdev_get_name'. */
libevdev_set_name(dev, RELABSD_DEVICE_PREFIX);
return -1;
}
if
(
snprintf
(
new_name,
new_name_size,
"%s %s",
RELABSD_DEVICE_PREFIX,
real_name
)
!= ((int) (new_name_size - 1))
)
{
/* This makes for a great message when strerror(errno) returns SUCCESS. */
RELABSD_ERROR
(
"Something unexpected happened while renaming the virtual device: %s.",
strerror(errno)
);
/* This frees whatever came from 'libevdev_get_name'. */
libevdev_set_name(dev, RELABSD_DEVICE_PREFIX);
free((void *) new_name);
return -1;
}
/* This frees whatever came from 'libevdev_get_name'. */
libevdev_set_name(dev, new_name);
free((void *) new_name);
return 0;
}
int relabsd_device_create
(
struct relabsd_device * const dev,
const struct relabsd_config * const config
)
{
int fd;
RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Creating virtual device...");
fd = open(config->input_file, O_RDONLY);
if (fd < 0)
{
RELABSD_FATAL
(
"Could not open device '%s' in read only mode: %s.",
config->input_file,
strerror(errno)
);
return -1;
}
if (libevdev_new_from_fd(fd, &(dev->dev)) < 0)
{
RELABSD_FATAL
(
"libevdev could not open '%s': '%s'.",
config->input_file,
strerror(errno)
);
close(fd);
return -1;
}
if (rename_device(dev->dev, config) < 0)
{
libevdev_free(dev->dev);
close(fd);
return -1;
}
libevdev_enable_event_type(dev->dev, EV_ABS);
replace_rel_axes(dev, config);
if
(
libevdev_uinput_create_from_device
(
dev->dev,
/* See top of the file. */
RELABSD_UINPUT_OPEN_MANAGED,
&(dev->uidev)
)
< 0
)
{
RELABSD_FATAL("Could not create relabsd device: %s.", strerror(errno));
libevdev_free(dev->dev);
close(fd);
return -1;
}
close(fd);
return 0;
}
void relabsd_device_destroy (const struct relabsd_device * const dev)
{
RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Destroying virtual device...");
libevdev_uinput_destroy(dev->uidev);
libevdev_free(dev->dev);
}
int relabsd_device_write_evdev_event
(
const struct relabsd_device * const dev,
unsigned int const type,
unsigned int const code,
int const value
)
{
RELABSD_DEBUG
(
RELABSD_DEBUG_VIRTUAL_EVENTS,
"Sending event: {type = %s; code = %s; value = %d}.",
libevdev_event_type_get_name(type),
libevdev_event_code_get_name(type, code),
value
);
/*
* We'll also send the 'EV_SYN' events when we receive them from the input
* device.
* OPTIMIZE: prevent 'EV_SYN' from being sent if we haven't sent any new
* values. (It might not be worth it though)
*/
if (libevdev_uinput_write_event(dev->uidev, type, code, value) == 0)
{
return 0;
}
return -1;
}
void relabsd_device_set_axes_to_zero
(
const struct relabsd_device * const dev,
const struct relabsd_config * const config
)
{
int i;
for (i = 0; i < RELABSD_VALID_AXES_COUNT; ++i)
{
if (config->axis[i].enabled)
{
relabsd_device_write_evdev_event
(
dev,
EV_ABS,
relabsd_axis_to_abs((enum relabsd_axis) i),
0
);
}
}
/*
* Also send a SYN event when the axes have been modified.
*/
libevdev_uinput_write_event(dev->uidev, EV_SYN, SYN_REPORT, 0);
}
|