/*
- * Copyright (C) 1998-2005 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file fade.c a volume fader and alarm clock */
+/** \file fade.c A volume fader and alarm clock for OSS. */
+
+#include <regex.h>
#include "fade.cmdline.h"
#include "para.h"
#include "fd.h"
-
-#include <stropts.h>
-#include <ctype.h>
-#include <curses.h>
-#include <stdlib.h> /* EXIT_SUCCESS */
-#include <unistd.h>
-#include <signal.h>
-#include <string.h>
-#include <limits.h>
-#include <linux/soundcard.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;
-struct gengetopt_args_info args_info;
+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)++)
-void para_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;
-/*
- * open mixer device
- */
-static int open_mixer(void)
-{
- int mixer_fd;
-
- if ((mixer_fd = open(args_info.mixer_device_arg, O_RDWR, 0)) < 0)
- return -1;
- return mixer_fd;
-}
-
-/*
- * 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 -1;
- /* take the mean value of left and right volume */
- return (volume % 256 + (volume >> 8)) / 2;
-}
-
-/*
- * open mixer, get volume and close mixer
- */
-static int get_vol(void)
+static int set_channel(struct mixer *m, struct mixer_handle *h, const char *channel)
{
- int mixer_fd;
- int volume;
-
- mixer_fd = open_mixer();
- if (mixer_fd < 0)
- return -1;
- 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;
- tmp = (volume << 8) + volume;
- if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
- return 0;
- return 1;
+ PARA_NOTICE_LOG("using %s mixer channel\n", channel?
+ channel : "default");
+ return m->set_channel(h, channel);
}
-/*
- * 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;
- int ret;
-
- mixer_fd = open_mixer();
- ret = 0;
- if (mixer_fd < 0)
- goto out;
- if (!do_set_vol(mixer_fd, volume))
- goto out;
- ret = 1;
- close(mixer_fd);
-out:
- return ret;
-}
-
-/*
- * Open mixer, get volume, fade to new_vol in secs seconds and
- * close mixer
- */
-static int fade(int new_vol, unsigned int secs)
-{
- int vol, mixer_fd = -1, diff, incr, ret;
+ int vol, diff, incr, ret;
+ unsigned secs;
struct timespec ts;
unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
- PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
- ret = 0;
- if (!secs)
- goto out;
- mixer_fd = open_mixer();
- if (mixer_fd < 0)
- goto out;
- vol = do_get_vol(mixer_fd);
- if (vol < 0)
+ if (fade_time <= 0)
+ return m->set(h, new_vol);
+ secs = fade_time;
+ ret = m->get(h);
+ if (ret < 0)
goto out;
+ vol = ret;
+ PARA_NOTICE_LOG("fading %s from %d to %d in %d seconds\n",
+ conf.mixer_channel_arg, vol, new_vol, secs);
diff = new_vol - vol;
if (!diff) {
sleep(secs);
- ret = 1;
goto out;
}
incr = diff > 0? 1: -1;
ts.tv_sec = tmp / 1000; /* really nec ?*/
//printf("ts.tv_sec: %i\n", ts.tv_nsec);
vol += incr;
- if (!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:
- if (mixer_fd >= 0)
- close(mixer_fd);
- return 1;
+ return ret;
}
-static int client_cmd(const char *cmd,...)
+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", cmdline);
+
+ PARA_NOTICE_LOG("%s\n", cmdline);
ret = para_exec_cmdline_pid(&pid, cmdline, fds);
free(cmdline);
- return ret;
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ goto fail;
+ }
+ do
+ 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);
}
-/*
- * sleep
- */
-static void sweet_dreams(void)
+static void change_afs_mode(char *afs_mode)
+{
+ 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)
+{
+ 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)
{
time_t t1, wake_time_epoch;
unsigned int delay;
struct tm *tm;
- int hour = args_info.wake_hour_arg;
- int min = args_info.wake_min_arg;
- char *fa_stream = args_info.fa_stream_arg;
- char *wake_stream = args_info.wake_stream_arg;
- //char *current_stream = stat_items[STREAM].content;
- int wf = args_info.wake_fade_arg;
- int sf = args_info.fa_fade_arg;
- int wv = args_info.wake_vol_arg;
- int sv = args_info.fa_vol_arg;
- int iv = args_info.sleep_ivol_arg;
- char *cmd, *sleep_stream = args_info.sleep_stream_given?
- args_info.sleep_stream_arg : NULL;
-
- if (sf) {
- PARA_INFO_LOG("initial volume: %d\n", iv);
- set_vol(iv);
- cmd = make_message("csp %s\n", fa_stream);
- client_cmd(cmd);
- free(cmd);
- fade(sv, sf);
- }
- if (sleep_stream) {
- cmd = make_message("csp %s\n", sleep_stream);
- client_cmd(cmd);
- free(cmd);
- } else
- client_cmd("stop");
- if (!wf)
- return;
+ 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;
+
/* calculate wake time */
time(&t1);
- tm = localtime(&t1);
- if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
- /* wake time is tomorrow */
- t1 += 86400;
+ if (conf.wake_hour_given) {
+ int hour = conf.wake_hour_arg;
+ 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);
+ }
+ 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);
- t1 -= 86400;
}
- tm->tm_hour = hour;
- tm->tm_min = min;
- tm->tm_sec = 0;
wake_time_epoch = mktime(tm);
- PARA_INFO_LOG("waketime: %s", asctime(tm));
- while (wake_time_epoch > t1 + wf) {
- delay = wake_time_epoch - t1 - wf;
+ PARA_INFO_LOG("waketime: %u:%02u", tm->tm_hour, tm->tm_min);
+ client_cmd("stop");
+ sleep(1);
+ if (fot) {
+ 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;
+ ret = fade(m, h, fov, fot);
+ if (ret < 0)
+ return ret;
+ } else {
+ ret = m->set(h, fov);
+ if (ret < 0)
+ return ret;
+ }
+ 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)
+ break;
+ delay = wake_time_epoch - t1 - fit;
PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
- delay, delay / 3600,
+ delay, delay / 3600,
(delay % 3600) / 60);
sleep(delay);
- time(&t1);
}
- cmd = make_message("csp %s\n", wake_stream);
- client_cmd(cmd);
- free(cmd);
- fade(wv, wf);
- PARA_INFO_LOG("%s", "fade complete, returning\n");
+ client_cmd("play");
+ ret = fade(m, h, fiv, fit);
+ PARA_INFO_LOG("fade complete, returning\n");
+ return ret;
}
-static void snooze(void)
+static int snooze(struct mixer *m, struct mixer_handle *h)
{
- if (get_vol() < args_info.snooze_out_vol_arg)
- set_vol(args_info.snooze_out_vol_arg);
+ int ret, val;
+
+ if (conf.so_time_arg <= 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);
else
- fade(args_info.snooze_out_vol_arg, args_info.snooze_out_fade_arg);
+ ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
+ if (ret < 0)
+ return ret;
client_cmd("pause");
- PARA_NOTICE_LOG("%d seconds snooze time...\n", args_info.snooze_time_arg);
- sleep(args_info.snooze_time_arg);
+ PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
+ sleep(conf.snooze_time_arg);
client_cmd("play");
- fade(args_info.snooze_in_vol_arg, args_info.snooze_in_fade_arg);
+ return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
}
static int configfile_exists(void)
{
- static char *config_file;
+ static char *config_file;
- if (!args_info.config_file_given) {
+ if (!conf.config_file_given) {
char *home = para_homedir();
- const char *conf = ".paraslash/fade.conf";
free(config_file);
- config_file = make_message("%s/%s", home, conf);
+ config_file = make_message("%s/.paraslash/fade.conf", home);
free(home);
- args_info.config_file_arg = config_file;
- }
- return file_exists(args_info.config_file_arg);
+ conf.config_file_arg = config_file;
+ }
+ return file_exists(conf.config_file_arg);
}
+static void init_mixers(void)
+{
+ 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);
+ }
+}
+
+static int set_val(struct mixer *m, struct mixer_handle *h)
+{
+ return m->set(h, conf.val_arg);
+}
+
+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;
+ }
+ 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);
+}
int main(int argc, char *argv[])
{
int ret;
+ struct mixer *m;
+ struct mixer_handle *h = NULL;
- if (cmdline_parser(argc, argv, &args_info))
- exit(EXIT_FAILURE);
+ 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 && args_info.config_file_given) {
+ if (!ret && conf.config_file_given) {
PARA_EMERG_LOG("can not read config file %s\n",
- args_info.config_file_arg);
+ conf.config_file_arg);
exit(EXIT_FAILURE);
}
- if (ret)
- cmdline_parser_configfile(args_info.config_file_arg,
- &args_info, 0, 0, 0);
- if ((ret = open_mixer()) < 0) {
- PARA_EMERG_LOG("can not open mixer device %s.",
- args_info.mixer_device_arg);
- exit(EXIT_FAILURE);
- } else
- close(ret);
- ret = 0;
- setlinebuf(stdout);
- if (!strcmp(args_info.mode_arg, "sleep")) {
- sweet_dreams();
- goto out;
+ 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);
}
- if (!strcmp(args_info.mode_arg, "fade")) {
- fade(args_info.fade_vol_arg, args_info.fade_time_arg);
+ init_mixers();
+ m = get_mixer_or_die();
+ ret = m->open(conf.mixer_device_arg, &h);
+ if (ret < 0)
goto out;
+ 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 (!strcmp(args_info.mode_arg, "snooze")) {
- snooze();
+ 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:
+ ret = snooze(m, h);
+ break;
+ case mode_arg_set:
+ ret = set_val(m, h);
+ break;
+ default: /* sleep mode */
+ ret = sweet_dreams(m, h);
+ break;
}
- ret = -1;
out:
- return ret;
+ m->close(&h);
+ if (ret < 0)
+ PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+ return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
}