Convert c to cpp except for ug files.
[profile/tv/apps/native/settings.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 <app.h>
18 #include <Elementary.h>
19 #include "dbg.h"
20 #include "stdlib.h"
21 #include "stdbool.h"
22 #include "timeout_handler.h"
23
24 #define EVENT_HANDLER_NUM 6
25
26 struct timeout_handler {
27         Ecore_Event_Handler * event_handler[EVENT_HANDLER_NUM];
28         Ecore_Timer *timer;
29 };
30
31 /**
32 *  An event callback for ecore_event_handler_add.
33 *
34 *  This callback is invoked once registered event occured,
35 *  this functions will reset timer.
36 *
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.
45 */
46 static Eina_Bool _event_occrued_cb(void *data, int type, void *event)
47 {
48         struct timeout_handler *handler;
49
50         if (!data) {
51                 _ERR("Invalid argument");
52                 return EINA_FALSE;
53         }
54
55         handler = (struct timeout_handler *) data;
56
57         ecore_timer_reset(handler->timer);
58
59         return EINA_FALSE;
60 }
61
62 /**
63 *  An timer expired callback for ecore_timer_add.
64 *
65 *  This callback is invoked once timer expired,
66 *  this functions will terminate app.
67 *
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.
71 */
72 static Eina_Bool _timer_expired_cb(void *data)
73 {
74         app_efl_exit();
75
76         return ECORE_CALLBACK_CANCEL;
77 }
78
79 /**
80 *  Freeze given ecore timer.
81 *
82 *  @param data [in] The timeout_handler data pointer, it include given ecore timer pointer.
83 *  @return void.
84 */
85 void timeout_handler_freeze_timer(struct timeout_handler *data)
86 {
87         if (!data || !data->timer) {
88                 _ERR("Invalid argument");
89                 return;
90         }
91
92         ecore_timer_freeze(data->timer);
93 }
94
95 /**
96 *  Thaw given ecore timer.
97 *
98 *  @param data [in] The timeout_handler data pointer, it include given ecore timer pointer.
99 *  @return void.
100 */
101 void timeout_handler_thaw_timer(struct timeout_handler *data)
102 {
103         if (!data || !data->timer) {
104                 _ERR("Invalid argument");
105                 return;
106         }
107
108         ecore_timer_thaw(data->timer);
109         ecore_timer_reset(data->timer);
110 }
111
112 /**
113 *  Change timer interval value.
114 *
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.
117 *  @return void.
118 */
119 void timeout_handler_change_interval(struct timeout_handler *data,
120                 double val)
121 {
122         if (!data || !data->timer) {
123                 _ERR("Invalid argument");
124                 return;
125         }
126
127         if (val == 0.0) {
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);
133         }
134 }
135
136 /**
137 *  Create and init timeout_handler data.
138 *
139 *  @param val [in] Timer interval value.
140 *  @return The timeout_handler data pointer, NULL if error.
141 */
142 struct timeout_handler *timeout_handler_init(double val)
143 {
144         struct timeout_handler *data;
145
146         data = (struct timeout_handler *) calloc(1, sizeof(*data));
147         if (!data) {
148                 _ERR("Fail to allocate memory for timeout handler");
149                 return NULL;
150         }
151
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);
166
167         data->timer = ecore_timer_add(val, _timer_expired_cb, NULL);
168
169         return data;
170 }
171
172 /**
173 *  Release given timeout_handler data.
174 *
175 *  @param data [in] The timeout_handler data pointer.
176 *  @return void.
177 */
178 void timeout_handler_fini(struct timeout_handler *data)
179 {
180         int i;
181
182         if (!data) {
183                 _ERR("Invalid argument");
184                 return;
185         }
186
187         for (i = 0; i < EVENT_HANDLER_NUM; i++)
188                 ecore_event_handler_del(data->event_handler[i]);
189
190         ecore_timer_del(data->timer);
191         free(data);
192 }