2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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>
45 Eina_List *event_list;
47 pthread_mutex_t event_list_lock;
48 int evt_pipe[PIPE_MAX];
49 int tcb_pipe[PIPE_MAX];
50 Ecore_Fd_Handler *event_handler;
52 int (*event_cb)(enum event_state state, struct event_data *event, void *data);
55 enum event_state event_state;
56 struct event_data event_data;
63 .event_handler = NULL,
68 .event_state = EVENT_STATE_DEACTIVATE,
77 HAPI int event_init(void)
80 ret = pthread_mutex_init(&s_info.event_list_lock, NULL);
82 ErrPrint("Mutex: %s\n", strerror(ret));
83 return LB_STATUS_ERROR_FAULT;
85 return LB_STATUS_SUCCESS;
88 HAPI int event_fini(void)
91 ret = pthread_mutex_destroy(&s_info.event_list_lock);
93 ErrPrint("Mutex destroy failed: %s\n", strerror(ret));
94 return LB_STATUS_SUCCESS;
97 static inline int processing_input_event(struct input_event *event)
99 struct event_data *item;
101 switch (event->type) {
103 switch (event->code) {
105 if (s_info.event_data.x < 0 || s_info.event_data.y < 0) {
106 /* Waiting full event packet */
110 item = malloc(sizeof(*item));
114 memcpy(item, &s_info.event_data, sizeof(*item));
116 CRITICAL_SECTION_BEGIN(&s_info.event_list_lock);
117 s_info.event_list = eina_list_append(s_info.event_list, item);
118 CRITICAL_SECTION_END(&s_info.event_list_lock);
121 if (write(s_info.evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
122 ErrPrint("Unable to send an event: %s\n", strerror(errno));
123 return LB_STATUS_ERROR_IO;
136 DbgPrint("EV_SYN, SYN_DROPPED\n");
140 DbgPrint("EV_SYN, 0x%x\n", event->code);
149 switch (event->code) {
152 case ABS_MT_POSITION_X:
153 s_info.event_data.x = event->value - s_info.x;
155 case ABS_MT_POSITION_Y:
156 s_info.event_data.y = event->value - s_info.y;
160 case ABS_MT_TRACKING_ID:
161 s_info.event_data.device = event->value;
163 case ABS_MT_TOUCH_MAJOR:
165 case ABS_MT_TOUCH_MINOR:
167 case ABS_MT_WIDTH_MAJOR:
169 case ABS_MT_WIDTH_MINOR:
172 DbgPrint("EV_ABS, 0x%x\n", event->code);
193 DbgPrint("0x%X, 0x%X\n", event->type, event->code);
197 return LB_STATUS_SUCCESS;
200 static void *event_thread_main(void *data)
204 struct input_event input_event;
205 char *ptr = (char *)&input_event;
210 DbgPrint("Initiated\n");
214 FD_SET(s_info.handle, &set);
215 FD_SET(s_info.tcb_pipe[PIPE_READ], &set);
217 fd = s_info.handle > s_info.tcb_pipe[PIPE_READ] ? s_info.handle : s_info.tcb_pipe[PIPE_READ];
218 ret = select(fd + 1, &set, NULL, NULL, NULL);
221 if (errno == EINTR) {
222 DbgPrint("Select receives INTR\n");
225 ErrPrint("Error: %s\n", strerror(errno));
227 } else if (ret == 0) {
228 ErrPrint("Timeout expired\n");
229 ret = LB_STATUS_ERROR_TIMEOUT;
233 if (FD_ISSET(s_info.handle, &set)) {
234 readsize = read(s_info.handle, ptr + offset, sizeof(input_event) - offset);
236 ErrPrint("Unable to read device: %s / fd: %d / offset: %d / size: %d - %d\n", strerror(errno), s_info.handle, offset, sizeof(input_event), readsize);
237 ret = LB_STATUS_ERROR_FAULT;
242 if (offset == sizeof(input_event)) {
244 if (processing_input_event(&input_event) < 0) {
245 ret = LB_STATUS_ERROR_FAULT;
251 if (FD_ISSET(s_info.tcb_pipe[PIPE_READ], &set)) {
254 if (read(s_info.tcb_pipe[PIPE_READ], &event_ch, sizeof(event_ch)) != sizeof(event_ch))
255 ErrPrint("Unable to read TCB_PIPE: %s\n", strerror(errno));
257 ret = LB_STATUS_ERROR_CANCEL;
265 static Eina_Bool event_read_cb(void *data, Ecore_Fd_Handler *handler)
268 struct event_data *item;
271 fd = ecore_main_fd_handler_fd_get(handler);
273 ErrPrint("Invalid fd\n");
274 return ECORE_CALLBACK_CANCEL;
277 if (read(fd, &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
278 ErrPrint("Unable to read event ch: %s\n", strerror(errno));
279 return ECORE_CALLBACK_CANCEL;
282 CRITICAL_SECTION_BEGIN(&s_info.event_list_lock);
283 item = eina_list_nth(s_info.event_list, 0);
285 s_info.event_list = eina_list_remove(s_info.event_list, item);
287 ErrPrint("Unable to get event\n");
288 CRITICAL_SECTION_END(&s_info.event_list_lock);
290 if (item && s_info.event_cb) {
291 switch (s_info.event_state) {
292 case EVENT_STATE_DEACTIVATE:
293 s_info.event_state = EVENT_STATE_ACTIVATE;
295 case EVENT_STATE_ACTIVATE:
296 s_info.event_state = EVENT_STATE_ACTIVATED;
298 case EVENT_STATE_ACTIVATED:
302 s_info.event_cb(s_info.event_state, item, s_info.cbdata);
306 return ECORE_CALLBACK_RENEW;
310 * x, y is the starting point.
312 HAPI int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
316 if (s_info.handle >= 0) {
317 DbgPrint("Already activated\n");
318 return LB_STATUS_SUCCESS;
321 s_info.handle = open(INPUT_PATH, O_RDONLY);
322 if (s_info.handle < 0) {
323 ErrPrint("Unable to access the device: %s\n", strerror(errno));
324 return LB_STATUS_ERROR_IO;
327 if (fcntl(s_info.handle, F_SETFD, FD_CLOEXEC) < 0)
328 ErrPrint("Error: %s\n", strerror(errno));
330 if (fcntl(s_info.handle, F_SETFL, O_NONBLOCK) < 0)
331 ErrPrint("Error: %s\n", strerror(errno));
333 status = pipe2(s_info.evt_pipe, O_NONBLOCK | O_CLOEXEC);
335 ErrPrint("Unable to prepare evt pipe: %s\n", strerror(errno));
336 if (close(s_info.handle) < 0)
337 ErrPrint("Failed to close handle: %s\n", strerror(errno));
339 return LB_STATUS_ERROR_FAULT;
342 status = pipe2(s_info.tcb_pipe, O_NONBLOCK | O_CLOEXEC);
344 ErrPrint("Unable to prepare tcb pipe: %s\n", strerror(errno));
345 if (close(s_info.handle) < 0)
346 ErrPrint("Failed to close handle: %s\n", strerror(errno));
348 CLOSE_PIPE(s_info.evt_pipe);
349 return LB_STATUS_ERROR_FAULT;
352 s_info.event_handler = ecore_main_fd_handler_add(s_info.evt_pipe[PIPE_READ], ECORE_FD_READ, event_read_cb, NULL, NULL, NULL);
353 if (!s_info.event_handler) {
354 if (close(s_info.handle) < 0)
355 ErrPrint("Failed to close handle: %s\n", strerror(errno));
358 CLOSE_PIPE(s_info.tcb_pipe);
359 CLOSE_PIPE(s_info.evt_pipe);
360 return LB_STATUS_ERROR_FAULT;
363 status = pthread_create(&s_info.tid, NULL, event_thread_main, NULL);
365 ErrPrint("Failed to initiate the thread: %s\n", strerror(status));
366 ecore_main_fd_handler_del(s_info.event_handler);
367 s_info.event_handler = NULL;
369 if (close(s_info.handle) < 0)
370 ErrPrint("close: %s\n", strerror(errno));
373 CLOSE_PIPE(s_info.tcb_pipe);
374 CLOSE_PIPE(s_info.evt_pipe);
375 return LB_STATUS_ERROR_FAULT;
378 s_info.event_cb = event_cb;
379 s_info.cbdata = data;
383 DbgPrint("Event handler activated\n");
384 return LB_STATUS_SUCCESS;
387 HAPI int event_deactivate(void)
390 struct event_data *event;
392 char event_ch = EVENT_CH;
394 if (s_info.handle < 0) {
395 ErrPrint("Event handler is not actiavated\n");
396 return LB_STATUS_SUCCESS;
399 if (write(s_info.tcb_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch))
400 ErrPrint("Unable to write tcb_pipe: %s\n", strerror(errno));
402 status = pthread_join(s_info.tid, &ret);
404 ErrPrint("Failed to join a thread: %s\n", strerror(errno));
406 DbgPrint("Thread returns: %d\n", (int)ret);
408 ecore_main_fd_handler_del(s_info.event_handler);
409 s_info.event_handler = NULL;
411 if (close(s_info.handle) < 0)
412 ErrPrint("Unable to release the fd: %s\n", strerror(errno));
415 DbgPrint("Event handler deactivated\n");
417 CLOSE_PIPE(s_info.tcb_pipe);
418 CLOSE_PIPE(s_info.evt_pipe);
420 EINA_LIST_FREE(s_info.event_list, event) {
421 if (s_info.event_cb) {
422 if (s_info.event_state == EVENT_STATE_DEACTIVATE) {
423 s_info.event_state = EVENT_STATE_ACTIVATE;
424 } else if (s_info.event_state == EVENT_STATE_ACTIVATE) {
425 s_info.event_state = EVENT_STATE_ACTIVATED;
427 s_info.event_cb(s_info.event_state, event, s_info.cbdata);
432 if (s_info.event_state != EVENT_STATE_DEACTIVATE) {
433 s_info.event_state = EVENT_STATE_DEACTIVATE;
436 s_info.event_cb(s_info.event_state, &s_info.event_data, s_info.cbdata);
439 s_info.event_data.x = -1;
440 s_info.event_data.y = -1;
441 return LB_STATUS_SUCCESS;
444 HAPI int event_is_activated(void)
446 return s_info.handle >= 0;