2 * Copyright (C) 1998-2009 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 linux. */
12 #include "fade.cmdline.h"
18 #include <stdlib.h> /* EXIT_SUCCESS */
23 #include <linux/soundcard.h>
29 struct fade_args_info conf;
31 __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
39 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
48 static int open_mixer(void)
50 return para_open(conf.mixer_device_arg, O_RDWR, 42);
54 * get volume via mixer_fd
56 static int do_get_vol(int mixer_fd)
60 if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &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 get_vol(void)
74 mixer_fd = open_mixer();
77 volume = do_get_vol(mixer_fd);
83 * set volume via mixer_fd
85 static int do_set_vol(int mixer_fd, int volume)
87 int tmp = (volume << 8) + volume;
89 if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
90 return -ERRNO_TO_PARA_ERROR(errno);
95 * open mixer, set volume and close mixer
97 static int set_vol(int volume)
99 int mixer_fd, ret = open_mixer();
104 ret = do_set_vol(mixer_fd, volume);
110 * Open mixer, get volume, fade to new_vol in secs seconds and
113 static int fade(int new_vol, int fade_time)
115 int vol, mixer_fd, diff, incr, ret;
118 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
123 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
128 ret = do_get_vol(mixer_fd);
132 diff = new_vol - vol;
137 incr = diff > 0? 1: -1;
138 diff = diff > 0? diff: -diff;
139 tmp = secs * 1000 / diff;
141 while ((new_vol - vol) * incr > 0) {
142 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
143 ts.tv_sec = tmp / 1000; /* really nec ?*/
144 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
146 ret = do_set_vol(mixer_fd, vol);
149 //printf("vol = %i\n", vol);
150 nanosleep(&ts, NULL);
157 static void client_cmd(const char *cmd)
159 int ret, fds[3] = {0, 0, 0};
161 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
163 PARA_INFO_LOG("%s\n", cmdline);
164 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
167 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
172 while (ret != -1 && errno != ECHILD);
175 static void change_afs_mode_and_play(char *afs_mode)
182 cmd = make_message("select %s\n", afs_mode);
191 static int sweet_dreams(void)
193 time_t t1, wake_time_epoch;
196 int ret, min = conf.wake_min_arg;
197 char *fa_mode = conf.fa_mode_arg;
198 char *wake_mode = conf.wake_mode_arg;
199 char *sleep_mode = conf.sleep_mode_arg;
200 int wf = conf.wake_fade_arg;
201 int sf = conf.fa_fade_arg;
202 int wv = conf.wake_vol_arg;
203 int sv = conf.fa_vol_arg;
204 int iv = conf.sleep_ivol_arg;
206 /* calculate wake time */
208 if (conf.wake_hour_given) {
209 int hour = conf.wake_hour_arg;
211 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
212 t1 += 86400; /* wake time is tomorrow */
219 t1 += 9 * 60 * 60; /* nine hours from now */
220 PARA_INFO_LOG("default wake time: %lu\n", t1);
223 wake_time_epoch = mktime(tm);
224 PARA_INFO_LOG("waketime: %s", asctime(tm));
228 PARA_INFO_LOG("initial volume: %d\n", iv);
232 change_afs_mode_and_play(fa_mode);
241 if (conf.sleep_mode_given)
242 change_afs_mode_and_play(sleep_mode);
249 if (wake_time_epoch <= t1 + wf)
251 delay = wake_time_epoch - t1 - wf;
252 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
254 (delay % 3600) / 60);
257 change_afs_mode_and_play(wake_mode);
259 PARA_INFO_LOG("fade complete, returning\n");
263 static int snooze(void)
268 if (conf.snooze_time_arg <= 0)
270 sleep_time = conf.snooze_time_arg;
271 if (get_vol() < conf.snooze_out_vol_arg)
272 ret = set_vol(conf.snooze_out_vol_arg);
274 ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
278 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
281 return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
284 static int configfile_exists(void)
286 static char *config_file;
288 if (!conf.config_file_given) {
289 char *home = para_homedir();
291 config_file = make_message("%s/.paraslash/fade.conf", home);
293 conf.config_file_arg = config_file;
295 return file_exists(conf.config_file_arg);
298 int main(int argc, char *argv[])
302 if (fade_cmdline_parser(argc, argv, &conf))
304 HANDLE_VERSION_FLAG("fade", conf);
305 ret = configfile_exists();
306 if (!ret && conf.config_file_given) {
307 PARA_EMERG_LOG("can not read config file %s\n",
308 conf.config_file_arg);
312 struct fade_cmdline_parser_params params = {
318 fade_cmdline_parser_config_file(conf.config_file_arg,
321 if (!strcmp(conf.mode_arg, "sleep")) {
322 ret = sweet_dreams();
325 if (!strcmp(conf.mode_arg, "fade")) {
326 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
329 if (!strcmp(conf.mode_arg, "snooze")) {
333 ret = -E_FADE_SYNTAX;
336 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
337 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;