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