2 * Copyright (C) 1998-2009 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>
12 #include <sys/ioctl.h>
14 #include <stdlib.h> /* EXIT_SUCCESS */
19 #include <sys/soundcard.h>
21 #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 return para_open(conf.mixer_device_arg, O_RDWR, 42);
53 * Get channel volume via mixer_fd.
55 static int get_mixer_channel(int mixer_fd)
59 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
60 return -ERRNO_TO_PARA_ERROR(errno);
61 /* take the mean value of left and right volume */
62 return (volume % 256 + (volume >> 8)) / 2;
66 * Open mixer, get volume and close mixer.
68 static int open_and_get_mixer_channel(void)
73 mixer_fd = open_mixer();
76 volume = get_mixer_channel(mixer_fd);
82 * Set channel volume via mixer_fd.
84 static int set_mixer_channel(int mixer_fd, int volume)
86 int tmp = (volume << 8) + volume;
88 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
89 return -ERRNO_TO_PARA_ERROR(errno);
94 * Open mixer, set volume and close mixer.
96 static int open_and_set_mixer_channel(int volume)
98 int mixer_fd, ret = open_mixer();
103 ret = set_mixer_channel(mixer_fd, volume);
108 static void fixup_mixer_channel_arg(void)
112 switch (conf.mixer_channel_arg) {
113 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
114 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
115 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
116 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
117 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
118 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
119 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
120 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
121 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
122 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
123 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
124 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
125 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
126 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
128 conf.mixer_channel_arg = val;
132 * Open mixer, get volume, fade to new_vol in secs seconds and
135 static int fade(int new_vol, int fade_time)
137 int vol, mixer_fd, diff, incr, ret;
140 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
145 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
150 ret = get_mixer_channel(mixer_fd);
154 diff = new_vol - vol;
159 incr = diff > 0? 1: -1;
160 diff = diff > 0? diff: -diff;
161 tmp = secs * 1000 / diff;
163 while ((new_vol - vol) * incr > 0) {
164 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
165 ts.tv_sec = tmp / 1000; /* really nec ?*/
166 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
168 ret = set_mixer_channel(mixer_fd, vol);
171 //printf("vol = %i\n", vol);
172 nanosleep(&ts, NULL);
179 static void client_cmd(const char *cmd)
181 int ret, fds[3] = {0, 0, 0};
183 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
185 PARA_INFO_LOG("%s\n", cmdline);
186 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
189 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
194 while (ret != -1 && errno != ECHILD);
197 static void change_afs_mode_and_play(char *afs_mode)
204 cmd = make_message("select %s\n", afs_mode);
213 static int sweet_dreams(void)
215 time_t t1, wake_time_epoch;
218 int ret, min = conf.wake_min_arg;
219 char *fo_mood = conf.fo_mood_arg;
220 char *fi_mood = conf.fi_mood_arg;
221 char *sleep_mood = conf.sleep_mood_arg;
222 int fit = conf.fi_time_arg;
223 int fot = conf.fo_time_arg;
224 int fiv = conf.fi_vol_arg;
225 int fov = conf.fo_vol_arg;
226 int iv = conf.ivol_arg;
228 /* calculate wake time */
230 if (conf.wake_hour_given) {
231 int hour = conf.wake_hour_arg;
233 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
234 t1 += 86400; /* wake time is tomorrow */
241 t1 += 9 * 60 * 60; /* nine hours from now */
242 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
245 wake_time_epoch = mktime(tm);
246 PARA_INFO_LOG("waketime: %s", asctime(tm));
250 PARA_INFO_LOG("initial volume: %d\n", iv);
251 ret = open_and_set_mixer_channel(iv);
254 change_afs_mode_and_play(fo_mood);
255 ret = fade(fov, fot);
259 ret = open_and_set_mixer_channel(fov);
263 if (conf.sleep_mood_given)
264 change_afs_mode_and_play(sleep_mood);
271 if (wake_time_epoch <= t1 + fit)
273 delay = wake_time_epoch - t1 - fit;
274 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
276 (delay % 3600) / 60);
279 change_afs_mode_and_play(fi_mood);
280 ret = fade(fiv, fit);
281 PARA_INFO_LOG("fade complete, returning\n");
285 static int snooze(void)
290 if (conf.so_time_arg <= 0)
292 sleep_time = conf.so_time_arg;
293 if (open_and_get_mixer_channel() < conf.so_vol_arg)
294 ret = open_and_set_mixer_channel(conf.so_vol_arg);
296 ret = fade(conf.so_vol_arg, conf.so_time_arg);
300 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
303 return fade(conf.si_vol_arg, conf.si_time_arg);
306 static int configfile_exists(void)
308 static char *config_file;
310 if (!conf.config_file_given) {
311 char *home = para_homedir();
313 config_file = make_message("%s/.paraslash/fade.conf", home);
315 conf.config_file_arg = config_file;
317 return file_exists(conf.config_file_arg);
320 int main(int argc, char *argv[])
324 if (fade_cmdline_parser(argc, argv, &conf))
326 HANDLE_VERSION_FLAG("fade", conf);
327 ret = configfile_exists();
328 if (!ret && conf.config_file_given) {
329 PARA_EMERG_LOG("can not read config file %s\n",
330 conf.config_file_arg);
334 struct fade_cmdline_parser_params params = {
338 .check_ambiguity = 0,
341 fade_cmdline_parser_config_file(conf.config_file_arg,
344 fixup_mixer_channel_arg();
345 switch (conf.mode_arg) {
347 ret = sweet_dreams();
350 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
352 case mode_arg_snooze:
357 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
358 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;