2 * Copyright (C) 1998-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fade.c A volume fader and alarm clock for OSS. */
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
13 #include <stdlib.h> /* EXIT_SUCCESS */
18 #include <sys/soundcard.h>
20 #include "fade.cmdline.h"
28 static struct fade_args_info conf;
30 __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
38 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
45 * Open the mixer device.
47 static int open_mixer(void)
49 PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
50 return para_open(conf.mixer_device_arg, O_RDWR, 42);
54 * Get channel volume via mixer_fd.
56 static int get_mixer_channel(int mixer_fd)
60 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
61 return -ERRNO_TO_PARA_ERROR(errno);
62 /* take the mean value of left and right volume */
63 return (volume % 256 + (volume >> 8)) / 2;
67 * Open mixer, get volume and close mixer.
69 static int open_and_get_mixer_channel(void)
74 mixer_fd = open_mixer();
77 volume = get_mixer_channel(mixer_fd);
83 * Set channel volume via mixer_fd.
85 static int set_mixer_channel(int mixer_fd, int volume)
87 int tmp = (volume << 8) + volume;
89 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
90 return -ERRNO_TO_PARA_ERROR(errno);
95 * Open mixer, set volume and close mixer.
97 static int open_and_set_mixer_channel(int volume)
99 int mixer_fd, ret = open_mixer();
104 ret = set_mixer_channel(mixer_fd, volume);
109 static void fixup_mixer_channel_arg(void)
111 int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
113 switch (conf.mixer_channel_arg) {
114 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
115 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
116 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
117 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
118 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
119 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
120 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
121 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
122 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
123 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
124 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
125 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
126 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
127 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
130 conf.mixer_channel_arg = val;
134 * Open mixer, get volume, fade to new_vol in secs seconds and
137 static int fade(int new_vol, int fade_time)
139 int vol, mixer_fd, diff, incr, ret;
142 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
147 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
152 ret = get_mixer_channel(mixer_fd);
156 diff = new_vol - vol;
161 incr = diff > 0? 1: -1;
162 diff = diff > 0? diff: -diff;
163 tmp = secs * 1000 / diff;
165 while ((new_vol - vol) * incr > 0) {
166 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
167 ts.tv_sec = tmp / 1000; /* really nec ?*/
168 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
170 ret = set_mixer_channel(mixer_fd, vol);
173 //printf("vol = %i\n", vol);
174 nanosleep(&ts, NULL);
181 static void client_cmd(const char *cmd)
183 int ret, fds[3] = {0, 0, 0};
185 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
187 PARA_INFO_LOG("%s\n", cmdline);
188 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
191 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
196 while (ret != -1 && errno != ECHILD);
199 static void change_afs_mode_and_play(char *afs_mode)
206 cmd = make_message("select %s\n", afs_mode);
215 static int sweet_dreams(void)
217 time_t t1, wake_time_epoch;
220 int ret, min = conf.wake_min_arg;
221 char *fo_mood = conf.fo_mood_arg;
222 char *fi_mood = conf.fi_mood_arg;
223 char *sleep_mood = conf.sleep_mood_arg;
224 int fit = conf.fi_time_arg;
225 int fot = conf.fo_time_arg;
226 int fiv = conf.fi_vol_arg;
227 int fov = conf.fo_vol_arg;
228 int iv = conf.ivol_arg;
230 /* calculate wake time */
232 if (conf.wake_hour_given) {
233 int hour = conf.wake_hour_arg;
235 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
236 t1 += 86400; /* wake time is tomorrow */
243 t1 += 9 * 60 * 60; /* nine hours from now */
244 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
247 wake_time_epoch = mktime(tm);
248 PARA_INFO_LOG("waketime: %s", asctime(tm));
252 PARA_INFO_LOG("initial volume: %d\n", iv);
253 ret = open_and_set_mixer_channel(iv);
256 change_afs_mode_and_play(fo_mood);
257 ret = fade(fov, fot);
261 ret = open_and_set_mixer_channel(fov);
265 if (conf.sleep_mood_given)
266 change_afs_mode_and_play(sleep_mood);
273 if (wake_time_epoch <= t1 + fit)
275 delay = wake_time_epoch - t1 - fit;
276 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
278 (delay % 3600) / 60);
281 change_afs_mode_and_play(fi_mood);
282 ret = fade(fiv, fit);
283 PARA_INFO_LOG("fade complete, returning\n");
287 static int snooze(void)
292 if (conf.so_time_arg <= 0)
294 sleep_time = conf.so_time_arg;
295 if (open_and_get_mixer_channel() < conf.so_vol_arg)
296 ret = open_and_set_mixer_channel(conf.so_vol_arg);
298 ret = fade(conf.so_vol_arg, conf.so_time_arg);
302 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
305 return fade(conf.si_vol_arg, conf.si_time_arg);
308 static int configfile_exists(void)
310 static char *config_file;
312 if (!conf.config_file_given) {
313 char *home = para_homedir();
315 config_file = make_message("%s/.paraslash/fade.conf", home);
317 conf.config_file_arg = config_file;
319 return file_exists(conf.config_file_arg);
322 int main(int argc, char *argv[])
326 if (fade_cmdline_parser(argc, argv, &conf))
328 HANDLE_VERSION_FLAG("fade", conf);
329 ret = configfile_exists();
330 if (!ret && conf.config_file_given) {
331 PARA_EMERG_LOG("can not read config file %s\n",
332 conf.config_file_arg);
336 struct fade_cmdline_parser_params params = {
340 .check_ambiguity = 0,
343 fade_cmdline_parser_config_file(conf.config_file_arg,
346 fixup_mixer_channel_arg();
347 switch (conf.mode_arg) {
349 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
351 case mode_arg_snooze:
354 default: /* sleep mode */
355 ret = sweet_dreams();
359 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
360 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;