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