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. */
11 #include "fade.cmdline.h"
20 static struct fade_args_info conf;
22 enum mixer_id {MIXER_ENUM};
23 static char *mixer_name[] = {MIXER_NAMES};
25 static struct mixer supported_mixer[] = {MIXER_ARRAY};
27 __printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
35 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
40 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
42 /* Fade to new volume in fade_time seconds. */
43 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
45 int vol, diff, incr, ret;
48 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
53 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
63 incr = diff > 0? 1: -1;
64 diff = diff > 0? diff: -diff;
65 tmp = secs * 1000 / diff;
67 while ((new_vol - vol) * incr > 0) {
68 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
69 ts.tv_sec = tmp / 1000; /* really nec ?*/
70 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
75 //printf("vol = %i\n", vol);
82 static void client_cmd(const char *cmd)
84 int ret, status, fds[3] = {0, 0, 0};
86 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
88 PARA_INFO_LOG("%s\n", cmdline);
89 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
92 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
96 pid = waitpid(pid, &status, 0);
97 while (pid == -1 && errno == EINTR);
99 PARA_ERROR_LOG("%s\n", strerror(errno));
102 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
106 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
110 static void change_afs_mode_and_play(char *afs_mode)
117 cmd = make_message("select %s", afs_mode);
123 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
125 time_t t1, wake_time_epoch;
128 int ret, min = conf.wake_min_arg;
129 char *fo_mood = conf.fo_mood_arg;
130 char *fi_mood = conf.fi_mood_arg;
131 char *sleep_mood = conf.sleep_mood_arg;
132 int fit = conf.fi_time_arg;
133 int fot = conf.fo_time_arg;
134 int fiv = conf.fi_vol_arg;
135 int fov = conf.fo_vol_arg;
136 int iv = conf.ivol_arg;
138 /* calculate wake time */
140 if (conf.wake_hour_given) {
141 int hour = conf.wake_hour_arg;
143 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
144 t1 += 86400; /* wake time is tomorrow */
151 t1 += 9 * 60 * 60; /* nine hours from now */
152 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
155 wake_time_epoch = mktime(tm);
156 PARA_INFO_LOG("waketime: %s", asctime(tm));
160 PARA_INFO_LOG("initial volume: %d\n", iv);
164 change_afs_mode_and_play(fo_mood);
165 ret = fade(m, h, fov, fot);
169 ret = m->set(h, fov);
173 if (conf.sleep_mood_given)
174 change_afs_mode_and_play(sleep_mood);
181 if (wake_time_epoch <= t1 + fit)
183 delay = wake_time_epoch - t1 - fit;
184 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
186 (delay % 3600) / 60);
189 change_afs_mode_and_play(fi_mood);
190 ret = fade(m, h, fiv, fit);
191 PARA_INFO_LOG("fade complete, returning\n");
195 static int snooze(struct mixer *m, struct mixer_handle *h)
199 if (conf.so_time_arg <= 0)
205 if (val < conf.so_vol_arg)
206 ret = m->set(h, conf.so_vol_arg);
208 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
212 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
213 sleep(conf.snooze_time_arg);
215 return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
218 static int configfile_exists(void)
220 static char *config_file;
222 if (!conf.config_file_given) {
223 char *home = para_homedir();
225 config_file = make_message("%s/.paraslash/fade.conf", home);
227 conf.config_file_arg = config_file;
229 return file_exists(conf.config_file_arg);
232 static void init_mixers(void)
236 for (i = 0; i < NUM_SUPPORTED_MIXERS; i++) {
237 struct mixer *m = &supported_mixer[i];
238 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
244 static int set_channel(struct mixer *m, struct mixer_handle *h)
249 ret = m->set_channel(h, conf.mixer_channel_arg);
251 PARA_NOTICE_LOG("using %s mixer channel\n",
252 conf.mixer_channel_arg? conf.mixer_channel_arg
256 channels = m->get_channels(h);
257 printf("Available channels: %s\n", channels);
262 int main(int argc, char *argv[])
266 struct mixer_handle *h = NULL;
268 if (fade_cmdline_parser(argc, argv, &conf))
270 HANDLE_VERSION_FLAG("fade", conf);
271 ret = configfile_exists();
272 if (!ret && conf.config_file_given) {
273 PARA_EMERG_LOG("can not read config file %s\n",
274 conf.config_file_arg);
278 struct fade_cmdline_parser_params params = {
282 .check_ambiguity = 0,
285 fade_cmdline_parser_config_file(conf.config_file_arg,
289 m = &supported_mixer[DEFAULT_MIXER];
290 PARA_INFO_LOG("using %s mixer\n", mixer_name[DEFAULT_MIXER]);
291 ret = m->open(conf.mixer_device_arg, &h);
294 ret = set_channel(m, h);
297 switch (conf.mode_arg) {
299 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
301 case mode_arg_snooze:
304 default: /* sleep mode */
305 ret = sweet_dreams(m, h);
311 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
312 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;