mixer: Implement non-linear time scale for fading.
[paraslash.git] / mixer.c
1 /*
2 * Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file mixer.c A volume fader and alarm clock for OSS. */
8
9 #include <regex.h>
10 #include <lopsub.h>
11 #include <math.h>
12
13 #include "mixer.lsg.h"
14 #include "para.h"
15 #include "fd.h"
16 #include "string.h"
17 #include "mix.h"
18 #include "error.h"
19 #include "version.h"
20
21 /** Array of error strings. */
22 DEFINE_PARA_ERRLIST;
23
24 enum mixer_id {MIXER_ENUM};
25 static char *mixer_name[] = {MIXER_NAMES};
26 DECLARE_MIXER_INITS;
27 static struct mixer supported_mixer[] = {MIXER_ARRAY};
28 #define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
29
30 static struct lls_parse_result *lpr, *sub_lpr;
31
32 #define CMD_PTR(_cmd) (lls_cmd(LSG_MIXER_CMD_ ## _cmd, mixer_suite))
33 #define OPT_RESULT(_cmd, _opt) (lls_opt_result( \
34 LSG_MIXER_ ## _cmd ## _OPT_ ## _opt, (LSG_MIXER_CMD_ ## _cmd == 0)? lpr : sub_lpr))
35 #define OPT_GIVEN(_cmd, _opt) (lls_opt_given(OPT_RESULT(_cmd, _opt)))
36 #define OPT_STRING_VAL(_cmd, _opt) (lls_string_val(0, OPT_RESULT(_cmd, _opt)))
37 #define OPT_UINT32_VAL(_cmd, _opt) (lls_uint32_val(0, OPT_RESULT(_cmd, _opt)))
38
39 typedef int (*mixer_subcommand_handler_t)(struct mixer *, struct mixer_handle *);
40
41 #define EXPORT_CMD(_cmd) const mixer_subcommand_handler_t \
42 lsg_mixer_com_ ## _cmd ## _user_data = &com_ ## _cmd;
43
44 static int loglevel;
45 static __printf_2_3 void date_log(int ll, const char *fmt, ...)
46 {
47 va_list argp;
48 time_t t1;
49 struct tm *tm;
50
51 if (ll < loglevel)
52 return;
53 time(&t1);
54 tm = localtime(&t1);
55 fprintf(stderr, "%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
56 va_start(argp, fmt);
57 vprintf(fmt, argp);
58 va_end(argp);
59 }
60 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
61
62 static int set_channel(struct mixer *m, struct mixer_handle *h, const char *channel)
63 {
64
65 PARA_NOTICE_LOG("using %s mixer channel\n", channel?
66 channel : "default");
67 return m->set_channel(h, channel);
68 }
69
70 static void millisleep(int ms)
71 {
72 struct timespec ts;
73
74 PARA_INFO_LOG("sleeping %dms\n", ms);
75 if (ms < 0)
76 return;
77 ts.tv_sec = ms / 1000,
78 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
79 nanosleep(&ts, NULL);
80 }
81
82 /*
83 * This implements the inverse function of t -> t^alpha, scaled to the time
84 * interval [0,T] and the range given by old_vol and new_vol. It returns the
85 * amount of milliseconds until the given volume is reached.
86 */
87 static unsigned volume_time(double vol, double old_vol, double new_vol,
88 double T, double alpha)
89 {
90 double c, d, x;
91
92 if (old_vol < new_vol) {
93 c = old_vol;
94 d = new_vol;
95 } else {
96 c = new_vol;
97 d = old_vol;
98 }
99
100 x = T * exp(log(((vol - c) / (d - c))) / alpha);
101 assert(x <= T);
102 if (old_vol < new_vol)
103 return x;
104 else
105 return T - x;
106 }
107
108 /* Fade to new volume in fade_time seconds. */
109 static int fade(struct mixer *m, struct mixer_handle *h, uint32_t new_vol,
110 uint32_t fade_time)
111 {
112 int i, T, old_vol, ret, slept, incr;
113 double ms, alpha;
114 uint32_t fe = OPT_UINT32_VAL(PARA_MIXER, FADE_EXPONENT);
115
116 if (fade_time <= 0 || fe >= 100) {
117 ret = m->set(h, new_vol);
118 if (ret < 0)
119 return ret;
120 goto sleep;
121 }
122 alpha = (100 - fe) / 100.0;
123 ret = m->get(h);
124 if (ret < 0)
125 return ret;
126 old_vol = ret;
127 if (old_vol == new_vol)
128 goto sleep;
129 PARA_NOTICE_LOG("fading %s from %d to %u in %u seconds\n",
130 OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL), old_vol,
131 new_vol, fade_time);
132 incr = old_vol < new_vol? 1 : -1;
133 T = fade_time * 1000;
134 i = old_vol;
135 slept = 0;
136 do {
137 ms = volume_time(i + incr, old_vol, new_vol, T, alpha);
138 millisleep(ms - slept);
139 slept = ms;
140 i += incr;
141 ret = m->set(h, i);
142 if (ret < 0)
143 return ret;
144 } while (i != new_vol);
145 return 1;
146 sleep:
147 sleep(fade_time);
148 return ret;
149 }
150
151 static int com_fade(struct mixer *m, struct mixer_handle *h)
152 {
153 uint32_t new_vol = OPT_UINT32_VAL(FADE, FADE_VOL);
154 uint32_t fade_time = OPT_UINT32_VAL(FADE, FADE_TIME);
155 return fade(m, h, new_vol, fade_time);
156 }
157 EXPORT_CMD(fade);
158
159 static void client_cmd(const char *cmd)
160 {
161 int ret, status, fds[3] = {0, 0, 0};
162 pid_t pid;
163 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
164
165 PARA_NOTICE_LOG("%s\n", cmdline);
166 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
167 free(cmdline);
168 if (ret < 0) {
169 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
170 goto fail;
171 }
172 do
173 pid = waitpid(pid, &status, 0);
174 while (pid == -1 && errno == EINTR);
175 if (pid < 0) {
176 PARA_ERROR_LOG("%s\n", strerror(errno));
177 goto fail;
178 }
179 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
180 goto fail;
181 return;
182 fail:
183 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
184 exit(EXIT_FAILURE);
185 }
186
187 static void change_afs_mode(const char *afs_mode)
188 {
189 char *cmd;
190
191 client_cmd("stop");
192 if (!afs_mode)
193 return;
194 cmd = make_message("select %s", afs_mode);
195 client_cmd(cmd);
196 free(cmd);
197 }
198
199 static int set_initial_volume(struct mixer *m, struct mixer_handle *h)
200 {
201 int i, ret;
202
203 for (i = 0; i < OPT_GIVEN(SLEEP, IVOL); i++) {
204 const char *val = lls_string_val(i, OPT_RESULT(SLEEP, IVOL));
205 char *p, *ch, *arg = para_strdup(val);
206 int32_t iv;
207 p = strchr(arg, ':');
208 if (p) {
209 *p = '\0';
210 p++;
211 ch = arg;
212 } else {
213 p = arg;
214 ch = NULL;
215 }
216 ret = para_atoi32(p, &iv);
217 if (ret < 0) {
218 free(arg);
219 return ret;
220 }
221 ret = set_channel(m, h, ch);
222 if (!ch)
223 ch = "default";
224 if (ret < 0) {
225 PARA_WARNING_LOG("ignoring channel %s\n", ch);
226 ret = 0;
227 } else {
228 PARA_INFO_LOG("initial volume %s: %d\n", ch, iv);
229 ret = m->set(h, iv);
230 }
231 free(arg);
232 if (ret < 0)
233 return ret;
234 }
235 return 1;
236 }
237
238 static int com_sleep(struct mixer *m, struct mixer_handle *h)
239 {
240 time_t t1, wake_time_epoch;
241 unsigned int delay;
242 struct tm *tm;
243 int ret;
244 const char *wake_time = OPT_STRING_VAL(SLEEP, WAKE_TIME);
245 const char *fo_mood = OPT_STRING_VAL(SLEEP, FO_MOOD);
246 const char *fi_mood = OPT_STRING_VAL(SLEEP, FI_MOOD);
247 const char *sleep_mood = OPT_STRING_VAL(SLEEP, SLEEP_MOOD);
248 int fit = OPT_UINT32_VAL(SLEEP, FI_TIME);
249 int fot = OPT_UINT32_VAL(SLEEP, FO_TIME);
250 int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL);
251 int fov = OPT_UINT32_VAL(SLEEP, FO_VOL);
252 int32_t hour, min = 0;
253 char *tmp;
254 char *wt = para_strdup(wake_time + (wake_time[0] == '+'));
255
256 /* calculate wake time */
257 time(&t1);
258 tmp = strchr(wt, ':');
259 if (tmp) {
260 *tmp = '\0';
261 tmp++;
262 ret = para_atoi32(tmp, &min);
263 if (ret < 0) {
264 free(wt);
265 return ret;
266 }
267 }
268 ret = para_atoi32(wt, &hour);
269 free(wt);
270 if (ret < 0)
271 return ret;
272 if (wake_time[0] == '+') { /* relative */
273 t1 += hour * 60 * 60 + min * 60;
274 tm = localtime(&t1);
275 } else {
276 tm = localtime(&t1);
277 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
278 t1 += 86400; /* wake time is tomorrow */
279 tm = localtime(&t1);
280 }
281 tm->tm_hour = hour;
282 tm->tm_min = min;
283 tm->tm_sec = 0;
284 }
285 wake_time_epoch = mktime(tm);
286 PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);
287 client_cmd("stop");
288 sleep(1);
289 if (fot) {
290 ret = set_initial_volume(m, h);
291 if (ret < 0)
292 return ret;
293 change_afs_mode(fo_mood);
294 client_cmd("play");
295 ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
296 if (ret < 0)
297 return ret;
298 ret = fade(m, h, fov, fot);
299 if (ret < 0)
300 return ret;
301 } else {
302 ret = m->set(h, fov);
303 if (ret < 0)
304 return ret;
305 }
306 if (OPT_GIVEN(SLEEP, SLEEP_MOOD)) {
307 change_afs_mode(sleep_mood);
308 client_cmd("play");
309 } else
310 client_cmd("stop");
311 if (!fit)
312 return 1;
313 change_afs_mode(fi_mood);
314 for (;;) {
315 time(&t1);
316 if (wake_time_epoch <= t1 + fit)
317 break;
318 delay = wake_time_epoch - t1 - fit;
319 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
320 delay, delay / 3600,
321 (delay % 3600) / 60);
322 sleep(delay);
323 }
324 client_cmd("play");
325 ret = fade(m, h, fiv, fit);
326 PARA_INFO_LOG("fade complete, returning\n");
327 return ret;
328 }
329 EXPORT_CMD(sleep);
330
331 static int com_snooze(struct mixer *m, struct mixer_handle *h)
332 {
333 int ret, val;
334
335 if (OPT_UINT32_VAL(SNOOZE, SO_TIME) == 0)
336 return 1;
337 ret = m->get(h);
338 if (ret < 0)
339 return ret;
340 val = ret;
341 if (val < OPT_UINT32_VAL(SNOOZE, SO_VOL))
342 ret = m->set(h, OPT_UINT32_VAL(SNOOZE, SO_VOL));
343 else
344 ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SO_VOL),
345 OPT_UINT32_VAL(SNOOZE, SO_TIME));
346 if (ret < 0)
347 return ret;
348 client_cmd("pause");
349 PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
350 OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
351 sleep(OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
352 client_cmd("play");
353 return fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
354 OPT_UINT32_VAL(SNOOZE, SI_TIME));
355 }
356 EXPORT_CMD(snooze);
357
358 static void init_mixers(void)
359 {
360 int i;
361
362 FOR_EACH_MIXER(i) {
363 struct mixer *m = &supported_mixer[i];
364 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
365 i, mixer_name[i]);
366 m->init(m);
367 }
368 }
369
370 static int com_set(struct mixer *m, struct mixer_handle *h)
371 {
372 return m->set(h, OPT_UINT32_VAL(SET, VAL));
373 }
374 EXPORT_CMD(set);
375
376 static struct mixer *get_mixer_or_die(void)
377 {
378 int i;
379
380 if (!OPT_GIVEN(PARA_MIXER, MIXER_API))
381 i = DEFAULT_MIXER;
382 else
383 FOR_EACH_MIXER(i)
384 if (!strcmp(mixer_name[i],
385 OPT_STRING_VAL(PARA_MIXER, MIXER_API)))
386 break;
387 if (i < NUM_SUPPORTED_MIXERS) {
388 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
389 return supported_mixer + i;
390 }
391 printf("available mixer APIs: ");
392 FOR_EACH_MIXER(i) {
393 int d = (i == DEFAULT_MIXER);
394 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
395 }
396 printf("\n");
397 exit(EXIT_FAILURE);
398 }
399
400 static void show_subcommands(void)
401 {
402 const struct lls_command *cmd;
403 int i;
404 printf("Subcommands:\n");
405 for (i = 1; (cmd = lls_cmd(i, mixer_suite)); i++) {
406 const char *name = lls_command_name(cmd);
407 const char *purpose = lls_purpose(cmd);
408 printf("%-20s%s\n", name, purpose);
409 }
410 }
411
412
413 static int com_help(__a_unused struct mixer *m, __a_unused struct mixer_handle *h)
414 {
415 const struct lls_command *cmd;
416 const struct lls_opt_result *r_l = OPT_RESULT(HELP, LONG);
417 char *txt, *errctx;
418 const char *name;
419 int ret;
420
421 ret = lls_check_arg_count(sub_lpr, 0, 1, NULL);
422 if (ret < 0)
423 return ret;
424 if (lls_num_inputs(sub_lpr) == 0) {
425 show_subcommands();
426 return 0;
427 }
428 name = lls_input(0, sub_lpr);
429 ret = lls(lls_lookup_subcmd(name, mixer_suite, &errctx));
430 if (ret < 0) {
431 if (errctx)
432 PARA_ERROR_LOG("%s\n", errctx);
433 free(errctx);
434 return ret;
435 }
436 cmd = lls_cmd(ret, mixer_suite);
437 if (lls_opt_given(r_l))
438 txt = lls_long_help(cmd);
439 else
440 txt = lls_short_help(cmd);
441 printf("%s", txt);
442 free(txt);
443 return 0;
444 }
445 EXPORT_CMD(help);
446
447 static void handle_help_flags(void)
448 {
449 char *help;
450
451 if (OPT_GIVEN(PARA_MIXER, DETAILED_HELP))
452 help = lls_long_help(CMD_PTR(PARA_MIXER));
453 else if (OPT_GIVEN(PARA_MIXER, HELP))
454 help = lls_short_help(CMD_PTR(PARA_MIXER));
455 else
456 return;
457 printf("%s", help);
458 free(help);
459 show_subcommands();
460 exit(EXIT_SUCCESS);
461 }
462
463 static int parse_and_merge_config_file(const struct lls_command *cmd)
464 {
465 int ret;
466 int cf_argc;
467 char **cf_argv;
468 char *cf, *errctx = NULL;
469 struct lls_parse_result **lprp, *cf_lpr, *merged_lpr;
470 void *map;
471 size_t sz;
472 const char *subcmd_name;
473
474 if (cmd == lls_cmd(0, mixer_suite)) {
475 lprp = &lpr;
476 subcmd_name = NULL;
477 } else {
478 lprp = &sub_lpr;
479 subcmd_name = lls_command_name(cmd);
480 }
481 if (OPT_GIVEN(PARA_MIXER, CONFIG_FILE))
482 cf = para_strdup(OPT_STRING_VAL(PARA_MIXER, CONFIG_FILE));
483 else {
484 char *home = para_homedir();
485 cf = make_message("%s/.paraslash/mixer.conf", home);
486 free(home);
487 }
488 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
489 if (ret < 0) {
490 if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT))
491 goto free_cf;
492 if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) &&
493 OPT_GIVEN(PARA_MIXER, CONFIG_FILE))
494 goto free_cf;
495 } else {
496 ret = lls(lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx));
497 para_munmap(map, sz);
498 if (ret < 0)
499 goto free_cf;
500 cf_argc = ret;
501 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx));
502 lls_free_argv(cf_argv);
503 if (ret < 0)
504 goto free_cf;
505 ret = lls(lls_merge(*lprp, cf_lpr, cmd, &merged_lpr, &errctx));
506 lls_free_parse_result(cf_lpr, cmd);
507 if (ret < 0)
508 goto free_cf;
509 lls_free_parse_result(*lprp, cmd);
510 *lprp = merged_lpr;
511 loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
512 }
513 ret = 1;
514 free_cf:
515 free(cf);
516 if (errctx)
517 PARA_ERROR_LOG("%s\n", errctx);
518 free(errctx);
519 return ret;
520 }
521
522 /**
523 * The main function of para_mixer.
524 *
525 * The executable is linked with the alsa or the oss mixer API, or both. It has
526 * a custom log function which prefixes log messages with the current date.
527 *
528 * \param argc Argument counter.
529 * \param argv Argument vector.
530 *
531 * \return EXIT_SUCCESS or EXIT_FAILURE.
532 */
533 int main(int argc, char *argv[])
534 {
535 const struct lls_command *cmd = CMD_PTR(PARA_MIXER);
536 int ret;
537 char *errctx;
538 const char *subcmd;
539 struct mixer *m;
540 struct mixer_handle *h;
541 unsigned n;
542
543 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
544 if (ret < 0)
545 goto fail;
546 loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
547 version_handle_flag("mixer", OPT_GIVEN(PARA_MIXER, VERSION));
548 handle_help_flags();
549
550 n = lls_num_inputs(lpr);
551 if (n == 0) {
552 show_subcommands();
553 ret = 0;
554 goto free_lpr;
555 }
556 ret = parse_and_merge_config_file(cmd);
557 if (ret < 0)
558 goto free_lpr;
559 subcmd = lls_input(0, lpr);
560 ret = lls(lls_lookup_subcmd(subcmd, mixer_suite, &errctx));
561 if (ret < 0)
562 goto fail;
563 cmd = lls_cmd(ret, mixer_suite);
564 ret = lls(lls_parse(n, argv + argc - n, cmd, &sub_lpr, &errctx));
565 if (ret < 0)
566 goto free_lpr;
567 ret = parse_and_merge_config_file(cmd);
568 if (ret < 0)
569 goto free_lpr;
570 init_mixers();
571 m = get_mixer_or_die();
572 ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), &h);
573 if (ret < 0)
574 goto free_sub_lpr;
575 ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
576 if (ret == -E_BAD_CHANNEL) {
577 char *channels = m->get_channels(h);
578 printf("Available channels: %s\n", channels);
579 free(channels);
580 }
581 if (ret < 0)
582 goto close_mixer;
583 ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m ,h);
584 close_mixer:
585 m->close(&h);
586 free_sub_lpr:
587 lls_free_parse_result(sub_lpr, cmd);
588 free_lpr:
589 lls_free_parse_result(lpr, CMD_PTR(PARA_MIXER));
590 if (ret >= 0)
591 return EXIT_SUCCESS;
592 fail:
593 if (errctx)
594 PARA_ERROR_LOG("%s\n", errctx);
595 free(errctx);
596 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
597 return EXIT_FAILURE;
598 }