-/*
- * Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
-/** \file mixer.c A volume fader and alarm clock for OSS. */
+/** \file mixer.c A volume fader and alarm clock. */
#include <regex.h>
#include <lopsub.h>
+#include <math.h>
#include "mixer.lsg.h"
#include "para.h"
+#include "lsu.h"
#include "fd.h"
#include "string.h"
#include "mix.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};
+/* At least one of the two is defined if this file gets compiled. */
+static const struct mixer *mixers[] = {
+#ifdef HAVE_ALSA
+ &alsa_mixer,
+#endif
+#ifdef HAVE_OSS
+ &oss_mixer,
+#endif
+};
+
+#define NUM_SUPPORTED_MIXERS (ARRAY_SIZE(mixers))
#define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
static struct lls_parse_result *lpr, *sub_lpr;
#define OPT_STRING_VAL(_cmd, _opt) (lls_string_val(0, OPT_RESULT(_cmd, _opt)))
#define OPT_UINT32_VAL(_cmd, _opt) (lls_uint32_val(0, OPT_RESULT(_cmd, _opt)))
-typedef int (*mixer_subcommand_handler_t)(struct mixer *, struct mixer_handle *);
+typedef int (*mixer_subcommand_handler_t)(const struct mixer *);
#define EXPORT_CMD(_cmd) const mixer_subcommand_handler_t \
lsg_mixer_com_ ## _cmd ## _user_data = &com_ ## _cmd;
}
__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)
+static int set_channel(const struct mixer *m, struct mixer_handle *h,
+ const char *channel)
{
PARA_NOTICE_LOG("using %s mixer channel\n", channel?
return m->set_channel(h, channel);
}
+static void millisleep(int ms)
+{
+ struct timespec ts;
+
+ PARA_INFO_LOG("sleeping %dms\n", ms);
+ if (ms < 0)
+ return;
+ ts.tv_sec = ms / 1000,
+ ts.tv_nsec = (ms % 1000) * 1000 * 1000;
+ nanosleep(&ts, NULL);
+}
+
+/*
+ * This implements the inverse function of t -> t^alpha, scaled to the time
+ * interval [0,T] and the range given by old_vol and new_vol. It returns the
+ * amount of milliseconds until the given volume is reached.
+ */
+static unsigned volume_time(double vol, double old_vol, double new_vol,
+ double T, double alpha)
+{
+ double c, d, x;
+
+ if (old_vol < new_vol) {
+ c = old_vol;
+ d = new_vol;
+ } else {
+ c = new_vol;
+ d = old_vol;
+ }
+
+ x = T * exp(log(((vol - c) / (d - c))) / alpha);
+ assert(x <= T);
+ if (old_vol < new_vol)
+ return x;
+ else
+ return T - x;
+}
+
/* Fade to new volume in fade_time seconds. */
-static int fade(struct mixer *m, struct mixer_handle *h, uint32_t new_vol,
+static int fade(const struct mixer *m, struct mixer_handle *h, uint32_t new_vol,
uint32_t fade_time)
{
- int vol, diff, incr, ret;
- unsigned secs;
- struct timespec ts;
- unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
+ int i, T, old_vol, ret, slept, incr;
+ double ms, alpha;
+ uint32_t fe = OPT_UINT32_VAL(PARA_MIXER, FADE_EXPONENT);
- if (fade_time <= 0)
- return m->set(h, new_vol);
- secs = fade_time;
+ if (fade_time <= 0 || fe >= 100) {
+ ret = m->set(h, new_vol);
+ if (ret < 0)
+ return ret;
+ goto sleep;
+ }
+ alpha = (100 - fe) / 100.0;
ret = m->get(h);
if (ret < 0)
- goto out;
- vol = ret;
+ return ret;
+ old_vol = ret;
+ if (old_vol == new_vol)
+ goto sleep;
PARA_NOTICE_LOG("fading %s from %d to %u in %u seconds\n",
- OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL), vol, new_vol, secs);
- diff = new_vol - vol;
- if (!diff) {
- sleep(secs);
- goto out;
- }
- incr = diff > 0? 1: -1;
- diff = diff > 0? diff: -diff;
- tmp = secs * 1000 / diff;
- tmp2 = tmp % 1000;
- while ((new_vol - vol) * incr > 0) {
- ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
- ts.tv_sec = tmp / 1000; /* really nec ?*/
- //printf("ts.tv_sec: %i\n", ts.tv_nsec);
- vol += incr;
- ret = m->set(h, vol);
+ OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL), old_vol,
+ new_vol, fade_time);
+ incr = old_vol < new_vol? 1 : -1;
+ T = fade_time * 1000;
+ i = old_vol;
+ slept = 0;
+ do {
+ ms = volume_time(i + incr, old_vol, new_vol, T, alpha);
+ millisleep(ms - slept);
+ slept = ms;
+ i += incr;
+ ret = m->set(h, i);
if (ret < 0)
- goto out;
- //printf("vol = %i\n", vol);
- nanosleep(&ts, NULL);
+ return ret;
+ } while (i != new_vol);
+ return 1;
+sleep:
+ sleep(fade_time);
+ return ret;
+}
+
+static int open_mixer_and_set_channel(const struct mixer *m, struct mixer_handle **h)
+{
+ int ret;
+
+ ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), h);
+ if (ret < 0)
+ return ret;
+ ret = set_channel(m, *h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
+ if (ret == -E_BAD_CHANNEL) {
+ char *channels = m->get_channels(*h);
+ printf("Available channels: %s\n", channels);
+ free(channels);
}
-out:
+ if (ret < 0)
+ m->close(h);
return ret;
}
-static int com_fade(struct mixer *m, struct mixer_handle *h)
+
+static int com_fade(const struct mixer *m)
{
uint32_t new_vol = OPT_UINT32_VAL(FADE, FADE_VOL);
uint32_t fade_time = OPT_UINT32_VAL(FADE, FADE_TIME);
- return fade(m, h, new_vol, fade_time);
+ struct mixer_handle *h;
+ int ret;
+
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ return ret;
+ ret = fade(m, h, new_vol, fade_time);
+ m->close(&h);
+ return ret;
}
EXPORT_CMD(fade);
{
char *cmd;
- client_cmd("stop");
- if (!afs_mode)
- return;
cmd = make_message("select %s", afs_mode);
client_cmd(cmd);
free(cmd);
}
-static int set_initial_volume(struct mixer *m, struct mixer_handle *h)
+static int set_initial_volume(const struct mixer *m, struct mixer_handle *h)
{
int i, ret;
return 1;
}
-static int com_sleep(struct mixer *m, struct mixer_handle *h)
+static int com_sleep(const struct mixer *m)
{
time_t t1, wake_time_epoch;
unsigned int delay;
struct tm *tm;
- int ret, min = OPT_UINT32_VAL(SLEEP, WAKE_MIN);
+ int ret;
+ const char *wake_time = OPT_STRING_VAL(SLEEP, WAKE_TIME);
const char *fo_mood = OPT_STRING_VAL(SLEEP, FO_MOOD);
const char *fi_mood = OPT_STRING_VAL(SLEEP, FI_MOOD);
const char *sleep_mood = OPT_STRING_VAL(SLEEP, SLEEP_MOOD);
int fot = OPT_UINT32_VAL(SLEEP, FO_TIME);
int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL);
int fov = OPT_UINT32_VAL(SLEEP, FO_VOL);
+ int32_t hour, min = 0;
+ char *tmp, *wt;
+ struct mixer_handle *h;
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ return ret;
+ wt = para_strdup(wake_time + (wake_time[0] == '+'));
/* calculate wake time */
time(&t1);
- if (OPT_GIVEN(SLEEP, WAKE_HOUR)) {
- int hour = OPT_UINT32_VAL(SLEEP, WAKE_HOUR);
+ tmp = strchr(wt, ':');
+ if (tmp) {
+ *tmp = '\0';
+ tmp++;
+ ret = para_atoi32(tmp, &min);
+ if (ret < 0) {
+ free(wt);
+ goto close_mixer;
+ }
+ }
+ ret = para_atoi32(wt, &hour);
+ free(wt);
+ if (ret < 0)
+ goto close_mixer;
+ if (wake_time[0] == '+') { /* relative */
+ t1 += hour * 60 * 60 + min * 60;
+ tm = localtime(&t1);
+ } else {
tm = localtime(&t1);
if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
t1 += 86400; /* wake time is tomorrow */
tm->tm_hour = hour;
tm->tm_min = min;
tm->tm_sec = 0;
- } else {
- t1 += 9 * 60 * 60; /* nine hours from now */
- PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
- tm = localtime(&t1);
}
wake_time_epoch = mktime(tm);
PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);
client_cmd("stop");
sleep(1);
- if (fot) {
+ if (fot && fo_mood && *fo_mood) {
ret = set_initial_volume(m, h);
if (ret < 0)
- return ret;
+ goto close_mixer;
change_afs_mode(fo_mood);
client_cmd("play");
ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
if (ret < 0)
- return ret;
+ goto close_mixer;
ret = fade(m, h, fov, fot);
if (ret < 0)
- return ret;
+ goto close_mixer;
} else {
ret = m->set(h, fov);
if (ret < 0)
- return ret;
+ goto close_mixer;
}
- if (OPT_GIVEN(SLEEP, SLEEP_MOOD)) {
+ if (sleep_mood && *sleep_mood) {
change_afs_mode(sleep_mood);
- client_cmd("play");
- } else
+ if (!fot || !fo_mood) /* currently stopped */
+ client_cmd("play");
+ } else if (fot && fo_mood && *fo_mood) /* currently playing */
client_cmd("stop");
- if (!fit)
+ m->close(&h);
+ if (!fit || !fi_mood || !*fi_mood) /* nothing to do */
return 1;
- change_afs_mode(fi_mood);
for (;;) {
time(&t1);
if (wake_time_epoch <= t1 + fit)
(delay % 3600) / 60);
sleep(delay);
}
- client_cmd("play");
+ change_afs_mode(fi_mood);
+ if (sleep_mood && *sleep_mood) /* currently playing */
+ client_cmd("next");
+ else /* currently stopped */
+ client_cmd("play");
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ return ret;
ret = fade(m, h, fiv, fit);
- PARA_INFO_LOG("fade complete, returning\n");
+close_mixer:
+ m->close(&h);
return ret;
}
EXPORT_CMD(sleep);
-static int com_snooze(struct mixer *m, struct mixer_handle *h)
+static int com_snooze(const struct mixer *m)
{
int ret, val;
+ struct mixer_handle *h;
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ return ret;
+ ret = 1;
if (OPT_UINT32_VAL(SNOOZE, SO_TIME) == 0)
- return 1;
+ goto close_mixer;
ret = m->get(h);
if (ret < 0)
- return ret;
+ goto close_mixer;
val = ret;
if (val < OPT_UINT32_VAL(SNOOZE, SO_VOL))
ret = m->set(h, OPT_UINT32_VAL(SNOOZE, SO_VOL));
ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SO_VOL),
OPT_UINT32_VAL(SNOOZE, SO_TIME));
if (ret < 0)
- return ret;
+ goto close_mixer;
client_cmd("pause");
PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
+ m->close(&h);
sleep(OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
client_cmd("play");
- return fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ goto close_mixer;
+ ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
OPT_UINT32_VAL(SNOOZE, SI_TIME));
+close_mixer:
+ m->close(&h);
+ return ret;
}
EXPORT_CMD(snooze);
-static void init_mixers(void)
+static int com_set(const struct mixer *m)
{
- int i;
-
- 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);
- }
-}
+ struct mixer_handle *h;
+ int ret;
-static int com_set(struct mixer *m, struct mixer_handle *h)
-{
- return m->set(h, OPT_UINT32_VAL(SET, VAL));
+ ret = open_mixer_and_set_channel(m, &h);
+ if (ret < 0)
+ return ret;
+ ret = m->set(h, OPT_UINT32_VAL(SET, VAL));
+ m->close(&h);
+ return ret;
}
EXPORT_CMD(set);
-static struct mixer *get_mixer_or_die(void)
+static const struct mixer *get_mixer_or_die(void)
{
int i;
if (!OPT_GIVEN(PARA_MIXER, MIXER_API))
- i = DEFAULT_MIXER;
+ i = 0; /* default: use first mixer */
else
FOR_EACH_MIXER(i)
- if (!strcmp(mixer_name[i],
+ if (!strcmp(mixers[i]->name,
OPT_STRING_VAL(PARA_MIXER, MIXER_API)))
break;
if (i < NUM_SUPPORTED_MIXERS) {
- PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
- return supported_mixer + i;
+ PARA_NOTICE_LOG("using %s mixer API\n", mixers[i]->name);
+ return mixers[i];
}
printf("available mixer APIs: ");
- FOR_EACH_MIXER(i) {
- int d = (i == DEFAULT_MIXER);
- printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
- }
+ FOR_EACH_MIXER(i)
+ printf("%s%s%s ", i == 0? "[" : "", mixers[i]->name,
+ i == 0? "]" : "");
printf("\n");
exit(EXIT_FAILURE);
}
}
}
-
-static int com_help(__a_unused struct mixer *m, __a_unused struct mixer_handle *h)
+static int com_help(__a_unused const struct mixer *m)
{
const struct lls_command *cmd;
const struct lls_opt_result *r_l = OPT_RESULT(HELP, LONG);
static int parse_and_merge_config_file(const struct lls_command *cmd)
{
int ret;
- int cf_argc;
- char **cf_argv;
- char *cf, *errctx = NULL;
- struct lls_parse_result **lprp, *cf_lpr, *merged_lpr;
- void *map;
- size_t sz;
- const char *subcmd_name;
-
- if (cmd == lls_cmd(0, mixer_suite)) {
- lprp = &lpr;
- subcmd_name = NULL;
- } else {
- lprp = &sub_lpr;
- subcmd_name = lls_command_name(cmd);
- }
- if (OPT_GIVEN(PARA_MIXER, CONFIG_FILE))
- cf = para_strdup(OPT_STRING_VAL(PARA_MIXER, CONFIG_FILE));
- else {
- char *home = para_homedir();
- cf = make_message("%s/.paraslash/mixer.conf", home);
- free(home);
- }
- 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(PARA_MIXER, CONFIG_FILE))
- goto free_cf;
- } else {
- ret = lls(lls_convert_config(map, sz, subcmd_name, &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, &cf_lpr, &errctx));
- lls_free_argv(cf_argv);
- if (ret < 0)
- goto free_cf;
- ret = lls(lls_merge(*lprp, cf_lpr, cmd, &merged_lpr, &errctx));
- lls_free_parse_result(cf_lpr, cmd);
- if (ret < 0)
- goto free_cf;
- lls_free_parse_result(*lprp, cmd);
- *lprp = merged_lpr;
- loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
- }
- ret = 1;
-free_cf:
- free(cf);
- if (errctx)
- PARA_ERROR_LOG("%s\n", errctx);
- free(errctx);
- return ret;
+ struct lls_parse_result **lprp = (cmd == lls_cmd(0, mixer_suite))?
+ &lpr : &sub_lpr;
+
+ ret = lsu_merge_config_file_options(OPT_STRING_VAL(PARA_MIXER,
+ CONFIG_FILE), "mixer.conf", lprp, cmd, mixer_suite,
+ 0 /* flags */);
+ if (ret < 0)
+ return ret;
+ loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
+ return 1;
}
/**
int ret;
char *errctx;
const char *subcmd;
- struct mixer *m;
- struct mixer_handle *h;
+ const struct mixer *m;
unsigned n;
ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
if (ret < 0)
goto free_lpr;
ret = parse_and_merge_config_file(cmd);
- if (ret < 0)
- goto free_lpr;
- init_mixers();
- m = get_mixer_or_die();
- ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), &h);
if (ret < 0)
goto free_sub_lpr;
- ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, 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 close_mixer;
- ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m ,h);
-close_mixer:
- m->close(&h);
+ m = get_mixer_or_die();
+ ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m);
free_sub_lpr:
lls_free_parse_result(sub_lpr, cmd);
free_lpr: