Add execute handler to struct writer.
[paraslash.git] / write_common.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "write.h"
19 #include "error.h"
20
21 /** the array containing the names of all supported writers */
22 const char *writer_names[] ={WRITER_NAMES};
23
24 /** the array of supported writers */
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
26
27 static void wng_pre_select(struct sched *s, struct task *t)
28 {
29 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
30 int i;
31
32 FOR_EACH_WRITER_NODE(i, g) {
33 struct writer_node *wn = &g->writer_nodes[i];
34 struct writer *w = writers + wn->writer_num;
35 if (!w->pre_select)
36 continue;
37 t->error = w->pre_select(s, wn);
38 if (t->error < 0)
39 return;
40 }
41 /*
42 * Force a minimal delay if something was written during the previous
43 * call to wng_post_select(). This is necessary because the filter
44 * chain might still have data for us which it couldn't convert during
45 * the previous run due to its buffer size constraints. In this case we
46 * do not want to wait until the next input data arrives as this could
47 * lead to buffer underruns.
48 */
49 if (g->last_written == 0)
50 return;
51 s->timeout.tv_sec = 0;
52 s->timeout.tv_usec = 1;
53 }
54
55 static void wng_post_select(struct sched *s, struct task *t)
56 {
57 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
58 int i;
59 size_t min_written = 0, max_written = 0;
60
61 FOR_EACH_WRITER_NODE(i, g) {
62 struct writer_node *wn = &g->writer_nodes[i];
63 struct writer *w = writers + wn->writer_num;
64 t->error = w->post_select(s, wn);
65 if (t->error < 0)
66 return;
67 if (!i)
68 min_written = wn->written;
69 else
70 min_written = PARA_MIN(min_written, wn->written);
71 max_written = PARA_MAX(max_written, wn->written);
72 }
73 g->last_written = max_written;
74 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
75 if (min_written) {
76 *g->loaded -= min_written;
77 FOR_EACH_WRITER_NODE(i, g)
78 g->writer_nodes[i].written -= min_written;
79 }
80 if (!*g->loaded && *g->input_error) {
81 t->error = *g->input_error;
82 return;
83 }
84 if (*g->loaded && min_written) {
85 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
86 memmove(*g->bufp, *g->bufp + min_written, *g->loaded);
87 }
88 }
89
90 /**
91 * call the open function of each writer in the group
92 *
93 * \param g the writer node group
94 *
95 * \return If at least one open function returned an error, all successful
96 * writer notes get closed and this error value is returned. Upon success, a
97 * task associated with \a g is registered to the scheduler and the function
98 * returns a positive value.
99 * */
100 int wng_open(struct writer_node_group *g)
101 {
102 int i, ret = 1;
103
104 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
105 FOR_EACH_WRITER_NODE(i, g) {
106 struct writer_node *wn = &g->writer_nodes[i];
107 struct writer *w = writers + wn->writer_num;
108 wn->wng = g;
109 ret = w->open(wn);
110 if (ret < 0)
111 goto err_out;
112 }
113 sprintf(g->task.status, "%s", "writer node group");
114 register_task(&g->task);
115 g->open = 1;
116 return 1;
117 err_out:
118 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
119 while (i > 0) {
120 struct writer_node *wn = &g->writer_nodes[--i];
121 struct writer *w = writers + wn->writer_num;
122 w->close(wn);
123 }
124 free(g->writer_nodes);
125 g->num_writers = 0;
126 g->task.error = -E_TASK_UNREGISTERED;
127 return ret;
128 }
129
130 /**
131 * call the close function of each writer in the given group
132 *
133 * \param g the writer node group to close
134 *
135 * This function also frees all resources of the given group.
136 */
137 void wng_close(struct writer_node_group *g)
138 {
139 int i;
140
141 if (!g || !g->open)
142 return;
143 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
144 FOR_EACH_WRITER_NODE(i, g) {
145 struct writer_node *wn = &g->writer_nodes[i];
146 struct writer *w = writers + wn->writer_num;
147 w->close(wn);
148 }
149 free(g->writer_nodes);
150 free(g);
151 }
152
153 /**
154 * allocate and initialize a new writer_node_group struct
155 *
156 * \param num_writers the number of writer nodes for the new group
157 *
158 * \return Pointer to the new writer node group
159 */
160 struct writer_node_group *wng_new(unsigned num_writers)
161 {
162 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
163 g->num_writers = num_writers;
164 g->writer_nodes = para_calloc(num_writers
165 * sizeof(struct writer_node));
166 g->task.post_select = wng_post_select;
167 g->task.pre_select = wng_pre_select;
168 return g;
169 }
170
171 /**
172 * Call the init function of each supported paraslash writer.
173 */
174 void writer_init(void)
175 {
176 int i;
177
178 FOR_EACH_WRITER(i)
179 writers[i].init(&writers[i]);
180 }
181 /**
182 * check if given string is a valid command line for any writer
183 *
184 * \param \wa string of the form writer_name:options
185 * \param writer_num contains the number of the writer upon success
186 *
187 * This function checks whether \a wa starts with the name of a supported
188 * paraslash writer, optionally followed by a colon and any options for that
189 * writer. If a valid writer name was found and further are present, the
190 * remaining part of \a wa is passed to that writer's config parser.
191 *
192 * \return On success, a pointer to the gengetopt args info struct is returned
193 * and \a writer_num contains the number of the writer. Otherwise this function
194 * returns \p NULL.
195 */
196 void *check_writer_arg(const char *wa, int *writer_num)
197 {
198 int i;
199
200 *writer_num = -E_WRITE_COMMON_SYNTAX;
201 PARA_INFO_LOG("checking %s\n", wa);
202 FOR_EACH_WRITER(i) {
203 const char *name = writer_names[i];
204 size_t len = strlen(name);
205 char c;
206 if (strlen(wa) < len)
207 continue;
208 if (strncmp(name, wa, len))
209 continue;
210 c = wa[len];
211 if (c && c != ' ')
212 continue;
213 if (c && !writers[i].parse_config)
214 return NULL;
215 *writer_num = i;
216 return writers[i].parse_config(c? wa + len + 1 : "");
217 }
218 PARA_ERROR_LOG("writer not found\n");
219 return NULL;
220 }
221
222 /**
223 * setup a writer node group with only one writer, the default writer
224 *
225 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
226 * osx_write for OS X, file writer if neither of these is supported.
227 *
228 * \return pointer to the allocated writer node group
229 */
230 struct writer_node_group *setup_default_wng(void)
231 {
232 struct writer_node_group *wng = wng_new(1);
233 wng->writer_nodes[0].writer_num = DEFAULT_WRITER;
234 PARA_INFO_LOG("using default writer: %s %p\n",
235 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
236 wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
237 return wng;
238 }
239 /**
240 * Print the help text of all writers to stdout.
241 *
242 * \param detailed Whether to print the detailed help text.
243 */
244 void print_writer_helps(int detailed)
245 {
246 int i;
247
248 printf_or_die("\nAvailable writers: \n\t");
249 FOR_EACH_WRITER(i)
250 printf_or_die("%s%s", i? " " : "", writer_names[i]);
251 printf_or_die("\n\n");
252 FOR_EACH_WRITER(i) {
253 struct writer *w = writers + i;
254
255 if (!w->help.short_help)
256 continue;
257 printf_or_die("Options for %s:\n", writer_names[i]);
258 ggo_print_help(&w->help, detailed);
259 }
260 }