]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - fade.c
Merge branch 't/sync'
[paraslash.git] / fade.c
diff --git a/fade.c b/fade.c
index 1bfa0ba1ee8e9ca862de62fef4104d6c3e9cd4a4..a8424d67a5db24ea2967670ee6eb90ccaf284100 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1998-2012 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -14,6 +14,7 @@
 #include "string.h"
 #include "mix.h"
 #include "error.h"
+#include "ggo.h"
 #include "version.h"
 
 INIT_FADE_ERRLISTS;
@@ -23,13 +24,17 @@ 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)++)
 
-__printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
+static int loglevel;
+static __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);
@@ -48,7 +53,7 @@ static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_t
        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 = m->get(h);
@@ -85,7 +90,7 @@ static void client_cmd(const char *cmd)
        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) {
@@ -233,7 +238,7 @@ static void init_mixers(void)
 {
        int i;
 
-       for (i = 0; i < NUM_SUPPORTED_MIXERS; i++) {
+       FOR_EACH_MIXER(i) {
                struct mixer *m = &supported_mixer[i];
                PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
                        i, mixer_name[i]);
@@ -259,15 +264,49 @@ static int set_channel(struct mixer *m, struct mixer_handle *h)
        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);
+}
+
+__noreturn static void print_help_and_die(void)
+{
+       struct ggo_help h = DEFINE_GGO_HELP(fade);
+       bool d = conf.detailed_help_given;
+
+       ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+       exit(0);
+}
+
 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);
-       HANDLE_VERSION_FLAG("fade", conf);
+       fade_cmdline_parser(argc, argv, &conf);
+       loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       version_handle_flag("fade", conf.version_given);
+       if (conf.help_given || conf.detailed_help_given)
+               print_help_and_die();
        ret = configfile_exists();
        if (!ret && conf.config_file_given) {
                PARA_EMERG_LOG("can not read config file %s\n",
@@ -284,10 +323,10 @@ int main(int argc, char *argv[])
                };
                fade_cmdline_parser_config_file(conf.config_file_arg,
                        &conf, &params);
+               loglevel = get_loglevel_by_name(conf.loglevel_arg);
        }
        init_mixers();
-       m = &supported_mixer[DEFAULT_MIXER];
-       PARA_INFO_LOG("using %s mixer\n", mixer_name[DEFAULT_MIXER]);
+       m = get_mixer_or_die();
        ret = m->open(conf.mixer_device_arg, &h);
        if (ret < 0)
                goto out;