X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=fade.c;h=38fcdfe7914a7afeb63ee37e6db43b32abfebbed;hb=d25417f95997d9e502d021d5fdb61ac843947495;hp=6b87680131fa99608b5e258533c3a7fe8545391f;hpb=93ea9f34dcb35755568e6c94f9b9445fd4d1984a;p=paraslash.git diff --git a/fade.c b/fade.c index 6b876801..34237c6b 100644 --- a/fade.c +++ b/fade.c @@ -1,134 +1,82 @@ /* - * Copyright (C) 1998-2008 Andre Noll + * Copyright (C) 1998 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 -#include "fade.cmdline.h" +#include "fade.lsg.h" #include "para.h" #include "fd.h" - -#include -#include -#include /* EXIT_SUCCESS */ -#include -#include -#include -#include -#include #include "string.h" +#include "mix.h" #include "error.h" +#include "version.h" + +/** Array of error strings. */ +DEFINE_PARA_ERRLIST; +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)++) -INIT_FADE_ERRLISTS; -struct fade_args_info conf; +static struct lls_parse_result *lpr; -void para_log(__a_unused int ll, const char *fmt,...) +#define CMD_PTR (lls_cmd(0, fade_suite)) +#define OPT_RESULT(_name) (lls_opt_result(LSG_FADE_PARA_FADE_OPT_ ## _name, lpr)) +#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name))) +#define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name))) +#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name))) + +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; -/* - * open mixer device - */ -static int open_mixer(void) +static int set_channel(struct mixer *m, struct mixer_handle *h, const char *channel) { - return para_open(conf.mixer_device_arg, O_RDWR, 42); -} -/* - * get volume via mixer_fd - */ -static int do_get_vol(int mixer_fd) -{ - int volume; - - if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0) - return -ERRNO_TO_PARA_ERROR(errno); - /* take the mean value of left and right volume */ - return (volume % 256 + (volume >> 8)) / 2; + PARA_NOTICE_LOG("using %s mixer channel\n", channel? + channel : "default"); + return m->set_channel(h, channel); } -/* - * open mixer, get volume and close mixer - */ -static int get_vol(void) -{ - int mixer_fd; - int volume; - - mixer_fd = open_mixer(); - if (mixer_fd < 0) - return mixer_fd; - volume = do_get_vol(mixer_fd); - close(mixer_fd); - return volume; -} - -/* - * set volume via mixer_fd - */ -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 -ERRNO_TO_PARA_ERROR(errno); - return 1; -} - -/* - * open mixer, set volume and close mixer - */ -static int set_vol(int volume) +/* Fade to new volume in fade_time seconds. */ +static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time) { - int mixer_fd, ret = open_mixer(); - - if (ret < 0) - return ret; - mixer_fd = ret; - ret = do_set_vol(mixer_fd, volume); - close(mixer_fd); - return ret; -} - -/* - * Open mixer, get volume, fade to new_vol in secs seconds and - * close mixer - */ -static int fade(int new_vol, int fade_time) -{ - int vol, mixer_fd, diff, incr, ret; + int vol, diff, incr, ret; unsigned secs; struct timespec ts; 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 = open_mixer(); - if (ret < 0) - return ret; - mixer_fd = ret; - ret = do_get_vol(mixer_fd); + ret = m->get(h); if (ret < 0) goto out; vol = ret; + PARA_NOTICE_LOG("fading %s from %d to %d in %u seconds\n", + OPT_STRING_VAL(MIXER_CHANNEL), vol, new_vol, secs); diff = new_vol - vol; if (!diff) { sleep(secs); @@ -143,70 +91,113 @@ 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 = m->set(h, vol); if (ret < 0) goto out; //printf("vol = %i\n", vol); nanosleep(&ts, NULL); } out: - close(mixer_fd); return ret; } static void client_cmd(const char *cmd) { - int ret, fds[3] = {0, 0, 0}; + int ret, status, fds[3] = {0, 0, 0}; 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) { - PARA_EMERG_LOG("%s\n", para_strerror(-ret)); - exit(EXIT_FAILURE); + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); + goto fail; } do - ret = wait(NULL); - while (ret != -1 && errno != ECHILD); + pid = waitpid(pid, &status, 0); + while (pid == -1 && errno == EINTR); + if (pid < 0) { + PARA_ERROR_LOG("%s\n", strerror(errno)); + goto fail; + } + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + goto fail; + return; +fail: + PARA_EMERG_LOG("command \"%s\" failed\n", cmd); + exit(EXIT_FAILURE); } -static void change_afs_mode_and_play(char *afs_mode) +static void change_afs_mode(const char *afs_mode) { char *cmd; client_cmd("stop"); if (!afs_mode) return; - cmd = make_message("select %s\n", afs_mode); + cmd = make_message("select %s", afs_mode); client_cmd(cmd); free(cmd); - client_cmd("play"); } -/* - * sleep - */ -static int sweet_dreams(void) +static int set_initial_volume(struct mixer *m, struct mixer_handle *h) +{ + int i, ret; + + for (i = 0; i < OPT_GIVEN(IVOL); i++) { + const char *val = lls_string_val(i, OPT_RESULT(IVOL)); + char *p, *ch, *arg = para_strdup(val); + 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) { time_t t1, wake_time_epoch; 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; + int ret, min = OPT_UINT32_VAL(WAKE_MIN); + const char *fo_mood = OPT_STRING_VAL(FO_MOOD); + const char *fi_mood = OPT_STRING_VAL(FI_MOOD); + const char *sleep_mood = OPT_STRING_VAL(SLEEP_MOOD); + int fit = OPT_UINT32_VAL(FI_TIME); + int fot = OPT_UINT32_VAL(FO_TIME); + int fiv = OPT_UINT32_VAL(FI_VOL); + int fov = OPT_UINT32_VAL(FO_VOL); /* calculate wake time */ time(&t1); - if (conf.wake_hour_given) { - int hour = conf.wake_hour_arg; + if (OPT_GIVEN(WAKE_HOUR)) { + int hour = OPT_UINT32_VAL(WAKE_HOUR); tm = localtime(&t1); if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) { t1 += 86400; /* wake time is tomorrow */ @@ -217,119 +208,234 @@ 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)); + PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min); client_cmd("stop"); sleep(1); - if (sf) { - PARA_INFO_LOG("initial volume: %d\n", iv); - ret = set_vol(iv); + if (fot) { + ret = set_initial_volume(m, h); if (ret < 0) return ret; - change_afs_mode_and_play(fa_mode); - ret = fade(sv, sf); + change_afs_mode(fo_mood); + client_cmd("play"); + ret = set_channel(m, h, OPT_STRING_VAL(MIXER_CHANNEL)); + if (ret < 0) + return ret; + ret = fade(m, h, fov, fot); if (ret < 0) return ret; } else { - ret = set_vol(sf); + ret = m->set(h, fov); if (ret < 0) return ret; } - change_afs_mode_and_play(sleep_mode); - if (!wf) + if (OPT_GIVEN(SLEEP_MOOD)) { + 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 + 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); + client_cmd("play"); + ret = fade(m, h, fiv, fit); PARA_INFO_LOG("fade complete, returning\n"); return ret; } -static int snooze(void) +static int snooze(struct mixer *m, struct mixer_handle *h) { - int ret; - unsigned sleep_time; + int ret, val; - if (conf.snooze_time_arg <= 0) + if (OPT_UINT32_VAL(SO_TIME) == 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); + ret = m->get(h); + if (ret < 0) + return ret; + val = ret; + if (val < OPT_UINT32_VAL(SO_VOL)) + ret = m->set(h, OPT_UINT32_VAL(SO_VOL)); else - ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg); + ret = fade(m, h, OPT_UINT32_VAL(SO_VOL), + OPT_UINT32_VAL(SO_TIME)); if (ret < 0) return ret; client_cmd("pause"); - PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg); - sleep(sleep_time); + PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n", + OPT_UINT32_VAL(SNOOZE_TIME)); + sleep(OPT_UINT32_VAL(SNOOZE_TIME)); client_cmd("play"); - return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg); + return fade(m, h, OPT_UINT32_VAL(SI_VOL), OPT_UINT32_VAL(SI_TIME)); } -static int configfile_exists(void) +static void init_mixers(void) { - static char *config_file; + int i; - if (!conf.config_file_given) { - char *home = para_homedir(); - free(config_file); - config_file = make_message("%s/.paraslash/fade.conf", home); - free(home); - conf.config_file_arg = config_file; + FOR_EACH_MIXER(i) { + struct mixer *m = &supported_mixer[i]; + PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n", + i, mixer_name[i]); + m->init(m); } - return file_exists(conf.config_file_arg); } -int main(int argc, char *argv[]) +static int set_val(struct mixer *m, struct mixer_handle *h) { - int ret; + return m->set(h, OPT_UINT32_VAL(VAL)); +} + +static struct mixer *get_mixer_or_die(void) +{ + int i; - if (fade_cmdline_parser(argc, argv, &conf)) - exit(EXIT_FAILURE); - HANDLE_VERSION_FLAG("fade", conf); - ret = configfile_exists(); - if (!ret && conf.config_file_given) { - PARA_EMERG_LOG("can not read config file %s\n", - conf.config_file_arg); - exit(EXIT_FAILURE); + if (!OPT_GIVEN(MIXER_API)) + i = DEFAULT_MIXER; + else + FOR_EACH_MIXER(i) + if (!strcmp(mixer_name[i], OPT_STRING_VAL(MIXER_API))) + break; + if (i < NUM_SUPPORTED_MIXERS) { + PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]); + return supported_mixer + i; } - 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); + printf("available mixer APIs: "); + FOR_EACH_MIXER(i) { + int d = (i == DEFAULT_MIXER); + printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : ""); } - if (!strcmp(conf.mode_arg, "sleep")) { - ret = sweet_dreams(); - goto out; + printf("\n"); + exit(EXIT_FAILURE); +} + +static void handle_help_flags(void) +{ + char *help; + + if (OPT_GIVEN(DETAILED_HELP)) + help = lls_long_help(CMD_PTR); + else if (OPT_GIVEN(HELP)) + help = lls_short_help(CMD_PTR); + else + return; + printf("%s\n", help); + free(help); + exit(EXIT_SUCCESS); +} + +/** + * 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[]) +{ + const struct lls_command *cmd = CMD_PTR; + int ret; + char *cf, *errctx; + struct mixer *m; + struct mixer_handle *h; + void *map; + size_t sz; + + ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx)); + if (ret < 0) + goto fail; + loglevel = OPT_UINT32_VAL(LOGLEVEL); + version_handle_flag("fade", OPT_GIVEN(VERSION)); + handle_help_flags(); + + if (OPT_GIVEN(CONFIG_FILE)) + cf = para_strdup(OPT_STRING_VAL(CONFIG_FILE)); + else { + char *home = para_homedir(); + cf = make_message("%s/.paraslash/fade.conf", home); + free(home); } - if (!strcmp(conf.mode_arg, "fade")) { - ret = fade(conf.fade_vol_arg, conf.fade_time_arg); - goto out; + ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL); + if (ret < 0) { + if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT)) + goto free_cf; + if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) && OPT_GIVEN(CONFIG_FILE)) + goto free_cf; + } else { + int cf_argc; + char **cf_argv; + struct lls_parse_result *cf_lpr, *merged_lpr; + ret = lls(lls_convert_config(map, sz, NULL, &cf_argv, &errctx)); + para_munmap(map, sz); + if (ret < 0) + goto free_cf; + cf_argc = ret; + ret = lls(lls_parse(cf_argc, cf_argv, CMD_PTR, &cf_lpr, &errctx)); + lls_free_argv(cf_argv); + if (ret < 0) + goto free_cf; + ret = lls(lls_merge(lpr, cf_lpr, CMD_PTR, &merged_lpr, + &errctx)); + lls_free_parse_result(cf_lpr, CMD_PTR); + if (ret < 0) + goto free_cf; + lls_free_parse_result(lpr, CMD_PTR); + lpr = merged_lpr; + loglevel = OPT_UINT32_VAL(LOGLEVEL); } - if (!strcmp(conf.mode_arg, "snooze")) { - ret = snooze(); - goto out; + init_mixers(); + m = get_mixer_or_die(); + ret = m->open(OPT_STRING_VAL(MIXER_DEVICE), &h); + if (ret < 0) + goto free_cf; + ret = set_channel(m, h, OPT_STRING_VAL(MIXER_CHANNEL)); + if (ret == -E_BAD_CHANNEL) { + char *channels = m->get_channels(h); + printf("Available channels: %s\n", channels); + free(channels); } - ret = -E_FADE_SYNTAX; -out: if (ret < 0) - PARA_EMERG_LOG("%s\n", para_strerror(-ret)); - return ret < 0? EXIT_FAILURE : EXIT_SUCCESS; + goto close_mixer; + if (strcmp(OPT_STRING_VAL(MODE), "fade") == 0) + ret = fade(m, h, OPT_UINT32_VAL(FADE_VOL), + OPT_UINT32_VAL(FADE_TIME)); + else if (strcmp(OPT_STRING_VAL(MODE), "snooze") == 0) + ret = snooze(m, h); + else if (strcmp(OPT_STRING_VAL(MODE), "set") == 0) + ret = set_val(m, h); + else if (strcmp(OPT_STRING_VAL(MODE), "sleep") == 0) + ret = sweet_dreams(m, h); + else { + PARA_ERROR_LOG("invalid mode: %s\n", OPT_STRING_VAL(MODE)); + ret = -ERRNO_TO_PARA_ERROR(EINVAL); + } +close_mixer: + m->close(&h); +free_cf: + free(cf); + lls_free_parse_result(lpr, CMD_PTR); + if (ret >= 0) + return EXIT_SUCCESS; +fail: + if (errctx) + PARA_ERROR_LOG("%s\n", errctx); + free(errctx); + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); + return EXIT_FAILURE; }