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