forked from IMbackK/iio-sensor-proxy
/
fake-input-accelerometer.c
310 lines (253 loc) · 6.15 KB
/
fake-input-accelerometer.c
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) 2011, 2014 Bastien Nocera <hadess@hadess.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*/
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <stdio.h>
#include <termios.h>
#include <gudev/gudev.h>
#include "uinput.h"
#define ONEG 256
typedef struct {
GMainLoop *loop;
int uinput;
int accel_x, accel_y, accel_z;
GUdevClient *client;
GUdevDevice *uinput_dev;
struct termios old_tio;
} OrientationData;
static GUdevDevice *
setup_uinput_udev (GUdevClient *client)
{
GList *devices, *l;
GUdevDevice *ret = NULL;
devices = g_udev_client_query_by_subsystem (client, "input");
for (l = devices; l != NULL; l = l->next) {
GUdevDevice *dev = l->data;
if (g_udev_device_get_property_as_boolean (dev, "ID_INPUT_ACCELEROMETER")) {
ret = g_object_ref (dev);
break;
}
}
g_list_free_full (devices, g_object_unref);
return ret;
}
static int
write_sysfs_string (char *filename,
char *basedir,
char *val)
{
int ret = 0;
FILE *sysfsfp;
char *temp;
temp = g_build_filename (basedir, filename, NULL);
sysfsfp = fopen (temp, "w");
if (sysfsfp == NULL) {
ret = -errno;
goto error_free;
}
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);
error_free:
g_free(temp);
return ret;
}
static gboolean
send_uinput_event (OrientationData *data)
{
struct uinput_event ev;
memset(&ev, 0, sizeof(ev));
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = data->accel_x;
(void) !write (data->uinput, &ev, sizeof(ev));
ev.code = ABS_Y;
ev.value = data->accel_y;
(void) !write (data->uinput, &ev, sizeof(ev));
ev.code = ABS_Z;
ev.value = data->accel_z;
(void) !write (data->uinput, &ev, sizeof(ev));
memset(&ev, 0, sizeof(ev));
gettimeofday(&ev.time, NULL);
ev.type = EV_SYN;
ev.code = SYN_REPORT;
(void) !write (data->uinput, &ev, sizeof(ev));
if (!data->uinput_dev)
data->uinput_dev = setup_uinput_udev (data->client);
if (!data->uinput_dev)
return FALSE;
if (write_sysfs_string ((char *) "uevent", (char *) g_udev_device_get_sysfs_path (data->uinput_dev), (char *) "change") < 0) {
g_warning ("Failed to write uevent");
return FALSE;
}
return TRUE;
}
static gboolean
setup_uinput (OrientationData *data)
{
struct uinput_dev dev;
int fd;
fd = open("/dev/uinput", O_RDWR);
if (fd < 0) {
g_warning ("Could not open uinput");
return FALSE;
}
memset (&dev, 0, sizeof(dev));
snprintf (dev.name, sizeof (dev.name), "%s", "iio-sensor-proxy test application");
dev.id.bustype = BUS_VIRTUAL;
dev.id.vendor = 0x00;
dev.id.product = 0x00;
/* 1G accel is reported as ~256, so clamp to 2G */
dev.absmin[ABS_X] = dev.absmin[ABS_Y] = dev.absmin[ABS_Z] = -512;
dev.absmax[ABS_X] = dev.absmax[ABS_Y] = dev.absmax[ABS_Z] = 512;
if (write (fd, &dev, sizeof(dev)) != sizeof(dev)) {
g_warning ("Error creating uinput device");
goto bail;
}
/* enabling key events */
if (ioctl (fd, UI_SET_EVBIT, EV_ABS) < 0) {
g_warning ("Error enabling uinput absolute events");
goto bail;
}
/* enabling keys */
if (ioctl (fd, UI_SET_ABSBIT, ABS_X) < 0 ||
ioctl (fd, UI_SET_ABSBIT, ABS_Y) < 0 ||
ioctl (fd, UI_SET_ABSBIT, ABS_Z) < 0) {
g_warning ("Couldn't enable uinput axis");
goto bail;
}
/* creating the device */
if (ioctl (fd, UI_DEV_CREATE) < 0) {
g_warning ("Error creating uinput device");
goto bail;
}
data->uinput = fd;
return TRUE;
bail:
close (fd);
return FALSE;
}
static void
keyboard_usage (void)
{
g_print ("Valid keys are: u (up), d (down), l (left), r (right), q/x (quit)\n");
}
static gboolean
check_keyboard (GIOChannel *source,
GIOCondition condition,
OrientationData *data)
{
GIOStatus status;
char buf[1];
status = g_io_channel_read_chars (source, buf, 1, NULL, NULL);
if (status == G_IO_STATUS_ERROR ||
status == G_IO_STATUS_EOF) {
g_main_loop_quit (data->loop);
return FALSE;
}
if (status == G_IO_STATUS_AGAIN)
return TRUE;
switch (buf[0]) {
case 'u':
data->accel_x = 0;
data->accel_y = -ONEG;
data->accel_z = 0;
break;
case 'd':
data->accel_x = 0;
data->accel_y = ONEG;
data->accel_z = 0;
break;
case 'l':
data->accel_x = ONEG;
data->accel_y = 0;
data->accel_z = 0;
break;
case 'r':
data->accel_x = -ONEG;
data->accel_y = 0;
data->accel_z = 0;
break;
case 'q':
case 'x':
g_main_loop_quit (data->loop);
return FALSE;
default:
keyboard_usage ();
return TRUE;
}
send_uinput_event (data);
return TRUE;
}
static gboolean
setup_keyboard (OrientationData *data)
{
GIOChannel *channel;
struct termios new_tio;
tcgetattr(STDIN_FILENO, &data->old_tio);
new_tio = data->old_tio;
new_tio.c_lflag &=(~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
channel = g_io_channel_unix_new (STDIN_FILENO);
if (!channel) {
g_warning ("Failed to open stdin");
return FALSE;
}
if (g_io_channel_set_encoding (channel, NULL, NULL) != G_IO_STATUS_NORMAL) {
g_warning ("Failed to set stdin encoding to NULL");
return FALSE;
}
g_io_add_watch (channel, G_IO_IN, (GIOFunc) check_keyboard, data);
return TRUE;
}
static void
free_orientation_data (OrientationData *data)
{
if (data == NULL)
return;
tcsetattr(STDIN_FILENO, TCSANOW, &data->old_tio);
if (data->uinput > 0)
close (data->uinput);
g_clear_object (&data->uinput_dev);
g_clear_object (&data->client);
g_clear_pointer (&data->loop, g_main_loop_unref);
g_free (data);
}
int main (int argc, char **argv)
{
OrientationData *data;
const gchar * const subsystems[] = { "input", NULL };
int ret = 0;
data = g_new0 (OrientationData, 1);
data->client = g_udev_client_new (subsystems);
if (!setup_keyboard (data)) {
g_warning ("Failed to setup keyboard capture");
ret = 1;
goto out;
}
/* Set up uinput */
if (!setup_uinput (data)) {
ret = 1;
goto out;
}
/* Start with the 'normal' orientation */
data->accel_x = 0;
data->accel_y = ONEG;
data->accel_z = 0;
send_uinput_event (data);
keyboard_usage ();
data->loop = g_main_loop_new (NULL, TRUE);
g_main_loop_run (data->loop);
out:
free_orientation_data (data);
return ret;
}