mirror of
https://git.wetfence.xyz/RandyTheSilly/1-More-Episode.git
synced 2026-08-28 04:36:28 -04:00
296 lines
12 KiB
C
296 lines
12 KiB
C
#include <pebble.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
// declare settings-derived statics
|
|
static uint8_t s_is_jellyfin;
|
|
static GColor s_color_primary = GColorWhite;
|
|
static GColor s_color_secondary = GColorWhite;
|
|
|
|
// declare window statics
|
|
static Window *s_main_window;
|
|
static TextLayer *s_config_app_layer;
|
|
static TextLayer *s_sleep_time_layer;
|
|
static TextLayer *s_last_watched_layer;
|
|
static Layer *s_button_bar_layer;
|
|
static Layer *s_sleep_bar_layer;
|
|
static Layer *s_sleep_icon_layer;
|
|
static Layer *s_pin_icon_layer;
|
|
static GDrawCommandImage *s_sleep_icon;
|
|
static GDrawCommandImage *s_pin_icon;
|
|
|
|
// declare animation statics
|
|
static GRect s_sleep_bar_start = GRect(0, 0, PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT);
|
|
static GRect s_sleep_icon_start = GRect((PBL_DISPLAY_WIDTH / 2) - 13, (PBL_DISPLAY_HEIGHT / 2) - 12, 25, 25);
|
|
|
|
// declare time tracking statics
|
|
static time_t s_sleep_timestamp;
|
|
static bool s_sleep_session_found;
|
|
static bool s_sleep_data_accessible;
|
|
|
|
static void load_complete_animation_handler(Animation *animation, bool finished, void *context) {
|
|
layer_set_hidden(text_layer_get_layer(s_sleep_time_layer), false);
|
|
animation_destroy(animation);
|
|
}
|
|
|
|
static void send_sleep_time_to_pkjs() {
|
|
DictionaryIterator *out;
|
|
AppMessageResult result = app_message_outbox_begin(&out);
|
|
if (result != APP_MSG_OK) {
|
|
text_layer_set_text(s_last_watched_layer, "outbox_begin failure");
|
|
return;
|
|
}
|
|
if (s_sleep_session_found) {
|
|
dict_write_uint32(out, MESSAGE_KEY_PKJS_SLEEP_TIMESTAMP, s_sleep_timestamp);
|
|
} else {
|
|
// send current UNIX timestamp to fetch the last thing played
|
|
dict_write_uint32(out, MESSAGE_KEY_PKJS_SLEEP_TIMESTAMP, time(NULL));
|
|
}
|
|
result = app_message_outbox_send();
|
|
if (result != APP_MSG_OK) {
|
|
text_layer_set_text(s_last_watched_layer, "outbox_send failure");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void inbox_received_handler(DictionaryIterator *iter, void *context) {
|
|
Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY);
|
|
if (ready_tuple) {
|
|
// APP_LOG(APP_LOG_LEVEL_INFO, "Received PKJS_READY, calling PKJS...");
|
|
send_sleep_time_to_pkjs();
|
|
}
|
|
|
|
Tuple *clay_tuple = dict_find(iter, MESSAGE_KEY_CLAY_API_IS_JELLYFIN);
|
|
if (clay_tuple) {
|
|
// APP_LOG(APP_LOG_LEVEL_INFO, "Received CLAY_API_IS_JELLYFIN, updating UI...");
|
|
bool is_jellyfin = clay_tuple->value->int32 == 1;
|
|
if (is_jellyfin) {
|
|
persist_write_int(0, 1);
|
|
s_color_primary = GColorVividViolet;
|
|
s_color_secondary = GColorPictonBlue;
|
|
#if PBL_ROUND
|
|
s_pin_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_PIN_ROUND_JELLYFIN_ICON);
|
|
#endif
|
|
} else {
|
|
persist_write_int(0, 2);
|
|
s_color_primary = GColorChromeYellow;
|
|
s_color_secondary = GColorLightGray;
|
|
#if PBL_ROUND
|
|
s_pin_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_PIN_ROUND_PLEX_ICON);
|
|
#endif
|
|
}
|
|
layer_mark_dirty(s_sleep_bar_layer);
|
|
#if PBL_ROUND
|
|
layer_mark_dirty(s_pin_icon_layer);
|
|
#else
|
|
layer_mark_dirty(s_button_bar_layer);
|
|
#endif
|
|
}
|
|
|
|
Tuple *watched_tuple = dict_find(iter, MESSAGE_KEY_PKJS_LAST_WATCHED);
|
|
if (watched_tuple) {
|
|
// APP_LOG(APP_LOG_LEVEL_INFO, "Received PKJS_LAST_WATCHED, displaying...");
|
|
if (s_sleep_session_found) {
|
|
text_layer_set_text(s_last_watched_layer, watched_tuple->value->cstring);
|
|
} else {
|
|
static char last_watched_buffer[1024];
|
|
snprintf(last_watched_buffer, sizeof(last_watched_buffer), "Last item played:\n%s", watched_tuple->value->cstring);
|
|
text_layer_set_text(s_last_watched_layer, last_watched_buffer);
|
|
}
|
|
#if PBL_DISPLAY_WIDTH >= 200
|
|
PropertyAnimation *sleep_bar_prop = property_animation_create_layer_frame(s_sleep_bar_layer, &s_sleep_bar_start, &GRect(0, -PBL_DISPLAY_HEIGHT + PBL_IF_ROUND_ELSE(53, 31), PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT));
|
|
#else
|
|
PropertyAnimation *sleep_bar_prop = property_animation_create_layer_frame(s_sleep_bar_layer, &s_sleep_bar_start, &GRect(0, -PBL_DISPLAY_HEIGHT + PBL_IF_ROUND_ELSE(49, 31), PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT));
|
|
#endif
|
|
Animation *sleep_bar_anim = property_animation_get_animation(sleep_bar_prop);
|
|
animation_set_duration(sleep_bar_anim, 512);
|
|
PropertyAnimation *sleep_icon_prop = property_animation_create_layer_frame(s_sleep_icon_layer, &s_sleep_icon_start, &GRect(PBL_IF_ROUND_ELSE((PBL_DISPLAY_WIDTH / 2) - 13, 4), 4, 25, 25));
|
|
Animation *sleep_icon_anim = property_animation_get_animation(sleep_icon_prop);
|
|
animation_set_duration(sleep_icon_anim, 512);
|
|
Animation *load_spawn_anim = animation_spawn_create(sleep_bar_anim, sleep_icon_anim, NULL);
|
|
animation_set_handlers(load_spawn_anim, (AnimationHandlers){.stopped = load_complete_animation_handler}, NULL);
|
|
animation_schedule(load_spawn_anim);
|
|
}
|
|
}
|
|
|
|
static bool cb_update_sleep_time(HealthActivity activity, time_t start_time, time_t end_time, void *context) {
|
|
s_sleep_timestamp = start_time;
|
|
return false;
|
|
}
|
|
|
|
static void update_sleep_time(time_t start, time_t end) {
|
|
health_service_activities_iterate(HealthActivitySleep, start, end,
|
|
HealthIterationDirectionPast,
|
|
cb_update_sleep_time, NULL);
|
|
}
|
|
|
|
static void button_bar_update_proc(Layer *layer, GContext *ctx) {
|
|
graphics_context_set_fill_color(ctx, s_color_secondary);
|
|
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
|
|
}
|
|
|
|
static void sleep_bar_update_proc(Layer *layer, GContext *ctx) {
|
|
graphics_context_set_fill_color(ctx, s_color_primary);
|
|
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
|
|
}
|
|
|
|
static void sleep_icon_update_proc(Layer *layer, GContext *ctx) {
|
|
graphics_context_set_antialiased(ctx, false);
|
|
gdraw_command_image_draw(ctx, s_sleep_icon, GPoint(0, 0));
|
|
}
|
|
|
|
static void pin_icon_update_proc(Layer *layer, GContext *ctx) {
|
|
graphics_context_set_antialiased(ctx, false);
|
|
gdraw_command_image_draw(ctx, s_pin_icon, GPoint(0, 0));
|
|
}
|
|
|
|
static void main_window_load(Window *window) {
|
|
// sleep bar and icon
|
|
s_sleep_bar_layer = layer_create(s_sleep_bar_start);
|
|
s_sleep_icon_layer = layer_create(s_sleep_icon_start);
|
|
|
|
if (s_is_jellyfin > 0) { // display help instead of sleep icon if app is unconfigured
|
|
layer_set_update_proc(s_sleep_icon_layer, sleep_icon_update_proc);
|
|
} else {
|
|
#if PBL_DISPLAY_WIDTH >= 200
|
|
s_config_app_layer = text_layer_create(GRect(4, (PBL_DISPLAY_HEIGHT / 2) - PBL_IF_ROUND_ELSE(33, 49), PBL_DISPLAY_WIDTH - 8, PBL_DISPLAY_HEIGHT));
|
|
text_layer_set_font(s_config_app_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
|
|
#else
|
|
s_config_app_layer = text_layer_create(GRect(4, (PBL_DISPLAY_HEIGHT / 2) - PBL_IF_ROUND_ELSE(43, 32), PBL_DISPLAY_WIDTH - 8, PBL_DISPLAY_HEIGHT));
|
|
text_layer_set_font(s_config_app_layer, fonts_get_system_font(PBL_IF_ROUND_ELSE(FONT_KEY_GOTHIC_24_BOLD, FONT_KEY_GOTHIC_18_BOLD)));
|
|
#endif
|
|
text_layer_set_text(s_config_app_layer, "Please configure the app's settings from your phone");
|
|
text_layer_set_text_alignment(s_config_app_layer, GTextAlignmentCenter);
|
|
}
|
|
|
|
// pin icon
|
|
s_pin_icon_layer = layer_create(GRect(PBL_DISPLAY_WIDTH - PBL_IF_ROUND_ELSE(27, 25), (PBL_DISPLAY_HEIGHT / 2) - 13, 25, 25));
|
|
layer_set_update_proc(s_pin_icon_layer, pin_icon_update_proc);
|
|
|
|
#if PBL_DISPLAY_WIDTH >= 200
|
|
s_sleep_time_layer = text_layer_create(GRect(0, PBL_IF_ROUND_ELSE(21, -2), PBL_DISPLAY_WIDTH, 30));
|
|
s_last_watched_layer = text_layer_create(GRect(PBL_IF_ROUND_ELSE(28, 4), PBL_DISPLAY_HEIGHT / 2 - 20, PBL_DISPLAY_WIDTH - PBL_IF_ROUND_ELSE(56, 29), PBL_DISPLAY_HEIGHT));
|
|
text_layer_set_font(s_sleep_time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
|
|
text_layer_set_text_alignment(s_sleep_time_layer, GTextAlignmentCenter);
|
|
text_layer_set_font(s_last_watched_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28));
|
|
#else
|
|
s_sleep_time_layer = text_layer_create(GRect(PBL_IF_ROUND_ELSE(0, 31), PBL_IF_ROUND_ELSE(24, 6), PBL_DISPLAY_WIDTH, 30));
|
|
s_last_watched_layer = text_layer_create(GRect(PBL_IF_ROUND_ELSE(28, 4), PBL_DISPLAY_HEIGHT / 2 - 17, PBL_DISPLAY_WIDTH - PBL_IF_ROUND_ELSE(56, 29), PBL_DISPLAY_HEIGHT));
|
|
text_layer_set_font(s_sleep_time_layer, fonts_get_system_font(PBL_IF_ROUND_ELSE(FONT_KEY_GOTHIC_18_BOLD, FONT_KEY_GOTHIC_14_BOLD)));
|
|
text_layer_set_text_alignment(s_sleep_time_layer, PBL_IF_ROUND_ELSE(GTextAlignmentCenter, GTextAlignmentLeft));
|
|
text_layer_set_font(s_last_watched_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
|
|
#endif
|
|
|
|
// sleep time
|
|
text_layer_set_background_color(s_sleep_time_layer, GColorClear);
|
|
text_layer_set_text_color(s_sleep_time_layer, GColorWhite);
|
|
if (s_sleep_timestamp != 0) {
|
|
static char buffer[8];
|
|
struct tm *timeinfo = localtime(&s_sleep_timestamp);
|
|
strftime(buffer, sizeof(buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", timeinfo);
|
|
text_layer_set_text(s_sleep_time_layer, buffer);
|
|
} else {
|
|
text_layer_set_text(s_sleep_time_layer, "No sleep times found");
|
|
}
|
|
layer_set_hidden(text_layer_get_layer(s_sleep_time_layer), true);
|
|
|
|
// misc. properties
|
|
layer_set_update_proc(s_sleep_bar_layer, sleep_bar_update_proc);
|
|
text_layer_set_background_color(s_last_watched_layer, GColorClear);
|
|
text_layer_set_text_color(s_last_watched_layer, GColorWhite);
|
|
|
|
if (s_sleep_data_accessible) {
|
|
text_layer_set_text(s_last_watched_layer, "N/A"); // default - will be updated before display
|
|
} else {
|
|
text_layer_set_text(s_last_watched_layer, "Sleep data inaccessible!\nEnsure Pebble Health is enabled.");
|
|
}
|
|
|
|
// add layers as children to windows
|
|
Layer *window_layer = window_get_root_layer(window);
|
|
#if PBL_ROUND
|
|
text_layer_set_text_alignment(s_last_watched_layer, GTextAlignmentCenter);
|
|
#else
|
|
// button bar
|
|
s_button_bar_layer = layer_create(GRect(PBL_DISPLAY_WIDTH - 25, 0, 25, PBL_DISPLAY_HEIGHT));
|
|
layer_set_update_proc(s_button_bar_layer, button_bar_update_proc);
|
|
layer_add_child(window_layer, s_button_bar_layer);
|
|
#endif
|
|
layer_add_child(window_layer, s_pin_icon_layer);
|
|
layer_add_child(window_layer, text_layer_get_layer(s_last_watched_layer));
|
|
layer_add_child(window_layer, s_sleep_bar_layer);
|
|
layer_add_child(window_layer, s_sleep_icon_layer);
|
|
layer_add_child(window_layer, text_layer_get_layer(s_sleep_time_layer));
|
|
if (s_is_jellyfin == 0) {
|
|
layer_add_child(window_layer, text_layer_get_layer(s_config_app_layer));
|
|
}
|
|
}
|
|
|
|
static void main_window_unload(Window *window) {
|
|
layer_destroy(s_sleep_icon_layer);
|
|
layer_destroy(s_sleep_bar_layer);
|
|
layer_destroy(s_button_bar_layer);
|
|
text_layer_destroy(s_sleep_time_layer);
|
|
text_layer_destroy(s_last_watched_layer);
|
|
}
|
|
|
|
static void init() {
|
|
// initialize window
|
|
s_main_window = window_create();
|
|
window_set_background_color(s_main_window, GColorBlack);
|
|
window_set_window_handlers(
|
|
s_main_window,
|
|
(WindowHandlers){.load = main_window_load, .unload = main_window_unload});
|
|
|
|
// set colors if is_jellyfin is locally set;
|
|
// if it has not yet been set, it will be once
|
|
// the user saves their settings
|
|
s_is_jellyfin = persist_read_int(0);
|
|
switch (s_is_jellyfin) {
|
|
case 1:
|
|
s_color_primary = GColorVividViolet;
|
|
s_color_secondary = GColorPictonBlue;
|
|
#if PBL_ROUND
|
|
s_pin_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_PIN_ROUND_JELLYFIN_ICON);
|
|
#endif
|
|
break;
|
|
case 2:
|
|
s_color_primary = GColorChromeYellow;
|
|
s_color_secondary = GColorLightGray;
|
|
#if PBL_ROUND
|
|
s_pin_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_PIN_ROUND_PLEX_ICON);
|
|
#endif
|
|
break;
|
|
}
|
|
#if PBL_RECT
|
|
s_pin_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_PIN_ICON);
|
|
#endif
|
|
s_sleep_icon = gdraw_command_image_create_with_resource(RESOURCE_ID_SLEEP_ICON);
|
|
|
|
// initialize pkjs
|
|
app_message_register_inbox_received(inbox_received_handler);
|
|
app_message_open(1024, 64);
|
|
|
|
// get sleep time (UNIX timestamp)
|
|
time_t end = time(NULL);
|
|
time_t start = end - (SECONDS_PER_DAY * 1.5);
|
|
if (health_service_any_activity_accessible(HealthActivitySleep, start, end) == HealthServiceAccessibilityMaskAvailable) {
|
|
s_sleep_data_accessible = true;
|
|
update_sleep_time(start, end);
|
|
if (s_sleep_timestamp > 0) {
|
|
s_sleep_session_found = true;
|
|
}
|
|
} else {
|
|
s_sleep_data_accessible = false;
|
|
}
|
|
|
|
window_stack_push(s_main_window, true);
|
|
}
|
|
|
|
static void deinit() { window_destroy(s_main_window); }
|
|
|
|
int main(void) {
|
|
init();
|
|
app_event_loop();
|
|
deinit();
|
|
}
|