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};
26 #define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
28 __printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
36 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
41 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
43 /* Fade to new volume in fade_time seconds. */
44 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
46 int vol, diff, incr, ret;
49 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
54 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
64 incr = diff > 0? 1: -1;
65 diff = diff > 0? diff: -diff;
66 tmp = secs * 1000 / diff;
68 while ((new_vol - vol) * incr > 0) {
69 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
70 ts.tv_sec = tmp / 1000; /* really nec ?*/
71 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
76 //printf("vol = %i\n", vol);
83 static void client_cmd(const char *cmd)
85 int ret, status, fds[3] = {0, 0, 0};
87 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
89 PARA_INFO_LOG("%s\n", cmdline);
90 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
93 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
97 pid = waitpid(pid, &status, 0);
98 while (pid == -1 && errno == EINTR);
100 PARA_ERROR_LOG("%s\n", strerror(errno));
103 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
107 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
111 static void change_afs_mode_and_play(char *afs_mode)
118 cmd = make_message("select %s", afs_mode);
124 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
126 time_t t1, wake_time_epoch;
129 int ret, min = conf.wake_min_arg;
130 char *fo_mood = conf.fo_mood_arg;
131 char *fi_mood = conf.fi_mood_arg;
132 char *sleep_mood = conf.sleep_mood_arg;
133 int fit = conf.fi_time_arg;
134 int fot = conf.fo_time_arg;
135 int fiv = conf.fi_vol_arg;
136 int fov = conf.fo_vol_arg;
137 int iv = conf.ivol_arg;
139 /* calculate wake time */
141 if (conf.wake_hour_given) {
142 int hour = conf.wake_hour_arg;
144 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
145 t1 += 86400; /* wake time is tomorrow */
152 t1 += 9 * 60 * 60; /* nine hours from now */
153 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
156 wake_time_epoch = mktime(tm);
157 PARA_INFO_LOG("waketime: %s", asctime(tm));
161 PARA_INFO_LOG("initial volume: %d\n", iv);
165 change_afs_mode_and_play(fo_mood);
166 ret = fade(m, h, fov, fot);
170 ret = m->set(h, fov);
174 if (conf.sleep_mood_given)
175 change_afs_mode_and_play(sleep_mood);
182 if (wake_time_epoch <= t1 + fit)
184 delay = wake_time_epoch - t1 - fit;
185 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
187 (delay % 3600) / 60);
190 change_afs_mode_and_play(fi_mood);
191 ret = fade(m, h, fiv, fit);
192 PARA_INFO_LOG("fade complete, returning\n");
196 static int snooze(struct mixer *m, struct mixer_handle *h)
200 if (conf.so_time_arg <= 0)
206 if (val < conf.so_vol_arg)
207 ret = m->set(h, conf.so_vol_arg);
209 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
213 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
214 sleep(conf.snooze_time_arg);
216 return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
219 static int configfile_exists(void)
221 static char *config_file;
223 if (!conf.config_file_given) {
224 char *home = para_homedir();
226 config_file = make_message("%s/.paraslash/fade.conf", home);
228 conf.config_file_arg = config_file;
230 return file_exists(conf.config_file_arg);
233 static void init_mixers(void)
238 struct mixer *m = &supported_mixer[i];
239 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
245 static int set_channel(struct mixer *m, struct mixer_handle *h)
250 ret = m->set_channel(h, conf.mixer_channel_arg);
252 PARA_NOTICE_LOG("using %s mixer channel\n",
253 conf.mixer_channel_arg? conf.mixer_channel_arg
257 channels = m->get_channels(h);
258 printf("Available channels: %s\n", channels);
263 static struct mixer *get_mixer_or_die(void)
267 if (!conf.mixer_api_given)
271 if (!strcmp(mixer_name[i], conf.mixer_api_arg))
273 if (i < NUM_SUPPORTED_MIXERS) {
274 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
275 return supported_mixer + i;
277 printf("available mixer APIs: ");
279 int d = (i == DEFAULT_MIXER);
280 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
286 int main(int argc, char *argv[])
290 struct mixer_handle *h = NULL;
292 if (fade_cmdline_parser(argc, argv, &conf))
294 HANDLE_VERSION_FLAG("fade", conf);
295 ret = configfile_exists();
296 if (!ret && conf.config_file_given) {
297 PARA_EMERG_LOG("can not read config file %s\n",
298 conf.config_file_arg);
302 struct fade_cmdline_parser_params params = {
306 .check_ambiguity = 0,
309 fade_cmdline_parser_config_file(conf.config_file_arg,
313 m = get_mixer_or_die();
314 ret = m->open(conf.mixer_device_arg, &h);
317 ret = set_channel(m, h);
320 switch (conf.mode_arg) {
322 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
324 case mode_arg_snooze:
327 default: /* sleep mode */
328 ret = sweet_dreams(m, h);
334 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
335 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;