xref: /OK3568_Linux_fs/external/recovery/minui/events.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <sys/epoll.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include <linux/input.h>
26 
27 #include "minui.h"
28 
29 #define MAX_DEVICES 16
30 #define MAX_MISC_FDS 16
31 
32 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
33 #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
34 
35 #define test_bit(bit, array) \
36     ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
37 
38 struct fd_info {
39     int fd;
40     ev_callback cb;
41     void *data;
42 };
43 
44 static int epollfd;
45 static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
46 static int npolledevents;
47 
48 static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
49 
50 static unsigned ev_count = 0;
51 static unsigned ev_dev_count = 0;
52 static unsigned ev_misc_count = 0;
53 
ev_init(ev_callback input_cb,void * data)54 int ev_init(ev_callback input_cb, void *data)
55 {
56     DIR *dir;
57     struct dirent *de;
58     int fd;
59     struct epoll_event ev;
60     bool epollctlfail = false;
61 
62     epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
63     if (epollfd == -1)
64         return -1;
65 
66     dir = opendir("/dev/input");
67     if (dir != 0) {
68         while ((de = readdir(dir))) {
69             unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
70 
71 //            fprintf(stderr,"/dev/input/%s\n", de->d_name);
72             if (strncmp(de->d_name, "event", 5)) continue;
73             fd = openat(dirfd(dir), de->d_name, O_RDONLY);
74             if (fd < 0) continue;
75 
76             /* read the evbits of the input device */
77             if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
78                 close(fd);
79                 continue;
80             }
81 
82             /* TODO: add ability to specify event masks. For now, just assume
83              * that only EV_KEY and EV_REL event types are ever needed. */
84             if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) {
85                 close(fd);
86                 continue;
87             }
88 
89             ev.events = EPOLLIN | EPOLLWAKEUP;
90             ev.data.ptr = (void *)&ev_fdinfo[ev_count];
91             if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
92                 close(fd);
93                 epollctlfail = true;
94                 continue;
95             }
96 
97             ev_fdinfo[ev_count].fd = fd;
98             ev_fdinfo[ev_count].cb = input_cb;
99             ev_fdinfo[ev_count].data = data;
100             ev_count++;
101             ev_dev_count++;
102             if (ev_dev_count == MAX_DEVICES) break;
103         }
104     }
105 
106     if (epollctlfail && !ev_count) {
107         close(epollfd);
108         epollfd = -1;
109         return -1;
110     }
111 
112     return 0;
113 }
114 
ev_add_fd(int fd,ev_callback cb,void * data)115 int ev_add_fd(int fd, ev_callback cb, void *data)
116 {
117     struct epoll_event ev;
118     int ret;
119 
120     if (ev_misc_count == MAX_MISC_FDS || cb == NULL)
121         return -1;
122 
123     ev.events = EPOLLIN | EPOLLWAKEUP;
124     ev.data.ptr = (void *)&ev_fdinfo[ev_count];
125     ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
126     if (!ret) {
127         ev_fdinfo[ev_count].fd = fd;
128         ev_fdinfo[ev_count].cb = cb;
129         ev_fdinfo[ev_count].data = data;
130         ev_count++;
131         ev_misc_count++;
132     }
133 
134     return ret;
135 }
136 
ev_get_epollfd(void)137 int ev_get_epollfd(void)
138 {
139     return epollfd;
140 }
141 
ev_exit(void)142 void ev_exit(void)
143 {
144     while (ev_count > 0) {
145         close(ev_fdinfo[--ev_count].fd);
146     }
147     ev_misc_count = 0;
148     ev_dev_count = 0;
149     close(epollfd);
150 }
151 
ev_wait(int timeout)152 int ev_wait(int timeout)
153 {
154     npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout);
155     if (npolledevents <= 0)
156         return -1;
157     return 0;
158 }
159 
ev_dispatch(void)160 void ev_dispatch(void)
161 {
162     int n;
163     int ret;
164 
165     for (n = 0; n < npolledevents; n++) {
166         struct fd_info *fdi = polledevents[n].data.ptr;
167         ev_callback cb = fdi->cb;
168         if (cb)
169             cb(fdi->fd, polledevents[n].events, fdi->data);
170     }
171 }
172 
ev_get_input(int fd,uint32_t epevents,struct input_event * ev)173 int ev_get_input(int fd, uint32_t epevents, struct input_event *ev)
174 {
175     int r;
176 
177     if (epevents & EPOLLIN) {
178         r = read(fd, ev, sizeof(*ev));
179         if (r == sizeof(*ev))
180             return 0;
181     }
182     return -1;
183 }
184 
ev_sync_key_state(ev_set_key_callback set_key_cb,void * data)185 int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data)
186 {
187     unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)];
188     unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
189     unsigned i;
190     int ret;
191 
192     for (i = 0; i < ev_dev_count; i++) {
193         int code;
194 
195         memset(key_bits, 0, sizeof(key_bits));
196         memset(ev_bits, 0, sizeof(ev_bits));
197 
198         ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
199         if (ret < 0 || !test_bit(EV_KEY, ev_bits))
200             continue;
201 
202         ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits);
203         if (ret < 0)
204             continue;
205 
206         for (code = 0; code <= KEY_MAX; code++) {
207             if (test_bit(code, key_bits))
208                 set_key_cb(code, 1, data);
209         }
210     }
211 
212     return 0;
213 }
214