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