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 */
9 #include "fade.cmdline.h"
16 #include <stdlib.h> /* EXIT_SUCCESS */
21 #include <linux/soundcard.h>
25 struct fade_args_info conf
;
27 void para_log(__a_unused
int ll
, const char *fmt
,...)
35 printf("%d:%02d:%02d ", tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
44 static int open_mixer(void)
46 return open(conf
.mixer_device_arg
, O_RDWR
, 0);
50 * get volume via mixer_fd
52 static int do_get_vol(int mixer_fd
)
56 if (ioctl(mixer_fd
, MIXER_READ(SOUND_MIXER_VOLUME
), &volume
) < 0)
58 /* take the mean value of left and right volume */
59 return (volume
% 256 + (volume
>> 8)) / 2;
63 * open mixer, get volume and close mixer
65 static int get_vol(void)
70 mixer_fd
= open_mixer();
73 volume
= do_get_vol(mixer_fd
);
79 * set volume via mixer_fd
81 static int do_set_vol(int mixer_fd
, int volume
)
83 int tmp
= (volume
<< 8) + volume
;
85 if (ioctl(mixer_fd
, MIXER_WRITE(SOUND_MIXER_VOLUME
), &tmp
) < 0)
91 * open mixer, set volume and close mixer
93 static int set_vol(int volume
)
95 int mixer_fd
, ret
= open_mixer();
100 ret
= do_set_vol(mixer_fd
, volume
);
106 * Open mixer, get volume, fade to new_vol in secs seconds and
109 static void fade(int new_vol
, int fade_time
)
111 int vol
, mixer_fd
, diff
, incr
;
114 unsigned long long tmp
, tmp2
; /* Careful with that axe, Eugene! */
119 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol
, secs
);
120 mixer_fd
= open_mixer();
123 vol
= do_get_vol(mixer_fd
);
126 diff
= new_vol
- vol
;
131 incr
= diff
> 0? 1: -1;
132 diff
= diff
> 0? diff
: -diff
;
133 tmp
= secs
* 1000 / diff
;
135 while ((new_vol
- vol
) * incr
> 0) {
136 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
137 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
138 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
140 if (do_set_vol(mixer_fd
, vol
) < 0)
142 //printf("vol = %i\n", vol);
143 nanosleep(&ts
, NULL
);
149 static int client_cmd(const char *cmd
,...)
151 int ret
, fds
[3] = {0, 0, 0};
153 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
155 PARA_INFO_LOG("%s\n", cmdline
);
156 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
164 static void sweet_dreams(void)
166 time_t t1
, wake_time_epoch
;
169 int min
= conf
.wake_min_arg
;
170 char *fa_stream
= conf
.fa_stream_arg
;
171 char *wake_stream
= conf
.wake_stream_arg
;
172 //char *current_stream = stat_items[STREAM].content;
173 int wf
= conf
.wake_fade_arg
;
174 int sf
= conf
.fa_fade_arg
;
175 int wv
= conf
.wake_vol_arg
;
176 int sv
= conf
.fa_vol_arg
;
177 int iv
= conf
.sleep_ivol_arg
;
178 char *cmd
, *sleep_stream
= conf
.sleep_stream_given
?
179 conf
.sleep_stream_arg
: NULL
;
181 /* calculate wake time */
183 if (conf
.wake_hour_given
) {
184 int hour
= conf
.wake_hour_arg
;
186 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
187 t1
+= 86400; /* wake time is tomorrow */
195 PARA_INFO_LOG("default wake time: %lu\n", t1
);
198 wake_time_epoch
= mktime(tm
);
199 PARA_INFO_LOG("waketime: %s", asctime(tm
));
201 PARA_INFO_LOG("initial volume: %d\n", iv
);
203 cmd
= make_message("csp %s\n", fa_stream
);
209 cmd
= make_message("csp %s\n", sleep_stream
);
218 if (wake_time_epoch
<= t1
+ wf
)
220 delay
= wake_time_epoch
- t1
- wf
;
221 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
223 (delay
% 3600) / 60);
226 cmd
= make_message("csp %s\n", wake_stream
);
230 PARA_INFO_LOG("%s", "fade complete, returning\n");
233 static void snooze(void)
237 if (conf
.snooze_time_arg
<= 0)
239 sleep_time
= conf
.snooze_time_arg
;
240 if (get_vol() < conf
.snooze_out_vol_arg
)
241 set_vol(conf
.snooze_out_vol_arg
);
243 fade(conf
.snooze_out_vol_arg
, conf
.snooze_out_fade_arg
);
245 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf
.snooze_time_arg
);
248 fade(conf
.snooze_in_vol_arg
, conf
.snooze_in_fade_arg
);
251 static int configfile_exists(void)
253 static char *config_file
;
255 if (!conf
.config_file_given
) {
256 char *home
= para_homedir();
258 config_file
= make_message("%s/.paraslash/fade.conf", home
);
260 conf
.config_file_arg
= config_file
;
262 return file_exists(conf
.config_file_arg
);
266 int main(int argc
, char *argv
[])
270 if (fade_cmdline_parser(argc
, argv
, &conf
))
272 HANDLE_VERSION_FLAG("fade", conf
);
273 ret
= configfile_exists();
274 if (!ret
&& conf
.config_file_given
) {
275 PARA_EMERG_LOG("can not read config file %s\n",
276 conf
.config_file_arg
);
280 struct fade_cmdline_parser_params params
= {
286 fade_cmdline_parser_config_file(conf
.config_file_arg
,
291 PARA_EMERG_LOG("can not open mixer device %s.",
292 conf
.mixer_device_arg
);
297 // setlinebuf(stdout);
298 if (!strcmp(conf
.mode_arg
, "sleep")) {
302 if (!strcmp(conf
.mode_arg
, "fade")) {
303 fade(conf
.fade_vol_arg
, conf
.fade_time_arg
);
306 if (!strcmp(conf
.mode_arg
, "snooze")) {
312 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;