shortcuts: refactor sleeptimer / talk_timedate

move some functions around, with no effect on behavior

Change-Id: I4638a28f5ff2a851534a3dd696ea7e763029cb2f
This commit is contained in:
Christian Soffke 2024-05-13 17:11:45 +02:00
parent 8eeef333a1
commit fdba79cd77
5 changed files with 185 additions and 171 deletions

View file

@ -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,

View file

@ -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();

View file

@ -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)*/
/**

View file

@ -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);

View file

@ -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,7 +462,91 @@ 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_speak_item(int selected_item, void * data)
{
(void)data;
struct shortcut *sc = get_shortcut(selected_item);
if (sc)
{
if (sc->talk_clip[0])
{
talk_file(NULL, NULL, sc->talk_clip, NULL, NULL, false);
}
else
{
switch (sc->type)
{
case SHORTCUT_BROWSER:
{
static char path[MAX_PATH];
DIR* dir;
struct dirent* entry;
char* filename = strrchr(sc->u.path, PATH_SEPCH) + 1;
if (*filename != '\0')
{
int dirlen = (filename - sc->u.path);
strmemccpy(path, sc->u.path, dirlen + 1);
dir = opendir(path);
if (dir)
{
while (0 != (entry = readdir(dir)))
{
if (!strcmp(entry->d_name, filename))
{
struct dirinfo info = dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY)
talk_dir_or_spell(sc->u.path, NULL, false);
else talk_file_or_spell(path, filename, NULL, false);
closedir(dir);
return 0;
}
}
closedir(dir);
}
}
else
{
talk_dir_or_spell(sc->u.path, NULL, false);
break;
}
talk_spell(sc->u.path, false);
}
break;
case SHORTCUT_FILE:
case SHORTCUT_PLAYLISTMENU:
talk_file_or_spell(NULL, sc->u.path, NULL, false);
break;
case SHORTCUT_SETTING:
talk_id(sc->u.setting->lang_id, false);
break;
case SHORTCUT_TIME:
#if CONFIG_RTC
if (sc->u.timedata.talktime)
talk_timedate();
else
#endif
if (sc->u.timedata.sleep_timeout < 0)
talk_sleeptimer();
else if (sc->name[0])
talk_spell(sc->name, false);
break;
case SHORTCUT_SHUTDOWN:
case SHORTCUT_REBOOT:
if (!sc->name[0])
{
talk_spell(type_strings[sc->type], false);
break;
}
/* fall-through */
default:
talk_spell(sc->name[0] ? sc->name : sc->u.path, false);
break;
}
}
}
return 0;
}
static int shortcut_menu_get_action(int action, struct gui_synclist *lists)
{
(void)lists;
@ -539,96 +619,6 @@ static enum themable_icons shortcut_menu_get_icon(int selected_item, void * data
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;
struct shortcut *sc = get_shortcut(selected_item);
if (sc)
{
if (sc->talk_clip[0])
{
talk_file(NULL, NULL, sc->talk_clip, NULL, NULL, false);
}
else
{
switch (sc->type)
{
case SHORTCUT_BROWSER:
{
static char path[MAX_PATH];
DIR* dir;
struct dirent* entry;
char* filename = strrchr(sc->u.path, PATH_SEPCH) + 1;
if (*filename != '\0')
{
int dirlen = (filename - sc->u.path);
strmemccpy(path, sc->u.path, dirlen + 1);
dir = opendir(path);
if (dir)
{
while (0 != (entry = readdir(dir)))
{
if (!strcmp(entry->d_name, filename))
{
struct dirinfo info = dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY)
talk_dir_or_spell(sc->u.path, NULL, false);
else talk_file_or_spell(path, filename, NULL, false);
closedir(dir);
return 0;
}
}
closedir(dir);
}
}
else
{
talk_dir_or_spell(sc->u.path, NULL, false);
break;
}
talk_spell(sc->u.path, false);
}
break;
case SHORTCUT_FILE:
case SHORTCUT_PLAYLISTMENU:
talk_file_or_spell(NULL, sc->u.path, NULL, false);
break;
case SHORTCUT_SETTING:
talk_id(sc->u.setting->lang_id, false);
break;
case SHORTCUT_TIME:
#if CONFIG_RTC
if (sc->u.timedata.talktime)
talk_timedate();
else
#endif
if (sc->u.timedata.sleep_timeout < 0)
sleep_timer_voice(selected_item, data);
else if (sc->name[0])
talk_spell(sc->name, false);
break;
case SHORTCUT_SHUTDOWN:
case SHORTCUT_REBOOT:
if (!sc->name[0])
{
talk_spell(type_strings[sc->type], false);
break;
}
/* fall-through */
default:
talk_spell(sc->name[0] ? sc->name : sc->u.path, false);
break;
}
}
}
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 */
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));
}