blob: 8d5d816932fca41efcd619bd0ceda54f0677fb12 [file] [log] [blame]
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <fcntl.h>
8#include <libtsm.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14
15#include "input.h"
16
17static int fd;
18
19int input_init()
20{
21 int ret;
22
23 /* XXX open all evdev devices and poll all the FDs */
24 fd = open("/dev/input/event0", O_RDONLY);
25 if (fd < 0)
26 return fd;
27
28 if (!isatty(fileno(stdout)))
29 setbuf(stdout, NULL);
30
31 /* Check for grabs. */
32 ret = ioctl(fd, EVIOCGRAB, (void *) 1);
33
34 if (!ret) {
35 ioctl(fd, EVIOCGRAB, (void *) 0);
36 } else {
37 printf("Evdev device grabbed by another process\n");
38 close(fd);
39 return -1;
40 }
41
42 return 0;
43}
44
45void input_close()
46{
47 close(fd);
48}
49
50struct input_key_event *input_get_event()
51{
52 struct input_event ev;
53 int ret;
54
55 ret = read(fd, &ev, sizeof (struct input_event));
56 if (ret < (int) sizeof (struct input_event)) {
57 printf("expected %d bytes, got %d\n",
58 (int) sizeof (struct input_event), ret);
59 return NULL;
60 }
61
62 if (ev.type == EV_KEY) {
63 struct input_key_event *event = malloc(sizeof (*event));
64 event->code = ev.code;
65 event->value = ev.value;
66 return event;
67 }
68
69 return NULL;
70}
71
72void input_put_event(struct input_key_event *event)
73{
74 free(event);
75}
76
77int input_get_fd()
78{
79 return fd;
80}