2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
17 #include "stopwatch_main.h"
18 #include "stopwatch_util.h"
20 /**********************************************************************
21 ******************define, struct ,typedef, union, enum, global val *************************************
22 ***********************************************************************/
24 /**********************************************************************
25 ******************Local function declear, extern function declear*************************************
26 ***********************************************************************/
27 static void _stw_btn_update(struct appdata *ad);
28 static void _stw_panel_set(void *data);
29 static void _stw_panel_time_update(void *data, double show_time);
31 static Eina_Bool _stw_timer_cb(void *data);
32 static void _stw_lbtn_cb(void *data, Evas_Object * obj, const char *emission,
34 static void _stw_rbtn_cb(void *data, Evas_Object * obj, const char *emission,
36 static void _stw_backbtn_cb(void *data, Evas_Object * obj, void *event_info);
38 static char *_stw_gl_label_get(void *data, Evas_Object * obj, const char *part);
39 static Evas_Object *_stw_gl_icon_get(void *data, Evas_Object * obj,
41 static Eina_Bool _stw_gl_state_get(void *data, Evas_Object * obj,
43 static void _stw_gl_del(void *data, Evas_Object * obj);
44 static void _stw_gl_sel(void *data, Evas_Object * obj, void *event_info);
46 static void _stw_lap_cb(void *data);
47 static void _stw_stop_cb(void *data);
48 static void _stw_start_cb(void *data);
49 static void _stw_restart_cb(void *data);
50 static void _stw_reset_cb(void *data);
51 /**********************************************************************
52 ******************Global val , static global val*************************************
53 ***********************************************************************/
55 static Elm_Genlist_Item_Class g_itc = {
56 .item_style = STRING_STW_LAPLIST,
57 .func.text_get = _stw_gl_label_get,
58 .func.content_get = _stw_gl_icon_get,
59 .func.state_get = _stw_gl_state_get,
60 .func.del = _stw_gl_del,
63 static Edje_Color_Class g_color_class_lbtn_hl = {
64 .name = STRING_STW_LBTN_TXT_COLOR,
65 .r = 249,.g = 249,.b = 249,.a = 255,
66 .r2 = 0,.g2 = 0,.b2 = 0,.a2 = 102,
67 .r3 = 0,.g3 = 0,.b3 = 0,.a3 = 0,
70 static Edje_Color_Class g_color_class_rbtn_hl = {
71 .name = STRING_STW_RBTN_TXT_COLOR,
72 .r = 249,.g = 249,.b = 249,.a = 255,
73 .r2 = 0,.g2 = 0,.b2 = 0,.a2 = 102,
74 .r3 = 0,.g3 = 0,.b3 = 0,.a3 = 0,
77 /**********************************************************************
78 ******************Local function ref*************************************
79 ***********************************************************************/
81 * Debug function: to update btn
84 * @param[in] data Pointer to struct appdata
86 * @return SUCCESS if success or FAILED if failed
88 static void _stw_btn_update(struct appdata *ad)
90 retm_if(NULL_CHECK(ad), "ad null");
91 switch (ad->cur_state) {
92 case STW_STATE_TYPE_RESET:
94 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
96 STRING_STW_IDS_COM_SK_START_S_);
97 edje_object_color_class_set(_EDJ(ad->eo_btn_lft),
98 STRING_STW_LBTN_TXT_COLOR,
99 249, 249, 249, 255, 0, 0, 0,
101 edje_object_signal_emit(_EDJ(ad->eo_btn_lft),
104 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
107 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
110 edje_object_part_text_set(_EDJ(ad->eo_btn_rht),
112 STRING_STW_IDS_COM_BODY_RESET_S_);
113 edje_object_color_class_set(_EDJ(ad->eo_btn_rht),
114 STRING_STW_RBTN_TXT_COLOR,
115 249, 249, 249, 102, 0, 0, 0,
117 edje_object_signal_emit(_EDJ(ad->ly_main), "btn/initial", "btn/initial");
120 case STW_STATE_TYPE_START:
122 edje_object_signal_emit(_EDJ(ad->ly_main), "btn/normal", "btn/normal");
123 edje_object_signal_emit(_EDJ(ad->eo_btn_lft), "lbtn/running", "lbtn/running");
124 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
126 STRING_STW_IDS_COM_SK_STOP_S_);
127 edje_object_color_class_set(_EDJ(ad->eo_btn_lft),
128 STRING_STW_LBTN_TXT_COLOR,
129 249, 249, 249, 255, 0, 0, 0,
132 edje_object_part_text_set(_EDJ(ad->eo_btn_rht),
134 STRING_STW_IDS_SWT_BUTTON_LAP_);
135 if (ad->lap_num < MAX_TAP_SIZE) {
136 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
139 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
142 edje_object_color_class_set(_EDJ
144 STRING_STW_RBTN_TXT_COLOR,
149 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
152 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
155 edje_object_color_class_set(_EDJ
157 STRING_STW_RBTN_TXT_COLOR,
164 case STW_STATE_TYPE_STOP:
166 edje_object_signal_emit(_EDJ(ad->ly_main), "btn/normal", "btn/normal");
167 edje_object_signal_emit(_EDJ(ad->eo_btn_lft), "lbtn/stop", "lbtn/stop");
168 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
170 STRING_STW_IDS_SWT_BUTTON_RESTART_);
171 edje_object_color_class_set(_EDJ(ad->eo_btn_lft),
172 STRING_STW_LBTN_TXT_COLOR,
173 249, 249, 249, 255, 0, 0, 0,
176 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
179 edje_object_signal_emit(_EDJ(ad->eo_btn_rht),
182 edje_object_part_text_set(_EDJ(ad->eo_btn_rht),
184 STRING_STW_IDS_COM_BODY_RESET_S_);
185 edje_object_color_class_set(_EDJ(ad->eo_btn_rht),
186 STRING_STW_RBTN_TXT_COLOR,
187 249, 249, 249, 255, 0, 0, 0,
192 CLK_ERR("error type");
198 * Debug function: to set time panel: STW_PANEL_TYPE_WITHOUT_HOUR/STW_PANEL_TYPE_WITH_HOUR
201 * @param[in] data Pointer to struct appdata
205 static void _stw_panel_set(void *data)
207 retm_if(!data, "data null");
208 struct appdata *ad = (struct appdata *)data;
213 char *str_panel = STRING_STW_PANEL_WITH_HOUR;
215 elm_object_part_content_unset(ad->ly_main, STRING_STW_TIME_PANEL);
216 EVAS_OBJECT_DELIF(ad->eo_panel);
218 ad->eo_panel = load_edj(ad->ly_main, EDJ_FILE, str_panel);
219 elm_object_part_content_set(ad->ly_main, STRING_STW_TIME_PANEL,
224 * Debug function: to update time_panel
226 * _stw_panel_time_update()
227 * @param[in] data Pointer to struct appdata
228 * @param[in] show_time Double to show
232 static void _stw_panel_time_update(void *data, double show_time)
234 retm_if(!data, "data null");
235 struct appdata *ad = (struct appdata *)data;
236 char time_str[6] = { 0 };
237 time_t sec = stw_systime_double_to_time_t(show_time, STW_TIME_TYPE_SEC);
238 struct tm tm_show_time;
239 gmtime_r(&sec, &tm_show_time);
240 // retm_if(!tm_show_time, "tm_show_time null");
242 stw_systime_double_to_time_t(show_time, STW_TIME_TYPE_USEC);
246 // begin to display time elapsing:--> hour unit of time
247 snprintf(time_str, sizeof(time_str), "%d",
248 (int)(tm_show_time.tm_hour / 10));
249 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_H1);
250 snprintf(time_str, sizeof(time_str), "%d",
251 (int)(tm_show_time.tm_hour % 10));
252 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_H2);
254 // begin to display time elapsing:--> minute unit of time
255 snprintf(time_str, sizeof(time_str), "%d",
256 (int)(tm_show_time.tm_min / 10));
257 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_M1);
258 snprintf(time_str, sizeof(time_str), "%d",
259 (int)(tm_show_time.tm_min % 10));
260 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_M2);
262 // begin to display time elapsing:--> second unit of time
263 snprintf(time_str, sizeof(time_str), "%d",
264 (int)(tm_show_time.tm_sec / 10));
265 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_S1);
266 snprintf(time_str, sizeof(time_str), "%d",
267 (int)(tm_show_time.tm_sec % 10));
268 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_S2);
270 // begin to display time elapsing:--> microsecond unit of time
271 snprintf(time_str, sizeof(time_str), "%d", (int)(usec / 10));
272 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_SM1);
273 snprintf(time_str, sizeof(time_str), "%d", (int)(usec % 10));
274 edje_object_signal_emit(_EDJ(ad->eo_panel), time_str, STRING_STW_SM2);
278 * Debug function: timer callback
281 * @param[in] data Pointer to struct appdata
285 static Eina_Bool _stw_timer_cb(void *data)
287 retvm_if(!data, EINA_FALSE, "data null");
288 struct appdata *ad = (struct appdata *)data;
291 panel_time = stw_get_systime() - ad->start_time + ad->elapsed_time;
292 //frist, update panel
293 _stw_panel_time_update(ad, panel_time);
298 * Debug function: to handle main button click logic
299 * This function requires data as parameters
302 * @param[in] data Pointer to user data, which contains the data of application
303 * @param[in] obj Pointer to button object
304 * @param[in] emission Pointer to signal
305 * @param[in] source Pointer to the source from which signal is emitted
309 static void _stw_lbtn_cb(void *data, Evas_Object * obj, const char *emission,
313 retm_if(NULL_CHECK(data), "data null");
314 struct appdata *ad = (struct appdata *)data;
315 const char *str = NULL;
316 str = edje_object_part_text_get(obj, STRING_STW_TXT);
317 if (IS_STR_EQUAL(str, STRING_STW_IDS_COM_SK_START_S_)) {
319 } else if (IS_STR_EQUAL(str, STRING_STW_IDS_COM_SK_STOP_S_)) {
321 } else if (IS_STR_EQUAL(str, STRING_STW_IDS_SWT_BUTTON_RESTART_)) {
328 * Debug function: to handle main button click logic
329 * This function requires data as parameters
332 * @param[in] data Pointer to user data, which contains the data of application
333 * @param[in] obj Pointer to button object
334 * @param[in] emission Pointer to signal
335 * @param[in] source Pointer to the source from which signal is emitted
339 static void _stw_rbtn_cb(void *data, Evas_Object * obj, const char *emission,
342 retm_if(NULL_CHECK(data), "data null");
343 struct appdata *ad = (struct appdata *)data;
344 const char *str = NULL;
345 str = edje_object_part_text_get(obj, STRING_STW_TXT);
346 if (IS_STR_EQUAL(str, STRING_STW_IDS_SWT_BUTTON_LAP_)) {
348 } else if (IS_STR_EQUAL(str, STRING_STW_IDS_COM_BODY_RESET_S_)) {
353 static void _stw_backbtn_cb(void *data, Evas_Object * obj, void *event_info)
356 Evas_Object *win = (Evas_Object *) data;
361 * Debug function: to get label text for genlist item
362 * This function requires data, obj and part as parameters
365 * @param[in] data Pointer to user data, which contains the part text of genlist item
366 * @param[in] obj Pointer to part object of current item
367 * @param[in] part Pointer to part name of current item
369 * @return char*, It is the pointer to label text.
371 static char *_stw_gl_label_get(void *data, Evas_Object * obj, const char *part)
373 struct lap_data *items = (struct lap_data *)data;
375 if (IS_STR_EQUAL(part, STRING_STW_INDEX)) {
376 return strdup(items->index);
377 } else if (IS_STR_EQUAL(part, STRING_STW_LAP_TXT)) {
378 return strdup(items->lap_data);
379 } else if (IS_STR_EQUAL(part, STRING_STW_LAP_SMALL_TXT)) {
380 return strdup(items->lap_data_small);
381 } else if (IS_STR_EQUAL(part, "lap_text_interval")) {
382 return strdup(items->interval_data);
388 * Debug function: to get icons for genlist item
389 * This function requires data, obj and part as parameters
392 * @param[in] data Pointer to user data, which contains the icon of genlist item
393 * @param[in] obj Pointer to part object of current item
394 * @param[in] part Pointer to part name of current item
396 * @return Evas_Object*, It is the pointer to icon.
398 static Evas_Object *_stw_gl_icon_get(void *data, Evas_Object * obj,
405 * Debug function: to get state for genlist item
406 * This function requires data, obj and part as parameters
409 * @param[in] data Pointer to user data, which contains the data of genlist item
410 * @param[in] obj Pointer to part object of current item
411 * @param[in] part Pointer to part name of current item
415 static Eina_Bool _stw_gl_state_get(void *data, Evas_Object * obj,
422 * Debug function: to delete the current genlist item
423 * This function requires data, obj as parameters
426 * @param[in] data Pointer to user data, which contains the data of genlist item
427 * @param[in] obj Pointer to current item
431 static void _stw_gl_del(void *data, Evas_Object * obj)
433 struct lap_data *item_data = (struct lap_data *)data;
438 * Debug function: to select genlist item
439 * This function requires data, obj and event_info as parameters
442 * @param[in] data Pointer to user data, which contains the data of genlist item
443 * @param[in] obj Pointer to current item
444 * @param[in] event_info Pointer to current item
448 static void _stw_gl_sel(void *data, Evas_Object * obj, void *event_info)
450 Elm_Object_Item *gli = (Elm_Object_Item *) (event_info);
451 elm_genlist_item_selected_set(gli, 0);
455 * Debug function: to implement lap logic when lap button is pressed
456 * This function requires data as parameters
459 * @param[in] data Pointer to user data, which contains the data of application
463 static void _stw_lap_cb(void *data)
465 retm_if(!data, "data null");
466 double interval_time;
468 struct appdata *ad = (struct appdata *)data;
469 struct lap_data *items = CALLOC(1, struct lap_data);
470 retm_if(NULL_CHECK(items), "items null");
472 interval_time = ad->last_lap_time;
475 stw_get_systime() - ad->start_time + ad->elapsed_time;
477 /* The interval is the difference between the previous lap time and this
479 interval_time = ad->last_lap_time - interval_time;
481 time_t sec = stw_systime_double_to_time_t(interval_time, STW_TIME_TYPE_SEC);
483 stw_systime_double_to_time_t(interval_time, STW_TIME_TYPE_USEC);
485 char *str_format = "%02H:%02M:%S.";
486 //IS_EQUAL(ad->cur_panel, STW_PANEL_TYPE_WITH_HOUR) ?
487 //"%02H:%02M:%S." : "%02M:%S.";
489 struct tm tm_show_time;
490 gmtime_r(&sec, &tm_show_time);
491 // retm_if(!tm_show_time, "tm_show_time null");
492 strftime(items->interval_data, sizeof(items->interval_data), str_format, &tm_show_time);
493 snprintf(items->lap_data_small, sizeof(items->lap_data_small), "%02d", (int)usec);
496 stw_systime_double_to_time_t(ad->last_lap_time, STW_TIME_TYPE_SEC);
497 char tmp_str_time[BUF_SIZE] = { 0 };
498 str_format = "%02H:%02M:%S:";
499 gmtime_r(&sec, &tm_show_time);
500 strftime(tmp_str_time, sizeof(tmp_str_time), str_format, &tm_show_time);
503 stw_systime_double_to_time_t(ad->last_lap_time, STW_TIME_TYPE_USEC);
504 snprintf(items->lap_data, sizeof(items->lap_data), "%s%02d", tmp_str_time, (int)usec);
506 // to check terminate condition
507 if (ad->lap_num >= MAX_TAP_SIZE) {
508 edje_object_signal_emit(_EDJ(ad->eo_btn_rht), STRING_STW_HIDE,
510 edje_object_signal_emit(_EDJ(ad->eo_btn_rht), STRING_STW_DIM,
512 edje_object_part_text_set(_EDJ(ad->eo_btn_rht), STRING_STW_TXT,
513 STRING_STW_IDS_SWT_BUTTON_LAP_);
514 edje_object_color_class_set(_EDJ(ad->eo_btn_rht),
515 STRING_STW_RBTN_TXT_COLOR, 255, 255,
516 255, 102, 0, 0, 0, 102, 0, 0, 0, 0);
518 snprintf(items->index, sizeof(items->index), "%.2d", ad->lap_num);
519 // to add a genlist item before genlist
520 Elm_Object_Item *new_item = elm_genlist_item_prepend(ad->gl, &g_itc,
523 ELM_GENLIST_ITEM_NONE,
526 // show new item on the top
527 elm_genlist_item_show(new_item, ELM_GENLIST_ITEM_SCROLLTO_TOP);
531 * Debug function: to implement stop logic when stop button is pressed
532 * This function requires data as parameters
535 * @param[in] data Pointer to user data, which contains the data of application
539 static void _stw_stop_cb(void *data)
541 retm_if(!data, "data null");
542 struct appdata *ad = (struct appdata *)data;
544 ad->cur_state = STW_STATE_TYPE_STOP;
545 ad->elapsed_time += stw_get_systime() - ad->start_time;
546 ecore_timer_freeze(ad->timer);
552 * Debug function: to implement start logic when start button is pressed
553 * This function requires data as parameters
556 * @param[in] data Pointer to user data, which contains the data of application
560 static void _stw_start_cb(void *data)
562 retm_if(!data, "data null");
563 struct appdata *ad = (struct appdata *)data;
565 ad->cur_state = STW_STATE_TYPE_START;
566 ad->start_time = stw_get_systime();
567 ecore_timer_thaw(ad->timer);
573 * Debug function: to implement restart logic when restart button is pressed
574 * This function requires data as parameters
577 * @param[in] data Pointer to user data, which contains the data of application
581 static void _stw_restart_cb(void *data)
587 * Debug function: to implement reset logic when reset button is pressed
588 * This function requires data as parameters
591 * @param[in] data Pointer to user data, which contains the data of application
595 static void _stw_reset_cb(void *data)
597 retm_if(!data, "data null");
598 struct appdata *ad = (struct appdata *)data;
600 ad->cur_state = STW_STATE_TYPE_RESET;
602 ad->elapsed_time = 0;
603 ad->last_lap_time = 0;
605 elm_genlist_clear(ad->gl);
607 _stw_panel_time_update(ad, 0);
611 /**********************************************************************
612 ******************Global function ref*************************************
613 ***********************************************************************/
616 int stw_create_main_view(struct appdata *ad)
618 ad->layout_main = widget_create_layout_empty(ad->parent);
619 ad->ly_main = load_edj(ad->layout_main, EDJ_FILE, GRP_MAIN);
622 clk_widget_create_button(ad->layout_main, EDJ_FILE,
624 &g_color_class_lbtn_hl, _stw_lbtn_cb, ad);
625 elm_object_part_content_set(ad->ly_main, STRING_STW_LBTN,
628 clk_widget_create_button(ad->layout_main, EDJ_FILE,
630 &g_color_class_rbtn_hl, _stw_rbtn_cb, ad);
631 elm_object_part_content_set(ad->ly_main, STRING_STW_RBTN,
634 widget_create_button(ad->layout_main,
635 STRING_STW_NAVIFRAME_END_BTN_DEFAULT, NULL,
636 NULL, _stw_backbtn_cb, ad->win);
638 //create navigation bar
639 ad->navi_bar = elm_naviframe_add(ad->layout_main);
641 widget_naviframe_push(ad->navi_bar,
642 STRING_STW_IDS_COM_BODY_STOPWATCH_S_,
643 ad->eo_btn_back, NULL, ad->ly_main, "tabbar",
645 elm_naviframe_item_title_visible_set(ad->ei_naviframe, EINA_FALSE);
646 elm_object_part_content_set(ad->layout_main,
647 STRING_STW_SLM_SWALLOW_CONTENT,
650 ad->gl = elm_genlist_add(ad->ly_main);
651 elm_object_part_content_set(ad->ly_main, STRING_STW_GENLIST, ad->gl);
653 ad->timer = ecore_timer_add(STW_TIMER_VALUE, _stw_timer_cb, ad);
654 ecore_timer_freeze(ad->timer);
656 ad->cur_state = STW_STATE_TYPE_RESET;
658 ad->elapsed_time = 0;
659 ad->last_lap_time = 0;
666 int stw_update_main_view(struct appdata *ad)
668 retm_if(NULL_CHECK(ad), "ad null");
669 switch (ad->cur_state) {
670 case STW_STATE_TYPE_RESET:
672 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
674 STRING_STW_IDS_COM_SK_START_S_);
675 edje_object_part_text_set(_EDJ(ad->eo_btn_rht),
677 STRING_STW_IDS_COM_BODY_RESET_S_);
680 case STW_STATE_TYPE_START:
683 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
685 STRING_STW_IDS_COM_SK_STOP_S_);
686 edje_object_part_text_set(_EDJ(ad->eo_btn_rht),
688 STRING_STW_IDS_SWT_BUTTON_LAP_);
692 case STW_STATE_TYPE_STOP:
694 edje_object_part_text_set(_EDJ(ad->eo_btn_lft),
696 STRING_STW_IDS_SWT_BUTTON_RESTART_);
700 CLK_ERR("error type");