replace strerr*() functions with %m format for thread-safety
[platform/core/uifw/pepper.git] / src / lib / evdev / evdev.c
1 /*
2 * Copyright © 2015-2017 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #define _GNU_SOURCE
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <dirent.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <linux/input.h>
37
38 #include <evdev-internal.h>
39 #include <pepper-input-backend.h>
40
41 #ifdef EVENT_MAX
42 #undef EVENT_MAX
43 #endif
44 #define EVENT_MAX 32
45
46 #ifndef LONG_BITS
47 #define LONG_BITS (sizeof(long) * 8)
48 #endif
49
50 #ifndef NLONGS
51 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
52 #endif
53
54 static void
55 _evdev_keyboard_event_post(pepper_input_device_t *device, uint32_t keycode, int state, uint32_t time)
56 {
57         pepper_input_event_t event;
58
59         event.time = time;
60         event.key = keycode;
61         event.state = state ? PEPPER_KEY_STATE_PRESSED : PEPPER_KEY_STATE_RELEASED;
62
63         pepper_object_emit_event((pepper_object_t *)device,
64                                                                 PEPPER_EVENT_INPUT_DEVICE_KEYBOARD_KEY, &event);
65 }
66
67 static void
68 _evdev_keyboard_event_flush(pepper_evdev_t *evdev)
69 {
70         evdev_key_event_t *event = NULL;
71         evdev_key_event_t *tmp = NULL;
72
73         pepper_list_for_each_safe(event, tmp, &evdev->key_event_queue, link)
74         {
75                 _evdev_keyboard_event_post(event->device, event->keycode, event->state, event->time);
76                 pepper_list_remove(&event->link);
77                 free(event);
78         }
79 }
80
81 static void
82 _evdev_keyboard_event_queue(uint32_t keycode, int state, uint32_t time, evdev_device_info_t *device_info)
83 {
84         evdev_key_event_t *event = NULL;
85         pepper_evdev_t *evdev = device_info->evdev;
86
87         event = (evdev_key_event_t *)calloc(1, sizeof(evdev_key_event_t));
88         PEPPER_CHECK(event, return, "[%s] Failed to allocate memory for key event.\n", __FUNCTION__);
89
90         event->keycode = keycode;
91         event->state = state;
92         event->time = time;
93         event->device = device_info->device;
94
95         pepper_list_insert(&evdev->key_event_queue, &event->link);
96 }
97
98 static void
99 _evdev_keyboard_event_process(struct input_event *ev, evdev_device_info_t *device_info)
100 {
101          uint32_t timestamp;
102
103         /* FIXME : need to think about using current time vs. time within event from kernel */
104         timestamp = ev->time.tv_sec * 1000 + ev->time.tv_usec / 1000;
105
106         switch (ev->type)
107         {
108                 case EV_KEY:
109                         _evdev_keyboard_event_queue((uint32_t)ev->code, ev->value, timestamp, device_info);
110                         break;
111
112                 case EV_SYN:
113                         _evdev_keyboard_event_flush(device_info->evdev);
114                         break;
115
116                 default:
117                         break;
118         }
119 }
120
121 static int
122 _evdev_keyboard_event_fd_read(int fd, uint32_t mask, void *data)
123 {
124         uint32_t i;
125         int nread;
126         struct input_event ev[EVENT_MAX];
127         evdev_device_info_t *device_info = (evdev_device_info_t *)data;
128
129         PEPPER_CHECK(!(mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)),
130                                         return 0,
131                                         "[%s] With the given fd, there is an error or it's been hung-up.\n",
132                                         __FUNCTION__);
133
134         if (!(mask & WL_EVENT_READABLE))
135                 return 0;
136
137         nread = read(fd, &ev, sizeof(ev));
138         PEPPER_CHECK(nread>=0, return 0, "[%s] Failed on reading given fd. (error msg : %m, fd:%d)\n",
139                                         __FUNCTION__, fd);
140
141         for (i = 0 ; i < (nread / sizeof(ev[0])); i++)
142         {
143                 _evdev_keyboard_event_process(&ev[i], device_info);
144         }
145
146         return 0;
147 }
148
149 static int
150 bit_is_set(const unsigned long *array, int bit)
151 {
152     return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
153 }
154
155 static void
156 _evdev_device_configure(evdev_device_info_t *device_info)
157 {
158         int rc;
159         unsigned long bits[NLONGS(EV_CNT)] = {0, };
160         unsigned long key_bits[NLONGS(KEY_CNT)] = {0, };
161         unsigned long found = 0, i;
162         char device_name[256] = {0, };
163
164         rc = ioctl(device_info->fd, EVIOCGBIT(0, sizeof(bits)), bits);
165         PEPPER_CHECK(rc >= 0, return, "Failed to get event bits\n");
166
167         if (bit_is_set(bits, EV_KEY)) {
168                 rc = ioctl(device_info->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits);
169                 if (rc >= 0) {
170                         for (i = 0; i < BTN_MISC / BITS_PER_LONG; ++i) {
171                                 found |= key_bits[i];
172                                 if (found) break;
173                         }
174                         if (!found) {
175                                 for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
176                                         if (bit_is_set(key_bits, i)) {
177                                                 found = 1;
178                                                 break;
179                                         }
180                                 }
181                         }
182
183                         if (found) device_info->caps |= WL_SEAT_CAPABILITY_KEYBOARD;
184                 } else
185                         PEPPER_ERROR("Failed to get key bits\n");
186         }
187
188         if (bit_is_set(bits, EV_REL)) {
189                 rc = ioctl(device_info->fd, EVIOCGNAME(sizeof(device_name) - 1), device_name);
190                 if (rc >= 0) {
191                         if (strcasestr(device_name, "mouse"))
192                                 device_info->caps |= WL_SEAT_CAPABILITY_POINTER;
193                 } else
194                         PEPPER_ERROR("Failed to get device name\n");
195         }
196 }
197
198 static int
199 _evdev_keyboard_device_open(pepper_evdev_t *evdev, const char *path)
200 {
201         int fd;
202         char device_path[32];
203         uint32_t event_mask;
204         evdev_device_info_t *device_info = NULL;
205         pepper_input_device_t *device = NULL;
206
207         PEPPER_CHECK(path, return 0, "[%s] Given path is NULL.\n", __FUNCTION__);
208
209         snprintf(device_path, sizeof(device_path), "/dev/input/%s", path);
210
211         fd = open(device_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
212         PEPPER_CHECK(fd >= 0, return 0, "[%s] Failed to open given path of device.\n", __FUNCTION__);
213
214         device_info = (evdev_device_info_t *)calloc(1, sizeof(evdev_device_info_t));
215         PEPPER_CHECK(device_info, goto error, "[%s] Failed to allocate memory for device info...\n", __FUNCTION__);
216
217         device_info->fd = fd;
218         device_info->evdev = evdev;
219         strncpy(device_info->path, path, MAX_PATH_LEN - 1);
220
221         _evdev_device_configure(device_info);
222         if (device_info->caps != WL_SEAT_CAPABILITY_KEYBOARD) goto error;
223
224         device = pepper_input_device_create(evdev->compositor, WL_SEAT_CAPABILITY_KEYBOARD, NULL, NULL);
225         PEPPER_CHECK(device, goto error, "[%s] Failed to create pepper input device.\n", __FUNCTION__);
226
227         device_info->device = device;
228         event_mask = WL_EVENT_READABLE;
229         device_info->event_source = wl_event_loop_add_fd(evdev->event_loop,
230                         fd, event_mask, _evdev_keyboard_event_fd_read, device_info);
231         PEPPER_CHECK(device_info->event_source, goto error, "[%s] Failed to add fd as an event source...\n", __FUNCTION__);
232
233         pepper_list_insert(&evdev->device_list, &device_info->link);
234
235         return 1;
236
237 error:
238         if (device)
239         {
240                 pepper_input_device_destroy(device);
241                 device = NULL;
242         }
243
244         if (device_info)
245         {
246                 if (device_info->event_source)
247                         wl_event_source_remove(device_info->event_source);
248
249                 free(device_info);
250                 device_info = NULL;
251         }
252
253         if (fd >=0)
254                 close(fd);
255
256         return 0;
257 }
258
259 static void
260 _evdev_keyboard_device_close(pepper_evdev_t *evdev, const char *path)
261 {
262         evdev_device_info_t *device_info = NULL;
263
264         evdev_device_info_t *tmp = NULL;
265
266         PEPPER_CHECK(path, return, "[%s] Given path is NULL.\n", __FUNCTION__);
267
268         pepper_list_for_each_safe(device_info, tmp, &evdev->device_list, link) {
269                 if (!strncmp(path, device_info->path, MAX_PATH_LEN)) {
270                         pepper_input_device_destroy(device_info->device);
271                         wl_event_source_remove(device_info->event_source);
272                         close(device_info->fd);
273
274                         pepper_list_remove(&device_info->link);
275                         free(device_info);
276
277                         break;
278                 }
279         }
280 }
281
282
283 PEPPER_API pepper_bool_t
284 pepper_evdev_device_path_add(pepper_evdev_t *evdev, const char *path)
285 {
286         int res = 0;
287
288         PEPPER_CHECK(evdev, return PEPPER_FALSE, "Invalid evdev structure.\n");
289         PEPPER_CHECK(path, return PEPPER_FALSE, "Invalid path.\n");
290
291         if (!strncmp(path, "event", 5)) {
292                 res = _evdev_keyboard_device_open(evdev, path);
293         } else {
294                 PEPPER_ERROR("Invalid path to open: %s\n", path);
295         }
296
297         if (res) return PEPPER_TRUE;
298         return PEPPER_FALSE;
299 }
300
301 PEPPER_API void
302 pepper_evdev_device_path_remove(pepper_evdev_t *evdev, const char *path)
303 {
304         PEPPER_CHECK(evdev, return, "Invalid evdev structure.\n");
305         PEPPER_CHECK(path, return, "Invalid path.\n");
306
307         if (!strncmp(path, "event", 5)) {
308                 _evdev_keyboard_device_close(evdev, path);
309         } else {
310                 PEPPER_ERROR("Invalid path to close: %s\n", path);
311         }
312 }
313
314 PEPPER_API uint32_t
315 pepper_evdev_device_probe(pepper_evdev_t *evdev, uint32_t caps)
316 {
317         uint32_t probed = 0;
318
319         DIR *dir_info = NULL;
320         struct dirent *dir_entry = NULL;
321
322         /* Probe event device nodes under /dev/input */
323         dir_info = opendir("/dev/input/");
324
325         if (dir_info)
326         {
327                 while ((dir_entry = readdir(dir_info)))
328                 {
329                         if (!strncmp(dir_entry->d_name, "event", 5))
330                         {
331                                 if (caps & WL_SEAT_CAPABILITY_KEYBOARD)
332                                         probed += _evdev_keyboard_device_open(evdev, dir_entry->d_name);
333                         }
334                 }
335
336                 closedir(dir_info);
337                 dir_info = NULL;
338         }
339
340         return probed;
341 }
342
343 PEPPER_API pepper_evdev_t *
344 pepper_evdev_create(pepper_compositor_t *compositor)
345 {
346         pepper_evdev_t *evdev = NULL;
347
348         evdev = (pepper_evdev_t *)calloc(1, sizeof(pepper_evdev_t));
349         PEPPER_CHECK(evdev, return NULL, "[%s] Failed to allocate memory for pepper evdev...\n", __FUNCTION__);
350
351         evdev->compositor = compositor;
352         evdev->display = pepper_compositor_get_display(compositor);
353         evdev->event_loop = wl_display_get_event_loop(evdev->display);
354
355         pepper_list_init(&evdev->device_list);
356         pepper_list_init(&evdev->key_event_queue);
357
358         return evdev;
359 }
360
361 PEPPER_API void
362 pepper_evdev_destroy(pepper_evdev_t *evdev)
363 {
364         evdev_device_info_t *device_info = NULL;
365         evdev_device_info_t *tmp = NULL;
366
367         if (!evdev)
368                 return;
369
370         /* clean-up/destroy key event queue */
371         if (!pepper_list_empty(&evdev->key_event_queue))
372         {
373                 _evdev_keyboard_event_flush(evdev);
374                 pepper_list_remove(&evdev->key_event_queue);
375         }
376
377         /* clean-up/destory device list */
378         if (!pepper_list_empty(&evdev->device_list))
379         {
380                 pepper_list_for_each_safe(device_info, tmp, &evdev->device_list, link)
381                 {
382                         if (device_info->device)
383                                 pepper_input_device_destroy(device_info->device);
384                         if (device_info->event_source)
385                                 wl_event_source_remove(device_info->event_source);
386                         if (device_info->fd)
387                                 close(device_info->fd);
388
389                         pepper_list_remove(&device_info->link);
390                         free(device_info);
391                 }
392
393                 pepper_list_remove(&evdev->device_list);
394         }
395
396         free(evdev);
397 }
398