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