summaryrefslogtreecommitdiff
path: root/src/input.c
blob: bb5e83e9b32616590bfdf2cefa90de79110fb49c (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
 * @file
 * @section AUTHORS
 *
 *  Authors:
 *       Eamon Walsh <ewalsh@tycho.nsa.gov>
 *
 * @section LICENSE
 *
 * This file is in the public domain.
 *
 * @section DESCRIPTION
 *
 * Two things here:
 * 1. Udev code to detect new input devices and start monitoring them.
 * 2. Handling of raw incoming input events from Linux.  The code to detect
 * secure key combinations that will be handled by the server (such as the
 * switch-between-guests key) is done here, in key_event().
 */

#include <sys/types.h>
#include <sys/select.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <libudev.h>
#include <linux/input.h>

#include "sys-queue.h"
#include "client.h"
#include "display.h"
#include "input.h"
#include "fd.h"

#define MAX_DEVICES 16
#define DEVICE_PATH_LEN 32
#define DEVICE_NAME_LEN 64
#define DEVICE_LEN (DEVICE_PATH_LEN + DEVICE_NAME_LEN)
#define DEVICE_STEM "/dev/input/event"
#define ABSOLUTE_INITIAL -5000

struct device {
    int fd;
    int x, y;
    char *path;
    char *name;
    TAILQ_ENTRY(device) next;
};

TAILQ_HEAD(device_list, device);

static struct device_list devices = TAILQ_HEAD_INITIALIZER(devices);
static struct udev_monitor *udev_monitor;

/* Key tracker */
/* XXX combine this with the set in dclient.c */
static fd_set keys_down;
static int nkeys;

/* Absolute mouse coordinates */
static int absx;
static int absy;

static int
is_sak_press(struct input_event *e)
{
    return (e->code == LINPICK_SWITCH_SAK || e->code == LINPICK_LABEL_SAK) &&
	e->value;
}

static void
key_event(struct input_event *e)
{
    static int secure;
    static int ctrl_down, alt_down;

    /* General state tracking goes here */
    if (e->value && !FD_ISSET(e->code, &keys_down)) {
	FD_SET(e->code, &keys_down);
	nkeys++;
    }
    if (!e->value && FD_ISSET(e->code, &keys_down)) {
	FD_CLR(e->code, &keys_down);
	nkeys--;
    }

    /* Specific keys we track for convenience */
    if (e->code == KEY_LEFTCTRL)
	ctrl_down = e->value;
    else if (e->code == KEY_LEFTALT)
	alt_down = e->value;

    /* Check if SAK combination has been entered */
    if (!secure && is_sak_press(e) && ctrl_down && alt_down) {
	/* SAK entered */
	active_display->send_key(active_display, 0, KEY_LEFTCTRL, absx, absy);
	active_display->send_key(active_display, 0, KEY_LEFTALT, absx, absy);
	display_secure_enter(e->code);
	secure = 1;
    }

    /* Check if keys have been released, causing return to guest */
    else if (secure && nkeys == 0) {
	secure = 0;
	display_secure_leave();
    }

    /* Send event to server in secure mode */
    else if (secure)
	display_key_event(e);

    /* Otherwise, send event to the active display */
    else
	active_display->send_key(active_display, e->value, e->code, absx, absy);
}

static void
motion_event(struct input_event *e)
{
    struct mouse_event m = { absx, absy, 0, 0, 0 };

    switch(e->code) {
    case REL_X:
	m.x = absx = clamp_value(absx + e->value, display_get_width());
	m.dx = e->value;
	break;
    case REL_Y:
	m.y = absy = clamp_value(absy + e->value, display_get_height());
	m.dy = e->value;
	break;
    case REL_WHEEL:
	m.dz = -e->value;
	break;
    default:
	printf("Unhandled relative axis %d\n", e->code);
    }

    display_position_event(absx, absy);
    active_display->send_mouse(active_display, &m);
}

static void
absolute_event(struct device *d, struct input_event *e)
{
    struct mouse_event m = { absx, absy, 0, 0, 0 };

    switch(e->code) {
    case ABS_X:
	m.dx = e->value - absx;
	m.x = absx = e->value;
	break;
    case ABS_Y:
	m.dy = e->value - absy;
	m.y = absy = e->value;
	break;
    default:
	break;
    }

    display_position_event(absx, absy);
    active_display->send_mouse(active_display, &m);
}

static void
input_event(void *closure)
{
    struct device *d = closure;
    struct input_event levt[64];
    int i, readlen;

    readlen = read(d->fd, levt, sizeof(levt));
    if (readlen <= 0)
	return;

    for (i =0; i < readlen / sizeof(*levt); i++) {
	switch (levt[i].type) {
	case EV_SYN:
	    switch(levt[i].code) {
	    case SYN_REPORT:
		break;
	    default:
		printf("SYN: %d\n", levt[i].code);
	    }
	    break;
	case EV_KEY:
	    key_event(levt + i);
	    break;
	case EV_REL:
	    motion_event(levt + i);
	    break;
	case EV_ABS:
	    absolute_event(d, levt + i);
	    break;
	case EV_MSC:
	    break;
	default:
	    printf("%d\n", levt[i].type);
	}
    }
}

static int
configure_absolute_device(int fd)
{
    struct input_absinfo ai;

    if (ioctl(fd, EVIOCGABS(ABS_X), &ai) < 0)
	return -1;

    ai.minimum = 0;
    ai.maximum = display_get_width() - 1;

    if (ioctl(fd, EVIOCSABS(ABS_X), &ai) < 0) {
	FD_LOG(0, "ioctl EVIOCSABS failed: %m\n");
	return -1;
    }

    if (ioctl(fd, EVIOCGABS(ABS_Y), &ai) < 0)
	return -1;

    ai.minimum = 0;
    ai.maximum = display_get_height() - 1;

    if (ioctl(fd, EVIOCSABS(ABS_Y), &ai) < 0) {
	FD_LOG(0, "ioctl EVIOCSABS failed: %m\n");
	return -1;
    }

    return 0;
}

static int
open_device(const char *path)
{
    struct device *d;
    int fd = -1;
    char *name = "(unknown)";
    unsigned int bits;

    d = calloc(DEVICE_LEN + sizeof(struct device), 1);
    if (!d)
	goto err;

    fd = open(path, O_RDWR);
    if (fd < 0)
	goto err;

    d->fd = fd;
    d->x = d->y = ABSOLUTE_INITIAL;
    d->path = (char *)(d + 1);
    d->name = (char *)(d + 1) + DEVICE_PATH_LEN;
    strncpy(d->path, path, DEVICE_PATH_LEN - 1);

    if (ioctl(fd, EVIOCGRAB, 1) < 0)
	goto err;

    if (ioctl(fd, EVIOCGNAME(DEVICE_NAME_LEN - 1), d->name) < 0)
	goto err;
    name = d->name;

    if (ioctl(fd, EVIOCGBIT(0, 4), &bits) < 0)
	goto err;
    if ((bits & (1 << EV_ABS)) && configure_absolute_device(fd) < 0)
	goto err;

    if (fd_set_handler(fd, input_event, d) < 0)
	goto err;

    TAILQ_INSERT_TAIL(&devices, d, next);
    return 0;
err:
    FD_LOG(1, "Warning: failed to open device '%s' on %s\n", name, path);
    free(d);
    if (fd >= 0) {
	ioctl(fd, EVIOCGRAB, 0);
	close(fd);
    }
    return -1;
}

/* -------------------------------------------------------------------- */

static void
close_device(struct device *d)
{
    ioctl(d->fd, EVIOCGRAB, 0);
    fd_set_handler(d->fd, NULL, NULL);
    close(d->fd);
    TAILQ_REMOVE(&devices, d, next);
    free(d);
}

static void
device_added(struct udev_device *udev_device)
{
    const char *path, *key, *value, *class;
    struct udev_list_entry *set, *entry;

    path = udev_device_get_devnode(udev_device);
    if (!path)
        return;

    if (strncmp(path, DEVICE_STEM, strlen(DEVICE_STEM)))
	return;

    class = udev_device_get_property_value(udev_device, "ID_CLASS");
    if (class && (!strcmp(class, "kbd") || !strcmp(class, "mouse"))) {
	open_device(path);
	return;
    }

    set = udev_device_get_properties_list_entry(udev_device);
    udev_list_entry_foreach(entry, set) {
        key = udev_list_entry_get_name(entry);
        if (!key)
            continue;
        value = udev_list_entry_get_value(entry);
        if (!strcmp(key, "ID_INPUT_KEY")) {
	    open_device(path);
        } else if (!strcmp(key, "ID_INPUT_MOUSE")) {
	    open_device(path);
        } else if (!strcmp(key, "ID_INPUT_JOYSTICK")) {
	    ;
        } else if (!strcmp(key, "ID_INPUT_TABLET")) {
	    ;
        } else if (!strcmp(key, "ID_INPUT_TOUCHPAD")) {
	    open_device(path);
        } else if (!strcmp(key, "ID_INPUT_TOUCHSCREEN")) {
	    ;
        }
    }
}

static void
device_removed(struct udev_device *udev_device)
{
    const char *path;
    struct device *d;

    path = udev_device_get_devnode(udev_device);
    if (!path)
        return;

    if (strncmp(path, DEVICE_STEM, strlen(DEVICE_STEM)))
	return;

    TAILQ_FOREACH(d, &devices, next)
	if (!strcmp(d->path, path)) {
	    close_device(d);
	    return;
	}

    FD_LOG(2, "Warning: failed to close device %s\n", path);
}

static void
udev_event(void *closure)
{
    struct udev_device *udev_device;
    const char *action;

    udev_device = udev_monitor_receive_device(udev_monitor);
    if (udev_device) {
	action = udev_device_get_action(udev_device);
	if (action) {
	    if (!strcmp(action, "add"))
		device_added(udev_device);
	    else if (!strcmp(action, "remove"))
		device_removed(udev_device);
	}
	udev_device_unref(udev_device);
    }
}

int
input_initialize(int argc, char *argv[])
{
    struct udev *udev;
    struct udev_enumerate *enumerate;
    struct udev_list_entry *devices, *device;
    int fd;

    if (!(udev = udev_new()))
	goto err;
    if (!(udev_monitor = udev_monitor_new_from_netlink(udev, "udev")))
	goto err;
    if (udev_monitor_enable_receiving(udev_monitor))
	goto err;

    if (!(enumerate = udev_enumerate_new(udev)))
	goto err;
    udev_enumerate_scan_devices(enumerate);
    devices = udev_enumerate_get_list_entry(enumerate);
    udev_list_entry_foreach(device, devices) {
        const char *syspath = udev_list_entry_get_name(device);
        struct udev_device *udev_device = udev_device_new_from_syspath(udev, syspath);
        device_added(udev_device);
        udev_device_unref(udev_device);
    }
    udev_enumerate_unref(enumerate);

    fd = udev_monitor_get_fd(udev_monitor);
    if (fd_set_handler(fd, udev_event, NULL) < 0)
	goto err;

    absx = display_get_width() / 2;
    absy = display_get_height() / 2;

    return 0;
err:
    FD_LOG(0, "Failed to set up udev.\n");
    return -1;
}

void
input_shutdown(void)
{
    struct udev *udev;
    int fd;

    udev = udev_monitor_get_udev(udev_monitor);
    fd = udev_monitor_get_fd(udev_monitor);
    fd_set_handler(fd, NULL, NULL);
    udev_monitor_unref(udev_monitor);
    udev_unref(udev);

    while (!TAILQ_EMPTY(&devices))
	close_device(TAILQ_FIRST(&devices));
}