fade: cosmetic cleanups
[paraslash.git] / fade.c
diff --git a/fade.c b/fade.c
index 55c62a7444b64430920aaf5d2b4521b8bf2050e6..d21ff4036778767a8801da9fac0d36517b3dfdae 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 1998-2005 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998-2007 Andre Noll <maan@systemlinux.org>
  *
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
  *
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -34,7 +34,7 @@
 #include "string.h"
 
 
 #include "string.h"
 
 
-struct fade_args_info args_info;
+struct fade_args_info conf;
 
 void para_log(__a_unused int ll, const char *fmt,...)
 {
 
 void para_log(__a_unused int ll, const char *fmt,...)
 {
@@ -55,11 +55,7 @@ void para_log(__a_unused int ll, const char *fmt,...)
  */
 static int open_mixer(void)
 {
  */
 static int open_mixer(void)
 {
-       int mixer_fd;
-
-       if ((mixer_fd = open(args_info.mixer_device_arg, O_RDWR, 0)) < 0)
-               return -1;
-       return mixer_fd;
+       return open(conf.mixer_device_arg, O_RDWR, 0);
 }
 
 /*
 }
 
 /*
@@ -96,10 +92,10 @@ static int get_vol(void)
  */
 static int do_set_vol(int mixer_fd, int volume)
 {
  */
 static int do_set_vol(int mixer_fd, int volume)
 {
-       int tmp;
-       tmp = (volume << 8) + volume;
+       int tmp = (volume << 8) + volume;
+
        if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
        if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
-               return 0;
+               return -1;
        return 1;
 }
 
        return 1;
 }
 
@@ -108,18 +104,13 @@ static int do_set_vol(int mixer_fd, int volume)
  */
 static int set_vol(int volume)
 {
  */
 static int set_vol(int volume)
 {
-       int mixer_fd;
-       int ret;
+       int mixer_fd, ret = open_mixer();
 
 
-       mixer_fd = open_mixer();
-       ret = 0;
-       if (mixer_fd < 0)
-               goto out;
-       if (!do_set_vol(mixer_fd, volume))
-               goto out;
-       ret = 1;
+       if (ret < 0)
+               return ret;
+       mixer_fd = ret;
+       ret = do_set_vol(mixer_fd, volume);
        close(mixer_fd);
        close(mixer_fd);
-out:
        return ret;
 }
 
        return ret;
 }
 
@@ -127,26 +118,26 @@ out:
  * Open mixer, get volume, fade to new_vol in secs seconds and
  * close mixer
  */
  * Open mixer, get volume, fade to new_vol in secs seconds and
  * close mixer
  */
-static int fade(int new_vol, unsigned int secs)
+static void fade(int new_vol, int fade_time)
 {
 {
-       int vol, mixer_fd = -1, diff, incr, ret;
+       int vol, mixer_fd, diff, incr;
+       unsigned secs;
        struct timespec ts;
        unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
 
        struct timespec ts;
        unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
 
+       if (fade_time <= 0)
+               return;
+       secs = fade_time;
        PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
        PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
-       ret = 0;
-       if (!secs)
-               goto out;
        mixer_fd = open_mixer();
        if (mixer_fd < 0)
        mixer_fd = open_mixer();
        if (mixer_fd < 0)
-               goto out;
+               return;
        vol = do_get_vol(mixer_fd);
        if (vol < 0)
                goto out;
        diff = new_vol - vol;
        if (!diff) {
                sleep(secs);
        vol = do_get_vol(mixer_fd);
        if (vol < 0)
                goto out;
        diff = new_vol - vol;
        if (!diff) {
                sleep(secs);
-               ret = 1;
                goto out;
        }
        incr = diff > 0? 1: -1;
                goto out;
        }
        incr = diff > 0? 1: -1;
@@ -158,15 +149,13 @@ static int fade(int new_vol, unsigned int secs)
                ts.tv_sec = tmp / 1000; /* really nec ?*/
                //printf("ts.tv_sec: %i\n", ts.tv_nsec);
                vol += incr;
                ts.tv_sec = tmp / 1000; /* really nec ?*/
                //printf("ts.tv_sec: %i\n", ts.tv_nsec);
                vol += incr;
-               if (!do_set_vol(mixer_fd, vol))
+               if (do_set_vol(mixer_fd, vol) < 0)
                        goto out;
                //printf("vol = %i\n", vol);
                nanosleep(&ts, NULL);
        }
 out:
                        goto out;
                //printf("vol = %i\n", vol);
                nanosleep(&ts, NULL);
        }
 out:
-       if (mixer_fd >= 0)
-               close(mixer_fd);
-       return 1;
+       close(mixer_fd);
 }
 
 static int client_cmd(const char *cmd,...)
 }
 
 static int client_cmd(const char *cmd,...)
@@ -174,6 +163,7 @@ static int client_cmd(const char *cmd,...)
        int ret, fds[3] = {0, 0, 0};
        pid_t pid;
        char *cmdline = make_message(BINDIR "/para_client %s", cmd);
        int ret, fds[3] = {0, 0, 0};
        pid_t pid;
        char *cmdline = make_message(BINDIR "/para_client %s", cmd);
+
        PARA_INFO_LOG("%s\n", cmdline);
        ret = para_exec_cmdline_pid(&pid, cmdline, fds);
        free(cmdline);
        PARA_INFO_LOG("%s\n", cmdline);
        ret = para_exec_cmdline_pid(&pid, cmdline, fds);
        free(cmdline);
@@ -188,22 +178,22 @@ static void sweet_dreams(void)
        time_t t1, wake_time_epoch;
        unsigned int delay;
        struct tm *tm;
        time_t t1, wake_time_epoch;
        unsigned int delay;
        struct tm *tm;
-       int min = args_info.wake_min_arg;
-       char *fa_stream = args_info.fa_stream_arg;
-       char *wake_stream = args_info.wake_stream_arg;
+       int min = conf.wake_min_arg;
+       char *fa_stream = conf.fa_stream_arg;
+       char *wake_stream = conf.wake_stream_arg;
        //char *current_stream = stat_items[STREAM].content;
        //char *current_stream = stat_items[STREAM].content;
-       int wf = args_info.wake_fade_arg;
-       int sf = args_info.fa_fade_arg;
-       int wv = args_info.wake_vol_arg;
-       int sv = args_info.fa_vol_arg;
-       int iv = args_info.sleep_ivol_arg;
-       char *cmd, *sleep_stream = args_info.sleep_stream_given?
-               args_info.sleep_stream_arg : NULL;
+       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 *cmd, *sleep_stream = conf.sleep_stream_given?
+               conf.sleep_stream_arg : NULL;
 
        /* calculate wake time */
        time(&t1);
 
        /* calculate wake time */
        time(&t1);
-       if (args_info.wake_hour_given) {
-               int hour = args_info.wake_hour_arg;
+       if (conf.wake_hour_given) {
+               int hour = conf.wake_hour_arg;
                tm = localtime(&t1);
                if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
                        t1 += 86400; /* wake time is tomorrow */
                tm = localtime(&t1);
                if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
                        t1 += 86400; /* wake time is tomorrow */
@@ -254,30 +244,34 @@ static void sweet_dreams(void)
 
 static void snooze(void)
 {
 
 static void snooze(void)
 {
-       if (get_vol() < args_info.snooze_out_vol_arg)
-               set_vol(args_info.snooze_out_vol_arg);
+       unsigned sleep_time;
+
+       if (conf.snooze_time_arg <= 0)
+               return;
+       sleep_time = conf.snooze_time_arg;
+       if (get_vol() < conf.snooze_out_vol_arg)
+               set_vol(conf.snooze_out_vol_arg);
        else
        else
-               fade(args_info.snooze_out_vol_arg, args_info.snooze_out_fade_arg);
+               fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
        client_cmd("pause");
        client_cmd("pause");
-       PARA_NOTICE_LOG("%d seconds snooze time...\n", args_info.snooze_time_arg);
-       sleep(args_info.snooze_time_arg);
+       PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
+       sleep(sleep_time);
        client_cmd("play");
        client_cmd("play");
-       fade(args_info.snooze_in_vol_arg, args_info.snooze_in_fade_arg);
+       fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
 }
 
 static int configfile_exists(void)
 {
 }
 
 static int configfile_exists(void)
 {
-        static char *config_file;
+       static char *config_file;
 
 
-        if (!args_info.config_file_given) {
+       if (!conf.config_file_given) {
                char *home = para_homedir();
                char *home = para_homedir();
-               const char *conf = ".paraslash/fade.conf";
                free(config_file);
                free(config_file);
-               config_file = make_message("%s/%s", home, conf);
+               config_file = make_message("%s/.paraslash/fade.conf", home);
                free(home);
                free(home);
-                args_info.config_file_arg = config_file;
-        }
-        return file_exists(args_info.config_file_arg);
+               conf.config_file_arg = config_file;
+       }
+       return file_exists(conf.config_file_arg);
 }
 
 
 }
 
 
@@ -285,38 +279,40 @@ int main(int argc, char *argv[])
 {
        int ret;
 
 {
        int ret;
 
-       if (fade_cmdline_parser(argc, argv, &args_info))
+       if (fade_cmdline_parser(argc, argv, &conf))
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
+       HANDLE_VERSION_FLAG("fade", conf);
        ret = configfile_exists();
        ret = configfile_exists();
-       if (!ret && args_info.config_file_given) {
+       if (!ret && conf.config_file_given) {
                PARA_EMERG_LOG("can not read config file %s\n",
                PARA_EMERG_LOG("can not read config file %s\n",
-                       args_info.config_file_arg);
+                       conf.config_file_arg);
                exit(EXIT_FAILURE);
        }
        if (ret)
                exit(EXIT_FAILURE);
        }
        if (ret)
-               fade_cmdline_parser_configfile(args_info.config_file_arg,
-                       &args_info, 0, 0, 0);
-       if ((ret = open_mixer()) < 0) {
+               fade_cmdline_parser_configfile(conf.config_file_arg,
+                       &conf, 0, 0, 0);
+       ret = open_mixer();
+       if (ret < 0) {
                PARA_EMERG_LOG("can not open mixer device %s.",
                PARA_EMERG_LOG("can not open mixer device %s.",
-                       args_info.mixer_device_arg);
+                       conf.mixer_device_arg);
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
-       } else
-               close(ret);
+       }
+       close(ret);
        ret = 0;
        ret = 0;
-       setlinebuf(stdout);
-       if (!strcmp(args_info.mode_arg, "sleep")) {
+//     setlinebuf(stdout);
+       if (!strcmp(conf.mode_arg, "sleep")) {
                sweet_dreams();
                goto out;
        }
                sweet_dreams();
                goto out;
        }
-       if (!strcmp(args_info.mode_arg, "fade")) {
-               fade(args_info.fade_vol_arg, args_info.fade_time_arg);
+       if (!strcmp(conf.mode_arg, "fade")) {
+               fade(conf.fade_vol_arg, conf.fade_time_arg);
                goto out;
        }
                goto out;
        }
-       if (!strcmp(args_info.mode_arg, "snooze")) {
+       if (!strcmp(conf.mode_arg, "snooze")) {
                snooze();
                goto out;
        }
        ret = -1;
 out:
                snooze();
                goto out;
        }
        ret = -1;
 out:
-       return ret;
+       return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
 }
 }