/*
- * Copyright (C) 1998-2013 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
/** \file fade.c A volume fader and alarm clock for OSS. */
#include <regex.h>
+#include <lopsub.h>
-#include "fade.cmdline.h"
+#include "fade.lsg.h"
#include "para.h"
#include "fd.h"
#include "string.h"
#include "mix.h"
#include "error.h"
-#include "ggo.h"
#include "version.h"
-INIT_FADE_ERRLISTS;
-static struct fade_args_info conf;
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
enum mixer_id {MIXER_ENUM};
static char *mixer_name[] = {MIXER_NAMES};
static struct mixer supported_mixer[] = {MIXER_ARRAY};
#define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
+static struct lls_parse_result *lpr;
+
+#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, ...)
{
}
__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)
{
if (fade_time <= 0)
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",
+ OPT_STRING_VAL(MIXER_CHANNEL), vol, new_vol, secs);
diff = new_vol - vol;
if (!diff) {
sleep(secs);
exit(EXIT_FAILURE);
}
-static void change_afs_mode_and_play(char *afs_mode)
+static void change_afs_mode(const char *afs_mode)
{
char *cmd;
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 < 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 *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;
+ 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 */
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, OPT_STRING_VAL(MIXER_CHANNEL));
if (ret < 0)
return ret;
- change_afs_mode_and_play(fo_mood);
ret = fade(m, h, fov, fot);
if (ret < 0)
return ret;
if (ret < 0)
return ret;
}
- if (conf.sleep_mood_given)
- change_afs_mode_and_play(sleep_mood);
- else
+ 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 + fit)
(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;
{
int ret, val;
- if (conf.so_time_arg <= 0)
+ if (OPT_UINT32_VAL(SO_TIME) == 0)
return 1;
ret = m->get(h);
if (ret < 0)
return ret;
val = ret;
- if (val < conf.so_vol_arg)
- ret = m->set(h, conf.so_vol_arg);
+ if (val < OPT_UINT32_VAL(SO_VOL))
+ ret = m->set(h, OPT_UINT32_VAL(SO_VOL));
else
- ret = fade(m, h, conf.so_vol_arg, conf.so_time_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(conf.snooze_time_arg);
+ PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
+ OPT_UINT32_VAL(SNOOZE_TIME));
+ sleep(OPT_UINT32_VAL(SNOOZE_TIME));
client_cmd("play");
- return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
-}
-
-static int configfile_exists(void)
-{
- static char *config_file;
-
- 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;
- }
- return file_exists(conf.config_file_arg);
+ return fade(m, h, OPT_UINT32_VAL(SI_VOL), OPT_UINT32_VAL(SI_TIME));
}
static void init_mixers(void)
}
}
-static int set_channel(struct mixer *m, struct mixer_handle *h)
-{
- char *channels;
- int ret;
-
- 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;
- }
- channels = m->get_channels(h);
- printf("Available channels: %s\n", channels);
- free(channels);
- return ret;
-}
-
static int set_val(struct mixer *m, struct mixer_handle *h)
{
- return m->set(h, conf.val_arg);
+ return m->set(h, OPT_UINT32_VAL(VAL));
}
static struct mixer *get_mixer_or_die(void)
{
int i;
- if (!conf.mixer_api_given)
+ if (!OPT_GIVEN(MIXER_API))
i = DEFAULT_MIXER;
else
FOR_EACH_MIXER(i)
- if (!strcmp(mixer_name[i], conf.mixer_api_arg))
+ 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]);
exit(EXIT_FAILURE);
}
-__noreturn static void print_help_and_die(void)
+static void handle_help_flags(void)
{
- struct ggo_help h = DEFINE_GGO_HELP(fade);
- bool d = conf.detailed_help_given;
+ char *help;
- ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
- exit(0);
+ 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 = NULL;
-
- 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",
- conf.config_file_arg);
- exit(EXIT_FAILURE);
+ 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 (ret) {
- struct fade_cmdline_parser_params params = {
- .override = 0,
- .initialize = 0,
- .check_required = 0,
- .check_ambiguity = 0,
- .print_errors = 1
- };
- fade_cmdline_parser_config_file(conf.config_file_arg,
- &conf, ¶ms);
- loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ 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);
}
init_mixers();
m = get_mixer_or_die();
- ret = m->open(conf.mixer_device_arg, &h);
+ ret = m->open(OPT_STRING_VAL(MIXER_DEVICE), &h);
if (ret < 0)
- goto out;
- ret = set_channel(m, h);
+ 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);
+ }
if (ret < 0)
- goto out;
- switch (conf.mode_arg) {
- case mode_arg_fade:
- ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
- break;
- case mode_arg_snooze:
+ 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);
- break;
- case mode_arg_set:
+ else if (strcmp(OPT_STRING_VAL(MODE), "set") == 0)
ret = set_val(m, h);
- break;
- default: /* sleep mode */
+ else if (strcmp(OPT_STRING_VAL(MODE), "sleep") == 0)
ret = sweet_dreams(m, h);
- break;
+ else {
+ PARA_ERROR_LOG("invalid mode: %s\n", OPT_STRING_VAL(MODE));
+ ret = -ERRNO_TO_PARA_ERROR(EINVAL);
}
-out:
+close_mixer:
m->close(&h);
- if (ret < 0)
- PARA_EMERG_LOG("%s\n", para_strerror(-ret));
- return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
+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;
}