X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fade.c;h=48faed83124baeea273337ae950b0e88aeb8fc03;hp=38a6517cd0dbadc47671f00b7c0b3ba2052ec02c;hb=1889013e900f08d5ee842a50dc13c66689ebf15b;hpb=3689799ede7bba230ca544ac953392b428c3d662 diff --git a/fade.c b/fade.c index 38a6517c..48faed83 100644 --- a/fade.c +++ b/fade.c @@ -1,10 +1,10 @@ /* - * Copyright (C) 1998-2007 Andre Noll + * Copyright (C) 1998-2008 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ -/** \file fade.c a volume fader and alarm clock */ +/** \file fade.c A volume fader and alarm clock for linux. */ #include #include @@ -15,7 +15,6 @@ #include #include -#include #include /* EXIT_SUCCESS */ #include #include @@ -23,8 +22,10 @@ #include #include #include "string.h" +#include "error.h" +INIT_FADE_ERRLISTS; struct fade_args_info conf; void para_log(__a_unused int ll, const char *fmt,...) @@ -46,7 +47,7 @@ void para_log(__a_unused int ll, const char *fmt,...) */ static int open_mixer(void) { - return open(conf.mixer_device_arg, O_RDWR, 0); + return para_open(conf.mixer_device_arg, O_RDWR, 42); } /* @@ -57,7 +58,7 @@ static int do_get_vol(int mixer_fd) int volume; if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0) - return -1; + return -ERRNO_TO_PARA_ERROR(errno); /* take the mean value of left and right volume */ return (volume % 256 + (volume >> 8)) / 2; } @@ -72,7 +73,7 @@ static int get_vol(void) mixer_fd = open_mixer(); if (mixer_fd < 0) - return -1; + return mixer_fd; volume = do_get_vol(mixer_fd); close(mixer_fd); return volume; @@ -86,7 +87,7 @@ 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 -1; + return -ERRNO_TO_PARA_ERROR(errno); return 1; } @@ -109,23 +110,25 @@ static int set_vol(int volume) * Open mixer, get volume, fade to new_vol in secs seconds and * close mixer */ -static void fade(int new_vol, int fade_time) +static int fade(int new_vol, int fade_time) { - int vol, mixer_fd, diff, incr; + int vol, mixer_fd, diff, incr, ret; unsigned secs; struct timespec ts; unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */ if (fade_time <= 0) - return; + return 1; secs = fade_time; PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs); - mixer_fd = open_mixer(); - if (mixer_fd < 0) - return; - vol = do_get_vol(mixer_fd); - if (vol < 0) + ret = open_mixer(); + if (ret < 0) + return ret; + mixer_fd = ret; + ret = do_get_vol(mixer_fd); + if (ret < 0) goto out; + vol = ret; diff = new_vol - vol; if (!diff) { sleep(secs); @@ -140,16 +143,18 @@ static void 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; - if (do_set_vol(mixer_fd, vol) < 0) + ret = do_set_vol(mixer_fd, vol); + if (ret < 0) goto out; //printf("vol = %i\n", vol); nanosleep(&ts, NULL); } out: close(mixer_fd); + return ret; } -static int client_cmd(const char *cmd,...) +static void client_cmd(const char *cmd) { int ret, fds[3] = {0, 0, 0}; pid_t pid; @@ -158,28 +163,45 @@ static int client_cmd(const char *cmd,...) PARA_INFO_LOG("%s\n", cmdline); ret = para_exec_cmdline_pid(&pid, cmdline, fds); free(cmdline); - return ret; + if (ret < 0) { + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); + exit(EXIT_FAILURE); + } + do + ret = wait(NULL); + while (ret != -1 && errno != ECHILD); +} + +static void change_afs_mode_and_play(char *afs_mode) +{ + char *cmd; + + client_cmd("stop"); + if (!afs_mode) + return; + cmd = make_message("select %s\n", afs_mode); + client_cmd(cmd); + free(cmd); + client_cmd("play"); } /* * sleep */ -static void sweet_dreams(void) +static int sweet_dreams(void) { time_t t1, wake_time_epoch; unsigned int delay; struct tm *tm; - 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; + 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 *cmd, *sleep_stream = conf.sleep_stream_given? - conf.sleep_stream_arg : NULL; /* calculate wake time */ time(&t1); @@ -194,61 +216,69 @@ static void sweet_dreams(void) tm->tm_min = min; tm->tm_sec = 0; } else { - t1 += 8 * 60 * 60; + t1 += 9 * 60 * 60; /* nine hours from now */ PARA_INFO_LOG("default wake time: %lu\n", t1); tm = localtime(&t1); } wake_time_epoch = mktime(tm); PARA_INFO_LOG("waketime: %s", asctime(tm)); + client_cmd("stop"); + sleep(1); if (sf) { PARA_INFO_LOG("initial volume: %d\n", iv); - set_vol(iv); - cmd = make_message("csp %s\n", fa_stream); - client_cmd(cmd); - free(cmd); - fade(sv, sf); + ret = set_vol(iv); + if (ret < 0) + return ret; + change_afs_mode_and_play(fa_mode); + ret = fade(sv, sf); + if (ret < 0) + return ret; + } else { + ret = set_vol(sf); + if (ret < 0) + return ret; } - if (sleep_stream) { - cmd = make_message("csp %s\n", sleep_stream); - client_cmd(cmd); - free(cmd); - } else + if (conf.sleep_mode_given) + change_afs_mode_and_play(sleep_mode); + else client_cmd("stop"); if (!wf) - return; + return 1; for (;;) { time(&t1); if (wake_time_epoch <= t1 + wf) break; delay = wake_time_epoch - t1 - wf; PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n", - delay, delay / 3600, + delay, delay / 3600, (delay % 3600) / 60); sleep(delay); } - cmd = make_message("csp %s\n", wake_stream); - client_cmd(cmd); - free(cmd); - fade(wv, wf); - PARA_INFO_LOG("%s", "fade complete, returning\n"); + change_afs_mode_and_play(wake_mode); + ret = fade(wv, wf); + PARA_INFO_LOG("fade complete, returning\n"); + return ret; } -static void snooze(void) +static int snooze(void) { + int ret; unsigned sleep_time; if (conf.snooze_time_arg <= 0) - return; + return 1; sleep_time = conf.snooze_time_arg; if (get_vol() < conf.snooze_out_vol_arg) - set_vol(conf.snooze_out_vol_arg); + ret = set_vol(conf.snooze_out_vol_arg); else - fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg); + ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg); + if (ret < 0) + return ret; client_cmd("pause"); PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg); sleep(sleep_time); client_cmd("play"); - fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg); + return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg); } static int configfile_exists(void) @@ -265,7 +295,6 @@ static int configfile_exists(void) return file_exists(conf.config_file_arg); } - int main(int argc, char *argv[]) { int ret; @@ -289,28 +318,21 @@ int main(int argc, char *argv[]) fade_cmdline_parser_config_file(conf.config_file_arg, &conf, ¶ms); } - ret = open_mixer(); - if (ret < 0) { - PARA_EMERG_LOG("can not open mixer device %s.", - conf.mixer_device_arg); - exit(EXIT_FAILURE); - } - close(ret); - ret = 0; -// setlinebuf(stdout); if (!strcmp(conf.mode_arg, "sleep")) { - sweet_dreams(); + ret = sweet_dreams(); goto out; } if (!strcmp(conf.mode_arg, "fade")) { - fade(conf.fade_vol_arg, conf.fade_time_arg); + ret = fade(conf.fade_vol_arg, conf.fade_time_arg); goto out; } if (!strcmp(conf.mode_arg, "snooze")) { - snooze(); + ret = snooze(); goto out; } - ret = -1; + ret = -E_FADE_SYNTAX; out: + if (ret < 0) + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); return ret < 0? EXIT_FAILURE : EXIT_SUCCESS; }