Merge branch 'refs/heads/t/gui-sigwinch'
[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 cmd = make_message("select %s", afs_mode);
196 client_cmd(cmd);
197 free(cmd);
198 }
199
200 static int set_initial_volume(const struct mixer *m, struct mixer_handle *h)
201 {
202 int i, ret;
203
204 for (i = 0; i < OPT_GIVEN(SLEEP, IVOL); i++) {
205 const char *val = lls_string_val(i, OPT_RESULT(SLEEP, IVOL));
206 char *p, *ch, *arg = para_strdup(val);
207 int32_t iv;
208 p = strchr(arg, ':');
209 if (p) {
210 *p = '\0';
211 p++;
212 ch = arg;
213 } else {
214 p = arg;
215 ch = NULL;
216 }
217 ret = para_atoi32(p, &iv);
218 if (ret < 0) {
219 free(arg);
220 return ret;
221 }
222 ret = set_channel(m, h, ch);
223 if (!ch)
224 ch = "default";
225 if (ret < 0) {
226 PARA_WARNING_LOG("ignoring channel %s\n", ch);
227 ret = 0;
228 } else {
229 PARA_INFO_LOG("initial volume %s: %d\n", ch, iv);
230 ret = m->set(h, iv);
231 }
232 free(arg);
233 if (ret < 0)
234 return ret;
235 }
236 return 1;
237 }
238
239 static int com_sleep(const struct mixer *m, struct mixer_handle *h)
240 {
241 time_t t1, wake_time_epoch;
242 unsigned int delay;
243 struct tm *tm;
244 int ret;
245 const char *wake_time = OPT_STRING_VAL(SLEEP, WAKE_TIME);
246 const char *fo_mood = OPT_STRING_VAL(SLEEP, FO_MOOD);
247 const char *fi_mood = OPT_STRING_VAL(SLEEP, FI_MOOD);
248 const char *sleep_mood = OPT_STRING_VAL(SLEEP, SLEEP_MOOD);
249 int fit = OPT_UINT32_VAL(SLEEP, FI_TIME);
250 int fot = OPT_UINT32_VAL(SLEEP, FO_TIME);
251 int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL);
252 int fov = OPT_UINT32_VAL(SLEEP, FO_VOL);
253 int32_t hour, min = 0;
254 char *tmp;
255 char *wt = para_strdup(wake_time + (wake_time[0] == '+'));
256
257 /* calculate wake time */
258 time(&t1);
259 tmp = strchr(wt, ':');
260 if (tmp) {
261 *tmp = '\0';
262 tmp++;
263 ret = para_atoi32(tmp, &min);
264 if (ret < 0) {
265 free(wt);
266 return ret;
267 }
268 }
269 ret = para_atoi32(wt, &hour);
270 free(wt);
271 if (ret < 0)
272 return ret;
273 if (wake_time[0] == '+') { /* relative */
274 t1 += hour * 60 * 60 + min * 60;
275 tm = localtime(&t1);
276 } else {
277 tm = localtime(&t1);
278 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
279 t1 += 86400; /* wake time is tomorrow */
280 tm = localtime(&t1);
281 }
282 tm->tm_hour = hour;
283 tm->tm_min = min;
284 tm->tm_sec = 0;
285 }
286 wake_time_epoch = mktime(tm);
287 PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);
288 client_cmd("stop");
289 sleep(1);
290 if (fot && fo_mood) {
291 ret = set_initial_volume(m, h);
292 if (ret < 0)
293 return ret;
294 change_afs_mode(fo_mood);
295 client_cmd("play");
296 ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
297 if (ret < 0)
298 return ret;
299 ret = fade(m, h, fov, fot);
300 if (ret < 0)
301 return ret;
302 } else {
303 ret = m->set(h, fov);
304 if (ret < 0)
305 return ret;
306 }
307 if (sleep_mood) {
308 change_afs_mode(sleep_mood);
309 if (!fot || !fo_mood) /* currently stopped */
310 client_cmd("play");
311 } else if (fot && fo_mood) /* currently playing */
312 client_cmd("stop");
313 if (!fit || !fi_mood) /* nothing to do */
314 return 1;
315 change_afs_mode(fi_mood);
316 for (;;) {
317 time(&t1);
318 if (wake_time_epoch <= t1 + fit)
319 break;
320 delay = wake_time_epoch - t1 - fit;
321 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
322 delay, delay / 3600,
323 (delay % 3600) / 60);
324 sleep(delay);
325 }
326 client_cmd("play");
327 ret = fade(m, h, fiv, fit);
328 PARA_INFO_LOG("fade complete, returning\n");
329 return ret;
330 }
331 EXPORT_CMD(sleep);
332
333 static int com_snooze(const struct mixer *m, struct mixer_handle *h)
334 {
335 int ret, val;
336
337 if (OPT_UINT32_VAL(SNOOZE, SO_TIME) == 0)
338 return 1;
339 ret = m->get(h);
340 if (ret < 0)
341 return ret;
342 val = ret;
343 if (val < OPT_UINT32_VAL(SNOOZE, SO_VOL))
344 ret = m->set(h, OPT_UINT32_VAL(SNOOZE, SO_VOL));
345 else
346 ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SO_VOL),
347 OPT_UINT32_VAL(SNOOZE, SO_TIME));
348 if (ret < 0)
349 return ret;
350 client_cmd("pause");
351 PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
352 OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
353 sleep(OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
354 client_cmd("play");
355 return fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
356 OPT_UINT32_VAL(SNOOZE, SI_TIME));
357 }
358 EXPORT_CMD(snooze);
359
360 static int com_set(const struct mixer *m, struct mixer_handle *h)
361 {
362 return m->set(h, OPT_UINT32_VAL(SET, VAL));
363 }
364 EXPORT_CMD(set);
365
366 static const struct mixer *get_mixer_or_die(void)
367 {
368 int i;
369
370 if (!OPT_GIVEN(PARA_MIXER, MIXER_API))
371 i = 0; /* default: use first mixer */
372 else
373 FOR_EACH_MIXER(i)
374 if (!strcmp(mixers[i]->name,
375 OPT_STRING_VAL(PARA_MIXER, MIXER_API)))
376 break;
377 if (i < NUM_SUPPORTED_MIXERS) {
378 PARA_NOTICE_LOG("using %s mixer API\n", mixers[i]->name);
379 return mixers[i];
380 }
381 printf("available mixer APIs: ");
382 FOR_EACH_MIXER(i)
383 printf("%s%s%s ", i == 0? "[" : "", mixers[i]->name,
384 i == 0? "]" : "");
385 printf("\n");
386 exit(EXIT_FAILURE);
387 }
388
389 static void show_subcommands(void)
390 {
391 const struct lls_command *cmd;
392 int i;
393 printf("Subcommands:\n");
394 for (i = 1; (cmd = lls_cmd(i, mixer_suite)); i++) {
395 const char *name = lls_command_name(cmd);
396 const char *purpose = lls_purpose(cmd);
397 printf("%-20s%s\n", name, purpose);
398 }
399 }
400
401
402 static int com_help(__a_unused const struct mixer *m,
403 __a_unused struct mixer_handle *h)
404 {
405 const struct lls_command *cmd;
406 const struct lls_opt_result *r_l = OPT_RESULT(HELP, LONG);
407 char *txt, *errctx;
408 const char *name;
409 int ret;
410
411 ret = lls_check_arg_count(sub_lpr, 0, 1, NULL);
412 if (ret < 0)
413 return ret;
414 if (lls_num_inputs(sub_lpr) == 0) {
415 show_subcommands();
416 return 0;
417 }
418 name = lls_input(0, sub_lpr);
419 ret = lls(lls_lookup_subcmd(name, mixer_suite, &errctx));
420 if (ret < 0) {
421 if (errctx)
422 PARA_ERROR_LOG("%s\n", errctx);
423 free(errctx);
424 return ret;
425 }
426 cmd = lls_cmd(ret, mixer_suite);
427 if (lls_opt_given(r_l))
428 txt = lls_long_help(cmd);
429 else
430 txt = lls_short_help(cmd);
431 printf("%s", txt);
432 free(txt);
433 return 0;
434 }
435 EXPORT_CMD(help);
436
437 static void handle_help_flags(void)
438 {
439 char *help;
440
441 if (OPT_GIVEN(PARA_MIXER, DETAILED_HELP))
442 help = lls_long_help(CMD_PTR(PARA_MIXER));
443 else if (OPT_GIVEN(PARA_MIXER, HELP))
444 help = lls_short_help(CMD_PTR(PARA_MIXER));
445 else
446 return;
447 printf("%s", help);
448 free(help);
449 show_subcommands();
450 exit(EXIT_SUCCESS);
451 }
452
453 static int parse_and_merge_config_file(const struct lls_command *cmd)
454 {
455 int ret;
456 int cf_argc;
457 char **cf_argv;
458 char *cf, *errctx = NULL;
459 struct lls_parse_result **lprp, *cf_lpr, *merged_lpr;
460 void *map;
461 size_t sz;
462 const char *subcmd_name;
463
464 if (cmd == lls_cmd(0, mixer_suite)) {
465 lprp = &lpr;
466 subcmd_name = NULL;
467 } else {
468 lprp = &sub_lpr;
469 subcmd_name = lls_command_name(cmd);
470 }
471 if (OPT_GIVEN(PARA_MIXER, CONFIG_FILE))
472 cf = para_strdup(OPT_STRING_VAL(PARA_MIXER, CONFIG_FILE));
473 else {
474 char *home = para_homedir();
475 cf = make_message("%s/.paraslash/mixer.conf", home);
476 free(home);
477 }
478 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
479 if (ret < 0) {
480 if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT))
481 goto free_cf;
482 if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) &&
483 OPT_GIVEN(PARA_MIXER, CONFIG_FILE))
484 goto free_cf;
485 } else {
486 ret = lls(lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx));
487 para_munmap(map, sz);
488 if (ret < 0)
489 goto free_cf;
490 cf_argc = ret;
491 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx));
492 lls_free_argv(cf_argv);
493 if (ret < 0)
494 goto free_cf;
495 ret = lls(lls_merge(*lprp, cf_lpr, cmd, &merged_lpr, &errctx));
496 lls_free_parse_result(cf_lpr, cmd);
497 if (ret < 0)
498 goto free_cf;
499 lls_free_parse_result(*lprp, cmd);
500 *lprp = merged_lpr;
501 loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
502 }
503 ret = 1;
504 free_cf:
505 free(cf);
506 if (errctx)
507 PARA_ERROR_LOG("%s\n", errctx);
508 free(errctx);
509 return ret;
510 }
511
512 /**
513 * The main function of para_mixer.
514 *
515 * The executable is linked with the alsa or the oss mixer API, or both. It has
516 * a custom log function which prefixes log messages with the current date.
517 *
518 * \param argc Argument counter.
519 * \param argv Argument vector.
520 *
521 * \return EXIT_SUCCESS or EXIT_FAILURE.
522 */
523 int main(int argc, char *argv[])
524 {
525 const struct lls_command *cmd = CMD_PTR(PARA_MIXER);
526 int ret;
527 char *errctx;
528 const char *subcmd;
529 const struct mixer *m;
530 struct mixer_handle *h;
531 unsigned n;
532
533 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
534 if (ret < 0)
535 goto fail;
536 loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
537 version_handle_flag("mixer", OPT_GIVEN(PARA_MIXER, VERSION));
538 handle_help_flags();
539
540 n = lls_num_inputs(lpr);
541 if (n == 0) {
542 show_subcommands();
543 ret = 0;
544 goto free_lpr;
545 }
546 ret = parse_and_merge_config_file(cmd);
547 if (ret < 0)
548 goto free_lpr;
549 subcmd = lls_input(0, lpr);
550 ret = lls(lls_lookup_subcmd(subcmd, mixer_suite, &errctx));
551 if (ret < 0)
552 goto fail;
553 cmd = lls_cmd(ret, mixer_suite);
554 ret = lls(lls_parse(n, argv + argc - n, cmd, &sub_lpr, &errctx));
555 if (ret < 0)
556 goto free_lpr;
557 ret = parse_and_merge_config_file(cmd);
558 if (ret < 0)
559 goto free_lpr;
560 m = get_mixer_or_die();
561 ret = m->open(OPT_STRING_VAL(PARA_MIXER, MIXER_DEVICE), &h);
562 if (ret < 0)
563 goto free_sub_lpr;
564 ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
565 if (ret == -E_BAD_CHANNEL) {
566 char *channels = m->get_channels(h);
567 printf("Available channels: %s\n", channels);
568 free(channels);
569 }
570 if (ret < 0)
571 goto close_mixer;
572 ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m ,h);
573 close_mixer:
574 m->close(&h);
575 free_sub_lpr:
576 lls_free_parse_result(sub_lpr, cmd);
577 free_lpr:
578 lls_free_parse_result(lpr, CMD_PTR(PARA_MIXER));
579 if (ret >= 0)
580 return EXIT_SUCCESS;
581 fail:
582 if (errctx)
583 PARA_ERROR_LOG("%s\n", errctx);
584 free(errctx);
585 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
586 return EXIT_FAILURE;
587 }