2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <Elementary.h>
22 #include "timeout_handler.h"
24 #define EVENT_HANDLER_NUM 6
26 struct timeout_handler {
27 Ecore_Event_Handler * event_handler[EVENT_HANDLER_NUM];
32 * An event callback for ecore_event_handler_add.
34 * This callback is invoked once registered event occured,
35 * this functions will reset timer.
37 * @param data [in] The function specific data passed by ecore_event_handler_add.
38 * @param type [in] Type of the event.
39 * @param event [in] Event data.
40 * @return EINA_TRUE, if it will keep being called as per normal, for
41 * each handler set up for that event type;
42 * EINA_FALSE, if it will cease processing handlers for that particular
43 * event, so all handler set to handle that event type that have not already
44 * been called, will not be.
46 static Eina_Bool _event_occrued_cb(void *data, int type, void *event)
48 struct timeout_handler *handler;
51 _ERR("Invalid argument");
55 handler = (struct timeout_handler *) data;
57 ecore_timer_reset(handler->timer);
63 * An timer expired callback for ecore_timer_add.
65 * This callback is invoked once timer expired,
66 * this functions will terminate app.
68 * @param data [in] The function specific data passed by ecore_timer_add.
69 * @return ECORE_CALLBACK_CANCEL if remove a callback,
70 * ECORE_CALLBACK_RENEW if keep a callback.
72 static Eina_Bool _timer_expired_cb(void *data)
76 return ECORE_CALLBACK_CANCEL;
80 * Freeze given ecore timer.
82 * @param data [in] The timeout_handler data pointer, it include given ecore timer pointer.
85 void timeout_handler_freeze_timer(struct timeout_handler *data)
87 if (!data || !data->timer) {
88 _ERR("Invalid argument");
92 ecore_timer_freeze(data->timer);
96 * Thaw given ecore timer.
98 * @param data [in] The timeout_handler data pointer, it include given ecore timer pointer.
101 void timeout_handler_thaw_timer(struct timeout_handler *data)
103 if (!data || !data->timer) {
104 _ERR("Invalid argument");
108 ecore_timer_thaw(data->timer);
109 ecore_timer_reset(data->timer);
113 * Change timer interval value.
115 * @param data [in] The timeout_handler data pointer, it include given ecore timer pointer.
116 * @param val [in] New interval value to be set.
119 void timeout_handler_change_interval(struct timeout_handler *data,
122 if (!data || !data->timer) {
123 _ERR("Invalid argument");
128 ecore_timer_freeze(data->timer);
129 } else if (val > 0.0) {
130 ecore_timer_interval_set(data->timer, val);
131 ecore_timer_reset(data->timer);
132 ecore_timer_thaw(data->timer);
137 * Create and init timeout_handler data.
139 * @param val [in] Timer interval value.
140 * @return The timeout_handler data pointer, NULL if error.
142 struct timeout_handler *timeout_handler_init(double val)
144 struct timeout_handler *data;
146 data = (struct timeout_handler *) calloc(1, sizeof(*data));
148 _ERR("Fail to allocate memory for timeout handler");
152 data->event_handler[0] = ecore_event_handler_add(
153 ECORE_EVENT_KEY_DOWN, _event_occrued_cb, data);
154 data->event_handler[1] = ecore_event_handler_add(
155 ECORE_EVENT_KEY_UP, _event_occrued_cb, data);
156 data->event_handler[2] = ecore_event_handler_add(
157 ECORE_EVENT_MOUSE_BUTTON_DOWN,
158 _event_occrued_cb, data);
159 data->event_handler[3] = ecore_event_handler_add(
160 ECORE_EVENT_MOUSE_BUTTON_UP,
161 _event_occrued_cb, data);
162 data->event_handler[4] = ecore_event_handler_add(
163 ECORE_EVENT_MOUSE_MOVE, _event_occrued_cb, data);
164 data->event_handler[5] = ecore_event_handler_add(
165 ECORE_EVENT_MOUSE_WHEEL, _event_occrued_cb, data);
167 data->timer = ecore_timer_add(val, _timer_expired_cb, NULL);
173 * Release given timeout_handler data.
175 * @param data [in] The timeout_handler data pointer.
178 void timeout_handler_fini(struct timeout_handler *data)
183 _ERR("Invalid argument");
187 for (i = 0; i < EVENT_HANDLER_NUM; i++)
188 ecore_event_handler_del(data->event_handler[i]);
190 ecore_timer_del(data->timer);