build: Convert alsa detection to new macros.
[paraslash.git] / write_common.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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 "buffer_tree.h"
17 #include "write.h"
18 #include "error.h"
19 #include "write_common.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, ret, argc;
55 const char *cmdline;
56 char **argv;
57 void *conf;
58
59 if (!wa || !*wa) {
60 i = DEFAULT_WRITER;
61 cmdline = NULL;
62 goto check;
63 }
64 PARA_INFO_LOG("checking %s\n", wa);
65 FOR_EACH_WRITER(i) {
66 const char *name = writer_names[i];
67 size_t len = strlen(name);
68 char c;
69
70 if (strlen(wa) < len)
71 continue;
72 if (strncmp(name, wa, len))
73 continue;
74 c = wa[len];
75 if (!c || c == ' ') {
76 cmdline = c? wa + len + 1 : NULL;
77 goto check;
78 }
79 }
80 PARA_EMERG_LOG("invalid writer %s\n", wa);
81 exit(EXIT_FAILURE);
82 check:
83 ret = create_shifted_argv(cmdline, " \t", &argv);
84 if (ret < 0) {
85 PARA_EMERG_LOG("%s: %s\n", wa, para_strerror(-ret));
86 exit(EXIT_FAILURE);
87 }
88 argc = ret;
89 argv[0] = make_message("%s_write", writer_names[i]);
90 *writer_num = i;
91 conf = writers[i].parse_config_or_die(argc, argv);
92 free_argv(argv);
93 return conf;
94 }
95
96 /**
97 * Open a writer node and register the corresponding task.
98 *
99 * \param wn The writer node to open.
100 * \param parent The parent btr node (the source for the writer node).
101 * \param s The scheduler instance to register the task to.
102 *
103 * The configuration of the writer node stored in \p wn->conf must be
104 * initialized before this function may be called.
105 */
106 void register_writer_node(struct writer_node *wn, struct btr_node *parent,
107 struct sched *s)
108 {
109 struct writer *w = writers + wn->writer_num;
110
111 wn->btrn = btr_new_node(&(struct btr_node_description)
112 EMBRACE(.name = writer_names[wn->writer_num], .parent = parent,
113 .handler = w->execute, .context = wn));
114 wn->task = task_register(&(struct task_info) {
115 .name = writer_names[wn->writer_num],
116 .pre_select = w->pre_select,
117 .post_select = w->post_select,
118 .context = wn,
119 }, s);
120 }
121
122 /**
123 * Print the help text of all writers to stdout.
124 *
125 * \param flags Passed to \ref ggo_print_help().
126 */
127 void print_writer_helps(unsigned flags)
128 {
129 int i;
130
131 printf_or_die("\nAvailable writers: ");
132 FOR_EACH_WRITER(i)
133 printf_or_die("%s%s", i? " " : "", writer_names[i]);
134 printf_or_die("\n");
135 FOR_EACH_WRITER(i) {
136 struct writer *w = writers + i;
137
138 if (!w->help.short_help)
139 continue;
140 printf_or_die("\n%s: %s", writer_names[i],
141 w->help.purpose);
142 ggo_print_help(&w->help, flags);
143 }
144 }
145
146 static void get_btr_value(struct btr_node *btrn, const char *cmd,
147 int32_t *result)
148 {
149 char *buf = NULL;
150 int ret = btr_exec_up(btrn, cmd, &buf);
151
152 if (ret < 0) {
153 /*
154 * This really should not happen. It means one of our parent
155 * nodes died unexpectedly. Proceed with fingers crossed.
156 */
157 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
158 *result = 0;
159 return;
160 }
161 ret = para_atoi32(buf, result);
162 assert(ret >= 0);
163 free(buf);
164 }
165
166 /**
167 * Ask parent btr nodes for the sample rate of the current stream.
168 *
169 * \param btrn Where to start the search.
170 * \param result Filled in by this function.
171 *
172 * This function is assumed to succeed and terminates on errors.
173 */
174 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
175 {
176 get_btr_value(btrn, "sample_rate", result);
177 }
178
179 /**
180 * Ask parent btr nodes for the channel count of the current stream.
181 *
182 * \param btrn See \ref get_btr_sample_rate.
183 * \param result See \ref get_btr_sample_rate.
184 */
185 void get_btr_channels(struct btr_node *btrn, int32_t *result)
186 {
187 get_btr_value(btrn, "channels", result);
188 }
189
190 /**
191 * Ask parent btr nodes for the number of bits per sample and the byte sex.
192 *
193 * \param btrn See \ref get_btr_sample_rate.
194 * \param result Contains the sample format as an enum sample_format type.
195 */
196 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
197 {
198 get_btr_value(btrn, "sample_format", result);
199 }