From: Andre Noll Date: Tue, 27 Dec 2016 23:53:29 +0000 (+0100) Subject: mixer: Allow arbitrary relative time for sleep subcommand. X-Git-Tag: v0.6.0~2^2~4 X-Git-Url: http://git.tuebingen.mpg.de/?a=commitdiff_plain;ds=sidebyside;h=c37b0175de9a871649074c9e08681dd6511102e9;p=paraslash.git mixer: Allow arbitrary relative time for sleep subcommand. If --wake-hour is not given, the wake time defaults to now + nine hours. This is arbitrary, and the constant 9 was hardcoded with no way to specify it at the command line or in the config file. This commit replaces --wake-hour and --wake-min by --wake-time whose argument must be given in HH:MM syntax. An optional leading '+' character allows to specify a relative offset. --- diff --git a/m4/lls/mixer.suite.m4 b/m4/lls/mixer.suite.m4 index 83d6ad95..9a565660 100644 --- a/m4/lls/mixer.suite.m4 +++ b/m4/lls/mixer.suite.m4 @@ -181,21 +181,18 @@ caption = List of subcommands Select the given mood or playlist after the fade-out. If unset, playback is stopped until fade-in starts. [/help] - [option wake-hour] - short_opt = H - summary = A number between 0 and 23 + [option wake-time] + short_opt = w + summary = when to start fade in arg_info = required_arg - arg_type = uint32 - typestr = hour + arg_type = string + typestr = [+][HH][:MM] + default_val = +9:00 [help] - If this is not given, the default is computed as now + 9 hours. + If the optional plus character is given, the wake time is computed as + now + HH hours + MM minutes. Otherwise the HH:MM argument is considered + an absolute time (referring to either the current or the next day). [/help] - [option wake-min] - short_opt = M - summary = A number between 0 and 59 - arg_info = required_arg - arg_type = uint32 - typestr = minutes [option fi-mood] summary = mood or playlist for fade-in arg_info = required_arg diff --git a/mixer.c b/mixer.c index 2089c5b1..6d1b5f5c 100644 --- a/mixer.c +++ b/mixer.c @@ -199,7 +199,8 @@ static int com_sleep(struct mixer *m, struct mixer_handle *h) time_t t1, wake_time_epoch; unsigned int delay; struct tm *tm; - int ret, min = OPT_UINT32_VAL(SLEEP, WAKE_MIN); + int ret; + const char *wake_time = OPT_STRING_VAL(SLEEP, WAKE_TIME); const char *fo_mood = OPT_STRING_VAL(SLEEP, FO_MOOD); const char *fi_mood = OPT_STRING_VAL(SLEEP, FI_MOOD); const char *sleep_mood = OPT_STRING_VAL(SLEEP, SLEEP_MOOD); @@ -207,11 +208,30 @@ static int com_sleep(struct mixer *m, struct mixer_handle *h) int fot = OPT_UINT32_VAL(SLEEP, FO_TIME); int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL); int fov = OPT_UINT32_VAL(SLEEP, FO_VOL); + int32_t hour, min = 0; + char *tmp; + char *wt = para_strdup(wake_time + (wake_time[0] == '+')); /* calculate wake time */ time(&t1); - if (OPT_GIVEN(SLEEP, WAKE_HOUR)) { - int hour = OPT_UINT32_VAL(SLEEP, WAKE_HOUR); + tmp = strchr(wt, ':'); + if (tmp) { + *tmp = '\0'; + tmp++; + ret = para_atoi32(tmp, &min); + if (ret < 0) { + free(wt); + return ret; + } + } + ret = para_atoi32(wt, &hour); + free(wt); + if (ret < 0) + return ret; + if (wake_time[0] == '+') { /* relative */ + t1 += hour * 60 * 60 + min * 60; + tm = localtime(&t1); + } else { tm = localtime(&t1); if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) { t1 += 86400; /* wake time is tomorrow */ @@ -220,10 +240,6 @@ static int com_sleep(struct mixer *m, struct mixer_handle *h) tm->tm_hour = hour; tm->tm_min = min; tm->tm_sec = 0; - } else { - t1 += 9 * 60 * 60; /* nine hours from now */ - PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1); - tm = localtime(&t1); } wake_time_epoch = mktime(tm); PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);