Initialize all task->status fields.
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006-2011 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 #include <stdbool.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "write.h"
19 #include "error.h"
20
21 /** the array containing the names of all supported writers */
22 const char *writer_names[] ={WRITER_NAMES};
23
24 /** the array of supported writers */
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
26
27 /**
28  * Call the init function of each supported paraslash writer.
29  */
30 void writer_init(void)
31 {
32         int i;
33
34         FOR_EACH_WRITER(i)
35                 writers[i].init(&writers[i]);
36 }
37 /**
38  * check if given string is a valid command line for any writer
39  *
40  * \param \wa string of the form writer_name:options
41  * \param writer_num contains the number of the writer upon success
42  *
43  * This function checks whether \a wa starts with the name of a supported
44  * paraslash writer, optionally followed by a colon and any options for that
45  * writer.  If a valid writer name was found and further are present, the
46  * remaining part of \a wa is passed to that writer's config parser.
47  *
48  * \return On success, a pointer to the gengetopt args info struct is returned
49  * and \a writer_num contains the number of the writer. Otherwise this function
50  * returns \p NULL.
51  */
52 void *check_writer_arg(const char *wa, int *writer_num)
53 {
54         int i;
55
56         *writer_num = -E_WRITE_COMMON_SYNTAX;
57         PARA_INFO_LOG("checking  %s\n", wa);
58         FOR_EACH_WRITER(i) {
59                 const char *name = writer_names[i];
60                 size_t len = strlen(name);
61                 char c;
62                 if (strlen(wa) < len)
63                         continue;
64                 if (strncmp(name, wa, len))
65                         continue;
66                 c = wa[len];
67                 if (c && c != ' ')
68                         continue;
69                 *writer_num = i;
70                 return writers[i].parse_config_or_die(c? wa + len + 1 : "");
71         }
72         PARA_ERROR_LOG("writer not found\n");
73         return NULL;
74 }
75
76 /**
77  * Open a writer node and register the corresponding task.
78  *
79  * \param wn The writer node to open.
80  * \param parent The parent btr node (the source for the writer node).
81  *
82  * The configuration of the writer node stored in \p wn->conf must be
83  * initialized before this function may be called.
84  */
85 void register_writer_node(struct writer_node *wn, struct btr_node *parent)
86 {
87         struct writer *w = writers + wn->writer_num;
88         char *name = make_message("%s writer", writer_names[wn->writer_num]);
89
90         wn->btrn = btr_new_node(&(struct btr_node_description)
91                 EMBRACE(.name = name, .parent = parent,
92                 .handler = w->execute, .context = wn));
93         strcpy(wn->task.status, name);
94         free(name);
95         wn->task.post_select = w->post_select;
96         wn->task.pre_select = w->pre_select;
97         register_task(&wn->task);
98 }
99
100 /**
101  * Setup a writer node with the default writer.
102  *
103  * If arg is \p NULL, the OS-dependent default writer is used with an empty
104  * configuration string.  It defaults to alsa for Linux, osx for OS X, oss for
105  * *BSD and the file writer if neither of these is supported.
106  *
107  * Once the writer configuration has been retrieved, a writer node is created,
108  * its buffer tree node is added to the buffer tree as a child of the given
109  * parent.
110  *
111  * Finally, the new writer node's taks structure is initialized and registered
112  * to the paraslash scheduler.
113  *
114  * \return A pointer to the allocated writer node group.
115  */
116 int setup_writer_node(const char *arg, struct btr_node *parent,
117                 struct writer_node *wn)
118 {
119         if (arg)
120                 wn->conf = check_writer_arg(arg, &wn->writer_num);
121         else {
122                 wn->writer_num = DEFAULT_WRITER;
123                 wn->conf = writers[DEFAULT_WRITER].parse_config_or_die("");
124         }
125         if (!wn->conf)
126                 return -E_WRITE_COMMON_SYNTAX;
127         register_writer_node(wn, parent);
128         return 1;
129 }
130
131 /**
132  * Print the help text of all writers to stdout.
133  *
134  * \param detailed Whether to print the detailed help text.
135  */
136 void print_writer_helps(int detailed)
137 {
138         int i;
139
140         printf_or_die("\nAvailable writers: \n\t");
141         FOR_EACH_WRITER(i)
142                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
143         printf_or_die("\n\n");
144         FOR_EACH_WRITER(i) {
145                 struct writer *w = writers + i;
146
147                 if (!w->help.short_help)
148                         continue;
149                 printf_or_die("Options for %s:\n", writer_names[i]);
150                 ggo_print_help(&w->help, detailed);
151         }
152 }
153
154 static void get_btr_value(struct btr_node *btrn, const char *cmd,
155                 int32_t *result)
156 {
157         char *buf = NULL;
158         int ret = btr_exec_up(btrn, cmd, &buf);
159
160         if (ret < 0) {
161                 /*
162                  * This really should not happen. It means one of our parent
163                  * nodes died unexpectedly. Proceed with fingers crossed.
164                  */
165                 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
166                 *result = 0;
167                 return;
168         }
169         ret = para_atoi32(buf, result);
170         assert(ret >= 0);
171         free(buf);
172 }
173
174 /**
175  * Ask parent btr nodes for the sample rate of the current stream.
176  *
177  * \param btrn Where to start the search.
178  * \param result Filled in by this function.
179  *
180  * This function is assumed to succeed and terminates on errors.
181  */
182 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
183 {
184         get_btr_value(btrn, "sample_rate", result);
185 }
186
187 /**
188  * Ask parent btr nodes for the channel count of the current stream.
189  *
190  * \param btrn See \ref get_btr_sample_rate.
191  * \param result See \ref get_btr_sample_rate.
192  */
193 void get_btr_channels(struct btr_node *btrn, int32_t *result)
194 {
195         get_btr_value(btrn, "channels", result);
196 }
197
198 /**
199  * Ask parent btr nodes for the number of bits per sample and the byte sex.
200  *
201  * \param btrn See \ref get_btr_sample_rate.
202  * \param result Contains the sample format as an enum sample_format type.
203  */
204 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
205 {
206         get_btr_value(btrn, "sample_format", result);
207 }