root/sweep/trunk/src/plugin.c

Revision 684, 3.0 kB (checked in by erikd, 2 years ago)

Reduce memory leakage in plugins.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <sys/stat.h>
32
33 #include <glib.h>
34 #include <gmodule.h>
35
36 #include <sweep/sweep_i18n.h>
37 #include <sweep/sweep_version.h>
38 #include <sweep/sweep_types.h>
39
40 #include "sweep_compat.h"
41
42 GList * plugins = NULL;
43
44 static GList * module_list = NULL;
45
46 static gint
47 cmp_proc_names (sw_procedure * a, sw_procedure * b)
48 {
49   return strcmp (_(a->name), _(b->name));
50 }
51
52 static void
53 sweep_plugin_init (const gchar * path)
54 {
55   GModule * module;
56   sw_plugin * m_plugin;
57   gpointer  m_plugin_ptr;
58   GList * gl;
59
60   module = g_module_open (path, G_MODULE_BIND_LAZY);
61
62   if (!module) {
63 #ifdef DEBUG
64     fprintf (stderr, "sweep_plugin_init: Error opening %s: %s\n",
65              path, g_module_error());
66 #endif
67     return;
68   }
69  
70   if (g_module_symbol (module, "plugin", &m_plugin_ptr)) {
71         m_plugin = (sw_plugin *)m_plugin_ptr;
72         module_list = g_list_append (module_list, m_plugin);
73     for (gl = m_plugin->plugin_init (); gl; gl = gl->next) {
74       plugins = g_list_insert_sorted (plugins, (sw_procedure *)gl->data,
75                                       (GCompareFunc)cmp_proc_names);
76     }
77   }
78 }
79
80 static void
81 init_dynamic_plugins_dir (gchar * dirname)
82 {
83   DIR * dir;
84   struct dirent * dirent;
85   char * name, * path;
86   struct stat statbuf;
87
88   dir = opendir (dirname);
89   if (!dir) {
90     /* fail silently */
91     return;
92   }
93
94   while ((dirent = readdir (dir)) != NULL) {
95     name = dirent->d_name;
96     path = g_module_build_path (PACKAGE_PLUGIN_DIR, dirent->d_name);
97     if (stat (path, &statbuf) == -1) {
98       /* system error -- non-fatal, ignore for plugin loading */
99     } else if (sw_stat_regular (statbuf.st_mode)) {
100       sweep_plugin_init (path);
101     }
102   }
103
104   closedir (dir);
105 }
106
107 /* Initialise dynamically linked plugins */
108 static void
109 init_dynamic_plugins (void)
110 {
111   init_dynamic_plugins_dir (PACKAGE_PLUGIN_DIR);
112 }
113
114 static void
115 release_one (gpointer data, gpointer user_data)
116 {
117   sw_plugin *p = (sw_plugin *) data;
118   if (p && p->plugin_cleanup)
119     p->plugin_cleanup ();
120 }
121
122
123 void
124 init_plugins (void)
125 {
126   if (g_module_supported ()) {
127     init_dynamic_plugins ();
128   }
129 }
130
131 void
132 release_plugins (void)
133 {
134   g_list_foreach (module_list, release_one, NULL);
135   g_list_free (plugins);
136   plugins = NULL;
137 }
Note: See TracBrowser for help on using the browser.