2 * Copyright (C) 1998-2009 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 for linux. */
10 #include <sys/types.h>
12 #include <sys/ioctl.h>
14 #include <stdlib.h> /* EXIT_SUCCESS */
19 #include <sys/soundcard.h>
21 #include "fade.cmdline.h"
28 struct fade_args_info conf
;
30 __printf_2_3
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 para_open(conf
.mixer_device_arg
, O_RDWR
, 42);
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)
60 return -ERRNO_TO_PARA_ERROR(errno
);
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)
89 return -ERRNO_TO_PARA_ERROR(errno
);
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 int fade(int new_vol
, int fade_time
)
114 int vol
, mixer_fd
, diff
, incr
, ret
;
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
);
127 ret
= do_get_vol(mixer_fd
);
131 diff
= new_vol
- vol
;
136 incr
= diff
> 0? 1: -1;
137 diff
= diff
> 0? diff
: -diff
;
138 tmp
= secs
* 1000 / diff
;
140 while ((new_vol
- vol
) * incr
> 0) {
141 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
142 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
143 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
145 ret
= do_set_vol(mixer_fd
, vol
);
148 //printf("vol = %i\n", vol);
149 nanosleep(&ts
, NULL
);
156 static void client_cmd(const char *cmd
)
158 int ret
, fds
[3] = {0, 0, 0};
160 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
162 PARA_INFO_LOG("%s\n", cmdline
);
163 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
166 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
171 while (ret
!= -1 && errno
!= ECHILD
);
174 static void change_afs_mode_and_play(char *afs_mode
)
181 cmd
= make_message("select %s\n", afs_mode
);
190 static int sweet_dreams(void)
192 time_t t1
, wake_time_epoch
;
195 int ret
, min
= conf
.wake_min_arg
;
196 char *fa_mode
= conf
.fa_mode_arg
;
197 char *wake_mode
= conf
.wake_mode_arg
;
198 char *sleep_mode
= conf
.sleep_mode_arg
;
199 int wf
= conf
.wake_fade_arg
;
200 int sf
= conf
.fa_fade_arg
;
201 int wv
= conf
.wake_vol_arg
;
202 int sv
= conf
.fa_vol_arg
;
203 int iv
= conf
.sleep_ivol_arg
;
205 /* calculate wake time */
207 if (conf
.wake_hour_given
) {
208 int hour
= conf
.wake_hour_arg
;
210 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
211 t1
+= 86400; /* wake time is tomorrow */
218 t1
+= 9 * 60 * 60; /* nine hours from now */
219 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1
);
222 wake_time_epoch
= mktime(tm
);
223 PARA_INFO_LOG("waketime: %s", asctime(tm
));
227 PARA_INFO_LOG("initial volume: %d\n", iv
);
231 change_afs_mode_and_play(fa_mode
);
240 if (conf
.sleep_mode_given
)
241 change_afs_mode_and_play(sleep_mode
);
248 if (wake_time_epoch
<= t1
+ wf
)
250 delay
= wake_time_epoch
- t1
- wf
;
251 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
253 (delay
% 3600) / 60);
256 change_afs_mode_and_play(wake_mode
);
258 PARA_INFO_LOG("fade complete, returning\n");
262 static int snooze(void)
267 if (conf
.snooze_time_arg
<= 0)
269 sleep_time
= conf
.snooze_time_arg
;
270 if (get_vol() < conf
.snooze_out_vol_arg
)
271 ret
= set_vol(conf
.snooze_out_vol_arg
);
273 ret
= fade(conf
.snooze_out_vol_arg
, conf
.snooze_out_fade_arg
);
277 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf
.snooze_time_arg
);
280 return fade(conf
.snooze_in_vol_arg
, conf
.snooze_in_fade_arg
);
283 static int configfile_exists(void)
285 static char *config_file
;
287 if (!conf
.config_file_given
) {
288 char *home
= para_homedir();
290 config_file
= make_message("%s/.paraslash/fade.conf", home
);
292 conf
.config_file_arg
= config_file
;
294 return file_exists(conf
.config_file_arg
);
297 int main(int argc
, char *argv
[])
301 if (fade_cmdline_parser(argc
, argv
, &conf
))
303 HANDLE_VERSION_FLAG("fade", conf
);
304 ret
= configfile_exists();
305 if (!ret
&& conf
.config_file_given
) {
306 PARA_EMERG_LOG("can not read config file %s\n",
307 conf
.config_file_arg
);
311 struct fade_cmdline_parser_params params
= {
317 fade_cmdline_parser_config_file(conf
.config_file_arg
,
320 if (!strcmp(conf
.mode_arg
, "sleep")) {
321 ret
= sweet_dreams();
324 if (!strcmp(conf
.mode_arg
, "fade")) {
325 ret
= fade(conf
.fade_vol_arg
, conf
.fade_time_arg
);
328 if (!strcmp(conf
.mode_arg
, "snooze")) {
332 ret
= -E_FADE_SYNTAX
;
335 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
336 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;