Merge branch 'master' into my-osx
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file write_common.c common functions of para_audiod and para_write */
20
21 #include "para.h"
22 #include "string.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "write.h"
26 #include "error.h"
27
28 const char *writer_names[] ={WRITER_NAMES};
29 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
30
31 static void wng_pre_select(__a_unused struct sched *s, struct task *t)
32 {
33         struct writer_node_group *g = t->private_data;
34         int i;
35
36         FOR_EACH_WRITER_NODE(i, g) {
37                 struct writer_node *wn = &g->writer_nodes[i];
38                 t->ret = wn->writer->pre_select(s, wn);
39                 if (t->ret < 0) {
40                         g->eof = 1;
41                         return;
42                 }
43         }
44 }
45
46 static void wng_post_select(struct sched *s, struct task *t)
47 {
48         struct writer_node_group *g = t->private_data;
49         int i;
50         size_t min_written = 0;
51
52         FOR_EACH_WRITER_NODE(i, g) {
53                 struct writer_node *wn = &g->writer_nodes[i];
54                 t->ret = wn->writer->post_select(s, wn);
55                 if (t->ret < 0) {
56                         g->eof = 1;
57                         return;
58                 }
59                 if (!i)
60                         min_written = wn->written;
61                 else
62                         min_written = PARA_MIN(min_written, wn->written);
63         }
64 //      PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
65         if (min_written) {
66                 *g->loaded -= min_written;
67                 FOR_EACH_WRITER_NODE(i, g)
68                         g->writer_nodes[i].written -= min_written;
69         }
70         if (!*g->loaded && *g->input_eof) {
71                 g->eof = 1;
72                 t->ret = -E_WNG_EOF;
73                 return;
74         }
75         t->ret = 1;
76         if (*g->loaded && min_written) {
77 //              PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
78                 memmove(g->buf, g->buf + min_written, *g->loaded);
79         }
80 }
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         register_task(&g->task);
88         FOR_EACH_WRITER_NODE(i, g) {
89                 struct writer_node *wn = &g->writer_nodes[i];
90                 wn->wng = g;
91                 ret = wn->writer->open(wn);
92                 if (ret < 0)
93                         goto err_out;
94                 wn->chunk_bytes = ret;
95                 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
96         }
97         sprintf(g->task.status, "%s", "writer node group");
98         g->eof = 0;
99         return 1;
100 err_out:
101         unregister_task(&g->task);
102         while (i > 0) {
103                 struct writer_node *wn = &g->writer_nodes[--i];
104                 wn->writer->close(wn);
105         }
106         g->num_writers = 0;
107         g->eof = 1;
108         return ret;
109 }
110
111 void wng_unregister(struct writer_node_group *g)
112 {
113         unregister_task(&g->task);
114         g->eof = 1;
115 }
116
117 void wng_close(struct writer_node_group *g)
118 {
119         int i;
120
121         if (!g)
122                 return;
123         PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
124         FOR_EACH_WRITER_NODE(i, g) {
125                 struct writer_node *wn = &g->writer_nodes[i];
126                 wn->writer->close(wn);
127         }
128         free(g->writer_nodes);
129         free(g);
130 }
131
132 struct writer_node_group *wng_new(unsigned num_writers)
133 {
134         struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
135         g->num_writers = num_writers;
136         g->writer_nodes = para_calloc(num_writers
137                 * sizeof(struct writer_node));
138         g->task.private_data = g;
139         g->task.post_select = wng_post_select;
140         g->task.pre_select = wng_pre_select;
141         return g;
142 }
143
144 void init_supported_writers(void)
145 {
146         int i;
147
148         FOR_EACH_WRITER(i)
149                 writers[i].init(&writers[i]);
150 }
151
152 void *check_writer_arg(char *wa, int *writer_num)
153 {
154         int i;
155
156         *writer_num = -E_WRITE_COMMON_SYNTAX;
157         PARA_INFO_LOG("checking  %s\n", wa);
158         FOR_EACH_WRITER(i) {
159                 const char *name = writer_names[i];
160                 size_t len = strlen(name);
161                 char c;
162                 if (strlen(wa) < len)
163                         continue;
164                 if (strncmp(name, wa, len))
165                         continue;
166                 c = wa[len];
167                 if (c && c != ' ')
168                         continue;
169                 if (c && !writers[i].parse_config)
170                         return NULL;
171                 *writer_num = i;
172                 return writers[i].parse_config(c? wa + len + 1 : "");
173         }
174         PARA_ERROR_LOG("%s", "writer not found\n");
175         return NULL;
176 }
177
178 struct writer_node_group *setup_default_wng(void)
179 {
180         struct writer_node_group *wng = wng_new(1);
181         enum writer_enum default_writer;
182
183         if (NUM_SUPPORTED_WRITERS == 1)
184                 default_writer = FILE_WRITE;
185         else
186                 default_writer = 1;
187         wng->writer_nodes[0].writer = &writers[default_writer];
188         PARA_INFO_LOG("using default writer: %s %p\n",
189                 writer_names[default_writer], writers[default_writer].parse_config);
190         wng->writer_nodes[0].conf = writers[default_writer].parse_config("");
191         return wng;
192 }