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