intuiclock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * Copyright (c) 2015 Tomek Wójcik <tomek@bthlabs.pl>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include <pebble.h>
  23. static Window *s_main_window;
  24. static GBitmap *s_background_bitmap;
  25. static GBitmap *s_battery_bitmap;
  26. static GFont s_text_font;
  27. static GPath *s_minute_hand;
  28. static GPath *s_hour_hand;
  29. static BitmapLayer *s_background_layer;
  30. static TextLayer *s_title_layer;
  31. static Layer *s_hands_layer;
  32. static TextLayer *s_date_layer;
  33. static TextLayer *s_ampm_layer;
  34. static BitmapLayer *s_battery_icon_layer;
  35. static Layer *s_battery_indicator_layer;
  36. static struct tm *t = NULL;
  37. static uint8_t battery_charge = 0;
  38. static bool hide_seconds_hand = false;
  39. static bool hide_battery_indicator = false;
  40. static char date_format[2];
  41. static bool use_wide_layout = false;
  42. static bool use_xen_style = false;
  43. static const GPathInfo MINUTE_HAND_POINTS = {
  44. 4,
  45. (GPoint []) {
  46. { 0, 0 },
  47. { -4.5, -26 },
  48. { 0, -38 },
  49. { 4.5, -26 }
  50. }
  51. };
  52. static const GPathInfo HOUR_HAND_POINTS = {
  53. 4,
  54. (GPoint []) {
  55. { 0, 0 },
  56. { -4.5, -22 },
  57. { 0, -29 },
  58. { 4.5, -22 }
  59. }
  60. };
  61. #define AMPM_TEXT_SIZE 3
  62. #define DUMMY_AMPM_TEXT ""
  63. #define DATE_TEXT_SIZE 12
  64. #define DUMMY_DATE_TEXT "23 Jul 1985"
  65. #define KEY_SECONDS_HAND 0
  66. #define KEY_BATTERY_INDICATOR 1
  67. #define KEY_DATE_FORMAT 2
  68. #define KEY_WIDE_LAYOUT 3
  69. #define KEY_XEN_STYLE 4
  70. static void update_date_text_layer() {
  71. static char date_text[DATE_TEXT_SIZE];
  72. size_t result = 0;
  73. if (strcmp(date_format, "d") == 0) {
  74. result = strftime(date_text, DATE_TEXT_SIZE, "%d/%m/%y", t);
  75. } else if (strcmp(date_format, "m") == 0) {
  76. result = strftime(date_text, DATE_TEXT_SIZE, "%m/%d/%y", t);
  77. } else {
  78. result = strftime(date_text, DATE_TEXT_SIZE, "%e %b %Y", t);
  79. }
  80. if (result != 0) {
  81. text_layer_set_text(s_date_layer, date_text);
  82. } else {
  83. text_layer_set_text(s_date_layer, DUMMY_DATE_TEXT);
  84. }
  85. }
  86. static void update_ampm_text_layer() {
  87. static char ampm_text[AMPM_TEXT_SIZE];
  88. if (clock_is_24h_style()) {
  89. text_layer_set_text(s_ampm_layer, DUMMY_AMPM_TEXT);
  90. } else {
  91. size_t result = strftime(ampm_text, AMPM_TEXT_SIZE, "%p", t);
  92. if (result != 0) {
  93. text_layer_set_text(s_ampm_layer, ampm_text);
  94. } else {
  95. text_layer_set_text(s_ampm_layer, DUMMY_AMPM_TEXT);
  96. }
  97. }
  98. text_layer_set_text(s_ampm_layer, ampm_text);
  99. }
  100. static void tick_handler(struct tm *tick_time, TimeUnits changed_units) {
  101. // Getting the current time.
  102. time_t now = time(NULL);
  103. t = localtime(&now);
  104. // Updating views.
  105. layer_mark_dirty(s_hands_layer);
  106. update_date_text_layer();
  107. update_ampm_text_layer();
  108. }
  109. static void s_hands_layer_update(Layer *layer, GContext *ctx) {
  110. GRect bounds = layer_get_bounds(layer);
  111. GPoint center = grect_center_point(&bounds);
  112. // Drawing the minute hand.
  113. graphics_context_set_fill_color(ctx, GColorBlack);
  114. graphics_context_set_stroke_color(ctx, GColorBlack);
  115. gpath_move_to(s_minute_hand, (GPoint){39.5, 39});
  116. gpath_rotate_to(s_minute_hand, TRIG_MAX_ANGLE * t->tm_min / 60);
  117. gpath_draw_outline(ctx, s_minute_hand);
  118. gpath_draw_filled(ctx, s_minute_hand);
  119. // Drawing the hour hand.
  120. graphics_context_set_fill_color(ctx, GColorBlack);
  121. graphics_context_set_stroke_color(ctx, GColorBlack);
  122. gpath_move_to(s_hour_hand, (GPoint){39.5, 39});
  123. gpath_rotate_to(s_hour_hand, (TRIG_MAX_ANGLE * (((t->tm_hour % 12) * 6) + (t->tm_min / 10))) / (12 * 6));
  124. gpath_draw_outline(ctx, s_hour_hand);
  125. gpath_draw_filled(ctx, s_hour_hand);
  126. // Drawing the second hand.
  127. if (hide_seconds_hand == false) {
  128. graphics_context_set_antialiased(ctx, false);
  129. int32_t second_angle = TRIG_MAX_ANGLE * t->tm_sec / 60;
  130. int16_t second_hand_length = (bounds.size.w / 2) - 1;
  131. GPoint second_hand = {
  132. .x = (int16_t)(sin_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.x,
  133. .y = (int16_t)(-cos_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.y,
  134. };
  135. graphics_context_set_stroke_color(ctx, GColorPictonBlue);
  136. graphics_draw_line(ctx, second_hand, center);
  137. graphics_context_set_antialiased(ctx, true);
  138. }
  139. }
  140. static void s_battery_indicator_layer_update(Layer *layer, GContext *ctx) {
  141. GRect bounds = layer_get_bounds(layer);
  142. graphics_context_set_fill_color(ctx, GColorGreen);
  143. if (battery_charge <= 10) {
  144. graphics_context_set_fill_color(ctx, GColorRed);
  145. } else if (battery_charge <= 60) {
  146. graphics_context_set_fill_color(ctx, GColorYellow);
  147. }
  148. graphics_fill_rect(ctx, bounds, 0, GCornerNone);
  149. }
  150. static void battery_state_handler(BatteryChargeState charge) {
  151. if (charge.charge_percent != battery_charge) {
  152. battery_charge = charge.charge_percent;
  153. layer_mark_dirty(s_battery_indicator_layer);
  154. }
  155. }
  156. static void start_time_tracking() {
  157. TimeUnits unit = MINUTE_UNIT;
  158. if (hide_seconds_hand == false) {
  159. unit = SECOND_UNIT;
  160. }
  161. tick_handler(NULL, unit);
  162. tick_timer_service_subscribe(unit, tick_handler);
  163. }
  164. static void start_battery_tracking() {
  165. if (hide_battery_indicator == false) {
  166. BatteryChargeState charge = battery_state_service_peek();
  167. battery_state_handler(charge);
  168. battery_state_service_subscribe(battery_state_handler);
  169. } else {
  170. battery_state_service_unsubscribe();
  171. }
  172. layer_set_hidden(bitmap_layer_get_layer(s_battery_icon_layer), hide_battery_indicator);
  173. layer_set_hidden(s_battery_indicator_layer, hide_battery_indicator);
  174. }
  175. static void update_geometry() {
  176. if (s_background_bitmap) {
  177. gbitmap_destroy(s_background_bitmap);
  178. }
  179. if (use_wide_layout == false) {
  180. if (use_xen_style == false) {
  181. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BKG_CLASSIC_NARROW);
  182. } else {
  183. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BKG_XEN_NARROW);
  184. }
  185. layer_set_frame(text_layer_get_layer(s_title_layer), GRect(36, 2, 46, 18));
  186. layer_set_frame(text_layer_get_layer(s_date_layer), GRect(17, 130, 109, 16));
  187. layer_set_frame(text_layer_get_layer(s_ampm_layer), GRect(108, 26, 16, 16));
  188. layer_set_frame(bitmap_layer_get_layer(s_battery_icon_layer), GRect(20, 26, 16, 7));
  189. layer_set_frame(s_battery_indicator_layer, GRect(21, 27, 13, 5));
  190. } else {
  191. if (use_xen_style == false) {
  192. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BKG_CLASSIC_WIDE);
  193. } else {
  194. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BKG_XEN_WIDE);
  195. }
  196. layer_set_frame(text_layer_get_layer(s_title_layer), GRect(24, 2, 71, 18));
  197. layer_set_frame(text_layer_get_layer(s_date_layer), GRect(4, 130, 136, 16));
  198. layer_set_frame(text_layer_get_layer(s_ampm_layer), GRect(123, 25, 16, 16));
  199. layer_set_frame(bitmap_layer_get_layer(s_battery_icon_layer), GRect(7, 26, 16, 7));
  200. layer_set_frame(s_battery_indicator_layer, GRect(8, 27, 13, 5));
  201. }
  202. bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  203. }
  204. static void inbox_received_handler(DictionaryIterator *iter, void *context) {
  205. // Handling seconds_hand setting.
  206. Tuple *seconds_hand_t = dict_find(iter, KEY_SECONDS_HAND);
  207. if (seconds_hand_t && seconds_hand_t->value->int32 > 0) {
  208. hide_seconds_hand = false;
  209. } else {
  210. hide_seconds_hand = true;
  211. }
  212. persist_write_bool(KEY_SECONDS_HAND, hide_seconds_hand);
  213. start_time_tracking();
  214. // Handling battery_indicator setting.
  215. Tuple *battery_indicator_t = dict_find(iter, KEY_BATTERY_INDICATOR);
  216. if (battery_indicator_t && battery_indicator_t->value->int32 > 0) {
  217. hide_battery_indicator = false;
  218. } else {
  219. hide_battery_indicator = true;
  220. }
  221. persist_write_bool(KEY_BATTERY_INDICATOR, hide_battery_indicator);
  222. start_battery_tracking();
  223. // Handling date_format setting.
  224. Tuple *date_format_t = dict_find(iter, KEY_DATE_FORMAT);
  225. if (date_format_t) {
  226. strcpy(date_format, date_format_t->value->cstring);
  227. }
  228. persist_write_string(KEY_DATE_FORMAT, date_format);
  229. update_date_text_layer();
  230. // Handling wide_layout setting.
  231. Tuple *wide_layout_t = dict_find(iter, KEY_WIDE_LAYOUT);
  232. if (wide_layout_t && wide_layout_t->value->int32 > 0) {
  233. use_wide_layout = true;
  234. } else {
  235. use_wide_layout = false;
  236. }
  237. persist_write_bool(KEY_WIDE_LAYOUT, use_wide_layout);
  238. // Handling xen_style setting.
  239. Tuple *xen_style_t = dict_find(iter, KEY_XEN_STYLE);
  240. if (xen_style_t && xen_style_t->value->int32 > 0) {
  241. use_xen_style = true;
  242. } else {
  243. use_xen_style = false;
  244. }
  245. persist_write_bool(KEY_XEN_STYLE, use_xen_style);
  246. // Updating geometry according to settings.
  247. update_geometry();
  248. }
  249. static void main_window_load(Window *window) {
  250. // Loading resources.
  251. s_battery_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BATTERY);
  252. s_text_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TOPAZ_16));
  253. // Creating hand paths.
  254. s_minute_hand = gpath_create(&MINUTE_HAND_POINTS);
  255. s_hour_hand = gpath_create(&HOUR_HAND_POINTS);
  256. Layer *window_layer = window_get_root_layer(window);
  257. GRect bounds = layer_get_bounds(window_layer);
  258. // Creating background image layer.
  259. s_background_layer = bitmap_layer_create(bounds);
  260. bitmap_layer_set_compositing_mode(s_background_layer, GCompOpSet);
  261. layer_add_child(window_layer, bitmap_layer_get_layer(s_background_layer));
  262. // Creating title text layer.
  263. s_title_layer = text_layer_create(GRect(0, 0, 71, 18));
  264. text_layer_set_background_color(s_title_layer, GColorClear);
  265. text_layer_set_text(s_title_layer, "Clock");
  266. text_layer_set_font(s_title_layer, s_text_font);
  267. layer_add_child(window_layer, text_layer_get_layer(s_title_layer));
  268. // Creating hands layer.
  269. s_hands_layer = layer_create(GRect(32, 38, 78, 78));
  270. layer_set_update_proc(s_hands_layer, s_hands_layer_update);
  271. layer_add_child(window_layer, s_hands_layer);
  272. // Creating date layer.
  273. s_date_layer = text_layer_create(GRect(0, 0, 136, 16));
  274. text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
  275. text_layer_set_background_color(s_date_layer, GColorClear);
  276. text_layer_set_font(s_date_layer, s_text_font);
  277. layer_add_child(window_layer, text_layer_get_layer(s_date_layer));
  278. // Creating AM/PM layer.
  279. s_ampm_layer = text_layer_create(GRect(0, 0, 16, 16));
  280. text_layer_set_text_alignment(s_ampm_layer, GTextAlignmentCenter);
  281. text_layer_set_background_color(s_ampm_layer, GColorClear);
  282. text_layer_set_text_color(s_ampm_layer, GColorWhite);
  283. text_layer_set_font(s_ampm_layer, s_text_font);
  284. layer_add_child(window_layer, text_layer_get_layer(s_ampm_layer));
  285. // Creating battery icon layer.
  286. s_battery_icon_layer = bitmap_layer_create(GRect(0, 0, 16, 7));
  287. bitmap_layer_set_bitmap(s_battery_icon_layer, s_battery_bitmap);
  288. bitmap_layer_set_compositing_mode(s_battery_icon_layer, GCompOpSet);
  289. layer_add_child(window_layer, bitmap_layer_get_layer(s_battery_icon_layer));
  290. // Creating battery indicator layer.
  291. s_battery_indicator_layer = layer_create(GRect(0, 0, 13, 5));
  292. layer_set_update_proc(s_battery_indicator_layer, s_battery_indicator_layer_update);
  293. layer_add_child(window_layer, s_battery_indicator_layer);
  294. // Update geometry according to wide_layout and xen_style settings.
  295. use_wide_layout = persist_read_bool(KEY_WIDE_LAYOUT);
  296. use_xen_style = persist_read_bool(KEY_XEN_STYLE);
  297. update_geometry();
  298. // Read date_format setting from persistent storage.
  299. int result = persist_read_string(KEY_DATE_FORMAT, date_format, 2);
  300. if (result == E_DOES_NOT_EXIST) {
  301. strcpy(date_format, "t");
  302. }
  303. // Starting time tracking.
  304. hide_seconds_hand = persist_read_bool(KEY_SECONDS_HAND);
  305. start_time_tracking();
  306. // Starting battery tracking.
  307. hide_battery_indicator = persist_read_bool(KEY_BATTERY_INDICATOR);
  308. start_battery_tracking();
  309. }
  310. static void main_window_unload(Window *window) {
  311. battery_state_service_unsubscribe();
  312. tick_timer_service_unsubscribe();
  313. // Apparently, freeing this pointer causes the watchface to crash on
  314. // v3.3. Weird.
  315. // if (t) {
  316. // free(t);
  317. // }
  318. layer_destroy(s_battery_indicator_layer);
  319. bitmap_layer_destroy(s_battery_icon_layer);
  320. text_layer_destroy(s_ampm_layer);
  321. text_layer_destroy(s_date_layer);
  322. layer_destroy(s_hands_layer);
  323. text_layer_destroy(s_title_layer);
  324. bitmap_layer_destroy(s_background_layer);
  325. gpath_destroy(s_hour_hand);
  326. gpath_destroy(s_minute_hand);
  327. fonts_unload_custom_font(s_text_font);
  328. gbitmap_destroy(s_battery_bitmap);
  329. gbitmap_destroy(s_background_bitmap);
  330. }
  331. static void init() {
  332. s_main_window = window_create();
  333. window_set_window_handlers(s_main_window, (WindowHandlers) {
  334. .load = main_window_load,
  335. .unload = main_window_unload
  336. });
  337. window_stack_push(s_main_window, true);
  338. app_message_register_inbox_received(inbox_received_handler);
  339. app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
  340. }
  341. static void deinit() {
  342. window_destroy(s_main_window);
  343. }
  344. int main(void) {
  345. init();
  346. app_event_loop();
  347. deinit();
  348. }