X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fade.c;h=c86b938532022b4aea628cfb0299e4a6510b4d2e;hp=3c622ef0f9ec3356169827d2959b75e2f4f7592a;hb=19d9318abf42debb15d833d4e56ab636893285c3;hpb=34762fdc56d5294c15e03fda433ecfccdc6c5fb2 diff --git a/fade.c b/fade.c index 3c622ef0..c86b9385 100644 --- a/fade.c +++ b/fade.c @@ -1,23 +1,14 @@ /* * Copyright (C) 1998-2007 Andre Noll * - * 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 - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Licensed under the GPL v2. For licencing details see COPYING. */ /** \file fade.c a volume fader and alarm clock */ +#include +#include + #include "fade.cmdline.h" #include "para.h" #include "fd.h" @@ -92,10 +83,10 @@ static int get_vol(void) */ 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) - return 0; + return -1; return 1; } @@ -104,18 +95,13 @@ static int do_set_vol(int mixer_fd, 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); -out: return ret; } @@ -125,18 +111,18 @@ out: */ static void fade(int new_vol, int fade_time) { - int vol, mixer_fd = -1, diff, incr; + int vol, mixer_fd, diff, incr; unsigned secs; struct timespec ts; unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */ if (fade_time <= 0) - goto out; + return; secs = fade_time; PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs); mixer_fd = open_mixer(); if (mixer_fd < 0) - goto out; + return; vol = do_get_vol(mixer_fd); if (vol < 0) goto out; @@ -154,25 +140,42 @@ 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)) + if (do_set_vol(mixer_fd, vol) < 0) goto out; //printf("vol = %i\n", vol); nanosleep(&ts, NULL); } out: - if (mixer_fd >= 0) - close(mixer_fd); + close(mixer_fd); } -static int client_cmd(const char *cmd,...) +static void client_cmd(const char *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); - return ret; + if (ret < 0) + 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"); } /* @@ -184,16 +187,14 @@ static void sweet_dreams(void) 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; + 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); @@ -208,26 +209,22 @@ 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); + change_afs_mode_and_play(fa_mode); fade(sv, sf); - } - if (sleep_stream) { - cmd = make_message("csp %s\n", sleep_stream); - client_cmd(cmd); - free(cmd); } else - client_cmd("stop"); + set_vol(sf); + change_afs_mode_and_play(sleep_mode); if (!wf) return; for (;;) { @@ -236,15 +233,13 @@ static void sweet_dreams(void) 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); + change_afs_mode_and_play(wake_mode); fade(wv, wf); - PARA_INFO_LOG("%s", "fade complete, returning\n"); + PARA_INFO_LOG("fade complete, returning\n"); } static void snooze(void) @@ -293,17 +288,25 @@ int main(int argc, char *argv[]) conf.config_file_arg); exit(EXIT_FAILURE); } - if (ret) - fade_cmdline_parser_configfile(conf.config_file_arg, - &conf, 0, 0, 0); - if ((ret = open_mixer()) < 0) { + if (ret) { + struct fade_cmdline_parser_params params = { + .override = 0, + .initialize = 0, + .check_required = 0, + .check_ambiguity = 0 + }; + 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); - } else - close(ret); + } + close(ret); ret = 0; - setlinebuf(stdout); +// setlinebuf(stdout); if (!strcmp(conf.mode_arg, "sleep")) { sweet_dreams(); goto out; @@ -318,5 +321,5 @@ int main(int argc, char *argv[]) } ret = -1; out: - return ret; + return ret < 0? EXIT_FAILURE : EXIT_SUCCESS; }