diff options
author | Christian Soffke <christian.soffke@gmail.com> | 2024-05-13 17:11:45 +0200 |
---|---|---|
committer | Christian Soffke <christian.soffke@gmail.com> | 2024-05-13 18:18:45 +0200 |
commit | fdba79cd7777d3318531995083984fade98afae8 (patch) | |
tree | 908ff3d74414e133456b9f29aab4fedb58f5ba0c | |
parent | 8eeef333a15d99a38f733a45612728dfae66b862 (diff) | |
download | rockbox-fdba79cd77.tar.gz rockbox-fdba79cd77.zip |
shortcuts: refactor sleeptimer / talk_timedate
move some functions around, with no effect on behavior
Change-Id: I4638a28f5ff2a851534a3dd696ea7e763029cb2f
-rw-r--r-- | apps/menus/settings_menu.c | 62 | ||||
-rw-r--r-- | apps/menus/time_menu.c | 17 | ||||
-rw-r--r-- | apps/misc.c | 76 | ||||
-rw-r--r-- | apps/misc.h | 13 | ||||
-rw-r--r-- | apps/shortcuts.c | 164 |
5 files changed, 173 insertions, 159 deletions
diff --git a/apps/menus/settings_menu.c b/apps/menus/settings_menu.c index 03c17b24e5..1e1572f15e 100644 --- a/apps/menus/settings_menu.c +++ b/apps/menus/settings_menu.c @@ -57,6 +57,7 @@ #endif #include "plugin.h" #include "onplay.h" +#include "misc.h" #ifndef HAS_BUTTON_HOLD static int selectivesoftlock_callback(int action, @@ -496,69 +497,20 @@ MAKE_MENU(system_menu, ID2P(LANG_SYSTEM), /***********************************/ /* STARTUP/SHUTDOWN MENU */ -/* sleep timer option */ -const char* sleep_timer_formatter(char* buffer, size_t buffer_size, - int value, const char* unit) -{ - (void) unit; - int minutes, hours; - - if (value) { - hours = value / 60; - minutes = value - (hours * 60); - snprintf(buffer, buffer_size, "%d:%02d", hours, minutes); - return buffer; - } else { - return str(LANG_OFF); - } -} - -static int seconds_to_min(int secs) -{ - return (secs + 10) / 60; /* round up for 50+ seconds */ -} -/* A string representation of either whether a sleep timer will be started or - canceled, and how long it will be or how long is remaining in brackets */ -char* sleep_timer_getname(int selected_item, void * data, - char *buffer, size_t buffer_len) +char* sleeptimer_getname(int selected_item, void * data, + char *buffer, size_t buffer_len) { (void)selected_item; (void)data; - int sec = get_sleep_timer(); - char timer_buf[10]; - - snprintf(buffer, buffer_len, "%s (%s)", - str(sec ? LANG_SLEEP_TIMER_CANCEL_CURRENT - : LANG_SLEEP_TIMER_START_CURRENT), - sleep_timer_formatter(timer_buf, sizeof(timer_buf), - sec ? seconds_to_min(sec) - : global_settings.sleeptimer_duration, NULL)); - return buffer; + return string_sleeptimer(buffer, buffer_len); } -int sleep_timer_voice(int selected_item, void*data) +int sleeptimer_voice(int selected_item, void*data) { (void)selected_item; (void)data; - int seconds = get_sleep_timer(); - long talk_ids[] = { - seconds ? LANG_SLEEP_TIMER_CANCEL_CURRENT - : LANG_SLEEP_TIMER_START_CURRENT, - VOICE_PAUSE, - (seconds ? seconds_to_min(seconds) - : global_settings.sleeptimer_duration) | UNIT_MIN << UNIT_SHIFT, - TALK_FINAL_ID - }; - talk_idarray(talk_ids, true); - return 0; -} - -/* If a sleep timer is running, cancel it, otherwise start one */ -int toggle_sleeptimer(void) -{ - set_sleeptimer_duration(get_sleep_timer() ? 0 - : global_settings.sleeptimer_duration); + talk_sleeptimer(); return 0; } @@ -587,7 +539,7 @@ static int sleeptimer_duration_cb(int action, MENUITEM_SETTING(start_screen, &global_settings.start_in_screen, NULL); MENUITEM_SETTING(poweroff, &global_settings.poweroff, NULL); MENUITEM_FUNCTION_DYNTEXT(sleeptimer_toggle, 0, toggle_sleeptimer, - sleep_timer_getname, sleep_timer_voice, NULL, + sleeptimer_getname, sleeptimer_voice, NULL, NULL, Icon_NOICON); MENUITEM_SETTING(sleeptimer_duration, &global_settings.sleeptimer_duration, diff --git a/apps/menus/time_menu.c b/apps/menus/time_menu.c index e6b5637047..79a38a1a57 100644 --- a/apps/menus/time_menu.c +++ b/apps/menus/time_menu.c @@ -144,23 +144,6 @@ MENUITEM_FUNCTION(alarm_wake_up_screen, 0, ID2P(LANG_ALARM_WAKEUP_SCREEN), #endif /* HAVE_RTC_ALARM */ -void talk_timedate(void) -{ - struct tm *tm = get_time(); - if (!global_settings.talk_menu) - return; - talk_id(VOICE_CURRENT_TIME, false); - if (valid_time(tm)) - { - talk_time(tm, true); - talk_date(get_time(), true); - } - else - { - talk_id(LANG_UNKNOWN, true); - } -} - static void draw_timedate(struct viewport *vp, struct screen *display) { struct tm *tm = get_time(); diff --git a/apps/misc.c b/apps/misc.c index 4faadb8130..7ba229a6f3 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1467,6 +1467,82 @@ void format_time(char* buf, int buf_size, long t) t < 0, "-", units_in[UNIT_IDX_HR], hashours, ":", hashours+1, units_in[UNIT_IDX_MIN], units_in[UNIT_IDX_SEC]); } + +const char* format_sleeptimer(char* buffer, size_t buffer_size, + int value, const char* unit) +{ + (void) unit; + int minutes, hours; + + if (value) { + hours = value / 60; + minutes = value - (hours * 60); + snprintf(buffer, buffer_size, "%d:%02d", hours, minutes); + return buffer; + } else { + return str(LANG_OFF); + } +} + +static int seconds_to_min(int secs) +{ + return (secs + 10) / 60; /* round up for 50+ seconds */ +} + +char* string_sleeptimer(char *buffer, size_t buffer_len) +{ + int sec = get_sleep_timer(); + char timer_buf[10]; + + snprintf(buffer, buffer_len, "%s (%s)", + str(sec ? LANG_SLEEP_TIMER_CANCEL_CURRENT + : LANG_SLEEP_TIMER_START_CURRENT), + format_sleeptimer(timer_buf, sizeof(timer_buf), + sec ? seconds_to_min(sec) + : global_settings.sleeptimer_duration, NULL)); + return buffer; +} + +/* If a sleep timer is running, cancel it, otherwise start one */ +int toggle_sleeptimer(void) +{ + set_sleeptimer_duration(get_sleep_timer() ? 0 + : global_settings.sleeptimer_duration); + return 0; +} + +void talk_sleeptimer(void) +{ + int seconds = get_sleep_timer(); + long talk_ids[] = { + seconds ? LANG_SLEEP_TIMER_CANCEL_CURRENT + : LANG_SLEEP_TIMER_START_CURRENT, + VOICE_PAUSE, + (seconds ? seconds_to_min(seconds) + : global_settings.sleeptimer_duration) | UNIT_MIN << UNIT_SHIFT, + TALK_FINAL_ID + }; + talk_idarray(talk_ids, true); +} + +#if CONFIG_RTC +void talk_timedate(void) +{ + struct tm *tm = get_time(); + if (!global_settings.talk_menu) + return; + talk_id(VOICE_CURRENT_TIME, false); + if (valid_time(tm)) + { + talk_time(tm, true); + talk_date(get_time(), true); + } + else + { + talk_id(LANG_UNKNOWN, true); + } +} +#endif /* CONFIG_RTC */ #endif /* !defined(CHECKWPS) && !defined(DBTOOL)*/ /** diff --git a/apps/misc.h b/apps/misc.h index fd48ccf648..28a982d1da 100644 --- a/apps/misc.h +++ b/apps/misc.h @@ -93,6 +93,19 @@ const char *format_time_auto(char *buffer, int buf_len, long value, */ void format_time(char* buf, int buf_size, long t); +const char* format_sleeptimer(char* buffer, size_t buffer_size, + int value, const char* unit); + +/* A string representation of either whether a sleep timer will be started or + canceled, and how long it will be or how long is remaining in brackets */ +char* string_sleeptimer(char *buffer, size_t buffer_len); +int toggle_sleeptimer(void); +void talk_sleeptimer(void); + +#if CONFIG_RTC +void talk_timedate(void); +#endif + /* Ask the user if they really want to erase the current dynamic playlist * returns true if the playlist should be replaced */ bool warn_on_pl_erase(void); diff --git a/apps/shortcuts.c b/apps/shortcuts.c index a32c298f6d..a7fbe7ccec 100644 --- a/apps/shortcuts.c +++ b/apps/shortcuts.c @@ -421,8 +421,6 @@ void shortcuts_init(void) --buflib_move_lock; } -char* sleep_timer_getname(int selected_item, void * data, - char *buffer, size_t buffer_len); /* settings_menu.c */ static const char * shortcut_menu_get_name(int selected_item, void * data, char * buffer, size_t buffer_len) { @@ -440,11 +438,9 @@ static const char * shortcut_menu_get_name(int selected_item, void * data, #if CONFIG_RTC && !sc->u.timedata.talktime #endif - ) /* Toggle Sleep Timer */ - { - sleep_timer_getname(selected_item, data, buffer, buffer_len); - return buffer; - } + ) /* String representation for toggling sleep timer */ + return string_sleeptimer(buffer, buffer_len); + return sc->name; } else if ((sc->type == SHORTCUT_SHUTDOWN || sc->type == SHORTCUT_REBOOT) && @@ -466,81 +462,6 @@ static const char * shortcut_menu_get_name(int selected_item, void * data, return sc->name[0] ? sc->name : sc->u.path; } -static int shortcut_menu_speak_item(int selected_item, void * data); -static int shortcut_menu_get_action(int action, struct gui_synclist *lists) -{ - (void)lists; - if (action == ACTION_STD_OK || action == ACTION_STD_MENU) - return ACTION_STD_CANCEL; - else if (action == ACTION_STD_QUICKSCREEN && action != ACTION_STD_CONTEXT) - return ACTION_STD_CANCEL; - else if (action == ACTION_STD_CONTEXT) - { - int selection = gui_synclist_get_sel_pos(lists); - - if (confirm_delete_yesno("") != YESNO_YES) - { - gui_synclist_set_title(lists, lists->title, lists->title_icon); - shortcut_menu_speak_item(selection, NULL); - return ACTION_REDRAW; - } - - remove_shortcut(selection); - gui_synclist_set_nb_items(lists, shortcut_count); - gui_synclist_set_title(lists, lists->title, lists->title_icon); - if (selection >= shortcut_count) - gui_synclist_select_item(lists, shortcut_count - 1); - first_idx_to_writeback = 0; - overwrite_shortcuts = true; - shortcuts_ata_idle_callback(); - if (shortcut_count == 0) - return ACTION_STD_CANCEL; - - shortcut_menu_speak_item(gui_synclist_get_sel_pos(lists), NULL); - return ACTION_REDRAW; - } - return action; -} - -static enum themable_icons shortcut_menu_get_icon(int selected_item, void * data) -{ - (void)data; - int icon; - struct shortcut *sc = get_shortcut(selected_item); - if (!sc) - return Icon_NOICON; - if (sc->icon == Icon_NOICON) - { - - switch (sc->type) - { - case SHORTCUT_FILE: - return filetype_get_icon(filetype_get_attr(sc->u.path)); - case SHORTCUT_BROWSER: - icon = filetype_get_icon(filetype_get_attr(sc->u.path)); - if (icon <= 0) - icon = Icon_Folder; - return icon; - case SHORTCUT_SETTING: - return Icon_Menu_setting; - case SHORTCUT_DEBUGITEM: - return Icon_Menu_functioncall; - case SHORTCUT_PLAYLISTMENU: - return Icon_Playlist; - case SHORTCUT_SHUTDOWN: - case SHORTCUT_REBOOT: - return Icon_System_menu; - case SHORTCUT_TIME: - return Icon_Menu_functioncall; - default: - break; - } - } - return sc->icon; -} - -void talk_timedate(void); -int sleep_timer_voice(int selected_item, void*data); /* settings_menu.c */ static int shortcut_menu_speak_item(int selected_item, void * data) { (void)data; @@ -605,7 +526,7 @@ static int shortcut_menu_speak_item(int selected_item, void * data) else #endif if (sc->u.timedata.sleep_timeout < 0) - sleep_timer_voice(selected_item, data); + talk_sleeptimer(); else if (sc->name[0]) talk_spell(sc->name, false); break; @@ -626,9 +547,78 @@ static int shortcut_menu_speak_item(int selected_item, void * data) return 0; } -const char* sleep_timer_formatter(char* buffer, size_t buffer_size, - int value, const char* unit); -int toggle_sleeptimer(void); /* settings_menu.c */ +static int shortcut_menu_get_action(int action, struct gui_synclist *lists) +{ + (void)lists; + if (action == ACTION_STD_OK || action == ACTION_STD_MENU) + return ACTION_STD_CANCEL; + else if (action == ACTION_STD_QUICKSCREEN && action != ACTION_STD_CONTEXT) + return ACTION_STD_CANCEL; + else if (action == ACTION_STD_CONTEXT) + { + int selection = gui_synclist_get_sel_pos(lists); + + if (confirm_delete_yesno("") != YESNO_YES) + { + gui_synclist_set_title(lists, lists->title, lists->title_icon); + shortcut_menu_speak_item(selection, NULL); + return ACTION_REDRAW; + } + + remove_shortcut(selection); + gui_synclist_set_nb_items(lists, shortcut_count); + gui_synclist_set_title(lists, lists->title, lists->title_icon); + if (selection >= shortcut_count) + gui_synclist_select_item(lists, shortcut_count - 1); + first_idx_to_writeback = 0; + overwrite_shortcuts = true; + shortcuts_ata_idle_callback(); + if (shortcut_count == 0) + return ACTION_STD_CANCEL; + + shortcut_menu_speak_item(gui_synclist_get_sel_pos(lists), NULL); + return ACTION_REDRAW; + } + return action; +} + +static enum themable_icons shortcut_menu_get_icon(int selected_item, void * data) +{ + (void)data; + int icon; + struct shortcut *sc = get_shortcut(selected_item); + if (!sc) + return Icon_NOICON; + if (sc->icon == Icon_NOICON) + { + + switch (sc->type) + { + case SHORTCUT_FILE: + return filetype_get_icon(filetype_get_attr(sc->u.path)); + case SHORTCUT_BROWSER: + icon = filetype_get_icon(filetype_get_attr(sc->u.path)); + if (icon <= 0) + icon = Icon_Folder; + return icon; + case SHORTCUT_SETTING: + return Icon_Menu_setting; + case SHORTCUT_DEBUGITEM: + return Icon_Menu_functioncall; + case SHORTCUT_PLAYLISTMENU: + return Icon_Playlist; + case SHORTCUT_SHUTDOWN: + case SHORTCUT_REBOOT: + return Icon_System_menu; + case SHORTCUT_TIME: + return Icon_Menu_functioncall; + default: + break; + } + } + return sc->icon; +} + int do_shortcut_menu(void *ignored) { (void)ignored; @@ -761,7 +751,7 @@ int do_shortcut_menu(void *ignored) { set_sleeptimer_duration(sc->u.timedata.sleep_timeout); splashf(HZ, "%s (%s)", str(LANG_SLEEP_TIMER), - sleep_timer_formatter(timer_buf, sizeof(timer_buf), + format_sleeptimer(timer_buf, sizeof(timer_buf), sc->u.timedata.sleep_timeout, NULL)); } |