2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
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"
29 static struct write_args_info conf;
31 static struct stdin_task sit;
34 INIT_STDERR_LOGGING(loglevel)
36 __noreturn static void print_help_and_die(void)
38 struct ggo_help h = DEFINE_GGO_HELP(write);
39 bool d = conf.detailed_help_given;
41 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
42 print_writer_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
47 * Parse config and register a task for a writer node.
49 * \param arg Command line arguments.
50 * \param parent The new node will be a child of \a parent.
51 * \param wn The writer node.
53 * If arg is \p NULL, the OS-dependent default writer is used with no
54 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
55 * *BSD, and the file writer if the default writer is not supported.
57 * Once the writer configuration has been retrieved from the ->parse_config
58 * callback a writer node is created, its buffer tree node is added to the
59 * buffer tree as a child of the given parent.
61 * Finally, the new writer node's task structure is initialized and registered
62 * to the paraslash scheduler.
66 static void setup_writer_node(const char *arg, struct btr_node *parent,
67 struct writer_node *wn, struct sched *s)
69 wn->conf = check_writer_arg_or_die(arg, &wn->writer_num);
70 register_writer_node(wn, parent, s);
75 struct check_wav_context *cwc;
78 static void write_pre_select(struct sched *s, struct task *t)
80 struct write_task *wt = task_context(t);
81 check_wav_pre_select(s, wt->cwc);
84 static int write_post_select(__a_unused struct sched *s, struct task *t)
86 struct write_task *wt = task_context(t);
87 return check_wav_post_select(wt->cwc);
90 static int setup_and_schedule(void)
93 struct btr_node *cw_btrn;
94 struct writer_node *wns;
95 static struct sched s;
99 sit.btrn = btr_new_node(&(struct btr_node_description)
100 EMBRACE(.name = "stdin"));
101 stdin_task_register(&sit, &s);
103 COPY_WAV_PARMS(&wp, &conf);
104 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
105 wt.task = task_register(&(struct task_info) {
107 .pre_select = write_pre_select,
108 .post_select = write_post_select,
111 if (!conf.writer_given) {
112 wns = para_calloc(sizeof(*wns));
113 setup_writer_node(NULL, cw_btrn, wns, &s);
116 wns = para_calloc(conf.writer_given * sizeof(*wns));
117 for (i = 0; i < conf.writer_given; i++)
118 setup_writer_node(conf.writer_arg[i], cw_btrn,
122 s.default_timeout.tv_sec = 10;
123 s.default_timeout.tv_usec = 50000;
127 for (j = 0; j < i; j++) {
128 struct writer_node *wn = wns + j;
129 ts = task_status(wn->task);
131 if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
132 const char *name = writer_names[wn->writer_num];
133 PARA_ERROR_LOG("%s: %s\n", name,
140 for (i--; i >= 0; i--) {
141 struct writer_node *wn = wns + i;
142 struct writer *w = writers + wn->writer_num;
145 btr_remove_node(&wn->btrn);
146 w->free_config(wn->conf);
150 check_wav_shutdown(wt.cwc);
156 * Para_write's main function.
158 * \param argc The usual argument counter.
159 * \param argv The usual argument vector.
161 * It sets up and starts the tasks and the buffer tree nodes determined by
162 * command line options.
164 * \return \p EXIT_SUCCESS or EXIT_FAILURE
166 int main(int argc, char *argv[])
170 write_cmdline_parser(argc, argv, &conf);
171 loglevel = get_loglevel_by_name(conf.loglevel_arg);
173 version_handle_flag("write", conf.version_given);
174 if (conf.help_given || conf.detailed_help_given)
175 print_help_and_die();
177 ret = setup_and_schedule();
179 PARA_ERROR_LOG("%s\n", para_strerror(-ret));