Make FOR_EACH_SUPPORTED_FILTER() local to filter_common.c.
[paraslash.git] / filter_common.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file filter_common.c Common helper functions for filter input/output. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** Iterate over the array of supported filters. */
23 #define FOR_EACH_SUPPORTED_FILTER(j) for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
24
25 /** The array of supported filters. */
26 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
27
28 /**
29 * Call the init function of each supported filter.
30 * \sa filter::init
31 */
32 void filter_init(void)
33 {
34 int i;
35
36 FOR_EACH_SUPPORTED_FILTER(i)
37 filters[i].init(filters + i);
38 }
39
40 /*
41 * If the filter has a command line parser and options is not NULL, run it.
42 * Returns filter_num on success, negative on errors
43 */
44 static int parse_filter_args(int filter_num, char *options, void **conf)
45 {
46 struct filter *f = &filters[filter_num];
47 int ret, argc;
48 char **argv;
49
50 if (!f->parse_config)
51 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
52 argc = create_shifted_argv(options, " \t", &argv);
53 if (argc < 0)
54 return -E_BAD_FILTER_OPTIONS;
55 argv[0] = para_strdup(f->name);
56 ret = f->parse_config(argc, argv, conf);
57 free_argv(argv);
58 return ret < 0? ret : filter_num;
59 }
60
61 /**
62 * Check the filter command line options.
63 *
64 * \param fa The command line options.
65 * \param conf Points to the filter configuration upon successful return.
66 *
67 * Check if \a fa starts with a the name of a supported filter, followed by
68 * a colon. If yes, call the command line parser of that filter.
69 *
70 * \return On success, the number of the filter is returned and \a conf
71 * is initialized to point to the filter configuration determined by \a fa.
72 * On errors, a negative value is returned.
73 *
74 * Note: If \a fa specifies a filter that has no command line parser success is
75 * returned, and \a conf is initialized to \p NULL.
76 *
77 * \sa filter::parse_config
78 */
79 int check_filter_arg(char *fa, void **conf)
80 {
81 int j;
82
83 *conf = NULL;
84 // PARA_DEBUG_LOG("arg: %s\n", fa);
85 FOR_EACH_SUPPORTED_FILTER(j) {
86 const char *name = filters[j].name;
87 size_t len = strlen(name);
88 char c;
89 if (strlen(fa) < len)
90 continue;
91 if (strncmp(name, fa, len))
92 continue;
93 c = fa[len];
94 if (c && c != ' ')
95 continue;
96 if (c && !filters[j].parse_config)
97 return -E_BAD_FILTER_OPTIONS;
98 return parse_filter_args(j, c? fa + len + 1 :
99 fa + strlen(fa), conf);
100 }
101 return -E_UNSUPPORTED_FILTER;
102 }
103
104 /**
105 * Print help text of each filter to stdout.
106 *
107 * \param flags Passed to \ref ggo_print_help().
108 */
109 void print_filter_helps(unsigned flags)
110 {
111 int i, num = 0;
112
113 printf_or_die("\nAvailable filters: ");
114 FOR_EACH_SUPPORTED_FILTER(i) {
115 if (num > 50) {
116 printf_or_die("\n ");
117 num = 0;
118 }
119 num += printf_or_die("%s%s", i? " " : "", filters[i].name);
120 }
121 printf_or_die("\n");
122
123 FOR_EACH_SUPPORTED_FILTER(i) {
124 struct filter *f = filters + i;
125
126 if (!f->help.short_help)
127 continue;
128 printf_or_die("\nOptions for %s (%s):", f->name,
129 f->help.purpose);
130 ggo_print_help(&f->help, flags);
131 }
132 }
133
134 /**
135 * Set select timeout of the scheduler.
136 *
137 * \param s The scheduler.
138 * \param context Pointer to the filter node (task context).
139 *
140 * This looks at the status of the btr node of the filter. If data is available
141 * in the input queue of the filter, or if an error occurred, a minimal timeout
142 * for the next select call is requested from the scheduler. Otherwise the
143 * scheduler timeout is left unchanged.
144 */
145 void generic_filter_pre_select(struct sched *s, void *context)
146 {
147 struct filter_node *fn = context;
148
149 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
150 sched_min_delay(s);
151 }
152
153 #ifdef WORDS_BIGENDIAN
154 #define DECODER_SAMPLE_FORMAT SF_S16_BE
155 #else
156 #define DECODER_SAMPLE_FORMAT SF_S16_LE
157 #endif
158
159 /**
160 * Execute a btr command for a decoder.
161 *
162 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
163 * channels count and sample format. This function is called by all decoders to
164 * answer these queries.
165 *
166 * \param cmd The command to be executed by the child node.
167 * \param sample_rate Known to the decoder.
168 * \param channels Known to the decoder.
169 * \param result Ascii representation on the answer is stored here.
170 *
171 * \return Standard.
172 */
173 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
174 char **result)
175 {
176 if (!strcmp(cmd, "sample_rate")) {
177 if (sample_rate == 0)
178 return -E_BTR_NAVAIL;
179 *result = make_message("%u", sample_rate);
180 return 1;
181 }
182 if (!strcmp(cmd, "channels")) {
183 if (channels == 0)
184 return -E_BTR_NAVAIL;
185 *result = make_message("%u", channels);
186 return 1;
187 }
188 if (!strcmp(cmd, "sample_format")) {
189 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
190 return 1;
191 }
192 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
193 }