/*
- * Copyright (C) 1998-2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1998-2011 Andre Noll <maan@systemlinux.org>
*
* 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 <sys/types.h>
-#include <dirent.h>
-
-#include "fade.cmdline.h"
-#include "para.h"
-#include "fd.h"
-
-#include <stropts.h>
+#include <sys/ioctl.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 <sys/soundcard.h>
+#include "fade.cmdline.h"
+#include "para.h"
+#include "fd.h"
+#include "string.h"
+#include "error.h"
+#include "version.h"
-struct fade_args_info conf;
+INIT_FADE_ERRLISTS;
+static struct fade_args_info conf;
-void para_log(__a_unused int ll, const char *fmt,...)
+__printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
{
va_list argp;
time_t t1;
}
/*
- * open mixer device
+ * Open the mixer device.
*/
static int open_mixer(void)
{
- return open(conf.mixer_device_arg, O_RDWR, 0);
+ PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
+ return para_open(conf.mixer_device_arg, O_RDWR, 42);
}
/*
- * get volume via mixer_fd
+ * Get channel volume via mixer_fd.
*/
-static int do_get_vol(int mixer_fd)
+static int get_mixer_channel(int mixer_fd)
{
int volume;
- if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
- return -1;
+ if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
+ return -ERRNO_TO_PARA_ERROR(errno);
/* take the mean value of left and right volume */
return (volume % 256 + (volume >> 8)) / 2;
}
/*
- * open mixer, get volume and close mixer
+ * Open mixer, get volume and close mixer.
*/
-static int get_vol(void)
+static int open_and_get_mixer_channel(void)
{
int mixer_fd;
int volume;
mixer_fd = open_mixer();
if (mixer_fd < 0)
- return -1;
- volume = do_get_vol(mixer_fd);
+ return mixer_fd;
+ volume = get_mixer_channel(mixer_fd);
close(mixer_fd);
return volume;
}
/*
- * set volume via mixer_fd
+ * Set channel volume via mixer_fd.
*/
-static int do_set_vol(int mixer_fd, int volume)
+static int set_mixer_channel(int mixer_fd, int volume)
{
int tmp = (volume << 8) + volume;
- if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
- return -1;
+ if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
+ return -ERRNO_TO_PARA_ERROR(errno);
return 1;
}
/*
- * open mixer, set volume and close mixer
+ * Open mixer, set volume and close mixer.
*/
-static int set_vol(int volume)
+static int open_and_set_mixer_channel(int volume)
{
int mixer_fd, ret = open_mixer();
if (ret < 0)
return ret;
mixer_fd = ret;
- ret = do_set_vol(mixer_fd, volume);
+ ret = set_mixer_channel(mixer_fd, volume);
close(mixer_fd);
return ret;
}
+static void fixup_mixer_channel_arg(void)
+{
+ int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
+
+ switch (conf.mixer_channel_arg) {
+ case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
+ case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
+ case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
+ case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
+ case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
+ case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
+ case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
+ case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
+ case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
+ case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
+ case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
+ case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
+ case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
+ case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
+ default: break;
+ }
+ conf.mixer_channel_arg = val;
+}
+
/*
* Open mixer, get volume, fade to new_vol in secs seconds and
- * close mixer
+ * close mixer.
*/
-static void fade(int new_vol, int fade_time)
+static int fade(int new_vol, int fade_time)
{
- int vol, mixer_fd, diff, incr;
+ int vol, mixer_fd, diff, incr, ret;
unsigned secs;
struct timespec ts;
unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
if (fade_time <= 0)
- return;
+ return 1;
secs = fade_time;
PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
- mixer_fd = open_mixer();
- if (mixer_fd < 0)
- return;
- vol = do_get_vol(mixer_fd);
- if (vol < 0)
+ ret = open_mixer();
+ if (ret < 0)
+ return ret;
+ mixer_fd = ret;
+ ret = get_mixer_channel(mixer_fd);
+ if (ret < 0)
goto out;
+ vol = ret;
diff = new_vol - vol;
if (!diff) {
sleep(secs);
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) < 0)
+ ret = set_mixer_channel(mixer_fd, 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)
PARA_INFO_LOG("%s\n", cmdline);
ret = para_exec_cmdline_pid(&pid, cmdline, fds);
free(cmdline);
- if (ret < 0)
+ if (ret < 0) {
+ PARA_EMERG_LOG("%s\n", para_strerror(-ret));
exit(EXIT_FAILURE);
+ }
do
ret = wait(NULL);
while (ret != -1 && errno != ECHILD);
/*
* sleep
*/
-static void sweet_dreams(void)
+static int sweet_dreams(void)
{
time_t t1, wake_time_epoch;
unsigned int delay;
struct tm *tm;
- int 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 = 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;
/* calculate wake time */
time(&t1);
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));
client_cmd("stop");
sleep(1);
- if (sf) {
+ if (fot) {
PARA_INFO_LOG("initial volume: %d\n", iv);
- set_vol(iv);
- change_afs_mode_and_play(fa_mode);
- fade(sv, sf);
- } else
- set_vol(sf);
- change_afs_mode_and_play(sleep_mode);
- if (!wf)
- return;
+ ret = open_and_set_mixer_channel(iv);
+ if (ret < 0)
+ return ret;
+ change_afs_mode_and_play(fo_mood);
+ ret = fade(fov, fot);
+ if (ret < 0)
+ return ret;
+ } else {
+ ret = open_and_set_mixer_channel(fov);
+ if (ret < 0)
+ return ret;
+ }
+ if (conf.sleep_mood_given)
+ change_afs_mode_and_play(sleep_mood);
+ else
+ client_cmd("stop");
+ if (!fit)
+ return 1;
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);
- fade(wv, wf);
+ change_afs_mode_and_play(fi_mood);
+ ret = fade(fiv, fit);
PARA_INFO_LOG("fade complete, returning\n");
+ return ret;
}
-static void snooze(void)
+static int snooze(void)
{
+ int ret;
unsigned sleep_time;
- if (conf.snooze_time_arg <= 0)
- return;
- sleep_time = conf.snooze_time_arg;
- if (get_vol() < conf.snooze_out_vol_arg)
- set_vol(conf.snooze_out_vol_arg);
+ if (conf.so_time_arg <= 0)
+ return 1;
+ sleep_time = conf.so_time_arg;
+ if (open_and_get_mixer_channel() < conf.so_vol_arg)
+ ret = open_and_set_mixer_channel(conf.so_vol_arg);
else
- fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
+ ret = fade(conf.so_vol_arg, conf.so_time_arg);
+ if (ret < 0)
+ return ret;
client_cmd("pause");
PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
sleep(sleep_time);
client_cmd("play");
- fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
+ return fade(conf.si_vol_arg, conf.si_time_arg);
}
static int configfile_exists(void)
return file_exists(conf.config_file_arg);
}
-
int main(int argc, char *argv[])
{
int ret;
.override = 0,
.initialize = 0,
.check_required = 0,
- .check_ambiguity = 0
+ .check_ambiguity = 0,
+ .print_errors = 1
};
fade_cmdline_parser_config_file(conf.config_file_arg,
&conf, ¶ms);
}
- ret = open_mixer();
- if (ret < 0) {
- PARA_EMERG_LOG("can not open mixer device %s.",
- conf.mixer_device_arg);
- exit(EXIT_FAILURE);
- }
- close(ret);
- ret = 0;
-// setlinebuf(stdout);
- if (!strcmp(conf.mode_arg, "sleep")) {
- sweet_dreams();
- goto out;
- }
- if (!strcmp(conf.mode_arg, "fade")) {
- fade(conf.fade_vol_arg, conf.fade_time_arg);
- goto out;
- }
- if (!strcmp(conf.mode_arg, "snooze")) {
- snooze();
- goto out;
+ fixup_mixer_channel_arg();
+ switch (conf.mode_arg) {
+ case mode_arg_fade:
+ ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
+ break;
+ case mode_arg_snooze:
+ ret = snooze();
+ break;
+ default: /* sleep mode */
+ ret = sweet_dreams();
+ break;
}
- ret = -1;
-out:
+ if (ret < 0)
+ PARA_EMERG_LOG("%s\n", para_strerror(-ret));
return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
}