2 * Copyright (C) 1998-2007 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"
19 #include <stdlib.h> /* EXIT_SUCCESS */
24 #include <linux/soundcard.h>
28 struct fade_args_info conf
;
30 void para_log(__a_unused
int ll
, const char *fmt
,...)
38 printf("%d:%02d:%02d ", tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
47 static int open_mixer(void)
49 return open(conf
.mixer_device_arg
, O_RDWR
, 0);
53 * get volume via mixer_fd
55 static int do_get_vol(int mixer_fd
)
59 if (ioctl(mixer_fd
, MIXER_READ(SOUND_MIXER_VOLUME
), &volume
) < 0)
61 /* take the mean value of left and right volume */
62 return (volume
% 256 + (volume
>> 8)) / 2;
66 * open mixer, get volume and close mixer
68 static int get_vol(void)
73 mixer_fd
= open_mixer();
76 volume
= do_get_vol(mixer_fd
);
82 * set volume via mixer_fd
84 static int do_set_vol(int mixer_fd
, int volume
)
86 int tmp
= (volume
<< 8) + volume
;
88 if (ioctl(mixer_fd
, MIXER_WRITE(SOUND_MIXER_VOLUME
), &tmp
) < 0)
94 * open mixer, set volume and close mixer
96 static int set_vol(int volume
)
98 int mixer_fd
, ret
= open_mixer();
103 ret
= do_set_vol(mixer_fd
, volume
);
109 * Open mixer, get volume, fade to new_vol in secs seconds and
112 static void fade(int new_vol
, int fade_time
)
114 int vol
, mixer_fd
, diff
, incr
;
117 unsigned long long tmp
, tmp2
; /* Careful with that axe, Eugene! */
122 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol
, secs
);
123 mixer_fd
= open_mixer();
126 vol
= do_get_vol(mixer_fd
);
129 diff
= new_vol
- vol
;
134 incr
= diff
> 0? 1: -1;
135 diff
= diff
> 0? diff
: -diff
;
136 tmp
= secs
* 1000 / diff
;
138 while ((new_vol
- vol
) * incr
> 0) {
139 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
140 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
141 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
143 if (do_set_vol(mixer_fd
, vol
) < 0)
145 //printf("vol = %i\n", vol);
146 nanosleep(&ts
, NULL
);
152 static void client_cmd(const char *cmd
)
154 int ret
, fds
[3] = {0, 0, 0};
156 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
158 PARA_INFO_LOG("%s\n", cmdline
);
159 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
165 while (ret
!= -1 && errno
!= ECHILD
);
168 static void change_afs_mode_and_play(char *afs_mode
)
175 cmd
= make_message("select %s\n", afs_mode
);
184 static void sweet_dreams(void)
186 time_t t1
, wake_time_epoch
;
189 int min
= conf
.wake_min_arg
;
190 char *fa_mode
= conf
.fa_mode_arg
;
191 char *wake_mode
= conf
.wake_mode_arg
;
192 char *sleep_mode
= conf
.sleep_mode_arg
;
193 int wf
= conf
.wake_fade_arg
;
194 int sf
= conf
.fa_fade_arg
;
195 int wv
= conf
.wake_vol_arg
;
196 int sv
= conf
.fa_vol_arg
;
197 int iv
= conf
.sleep_ivol_arg
;
199 /* calculate wake time */
201 if (conf
.wake_hour_given
) {
202 int hour
= conf
.wake_hour_arg
;
204 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
205 t1
+= 86400; /* wake time is tomorrow */
212 t1
+= 9 * 60 * 60; /* nine hours from now */
213 PARA_INFO_LOG("default wake time: %lu\n", t1
);
216 wake_time_epoch
= mktime(tm
);
217 PARA_INFO_LOG("waketime: %s", asctime(tm
));
221 PARA_INFO_LOG("initial volume: %d\n", iv
);
223 change_afs_mode_and_play(fa_mode
);
227 change_afs_mode_and_play(sleep_mode
);
232 if (wake_time_epoch
<= t1
+ wf
)
234 delay
= wake_time_epoch
- t1
- wf
;
235 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
237 (delay
% 3600) / 60);
240 change_afs_mode_and_play(wake_mode
);
242 PARA_INFO_LOG("fade complete, returning\n");
245 static void snooze(void)
249 if (conf
.snooze_time_arg
<= 0)
251 sleep_time
= conf
.snooze_time_arg
;
252 if (get_vol() < conf
.snooze_out_vol_arg
)
253 set_vol(conf
.snooze_out_vol_arg
);
255 fade(conf
.snooze_out_vol_arg
, conf
.snooze_out_fade_arg
);
257 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf
.snooze_time_arg
);
260 fade(conf
.snooze_in_vol_arg
, conf
.snooze_in_fade_arg
);
263 static int configfile_exists(void)
265 static char *config_file
;
267 if (!conf
.config_file_given
) {
268 char *home
= para_homedir();
270 config_file
= make_message("%s/.paraslash/fade.conf", home
);
272 conf
.config_file_arg
= config_file
;
274 return file_exists(conf
.config_file_arg
);
278 int main(int argc
, char *argv
[])
282 if (fade_cmdline_parser(argc
, argv
, &conf
))
284 HANDLE_VERSION_FLAG("fade", conf
);
285 ret
= configfile_exists();
286 if (!ret
&& conf
.config_file_given
) {
287 PARA_EMERG_LOG("can not read config file %s\n",
288 conf
.config_file_arg
);
292 struct fade_cmdline_parser_params params
= {
298 fade_cmdline_parser_config_file(conf
.config_file_arg
,
303 PARA_EMERG_LOG("can not open mixer device %s.",
304 conf
.mixer_device_arg
);
309 // setlinebuf(stdout);
310 if (!strcmp(conf
.mode_arg
, "sleep")) {
314 if (!strcmp(conf
.mode_arg
, "fade")) {
315 fade(conf
.fade_vol_arg
, conf
.fade_time_arg
);
318 if (!strcmp(conf
.mode_arg
, "snooze")) {
324 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;