---
lib/Makefile.am | 6 +-
lib/plugin.h | 49 +++++++++++++++++++++
lib/plugin_loader.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++
lib/plugin_loader.h | 35 +++++++++++++++
4 files changed, 207 insertions(+), 3 deletions(-)
create mode 100644 lib/plugin.h
create mode 100644 lib/plugin_loader.c
create mode 100644 lib/plugin_loader.h
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 3f7f560..9bb2194 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,7 +1,7 @@
AM_CPPFLAGS=-I$(top_srcdir)/plugins -I$(top_srcdir)/include
lib_LTLIBRARIES=libgregorio.la
-libgregorio_la_SOURCES = struct.c messages.c
+libgregorio_la_SOURCES = struct.c messages.c plugin_loader.c
libgregorio_la_LDFLAGS=-no-undefined
-libgregorio_la_LIBADD = $(LIBINTL)
-pkginclude_HEADERS = struct.h messages.h
+libgregorio_la_LIBADD = $(LIBINTL) $(LIBTOOL_LIB)
+pkginclude_HEADERS = struct.h messages.h plugin_loader.h plugin.h
diff --git a/lib/plugin.h b/lib/plugin.h
new file mode 100644
index 0000000..76151db
--- /dev/null
+++ b/lib/plugin.h
@@ -0,0 +1,49 @@
+/*
+ * Gregorio plugin declaration headers.
+ * Copyright (C) 2008 Jeremie Corbier
+ *
+ * This program is free software; you can redistribute it and/or modify it
under
+ * the terms of the GNU General Public License as published by the Free
Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef PLUGIN_H
+#define PLUGIN_H
+
+#include <stdio.h>
+#include "struct.h"
+
+#define GREGORIO_PLUGIN_INPUT 1
+#define GREGORIO_PLUGIN_OUTPUT 2
+#define GREGORIO_PLUGIN_BOTH GREGORIO_PLUGIN_INPUT | GREGORIO_PLUGIN_OUTPUT
+
+typedef gregorio_score *(*gregorio_read_func)(FILE *);
+typedef void (*gregorio_write_func)(FILE *, gregorio_score *);
+
+typedef struct {
+ char *id;
+ char *name;
+ char *author;
+ char *description;
+
+ char *file_extension;
+
+ int type;
+
+ gregorio_read_func read;
+ gregorio_write_func write;
+} gregorio_plugin_info;
+
+#define DECLARE_PLUGIN(_id) gregorio_plugin_info _id##_LTX_info =
+
+#endif
diff --git a/lib/plugin_loader.c b/lib/plugin_loader.c
new file mode 100644
index 0000000..75884b6
--- /dev/null
+++ b/lib/plugin_loader.c
@@ -0,0 +1,120 @@
+/*
+ * Gregorio plugin loader.
+ * Copyright (C) 2008 Jeremie Corbier
+ *
+ * This program is free software; you can redistribute it and/or modify it
under
+ * the terms of the GNU General Public License as published by the Free
Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <ltdl.h>
+
+#include "plugin_loader.h"
+
+int gregorio_plugin_loader_init ()
+{
+ return lt_dlinit();
+}
+
+int gregorio_plugin_loader_exit ()
+{
+ return lt_dlexit();
+}
+
+/*
+ * Holds LTDL handle and plugin infos - should only be accessed through
+ * gregorio_plugin_get_info.
+ */
+struct gregorio_plugin {
+ gregorio_plugin_info *info;
+ lt_dlhandle handle;
+};
+
+/*
+ * Internal struct - workaround to pass two parameters to lt_dlforeachfiles
+ * utility function
+ */
+struct load_params {
+ const char *id;
+ gregorio_plugin *plugin;
+};
+
+static int do_load_plugin(const char *filename, void *data)
+{
+ struct load_params *params = (struct load_params *)data;
+
+ params->plugin->handle = lt_dlopenext(filename);
+ if (params->plugin->handle == NULL)
+ {
+ return 0;
+ }
+
+ params->plugin->info = lt_dlsym(params->plugin->handle, "info");
+ if (params->plugin->info == NULL)
+ {
+ goto end;
+ }
+
+ if (strcmp(params->plugin->info->id, params->id) == 0)
+ {
+ return 1;
+ }
+
+end:
+ lt_dlclose(params->plugin->handle);
+ params->plugin->handle = NULL;
+ return 0;
+}
+
+gregorio_plugin *gregorio_plugin_load (const char *path, const char *id)
+{
+ struct load_params params;
+
+ params.id = id;
+ params.plugin = malloc (sizeof(gregorio_plugin));
+
+ if (params.plugin == NULL)
+ {
+ return NULL;
+ }
+
+ /* Find the first plugin in path matching params.id */
+ lt_dlforeachfile (path, do_load_plugin, (void *)¶ms);
+
+ if (params.plugin->handle == NULL)
+ {
+ free(params.plugin);
+ return NULL;
+ }
+
+ return params.plugin;
+}
+
+void gregorio_plugin_unload (gregorio_plugin *plugin)
+{
+ if (plugin != NULL)
+ {
+ lt_dlclose(plugin->handle);
+ free(plugin);
+ }
+}
+
+gregorio_plugin_info *gregorio_plugin_get_info(gregorio_plugin *plugin)
+{
+ if (plugin != NULL)
+ {
+ return plugin->info;
+ }
+ return NULL;
+}
diff --git a/lib/plugin_loader.h b/lib/plugin_loader.h
new file mode 100644
index 0000000..4e4b046
--- /dev/null
+++ b/lib/plugin_loader.h
@@ -0,0 +1,35 @@
+/*
+ * Gregorio plugin loader headers.
+ * Copyright (C) 2008 Jeremie Corbier
+ *
+ * This program is free software; you can redistribute it and/or modify it
under
+ * the terms of the GNU General Public License as published by the Free
Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef PLUGIN_LOADER_H
+#define PLUGIN_LOADER_H
+
+#include "plugin.h"
+
+typedef struct gregorio_plugin gregorio_plugin;
+
+int gregorio_plugin_loader_init();
+int gregorio_plugin_loader_exit();
+
+gregorio_plugin *gregorio_plugin_load(const char *path, const char *id);
+void gregorio_plugin_unload(gregorio_plugin *plugin);
+
+gregorio_plugin_info *gregorio_plugin_get_info(gregorio_plugin *plugin);
+
+#endif
--
1.5.6.5
_______________________________________________
Gregorio-devel mailing list
[email protected]
https://mail.gna.org/listinfo/gregorio-devel