write/alsa: Add btr support.
[paraslash.git] / write.c
diff --git a/write.c b/write.c
index c692dc92fe1a77433aad26078879555a93bb39c6..21a0dad9253e66877ada6da5814062764d1269fb 100644 (file)
--- a/write.c
+++ b/write.c
@@ -1,24 +1,28 @@
 /*
- * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file write.c Paraslash's standalone wav/raw player. */
 
+#include <regex.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <stdbool.h>
 
 #include "para.h"
 #include "string.h"
 #include "write.cmdline.h"
 #include "list.h"
 #include "sched.h"
+#include "ggo.h"
 #include "stdin.h"
 #include "write.h"
 #include "write_common.h"
 #include "fd.h"
 #include "error.h"
+#include "buffer_tree.h"
 
 INIT_WRITE_ERRLISTS;
 
@@ -34,6 +38,7 @@ struct check_wav_task {
        unsigned channels;
        /** Sample rate specified in wav header given by \a buf. */
        unsigned samplerate;
+       /** The task structure used by the scheduler. */
        struct task task;
 };
 
@@ -105,17 +110,23 @@ static void initial_delay_pre_select(struct sched *s, struct task *t)
 
        if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
                t->error = -E_NO_DELAY;
-               return;
+               goto register_check_wav;
        }
        if (tv_diff(now, &idt->start_time, &diff) > 0) {
                t->error = -E_DELAY_TIMEOUT;
-               return;
+               goto register_check_wav;
        }
        if (tv_diff(&s->timeout , &diff, NULL) > 0)
                s->timeout = diff;
+       return;
+register_check_wav:
+       register_task(&the_check_wav_task.task);
+       s->timeout.tv_sec = 0;
+       s->timeout.tv_usec = 1;
 }
 
-INIT_STDERR_LOGGING(conf.loglevel_arg)
+static int loglevel;
+INIT_STDERR_LOGGING(loglevel)
 
 static struct writer_node_group *check_args(void)
 {
@@ -123,20 +134,7 @@ static struct writer_node_group *check_args(void)
        struct writer_node_group *g = NULL;
        struct initial_delay_task *idt = &the_initial_delay_task;
 
-       if (conf.list_writers_given) {
-               char *msg = NULL;
-               FOR_EACH_WRITER(i) {
-                       char *tmp = make_message("%s%s%s",
-                               i? msg : "",
-                               i? " " : "",
-                               writer_names[i]);
-                       free(msg);
-                       msg = tmp;
-               }
-               fprintf(stderr, "%s\n", msg);
-               free(msg);
-               exit(EXIT_SUCCESS);
-       }
+       loglevel = get_loglevel_by_name(conf.loglevel_arg);
        if (conf.start_time_given) {
                long unsigned sec, usec;
                if (sscanf(conf.start_time_arg, "%lu:%lu",
@@ -158,7 +156,7 @@ static struct writer_node_group *check_args(void)
                        conf.writer_arg[i], &writer_num);
                if (!g->writer_nodes[i].conf)
                        goto out;
-               g->writer_nodes[i].writer = &writers[writer_num];
+               g->writer_nodes[i].writer_num = writer_num;
        }
        ret = 1;
 out:
@@ -168,6 +166,51 @@ out:
        return NULL;
 }
 
+__noreturn static void print_help_and_die(void)
+{
+       int d = conf.detailed_help_given;
+       const char **p = d? write_args_info_detailed_help
+               : write_args_info_help;
+
+       printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
+               WRITE_CMDLINE_PARSER_VERSION);
+       printf_or_die("%s\n\n", write_args_info_usage);
+       for (; *p; p++)
+               printf_or_die("%s\n", *p);
+       print_writer_helps(d);
+       exit(0);
+}
+
+/*
+ TODO: check wav, initial delay, multiple writers, non-default writers
+ */
+static int main_btr(struct sched *s)
+{
+       struct writer_node *wn = para_malloc(sizeof(*wn));
+       struct writer *w = writers + DEFAULT_WRITER;
+       int ret;
+
+       wn->writer_num = DEFAULT_WRITER;
+       wn->conf = writers[DEFAULT_WRITER].parse_config("-B");
+       sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL);
+       stdin_set_defaults(&sit);
+       register_task(&sit.task);
+
+       wn->btrn = btr_new_node("writer", sit.btrn, NULL);
+
+       sprintf(wn->task.status, "some writer");
+       w->open(wn);
+       wn->task.post_select = w->post_select_btr;
+       wn->task.pre_select = w->pre_select_btr;
+       register_task(&wn->task);
+
+       s->default_timeout.tv_sec = 10;
+       s->default_timeout.tv_usec = 50000;
+       ret = schedule(s);
+       w->close(wn);
+       return ret;
+}
+
 /**
  * Para_write's main function.
  *
@@ -182,23 +225,33 @@ out:
 int main(int argc, char *argv[])
 {
        int ret = -E_WRITE_SYNTAX;
-       struct sched s;
+       static struct sched s;
        struct check_wav_task *cwt = &the_check_wav_task;
        struct initial_delay_task *idt = &the_initial_delay_task;
 
+       writer_init();
        write_cmdline_parser(argc, argv, &conf);
        HANDLE_VERSION_FLAG("write", conf);
-       init_supported_writers();
+       if (conf.help_given || conf.detailed_help_given)
+               print_help_and_die();
 
+       if (conf.buffer_tree_given) {
+               ret = main_btr(&s);
+               goto out;
+       }
        wng = check_args();
        if (!wng)
                goto out;
        stdin_set_defaults(&sit);
-       if (conf.bufsize_given)
-               sit.bufsize = conf.bufsize_arg;
+       ret = -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (conf.bufsize_arg < 0)
+               goto out;
+       if (conf.bufsize_arg >= INT_MAX / 1024)
+               goto out;
+       sit.bufsize = conf.bufsize_arg * 1024;
        sit.buf = para_malloc(sit.bufsize);
 
-       wng->buf = sit.buf;
+       wng->bufp = &sit.buf;
        wng->loaded = &sit.loaded;
        wng->input_error = &sit.task.error;
 
@@ -209,7 +262,6 @@ int main(int argc, char *argv[])
        cwt->input_error = &sit.task.error;
        sprintf(cwt->task.status, "check wav");
        cwt->task.pre_select = check_wav_pre_select;
-       register_task(&cwt->task);
 
        idt->task.pre_select = initial_delay_pre_select;
        sprintf(idt->task.status, "initial_delay");