1 | //filter-slfi-file2filter.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012-2014 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <roaraudio.h> |
---|
27 | #include <libroarlight/libroarlight.h> |
---|
28 | |
---|
29 | #define MAX_STACKSIZE 16 |
---|
30 | |
---|
31 | static int __cb_event_add(struct roar_slfi_inst * inst, void * userdata, uint8_t event) { |
---|
32 | (void)inst; |
---|
33 | return roar_slfi_event_add(userdata, event); |
---|
34 | } |
---|
35 | |
---|
36 | static struct roar_slfi_inst * __parse_filter(char * p) { |
---|
37 | struct roar_slfi_inst * ret; |
---|
38 | struct roar_keyval * para = NULL; |
---|
39 | ssize_t paralen = -1; |
---|
40 | char * args; |
---|
41 | int err; |
---|
42 | |
---|
43 | args = strstr(p, " "); |
---|
44 | if ( args != NULL ) { |
---|
45 | *args = 0; |
---|
46 | args++; |
---|
47 | // strip spaces: |
---|
48 | for (; *p == ' '; p++); |
---|
49 | |
---|
50 | paralen = roar_keyval_split(¶, args, NULL, NULL, 1); |
---|
51 | } |
---|
52 | |
---|
53 | ret = roar_slfi_new(p, 1, para, paralen); |
---|
54 | err = roar_error; |
---|
55 | |
---|
56 | if ( para != NULL ) |
---|
57 | roar_mm_free(para); |
---|
58 | |
---|
59 | roar_err_set(err); |
---|
60 | return ret; |
---|
61 | } |
---|
62 | |
---|
63 | static struct roar_slfi_inst * __parse_vio(struct roar_vio_calls * vio) { |
---|
64 | struct roar_slfi_inst * stack[MAX_STACKSIZE] = {NULL}; |
---|
65 | size_t stackpointer = 0; |
---|
66 | size_t i; |
---|
67 | char buf[1024]; |
---|
68 | char * p; |
---|
69 | int err = ROAR_ERROR_NONE; |
---|
70 | char * end; |
---|
71 | |
---|
72 | // create root node. |
---|
73 | stack[stackpointer] = roar_slfi_new("chain", 1, NULL, -1); |
---|
74 | if ( stack[stackpointer] == NULL ) |
---|
75 | return NULL; |
---|
76 | stackpointer++; |
---|
77 | |
---|
78 | while ((p = roar_vio_fgets(vio, buf, sizeof(buf))) != NULL) { |
---|
79 | // strip spaces: |
---|
80 | for (; *p == ' '; p++); |
---|
81 | |
---|
82 | // strip newlines. |
---|
83 | for (end = p; *end; end++) { |
---|
84 | if ( *end == '\n' || *end == '\r' ) { |
---|
85 | *end = 0; |
---|
86 | break; |
---|
87 | } |
---|
88 | } |
---|
89 | // skip comments: |
---|
90 | if ( *p == '#' ) |
---|
91 | continue; |
---|
92 | if ( *p == '{' ) { |
---|
93 | if ( stackpointer == (MAX_STACKSIZE-1) ) { |
---|
94 | err = ROAR_ERROR_NOSPC; |
---|
95 | break; |
---|
96 | } |
---|
97 | stackpointer++; |
---|
98 | stack[stackpointer] = NULL; |
---|
99 | } else if ( *p == '}' ) { |
---|
100 | if ( stackpointer == 1 ) { |
---|
101 | err = ROAR_ERROR_ILLSEQ; |
---|
102 | break; |
---|
103 | } |
---|
104 | if ( stack[stackpointer] != NULL ) { |
---|
105 | roar_slfi_unref(stack[stackpointer]); |
---|
106 | stack[stackpointer] = NULL; |
---|
107 | } |
---|
108 | stackpointer--; |
---|
109 | } else { |
---|
110 | if ( stack[stackpointer] != NULL ) { |
---|
111 | roar_slfi_unref(stack[stackpointer]); |
---|
112 | stack[stackpointer] = NULL; |
---|
113 | } |
---|
114 | stack[stackpointer] = __parse_filter(p); |
---|
115 | if ( stack[stackpointer] == NULL ) { |
---|
116 | err = roar_error; |
---|
117 | break; |
---|
118 | } |
---|
119 | if ( roar_slfi_ctl(stack[stackpointer-1], ROAR_SLFI_CMD_PUSH, stack[stackpointer]) == -1 ) { |
---|
120 | err = roar_error; |
---|
121 | break; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | for (i = stackpointer; i; i--) |
---|
127 | roar_slfi_unref(stack[i]); |
---|
128 | |
---|
129 | if (err != ROAR_ERROR_NONE ) { |
---|
130 | roar_slfi_unref(stack[0]); |
---|
131 | stack[0] = NULL; |
---|
132 | } |
---|
133 | |
---|
134 | roar_err_set(err); |
---|
135 | return stack[0]; |
---|
136 | } |
---|
137 | |
---|
138 | static struct roar_slfi_inst * __parse_file(const char * file) { |
---|
139 | struct roar_vio_calls vio; |
---|
140 | struct roar_slfi_inst * ret; |
---|
141 | int err; |
---|
142 | |
---|
143 | if ( file == NULL ) { |
---|
144 | roar_err_set(ROAR_ERROR_INVAL); |
---|
145 | return NULL; |
---|
146 | } |
---|
147 | |
---|
148 | if ( roar_vio_open_dstr_simple(&vio, file, ROAR_VIOF_READ) == -1 ) |
---|
149 | return NULL; |
---|
150 | |
---|
151 | ret = __parse_vio(&vio); |
---|
152 | err = roar_error; |
---|
153 | |
---|
154 | roar_vio_close(&vio); |
---|
155 | |
---|
156 | roar_err_set(err); |
---|
157 | return ret; |
---|
158 | } |
---|
159 | |
---|
160 | static int __init(struct roar_slfi_inst * inst, const struct roar_keyval * para, ssize_t paralen) { |
---|
161 | const struct roar_keyval * kv; |
---|
162 | const char * filename = NULL; |
---|
163 | ssize_t i; |
---|
164 | |
---|
165 | for (i = 0; i < paralen; i++) { |
---|
166 | kv = &(para[i]); |
---|
167 | if ( kv->key == NULL || kv->value == NULL ) |
---|
168 | continue; |
---|
169 | if ( !strcmp(kv->key, "file") ) { |
---|
170 | filename = kv->value; |
---|
171 | } else { |
---|
172 | ROAR_WARN("__init(*): Unknown parameter: %s", kv->key); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | inst->userdata = __parse_file(filename); |
---|
177 | if ( inst->userdata == NULL ) |
---|
178 | return -1; |
---|
179 | |
---|
180 | roar_slfi_cb_set_event_add(inst->userdata, __cb_event_add, inst); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int __uninit(struct roar_slfi_inst * inst) { |
---|
186 | roar_slfi_unref(inst->userdata); |
---|
187 | inst->userdata = NULL; |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | static int __update(struct roar_slfi_inst * inst, uint8_t * universe, ssize_t size_of_universe, int32_t usecspassed, const uint8_t * event, size_t eventlen) { |
---|
192 | return roar_slfi_update(inst->userdata, universe, size_of_universe, usecspassed, event, eventlen); |
---|
193 | } |
---|
194 | |
---|
195 | static const struct roar_slfi_filter filter[1] = { |
---|
196 | { |
---|
197 | .name = "file2filter", |
---|
198 | .description = "SLFI filter structure builder", |
---|
199 | .flags = ROAR_SLFI_FLAG_ON_UPDATE, |
---|
200 | .init = __init, |
---|
201 | .uninit = __uninit, |
---|
202 | .update = __update, |
---|
203 | .ctl = NULL |
---|
204 | } |
---|
205 | }; |
---|
206 | |
---|
207 | ROAR_DL_PLUGIN_REG_SLFI(filter); |
---|
208 | |
---|
209 | // This is the plugin control block. |
---|
210 | ROAR_DL_PLUGIN_START(filter_slfi_file2filter) { |
---|
211 | // Here we set the name and vendor of our plugin. |
---|
212 | // If you have no Vendor ID you need to use ROAR_DL_PLUGIN_META_PRODUCT_NV(). |
---|
213 | ROAR_DL_PLUGIN_META_PRODUCT_NIV("filter-slfi-file2filter", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO); |
---|
214 | |
---|
215 | // This sets the version of your plugin. |
---|
216 | ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING); |
---|
217 | |
---|
218 | // This sets the license of your plugin. |
---|
219 | // If there is no tag for the license you use you can just |
---|
220 | // use ROAR_DL_PLUGIN_META_LICENSE(). |
---|
221 | ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0); |
---|
222 | |
---|
223 | // This sets the author and contact infos. |
---|
224 | // There are several other macros to do this with other parameters. |
---|
225 | // See ROAR_DL_PLUGIN_META_CONTACT*() in the header or documentation. |
---|
226 | ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org"); |
---|
227 | |
---|
228 | // This sets the description for your plugin. |
---|
229 | ROAR_DL_PLUGIN_META_DESC("This plugin loads SLFI filter structures based on informations store in a file."); |
---|
230 | |
---|
231 | // Load filters. |
---|
232 | ROAR_DL_PLUGIN_REG_FNFUNC(ROAR_DL_FN_FILTER); |
---|
233 | |
---|
234 | // This is the end of the control block. |
---|
235 | } ROAR_DL_PLUGIN_END |
---|
236 | |
---|
237 | //ll |
---|