corrected input
[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         char *c;
48         int fd;
49         int rc;
50
51         if (name == NULL)
52                 return -EINVAL;
53
54         d = opendir("/dev/input");
55         if (d == NULL)
56                 return -1;
57
58         while ((di = readdir(d))) {
59                 if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
60                         continue;
61
62                 snprintf(path, PATH_MAX, "/dev/input/%s", di->d_name);
63                 fd = open(path, O_RDONLY | O_NONBLOCK);
64                 if (fd < 0)
65                         continue;
66
67                 rc = ioctl(fd, EVIOCGNAME(sizeof(input_name) - 1), &input_name);
68                 if (rc < 0)
69                         continue;
70
71                 c = strstr(&input_name, "\n");
72                 if (c != NULL)
73                         *c = '\0';
74
75                 if (strcmp(input_name, name) == 0)
76                         return fd;
77                 else
78                         close(fd);
79         }
80
81         return -1;      
82 }
83
84 int sysfs_path_prefix(char *name, char *path_prefix)
85 {
86         DIR *d;
87         struct dirent *di;
88
89         char input_name[80] = { 0 };
90         char path[PATH_MAX];
91         char *c;
92         int fd;
93
94         if (name == NULL || path_prefix == NULL)
95                 return -EINVAL;
96
97         d = opendir("/sys/class/input");
98         if (d == NULL)
99                 return -1;
100
101         while ((di = readdir(d))) {
102                 if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
103                         continue;
104
105                 snprintf(path, PATH_MAX, "/sys/class/input/%s/name", di->d_name);
106
107                 fd = open(path, O_RDONLY);
108                 if (fd < 0)
109                         continue;
110
111                 read(fd, &input_name, sizeof(input_name));
112                 close(fd);
113
114                 c = strstr(&input_name, "\n");
115                 if (c != NULL)
116                         *c = '\0';
117
118                 if (strcmp(input_name, name) == 0) {
119                         snprintf(path_prefix, PATH_MAX, "/sys/class/input/%s", di->d_name);
120                         return 0;
121                 }
122         }
123
124         return -1;
125 }