2 * Copyright (C) 1998-2008 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 */
12 #include "fade.cmdline.h"
18 #include <stdlib.h> /* EXIT_SUCCESS */
23 #include <linux/soundcard.h>
27 struct fade_args_info conf;
29 void para_log(__a_unused int ll, const char *fmt,...)
37 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
46 static int open_mixer(void)
48 return open(conf.mixer_device_arg, O_RDWR, 0);
52 * get volume via mixer_fd
54 static int do_get_vol(int mixer_fd)
58 if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
60 /* take the mean value of left and right volume */
61 return (volume % 256 + (volume >> 8)) / 2;
65 * open mixer, get volume and close mixer
67 static int get_vol(void)
72 mixer_fd = open_mixer();
75 volume = do_get_vol(mixer_fd);
81 * set volume via mixer_fd
83 static int do_set_vol(int mixer_fd, int volume)
85 int tmp = (volume << 8) + volume;
87 if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
93 * open mixer, set volume and close mixer
95 static int set_vol(int volume)
97 int mixer_fd, ret = open_mixer();
102 ret = do_set_vol(mixer_fd, volume);
108 * Open mixer, get volume, fade to new_vol in secs seconds and
111 static void fade(int new_vol, int fade_time)
113 int vol, mixer_fd, diff, incr;
116 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
121 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
122 mixer_fd = open_mixer();
125 vol = do_get_vol(mixer_fd);
128 diff = new_vol - vol;
133 incr = diff > 0? 1: -1;
134 diff = diff > 0? diff: -diff;
135 tmp = secs * 1000 / diff;
137 while ((new_vol - vol) * incr > 0) {
138 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
139 ts.tv_sec = tmp / 1000; /* really nec ?*/
140 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
142 if (do_set_vol(mixer_fd, vol) < 0)
144 //printf("vol = %i\n", vol);
145 nanosleep(&ts, NULL);
151 static void client_cmd(const char *cmd)
153 int ret, fds[3] = {0, 0, 0};
155 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
157 PARA_INFO_LOG("%s\n", cmdline);
158 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
164 while (ret != -1 && errno != ECHILD);
167 static void change_afs_mode_and_play(char *afs_mode)
174 cmd = make_message("select %s\n", afs_mode);
183 static void sweet_dreams(void)
185 time_t t1, wake_time_epoch;
188 int min = conf.wake_min_arg;
189 char *fa_mode = conf.fa_mode_arg;
190 char *wake_mode = conf.wake_mode_arg;
191 char *sleep_mode = conf.sleep_mode_arg;
192 int wf = conf.wake_fade_arg;
193 int sf = conf.fa_fade_arg;
194 int wv = conf.wake_vol_arg;
195 int sv = conf.fa_vol_arg;
196 int iv = conf.sleep_ivol_arg;
198 /* calculate wake time */
200 if (conf.wake_hour_given) {
201 int hour = conf.wake_hour_arg;
203 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
204 t1 += 86400; /* wake time is tomorrow */
211 t1 += 9 * 60 * 60; /* nine hours from now */
212 PARA_INFO_LOG("default wake time: %lu\n", t1);
215 wake_time_epoch = mktime(tm);
216 PARA_INFO_LOG("waketime: %s", asctime(tm));
220 PARA_INFO_LOG("initial volume: %d\n", iv);
222 change_afs_mode_and_play(fa_mode);
226 change_afs_mode_and_play(sleep_mode);
231 if (wake_time_epoch <= t1 + wf)
233 delay = wake_time_epoch - t1 - wf;
234 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
236 (delay % 3600) / 60);
239 change_afs_mode_and_play(wake_mode);
241 PARA_INFO_LOG("fade complete, returning\n");
244 static void snooze(void)
248 if (conf.snooze_time_arg <= 0)
250 sleep_time = conf.snooze_time_arg;
251 if (get_vol() < conf.snooze_out_vol_arg)
252 set_vol(conf.snooze_out_vol_arg);
254 fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
256 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
259 fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
262 static int configfile_exists(void)
264 static char *config_file;
266 if (!conf.config_file_given) {
267 char *home = para_homedir();
269 config_file = make_message("%s/.paraslash/fade.conf", home);
271 conf.config_file_arg = config_file;
273 return file_exists(conf.config_file_arg);
277 int main(int argc, char *argv[])
281 if (fade_cmdline_parser(argc, argv, &conf))
283 HANDLE_VERSION_FLAG("fade", conf);
284 ret = configfile_exists();
285 if (!ret && conf.config_file_given) {
286 PARA_EMERG_LOG("can not read config file %s\n",
287 conf.config_file_arg);
291 struct fade_cmdline_parser_params params = {
297 fade_cmdline_parser_config_file(conf.config_file_arg,
302 PARA_EMERG_LOG("can not open mixer device %s.",
303 conf.mixer_device_arg);
308 // setlinebuf(stdout);
309 if (!strcmp(conf.mode_arg, "sleep")) {
313 if (!strcmp(conf.mode_arg, "fade")) {
314 fade(conf.fade_vol_arg, conf.fade_time_arg);
317 if (!strcmp(conf.mode_arg, "snooze")) {
323 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;