shortcuts: sleep timer: allow omitting number of minutes

'sleep' can now appear in the data field of a 'time' shortcut
without being followed by a number, allowing you to stop a
running timer, or to start a new one using the default sleep
timer duration (the duration setting can already be added to
the Shortcuts menu as well).

Also see here:
https://forums.rockbox.org/index.php/topic,54312.msg250940.html

Change-Id: I9d0e62ef1b6187c35133067349729a4d94273c7a
This commit is contained in:
Christian Soffke 2024-05-11 18:18:51 +02:00
parent b8b4fdd999
commit f631bfe5b4
3 changed files with 50 additions and 17 deletions

View file

@ -520,7 +520,7 @@ static int seconds_to_min(int secs)
/* A string representation of either whether a sleep timer will be started or /* 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 */ canceled, and how long it will be or how long is remaining in brackets */
static char* sleep_timer_getname(int selected_item, void * data, char* sleep_timer_getname(int selected_item, void * data,
char *buffer, size_t buffer_len) char *buffer, size_t buffer_len)
{ {
(void)selected_item; (void)selected_item;
@ -537,7 +537,7 @@ static char* sleep_timer_getname(int selected_item, void * data,
return buffer; return buffer;
} }
static int sleep_timer_voice(int selected_item, void*data) int sleep_timer_voice(int selected_item, void*data)
{ {
(void)selected_item; (void)selected_item;
(void)data; (void)data;
@ -555,7 +555,7 @@ static int sleep_timer_voice(int selected_item, void*data)
} }
/* If a sleep timer is running, cancel it, otherwise start one */ /* If a sleep timer is running, cancel it, otherwise start one */
static int toggle_sleeptimer(void) int toggle_sleeptimer(void)
{ {
set_sleeptimer_duration(get_sleep_timer() ? 0 set_sleeptimer_duration(get_sleep_timer() ? 0
: global_settings.sleeptimer_duration); : global_settings.sleeptimer_duration);

View file

@ -244,10 +244,13 @@ static void shortcuts_ata_idle_callback(void)
#endif #endif
{ {
write(fd, "sleep ", 6); write(fd, "sleep ", 6);
if (sc->u.timedata.sleep_timeout >= 0)
{
len = snprintf(buf, MAX_PATH, "%d", sc->u.timedata.sleep_timeout); len = snprintf(buf, MAX_PATH, "%d", sc->u.timedata.sleep_timeout);
write(fd, buf, len); write(fd, buf, len);
} }
} }
}
else else
write(fd, sc->u.path, strlen(sc->u.path)); write(fd, sc->u.path, strlen(sc->u.path));
@ -349,7 +352,11 @@ static int readline_cb(int n, char *buf, void *parameters)
else else
#endif #endif
if (!strncasecmp(value, "sleep", strlen("sleep"))) if (!strncasecmp(value, "sleep", strlen("sleep")))
sc->u.timedata.sleep_timeout = atoi(&value[strlen("sleep ")]); {
/* 'sleep' may appear alone or followed by number after a space */
sc->u.timedata.sleep_timeout = strlen(&value[5]) > 1 ?
atoi(&value[strlen("sleep ")]) : -1;
}
else else
sc->type = SHORTCUT_UNDEFINED; /* error */ sc->type = SHORTCUT_UNDEFINED; /* error */
break; break;
@ -411,6 +418,8 @@ void shortcuts_init(void)
--buflib_move_lock; --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, static const char * shortcut_menu_get_name(int selected_item, void * data,
char * buffer, size_t buffer_len) char * buffer, size_t buffer_len)
{ {
@ -420,8 +429,21 @@ static const char * shortcut_menu_get_name(int selected_item, void * data,
return ""; return "";
if (sc->type == SHORTCUT_SETTING) if (sc->type == SHORTCUT_SETTING)
return sc->name[0] ? sc->name : P2STR(ID2P(sc->u.setting->lang_id)); return sc->name[0] ? sc->name : P2STR(ID2P(sc->u.setting->lang_id));
else if (sc->type == SHORTCUT_SEPARATOR || sc->type == SHORTCUT_TIME) else if (sc->type == SHORTCUT_SEPARATOR)
return sc->name; return sc->name;
else if (sc->type == SHORTCUT_TIME)
{
if (sc->u.timedata.sleep_timeout < 0
#if CONFIG_RTC
&& !sc->u.timedata.talktime
#endif
) /* Toggle Sleep Timer */
{
sleep_timer_getname(selected_item, data, buffer, buffer_len);
return buffer;
}
return sc->name;
}
else if (sc->type == SHORTCUT_SHUTDOWN && sc->name[0] == '\0') else if (sc->type == SHORTCUT_SHUTDOWN && sc->name[0] == '\0')
{ {
/* No translation support as only soft_shutdown has LANG_SHUTDOWN defined */ /* No translation support as only soft_shutdown has LANG_SHUTDOWN defined */
@ -513,6 +535,7 @@ static enum themable_icons shortcut_menu_get_icon(int selected_item, void * data
} }
void talk_timedate(void); 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) static int shortcut_menu_speak_item(int selected_item, void * data)
{ {
(void)data; (void)data;
@ -576,7 +599,9 @@ static int shortcut_menu_speak_item(int selected_item, void * data)
talk_timedate(); talk_timedate();
else else
#endif #endif
if (sc->name[0]) if (sc->u.timedata.sleep_timeout < 0)
sleep_timer_voice(selected_item, data);
else if (sc->name[0])
talk_spell(sc->name, false); talk_spell(sc->name, false);
break; break;
case SHORTCUT_SHUTDOWN: case SHORTCUT_SHUTDOWN:
@ -596,7 +621,7 @@ static int shortcut_menu_speak_item(int selected_item, void * data)
const char* sleep_timer_formatter(char* buffer, size_t buffer_size, const char* sleep_timer_formatter(char* buffer, size_t buffer_size,
int value, const char* unit); int value, const char* unit);
int toggle_sleeptimer(void); /* settings_menu.c */
int do_shortcut_menu(void *ignored) int do_shortcut_menu(void *ignored)
{ {
(void)ignored; (void)ignored;
@ -717,10 +742,16 @@ int do_shortcut_menu(void *ignored)
#endif #endif
{ {
char timer_buf[10]; char timer_buf[10];
if (sc->u.timedata.sleep_timeout >= 0)
{
set_sleeptimer_duration(sc->u.timedata.sleep_timeout); set_sleeptimer_duration(sc->u.timedata.sleep_timeout);
splashf(HZ, "%s (%s)", str(LANG_SLEEP_TIMER), splashf(HZ, "%s (%s)", str(LANG_SLEEP_TIMER),
sleep_timer_formatter(timer_buf, sizeof(timer_buf), sleep_timer_formatter(timer_buf, sizeof(timer_buf),
sc->u.timedata.sleep_timeout, NULL)); sc->u.timedata.sleep_timeout,
NULL));
}
else
toggle_sleeptimer();
} }
break; break;
case SHORTCUT_UNDEFINED: case SHORTCUT_UNDEFINED:

View file

@ -325,8 +325,10 @@ Available types are:
\item[separator] \config{data} is ignored; \config{name} can be used to display text, \item[separator] \config{data} is ignored; \config{name} can be used to display text,
or left blank to make the list more accessible with visual gaps or left blank to make the list more accessible with visual gaps
\item[time] \config{data} needs to be \opt{rtc}{either ``talk'' to talk the time, or }``sleep X'' \item[time] \config{data} needs to be \opt{rtc}{either ``talk'' to talk the time, or }``sleep X''
where X is the number of minutes to run the sleep timer for (0 to disable). \config{name} where X can, optionally, be the number of minutes to run the sleep timer for (0 to disable).
is required for this shortcut type. If ``sleep'' is not followed by a number, the sleep timer can be stopped, if running,
or started using the default duration; \config{name} will be ignored in that case. Otherwise
\config{name} is required for this shortcut type.
\item[shutdown] \config{data} is ignored; \config{name} can be used to display text \item[shutdown] \config{data} is ignored; \config{name} can be used to display text
\end{description} \end{description}