task_register() conversion: writers
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write.c Paraslash's standalone wav/raw player. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "write.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "stdin.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "fd.h"
23 #include "error.h"
24 #include "version.h"
25 #include "check_wav.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 static struct write_args_info conf;
30
31 static struct stdin_task sit;
32
33 static int loglevel;
34 INIT_STDERR_LOGGING(loglevel)
35
36 __noreturn static void print_help_and_die(void)
37 {
38 struct ggo_help h = DEFINE_GGO_HELP(write);
39 bool d = conf.detailed_help_given;
40
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);
43 exit(0);
44 }
45
46 /*
47 * Parse config and register a task for a writer node.
48 *
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.
52 *
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.
56 *
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.
60 *
61 * Finally, the new writer node's task structure is initialized and registered
62 * to the paraslash scheduler.
63 *
64 * \return Standard.
65 */
66 static void setup_writer_node(const char *arg, struct btr_node *parent,
67 struct writer_node *wn, struct sched *s)
68 {
69 wn->conf = check_writer_arg_or_die(arg, &wn->writer_num);
70 register_writer_node(wn, parent, s);
71 }
72
73 struct write_task {
74 struct task *task;
75 struct check_wav_context *cwc;
76 };
77
78 static void write_pre_select(struct sched *s, struct task *t)
79 {
80 struct write_task *wt = task_context(t);
81 check_wav_pre_select(s, wt->cwc);
82 }
83
84 static int write_post_select(__a_unused struct sched *s, struct task *t)
85 {
86 struct write_task *wt = task_context(t);
87 return check_wav_post_select(wt->cwc);
88 }
89
90 static int setup_and_schedule(void)
91 {
92 int i, ret;
93 struct btr_node *cw_btrn;
94 struct writer_node *wns;
95 static struct sched s;
96 struct wav_params wp;
97 struct write_task wt;
98
99 sit.btrn = btr_new_node(&(struct btr_node_description)
100 EMBRACE(.name = "stdin"));
101 stdin_task_register(&sit, &s);
102
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) {
106 .name = "write",
107 .pre_select = write_pre_select,
108 .post_select = write_post_select,
109 .context = &wt,
110 }, &s);
111 if (!conf.writer_given) {
112 wns = para_calloc(sizeof(*wns));
113 setup_writer_node(NULL, cw_btrn, wns, &s);
114 i = 1;
115 } else {
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,
119 wns + i, &s);
120 }
121
122 s.default_timeout.tv_sec = 10;
123 s.default_timeout.tv_usec = 50000;
124 ret = schedule(&s);
125 if (ret >= 0) {
126 int j;
127 for (j = 0; j < i; j++) {
128 struct task *t = wns[j].task;
129 assert(t->error < 0);
130 if (t->error != -E_WRITE_COMMON_EOF
131 && t->error != -E_BTR_EOF) {
132 PARA_ERROR_LOG("%s: %s\n", t->status,
133 para_strerror(-t->error));
134 if (ret >= 0)
135 ret = t->error;
136 }
137 }
138 }
139 for (i--; i >= 0; i--) {
140 struct writer_node *wn = wns + i;
141 struct writer *w = writers + wn->writer_num;
142
143 w->close(wn);
144 btr_remove_node(&wn->btrn);
145 w->free_config(wn->conf);
146 free(wn->conf);
147 }
148 free(wns);
149 check_wav_shutdown(wt.cwc);
150 sched_shutdown(&s);
151 return ret;
152 }
153
154 /**
155 * Para_write's main function.
156 *
157 * \param argc The usual argument counter.
158 * \param argv The usual argument vector.
159 *
160 * It sets up and starts the tasks and the buffer tree nodes determined by
161 * command line options.
162 *
163 * \return \p EXIT_SUCCESS or EXIT_FAILURE
164 */
165 int main(int argc, char *argv[])
166 {
167 int ret;
168
169 write_cmdline_parser(argc, argv, &conf);
170 loglevel = get_loglevel_by_name(conf.loglevel_arg);
171 writer_init();
172 version_handle_flag("write", conf.version_given);
173 if (conf.help_given || conf.detailed_help_given)
174 print_help_and_die();
175
176 ret = setup_and_schedule();
177 if (ret < 0) {
178 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
179 exit(EXIT_FAILURE);
180 }
181 exit(EXIT_SUCCESS);
182 }