Cutting new release
[profile/ivi/clock.git] / timer / src / timer_motion.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 "timer_motion.h"
18 #include "timer_utils.h"
19 //#include "timer_resource.h"
20
21 /**********************************************************************
22 ******************define, struct ,typedef, union, enum, global val *************************************
23 ***********************************************************************/
24
25 static Edje_Color_Class g_color_class_lbtn_hl = {
26         .name = STRING_TIMER_MOTION_LBTN_TXT_COLOR,
27         .r = 249,.g = 249,.b = 249,.a = 255,
28         .r2 = 0,.g2 = 0,.b2 = 0,.a2 = 102,
29         .r3 = 0,.g3 = 0,.b3 = 0,.a3 = 0,
30 };
31
32 static Edje_Color_Class g_color_class_rbtn_hl = {
33         .name = STRING_TIMER_MOTION_RBTN_TXT_COLOR,
34         .r = 249,.g = 249,.b = 249,.a = 255,
35         .r2 = 0,.g2 = 0,.b2 = 0,.a2 = 102,
36         .r3 = 0,.g3 = 0,.b3 = 0,.a3 = 0,
37 };
38
39 /**********************************************************************
40 ******************Local function declear, extern function declear*************************************
41 ***********************************************************************/
42
43 //
44 static void _timer_view_motion_btn_set(motion_view * view);
45 static Eina_Bool _timer_view_motion_timer_cb(void *data);
46 static int _timer_view_motion_btn_stop_cb(struct appdata *ad);
47 static int _timer_view_motion_btn_restart_cb(struct appdata *ad);
48 static int _timer_view_motion_btn_reset_cb(struct appdata *ad);
49 static int _timer_view_motion_create_clock(struct appdata *ad);
50 //callback
51 //static void _timer_view_motion_effect_cb (void *data, Elm_Transit * transit);
52 //
53 static int _timer_view_motion_create(void *data);
54 static int _timer_view_motion_destroy(void *data);
55 static int _timer_view_motion_update(void *data);
56 static int _timer_view_motion_cleanup(void *data);
57 //
58 static int _timer_view_motion_create_layout(void *data);
59 static int _timer_view_motion_update_layout(void *data);
60 /**********************************************************************
61 ******************Global val , static global val*************************************
62 ***********************************************************************/
63 static int mutex_reset, mutex_stop_restart;     // to control button clicking logic
64
65 clk_view timer_view_motion = {
66         .create = _timer_view_motion_create,
67         .destroy = _timer_view_motion_destroy,
68         .update = _timer_view_motion_update,
69         .cleanup = _timer_view_motion_cleanup,
70         .is_create = EINA_FALSE,
71         .layer = MAX_CLK_FWK_VIEW_LAYER,
72 };
73
74 /**********************************************************************
75 ******************Local function ref*************************************
76 ***********************************************************************/
77 //
78 static void _timer_view_motion_btn_set(motion_view * view)
79 {
80         retm_if(NULL_CHECK(view), "view null");
81         switch (view->type_state) {
82         case TIMER_VIEW_MOTION_STATE_RUN:
83                 edje_object_part_text_set(_EDJ(view->eo_btn_lft),
84                                           STRING_TIMER_TXT,
85                                           STRING_TIMER_IDS_COM_SK_STOP_S_);
86                 edje_object_color_class_set(_EDJ(view->eo_btn_lft),
87                                             STRING_TIMER_MOTION_LBTN_TXT_COLOR,
88                                             255, 255, 255, 255, 0, 0, 0, 102, 0,
89                                             0, 0, 0);
90                 edje_object_part_text_set(_EDJ(view->eo_btn_rht),
91                                           STRING_TIMER_TXT,
92                                           STRING_TIMER_IDS_COM_BODY_RESET_S_);
93                 edje_object_color_class_set(_EDJ(view->eo_btn_rht),
94                                             STRING_TIMER_MOTION_RBTN_TXT_COLOR,
95                                             255, 255, 255, 255, 0, 0, 0, 102, 0,
96                                             0, 0, 0);
97                 break;
98         case TIMER_VIEW_MOTION_STATE_STOP:
99                 edje_object_part_text_set(_EDJ(view->eo_btn_lft),
100                                           STRING_TIMER_TXT,
101                                           STRING_TIMER_IDS_TMR_SK3_RESTART_);
102                 edje_object_color_class_set(_EDJ(view->eo_btn_lft),
103                                             STRING_TIMER_MOTION_LBTN_TXT_COLOR,
104                                             255, 255, 255, 255, 0, 0, 0, 102, 0,
105                                             0, 0, 0);
106                 edje_object_part_text_set(_EDJ(view->eo_btn_rht),
107                                           STRING_TIMER_TXT,
108                                           STRING_TIMER_IDS_COM_BODY_RESET_S_);
109                 edje_object_color_class_set(_EDJ(view->eo_btn_rht),
110                                             STRING_TIMER_MOTION_RBTN_TXT_COLOR,
111                                             255, 255, 255, 255, 0, 0, 102, 0, 0,
112                                             0, 0, 0);
113                 break;
114         default:
115                 break;
116         }
117 }
118
119 /**
120  * Debug function: to update time and interval in each timer intervals
121  * This function requires data as parameters
122  *
123  * _timer_view_motion_timer_cb()
124  * @param[in] data                  Pointer to user data, which contains the data of application
125  *
126  * @return      int, which indicates the result state of operation
127  */
128 static Eina_Bool _timer_view_motion_timer_cb(void *data)
129 {
130         retvm_if(NULL_CHECK(data), ECORE_CALLBACK_CANCEL, "data null");
131         struct appdata *ad = (struct appdata *)data;
132         motion_view *view = ad->view_motion;
133         retvm_if(NULL_CHECK(view), ECORE_CALLBACK_CANCEL, "view null");
134         struct timeval time_past = { 0, };
135         //read the system time
136         gettimeofday(&ad->current_systime, NULL);
137         //get time passed
138         time_past.tv_sec =
139             ad->current_systime.tv_sec - ad->start_systime.tv_sec;
140         time_past.tv_usec =
141             ad->current_systime.tv_usec - ad->start_systime.tv_usec;
142         if ((time_past.tv_sec) && (time_past.tv_usec < 0)) {
143                 time_past.tv_sec -= 1;
144                 time_past.tv_usec += 1000000;
145         }
146         //reduce due-time according to time passed
147         ad->due_time.tv_sec -= time_past.tv_sec;
148         ad->due_time.tv_usec -= time_past.tv_usec;
149         if ((ad->due_time.tv_sec) && (ad->due_time.tv_usec < 0)) {
150                 ad->due_time.tv_sec -= 1;
151                 ad->due_time.tv_usec += 1000000;
152         }
153         if ((ad->due_time.tv_sec < 0)
154             || ((ad->due_time.tv_sec == 0) && (ad->due_time.tv_usec <= 0))) {
155                 edje_color_class_set(STRING_TIMER_MOTION_TITLE_COLOR, 246, 105,
156                                      50, 255, 0, 0, 0, 0, 0, 0, 0, 0);
157                 timer_utils_view_change(ad);
158                 return ECORE_CALLBACK_CANCEL;
159         } else if (ad->due_time.tv_sec >= 46800) {
160                 ecore_timer_freeze(ad->timer);
161                 timer_utils_unregister_alarm(ad);
162                 clk_view_change(&timer_view_motion, &timer_view_setting, ad);
163                 elm_object_part_content_set(ad->eo_effect,
164                                             STRING_TIMER_ELM_SWALLOW_CENTER,
165                                             ad->view_setting->eo_navibar);
166                 return ECORE_CALLBACK_CANCEL;
167         } else {
168                 if (!ad->isTimerView) {
169                         // let start system time equals current system time
170                         ad->start_systime.tv_sec = ad->current_systime.tv_sec;
171                         ad->start_systime.tv_usec = ad->current_systime.tv_usec;
172                         return ECORE_CALLBACK_RENEW;
173                 }
174                 char time_str[BUF_SIZE] = { 0, };
175                 struct tm show_time;
176                 gmtime_r(&ad->due_time.tv_sec, &show_time);
177
178
179                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_hour/10);
180                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/hour/1");
181                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_hour%10);
182                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/hour/2");
183                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_min/10);
184                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/min/1");
185                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_min%10);
186                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/min/2");
187                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_sec/10);
188                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/sec/1");
189                 snprintf(time_str, sizeof(time_str), "%d", show_time.tm_sec%10);
190                 edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/sec/2");
191
192                 // let start system time equals current system time
193                 ad->start_systime.tv_sec = ad->current_systime.tv_sec;
194                 ad->start_systime.tv_usec = ad->current_systime.tv_usec;
195                 return ECORE_CALLBACK_RENEW;
196         }
197 }
198
199 //
200 static int _timer_view_motion_btn_stop_cb(struct appdata *ad)
201 {
202         retvm_if(NULL_CHECK(ad), FAILED, "ad null");
203         motion_view *view = ad->view_motion;
204         if ((ad->due_time.tv_sec > 1)
205             || (ad->due_time.tv_sec == 1 && ad->due_time.tv_usec > 2100)) {
206                 ECORE_TIMER_DELIF(ad->timer);
207                 timer_utils_unregister_alarm(ad);
208                 view->type_state = TIMER_VIEW_MOTION_STATE_STOP;
209                 _timer_view_motion_btn_set(view);
210         }
211         return SUCCESS;
212 }
213
214 //
215 static int _timer_view_motion_btn_restart_cb(struct appdata *ad)
216 {
217         retvm_if(NULL_CHECK(ad), FAILED, "ad null");
218         motion_view *view = ad->view_motion;
219         timer_utils_register_alarm(ad);
220         gettimeofday(&ad->start_systime, NULL);
221         edje_color_class_set(STRING_TIMER_MOTION_TITLE_COLOR, 223, 180, 152,
222                              255, 0, 0, 0, 0, 0, 0, 0, 0);
223         ECORE_TIMER_DELIF(ad->timer);
224         _timer_view_motion_timer_cb(ad);
225         ad->timer =
226             ecore_timer_add(ad->timer_interval, _timer_view_motion_timer_cb,
227                             ad);
228
229         view->type_state = TIMER_VIEW_MOTION_STATE_RUN;
230         _timer_view_motion_btn_set(view);
231         return SUCCESS;
232 }
233
234 //
235 static int _timer_view_motion_btn_reset_cb(struct appdata *ad)
236 {
237         retvm_if(NULL_CHECK(ad), FAILED, "ad null");
238         if ((ad->due_time.tv_sec > 1)
239             || (ad->due_time.tv_sec == 1 && ad->due_time.tv_usec > 2100)) {
240                 timer_utils_unregister_alarm(ad);
241                 timer_utils_view_change(ad);
242         }
243         return SUCCESS;
244 }
245
246 //
247 static int _timer_view_motion_create(void *data)
248 {
249         int ret = SUCCESS;
250         struct appdata *ad = NULL;
251         CLK_FUN_BEG();
252         ad = (struct appdata *)data;
253         //
254         ad->page = TIMER_VIEW_PAGE_MOTION;
255         ret = _timer_view_motion_create_layout(data);
256         ret |= _timer_view_motion_update_layout(data);
257         timer_show_view(ad, EINA_TRUE);
258         timer_view_motion.is_create = EINA_TRUE;
259         timer_view_motion.layer = 1;
260         CLK_FUN_END();
261         return ret;
262 }
263
264 //
265 static int __timer_view_motion_destroy_cb(void *data)
266 {
267         struct appdata *ad = NULL;
268         motion_view *view = NULL;
269         ad = (struct appdata *)data;
270         view = ad->view_motion;
271         //
272         ECORE_TIMER_DELIF(ad->timer);
273         EVAS_OBJECT_DELIF(view->eo_icon_hour);
274         EVAS_OBJECT_DELIF(view->eo_clock);
275         EVAS_OBJECT_DELIF(view->eo_content);
276         EVAS_OBJECT_DELIF(view->eo_content);
277         timer_view_motion.is_create = EINA_FALSE;
278         return SUCCESS;
279 }
280
281 //
282 static int _timer_view_motion_destroy(void *data)
283 {
284         int ret = SUCCESS;
285         CLK_FUN_BEG();
286         //effect
287         __timer_view_motion_destroy_cb(data);
288         CLK_FUN_END();
289         return ret;
290 }
291
292 //
293 static int _timer_view_motion_update(void *data)
294 {
295         int ret = SUCCESS;
296         struct appdata *ad = NULL;
297         CLK_FUN_BEG();
298         ad = (struct appdata *)data;
299         //
300         ad->page = TIMER_VIEW_PAGE_MOTION;
301         ret = _timer_view_motion_update_layout(data);
302         timer_show_view(ad, EINA_TRUE);
303         CLK_FUN_END();
304         return ret;
305 }
306
307 //
308 static int _timer_view_motion_cleanup(void *data)
309 {
310         return _timer_view_motion_destroy(data);
311 }
312
313 //
314 static int _timer_view_motion_create_layout(void *data)
315 {
316         retvm_if(!data, FAILED, "data null");
317         struct appdata *ad = (struct appdata *)data;
318         motion_view *view = ad->view_motion;
319         retvm_if(NULL_CHECK(view), FAILED, "view null");
320         //
321         view->eo_parent = ad->parent;
322         view->eo_layout = ad->layout;
323         view->eo_content = load_edj(view->eo_layout, EDJ_NAME, CONTENT_MOTION);
324 //  elm_object_part_content_set(ad->eo_effect, STRING_TIMER_ELM_SWALLOW_CENTER, view->eo_content);
325         //create button
326         view->eo_btn_lft =
327             clk_widget_create_button(view->eo_content, EDJ_NAME,
328                                      STRING_TIMER_MOTION_LBTN,
329                                      &g_color_class_lbtn_hl,
330                                      timer_motion_lbtn_cb, ad);
331         elm_object_part_content_set(view->eo_content, STRING_TIMER_SW_LBTN,
332                                     view->eo_btn_lft);
333         view->eo_btn_rht =
334             clk_widget_create_button(view->eo_content, EDJ_NAME,
335                                      STRING_TIMER_MOTION_RBTN,
336                                      &g_color_class_rbtn_hl,
337                                      timer_motion_rbtn_cb, ad);
338         elm_object_part_content_set(view->eo_content, STRING_TIMER_SW_RBTN,
339                                     view->eo_btn_rht);
340
341         edje_object_part_text_set(_EDJ(view->eo_content),
342                                   STRING_TIMER_TIME_LABEL_HOUR,
343                                   STRING_TIMER_IDS_TMR_BODY_HOUR_);
344         edje_object_part_text_set(_EDJ(view->eo_content),
345                                   STRING_TIMER_TIME_LABEL_MINUTE,
346                                   STRING_TIMER_IDS_TMR_BODY_MIN_);
347         edje_object_part_text_set(_EDJ(view->eo_content),
348                                   STRING_TIMER_TIME_LABEL_SECOND,
349                                   STRING_TIMER_IDS_TMR_BODY_SEC_);
350         //
351         //_timer_view_motion_create_clock(ad);
352         //
353         mutex_reset = mutex_stop_restart = 0;
354         //
355         gettimeofday(&ad->start_systime, NULL);
356         _timer_view_motion_timer_cb(ad);
357         ad->timer =
358             ecore_timer_add(ad->timer_interval, _timer_view_motion_timer_cb,
359                             ad);
360         return SUCCESS;
361 }
362
363 //
364 static int _timer_view_motion_update_layout(void *data)
365 {
366         retvm_if(!data, FAILED, "data null");
367         struct appdata *ad = (struct appdata *)data;
368         motion_view *view = ad->view_motion;
369         retvm_if(!view, FAILED, "view null");
370         edje_object_part_text_set(_EDJ(view->eo_content),
371                                                   STRING_TIMER_TIME_LABEL_HOUR,
372                                                   STRING_TIMER_IDS_TMR_BODY_HOUR_);
373         edje_object_part_text_set(_EDJ(view->eo_content),
374                                                   STRING_TIMER_TIME_LABEL_MINUTE,
375                                                   STRING_TIMER_IDS_TMR_BODY_MIN_);
376         edje_object_part_text_set(_EDJ(view->eo_content),
377                                                   STRING_TIMER_TIME_LABEL_SECOND,
378                                                   STRING_TIMER_IDS_TMR_BODY_SEC_);
379
380         //set button
381         view->type_state = TIMER_VIEW_MOTION_STATE_RUN;
382         _timer_view_motion_btn_set(view);
383         return SUCCESS;
384 }
385
386 /**********************************************************************
387 ******************Global function ref*************************************
388 ***********************************************************************/
389
390 /**
391  * Debug function: to handle button clicking logic
392  * This function requires data, obj, emission, source as parameters
393  *
394  * timer_motion_lbtn_cb()
395  * @param[in] data                  Pointer to user data, which contains the data of application
396  * @param[in] obj                    Pointer to button object
397  * @param[in] emission             Pointer to signal
398  * @param[in] source               Pointer to the source from which signal is emitted
399  *
400  * @return      void
401  */
402 void timer_motion_lbtn_cb(void *data, Evas_Object * obj, const char *emission,
403                           const char *source)
404 {
405         retm_if(NULL_CHECK(data), "data null");
406         struct appdata *ad = (struct appdata *)data;
407         const char *str = NULL;
408         str = edje_object_part_text_get(obj, STRING_TIMER_TXT);
409         if (IS_STR_EQUAL(STRING_TIMER_IDS_COM_SK_STOP_S_, str)) {
410                 _timer_view_motion_btn_stop_cb(ad);
411         } else if (IS_STR_EQUAL(STRING_TIMER_IDS_TMR_SK3_RESTART_, str)) {
412                 _timer_view_motion_btn_restart_cb(ad);
413         }
414 }
415
416 //
417 void timer_motion_rbtn_cb(void *data, Evas_Object * obj, const char *emission,
418                           const char *source)
419 {
420         retm_if(NULL_CHECK(data), "data null");
421         struct appdata *ad = (struct appdata *)data;
422         _timer_view_motion_btn_reset_cb(ad);
423 }