]> git.tuebingen.mpg.de Git - paraslash.git/blob - mixer.c
mixer: sleep: Add --initial-mood and --initial-delay.
[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 *initial_mood = OPT_STRING_VAL(SLEEP, INITIAL_MOOD);
274         const char *fo_mood = OPT_STRING_VAL(SLEEP, FO_MOOD);
275         const char *fi_mood = OPT_STRING_VAL(SLEEP, FI_MOOD);
276         const char *sleep_mood = OPT_STRING_VAL(SLEEP, SLEEP_MOOD);
277         int fit = OPT_UINT32_VAL(SLEEP, FI_TIME);
278         int fot = OPT_UINT32_VAL(SLEEP, FO_TIME);
279         int fiv = OPT_UINT32_VAL(SLEEP, FI_VOL);
280         int fov = OPT_UINT32_VAL(SLEEP, FO_VOL);
281         int32_t hour, min = 0;
282         char *tmp, *wt;
283         struct mixer_handle *h;
284
285         ret = open_mixer_and_set_channel(m, &h);
286         if (ret < 0)
287                 return ret;
288         wt = para_strdup(wake_time + (wake_time[0] == '+'));
289         /* calculate wake time */
290         time(&t1);
291         tmp = strchr(wt, ':');
292         if (tmp) {
293                 *tmp = '\0';
294                 tmp++;
295                 ret = para_atoi32(tmp, &min);
296                 if (ret < 0) {
297                         free(wt);
298                         goto close_mixer;
299                 }
300         }
301         ret = para_atoi32(wt, &hour);
302         free(wt);
303         if (ret < 0)
304                 goto close_mixer;
305         if (wake_time[0] == '+') { /* relative */
306                 t1 += hour * 60 * 60 + min * 60;
307                 tm = localtime(&t1);
308         } else {
309                 tm = localtime(&t1);
310                 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
311                         t1 += 86400; /* wake time is tomorrow */
312                         tm = localtime(&t1);
313                 }
314                 tm->tm_hour = hour;
315                 tm->tm_min = min;
316                 tm->tm_sec = 0;
317         }
318         wake_time_epoch = mktime(tm);
319         PARA_INFO_LOG("waketime: %d:%02d\n", tm->tm_hour, tm->tm_min);
320         client_cmd("stop");
321         sleep(1);
322         ret = set_initial_volume(m, h);
323         if (ret < 0)
324                 goto close_mixer;
325         /*
326          * Setting the volume invalidates the current channel setting, so we
327          * have to set it again.
328          */
329         ret = set_channel(m, h, OPT_STRING_VAL(PARA_MIXER, MIXER_CHANNEL));
330         if (ret < 0)
331                 goto close_mixer;
332         delay = OPT_UINT32_VAL(SLEEP, INITIAL_DELAY);
333         if (delay > 0 && initial_mood && *initial_mood) {
334                 change_afs_mode(initial_mood);
335                 client_cmd("play");
336                 sleep(delay);
337                 client_cmd("stop");
338         }
339         if (fot && fo_mood && *fo_mood) {
340                 change_afs_mode(fo_mood);
341                 client_cmd("play");
342                 ret = fade(m, h, fov, fot);
343                 if (ret < 0)
344                         goto close_mixer;
345         } else {
346                 ret = m->set(h, fov);
347                 if (ret < 0)
348                         goto close_mixer;
349         }
350         if (sleep_mood && *sleep_mood) {
351                 change_afs_mode(sleep_mood);
352                 if (!fot || !fo_mood) /* currently stopped */
353                         client_cmd("play");
354         } else if (fot && fo_mood && *fo_mood) /* currently playing */
355                 client_cmd("stop");
356         m->close(&h);
357         if (!fit || !fi_mood || !*fi_mood) /* nothing to do */
358                 return 1;
359         for (;;) {
360                 time(&t1);
361                 if (wake_time_epoch <= t1 + fit)
362                         break;
363                 delay = wake_time_epoch - t1 - fit;
364                 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
365                         delay, delay / 3600,
366                         (delay % 3600) / 60);
367                 sleep(delay);
368         }
369         change_afs_mode(fi_mood);
370         if (sleep_mood && *sleep_mood) /* currently playing */
371                 client_cmd("next");
372         else /* currently stopped */
373                 client_cmd("play");
374         ret = open_mixer_and_set_channel(m, &h);
375         if (ret < 0)
376                 return ret;
377         ret = fade(m, h, fiv, fit);
378 close_mixer:
379         m->close(&h);
380         return ret;
381 }
382 EXPORT_CMD(sleep);
383
384 static int com_snooze(const struct mixer *m)
385 {
386         int ret, val;
387         struct mixer_handle *h;
388
389         ret = open_mixer_and_set_channel(m, &h);
390         if (ret < 0)
391                 return ret;
392         ret = 1;
393         if (OPT_UINT32_VAL(SNOOZE, SO_TIME) == 0)
394                 goto close_mixer;
395         ret = m->get(h);
396         if (ret < 0)
397                 goto close_mixer;
398         val = ret;
399         if (val < OPT_UINT32_VAL(SNOOZE, SO_VOL))
400                 ret = m->set(h, OPT_UINT32_VAL(SNOOZE, SO_VOL));
401         else
402                 ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SO_VOL),
403                         OPT_UINT32_VAL(SNOOZE, SO_TIME));
404         if (ret < 0)
405                 goto close_mixer;
406         client_cmd("pause");
407         PARA_NOTICE_LOG("%" PRIu32 " seconds snooze time...\n",
408                 OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
409         m->close(&h);
410         sleep(OPT_UINT32_VAL(SNOOZE, SNOOZE_TIME));
411         client_cmd("play");
412         ret = open_mixer_and_set_channel(m, &h);
413         if (ret < 0)
414                 goto close_mixer;
415         ret = fade(m, h, OPT_UINT32_VAL(SNOOZE, SI_VOL),
416                 OPT_UINT32_VAL(SNOOZE, SI_TIME));
417 close_mixer:
418         m->close(&h);
419         return ret;
420 }
421 EXPORT_CMD(snooze);
422
423 static int com_set(const struct mixer *m)
424 {
425         struct mixer_handle *h;
426         int ret;
427
428         ret = open_mixer_and_set_channel(m, &h);
429         if (ret < 0)
430                 return ret;
431         ret = m->set(h, OPT_UINT32_VAL(SET, VAL));
432         m->close(&h);
433         return ret;
434 }
435 EXPORT_CMD(set);
436
437 static const struct mixer *get_mixer_or_die(void)
438 {
439         int i;
440
441         if (!OPT_GIVEN(PARA_MIXER, MIXER_API))
442                 i = 0; /* default: use first mixer */
443         else
444                 FOR_EACH_MIXER(i)
445                         if (!strcmp(mixers[i]->name,
446                                         OPT_STRING_VAL(PARA_MIXER, MIXER_API)))
447                                 break;
448         if (i < NUM_SUPPORTED_MIXERS) {
449                 PARA_NOTICE_LOG("using %s mixer API\n", mixers[i]->name);
450                 return mixers[i];
451         }
452         printf("available mixer APIs: ");
453         FOR_EACH_MIXER(i)
454                 printf("%s%s%s ", i == 0? "[" : "", mixers[i]->name,
455                         i == 0? "]" : "");
456         printf("\n");
457         exit(EXIT_FAILURE);
458 }
459
460 static void show_subcommands(void)
461 {
462         const struct lls_command *cmd;
463         int i;
464         printf("Subcommands:\n");
465         for (i = 1; (cmd = lls_cmd(i, mixer_suite)); i++) {
466                 const char *name = lls_command_name(cmd);
467                 const char *purpose = lls_purpose(cmd);
468                 printf("%-20s%s\n", name, purpose);
469         }
470 }
471
472 static int com_help(__a_unused const struct mixer *m)
473 {
474         const struct lls_command *cmd;
475         const struct lls_opt_result *r_l = OPT_RESULT(HELP, LONG);
476         char *txt, *errctx;
477         const char *name;
478         int ret;
479
480         ret = lls_check_arg_count(sub_lpr, 0, 1, NULL);
481         if (ret < 0)
482                 return ret;
483         if (lls_num_inputs(sub_lpr) == 0) {
484                 show_subcommands();
485                 return 0;
486         }
487         name = lls_input(0, sub_lpr);
488         ret = lls(lls_lookup_subcmd(name, mixer_suite, &errctx));
489         if (ret < 0) {
490                 if (errctx)
491                         PARA_ERROR_LOG("%s\n", errctx);
492                 free(errctx);
493                 return ret;
494         }
495         cmd = lls_cmd(ret, mixer_suite);
496         if (lls_opt_given(r_l))
497                 txt = lls_long_help(cmd);
498         else
499                 txt = lls_short_help(cmd);
500         printf("%s", txt);
501         free(txt);
502         return 0;
503 }
504 EXPORT_CMD(help);
505
506 static void handle_help_flags(void)
507 {
508         char *help;
509
510         if (OPT_GIVEN(PARA_MIXER, DETAILED_HELP))
511                 help = lls_long_help(CMD_PTR(PARA_MIXER));
512         else if (OPT_GIVEN(PARA_MIXER, HELP))
513                 help = lls_short_help(CMD_PTR(PARA_MIXER));
514         else
515                 return;
516         printf("%s", help);
517         free(help);
518         show_subcommands();
519         exit(EXIT_SUCCESS);
520 }
521
522 static int parse_and_merge_config_file(const struct lls_command *cmd)
523 {
524         int ret;
525         struct lls_parse_result **lprp = (cmd == lls_cmd(0, mixer_suite))?
526                 &lpr : &sub_lpr;
527
528         ret = lsu_merge_config_file_options(OPT_STRING_VAL(PARA_MIXER,
529                 CONFIG_FILE), "mixer.conf", lprp, cmd, mixer_suite,
530                 0 /* flags */);
531         if (ret < 0)
532                 return ret;
533         loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
534         return 1;
535 }
536
537 /**
538  * The main function of para_mixer.
539  *
540  * The executable is linked with the alsa or the oss mixer API, or both. It has
541  * a custom log function which prefixes log messages with the current date.
542  *
543  * \param argc Argument counter.
544  * \param argv Argument vector.
545  *
546  * \return EXIT_SUCCESS or EXIT_FAILURE.
547  */
548 int main(int argc, char *argv[])
549 {
550         const struct lls_command *cmd = CMD_PTR(PARA_MIXER);
551         int ret;
552         char *errctx;
553         const char *subcmd;
554         const struct mixer *m;
555         unsigned n;
556
557         ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
558         if (ret < 0)
559                 goto fail;
560         loglevel = OPT_UINT32_VAL(PARA_MIXER, LOGLEVEL);
561         version_handle_flag("mixer", OPT_GIVEN(PARA_MIXER, VERSION));
562         handle_help_flags();
563
564         n = lls_num_inputs(lpr);
565         if (n == 0) {
566                 show_subcommands();
567                 ret = 0;
568                 goto free_lpr;
569         }
570         ret = parse_and_merge_config_file(cmd);
571         if (ret < 0)
572                 goto free_lpr;
573         subcmd = lls_input(0, lpr);
574         ret = lls(lls_lookup_subcmd(subcmd, mixer_suite, &errctx));
575         if (ret < 0)
576                 goto fail;
577         cmd = lls_cmd(ret, mixer_suite);
578         ret = lls(lls_parse(n, argv + argc - n, cmd, &sub_lpr, &errctx));
579         if (ret < 0)
580                 goto free_lpr;
581         ret = parse_and_merge_config_file(cmd);
582         if (ret < 0)
583                 goto free_sub_lpr;
584         m = get_mixer_or_die();
585         ret = (*(mixer_subcommand_handler_t *)(lls_user_data(cmd)))(m);
586 free_sub_lpr:
587         lls_free_parse_result(sub_lpr, cmd);
588 free_lpr:
589         lls_free_parse_result(lpr, CMD_PTR(PARA_MIXER));
590         if (ret >= 0)
591                 return EXIT_SUCCESS;
592 fail:
593         if (errctx)
594                 PARA_ERROR_LOG("%s\n", errctx);
595         free(errctx);
596         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
597         return EXIT_FAILURE;
598 }