2 * Copyright (C) 1998-2005 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"
26 #include <stdlib.h> /* EXIT_SUCCESS */
31 #include <linux/soundcard.h>
35 struct gengetopt_args_info args_info
;
37 void para_log(__unused
int ll
, char *fmt
,...)
45 printf("%d:%02d:%02d ", tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
54 static int open_mixer(void)
58 if ((mixer_fd
= open(args_info
.mixer_device_arg
, O_RDWR
, 0)) < 0)
64 * get volume via mixer_fd
66 static int do_get_vol(int mixer_fd
)
70 if (ioctl(mixer_fd
, MIXER_READ(SOUND_MIXER_VOLUME
), &volume
) < 0)
72 /* take the mean value of left and right volume */
73 return (volume
% 256 + (volume
>> 8)) / 2;
77 * open mixer, get volume and close mixer
79 static int get_vol(void)
84 mixer_fd
= open_mixer();
87 volume
= do_get_vol(mixer_fd
);
93 * set volume via mixer_fd
95 static int do_set_vol(int mixer_fd
, int volume
)
98 tmp
= (volume
<< 8) + volume
;
99 if (ioctl(mixer_fd
, MIXER_WRITE(SOUND_MIXER_VOLUME
), &tmp
) < 0)
105 * open mixer, set volume and close mixer
107 static int set_vol(int volume
)
112 mixer_fd
= open_mixer();
116 if (!do_set_vol(mixer_fd
, volume
))
125 * Open mixer, get volume, fade to new_vol in secs seconds and
128 static int fade(int new_vol
, unsigned int secs
)
130 int vol
, mixer_fd
= -1, diff
, incr
, ret
;
132 unsigned long long tmp
, tmp2
; /* Careful with that axe, Eugene! */
134 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol
, secs
);
138 mixer_fd
= open_mixer();
141 vol
= do_get_vol(mixer_fd
);
144 diff
= new_vol
- vol
;
150 incr
= diff
> 0? 1: -1;
151 diff
= diff
> 0? diff
: -diff
;
152 tmp
= secs
* 1000 / diff
;
154 while ((new_vol
- vol
) * incr
> 0) {
155 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
156 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
157 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
159 if (!do_set_vol(mixer_fd
, vol
))
161 //printf("vol = %i\n", vol);
162 nanosleep(&ts
, NULL
);
170 static int client_cmd(char *cmd
,...)
172 int ret
, fds
[3] = {0, 0, 0};
174 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
175 PARA_INFO_LOG("%s", cmdline
);
176 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
184 static void sweet_dreams(void)
186 time_t t1
, wake_time_epoch
;
189 int hour
= args_info
.wake_hour_arg
;
190 int min
= args_info
.wake_min_arg
;
191 char *fa_stream
= args_info
.fa_stream_arg
;
192 char *wake_stream
= args_info
.wake_stream_arg
;
193 //char *current_stream = stat_items[STREAM].content;
194 int wf
= args_info
.wake_fade_arg
;
195 int sf
= args_info
.fa_fade_arg
;
196 int wv
= args_info
.wake_vol_arg
;
197 int sv
= args_info
.fa_vol_arg
;
198 int iv
= args_info
.sleep_ivol_arg
;
199 char *cmd
, *sleep_stream
= args_info
.sleep_stream_given
?
200 args_info
.sleep_stream_arg
: NULL
;
203 PARA_INFO_LOG("initial volume: %d\n", iv
);
205 cmd
= make_message("csp %s\n", fa_stream
);
211 cmd
= make_message("csp %s\n", sleep_stream
);
218 /* calculate wake time */
221 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
222 /* wake time is tomorrow */
230 wake_time_epoch
= mktime(tm
);
231 PARA_INFO_LOG("waketime: %s", asctime(tm
));
232 while (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);
240 cmd
= make_message("csp %s\n", wake_stream
);
244 PARA_INFO_LOG("%s", "fade complete, returning\n");
247 static void snooze(void)
249 if (get_vol() < args_info
.snooze_out_vol_arg
)
250 set_vol(args_info
.snooze_out_vol_arg
);
252 fade(args_info
.snooze_out_vol_arg
, args_info
.snooze_out_fade_arg
);
254 PARA_NOTICE_LOG("%d seconds snooze time...\n", args_info
.snooze_time_arg
);
255 sleep(args_info
.snooze_time_arg
);
257 fade(args_info
.snooze_in_vol_arg
, args_info
.snooze_in_fade_arg
);
260 static int configfile_exists(void)
262 static char *config_file
;
264 if (!args_info
.config_file_given
) {
265 char *home
= para_homedir();
266 const char *conf
= ".paraslash/fade.conf";
268 config_file
= make_message("%s/%s", home
, conf
);
270 args_info
.config_file_arg
= config_file
;
272 return file_exists(args_info
.config_file_arg
);
276 int main(int argc
, char *argv
[])
280 if (cmdline_parser(argc
, argv
, &args_info
))
282 ret
= configfile_exists();
283 if (!ret
&& args_info
.config_file_given
) {
284 PARA_EMERG_LOG("can not read config file %s\n",
285 args_info
.config_file_arg
);
289 cmdline_parser_configfile(args_info
.config_file_arg
,
290 &args_info
, 0, 0, 0);
291 if ((ret
= open_mixer()) < 0) {
292 PARA_EMERG_LOG("can not open mixer device %s.",
293 args_info
.mixer_device_arg
);
299 if (!strcmp(args_info
.mode_arg
, "sleep")) {
303 if (!strcmp(args_info
.mode_arg
, "fade")) {
304 fade(args_info
.fade_vol_arg
, args_info
.fade_time_arg
);
307 if (!strcmp(args_info
.mode_arg
, "snooze")) {