8da91134a5208d36700e6aba4aafbc02ebc685ff
2 * Copyright (C) 1998-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file fade.c a volume fader and alarm clock */
21 #include "fade.cmdline.h"
28 #include <stdlib.h> /* EXIT_SUCCESS */
33 #include <linux/soundcard.h>
37 struct fade_args_info conf
;
39 void para_log(__a_unused
int ll
, const char *fmt
,...)
47 printf("%d:%02d:%02d ", tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
56 static int open_mixer(void)
60 if ((mixer_fd
= open(conf
.mixer_device_arg
, O_RDWR
, 0)) < 0)
66 * get volume via mixer_fd
68 static int do_get_vol(int mixer_fd
)
72 if (ioctl(mixer_fd
, MIXER_READ(SOUND_MIXER_VOLUME
), &volume
) < 0)
74 /* take the mean value of left and right volume */
75 return (volume
% 256 + (volume
>> 8)) / 2;
79 * open mixer, get volume and close mixer
81 static int get_vol(void)
86 mixer_fd
= open_mixer();
89 volume
= do_get_vol(mixer_fd
);
95 * set volume via mixer_fd
97 static int do_set_vol(int mixer_fd
, int volume
)
100 tmp
= (volume
<< 8) + volume
;
101 if (ioctl(mixer_fd
, MIXER_WRITE(SOUND_MIXER_VOLUME
), &tmp
) < 0)
107 * open mixer, set volume and close mixer
109 static int set_vol(int volume
)
114 mixer_fd
= open_mixer();
118 if (!do_set_vol(mixer_fd
, volume
))
127 * Open mixer, get volume, fade to new_vol in secs seconds and
130 static void fade(int new_vol
, int fade_time
)
132 int vol
, mixer_fd
= -1, diff
, incr
;
135 unsigned long long tmp
, tmp2
; /* Careful with that axe, Eugene! */
140 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol
, secs
);
141 mixer_fd
= open_mixer();
144 vol
= do_get_vol(mixer_fd
);
147 diff
= new_vol
- vol
;
152 incr
= diff
> 0? 1: -1;
153 diff
= diff
> 0? diff
: -diff
;
154 tmp
= secs
* 1000 / diff
;
156 while ((new_vol
- vol
) * incr
> 0) {
157 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
158 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
159 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
161 if (!do_set_vol(mixer_fd
, vol
))
163 //printf("vol = %i\n", vol);
164 nanosleep(&ts
, NULL
);
171 static int client_cmd(const char *cmd
,...)
173 int ret
, fds
[3] = {0, 0, 0};
175 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
176 PARA_INFO_LOG("%s\n", cmdline
);
177 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
185 static void sweet_dreams(void)
187 time_t t1
, wake_time_epoch
;
190 int min
= conf
.wake_min_arg
;
191 char *fa_stream
= conf
.fa_stream_arg
;
192 char *wake_stream
= conf
.wake_stream_arg
;
193 //char *current_stream = stat_items[STREAM].content;
194 int wf
= conf
.wake_fade_arg
;
195 int sf
= conf
.fa_fade_arg
;
196 int wv
= conf
.wake_vol_arg
;
197 int sv
= conf
.fa_vol_arg
;
198 int iv
= conf
.sleep_ivol_arg
;
199 char *cmd
, *sleep_stream
= conf
.sleep_stream_given
?
200 conf
.sleep_stream_arg
: NULL
;
202 /* calculate wake time */
204 if (conf
.wake_hour_given
) {
205 int hour
= conf
.wake_hour_arg
;
207 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
208 t1
+= 86400; /* wake time is tomorrow */
216 PARA_INFO_LOG("default wake time: %lu\n", t1
);
219 wake_time_epoch
= mktime(tm
);
220 PARA_INFO_LOG("waketime: %s", asctime(tm
));
222 PARA_INFO_LOG("initial volume: %d\n", iv
);
224 cmd
= make_message("csp %s\n", fa_stream
);
230 cmd
= make_message("csp %s\n", sleep_stream
);
239 if (wake_time_epoch
<= t1
+ wf
)
241 delay
= wake_time_epoch
- t1
- wf
;
242 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
244 (delay
% 3600) / 60);
247 cmd
= make_message("csp %s\n", wake_stream
);
251 PARA_INFO_LOG("%s", "fade complete, returning\n");
254 static void snooze(void)
258 if (conf
.snooze_time_arg
<= 0)
260 sleep_time
= conf
.snooze_time_arg
;
261 if (get_vol() < conf
.snooze_out_vol_arg
)
262 set_vol(conf
.snooze_out_vol_arg
);
264 fade(conf
.snooze_out_vol_arg
, conf
.snooze_out_fade_arg
);
266 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf
.snooze_time_arg
);
269 fade(conf
.snooze_in_vol_arg
, conf
.snooze_in_fade_arg
);
272 static int configfile_exists(void)
274 static char *config_file
;
276 if (!conf
.config_file_given
) {
277 char *home
= para_homedir();
279 config_file
= make_message("%s/.paraslash/fade.conf", home
);
281 conf
.config_file_arg
= config_file
;
283 return file_exists(conf
.config_file_arg
);
287 int main(int argc
, char *argv
[])
291 if (fade_cmdline_parser(argc
, argv
, &conf
))
293 HANDLE_VERSION_FLAG("fade", conf
);
294 ret
= configfile_exists();
295 if (!ret
&& conf
.config_file_given
) {
296 PARA_EMERG_LOG("can not read config file %s\n",
297 conf
.config_file_arg
);
301 fade_cmdline_parser_configfile(conf
.config_file_arg
,
303 if ((ret
= open_mixer()) < 0) {
304 PARA_EMERG_LOG("can not open mixer device %s.",
305 conf
.mixer_device_arg
);
311 if (!strcmp(conf
.mode_arg
, "sleep")) {
315 if (!strcmp(conf
.mode_arg
, "fade")) {
316 fade(conf
.fade_vol_arg
, conf
.fade_time_arg
);
319 if (!strcmp(conf
.mode_arg
, "snooze")) {