2 * Copyright (C) 1998-2010 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 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;
129 conf.mixer_channel_arg = val;
133 * Open mixer, get volume, fade to new_vol in secs seconds and
136 static int fade(int new_vol, int fade_time)
138 int vol, mixer_fd, diff, incr, ret;
141 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
146 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
151 ret = get_mixer_channel(mixer_fd);
155 diff = new_vol - vol;
160 incr = diff > 0? 1: -1;
161 diff = diff > 0? diff: -diff;
162 tmp = secs * 1000 / diff;
164 while ((new_vol - vol) * incr > 0) {
165 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
166 ts.tv_sec = tmp / 1000; /* really nec ?*/
167 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
169 ret = set_mixer_channel(mixer_fd, vol);
172 //printf("vol = %i\n", vol);
173 nanosleep(&ts, NULL);
180 static void client_cmd(const char *cmd)
182 int ret, fds[3] = {0, 0, 0};
184 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
186 PARA_INFO_LOG("%s\n", cmdline);
187 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
190 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
195 while (ret != -1 && errno != ECHILD);
198 static void change_afs_mode_and_play(char *afs_mode)
205 cmd = make_message("select %s\n", afs_mode);
214 static int sweet_dreams(void)
216 time_t t1, wake_time_epoch;
219 int ret, min = conf.wake_min_arg;
220 char *fo_mood = conf.fo_mood_arg;
221 char *fi_mood = conf.fi_mood_arg;
222 char *sleep_mood = conf.sleep_mood_arg;
223 int fit = conf.fi_time_arg;
224 int fot = conf.fo_time_arg;
225 int fiv = conf.fi_vol_arg;
226 int fov = conf.fo_vol_arg;
227 int iv = conf.ivol_arg;
229 /* calculate wake time */
231 if (conf.wake_hour_given) {
232 int hour = conf.wake_hour_arg;
234 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
235 t1 += 86400; /* wake time is tomorrow */
242 t1 += 9 * 60 * 60; /* nine hours from now */
243 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
246 wake_time_epoch = mktime(tm);
247 PARA_INFO_LOG("waketime: %s", asctime(tm));
251 PARA_INFO_LOG("initial volume: %d\n", iv);
252 ret = open_and_set_mixer_channel(iv);
255 change_afs_mode_and_play(fo_mood);
256 ret = fade(fov, fot);
260 ret = open_and_set_mixer_channel(fov);
264 if (conf.sleep_mood_given)
265 change_afs_mode_and_play(sleep_mood);
272 if (wake_time_epoch <= t1 + fit)
274 delay = wake_time_epoch - t1 - fit;
275 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
277 (delay % 3600) / 60);
280 change_afs_mode_and_play(fi_mood);
281 ret = fade(fiv, fit);
282 PARA_INFO_LOG("fade complete, returning\n");
286 static int snooze(void)
291 if (conf.so_time_arg <= 0)
293 sleep_time = conf.so_time_arg;
294 if (open_and_get_mixer_channel() < conf.so_vol_arg)
295 ret = open_and_set_mixer_channel(conf.so_vol_arg);
297 ret = fade(conf.so_vol_arg, conf.so_time_arg);
301 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
304 return fade(conf.si_vol_arg, conf.si_time_arg);
307 static int configfile_exists(void)
309 static char *config_file;
311 if (!conf.config_file_given) {
312 char *home = para_homedir();
314 config_file = make_message("%s/.paraslash/fade.conf", home);
316 conf.config_file_arg = config_file;
318 return file_exists(conf.config_file_arg);
321 int main(int argc, char *argv[])
325 if (fade_cmdline_parser(argc, argv, &conf))
327 HANDLE_VERSION_FLAG("fade", conf);
328 ret = configfile_exists();
329 if (!ret && conf.config_file_given) {
330 PARA_EMERG_LOG("can not read config file %s\n",
331 conf.config_file_arg);
335 struct fade_cmdline_parser_params params = {
339 .check_ambiguity = 0,
342 fade_cmdline_parser_config_file(conf.config_file_arg,
345 fixup_mixer_channel_arg();
346 switch (conf.mode_arg) {
348 ret = sweet_dreams();
351 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
353 case mode_arg_snooze:
358 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
359 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;