Mercurial > audlegacy
annotate src/audacious/pluginenum.c @ 2623:0ad10a95ed10 trunk
[svn] - plugin reloading.
author | nenolod |
---|---|
date | Thu, 15 Mar 2007 19:25:27 -0700 |
parents | 4daf4fa409e0 |
children | 840fb578a834 |
rev | line source |
---|---|
2313 | 1 /* Audacious - Cross-platform multimedia player |
2 * Copyright (C) 2005-2007 Audacious development team | |
3 * | |
4 * Based on BMP: | |
5 * Copyright (C) 2003-2004 BMP development team | |
6 * | |
7 * Based on XMMS: | |
8 * Copyright (C) 1998-2003 XMMS development team | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; under version 2 of the License. | |
13 * | |
14 * This program 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 program; if not, write to the Free Software | |
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 # include "config.h" | |
26 #endif | |
27 | |
28 #ifndef SHARED_SUFFIX | |
29 # define SHARED_SUFFIX G_MODULE_SUFFIX | |
30 #endif | |
31 | |
32 #include "pluginenum.h" | |
33 | |
34 #include <glib.h> | |
35 #include <gmodule.h> | |
36 #include <glib/gprintf.h> | |
37 #include <string.h> | |
38 | |
39 #include "controlsocket.h" | |
40 #include "main.h" | |
41 #include "ui_main.h" | |
42 #include "playback.h" | |
43 #include "playlist.h" | |
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2313
diff
changeset
|
44 #include "strings.h" |
2313 | 45 #include "util.h" |
46 | |
47 #include "effect.h" | |
48 #include "general.h" | |
49 #include "input.h" | |
50 #include "output.h" | |
51 #include "visualization.h" | |
52 | |
53 const gchar *plugin_dir_list[] = { | |
54 PLUGINSUBS, | |
55 NULL | |
56 }; | |
57 | |
58 GHashTable *plugin_matrix = NULL; | |
59 GList *lowlevel_list = NULL; | |
60 | |
2623 | 61 extern GList *vfs_transports; |
62 | |
2313 | 63 static gint |
64 inputlist_compare_func(gconstpointer a, gconstpointer b) | |
65 { | |
66 const InputPlugin *ap = a, *bp = b; | |
67 return strcasecmp(ap->description, bp->description); | |
68 } | |
69 | |
70 static gint | |
71 outputlist_compare_func(gconstpointer a, gconstpointer b) | |
72 { | |
73 const OutputPlugin *ap = a, *bp = b; | |
74 return strcasecmp(ap->description, bp->description); | |
75 } | |
76 | |
77 static gint | |
78 effectlist_compare_func(gconstpointer a, gconstpointer b) | |
79 { | |
80 const EffectPlugin *ap = a, *bp = b; | |
81 return strcasecmp(ap->description, bp->description); | |
82 } | |
83 | |
84 static gint | |
85 generallist_compare_func(gconstpointer a, gconstpointer b) | |
86 { | |
87 const GeneralPlugin *ap = a, *bp = b; | |
88 return strcasecmp(ap->description, bp->description); | |
89 } | |
90 | |
91 static gint | |
92 vislist_compare_func(gconstpointer a, gconstpointer b) | |
93 { | |
94 const VisPlugin *ap = a, *bp = b; | |
95 return strcasecmp(ap->description, bp->description); | |
96 } | |
97 | |
98 static gboolean | |
99 plugin_is_duplicate(const gchar * filename) | |
100 { | |
101 GList *l; | |
102 const gchar *basename = g_basename(filename); | |
103 | |
104 /* FIXME: messy stuff */ | |
105 | |
106 for (l = ip_data.input_list; l; l = g_list_next(l)) | |
107 if (!strcmp(basename, g_basename(INPUT_PLUGIN(l->data)->filename))) | |
108 return TRUE; | |
109 | |
110 for (l = op_data.output_list; l; l = g_list_next(l)) | |
111 if (!strcmp(basename, g_basename(OUTPUT_PLUGIN(l->data)->filename))) | |
112 return TRUE; | |
113 | |
114 for (l = ep_data.effect_list; l; l = g_list_next(l)) | |
115 if (!strcmp(basename, g_basename(EFFECT_PLUGIN(l->data)->filename))) | |
116 return TRUE; | |
117 | |
118 for (l = gp_data.general_list; l; l = g_list_next(l)) | |
119 if (!strcmp(basename, g_basename(GENERAL_PLUGIN(l->data)->filename))) | |
120 return TRUE; | |
121 | |
122 for (l = vp_data.vis_list; l; l = g_list_next(l)) | |
123 if (!strcmp(basename, g_basename(VIS_PLUGIN(l->data)->filename))) | |
124 return TRUE; | |
125 | |
126 for (l = lowlevel_list; l; l = g_list_next(l)) | |
127 if (!strcmp(basename, g_basename(VIS_PLUGIN(l->data)->filename))) | |
128 return TRUE; | |
129 | |
130 return FALSE; | |
131 } | |
132 | |
133 | |
134 #define PLUGIN_GET_INFO(x) ((PluginGetInfoFunc)(x))() | |
135 typedef Plugin * (*PluginGetInfoFunc) (void); | |
136 | |
137 static void | |
138 input_plugin_init(Plugin * plugin) | |
139 { | |
140 InputPlugin *p = INPUT_PLUGIN(plugin); | |
141 | |
142 p->get_vis_type = input_get_vis_type; | |
143 p->add_vis_pcm = input_add_vis_pcm; | |
144 | |
145 /* Pretty const casts courtesy of XMMS's plugin.h legacy. Anyone | |
146 else thinks we could use a CONST macro to solve the warnings? | |
147 - descender */ | |
148 p->set_info = (void (*)(gchar *, gint, gint, gint, gint)) playlist_set_info_old_abi; | |
149 p->set_info_text = (void (*)(gchar *)) input_set_info_text; | |
150 p->set_status_buffering = (void (*)(gboolean)) input_set_status_buffering; | |
151 | |
152 ip_data.input_list = g_list_append(ip_data.input_list, p); | |
153 | |
154 g_hash_table_replace(plugin_matrix, g_path_get_basename(p->filename), | |
155 GINT_TO_POINTER(1)); | |
156 } | |
157 | |
158 static void | |
159 output_plugin_init(Plugin * plugin) | |
160 { | |
161 OutputPlugin *p = OUTPUT_PLUGIN(plugin); | |
162 op_data.output_list = g_list_append(op_data.output_list, p); | |
163 } | |
164 | |
165 static void | |
166 effect_plugin_init(Plugin * plugin) | |
167 { | |
168 EffectPlugin *p = EFFECT_PLUGIN(plugin); | |
169 ep_data.effect_list = g_list_append(ep_data.effect_list, p); | |
170 } | |
171 | |
172 static void | |
173 general_plugin_init(Plugin * plugin) | |
174 { | |
175 GeneralPlugin *p = GENERAL_PLUGIN(plugin); | |
176 p->xmms_session = ctrlsocket_get_session_id(); | |
177 gp_data.general_list = g_list_append(gp_data.general_list, p); | |
178 } | |
179 | |
180 static void | |
181 vis_plugin_init(Plugin * plugin) | |
182 { | |
183 VisPlugin *p = VIS_PLUGIN(plugin); | |
184 p->xmms_session = ctrlsocket_get_session_id(); | |
185 p->disable_plugin = vis_disable_plugin; | |
186 vp_data.vis_list = g_list_append(vp_data.vis_list, p); | |
187 } | |
188 | |
189 static void | |
190 lowlevel_plugin_init(Plugin * plugin) | |
191 { | |
192 LowlevelPlugin *p = LOWLEVEL_PLUGIN(plugin); | |
193 lowlevel_list = g_list_append(lowlevel_list, p); | |
194 } | |
195 | |
196 /* FIXME: Placed here (hopefully) temporarily - descender */ | |
197 | |
198 typedef struct { | |
199 const gchar *name; | |
200 const gchar *id; | |
201 void (*init)(Plugin *); | |
202 } PluginType; | |
203 | |
204 static PluginType plugin_types[] = { | |
205 { "input" , "get_iplugin_info", input_plugin_init }, | |
206 { "output" , "get_oplugin_info", output_plugin_init }, | |
207 { "effect" , "get_eplugin_info", effect_plugin_init }, | |
208 { "general" , "get_gplugin_info", general_plugin_init }, | |
209 { "visualization", "get_vplugin_info", vis_plugin_init }, | |
210 { "lowlevel" , "get_lplugin_info", lowlevel_plugin_init }, | |
211 { NULL, NULL, NULL } | |
212 }; | |
213 | |
214 static void | |
215 add_plugin(const gchar * filename) | |
216 { | |
217 PluginType *type; | |
218 GModule *module; | |
219 gpointer func; | |
220 | |
221 if (plugin_is_duplicate(filename)) | |
222 return; | |
223 | |
2623 | 224 g_message("Loaded plugin (%s)", filename); |
225 | |
2313 | 226 if (!(module = g_module_open(filename, G_MODULE_BIND_LOCAL))) { |
227 printf("Failed to load plugin (%s): %s\n", | |
228 filename, g_module_error()); | |
229 return; | |
230 } | |
231 | |
232 for (type = plugin_types; type->name; type++) | |
233 { | |
234 if (g_module_symbol(module, type->id, &func)) { | |
235 Plugin *plugin = PLUGIN_GET_INFO(func); | |
236 | |
237 plugin->handle = module; | |
238 plugin->filename = g_strdup(filename); | |
239 type->init(PLUGIN_GET_INFO(func)); | |
240 | |
241 return; | |
242 } | |
243 } | |
244 | |
245 printf("Invalid plugin (%s)\n", filename); | |
246 g_module_close(module); | |
247 } | |
248 | |
249 static gboolean | |
250 scan_plugin_func(const gchar * path, const gchar * basename, gpointer data) | |
251 { | |
252 if (!str_has_suffix_nocase(basename, SHARED_SUFFIX)) | |
253 return FALSE; | |
254 | |
255 if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) | |
256 return FALSE; | |
257 | |
258 add_plugin(path); | |
259 | |
260 return FALSE; | |
261 } | |
262 | |
263 static void | |
264 scan_plugins(const gchar * path) | |
265 { | |
266 dir_foreach(path, scan_plugin_func, NULL, NULL); | |
267 } | |
268 | |
269 void | |
270 plugin_system_init(void) | |
271 { | |
272 gchar *dir, **disabled; | |
273 GList *node; | |
274 OutputPlugin *op; | |
275 InputPlugin *ip; | |
276 LowlevelPlugin *lp; | |
277 gint dirsel = 0, i = 0; | |
278 | |
279 if (!g_module_supported()) { | |
280 report_error("Module loading not supported! Plugins will not be loaded.\n"); | |
281 return; | |
282 } | |
283 | |
2400
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
284 /* |
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
285 * FIXME: this collides in many cases. this is really bad. |
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
286 * i have no idea why we use a hashtable here, there is no point, when |
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
287 * a list of enabled plugins would realistically offer the same |
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
288 * scalability. - nenolod |
4daf4fa409e0
[svn] - make a note that hashtable collisions in the plugin matrix are not
nenolod
parents:
2373
diff
changeset
|
289 */ |
2313 | 290 plugin_matrix = g_hash_table_new_full(g_str_hash, g_int_equal, g_free, |
291 NULL); | |
292 | |
293 #ifndef DISABLE_USER_PLUGIN_DIR | |
294 scan_plugins(bmp_paths[BMP_PATH_USER_PLUGIN_DIR]); | |
295 /* | |
296 * This is in a separate loop so if the user puts them in the | |
297 * wrong dir we'll still get them in the right order (home dir | |
298 * first) - Zinx | |
299 */ | |
300 while (plugin_dir_list[dirsel]) { | |
301 dir = g_build_filename(bmp_paths[BMP_PATH_USER_PLUGIN_DIR], | |
302 plugin_dir_list[dirsel++], NULL); | |
303 scan_plugins(dir); | |
304 g_free(dir); | |
305 } | |
306 dirsel = 0; | |
307 #endif | |
308 | |
309 while (plugin_dir_list[dirsel]) { | |
310 dir = g_build_filename(PLUGIN_DIR, plugin_dir_list[dirsel++], NULL); | |
311 scan_plugins(dir); | |
312 g_free(dir); | |
313 } | |
314 | |
315 op_data.output_list = g_list_sort(op_data.output_list, outputlist_compare_func); | |
316 if (!op_data.current_output_plugin | |
317 && g_list_length(op_data.output_list)) { | |
318 op_data.current_output_plugin = op_data.output_list->data; | |
319 } | |
320 | |
321 ip_data.input_list = g_list_sort(ip_data.input_list, inputlist_compare_func); | |
322 | |
323 ep_data.effect_list = g_list_sort(ep_data.effect_list, effectlist_compare_func); | |
324 ep_data.enabled_list = NULL; | |
325 | |
326 gp_data.general_list = g_list_sort(gp_data.general_list, generallist_compare_func); | |
327 gp_data.enabled_list = NULL; | |
328 | |
329 vp_data.vis_list = g_list_sort(vp_data.vis_list, vislist_compare_func); | |
330 vp_data.enabled_list = NULL; | |
331 | |
332 general_enable_from_stringified_list(cfg.enabled_gplugins); | |
333 vis_enable_from_stringified_list(cfg.enabled_vplugins); | |
334 effect_enable_from_stringified_list(cfg.enabled_eplugins); | |
335 | |
336 g_free(cfg.enabled_gplugins); | |
337 cfg.enabled_gplugins = NULL; | |
338 | |
339 g_free(cfg.enabled_vplugins); | |
340 cfg.enabled_vplugins = NULL; | |
341 | |
342 g_free(cfg.enabled_eplugins); | |
343 cfg.enabled_eplugins = NULL; | |
344 | |
345 for (node = op_data.output_list; node; node = g_list_next(node)) { | |
346 op = OUTPUT_PLUGIN(node->data); | |
347 /* | |
348 * Only test basename to avoid problems when changing | |
349 * prefix. We will only see one plugin with the same | |
350 * basename, so this is usually what the user want. | |
351 */ | |
352 if (!strcmp(g_basename(cfg.outputplugin), g_basename(op->filename))) | |
353 op_data.current_output_plugin = op; | |
354 if (op->init) | |
355 op->init(); | |
356 } | |
357 | |
358 for (node = ip_data.input_list; node; node = g_list_next(node)) { | |
359 ip = INPUT_PLUGIN(node->data); | |
360 if (ip->init) | |
361 ip->init(); | |
362 } | |
363 | |
364 for (node = lowlevel_list; node; node = g_list_next(node)) { | |
365 lp = LOWLEVEL_PLUGIN(node->data); | |
366 if (lp->init) | |
367 lp->init(); | |
368 } | |
369 | |
370 if (cfg.disabled_iplugins) { | |
371 disabled = g_strsplit(cfg.disabled_iplugins, ":", 0); | |
372 while (disabled[i]) { | |
373 g_hash_table_replace(plugin_matrix, disabled[i], | |
374 GINT_TO_POINTER(FALSE)); | |
375 i++; | |
376 } | |
377 | |
378 g_free(disabled); | |
379 | |
380 g_free(cfg.disabled_iplugins); | |
381 cfg.disabled_iplugins = NULL; | |
382 } | |
383 } | |
384 | |
385 void | |
386 plugin_system_cleanup(void) | |
387 { | |
388 InputPlugin *ip; | |
389 OutputPlugin *op; | |
390 EffectPlugin *ep; | |
391 GeneralPlugin *gp; | |
392 VisPlugin *vp; | |
393 LowlevelPlugin *lp; | |
394 GList *node; | |
395 | |
396 g_message("Shutting down plugin system"); | |
397 | |
398 if (playback_get_playing()) { | |
399 ip_data.stop = TRUE; | |
400 playback_stop(); | |
401 ip_data.stop = FALSE; | |
402 } | |
403 | |
2623 | 404 /* FIXME: race condition -nenolod */ |
405 op_data.current_output_plugin = NULL; | |
406 | |
2313 | 407 for (node = get_input_list(); node; node = g_list_next(node)) { |
408 ip = INPUT_PLUGIN(node->data); | |
409 if (ip && ip->cleanup) { | |
410 ip->cleanup(); | |
411 GDK_THREADS_LEAVE(); | |
412 while (g_main_context_iteration(NULL, FALSE)); | |
413 GDK_THREADS_ENTER(); | |
414 } | |
415 g_module_close(ip->handle); | |
416 } | |
417 | |
2623 | 418 if (ip_data.input_list != NULL) |
419 { | |
2313 | 420 g_list_free(ip_data.input_list); |
2623 | 421 ip_data.input_list = NULL; |
422 } | |
2313 | 423 |
424 for (node = get_output_list(); node; node = g_list_next(node)) { | |
425 op = OUTPUT_PLUGIN(node->data); | |
426 if (op && op->cleanup) { | |
427 op->cleanup(); | |
428 GDK_THREADS_LEAVE(); | |
429 while (g_main_context_iteration(NULL, FALSE)); | |
430 GDK_THREADS_ENTER(); | |
431 } | |
432 g_module_close(op->handle); | |
433 } | |
434 | |
2623 | 435 if (op_data.output_list != NULL) |
436 { | |
2313 | 437 g_list_free(op_data.output_list); |
2623 | 438 op_data.output_list = NULL; |
439 } | |
2313 | 440 |
441 for (node = get_effect_list(); node; node = g_list_next(node)) { | |
442 ep = EFFECT_PLUGIN(node->data); | |
443 if (ep && ep->cleanup) { | |
444 ep->cleanup(); | |
445 GDK_THREADS_LEAVE(); | |
446 while (g_main_context_iteration(NULL, FALSE)); | |
447 GDK_THREADS_ENTER(); | |
448 } | |
449 g_module_close(ep->handle); | |
450 } | |
451 | |
2623 | 452 if (ep_data.effect_list != NULL) |
453 { | |
2313 | 454 g_list_free(ep_data.effect_list); |
2623 | 455 ep_data.effect_list = NULL; |
2313 | 456 } |
457 | |
458 for (node = get_general_list(); node; node = g_list_next(node)) { | |
459 gp = GENERAL_PLUGIN(node->data); | |
460 if (gp && gp->cleanup) { | |
461 gp->cleanup(); | |
462 GDK_THREADS_LEAVE(); | |
463 while (g_main_context_iteration(NULL, FALSE)); | |
464 GDK_THREADS_ENTER(); | |
465 } | |
466 g_module_close(gp->handle); | |
467 } | |
468 | |
2623 | 469 if (gp_data.general_list != NULL) |
470 { | |
2313 | 471 g_list_free(gp_data.general_list); |
2623 | 472 gp_data.general_list = NULL; |
2313 | 473 } |
474 | |
475 for (node = get_vis_list(); node; node = g_list_next(node)) { | |
476 vp = VIS_PLUGIN(node->data); | |
477 if (vp && vp->cleanup) { | |
478 vp->cleanup(); | |
479 GDK_THREADS_LEAVE(); | |
480 while (g_main_context_iteration(NULL, FALSE)); | |
481 GDK_THREADS_ENTER(); | |
482 } | |
483 g_module_close(vp->handle); | |
484 } | |
485 | |
2623 | 486 if (vp_data.vis_list != NULL) |
487 { | |
2313 | 488 g_list_free(vp_data.vis_list); |
2623 | 489 vp_data.vis_list = NULL; |
490 } | |
2313 | 491 |
492 for (node = lowlevel_list; node; node = g_list_next(node)) { | |
493 lp = LOWLEVEL_PLUGIN(node->data); | |
494 if (lp && lp->cleanup) { | |
495 lp->cleanup(); | |
496 GDK_THREADS_LEAVE(); | |
497 while (g_main_context_iteration(NULL, FALSE)); | |
498 GDK_THREADS_ENTER(); | |
499 } | |
500 g_module_close(lp->handle); | |
501 } | |
502 | |
2623 | 503 if (lowlevel_list != NULL) |
504 { | |
2313 | 505 g_list_free(lowlevel_list); |
2623 | 506 lowlevel_list = NULL; |
507 } | |
508 | |
509 /* XXX: vfs will crash otherwise. -nenolod */ | |
510 if (vfs_transports != NULL) | |
511 { | |
512 g_list_free(vfs_transports); | |
513 vfs_transports = NULL; | |
514 } | |
2313 | 515 } |