X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fade.c;h=2a001ebacde964eb1f710413540cc23f2bc85699;hp=1bfa0ba1ee8e9ca862de62fef4104d6c3e9cd4a4;hb=121015ff5cdbe6af7b84f6245ebe62fe2a52a859;hpb=46af7dd81149caee0409535d5d9884ed9fa5af0e diff --git a/fade.c b/fade.c index 1bfa0ba1..2a001eba 100644 --- a/fade.c +++ b/fade.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1998-2012 Andre Noll + * Copyright (C) 1998 Andre Noll * * 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,22 +24,34 @@ 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); + fprintf(stderr, "%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec); va_start(argp, fmt); vprintf(fmt, argp); va_end(argp); } __printf_2_3 void (*para_log)(int, const char*, ...) = date_log; +static int set_channel(struct mixer *m, struct mixer_handle *h, const char *channel) +{ + + PARA_NOTICE_LOG("using %s mixer channel\n", channel? + channel : "default"); + return m->set_channel(h, channel); +} + /* Fade to new volume in fade_time seconds. */ static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time) { @@ -48,13 +61,14 @@ 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); if (ret < 0) goto out; vol = ret; + PARA_NOTICE_LOG("fading %s from %d to %d in %u seconds\n", + conf.mixer_channel_arg, vol, new_vol, secs); diff = new_vol - vol; if (!diff) { sleep(secs); @@ -85,7 +99,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) { @@ -107,7 +121,7 @@ fail: exit(EXIT_FAILURE); } -static void change_afs_mode_and_play(char *afs_mode) +static void change_afs_mode(char *afs_mode) { char *cmd; @@ -117,7 +131,44 @@ static void change_afs_mode_and_play(char *afs_mode) cmd = make_message("select %s", afs_mode); client_cmd(cmd); free(cmd); - client_cmd("play"); +} + +static int set_initial_volume(struct mixer *m, struct mixer_handle *h) +{ + int i, ret; + + for (i = 0; i < conf.ivol_given; i++) { + char *p, *ch, *arg = para_strdup(conf.ivol_arg[i]); + int32_t iv; + p = strchr(arg, ':'); + if (p) { + *p = '\0'; + p++; + ch = arg; + } else { + p = arg; + ch = NULL; + } + ret = para_atoi32(p, &iv); + if (ret < 0) { + free(arg); + return ret; + } + ret = set_channel(m, h, ch); + if (!ch) + ch = "default"; + if (ret < 0) { + PARA_WARNING_LOG("ignoring channel %s\n", ch); + ret = 0; + } else { + PARA_INFO_LOG("initial volume %s: %d\n", ch, iv); + ret = m->set(h, iv); + } + free(arg); + if (ret < 0) + return ret; + } + return 1; } static int sweet_dreams(struct mixer *m, struct mixer_handle *h) @@ -133,7 +184,6 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h) 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); @@ -153,15 +203,18 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h) tm = localtime(&t1); } wake_time_epoch = mktime(tm); - PARA_INFO_LOG("waketime: %s", asctime(tm)); + PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min); client_cmd("stop"); sleep(1); if (fot) { - PARA_INFO_LOG("initial volume: %d\n", iv); - ret = m->set(h, iv); + ret = set_initial_volume(m, h); + if (ret < 0) + return ret; + change_afs_mode(fo_mood); + client_cmd("play"); + ret = set_channel(m, h, conf.mixer_channel_arg); if (ret < 0) return ret; - change_afs_mode_and_play(fo_mood); ret = fade(m, h, fov, fot); if (ret < 0) return ret; @@ -170,12 +223,14 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h) if (ret < 0) return ret; } - if (conf.sleep_mood_given) - change_afs_mode_and_play(sleep_mood); - else + if (conf.sleep_mood_given) { + change_afs_mode(sleep_mood); + client_cmd("play"); + } else client_cmd("stop"); if (!fit) return 1; + change_afs_mode(fi_mood); for (;;) { time(&t1); if (wake_time_epoch <= t1 + fit) @@ -186,7 +241,7 @@ static int sweet_dreams(struct mixer *m, struct mixer_handle *h) (delay % 3600) / 60); sleep(delay); } - change_afs_mode_and_play(fi_mood); + client_cmd("play"); ret = fade(m, h, fiv, fit); PARA_INFO_LOG("fade complete, returning\n"); return ret; @@ -233,7 +288,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]); @@ -241,33 +296,65 @@ static void init_mixers(void) } } -static int set_channel(struct mixer *m, struct mixer_handle *h) +static int set_val(struct mixer *m, struct mixer_handle *h) { - char *channels; - int ret; + return m->set(h, conf.val_arg); +} - ret = m->set_channel(h, conf.mixer_channel_arg); - if (ret >= 0) { - PARA_NOTICE_LOG("using %s mixer channel\n", - conf.mixer_channel_arg? conf.mixer_channel_arg - : "default"); - 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; } - channels = m->get_channels(h); - printf("Available channels: %s\n", channels); - free(channels); - return ret; + 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); +} + +/** + * The main function of para_fade. + * + * The executable is linked with the alsa or the oss mixer API, or both. It has + * a custom log function which prefixes log messages with the current date. + * + * \param argc Argument counter. + * \param argv Argument vector. + * + * \return EXIT_SUCCESS or EXIT_FAILURE. + */ 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,14 +371,19 @@ int main(int argc, char *argv[]) }; fade_cmdline_parser_config_file(conf.config_file_arg, &conf, ¶ms); + 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; - ret = set_channel(m, h); + ret = set_channel(m, h, conf.mixer_channel_arg); + if (ret == -E_BAD_CHANNEL) { + char *channels = m->get_channels(h); + printf("Available channels: %s\n", channels); + free(channels); + } if (ret < 0) goto out; switch (conf.mode_arg) { @@ -301,6 +393,9 @@ int main(int argc, char *argv[]) case mode_arg_snooze: ret = snooze(m, h); break; + case mode_arg_set: + ret = set_val(m, h); + break; default: /* sleep mode */ ret = sweet_dreams(m, h); break;