]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
mixer: Allow arbitrary relative time for sleep subcommand.
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 27 Dec 2016 23:53:29 +0000 (00:53 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 26 Mar 2017 09:04:05 +0000 (11:04 +0200)
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.

m4/lls/mixer.suite.m4
mixer.c

index 83d6ad955771c9f4a9bd0ccc567e0156e2f8da0e..9a56566031aca9373aad7c3cca18fdd9027e80b1 100644 (file)
@@ -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 2089c5b104ac72ebecc493496d9a350286248661..6d1b5f5cad2d344c3f1eb6b2b441e7ef62e5d79c 100644 (file)
--- 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);