2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player. */
10 #include <sys/types.h>
14 #include "write.cmdline.h"
19 #include "buffer_tree.h"
21 #include "write_common.h"
25 #include "check_wav.h"
27 /** Array of error strings. */
30 static struct write_args_info conf;
32 static struct stdin_task sit;
35 INIT_STDERR_LOGGING(loglevel)
37 __noreturn static void print_help_and_die(void)
39 struct ggo_help h = DEFINE_GGO_HELP(write);
40 bool d = conf.detailed_help_given;
42 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
43 print_writer_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
48 * Parse config and register a task for a writer node.
50 * \param arg Command line arguments.
51 * \param parent The new node will be a child of \a parent.
52 * \param wn The writer node.
54 * If arg is \p NULL, the OS-dependent default writer is used with no
55 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
56 * *BSD, and the file writer if the default writer is not supported.
58 * Once the writer configuration has been retrieved from the ->parse_config
59 * callback a writer node is created, its buffer tree node is added to the
60 * buffer tree as a child of the given parent.
62 * Finally, the new writer node's task structure is initialized and registered
63 * to the paraslash scheduler.
67 static void setup_writer_node(const char *arg, struct btr_node *parent,
68 struct writer_node *wn, struct sched *s)
70 wn->conf = check_writer_arg_or_die(arg, &wn->writer_num);
71 register_writer_node(wn, parent, s);
76 struct check_wav_context *cwc;
79 static void write_pre_select(struct sched *s, void *context)
81 struct write_task *wt = context;
82 check_wav_pre_select(s, wt->cwc);
85 static int write_post_select(__a_unused struct sched *s, void *context)
87 struct write_task *wt = context;
88 return check_wav_post_select(wt->cwc);
91 static int setup_and_schedule(void)
94 struct btr_node *cw_btrn;
95 struct writer_node *wns;
96 static struct sched s;
100 sit.btrn = btr_new_node(&(struct btr_node_description)
101 EMBRACE(.name = "stdin"));
102 stdin_task_register(&sit, &s);
104 COPY_WAV_PARMS(&wp, &conf);
105 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
106 wt.task = task_register(&(struct task_info) {
108 .pre_select = write_pre_select,
109 .post_select = write_post_select,
112 if (!conf.writer_given) {
113 wns = para_calloc(sizeof(*wns));
114 setup_writer_node(NULL, cw_btrn, wns, &s);
117 wns = para_calloc(conf.writer_given * sizeof(*wns));
118 for (i = 0; i < conf.writer_given; i++)
119 setup_writer_node(conf.writer_arg[i], cw_btrn,
123 s.default_timeout.tv_sec = 10;
124 s.default_timeout.tv_usec = 50000;
128 for (j = 0; j < i; j++) {
129 struct writer_node *wn = wns + j;
130 ts = task_status(wn->task);
132 if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
133 const char *name = writer_names[wn->writer_num];
134 PARA_ERROR_LOG("%s: %s\n", name,
141 for (i--; i >= 0; i--) {
142 struct writer_node *wn = wns + i;
143 struct writer *w = writers + wn->writer_num;
146 btr_remove_node(&wn->btrn);
147 w->free_config(wn->conf);
151 check_wav_shutdown(wt.cwc);
157 * Para_write's main function.
159 * \param argc The usual argument counter.
160 * \param argv The usual argument vector.
162 * It sets up and starts the tasks and the buffer tree nodes determined by
163 * command line options.
165 * \return \p EXIT_SUCCESS or EXIT_FAILURE
167 int main(int argc, char *argv[])
171 write_cmdline_parser(argc, argv, &conf);
172 loglevel = get_loglevel_by_name(conf.loglevel_arg);
174 version_handle_flag("write", conf.version_given);
175 if (conf.help_given || conf.detailed_help_given)
176 print_help_and_die();
178 ret = setup_and_schedule();
180 PARA_ERROR_LOG("%s\n", para_strerror(-ret));