open_and_update_audio_file(): Always print file name on errors.
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file fade.c A volume fader and alarm clock for OSS. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/ioctl.h>
13 #include <ctype.h>
14 #include <stdlib.h> /* EXIT_SUCCESS */
15 #include <unistd.h>
16 #include <signal.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <sys/soundcard.h>
20
21 #include "fade.cmdline.h"
22 #include "para.h"
23 #include "fd.h"
24 #include "string.h"
25 #include "error.h"
26
27 INIT_FADE_ERRLISTS;
28 static struct fade_args_info conf;
29
30 __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
31 {
32 va_list argp;
33 time_t t1;
34 struct tm *tm;
35
36 time(&t1);
37 tm = localtime(&t1);
38 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
39 va_start(argp, fmt);
40 vprintf(fmt, argp);
41 va_end(argp);
42 }
43
44 /*
45 * Open the mixer device.
46 */
47 static int open_mixer(void)
48 {
49 PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
50 return para_open(conf.mixer_device_arg, O_RDWR, 42);
51 }
52
53 /*
54 * Get channel volume via mixer_fd.
55 */
56 static int get_mixer_channel(int mixer_fd)
57 {
58 int volume;
59
60 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
61 return -ERRNO_TO_PARA_ERROR(errno);
62 /* take the mean value of left and right volume */
63 return (volume % 256 + (volume >> 8)) / 2;
64 }
65
66 /*
67 * Open mixer, get volume and close mixer.
68 */
69 static int open_and_get_mixer_channel(void)
70 {
71 int mixer_fd;
72 int volume;
73
74 mixer_fd = open_mixer();
75 if (mixer_fd < 0)
76 return mixer_fd;
77 volume = get_mixer_channel(mixer_fd);
78 close(mixer_fd);
79 return volume;
80 }
81
82 /*
83 * Set channel volume via mixer_fd.
84 */
85 static int set_mixer_channel(int mixer_fd, int volume)
86 {
87 int tmp = (volume << 8) + volume;
88
89 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
90 return -ERRNO_TO_PARA_ERROR(errno);
91 return 1;
92 }
93
94 /*
95 * Open mixer, set volume and close mixer.
96 */
97 static int open_and_set_mixer_channel(int volume)
98 {
99 int mixer_fd, ret = open_mixer();
100
101 if (ret < 0)
102 return ret;
103 mixer_fd = ret;
104 ret = set_mixer_channel(mixer_fd, volume);
105 close(mixer_fd);
106 return ret;
107 }
108
109 static void fixup_mixer_channel_arg(void)
110 {
111 int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
112
113 switch (conf.mixer_channel_arg) {
114 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
115 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
116 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
117 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
118 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
119 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
120 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
121 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
122 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
123 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
124 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
125 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
126 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
127 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
128 default: break;
129 }
130 conf.mixer_channel_arg = val;
131 }
132
133 /*
134 * Open mixer, get volume, fade to new_vol in secs seconds and
135 * close mixer.
136 */
137 static int fade(int new_vol, int fade_time)
138 {
139 int vol, mixer_fd, diff, incr, ret;
140 unsigned secs;
141 struct timespec ts;
142 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
143
144 if (fade_time <= 0)
145 return 1;
146 secs = fade_time;
147 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
148 ret = open_mixer();
149 if (ret < 0)
150 return ret;
151 mixer_fd = ret;
152 ret = get_mixer_channel(mixer_fd);
153 if (ret < 0)
154 goto out;
155 vol = ret;
156 diff = new_vol - vol;
157 if (!diff) {
158 sleep(secs);
159 goto out;
160 }
161 incr = diff > 0? 1: -1;
162 diff = diff > 0? diff: -diff;
163 tmp = secs * 1000 / diff;
164 tmp2 = tmp % 1000;
165 while ((new_vol - vol) * incr > 0) {
166 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
167 ts.tv_sec = tmp / 1000; /* really nec ?*/
168 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
169 vol += incr;
170 ret = set_mixer_channel(mixer_fd, vol);
171 if (ret < 0)
172 goto out;
173 //printf("vol = %i\n", vol);
174 nanosleep(&ts, NULL);
175 }
176 out:
177 close(mixer_fd);
178 return ret;
179 }
180
181 static void client_cmd(const char *cmd)
182 {
183 int ret, fds[3] = {0, 0, 0};
184 pid_t pid;
185 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
186
187 PARA_INFO_LOG("%s\n", cmdline);
188 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
189 free(cmdline);
190 if (ret < 0) {
191 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
192 exit(EXIT_FAILURE);
193 }
194 do
195 ret = wait(NULL);
196 while (ret != -1 && errno != ECHILD);
197 }
198
199 static void change_afs_mode_and_play(char *afs_mode)
200 {
201 char *cmd;
202
203 client_cmd("stop");
204 if (!afs_mode)
205 return;
206 cmd = make_message("select %s\n", afs_mode);
207 client_cmd(cmd);
208 free(cmd);
209 client_cmd("play");
210 }
211
212 /*
213 * sleep
214 */
215 static int sweet_dreams(void)
216 {
217 time_t t1, wake_time_epoch;
218 unsigned int delay;
219 struct tm *tm;
220 int ret, min = conf.wake_min_arg;
221 char *fo_mood = conf.fo_mood_arg;
222 char *fi_mood = conf.fi_mood_arg;
223 char *sleep_mood = conf.sleep_mood_arg;
224 int fit = conf.fi_time_arg;
225 int fot = conf.fo_time_arg;
226 int fiv = conf.fi_vol_arg;
227 int fov = conf.fo_vol_arg;
228 int iv = conf.ivol_arg;
229
230 /* calculate wake time */
231 time(&t1);
232 if (conf.wake_hour_given) {
233 int hour = conf.wake_hour_arg;
234 tm = localtime(&t1);
235 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
236 t1 += 86400; /* wake time is tomorrow */
237 tm = localtime(&t1);
238 }
239 tm->tm_hour = hour;
240 tm->tm_min = min;
241 tm->tm_sec = 0;
242 } else {
243 t1 += 9 * 60 * 60; /* nine hours from now */
244 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
245 tm = localtime(&t1);
246 }
247 wake_time_epoch = mktime(tm);
248 PARA_INFO_LOG("waketime: %s", asctime(tm));
249 client_cmd("stop");
250 sleep(1);
251 if (fot) {
252 PARA_INFO_LOG("initial volume: %d\n", iv);
253 ret = open_and_set_mixer_channel(iv);
254 if (ret < 0)
255 return ret;
256 change_afs_mode_and_play(fo_mood);
257 ret = fade(fov, fot);
258 if (ret < 0)
259 return ret;
260 } else {
261 ret = open_and_set_mixer_channel(fov);
262 if (ret < 0)
263 return ret;
264 }
265 if (conf.sleep_mood_given)
266 change_afs_mode_and_play(sleep_mood);
267 else
268 client_cmd("stop");
269 if (!fit)
270 return 1;
271 for (;;) {
272 time(&t1);
273 if (wake_time_epoch <= t1 + fit)
274 break;
275 delay = wake_time_epoch - t1 - fit;
276 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
277 delay, delay / 3600,
278 (delay % 3600) / 60);
279 sleep(delay);
280 }
281 change_afs_mode_and_play(fi_mood);
282 ret = fade(fiv, fit);
283 PARA_INFO_LOG("fade complete, returning\n");
284 return ret;
285 }
286
287 static int snooze(void)
288 {
289 int ret;
290 unsigned sleep_time;
291
292 if (conf.so_time_arg <= 0)
293 return 1;
294 sleep_time = conf.so_time_arg;
295 if (open_and_get_mixer_channel() < conf.so_vol_arg)
296 ret = open_and_set_mixer_channel(conf.so_vol_arg);
297 else
298 ret = fade(conf.so_vol_arg, conf.so_time_arg);
299 if (ret < 0)
300 return ret;
301 client_cmd("pause");
302 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
303 sleep(sleep_time);
304 client_cmd("play");
305 return fade(conf.si_vol_arg, conf.si_time_arg);
306 }
307
308 static int configfile_exists(void)
309 {
310 static char *config_file;
311
312 if (!conf.config_file_given) {
313 char *home = para_homedir();
314 free(config_file);
315 config_file = make_message("%s/.paraslash/fade.conf", home);
316 free(home);
317 conf.config_file_arg = config_file;
318 }
319 return file_exists(conf.config_file_arg);
320 }
321
322 int main(int argc, char *argv[])
323 {
324 int ret;
325
326 if (fade_cmdline_parser(argc, argv, &conf))
327 exit(EXIT_FAILURE);
328 HANDLE_VERSION_FLAG("fade", conf);
329 ret = configfile_exists();
330 if (!ret && conf.config_file_given) {
331 PARA_EMERG_LOG("can not read config file %s\n",
332 conf.config_file_arg);
333 exit(EXIT_FAILURE);
334 }
335 if (ret) {
336 struct fade_cmdline_parser_params params = {
337 .override = 0,
338 .initialize = 0,
339 .check_required = 0,
340 .check_ambiguity = 0,
341 .print_errors = 1
342 };
343 fade_cmdline_parser_config_file(conf.config_file_arg,
344 &conf, &params);
345 }
346 fixup_mixer_channel_arg();
347 switch (conf.mode_arg) {
348 case mode_arg_fade:
349 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
350 break;
351 case mode_arg_snooze:
352 ret = snooze();
353 break;
354 default: /* sleep mode */
355 ret = sweet_dreams();
356 break;
357 }
358 if (ret < 0)
359 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
360 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
361 }