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