]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mixer.c
mixer: Allow arbitrary relative time for sleep subcommand.
[paraslash.git] / mixer.c
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);