tizen 2.3.1 release
[apps/home/volume-app.git] / src / timer.c
1 /*
2  * Copyright (c) 2009-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 <vconf.h>
18 #include <vconf-keys.h>
19
20 #include "main.h"
21 #include "_util_log.h"
22 #include "view.h"
23 #include "control.h"
24 #include "sound.h"
25 #include "timer.h"
26
27 #define DEL_TIMER(x) \
28         if (x) {\
29                 _D("DELTIMER x : %d\n", x);\
30                 ecore_timer_del(x);\
31                 x = NULL;\
32         }
33 #define ADD_TIMER(x, time, _timer_cb, data) \
34         if(x != NULL) DEL_TIMER(x); \
35         x = ecore_timer_add(time, _timer_cb, data);\
36         _D("ADDTIMER x : %d\n", x);\
37
38
39 static struct _s_info {
40         Ecore_Timer *popup_timer; /* pop-up timer */
41         Ecore_Timer *slider_timer; /* slider timer */
42         Ecore_Timer *su_timer; /* short up timer */
43         Ecore_Timer *sd_timer; /* short down timer */
44         Ecore_Timer *lu_timer; /* long up timer */
45         Ecore_Timer *ld_timer; /* long down timer */
46 } s_info = {
47         .popup_timer = NULL,
48         .slider_timer = NULL,
49         .su_timer = NULL,
50         .sd_timer = NULL,
51         .lu_timer = NULL,
52         .ld_timer = NULL,
53 };
54
55 Ecore_Timer *volume_timer_popup_timer_get(void)
56 {
57         return s_info.popup_timer;
58 }
59
60 Ecore_Timer *volume_timer_slider_timer_get(void)
61 {
62         return s_info.slider_timer;
63 }
64
65 Ecore_Timer *volume_timer_su_timer_get(void)
66 {
67         return s_info.su_timer;
68 }
69
70 Ecore_Timer *volume_timer_sd_timer_get(void)
71 {
72         return s_info.sd_timer;
73 }
74
75 static Eina_Bool _timer_popup_cb(void *data)
76 {
77         if(VOLUME_ERROR_OK != volume_control_close())
78         {
79                 _E("Failed to close volume");
80         }
81         if(VOLUME_ERROR_OK != volume_control_cache_flush())
82         {
83                 _E("Failed to flush cache");
84         }
85
86         return ECORE_CALLBACK_CANCEL;
87 }
88
89 static Eina_Bool _timer_slider_cb(void *data)
90 {
91         Evas_Object *slider = volume_view_slider_get();
92         if(slider == NULL) {
93                 s_info.slider_timer = NULL;
94                 return ECORE_CALLBACK_CANCEL;
95         }
96
97         double val = 0;
98
99         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
100         _D("sound status : %d", sound);
101
102 #if 0
103         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
104         _D("vibration : %d", vibration);
105 #endif
106
107         //sound_type_e sound_type = volume_sound_sound_manager_type_get();
108         sound_type_e sound_type = volume_control_sound_type_at_show_get();
109         _D("sound type at show : %d", sound_type);
110
111         val = elm_slider_value_get(slider);
112         val += 0.5;
113         _D("slider value : %d", (int)val);
114
115         if ((int)val != 0) {
116                 if(sound_type != SOUND_TYPE_MEDIA){
117                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
118                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
119                 }
120                 volume_sound_is_vibration_set(EINA_FALSE);
121 #if 0
122                 if(VOLUME_ERROR_OK != volume_view_icon_set()) {
123                         _E("Failed to set icon");
124                 }
125 #endif
126         }
127         if (val < 1) {
128                 if(sound_type != SOUND_TYPE_MEDIA) {
129                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
130
131                         if(sound) {
132                                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
133                                 volume_sound_vib_play();
134                         }
135                 }
136                 elm_slider_value_set(slider, 0);
137                 volume_sound_level_set(sound_type, 1);
138         }
139
140         return ECORE_CALLBACK_RENEW;
141 }
142
143 static Eina_Bool _timer_short_up_cb(void *data)
144 {
145         Evas_Object *win = volume_view_win_get();
146         if(!win)
147         {
148                 _E("Window is NULL");
149                 return ECORE_CALLBACK_CANCEL;
150         }
151
152         sound_type_e sound_type = volume_control_sound_type_at_show_get();
153         _D("sound type at show : %d", sound_type);
154
155         if(!evas_object_visible_get(win) && sound_type == SOUND_TYPE_RINGTONE)
156         {
157                 _E("Window is hidden");
158                 return ECORE_CALLBACK_CANCEL;
159         }
160
161         if(volume_view_is_slider_touching_get())
162         {
163                 return ECORE_CALLBACK_RENEW;
164         }
165
166         ecore_timer_interval_set(s_info.su_timer, 0.1);
167         volume_timer_del(TYPE_TIMER_SLIDER);
168
169         int sound_step = volume_sound_step_get();
170         _D("sound step : %d", sound_step);
171
172         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
173         _D("sound status : %d", sound);
174
175         int val = volume_sound_level_get(sound_type);
176         if(val == -1)
177         {
178                 _E("Failed to get volume level");
179                 return ECORE_CALLBACK_CANCEL;
180         }
181         _D("sound value : %d", val);
182
183         if(val == sound_step)
184         {
185                 _D("already sound value : %d", sound_step);
186                 return ECORE_CALLBACK_RENEW;
187         }
188
189         if(!sound && sound_type == SOUND_TYPE_NOTIFICATION){
190                 _D("Do not adjust the noti type in no sound.");
191                 return ECORE_CALLBACK_RENEW;
192         }
193
194         if(!sound && sound_type == SOUND_TYPE_RINGTONE)
195         {
196                 volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
197                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
198         }
199
200         if(volume_sound_level_set(sound_type, val+1 > sound_step ? sound_step : val+1))
201         {
202                 _D("[SAFETY_SOUND] release timer");
203                 volume_timer_add(3.0, TYPE_TIMER_POPUP);
204         }
205
206         return ECORE_CALLBACK_RENEW;
207 }
208
209 static Eina_Bool _timer_short_down_cb(void *data)
210 {
211         Evas_Object *win = volume_view_win_get();
212         if(!win)
213         {
214                 _E("Window is NULL");
215                 return ECORE_CALLBACK_CANCEL;
216         }
217
218         sound_type_e sound_type = volume_control_sound_type_at_show_get();
219         _D("sound type at show : %d", sound_type);
220
221         if(!evas_object_visible_get(win) && sound_type == SOUND_TYPE_RINGTONE)
222         {
223                 _E("Window is hidden");;
224                 return ECORE_CALLBACK_CANCEL;
225         }
226
227         if(volume_view_is_slider_touching_get())
228         {
229                 return ECORE_CALLBACK_RENEW;
230         }
231
232         ecore_timer_interval_set(s_info.sd_timer, 0.1);
233         volume_timer_del(TYPE_TIMER_SLIDER);
234
235         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
236         _D("sound status : %d", sound);
237
238         int val = volume_sound_level_get(sound_type);
239         if(val == -1)
240         {
241                 _E("Failed to get volume level");
242                 return ECORE_CALLBACK_CANCEL;
243         }
244
245         if(!sound && sound_type == SOUND_TYPE_NOTIFICATION){
246                 _D("Do not adjust the noti type in no sound.");
247                 return ECORE_CALLBACK_RENEW;
248         }
249         _D("sound value : %d", val);
250
251         if(val == 1)
252         {
253                 if(sound && sound_type == SOUND_TYPE_RINGTONE)
254                 {
255                         _D("Set sound status to vibration in long press");
256                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
257                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
258                         volume_sound_vib_play();
259                 }
260         }
261         else if(!val)
262         {
263                 _D("already sound value : 0");
264                 return ECORE_CALLBACK_RENEW;
265         }
266
267         if(sound || sound_type != SOUND_TYPE_RINGTONE)
268         {
269                 volume_sound_level_set(sound_type, val-1 <= 0 ? 0 : val-1);
270         }
271
272         return ECORE_CALLBACK_RENEW;
273 }
274
275 void volume_timer_add(double time, volume_timer_type type)
276 {
277         if(type == TYPE_TIMER_POPUP)
278         {
279                 ADD_TIMER(s_info.popup_timer, time, _timer_popup_cb, NULL);
280         }
281         else if(type == TYPE_TIMER_SLIDER)
282         {
283                 ADD_TIMER(s_info.slider_timer, time, _timer_slider_cb, NULL);
284         }
285         else if(type == TYPE_TIMER_SD)
286         {
287                 ADD_TIMER(s_info.sd_timer, time, _timer_short_down_cb, NULL);
288         }
289         else if(type == TYPE_TIMER_SU)
290         {
291                 ADD_TIMER(s_info.su_timer, time, _timer_short_up_cb, NULL);
292         }
293         else
294         {
295                 _E("Failed to get type : type error(%d)", type);
296                 return;
297         }
298 }
299
300 void volume_timer_del(volume_timer_type type)
301 {
302         if(type == TYPE_TIMER_POPUP)
303         {
304                 DEL_TIMER(s_info.popup_timer);
305         }
306         else if(type == TYPE_TIMER_SLIDER)
307         {
308                 DEL_TIMER(s_info.slider_timer);
309         }
310         else if(type == TYPE_TIMER_SD)
311         {
312                 DEL_TIMER(s_info.sd_timer);
313         }
314         else if(type == TYPE_TIMER_SU)
315         {
316                 DEL_TIMER(s_info.su_timer);
317         }
318         else
319         {
320                 _E("Failed to get type : type error(%d)", type);
321                 return;
322         }
323 }