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