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;
133 DbgPrint("EV_SYN, SYN_DROPPED\n");
137 DbgPrint("EV_SYN, 0x%x\n", event->code);
146 switch (event->code) {
149 case ABS_MT_POSITION_X:
150 s_info.event_data.x = event->value - s_info.x;
152 case ABS_MT_POSITION_Y:
153 s_info.event_data.y = event->value - s_info.y;
157 case ABS_MT_TRACKING_ID:
158 s_info.event_data.device = event->value;
160 case ABS_MT_TOUCH_MAJOR:
162 case ABS_MT_TOUCH_MINOR:
164 case ABS_MT_WIDTH_MAJOR:
166 case ABS_MT_WIDTH_MINOR:
169 DbgPrint("EV_ABS, 0x%x\n", event->code);
190 DbgPrint("0x%X, 0x%X\n", event->type, event->code);
194 return LB_STATUS_SUCCESS;
197 static void *event_thread_main(void *data)
201 struct input_event input_event;
202 char *ptr = (char *)&input_event;
207 DbgPrint("Initiated\n");
211 FD_SET(s_info.handle, &set);
212 FD_SET(s_info.tcb_pipe[PIPE_READ], &set);
214 fd = s_info.handle > s_info.tcb_pipe[PIPE_READ] ? s_info.handle : s_info.tcb_pipe[PIPE_READ];
215 ret = select(fd + 1, &set, NULL, NULL, NULL);
218 if (errno == EINTR) {
219 DbgPrint("Select receives INTR\n");
222 ErrPrint("Error: %s\n", strerror(errno));
224 } else if (ret == 0) {
225 ErrPrint("Timeout expired\n");
226 ret = LB_STATUS_ERROR_TIMEOUT;
230 if (FD_ISSET(s_info.handle, &set)) {
231 readsize = read(s_info.handle, ptr + offset, sizeof(input_event) - offset);
233 ErrPrint("Unable to read device: %s / fd: %d / offset: %d / size: %d - %d\n", strerror(errno), s_info.handle, offset, sizeof(input_event), readsize);
234 ret = LB_STATUS_ERROR_FAULT;
239 if (offset == sizeof(input_event)) {
241 if (processing_input_event(&input_event) < 0) {
242 ret = LB_STATUS_ERROR_FAULT;
248 if (FD_ISSET(s_info.tcb_pipe[PIPE_READ], &set)) {
251 if (read(s_info.tcb_pipe[PIPE_READ], &event_ch, sizeof(event_ch)) != sizeof(event_ch))
252 ErrPrint("Unable to read TCB_PIPE: %s\n", strerror(errno));
254 ret = LB_STATUS_ERROR_CANCEL;
262 static Eina_Bool event_read_cb(void *data, Ecore_Fd_Handler *handler)
265 struct event_data *item;
268 fd = ecore_main_fd_handler_fd_get(handler);
270 ErrPrint("Invalid fd\n");
271 return ECORE_CALLBACK_CANCEL;
274 if (read(fd, &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
275 ErrPrint("Unable to read event ch: %s\n", strerror(errno));
276 return ECORE_CALLBACK_CANCEL;
279 CRITICAL_SECTION_BEGIN(&s_info.event_list_lock);
280 item = eina_list_nth(s_info.event_list, 0);
282 s_info.event_list = eina_list_remove(s_info.event_list, item);
284 ErrPrint("Unable to get event\n");
285 CRITICAL_SECTION_END(&s_info.event_list_lock);
287 if (item && s_info.event_cb) {
288 switch (s_info.event_state) {
289 case EVENT_STATE_DEACTIVATE:
290 s_info.event_state = EVENT_STATE_ACTIVATE;
292 case EVENT_STATE_ACTIVATE:
293 s_info.event_state = EVENT_STATE_ACTIVATED;
295 case EVENT_STATE_ACTIVATED:
299 s_info.event_cb(s_info.event_state, item, s_info.cbdata);
303 return ECORE_CALLBACK_RENEW;
307 * x, y is the starting point.
309 HAPI int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
313 if (s_info.handle >= 0) {
314 DbgPrint("Already activated\n");
315 return LB_STATUS_SUCCESS;
318 s_info.handle = open(INPUT_PATH, O_RDONLY);
319 if (s_info.handle < 0) {
320 ErrPrint("Unable to access the device: %s\n", strerror(errno));
321 return LB_STATUS_ERROR_IO;
324 if (fcntl(s_info.handle, F_SETFD, FD_CLOEXEC) < 0)
325 ErrPrint("Error: %s\n", strerror(errno));
327 if (fcntl(s_info.handle, F_SETFL, O_NONBLOCK) < 0)
328 ErrPrint("Error: %s\n", strerror(errno));
330 status = pipe2(s_info.evt_pipe, O_NONBLOCK | O_CLOEXEC);
332 ErrPrint("Unable to prepare evt pipe: %s\n", strerror(errno));
333 if (close(s_info.handle) < 0)
334 ErrPrint("Failed to close handle: %s\n", strerror(errno));
336 return LB_STATUS_ERROR_FAULT;
339 status = pipe2(s_info.tcb_pipe, O_NONBLOCK | O_CLOEXEC);
341 ErrPrint("Unable to prepare tcb pipe: %s\n", strerror(errno));
342 if (close(s_info.handle) < 0)
343 ErrPrint("Failed to close handle: %s\n", strerror(errno));
345 CLOSE_PIPE(s_info.evt_pipe);
346 return LB_STATUS_ERROR_FAULT;
349 s_info.event_handler = ecore_main_fd_handler_add(s_info.evt_pipe[PIPE_READ], ECORE_FD_READ, event_read_cb, NULL, NULL, NULL);
350 if (!s_info.event_handler) {
351 if (close(s_info.handle) < 0)
352 ErrPrint("Failed to close handle: %s\n", strerror(errno));
355 CLOSE_PIPE(s_info.tcb_pipe);
356 CLOSE_PIPE(s_info.evt_pipe);
357 return LB_STATUS_ERROR_FAULT;
360 status = pthread_create(&s_info.tid, NULL, event_thread_main, NULL);
362 ErrPrint("Failed to initiate the thread: %s\n", strerror(status));
363 ecore_main_fd_handler_del(s_info.event_handler);
364 s_info.event_handler = NULL;
366 if (close(s_info.handle) < 0)
367 ErrPrint("close: %s\n", strerror(errno));
370 CLOSE_PIPE(s_info.tcb_pipe);
371 CLOSE_PIPE(s_info.evt_pipe);
372 return LB_STATUS_ERROR_FAULT;
375 s_info.event_cb = event_cb;
376 s_info.cbdata = data;
380 DbgPrint("Event handler activated\n");
381 return LB_STATUS_SUCCESS;
384 HAPI int event_deactivate(void)
387 struct event_data *event;
389 char event_ch = EVENT_CH;
391 if (s_info.handle < 0) {
392 ErrPrint("Event handler is not actiavated\n");
393 return LB_STATUS_SUCCESS;
396 if (write(s_info.tcb_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch))
397 ErrPrint("Unable to write tcb_pipe: %s\n", strerror(errno));
399 status = pthread_join(s_info.tid, &ret);
401 ErrPrint("Failed to join a thread: %s\n", strerror(errno));
403 DbgPrint("Thread returns: %d\n", (int)ret);
405 ecore_main_fd_handler_del(s_info.event_handler);
406 s_info.event_handler = NULL;
408 if (close(s_info.handle) < 0)
409 ErrPrint("Unable to release the fd: %s\n", strerror(errno));
412 DbgPrint("Event handler deactivated\n");
414 CLOSE_PIPE(s_info.tcb_pipe);
415 CLOSE_PIPE(s_info.evt_pipe);
417 EINA_LIST_FREE(s_info.event_list, event) {
418 if (s_info.event_cb) {
419 if (s_info.event_state == EVENT_STATE_DEACTIVATE) {
420 s_info.event_state = EVENT_STATE_ACTIVATE;
421 } else if (s_info.event_state == EVENT_STATE_ACTIVATE) {
422 s_info.event_state = EVENT_STATE_ACTIVATED;
424 s_info.event_cb(s_info.event_state, event, s_info.cbdata);
429 if (s_info.event_state != EVENT_STATE_DEACTIVATE) {
430 s_info.event_state = EVENT_STATE_DEACTIVATE;
433 s_info.event_cb(s_info.event_state, &s_info.event_data, s_info.cbdata);
436 s_info.event_data.x = -1;
437 s_info.event_data.y = -1;
438 return LB_STATUS_SUCCESS;
441 HAPI int event_is_activated(void)
443 return s_info.handle >= 0;