2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://www.tizenopensource.org/license
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.
25 #include <sys/types.h>
27 #include <linux/input.h>
32 #include <livebox-errno.h>
39 #define CRITICAL_SECTION_BEGIN(lock) do { \
41 ret = pthread_mutex_lock(&lock); \
43 ErrPrint("Unable to get lock: %s\n", strerror(ret)); \
47 #define CRITICAL_SECTION_END(lock) do { \
49 ret = pthread_mutex_unlock(&lock); \
51 ErrPrint("Unable to unlock: %s\n", strerror(ret)); \
55 #define CANCEL_SECTION_BEGIN() do { \
57 ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
59 ErrPrint("Unable to set cancelate state: %s\n", strerror(ret)); \
62 #define CANCEL_SECTION_END() do { \
64 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
66 ErrPrint("Unable to set cancelate state: %s\n", strerror(ret)); \
77 Eina_List *event_list;
79 pthread_mutex_t event_list_lock;
81 Ecore_Fd_Handler *event_handler;
83 int (*event_cb)(enum event_state state, struct event_data *event, void *data);
86 enum event_state event_state;
87 struct event_data event_data;
94 .event_handler = NULL,
99 .event_state = EVENT_STATE_DEACTIVATE,
108 HAPI int event_init(void)
111 ret = pthread_mutex_init(&s_info.event_list_lock, NULL);
113 ErrPrint("Mutex: %s\n", strerror(ret));
114 return LB_STATUS_ERROR_FAULT;
116 return LB_STATUS_SUCCESS;
119 HAPI int event_fini(void)
122 ret = pthread_mutex_destroy(&s_info.event_list_lock);
124 ErrPrint("Mutex destroy failed: %s\n", strerror(ret));
125 return LB_STATUS_SUCCESS;
128 static inline int processing_input_event(struct input_event *event)
130 struct event_data *item;
132 switch (event->type) {
134 switch (event->code) {
136 if (s_info.event_data.x < 0 || s_info.event_data.y < 0) {
137 /* Waiting full event packet */
141 item = malloc(sizeof(*item));
145 memcpy(item, &s_info.event_data, sizeof(*item));
147 CRITICAL_SECTION_BEGIN(s_info.event_list_lock);
148 s_info.event_list = eina_list_append(s_info.event_list, item);
149 CRITICAL_SECTION_END(s_info.event_list_lock);
152 if (write(s_info.evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
153 ErrPrint("Unable to send an event: %s\n", strerror(errno));
154 return LB_STATUS_ERROR_IO;
164 DbgPrint("EV_SYN, SYN_DROPPED\n");
168 DbgPrint("EV_SYN, 0x%x\n", event->code);
177 switch (event->code) {
180 case ABS_MT_POSITION_X:
181 s_info.event_data.x = event->value - s_info.x;
183 case ABS_MT_POSITION_Y:
184 s_info.event_data.y = event->value - s_info.y;
188 case ABS_MT_TRACKING_ID:
189 s_info.event_data.device = event->value;
191 case ABS_MT_TOUCH_MAJOR:
193 case ABS_MT_TOUCH_MINOR:
195 case ABS_MT_WIDTH_MAJOR:
197 case ABS_MT_WIDTH_MINOR:
200 DbgPrint("EV_ABS, 0x%x\n", event->code);
221 DbgPrint("0x%X, 0x%X\n", event->type, event->code);
225 return LB_STATUS_SUCCESS;
228 static void *event_main(void *data)
232 struct input_event input_event;
233 char *ptr = (char *)&input_event;
237 DbgPrint("event_main initiated\n");
240 CANCEL_SECTION_BEGIN();
242 FD_SET(s_info.handle, &set);
243 ret = select(s_info.handle + 1, &set, NULL, NULL, NULL);
246 if (errno == EINTR) {
247 DbgPrint("Select receives INTR\n");
248 CANCEL_SECTION_END();
251 ErrPrint("Error: %s\n", strerror(errno));
252 CANCEL_SECTION_END();
254 } else if (ret == 0) {
255 ErrPrint("Timeout expired\n");
256 CANCEL_SECTION_END();
257 return (void *)LB_STATUS_ERROR_TIMEOUT;
259 CANCEL_SECTION_END();
261 if (!FD_ISSET(s_info.handle, &set)) {
262 ErrPrint("Unexpected handle is toggled\n");
263 ret = LB_STATUS_ERROR_INVALID;
267 readsize = read(s_info.handle, ptr + offset, sizeof(input_event) - offset);
269 ErrPrint("Unable to read device: %s / fd: %d / offset: %d / size: %d - %d\n", strerror(errno), s_info.handle, offset, sizeof(input_event), readsize);
270 ret = LB_STATUS_ERROR_FAULT;
275 if (offset == sizeof(input_event)) {
277 if (processing_input_event(&input_event) < 0) {
278 ret = LB_STATUS_ERROR_FAULT;
287 static Eina_Bool event_read_cb(void *data, Ecore_Fd_Handler *handler)
290 struct event_data *item;
293 fd = ecore_main_fd_handler_fd_get(handler);
295 ErrPrint("Invalid fd\n");
296 return ECORE_CALLBACK_CANCEL;
299 if (read(fd, &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
300 ErrPrint("Unable to read event ch: %s\n", strerror(errno));
301 return ECORE_CALLBACK_CANCEL;
304 CRITICAL_SECTION_BEGIN(s_info.event_list_lock);
305 item = eina_list_nth(s_info.event_list, 0);
307 s_info.event_list = eina_list_remove(s_info.event_list, item);
309 ErrPrint("Unable to get event\n");
310 CRITICAL_SECTION_END(s_info.event_list_lock);
312 if (item && s_info.event_cb) {
313 switch (s_info.event_state) {
314 case EVENT_STATE_DEACTIVATE:
315 s_info.event_state = EVENT_STATE_ACTIVATE;
317 case EVENT_STATE_ACTIVATE:
318 s_info.event_state = EVENT_STATE_ACTIVATED;
320 case EVENT_STATE_ACTIVATED:
324 s_info.event_cb(s_info.event_state, item, s_info.cbdata);
328 return ECORE_CALLBACK_RENEW;
331 HAPI int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
335 if (s_info.handle >= 0) {
336 DbgPrint("Already activated\n");
337 return LB_STATUS_SUCCESS;
340 s_info.handle = open(INPUT_PATH, O_RDONLY);
341 if (s_info.handle < 0) {
342 ErrPrint("Unable to access the device: %s\n", strerror(errno));
343 return LB_STATUS_ERROR_IO;
346 if (fcntl(s_info.handle, F_SETFD, FD_CLOEXEC) < 0)
347 ErrPrint("Error: %s\n", strerror(errno));
349 if (fcntl(s_info.handle, F_SETFL, O_NONBLOCK) < 0)
350 ErrPrint("Error: %s\n", strerror(errno));
352 status = pipe2(s_info.evt_pipe, O_NONBLOCK | O_CLOEXEC);
354 ErrPrint("Unable to prepare evt pipe: %s\n", strerror(errno));
355 if (close(s_info.handle) < 0)
356 ErrPrint("Failed to close handle: %s\n", strerror(errno));
358 return LB_STATUS_ERROR_FAULT;
361 s_info.event_handler = ecore_main_fd_handler_add(s_info.evt_pipe[PIPE_READ], ECORE_FD_READ, event_read_cb, NULL, NULL, NULL);
362 if (!s_info.event_handler) {
363 if (close(s_info.handle) < 0)
364 ErrPrint("Failed to close handle: %s\n", strerror(errno));
366 if (close(s_info.evt_pipe[PIPE_READ]) < 0)
367 ErrPrint("Failed to close handle: %s\n", strerror(errno));
369 if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
370 ErrPrint("Failed to close handle: %s\n", strerror(errno));
373 return LB_STATUS_ERROR_FAULT;
376 status = pthread_create(&s_info.tid, NULL, event_main, NULL);
378 ErrPrint("Failed to initiate the thread: %s\n", strerror(status));
379 ecore_main_fd_handler_del(s_info.event_handler);
380 s_info.event_handler = NULL;
382 if (close(s_info.handle) < 0)
383 ErrPrint("close: %s\n", strerror(errno));
386 if (close(s_info.evt_pipe[PIPE_READ]) < 0)
387 ErrPrint("close: %s\n", strerror(errno));
389 if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
390 ErrPrint("close: %s\n", strerror(errno));
392 return LB_STATUS_ERROR_FAULT;
395 s_info.event_cb = event_cb;
396 s_info.cbdata = data;
400 DbgPrint("Event handler activated\n");
401 return LB_STATUS_SUCCESS;
404 HAPI int event_deactivate(void)
407 struct event_data *event;
410 if (s_info.handle < 0) {
411 ErrPrint("Event handler is not actiavated\n");
412 return LB_STATUS_SUCCESS;
415 status = pthread_cancel(s_info.tid);
417 ErrPrint("Failed to cacnel a thread: %s\n", strerror(errno));
419 status = pthread_join(s_info.tid, &ret);
421 ErrPrint("Failed to join a thread: %s\n", strerror(errno));
422 else if (ret == PTHREAD_CANCELED)
423 DbgPrint("Thread is canceled\n");
425 ecore_main_fd_handler_del(s_info.event_handler);
426 s_info.event_handler = NULL;
428 if (close(s_info.evt_pipe[PIPE_READ]) < 0)
429 ErrPrint("Failed to close: %s\n", strerror(errno));
431 if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
432 ErrPrint("Failed to close: %s\n", strerror(errno));
434 if (close(s_info.handle) < 0)
435 ErrPrint("Unable to release the fd: %s\n", strerror(errno));
438 DbgPrint("Event handler deactivated\n");
440 EINA_LIST_FREE(s_info.event_list, event) {
441 if (s_info.event_cb) {
442 if (s_info.event_state == EVENT_STATE_DEACTIVATE) {
443 s_info.event_state = EVENT_STATE_ACTIVATE;
444 } else if (s_info.event_state == EVENT_STATE_ACTIVATE) {
445 s_info.event_state = EVENT_STATE_ACTIVATED;
447 s_info.event_cb(s_info.event_state, event, s_info.cbdata);
452 if (s_info.event_state != EVENT_STATE_DEACTIVATE) {
453 s_info.event_state = EVENT_STATE_DEACTIVATE;
456 s_info.event_cb(s_info.event_state, &s_info.event_data, s_info.cbdata);
459 s_info.event_data.x = -1;
460 s_info.event_data.y = -1;
462 return LB_STATUS_SUCCESS;
465 HAPI int event_is_activated(void)
467 return s_info.handle >= 0;