2 * Copyright (C) 1998-2013 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};
26 #define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
29 __printf_2_3 void date_log(int ll, const char *fmt, ...)
39 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
44 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
46 /* Fade to new volume in fade_time seconds. */
47 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
49 int vol, diff, incr, ret;
52 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
55 return m->set(h, new_vol);
57 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
67 incr = diff > 0? 1: -1;
68 diff = diff > 0? diff: -diff;
69 tmp = secs * 1000 / diff;
71 while ((new_vol - vol) * incr > 0) {
72 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
73 ts.tv_sec = tmp / 1000; /* really nec ?*/
74 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
79 //printf("vol = %i\n", vol);
86 static void client_cmd(const char *cmd)
88 int ret, status, fds[3] = {0, 0, 0};
90 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
92 PARA_NOTICE_LOG("%s\n", cmdline);
93 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
96 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
100 pid = waitpid(pid, &status, 0);
101 while (pid == -1 && errno == EINTR);
103 PARA_ERROR_LOG("%s\n", strerror(errno));
106 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
110 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
114 static void change_afs_mode_and_play(char *afs_mode)
121 cmd = make_message("select %s", afs_mode);
127 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
129 time_t t1, wake_time_epoch;
132 int ret, min = conf.wake_min_arg;
133 char *fo_mood = conf.fo_mood_arg;
134 char *fi_mood = conf.fi_mood_arg;
135 char *sleep_mood = conf.sleep_mood_arg;
136 int fit = conf.fi_time_arg;
137 int fot = conf.fo_time_arg;
138 int fiv = conf.fi_vol_arg;
139 int fov = conf.fo_vol_arg;
140 int iv = conf.ivol_arg;
142 /* calculate wake time */
144 if (conf.wake_hour_given) {
145 int hour = conf.wake_hour_arg;
147 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
148 t1 += 86400; /* wake time is tomorrow */
155 t1 += 9 * 60 * 60; /* nine hours from now */
156 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
159 wake_time_epoch = mktime(tm);
160 PARA_INFO_LOG("waketime: %s", asctime(tm));
164 PARA_INFO_LOG("initial volume: %d\n", iv);
168 change_afs_mode_and_play(fo_mood);
169 ret = fade(m, h, fov, fot);
173 ret = m->set(h, fov);
177 if (conf.sleep_mood_given)
178 change_afs_mode_and_play(sleep_mood);
185 if (wake_time_epoch <= t1 + fit)
187 delay = wake_time_epoch - t1 - fit;
188 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
190 (delay % 3600) / 60);
193 change_afs_mode_and_play(fi_mood);
194 ret = fade(m, h, fiv, fit);
195 PARA_INFO_LOG("fade complete, returning\n");
199 static int snooze(struct mixer *m, struct mixer_handle *h)
203 if (conf.so_time_arg <= 0)
209 if (val < conf.so_vol_arg)
210 ret = m->set(h, conf.so_vol_arg);
212 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
216 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
217 sleep(conf.snooze_time_arg);
219 return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
222 static int configfile_exists(void)
224 static char *config_file;
226 if (!conf.config_file_given) {
227 char *home = para_homedir();
229 config_file = make_message("%s/.paraslash/fade.conf", home);
231 conf.config_file_arg = config_file;
233 return file_exists(conf.config_file_arg);
236 static void init_mixers(void)
241 struct mixer *m = &supported_mixer[i];
242 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
248 static int set_channel(struct mixer *m, struct mixer_handle *h)
253 ret = m->set_channel(h, conf.mixer_channel_arg);
255 PARA_NOTICE_LOG("using %s mixer channel\n",
256 conf.mixer_channel_arg? conf.mixer_channel_arg
260 channels = m->get_channels(h);
261 printf("Available channels: %s\n", channels);
266 static struct mixer *get_mixer_or_die(void)
270 if (!conf.mixer_api_given)
274 if (!strcmp(mixer_name[i], conf.mixer_api_arg))
276 if (i < NUM_SUPPORTED_MIXERS) {
277 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
278 return supported_mixer + i;
280 printf("available mixer APIs: ");
282 int d = (i == DEFAULT_MIXER);
283 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
289 int main(int argc, char *argv[])
293 struct mixer_handle *h = NULL;
295 if (fade_cmdline_parser(argc, argv, &conf))
297 HANDLE_VERSION_FLAG("fade", conf);
298 ret = configfile_exists();
299 if (!ret && conf.config_file_given) {
300 PARA_EMERG_LOG("can not read config file %s\n",
301 conf.config_file_arg);
305 struct fade_cmdline_parser_params params = {
309 .check_ambiguity = 0,
312 fade_cmdline_parser_config_file(conf.config_file_arg,
315 loglevel = get_loglevel_by_name(conf.loglevel_arg);
317 m = get_mixer_or_die();
318 ret = m->open(conf.mixer_device_arg, &h);
321 ret = set_channel(m, h);
324 switch (conf.mode_arg) {
326 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
328 case mode_arg_snooze:
331 default: /* sleep mode */
332 ret = sweet_dreams(m, h);
338 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
339 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;