2 * Copyright (C) 1998-2012 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 OSS. */
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
13 #include <stdlib.h> /* EXIT_SUCCESS */
18 #include <sys/soundcard.h>
20 #include "fade.cmdline.h"
28 static struct fade_args_info conf
;
30 __printf_2_3
void date_log(__a_unused
int ll
, const char *fmt
, ...)
38 printf("%d:%02d:%02d ", tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
43 __printf_2_3
void (*para_log
)(int, const char*, ...) = date_log
;
46 * Open the mixer device.
48 static int open_mixer(void)
50 PARA_INFO_LOG("opening %s\n", conf
.mixer_device_arg
);
51 return para_open(conf
.mixer_device_arg
, O_RDWR
, 42);
55 * Get channel volume via mixer_fd.
57 static int get_mixer_channel(int mixer_fd
)
61 if (ioctl(mixer_fd
, MIXER_READ(conf
.mixer_channel_arg
), &volume
) < 0)
62 return -ERRNO_TO_PARA_ERROR(errno
);
63 /* take the mean value of left and right volume */
64 return (volume
% 256 + (volume
>> 8)) / 2;
68 * Open mixer, get volume and close mixer.
70 static int open_and_get_mixer_channel(void)
75 mixer_fd
= open_mixer();
78 volume
= get_mixer_channel(mixer_fd
);
84 * Set channel volume via mixer_fd.
86 static int set_mixer_channel(int mixer_fd
, int volume
)
88 int tmp
= (volume
<< 8) + volume
;
90 if (ioctl(mixer_fd
, MIXER_WRITE(conf
.mixer_channel_arg
), &tmp
) < 0)
91 return -ERRNO_TO_PARA_ERROR(errno
);
96 * Open mixer, set volume and close mixer.
98 static int open_and_set_mixer_channel(int volume
)
100 int mixer_fd
, ret
= open_mixer();
105 ret
= set_mixer_channel(mixer_fd
, volume
);
110 static void fixup_mixer_channel_arg(void)
112 int val
= SOUND_MIXER_VOLUME
; /* STFU, gcc */
114 switch (conf
.mixer_channel_arg
) {
115 case mixer_channel_arg_volume
: val
= SOUND_MIXER_VOLUME
; break;
116 case mixer_channel_arg_bass
: val
= SOUND_MIXER_BASS
; break;
117 case mixer_channel_arg_treble
: val
= SOUND_MIXER_TREBLE
; break;
118 case mixer_channel_arg_synth
: val
= SOUND_MIXER_SYNTH
; break;
119 case mixer_channel_arg_pcm
: val
= SOUND_MIXER_PCM
; break;
120 case mixer_channel_arg_speaker
: val
= SOUND_MIXER_SPEAKER
; break;
121 case mixer_channel_arg_line
: val
= SOUND_MIXER_LINE
; break;
122 case mixer_channel_arg_mic
: val
= SOUND_MIXER_MIC
; break;
123 case mixer_channel_arg_cd
: val
= SOUND_MIXER_CD
; break;
124 case mixer_channel_arg_imix
: val
= SOUND_MIXER_IMIX
; break;
125 case mixer_channel_arg_altpcm
: val
= SOUND_MIXER_ALTPCM
; break;
126 case mixer_channel_arg_reclev
: val
= SOUND_MIXER_RECLEV
; break;
127 case mixer_channel_arg_igain
: val
= SOUND_MIXER_IGAIN
; break;
128 case mixer_channel_arg_ogain
: val
= SOUND_MIXER_OGAIN
; break;
131 conf
.mixer_channel_arg
= val
;
135 * Open mixer, get volume, fade to new_vol in secs seconds and
138 static int fade(int new_vol
, int fade_time
)
140 int vol
, mixer_fd
, diff
, incr
, ret
;
143 unsigned long long tmp
, tmp2
; /* Careful with that axe, Eugene! */
148 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol
, secs
);
153 ret
= get_mixer_channel(mixer_fd
);
157 diff
= new_vol
- vol
;
162 incr
= diff
> 0? 1: -1;
163 diff
= diff
> 0? diff
: -diff
;
164 tmp
= secs
* 1000 / diff
;
166 while ((new_vol
- vol
) * incr
> 0) {
167 ts
.tv_nsec
= tmp2
* 1000000; /* really nec ?*/
168 ts
.tv_sec
= tmp
/ 1000; /* really nec ?*/
169 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
171 ret
= set_mixer_channel(mixer_fd
, vol
);
174 //printf("vol = %i\n", vol);
175 nanosleep(&ts
, NULL
);
182 static void client_cmd(const char *cmd
)
184 int ret
, fds
[3] = {0, 0, 0};
186 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
188 PARA_INFO_LOG("%s\n", cmdline
);
189 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
192 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
197 while (ret
!= -1 && errno
!= ECHILD
);
200 static void change_afs_mode_and_play(char *afs_mode
)
207 cmd
= make_message("select %s\n", afs_mode
);
216 static int sweet_dreams(void)
218 time_t t1
, wake_time_epoch
;
221 int ret
, min
= conf
.wake_min_arg
;
222 char *fo_mood
= conf
.fo_mood_arg
;
223 char *fi_mood
= conf
.fi_mood_arg
;
224 char *sleep_mood
= conf
.sleep_mood_arg
;
225 int fit
= conf
.fi_time_arg
;
226 int fot
= conf
.fo_time_arg
;
227 int fiv
= conf
.fi_vol_arg
;
228 int fov
= conf
.fo_vol_arg
;
229 int iv
= conf
.ivol_arg
;
231 /* calculate wake time */
233 if (conf
.wake_hour_given
) {
234 int hour
= conf
.wake_hour_arg
;
236 if (tm
->tm_hour
> hour
|| (tm
->tm_hour
== hour
&& tm
->tm_min
> min
)) {
237 t1
+= 86400; /* wake time is tomorrow */
244 t1
+= 9 * 60 * 60; /* nine hours from now */
245 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1
);
248 wake_time_epoch
= mktime(tm
);
249 PARA_INFO_LOG("waketime: %s", asctime(tm
));
253 PARA_INFO_LOG("initial volume: %d\n", iv
);
254 ret
= open_and_set_mixer_channel(iv
);
257 change_afs_mode_and_play(fo_mood
);
258 ret
= fade(fov
, fot
);
262 ret
= open_and_set_mixer_channel(fov
);
266 if (conf
.sleep_mood_given
)
267 change_afs_mode_and_play(sleep_mood
);
274 if (wake_time_epoch
<= t1
+ fit
)
276 delay
= wake_time_epoch
- t1
- fit
;
277 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
279 (delay
% 3600) / 60);
282 change_afs_mode_and_play(fi_mood
);
283 ret
= fade(fiv
, fit
);
284 PARA_INFO_LOG("fade complete, returning\n");
288 static int snooze(void)
293 if (conf
.so_time_arg
<= 0)
295 sleep_time
= conf
.so_time_arg
;
296 if (open_and_get_mixer_channel() < conf
.so_vol_arg
)
297 ret
= open_and_set_mixer_channel(conf
.so_vol_arg
);
299 ret
= fade(conf
.so_vol_arg
, conf
.so_time_arg
);
303 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf
.snooze_time_arg
);
306 return fade(conf
.si_vol_arg
, conf
.si_time_arg
);
309 static int configfile_exists(void)
311 static char *config_file
;
313 if (!conf
.config_file_given
) {
314 char *home
= para_homedir();
316 config_file
= make_message("%s/.paraslash/fade.conf", home
);
318 conf
.config_file_arg
= config_file
;
320 return file_exists(conf
.config_file_arg
);
323 int main(int argc
, char *argv
[])
327 if (fade_cmdline_parser(argc
, argv
, &conf
))
329 HANDLE_VERSION_FLAG("fade", conf
);
330 ret
= configfile_exists();
331 if (!ret
&& conf
.config_file_given
) {
332 PARA_EMERG_LOG("can not read config file %s\n",
333 conf
.config_file_arg
);
337 struct fade_cmdline_parser_params params
= {
341 .check_ambiguity
= 0,
344 fade_cmdline_parser_config_file(conf
.config_file_arg
,
347 fixup_mixer_channel_arg();
348 switch (conf
.mode_arg
) {
350 ret
= fade(conf
.fade_vol_arg
, conf
.fade_time_arg
);
352 case mode_arg_snooze
:
355 default: /* sleep mode */
356 ret
= sweet_dreams();
360 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
361 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;