From d836e9913e3fa991c935a8e7d5064f9adb51e191 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 25 Oct 2009 17:15:10 +0000 Subject: [PATCH] Allow applications to specify plugin and config paths --- Makefile.am | 4 +-- gen.sh | 6 ++--- src/Makefile.am | 2 +- src/gis | 1 + src/gis-plugin.c | 69 ++++++++++++++++++++++++++++++++---------------- src/gis-plugin.h | 17 +++++++----- src/gis-prefs.c | 23 ++++++++-------- src/gis-prefs.h | 2 +- src/gis_test.c | 4 +-- src/libgis.pc.in | 2 +- 10 files changed, 80 insertions(+), 50 deletions(-) create mode 120000 src/gis diff --git a/Makefile.am b/Makefile.am index 3373886..3f41410 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,12 +24,12 @@ depscan: --book=atk \ --book=gdk \ --book=gdk-pixbuf \ - --book=glib \ --book=gio \ + --book=glib \ --book=gobject \ --book=gtk \ - --book=pango \ --book=libsoup-2.4 \ + --book=pango \ --list-unknown \ "$@" \ src/*.c diff --git a/gen.sh b/gen.sh index 8c2b18f..a6e66e3 100755 --- a/gen.sh +++ b/gen.sh @@ -1,8 +1,8 @@ #!/bin/bash dir=$(dirname $(readlink -f $0)) ./autogen.sh \ - "--libdir=$dir/src/plugins" \ - "--datadir=$dir/data" \ - --enable-gtk-doc \ + "--enable-gtk-doc" \ + "--libdir=$dir/src/.libs" \ + "--includedir=$dir/src" \ CFLAGS="-g -Werror -Wno-unused $CFLAGS" \ LDFLAGS="-Wl,-z,defs" diff --git a/src/Makefile.am b/src/Makefile.am index bb6a37c..aba03b7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,7 +29,7 @@ libgis_la_SOURCES = \ wms.c wms.h \ gpqueue.c gpqueue.h libgis_la_CPPFLAGS = $(AM_CPPFLAGS) \ - -DDATADIR="\"$(datadir)\"" -DPLUGINDIR="\"$(libdir)/gis\"" + -DPKGDATADIR="\"$(datadir)/gis\"" -DPLUGINSDIR="\"$(libdir)/gis\"" libgis_la_LIBADD = $(AM_LDADD) pkgconfigdir = $(libdir)/pkgconfig diff --git a/src/gis b/src/gis new file mode 120000 index 0000000..945c9b4 --- /dev/null +++ b/src/gis @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/src/gis-plugin.c b/src/gis-plugin.c index 4bfab58..c874095 100644 --- a/src/gis-plugin.c +++ b/src/gis-plugin.c @@ -68,35 +68,49 @@ typedef struct { GisPlugin *plugin; } GisPluginStore; -GisPlugins *gis_plugins_new() +GisPlugins *gis_plugins_new(gchar *dir) { - return g_ptr_array_new(); + g_debug("GisPlugins: new - dir=%s", dir); + GisPlugins *plugins = g_new0(GisPlugins, 1); + if (dir) + plugins->dir = g_strdup(dir); + plugins->plugins = g_ptr_array_new(); + return plugins; } void gis_plugins_free(GisPlugins *self) { - for (int i = 0; i < self->len; i++) { - GisPluginStore *store = g_ptr_array_index(self, i); + g_debug("GisPlugins: free"); + for (int i = 0; i < self->plugins->len; i++) { + GisPluginStore *store = g_ptr_array_index(self->plugins, i); g_object_unref(store->plugin); g_free(store->name); g_free(store); - g_ptr_array_remove_index(self, i); + g_ptr_array_remove_index(self->plugins, i); } - g_ptr_array_free(self, TRUE); + g_ptr_array_free(self->plugins, TRUE); + if (self->dir) + g_free(self->dir); + g_free(self); } -GList *gis_plugins_available() +GList *gis_plugins_available(GisPlugins *self) { - GDir *dir = g_dir_open(PLUGINDIR, 0, NULL); - if (dir == NULL) - return NULL; + g_debug("GisPlugins: available"); GList *list = NULL; - const gchar *name; - while ((name = g_dir_read_name(dir))) { - if (g_pattern_match_simple("*.so", name)) { - gchar **parts = g_strsplit(name, ".", 2); - list = g_list_prepend(list, g_strdup(parts[0])); - g_strfreev(parts); + gchar *dirs[] = {self->dir, PLUGINSDIR}; + for (int i = 0; i<2; i++) { + GDir *dir = g_dir_open(dirs[i], 0, NULL); + if (dir == NULL) + continue; + g_debug(" checking %s", dirs[i]); + const gchar *name; + while ((name = g_dir_read_name(dir))) { + if (g_pattern_match_simple("*.so", name)) { + gchar **parts = g_strsplit(name, ".", 2); + list = g_list_prepend(list, g_strdup(parts[0])); + g_strfreev(parts); + } } } return list; @@ -105,7 +119,14 @@ GList *gis_plugins_available() GisPlugin *gis_plugins_load(GisPlugins *self, const char *name, GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs) { - gchar *path = g_strdup_printf("%s/%s.%s", PLUGINDIR, name, G_MODULE_SUFFIX); + g_debug("GisPlugins: load %s", name); + gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX); + if (!g_file_test(path, G_FILE_TEST_EXISTS)) + path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX); + if (!g_file_test(path, G_FILE_TEST_EXISTS)) { + g_warning("Module %s not found", name); + return NULL; + } GModule *module = g_module_open(path, 0); g_free(path); if (module == NULL) { @@ -128,29 +149,31 @@ GisPlugin *gis_plugins_load(GisPlugins *self, const char *name, GisPluginStore *store = g_malloc(sizeof(GisPluginStore)); store->name = g_strdup(name); store->plugin = constructor(world, view, opengl, prefs); - g_ptr_array_add(self, store); + g_ptr_array_add(self->plugins, store); return store->plugin; } gboolean gis_plugins_unload(GisPlugins *self, const char *name) { - for (int i = 0; i < self->len; i++) { - GisPluginStore *store = g_ptr_array_index(self, i); + g_debug("GisPlugins: unload %s", name); + for (int i = 0; i < self->plugins->len; i++) { + GisPluginStore *store = g_ptr_array_index(self->plugins, i); if (g_str_equal(store->name, name)) { g_object_unref(store->plugin); g_free(store->name); g_free(store); - g_ptr_array_remove_index(self, i); + g_ptr_array_remove_index(self->plugins, i); } } return FALSE; } void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data) { + g_debug("GisPlugins: foreach"); typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer); CBFunc callback = (CBFunc)_callback; - for (int i = 0; i < self->len; i++) { - GisPluginStore *store = g_ptr_array_index(self, i); + for (int i = 0; i < self->plugins->len; i++) { + GisPluginStore *store = g_ptr_array_index(self->plugins, i); callback(store->plugin, store->name, user_data); } } diff --git a/src/gis-plugin.h b/src/gis-plugin.h index a667fba..6390f47 100644 --- a/src/gis-plugin.h +++ b/src/gis-plugin.h @@ -28,7 +28,7 @@ typedef struct _GisPlugin GisPlugin; typedef struct _GisPluginInterface GisPluginInterface; -typedef GPtrArray GisPlugins; +typedef struct _GisPlugins GisPlugins; struct _GisPluginInterface { @@ -51,14 +51,19 @@ GtkWidget *gis_plugin_get_config(GisPlugin *self); #include "gis-opengl.h" #include "gis-prefs.h" +struct _GisPlugins { + gchar *dir; + GPtrArray *plugins; +}; + typedef GisPlugin *(*GisPluginConstructor)(GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs); -GisPlugins *gis_plugins_new(); +GisPlugins *gis_plugins_new(gchar *dir); void gis_plugins_free(); -GList *gis_plugins_available(); -GisPlugin *gis_plugins_load(GisPlugins *self, const char *name, +GList *gis_plugins_available(GisPlugins *plugins); +GisPlugin *gis_plugins_load(GisPlugins *plugins, const char *name, GisWorld *world, GisView *view, GisOpenGL *opengl, GisPrefs *prefs); -gboolean gis_plugins_unload(GisPlugins *self, const char *name); -void gis_plugins_foreach(GisPlugins *self, GCallback callback, gpointer user_data); +gboolean gis_plugins_unload(GisPlugins *plugins, const char *name); +void gis_plugins_foreach(GisPlugins *plugins, GCallback callback, gpointer user_data); #endif diff --git a/src/gis-prefs.c b/src/gis-prefs.c index abae1a2..4a1ee0d 100644 --- a/src/gis-prefs.c +++ b/src/gis-prefs.c @@ -30,27 +30,28 @@ static guint signals[NUM_SIGNALS]; /*********** * Methods * ***********/ -GisPrefs *gis_prefs_new(const gchar *prog) +GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults) { - g_debug("GisPrefs: new - %s", prog); + g_debug("GisPrefs: new - %s, %s", config, defaults); GisPrefs *self = g_object_new(GIS_TYPE_PREFS, NULL); - self->key_path = g_build_filename(g_get_user_config_dir(), - prog, "config.ini", NULL); + if (config) + self->key_path = g_strdup(config); + else + self->key_path = g_build_filename(g_get_user_config_dir(), + "gis", "config.ini", NULL); GError *error = NULL; g_key_file_load_from_file(self->key_file, self->key_path, G_KEY_FILE_KEEP_COMMENTS, &error); - if (error) { - g_debug("GisPrefs: new - Trying %s defaults", prog); + if (error && defaults) { + g_debug("GisPrefs: new - Trying %s defaults", defaults); g_clear_error(&error); - gchar *tmp = g_build_filename(DATADIR, prog, "defaults.ini", NULL); - g_key_file_load_from_file(self->key_file, tmp, + g_key_file_load_from_file(self->key_file, defaults, G_KEY_FILE_KEEP_COMMENTS, &error); - g_free(tmp); } if (error) { g_debug("GisPrefs: new - Trying GIS defaults"); g_clear_error(&error); - gchar *tmp = g_build_filename(DATADIR, "gis", "defaults.ini", NULL); + gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL); g_key_file_load_from_file(self->key_file, tmp, G_KEY_FILE_KEEP_COMMENTS, &error); g_free(tmp); @@ -116,7 +117,7 @@ static void gis_prefs_init(GisPrefs *self) static GObject *gis_prefs_constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) { - g_debug("gis_prefs: constructor"); + g_debug("GisPrefs: constructor"); GObjectClass *parent_class = G_OBJECT_CLASS(gis_prefs_parent_class); return parent_class->constructor(gtype, n_properties, properties); } diff --git a/src/gis-prefs.h b/src/gis-prefs.h index 1645734..ac9cde6 100644 --- a/src/gis-prefs.h +++ b/src/gis-prefs.h @@ -48,7 +48,7 @@ struct _GisPrefsClass { GType gis_prefs_get_type(void); /* Methods */ -GisPrefs *gis_prefs_new(const gchar *prog); +GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults); gchar *gis_prefs_get_string (GisPrefs *prefs, const gchar *key); gboolean gis_prefs_get_boolean (GisPrefs *prefs, const gchar *key); diff --git a/src/gis_test.c b/src/gis_test.c index fbe46a9..d587936 100644 --- a/src/gis_test.c +++ b/src/gis_test.c @@ -44,8 +44,8 @@ int main(int argc, char **argv) gtk_init(&argc, &argv); g_thread_init(NULL); - GisPrefs *prefs = gis_prefs_new("aweather"); - GisPlugins *plugins = gis_plugins_new(); + GisPrefs *prefs = gis_prefs_new(NULL, NULL); + GisPlugins *plugins = gis_plugins_new(NULL); GisWorld *world = gis_world_new(); GisView *view = gis_view_new(); GisOpenGL *opengl = gis_opengl_new(world, view, plugins); diff --git a/src/libgis.pc.in b/src/libgis.pc.in index 859289e..031cb4a 100644 --- a/src/libgis.pc.in +++ b/src/libgis.pc.in @@ -8,4 +8,4 @@ Description: GIS Library for Gtk+ applications Version: @VERSION@ Requires: gmodule-2.0 gtk+-2.0 gtkglext-1.0 libsoup-2.4 Libs: -L${libdir} -lgis -Cflags: -I${includedir}/gis +Cflags: -I${includedir} -- 2.43.2