Apply the Listener
[profile/tv/apps/native/videoplayer.git] / src / timeout_handler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 <Elementary.h>
18 #include <Eina.h>
19 #include <Ecore.h>
20 #include "timeout_handler.h"
21 #include "dbg.h"
22
23 #define NUM_EVENT_HANDLERS 7
24
25 struct timeout_handler {
26         Eina_List *eventhandler_list;
27         Ecore_Timer *timer;
28
29         timeout_event_cb event_cb;
30         void *event_data;
31
32         timeout_event_cb timeout_cb;
33         void *timeout_data;
34
35         double timeout;
36 };
37
38 static Eina_Bool _timer_cb(void *data)
39 {
40         struct timeout_handler *handle;
41
42         if (!data)
43                 return EINA_FALSE;
44
45         handle = (timeout_handler *)data;
46
47         handle->timeout_cb(handle->timeout_data, 0, NULL);
48         handle->timer = NULL;
49
50         return EINA_FALSE;
51 }
52
53 static Eina_Bool _event_occured(void *data, int type, void *event)
54 {
55         struct timeout_handler *handle;
56
57         if (!data)
58                 return ECORE_CALLBACK_PASS_ON;
59
60         handle = (timeout_handler *)data;
61
62         if (type == ECORE_EVENT_KEY_UP ||
63                         type == ECORE_EVENT_MOUSE_MOVE)
64                 handle->event_cb(handle->event_data, type, event);
65
66         if (handle->timer)
67                 ecore_timer_reset(handle->timer);
68         else
69                 handle->timer = ecore_timer_add(handle->timeout,
70                                 _timer_cb, handle);
71
72         return ECORE_CALLBACK_PASS_ON;
73 }
74
75 void timeout_handler_reset(struct timeout_handler *handle)
76 {
77         if (handle->timer)
78                 ecore_timer_reset(handle->timer);
79         else
80                 handle->timer = ecore_timer_add(handle->timeout,
81                                 _timer_cb, handle);
82 }
83
84 struct timeout_handler *timeout_handler_init(double timeout,
85                 timeout_event_cb timeout_cb, void *timeout_data,
86                 timeout_event_cb event_cb, void *event_data)
87 {
88         struct timeout_handler *handle;
89         Ecore_Event_Handler *event_handler;
90         int i;
91         int event_type[NUM_EVENT_HANDLERS] = {
92                 ECORE_EVENT_KEY_UP,
93                 ECORE_EVENT_MOUSE_BUTTON_DOWN,
94                 ECORE_EVENT_MOUSE_BUTTON_UP,
95                 ECORE_EVENT_MOUSE_MOVE,
96                 ECORE_EVENT_MOUSE_WHEEL,
97                 ECORE_EVENT_MOUSE_IN,
98                 ECORE_EVENT_MOUSE_OUT
99         };
100
101         handle = (timeout_handler *)calloc(1, sizeof(*handle));
102         if (!handle)
103                 return NULL;
104
105         for (i = 0; i < NUM_EVENT_HANDLERS; i++) {
106                 event_handler = ecore_event_handler_add(
107                                 event_type[i], _event_occured, handle);
108                 if (!event_handler)
109                         goto error;
110
111                 handle->eventhandler_list = eina_list_append(
112                                 handle->eventhandler_list, event_handler);
113         }
114
115         handle->timer = ecore_timer_add(timeout, _timer_cb, handle);
116         if (!handle->timer)
117                 goto error;
118
119         handle->event_cb = event_cb;
120         handle->event_data = event_data;
121         handle->timeout_cb = timeout_cb;
122         handle->timeout_data = timeout_data;
123         handle->timeout = timeout;
124
125         return handle;
126
127 error:
128         timeout_handler_fini(handle);
129
130         return NULL;
131 }
132
133 void timeout_handler_fini(struct timeout_handler *handle)
134 {
135         Ecore_Event_Handler *event_handler;
136         void *o;
137
138         if (!handle)
139                 return;
140
141         EINA_LIST_FREE(handle->eventhandler_list, o) {
142                 event_handler = (Ecore_Event_Handler *)o;
143                 ecore_event_handler_del(event_handler);
144         }
145
146         if (handle->timer) {
147                 ecore_timer_del(handle->timer);
148                 handle->timer = NULL;
149         }
150
151         free(handle);
152 }