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