doc: Minor doxygen fixes.
[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  * prints an error message and calls exit().
51  */
52 void *check_writer_arg_or_die(const char *wa, int *writer_num)
53 {
54         int i;
55
56         PARA_INFO_LOG("checking %s\n", wa);
57         FOR_EACH_WRITER(i) {
58                 const char *name = writer_names[i];
59                 size_t len = strlen(name);
60                 char c;
61                 if (strlen(wa) < len)
62                         continue;
63                 if (strncmp(name, wa, len))
64                         continue;
65                 c = wa[len];
66                 if (c && c != ' ')
67                         continue;
68                 *writer_num = i;
69                 return writers[i].parse_config_or_die(c? wa + len + 1 : "");
70         }
71         PARA_EMERG_LOG("invalid writer %s\n", wa);
72         exit(EXIT_FAILURE);
73 }
74
75 /**
76  * Open a writer node and register the corresponding task.
77  *
78  * \param wn The writer node to open.
79  * \param parent The parent btr node (the source for the writer node).
80  *
81  * The configuration of the writer node stored in \p wn->conf must be
82  * initialized before this function may be called.
83  */
84 void register_writer_node(struct writer_node *wn, struct btr_node *parent)
85 {
86         struct writer *w = writers + wn->writer_num;
87         char *name = make_message("%s writer", writer_names[wn->writer_num]);
88
89         wn->btrn = btr_new_node(&(struct btr_node_description)
90                 EMBRACE(.name = name, .parent = parent,
91                 .handler = w->execute, .context = wn));
92         strcpy(wn->task.status, name);
93         free(name);
94         wn->task.post_select = w->post_select;
95         wn->task.pre_select = w->pre_select;
96         register_task(&wn->task);
97 }
98
99 /**
100  * Print the help text of all writers to stdout.
101  *
102  * \param detailed Whether to print the detailed help text.
103  */
104 void print_writer_helps(int detailed)
105 {
106         int i;
107
108         printf_or_die("\nAvailable writers: \n\t");
109         FOR_EACH_WRITER(i)
110                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
111         printf_or_die("\n\n");
112         FOR_EACH_WRITER(i) {
113                 struct writer *w = writers + i;
114
115                 if (!w->help.short_help)
116                         continue;
117                 printf_or_die("Options for %s:\n", writer_names[i]);
118                 ggo_print_help(&w->help, detailed);
119         }
120 }
121
122 static void get_btr_value(struct btr_node *btrn, const char *cmd,
123                 int32_t *result)
124 {
125         char *buf = NULL;
126         int ret = btr_exec_up(btrn, cmd, &buf);
127
128         if (ret < 0) {
129                 /*
130                  * This really should not happen. It means one of our parent
131                  * nodes died unexpectedly. Proceed with fingers crossed.
132                  */
133                 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
134                 *result = 0;
135                 return;
136         }
137         ret = para_atoi32(buf, result);
138         assert(ret >= 0);
139         free(buf);
140 }
141
142 /**
143  * Ask parent btr nodes for the sample rate of the current stream.
144  *
145  * \param btrn Where to start the search.
146  * \param result Filled in by this function.
147  *
148  * This function is assumed to succeed and terminates on errors.
149  */
150 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
151 {
152         get_btr_value(btrn, "sample_rate", result);
153 }
154
155 /**
156  * Ask parent btr nodes for the channel count of the current stream.
157  *
158  * \param btrn See \ref get_btr_sample_rate.
159  * \param result See \ref get_btr_sample_rate.
160  */
161 void get_btr_channels(struct btr_node *btrn, int32_t *result)
162 {
163         get_btr_value(btrn, "channels", result);
164 }
165
166 /**
167  * Ask parent btr nodes for the number of bits per sample and the byte sex.
168  *
169  * \param btrn See \ref get_btr_sample_rate.
170  * \param result Contains the sample format as an enum sample_format type.
171  */
172 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
173 {
174         get_btr_value(btrn, "sample_format", result);
175 }