1f8a427bd497d26072855097693a39a9ad49ee8e
[piranha_sensors.git] / input.c
1 /*
2  * Copyright (C) 2013 Paul Kocialkowski
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <dirent.h>
24 #include <linux/ioctl.h>
25 #include <linux/input.h>
26
27 #define LOG_TAG "piranha_sensors"
28 #include <utils/Log.h>
29
30 #include "piranha_sensors.h"
31
32 int64_t input_timestamp(struct input_event *event)
33 {
34         if (event == NULL)
35                 return -1;
36
37         return event->time.tv_sec*1000000000LL + event->time.tv_usec*1000;
38 }
39
40 int input_open(char *name)
41 {
42         DIR *d;
43         struct dirent *di;
44
45         char input_name[80] = { 0 };
46         char path[PATH_MAX];
47         int fd;
48         int rc;
49
50         if (name == NULL)
51                 return -EINVAL;
52
53         d = opendir("/dev/input");
54         if (d == NULL)
55                 return -1;
56
57         while ((di = readdir(d))) {
58                 if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
59                         continue;
60
61                 snprintf(path, PATH_MAX, "/dev/input/%s", di->d_name);
62                 fd = open(path, O_RDONLY | O_NONBLOCK);
63                 if (fd < 0)
64                         continue;
65
66                 rc = ioctl(fd, EVIOCGNAME(sizeof(input_name) - 1), &input_name);
67                 if (rc < 0)
68                         continue;
69
70                 if (strcmp(input_name, name) == 0)
71                         return fd;
72                 else
73                         close(fd);
74         }
75
76         return -1;      
77 }
78
79 int sysfs_path_prefix(char *name, char *path_prefix)
80 {
81         DIR *d;
82         struct dirent *di;
83
84         char input_name[80] = { 0 };
85         char path[PATH_MAX];
86         int fd;
87
88         if (name == NULL || path_prefix == NULL)
89                 return -EINVAL;
90
91         d = opendir("/sys/class/input");
92         if (d == NULL)
93                 return -1;
94
95         while ((di = readdir(d))) {
96                 if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
97                         continue;
98
99                 snprintf(path, PATH_MAX, "/sys/class/input/%s/name", di->d_name);
100
101                 fd = open(path, O_RDONLY);
102                 if (fd < 0)
103                         continue;
104
105                 read(fd, &input_name, sizeof(input_name));
106                 close(fd);
107
108                 if (strncmp(input_name, name, strlen(name)) == 0) {
109                         snprintf(path_prefix, PATH_MAX, "/sys/class/input/%s", di->d_name);
110                         return 0;
111                 }
112         }
113
114         return -1;
115 }