2 * Copyright 2012 Samsung Electronics Co., Ltd
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
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.
21 #include <runtime_info.h>
23 #include <unicode/udat.h>
24 #include <unicode/udatpg.h>
27 #include "indicator.h"
28 #include "indicator_ui.h"
29 #include "indicator_gui.h"
30 #include "indicator_icon_util.h"
33 #define SYSTEM_RESUME "system_wakeup"
35 #define TIME_FONT_SIZE_24 34
36 #define TIME_FONT_SIZE_12 30
37 #define TIME_FONT_SIZE_BATTERY 32
38 #define TIME_FONT_COLOR 243, 243, 243, 255
40 #define AMPM_FONT_SIZE 24
41 #define AMPM_FONT_COLOR 243, 243, 243, 255
42 #define LABEL_STRING "<font_size=%d>%s" \
45 #define CLOCK_STR_LEN 256
48 INDICATOR_CLOCK_MODE_12H = 0,
49 INDICATOR_CLOCK_MODE_24H,
50 INDICATOR_CLOCK_MODE_MAX
54 static int clock_mode = INDICATOR_CLOCK_MODE_12H;
55 static int apm_length = 0;
56 static int apm_position = 0;
57 static Ecore_Timer *timer = NULL;
58 static Ecore_Timer *battery_timer = NULL;
60 static int register_clock_module(void *data);
61 static int unregister_clock_module(void);
62 static int hib_enter_clock_module(void);
63 static int hib_leave_clock_module(void *data);
64 static int language_changed_cb(void *data);
65 static int region_changed_cb(void *data);
67 #define ICON_PRIORITY INDICATOR_PRIORITY_FIXED6
68 #define MODULE_NAME "clock"
70 static void indicator_get_time_by_region(char* output, void* data);
71 static void ICU_set_timezone(const char *timezone);
73 Indicator_Icon_Object sysclock[INDICATOR_WIN_MAX] = {
75 .win_type = INDICATOR_WIN_PORT,
76 .type = INDICATOR_TXT_ICON,
78 .priority = ICON_PRIORITY,
79 .always_top = EINA_FALSE,
82 .obj_exist = EINA_FALSE,
83 .exist_in_view = EINA_FALSE,
84 .init = register_clock_module,
85 .fini = unregister_clock_module,
86 .hib_enter = hib_enter_clock_module,
87 .hib_leave = hib_leave_clock_module,
89 .region_changed = region_changed_cb,
90 .lang_changed = language_changed_cb
93 .win_type = INDICATOR_WIN_LAND,
94 .type = INDICATOR_TXT_ICON,
96 .priority = ICON_PRIORITY,
97 .always_top = EINA_FALSE,
100 .obj_exist = EINA_FALSE,
101 .exist_in_view = EINA_FALSE,
102 .init = register_clock_module,
103 .fini = unregister_clock_module,
104 .hib_enter = hib_enter_clock_module,
105 .hib_leave = hib_leave_clock_module,
106 .lang_changed = NULL,
107 .region_changed = region_changed_cb,
108 .lang_changed = language_changed_cb
112 static void set_app_state(void* data)
116 for (i=0 ; i<INDICATOR_WIN_MAX ; i++)
118 sysclock[i].ad = data;
122 static void indicator_clock_changed_cb(void *data)
124 struct appdata *ad = (struct appdata *)data;
126 char time_buf[128], ampm_buf[128];
127 char buf[CLOCK_STR_LEN];
128 char icu_apm[CLOCK_STR_LEN] = {0,};
129 char apm_result[CLOCK_STR_LEN] ={0,};
130 struct tm *ts = NULL;
135 retif(data == NULL, , "Invalid parameter!");
137 if (battery_timer != NULL)
139 DBG("battery is displaying. ignore clock callback");
144 ts = localtime(&ctime);
149 ecore_timer_del(timer);
153 memset(time_str, 0x00, sizeof(time_str));
154 memset(time_buf, 0x00, sizeof(time_buf));
155 memset(ampm_buf, 0x00, sizeof(ampm_buf));
156 memset(buf, 0x00, sizeof(buf));
159 ecore_timer_add(60 - ts->tm_sec, (void *)indicator_clock_changed_cb,
162 indicator_get_time_by_region(icu_apm,data);
164 if (clock_mode == INDICATOR_CLOCK_MODE_12H) {
165 char bf1[32] = { 0, };
168 if(apm_length>0 && apm_length<=4)
170 snprintf(ampm_buf, sizeof(ampm_buf),LABEL_STRING, AMPM_FONT_SIZE,icu_apm);
174 if (ts->tm_hour >= 0 && ts->tm_hour < 12)
175 snprintf(ampm_buf, sizeof(ampm_buf),
176 LABEL_STRING, AMPM_FONT_SIZE,
179 snprintf(ampm_buf, sizeof(ampm_buf),
180 LABEL_STRING, AMPM_FONT_SIZE,
184 strftime(bf1, sizeof(bf1), "%l", ts);
186 strftime(bf1, sizeof(bf1), ":%M", ts);
188 snprintf(time_str, sizeof(time_str), "%d%s", hour, bf1);
189 font_size = TIME_FONT_SIZE_12;
190 indicator_signal_emit(data,"indicator.clock.ampm","indicator.prog");
193 font_size = TIME_FONT_SIZE_24;
194 strftime(time_str, sizeof(time_str), "%H:%M", ts);
195 indicator_signal_emit(data,"indicator.clock.default","indicator.prog");
198 snprintf(time_buf, sizeof(time_buf), LABEL_STRING, font_size, time_str);
200 if(apm_position == 0)
201 len = snprintf(buf, sizeof(buf), "%s%s", ampm_buf, time_buf);
203 len = snprintf(buf, sizeof(buf), "%s%s", time_buf, ampm_buf);
206 ERR("Unexpected ERROR!");
210 INFO("[CLOCK MODULE] Timer Status : %d Time: %s", timer, buf);
212 indicator_part_text_emit(data,"elm.text.clock", buf);
217 static void indicator_clock_format_changed_cb(keynode_t *node, void *data)
219 retif(data == NULL, , "Invalid parameter!");
223 bool is_24hour_enabled = false;
225 INFO("[Enter] indicator_clock_format_changed_cb");
227 r = runtime_info_get_value_bool(
228 RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
230 if( r==RUNTIME_INFO_ERROR_NONE&&is_24hour_enabled==true)
232 clock_mode = INDICATOR_CLOCK_MODE_24H;
236 clock_mode = INDICATOR_CLOCK_MODE_12H;
239 char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
240 ICU_set_timezone(timezone);
241 indicator_clock_changed_cb(data);
245 static void indicator_clock_battery_changed_cb(keynode_t *node, void *data)
247 indicator_clock_display_battery_percentage(data);
250 static void indicator_clock_battery_display_cb(void *data)
254 if (battery_timer != NULL) {
255 ecore_timer_del(battery_timer);
256 battery_timer = NULL;
259 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
260 indicator_clock_battery_changed_cb);
262 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
264 indicator_clock_changed_cb(data);
267 void indicator_clock_display_battery_percentage(void *data)
271 int battery_capa = 0;
272 char buf[256] = {0,};
273 char temp[256] = {0,};
275 ret = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &status);
277 ERR("Fail to get [%s: %d]",
278 VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, ret);
282 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_capa);
285 ERR("Fail to get [VCONFKEY_SYSMAN_BATTERY_CAPACITY:%d]", ret);
288 if (battery_capa < 0)
290 INFO("Invalid Battery Capacity: %d", battery_capa);
294 INFO("Battery Capacity: %d", battery_capa);
296 if (battery_capa > 100)
299 snprintf(temp, sizeof(temp), "%d%%",battery_capa);
301 snprintf(buf, sizeof(buf), LABEL_STRING, TIME_FONT_SIZE_BATTERY, temp);
303 INFO("indicator_clock_display_battery_percentage %s", buf);
305 indicator_part_text_emit(data,"elm.text.clock", buf);
307 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
308 indicator_clock_battery_changed_cb, data);
310 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
314 battery_timer = ecore_timer_add(3, (void *)indicator_clock_battery_display_cb,data);
320 static int language_changed_cb(void *data)
322 DBG("language_changed_cb");
323 indicator_clock_changed_cb(data);
327 static int region_changed_cb(void *data)
329 DBG("region_changed_cb");
330 indicator_clock_format_changed_cb(NULL, data);
334 static int register_clock_module(void *data)
338 retif(data == NULL, FAIL, "Invalid parameter!");
342 notifd = heynoti_init();
345 ERR("heynoti_init is failed");
350 heynoti_subscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb,
353 ERR("Fail: register SYSTEM_RESUME");
357 ret = heynoti_attach_handler(notifd);
359 ERR("Failed to attach heynoti handler!");
363 ret = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
364 indicator_clock_format_changed_cb, data);
366 ERR("Fail: register VCONFKEY_SYSTEM_TIME_CHANGED");
370 ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
371 indicator_clock_format_changed_cb, data);
373 ERR("Fail: register VCONFKEY_REGIONFORMAT_TIME1224");
377 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
378 indicator_clock_format_changed_cb, data);
380 ERR("Fail: register VCONFKEY_SETAPPL_TIMEZONE_INT");
384 ret = vconf_notify_key_changed(VCONFKEY_PM_STATE, indicator_clock_format_changed_cb, (void *)data);
387 ERR("Fail: register VCONFKEY_PM_STATE");
391 indicator_clock_format_changed_cb(NULL, data);
396 static int unregister_clock_module(void)
400 heynoti_unsubscribe(notifd, SYSTEM_RESUME, indicator_clock_changed_cb);
402 heynoti_close(notifd);
405 ret = vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
406 indicator_clock_format_changed_cb);
408 ERR("Fail: unregister VCONFKEY_SYSTEM_TIME_CHANGED");
410 ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
411 indicator_clock_format_changed_cb);
413 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
415 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT,
416 indicator_clock_format_changed_cb);
418 ERR("Fail: unregister VCONFKEY_SETAPPL_TIMEZONE_INT");
420 ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE,
421 indicator_clock_format_changed_cb);
423 ERR("Fail: unregister VCONFKEY_PM_STATE");
426 ecore_timer_del(timer);
432 static int hib_enter_clock_module(void)
436 ret = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
437 indicator_clock_format_changed_cb);
439 ERR("Fail: unregister VCONFKEY_REGIONFORMAT_TIME1224");
442 ecore_timer_del(timer);
449 static int hib_leave_clock_module(void *data)
453 retif(data == NULL, FAIL, "Invalid parameter!");
455 ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
456 indicator_clock_format_changed_cb, data);
457 retif(ret != OK, FAIL, "Failed to register callback!");
459 indicator_clock_format_changed_cb(NULL, data);
463 void indicator_get_time_by_region(char* output,void *data)
465 retif(data == NULL, NULL, "Data parameter is NULL");
466 retif(output == NULL, NULL, "output parameter is NULL");
469 UChar customSkeleton[CLOCK_STR_LEN] = { 0, };
470 UErrorCode status = U_ZERO_ERROR;
471 UDateFormat *formatter = NULL;
473 UChar bestPattern[CLOCK_STR_LEN] = { 0, };
474 UChar formatted[CLOCK_STR_LEN] = { 0, };
476 char bestPatternString[CLOCK_STR_LEN] = { 0, };
477 char formattedString[CLOCK_STR_LEN] = { 0, };
479 UDateTimePatternGenerator *pattern_generator = NULL;
481 char *time_skeleton = "hhmm";
483 char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);
484 if (locale == NULL) {
485 DBG("[Error] get value of VCONFKEY_REGIONFORMAT fail.");
488 u_uastrncpy(customSkeleton, time_skeleton, strlen(time_skeleton));
490 pattern_generator = udatpg_open(locale, &status);
492 int32_t bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
493 (void)udatpg_getBestPattern(pattern_generator, customSkeleton,
494 u_strlen(customSkeleton), bestPattern,
495 bestPatternCapacity, &status);
497 u_austrcpy(bestPatternString, bestPattern);
498 u_uastrcpy(bestPattern,"a");
500 if(bestPatternString[0] == 'a')
509 UDate date = ucal_getNow();
510 formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
511 int32_t formattedCapacity = (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
512 (void)udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
513 u_austrcpy(formattedString, formatted);
515 DBG("DATE & TIME is %s %s %d %s", locale, formattedString, u_strlen(formatted), bestPatternString);
517 apm_length = u_strlen(formatted);
519 udatpg_close(pattern_generator);
521 udat_close(formatter);
523 if(strlen(formattedString)<CLOCK_STR_LEN)
525 strncpy(output,formattedString,strlen(formattedString));
529 strncpy(output,formattedString,CLOCK_STR_LEN-1);
535 static UChar *uastrcpy(const char *chars)
540 str = (UChar *) malloc(sizeof(UChar) *(len + 1));
543 u_uastrcpy(str, chars);
547 static void ICU_set_timezone(const char *timezone)
549 DBG("ICU_set_timezone = %s ", timezone);
550 UErrorCode ec = U_ZERO_ERROR;
551 UChar *str = uastrcpy(timezone);
553 ucal_setDefaultTimeZone(str, &ec);
555 DBG("ucal_setDefaultTimeZone() SUCCESS ");
557 DBG("ucal_setDefaultTimeZone() FAILED : %s ",