2 * Copyright (C) 1998-2012 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 <sys/soundcard.h>
15 #include "fade.cmdline.h"
23 static struct fade_args_info conf;
25 __printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
33 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
38 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
41 * Open the mixer device.
43 static int open_mixer(void)
45 PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
46 return para_open(conf.mixer_device_arg, O_RDWR, 42);
50 * Get channel volume via mixer_fd.
52 static int get_mixer_channel(int mixer_fd)
56 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
57 return -ERRNO_TO_PARA_ERROR(errno);
58 /* take the mean value of left and right volume */
59 return (volume % 256 + (volume >> 8)) / 2;
63 * Open mixer, get volume and close mixer.
65 static int open_and_get_mixer_channel(void)
70 mixer_fd = open_mixer();
73 volume = get_mixer_channel(mixer_fd);
79 * Set channel volume via mixer_fd.
81 static int set_mixer_channel(int mixer_fd, int volume)
83 int tmp = (volume << 8) + volume;
85 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
86 return -ERRNO_TO_PARA_ERROR(errno);
91 * Open mixer, set volume and close mixer.
93 static int open_and_set_mixer_channel(int volume)
95 int mixer_fd, ret = open_mixer();
100 ret = set_mixer_channel(mixer_fd, volume);
105 static void fixup_mixer_channel_arg(void)
107 int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
109 switch (conf.mixer_channel_arg) {
110 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
111 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
112 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
113 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
114 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
115 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
116 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
117 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
118 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
119 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
120 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
121 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
122 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
123 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
126 conf.mixer_channel_arg = val;
130 * Open mixer, get volume, fade to new_vol in secs seconds and
133 static int fade(int new_vol, int fade_time)
135 int vol, mixer_fd, diff, incr, ret;
138 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
143 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
148 ret = get_mixer_channel(mixer_fd);
152 diff = new_vol - vol;
157 incr = diff > 0? 1: -1;
158 diff = diff > 0? diff: -diff;
159 tmp = secs * 1000 / diff;
161 while ((new_vol - vol) * incr > 0) {
162 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
163 ts.tv_sec = tmp / 1000; /* really nec ?*/
164 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
166 ret = set_mixer_channel(mixer_fd, vol);
169 //printf("vol = %i\n", vol);
170 nanosleep(&ts, NULL);
177 static void client_cmd(const char *cmd)
179 int ret, status, fds[3] = {0, 0, 0};
181 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
183 PARA_INFO_LOG("%s\n", cmdline);
184 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
187 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
191 pid = waitpid(pid, &status, 0);
192 while (pid == -1 && errno == EINTR);
194 PARA_ERROR_LOG("%s\n", strerror(errno));
197 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
201 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
205 static void change_afs_mode_and_play(char *afs_mode)
212 cmd = make_message("select %s", afs_mode);
221 static int sweet_dreams(void)
223 time_t t1, wake_time_epoch;
226 int ret, min = conf.wake_min_arg;
227 char *fo_mood = conf.fo_mood_arg;
228 char *fi_mood = conf.fi_mood_arg;
229 char *sleep_mood = conf.sleep_mood_arg;
230 int fit = conf.fi_time_arg;
231 int fot = conf.fo_time_arg;
232 int fiv = conf.fi_vol_arg;
233 int fov = conf.fo_vol_arg;
234 int iv = conf.ivol_arg;
236 /* calculate wake time */
238 if (conf.wake_hour_given) {
239 int hour = conf.wake_hour_arg;
241 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
242 t1 += 86400; /* wake time is tomorrow */
249 t1 += 9 * 60 * 60; /* nine hours from now */
250 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
253 wake_time_epoch = mktime(tm);
254 PARA_INFO_LOG("waketime: %s", asctime(tm));
258 PARA_INFO_LOG("initial volume: %d\n", iv);
259 ret = open_and_set_mixer_channel(iv);
262 change_afs_mode_and_play(fo_mood);
263 ret = fade(fov, fot);
267 ret = open_and_set_mixer_channel(fov);
271 if (conf.sleep_mood_given)
272 change_afs_mode_and_play(sleep_mood);
279 if (wake_time_epoch <= t1 + fit)
281 delay = wake_time_epoch - t1 - fit;
282 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
284 (delay % 3600) / 60);
287 change_afs_mode_and_play(fi_mood);
288 ret = fade(fiv, fit);
289 PARA_INFO_LOG("fade complete, returning\n");
293 static int snooze(void)
297 if (conf.so_time_arg <= 0)
299 if (open_and_get_mixer_channel() < conf.so_vol_arg)
300 ret = open_and_set_mixer_channel(conf.so_vol_arg);
302 ret = fade(conf.so_vol_arg, conf.so_time_arg);
306 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
307 sleep(conf.snooze_time_arg);
309 return fade(conf.si_vol_arg, conf.si_time_arg);
312 static int configfile_exists(void)
314 static char *config_file;
316 if (!conf.config_file_given) {
317 char *home = para_homedir();
319 config_file = make_message("%s/.paraslash/fade.conf", home);
321 conf.config_file_arg = config_file;
323 return file_exists(conf.config_file_arg);
326 int main(int argc, char *argv[])
330 if (fade_cmdline_parser(argc, argv, &conf))
332 HANDLE_VERSION_FLAG("fade", conf);
333 ret = configfile_exists();
334 if (!ret && conf.config_file_given) {
335 PARA_EMERG_LOG("can not read config file %s\n",
336 conf.config_file_arg);
340 struct fade_cmdline_parser_params params = {
344 .check_ambiguity = 0,
347 fade_cmdline_parser_config_file(conf.config_file_arg,
350 fixup_mixer_channel_arg();
351 switch (conf.mode_arg) {
353 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
355 case mode_arg_snooze:
358 default: /* sleep mode */
359 ret = sweet_dreams();
363 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
364 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;