X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fade.c;h=5fbd0dd48027ad990345aa5d8f86fa0d952e6188;hp=6b87680131fa99608b5e258533c3a7fe8545391f;hb=87c857765884b6d05a2b090fa9509e90d220a2f8;hpb=93ea9f34dcb35755568e6c94f9b9445fd4d1984a diff --git a/fade.c b/fade.c index 6b876801..5fbd0dd4 100644 --- a/fade.c +++ b/fade.c @@ -1,34 +1,33 @@ /* - * Copyright (C) 1998-2008 Andre Noll + * Copyright (C) 1998-2011 Andre Noll * * 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 #include #include - -#include "fade.cmdline.h" -#include "para.h" -#include "fd.h" - -#include +#include #include #include /* EXIT_SUCCESS */ #include #include #include #include -#include +#include + +#include "fade.cmdline.h" +#include "para.h" +#include "fd.h" #include "string.h" #include "error.h" - INIT_FADE_ERRLISTS; -struct fade_args_info conf; +static struct fade_args_info conf; -void para_log(__a_unused int ll, const char *fmt,...) +__printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...) { va_list argp; time_t t1; @@ -43,30 +42,31 @@ void para_log(__a_unused int ll, const char *fmt,...) } /* - * open mixer device + * Open the mixer device. */ static int open_mixer(void) { + PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg); return para_open(conf.mixer_device_arg, O_RDWR, 42); } /* - * get volume via mixer_fd + * Get channel volume via mixer_fd. */ -static int do_get_vol(int mixer_fd) +static int get_mixer_channel(int mixer_fd) { int volume; - if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0) + if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &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 + * Open mixer, get volume and close mixer. */ -static int get_vol(void) +static int open_and_get_mixer_channel(void) { int mixer_fd; int volume; @@ -74,41 +74,65 @@ static int get_vol(void) mixer_fd = open_mixer(); if (mixer_fd < 0) return mixer_fd; - volume = do_get_vol(mixer_fd); + volume = get_mixer_channel(mixer_fd); close(mixer_fd); return volume; } /* - * set volume via mixer_fd + * Set channel volume via mixer_fd. */ -static int do_set_vol(int mixer_fd, int volume) +static int set_mixer_channel(int mixer_fd, int volume) { int tmp = (volume << 8) + volume; - if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0) + if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0) return -ERRNO_TO_PARA_ERROR(errno); return 1; } /* - * open mixer, set volume and close mixer + * Open mixer, set volume and close mixer. */ -static int set_vol(int volume) +static int open_and_set_mixer_channel(int volume) { int mixer_fd, ret = open_mixer(); if (ret < 0) return ret; mixer_fd = ret; - ret = do_set_vol(mixer_fd, volume); + ret = set_mixer_channel(mixer_fd, volume); close(mixer_fd); return ret; } +static void fixup_mixer_channel_arg(void) +{ + int val = SOUND_MIXER_VOLUME; /* STFU, gcc */ + + switch (conf.mixer_channel_arg) { + case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break; + case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break; + case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break; + case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break; + case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break; + case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break; + case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break; + case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break; + case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break; + case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break; + case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break; + case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break; + case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break; + case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break; + default: break; + } + conf.mixer_channel_arg = val; +} + /* * Open mixer, get volume, fade to new_vol in secs seconds and - * close mixer + * close mixer. */ static int fade(int new_vol, int fade_time) { @@ -125,7 +149,7 @@ static int fade(int new_vol, int fade_time) if (ret < 0) return ret; mixer_fd = ret; - ret = do_get_vol(mixer_fd); + ret = get_mixer_channel(mixer_fd); if (ret < 0) goto out; vol = ret; @@ -143,7 +167,7 @@ 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 = set_mixer_channel(mixer_fd, vol); if (ret < 0) goto out; //printf("vol = %i\n", vol); @@ -194,14 +218,14 @@ static int sweet_dreams(void) 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); @@ -217,42 +241,45 @@ 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 = open_and_set_mixer_channel(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(fov, fot); if (ret < 0) return ret; } else { - ret = set_vol(sf); + ret = open_and_set_mixer_channel(fov); if (ret < 0) return ret; } - change_afs_mode_and_play(sleep_mode); - if (!wf) + if (conf.sleep_mood_given) + change_afs_mode_and_play(sleep_mood); + else + client_cmd("stop"); + 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(fiv, fit); PARA_INFO_LOG("fade complete, returning\n"); return ret; } @@ -262,20 +289,20 @@ static int snooze(void) int ret; unsigned sleep_time; - 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); + sleep_time = conf.so_time_arg; + if (open_and_get_mixer_channel() < conf.so_vol_arg) + ret = open_and_set_mixer_channel(conf.so_vol_arg); else - ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg); + ret = fade(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); client_cmd("play"); - return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg); + return fade(conf.si_vol_arg, conf.si_time_arg); } static int configfile_exists(void) @@ -310,25 +337,24 @@ 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, ¶ms); } - if (!strcmp(conf.mode_arg, "sleep")) { - ret = sweet_dreams(); - goto out; - } - if (!strcmp(conf.mode_arg, "fade")) { + fixup_mixer_channel_arg(); + switch (conf.mode_arg) { + case mode_arg_fade: ret = fade(conf.fade_vol_arg, conf.fade_time_arg); - goto out; - } - if (!strcmp(conf.mode_arg, "snooze")) { + break; + case mode_arg_snooze: ret = snooze(); - goto out; + break; + default: /* sleep mode */ + ret = sweet_dreams(); + break; } - ret = -E_FADE_SYNTAX; -out: if (ret < 0) PARA_EMERG_LOG("%s\n", para_strerror(-ret)); return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;