string: Clean up for_each_line() and related functions.
[paraslash.git] / fade.c
diff --git a/fade.c b/fade.c
index 0304f4c6637a00fb89d9c5e43c6f04c78b616d16..fee8f8ca2af34ca8aef421ede07fc8f00763ab7b 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -1,38 +1,39 @@
 /*
- * Copyright (C) 1998-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file fade.c A volume fader and alarm clock for linux. */
+/** \file fade.c A volume fader and alarm clock for OSS. */
 
 #include <regex.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <sys/ioctl.h>
-#include <ctype.h>
-#include <stdlib.h> /* EXIT_SUCCESS */
-#include <unistd.h>
-#include <signal.h>
-#include <string.h>
-#include <limits.h>
-#include <sys/soundcard.h>
 
 #include "fade.cmdline.h"
 #include "para.h"
 #include "fd.h"
 #include "string.h"
+#include "mix.h"
 #include "error.h"
+#include "version.h"
 
 INIT_FADE_ERRLISTS;
-struct fade_args_info conf;
+static struct fade_args_info conf;
 
-__printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
+enum mixer_id {MIXER_ENUM};
+static char *mixer_name[] = {MIXER_NAMES};
+DECLARE_MIXER_INITS;
+static struct mixer supported_mixer[] = {MIXER_ARRAY};
+#define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
+
+static int loglevel;
+__printf_2_3 void date_log(int ll, const char *fmt, ...)
 {
        va_list argp;
        time_t t1;
        struct tm *tm;
 
+       if (ll < loglevel)
+               return;
        time(&t1);
        tm = localtime(&t1);
        printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
@@ -40,91 +41,21 @@ __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
        vprintf(fmt, argp);
        va_end(argp);
 }
+__printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
 
-/*
- * open mixer device
- */
-static int open_mixer(void)
-{
-       return para_open(conf.mixer_device_arg, O_RDWR, 42);
-}
-
-/*
- * get volume via mixer_fd
- */
-static int do_get_vol(int mixer_fd)
-{
-       int volume;
-
-       if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
-               return -ERRNO_TO_PARA_ERROR(errno);
-       /* take the mean value of left and right volume */
-       return (volume % 256 + (volume >> 8)) / 2;
-}
-
-/*
- * open mixer, get volume and close mixer
- */
-static int get_vol(void)
-{
-       int mixer_fd;
-       int volume;
-
-       mixer_fd = open_mixer();
-       if (mixer_fd < 0)
-               return mixer_fd;
-       volume = do_get_vol(mixer_fd);
-       close(mixer_fd);
-       return volume;
-}
-
-/*
- * set volume via mixer_fd
- */
-static int do_set_vol(int mixer_fd, int volume)
-{
-       int tmp = (volume << 8) + volume;
-
-       if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
-               return -ERRNO_TO_PARA_ERROR(errno);
-       return 1;
-}
-
-/*
- * open mixer, set volume and close mixer
- */
-static int set_vol(int volume)
-{
-       int mixer_fd, ret = open_mixer();
-
-       if (ret < 0)
-               return ret;
-       mixer_fd = ret;
-       ret = do_set_vol(mixer_fd, volume);
-       close(mixer_fd);
-       return ret;
-}
-
-/*
- * Open mixer, get volume, fade to new_vol in secs seconds and
- * close mixer
- */
-static int fade(int new_vol, int fade_time)
+/* Fade to new volume in fade_time seconds. */
+static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
 {
-       int vol, mixer_fd, diff, incr, ret;
+       int vol, diff, incr, ret;
        unsigned secs;
        struct timespec ts;
        unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
 
        if (fade_time <= 0)
-               return 1;
+               return m->set(h, new_vol);
        secs = fade_time;
        PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
-       ret = open_mixer();
-       if (ret < 0)
-               return ret;
-       mixer_fd = ret;
-       ret = do_get_vol(mixer_fd);
+       ret = m->get(h);
        if (ret < 0)
                goto out;
        vol = ret;
@@ -142,33 +73,42 @@ static int fade(int new_vol, int fade_time)
                ts.tv_sec = tmp / 1000; /* really nec ?*/
                //printf("ts.tv_sec: %i\n", ts.tv_nsec);
                vol += incr;
-               ret = do_set_vol(mixer_fd, vol);
+               ret = m->set(h, vol);
                if (ret < 0)
                        goto out;
                //printf("vol = %i\n", vol);
                nanosleep(&ts, NULL);
        }
 out:
-       close(mixer_fd);
        return ret;
 }
 
 static void client_cmd(const char *cmd)
 {
-       int ret, fds[3] = {0, 0, 0};
+       int ret, status, fds[3] = {0, 0, 0};
        pid_t pid;
        char *cmdline = make_message(BINDIR "/para_client %s", cmd);
 
-       PARA_INFO_LOG("%s\n", cmdline);
+       PARA_NOTICE_LOG("%s\n", cmdline);
        ret = para_exec_cmdline_pid(&pid, cmdline, fds);
        free(cmdline);
        if (ret < 0) {
-               PARA_EMERG_LOG("%s\n", para_strerror(-ret));
-               exit(EXIT_FAILURE);
+               PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+               goto fail;
        }
        do
-               ret = wait(NULL);
-       while (ret != -1 && errno != ECHILD);
+               pid = waitpid(pid, &status, 0);
+       while (pid == -1 && errno == EINTR);
+       if (pid < 0) {
+               PARA_ERROR_LOG("%s\n", strerror(errno));
+               goto fail;
+       }
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               goto fail;
+       return;
+fail:
+       PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
+       exit(EXIT_FAILURE);
 }
 
 static void change_afs_mode_and_play(char *afs_mode)
@@ -178,29 +118,26 @@ static void change_afs_mode_and_play(char *afs_mode)
        client_cmd("stop");
        if (!afs_mode)
                return;
-       cmd = make_message("select %s\n", afs_mode);
+       cmd = make_message("select %s", afs_mode);
        client_cmd(cmd);
        free(cmd);
        client_cmd("play");
 }
 
-/*
- * sleep
- */
-static int sweet_dreams(void)
+static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
 {
        time_t t1, wake_time_epoch;
        unsigned int delay;
        struct tm *tm;
        int ret, min = conf.wake_min_arg;
-       char *fa_mode = conf.fa_mode_arg;
-       char *wake_mode = conf.wake_mode_arg;
-       char *sleep_mode = conf.sleep_mode_arg;
-       int wf = conf.wake_fade_arg;
-       int sf = conf.fa_fade_arg;
-       int wv = conf.wake_vol_arg;
-       int sv = conf.fa_vol_arg;
-       int iv = conf.sleep_ivol_arg;
+       char *fo_mood = conf.fo_mood_arg;
+       char *fi_mood = conf.fi_mood_arg;
+       char *sleep_mood = conf.sleep_mood_arg;
+       int fit = conf.fi_time_arg;
+       int fot = conf.fo_time_arg;
+       int fiv = conf.fi_vol_arg;
+       int fov = conf.fo_vol_arg;
+       int iv = conf.ivol_arg;
 
        /* calculate wake time */
        time(&t1);
@@ -216,68 +153,70 @@ static int sweet_dreams(void)
                tm->tm_sec = 0;
        } else {
                t1 += 9 * 60 * 60; /* nine hours from now */
-               PARA_INFO_LOG("default wake time: %lu\n", t1);
+               PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
                tm = localtime(&t1);
        }
        wake_time_epoch = mktime(tm);
        PARA_INFO_LOG("waketime: %s", asctime(tm));
        client_cmd("stop");
        sleep(1);
-       if (sf) {
+       if (fot) {
                PARA_INFO_LOG("initial volume: %d\n", iv);
-               ret = set_vol(iv);
+               ret = m->set(h, iv);
                if (ret < 0)
                        return ret;
-               change_afs_mode_and_play(fa_mode);
-               ret = fade(sv, sf);
+               change_afs_mode_and_play(fo_mood);
+               ret = fade(m, h, fov, fot);
                if (ret < 0)
                        return ret;
        } else {
-               ret = set_vol(sf);
+               ret = m->set(h, fov);
                if (ret < 0)
                        return ret;
        }
-       if (conf.sleep_mode_given)
-               change_afs_mode_and_play(sleep_mode);
+       if (conf.sleep_mood_given)
+               change_afs_mode_and_play(sleep_mood);
        else
                client_cmd("stop");
-       if (!wf)
+       if (!fit)
                return 1;
        for (;;) {
                time(&t1);
-               if (wake_time_epoch <= t1 + wf)
+               if (wake_time_epoch <= t1 + fit)
                        break;
-               delay = wake_time_epoch - t1 - wf;
+               delay = wake_time_epoch - t1 - fit;
                PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
                        delay, delay / 3600,
                        (delay % 3600) / 60);
                sleep(delay);
        }
-       change_afs_mode_and_play(wake_mode);
-       ret = fade(wv, wf);
+       change_afs_mode_and_play(fi_mood);
+       ret = fade(m, h, fiv, fit);
        PARA_INFO_LOG("fade complete, returning\n");
        return ret;
 }
 
-static int snooze(void)
+static int snooze(struct mixer *m, struct mixer_handle *h)
 {
-       int ret;
-       unsigned sleep_time;
+       int ret, val;
 
-       if (conf.snooze_time_arg <= 0)
+       if (conf.so_time_arg <= 0)
                return 1;
-       sleep_time = conf.snooze_time_arg;
-       if (get_vol() < conf.snooze_out_vol_arg)
-               ret = set_vol(conf.snooze_out_vol_arg);
+       ret = m->get(h);
+       if (ret < 0)
+               return ret;
+       val = ret;
+       if (val < conf.so_vol_arg)
+               ret = m->set(h, conf.so_vol_arg);
        else
-               ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
+               ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
        if (ret < 0)
                return ret;
        client_cmd("pause");
        PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
-       sleep(sleep_time);
+       sleep(conf.snooze_time_arg);
        client_cmd("play");
-       return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
+       return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
 }
 
 static int configfile_exists(void)
@@ -294,9 +233,64 @@ static int configfile_exists(void)
        return file_exists(conf.config_file_arg);
 }
 
+static void init_mixers(void)
+{
+       int i;
+
+       FOR_EACH_MIXER(i) {
+               struct mixer *m = &supported_mixer[i];
+               PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
+                       i, mixer_name[i]);
+               m->init(m);
+       }
+}
+
+static int set_channel(struct mixer *m, struct mixer_handle *h)
+{
+       char *channels;
+       int ret;
+
+       ret = m->set_channel(h, conf.mixer_channel_arg);
+       if (ret >= 0) {
+               PARA_NOTICE_LOG("using %s mixer channel\n",
+                       conf.mixer_channel_arg?  conf.mixer_channel_arg
+                               : "default");
+               return ret;
+       }
+       channels = m->get_channels(h);
+       printf("Available channels: %s\n", channels);
+       free(channels);
+       return ret;
+}
+
+static struct mixer *get_mixer_or_die(void)
+{
+       int i;
+
+       if (!conf.mixer_api_given)
+               i = DEFAULT_MIXER;
+       else
+               FOR_EACH_MIXER(i)
+                       if (!strcmp(mixer_name[i], conf.mixer_api_arg))
+                               break;
+       if (i < NUM_SUPPORTED_MIXERS) {
+               PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
+               return supported_mixer + i;
+       }
+       printf("available mixer APIs: ");
+       FOR_EACH_MIXER(i) {
+               int d = (i == DEFAULT_MIXER);
+               printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
+       }
+       printf("\n");
+       exit(EXIT_FAILURE);
+}
+
 int main(int argc, char *argv[])
 {
        int ret;
+       struct mixer *m;
+       struct mixer_handle *h = NULL;
 
        if (fade_cmdline_parser(argc, argv, &conf))
                exit(EXIT_FAILURE);
@@ -312,25 +306,34 @@ int main(int argc, char *argv[])
                        .override = 0,
                        .initialize = 0,
                        .check_required = 0,
-                       .check_ambiguity = 0
+                       .check_ambiguity = 0,
+                       .print_errors = 1
                };
                fade_cmdline_parser_config_file(conf.config_file_arg,
                        &conf, &params);
        }
-       if (!strcmp(conf.mode_arg, "sleep")) {
-               ret = sweet_dreams();
-               goto out;
-       }
-       if (!strcmp(conf.mode_arg, "fade")) {
-               ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
+       loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       init_mixers();
+       m = get_mixer_or_die();
+       ret = m->open(conf.mixer_device_arg, &h);
+       if (ret < 0)
                goto out;
-       }
-       if (!strcmp(conf.mode_arg, "snooze")) {
-               ret = snooze();
+       ret = set_channel(m, h);
+       if (ret < 0)
                goto out;
+       switch (conf.mode_arg) {
+       case mode_arg_fade:
+               ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
+               break;
+       case mode_arg_snooze:
+               ret = snooze(m, h);
+               break;
+       default: /* sleep mode */
+               ret = sweet_dreams(m, h);
+               break;
        }
-       ret = -E_FADE_SYNTAX;
 out:
+       m->close(&h);
        if (ret < 0)
                PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;