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