2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter_common.c Common helper functions for filter input/output. */
10 #include <sys/types.h>
19 #include "buffer_tree.h"
24 /** The array of supported filters. */
25 struct filter filters
[NUM_SUPPORTED_FILTERS
] = {FILTER_ARRAY
};
28 * Call the init function of each supported filter.
31 void filter_init(void)
35 FOR_EACH_SUPPORTED_FILTER(i
)
36 filters
[i
].init(filters
+ i
);
40 * If the filter has a command line parser and options is not NULL, run it.
41 * Returns filter_num on success, negative on errors
43 static int parse_filter_args(int filter_num
, char *options
, void **conf
)
45 struct filter
*f
= &filters
[filter_num
];
49 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
50 // options? options : "(none)", f->parse_config);
52 return strlen(options
)? -E_BAD_FILTER_OPTIONS
: filter_num
;
53 // PARA_DEBUG_LOG("options: %s\n", options);
54 argc
= create_argv(options
, " \t", &argv
);
56 return -E_BAD_FILTER_OPTIONS
;
57 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc
, argv
[0]);
58 for (i
= argc
- 1; i
>= 0; i
--)
59 argv
[i
+ 1] = argv
[i
];
60 argv
[0] = para_strdup(f
->name
);
62 ret
= f
->parse_config(argc
, argv
, conf
);
64 argv
[argc
- 1] = NULL
;
66 return ret
< 0? ret
: filter_num
;
70 * Check the filter command line options.
72 * \param fa The command line options.
73 * \param conf Points to the filter configuration upon successful return.
75 * Check if \a fa starts with a the name of a supported filter, followed by
76 * a colon. If yes, call the command line parser of that filter.
78 * \return On success, the number of the filter is returned and \a conf
79 * is initialized to point to the filter configuration determined by \a fa.
80 * On errors, a negative value is returned.
82 * Note: If \a fa specifies a filter that has no command line parser success is
83 * returned, and \a conf is initialized to \p NULL.
85 * \sa filter::parse_config
87 int check_filter_arg(char *fa
, void **conf
)
92 // PARA_DEBUG_LOG("arg: %s\n", fa);
93 FOR_EACH_SUPPORTED_FILTER(j
) {
94 const char *name
= filters
[j
].name
;
95 size_t len
= strlen(name
);
99 if (strncmp(name
, fa
, len
))
104 if (c
&& !filters
[j
].parse_config
)
105 return -E_BAD_FILTER_OPTIONS
;
106 return parse_filter_args(j
, c
? fa
+ len
+ 1 :
107 fa
+ strlen(fa
), conf
);
109 return -E_UNSUPPORTED_FILTER
;
112 void print_filter_helps(int detailed
)
116 printf_or_die("\nAvailable filters: \n\t");
117 FOR_EACH_SUPPORTED_FILTER(i
)
118 printf_or_die("%s%s", i
? " " : "", filters
[i
].name
);
119 printf_or_die("\n\n");
121 FOR_EACH_SUPPORTED_FILTER(i
) {
122 struct filter
*f
= filters
+ i
;
124 if (!f
->help
.short_help
)
126 printf_or_die("Options for %s:\n", f
->name
);
127 ggo_print_help(&f
->help
, detailed
);
131 void generic_filter_pre_select(struct sched
*s
, struct task
*t
)
133 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
136 if (btr_node_status(fn
->btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
) != 0)
141 * Execute a btr command for a decoder.
143 * The buffer tree nodes of the writers ask the parent nodes about sample_rate
144 * and the channels count. This function is called by all decoders to answer
147 * \param cmd The command to be executed by the child node.
148 * \param sample_rate Known to the decoder.
149 * \param channels Known to the decoder.
150 * \param result Ascii representation on the answer is stored here.
152 int decoder_execute(const char *cmd
, unsigned sample_rate
, unsigned channels
,
155 if (!strcmp(cmd
, "sample_rate")) {
156 if (sample_rate
== 0)
157 return -E_BTR_NAVAIL
;
158 *result
= make_message("%u", sample_rate
);
161 if (!strcmp(cmd
, "channels")) {
163 return -E_BTR_NAVAIL
;
164 *result
= make_message("%u", channels
);
167 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);