~ubuntu-branches/ubuntu/utopic/glame/utopic

« back to all changes in this revision

Viewing changes to src/glmid/glmid.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-09 17:14:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020409171412-jzpnov7mbz2w6zsr
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * glmid.c
 
3
 *
 
4
 * Copyright (C) 2000 Richard Guenther
 
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 <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <dirent.h>
 
29
#include <unistd.h>
 
30
#include <string.h>
 
31
#include <guile/gh.h>
 
32
#if 1
 
33
#include <ltdl.h>
 
34
#endif
 
35
#include "glame_hash.h"
 
36
#include "swapfile.h"
 
37
#include "filter.h"
 
38
#include "glplugin.h"
 
39
#include "glsimd.h"
 
40
#include "glscript.h"
 
41
#include "glconfig.h"
 
42
 
 
43
/* auto-generated builtin register function */
 
44
#include "../plugins/builtins.c"
 
45
 
 
46
 
 
47
 
 
48
static int is_scm(const char *fname)
 
49
{
 
50
        FILE *f;
 
51
        char c;
 
52
 
 
53
        if (!(f = fopen(fname, "r")))
 
54
                return 0;
 
55
 
 
56
        if (fscanf(f, " %c", &c) != 1
 
57
            || (c != '(' && c != ';')) {
 
58
                fclose(f);
 
59
                return 0;
 
60
        }
 
61
 
 
62
        fclose(f);
 
63
        return 1;
 
64
}
 
65
 
 
66
int glame_load_plugin(const char *fname)
 
67
{
 
68
        SCM s_res;
 
69
 
 
70
        /* Shared object plugin type? Just try to load it as such, will
 
71
         * fail if it is not. */
 
72
        if (plugin_load(fname) == 0)
 
73
                return 0;
 
74
 
 
75
        /* Scheme macro plugin type? We really can't detect, if
 
76
         * it is or not... hope guile doesnt choke on random garbage...
 
77
         */
 
78
        if (!is_scm(fname))
 
79
                return -1;
 
80
 
 
81
        glscript_load_mode = 0;
 
82
        s_res = glame_gh_safe_eval_file(fname);
 
83
        if (gh_boolean_p(s_res) && !gh_scm2bool(s_res))
 
84
                return -1;
 
85
        return 0;
 
86
}
 
87
 
 
88
filter_t *glame_load_instance(const char *fname)
 
89
{
 
90
        SCM s_res;
 
91
 
 
92
        if (!is_scm(fname))
 
93
                return NULL;
 
94
 
 
95
        glscript_load_mode = 1;
 
96
        s_res = glame_gh_safe_eval_file(fname);
 
97
        if (gh_boolean_p(s_res) && !gh_scm2bool(s_res))
 
98
                return NULL;
 
99
        return last_loaded_filter_instance; /* HACK... */
 
100
}
 
101
 
 
102
static void add_plugin_path(const char *path)
 
103
{
 
104
        char *tmp, *dir, *p;
 
105
        DIR *d;
 
106
 
 
107
        if (!path || !(tmp = p = strdup(path)))
 
108
                return;
 
109
 
 
110
        dir = p;
 
111
        do {
 
112
                if ((p = strchr(p, ':')))
 
113
                        *p++ = '\0';
 
114
 
 
115
                /* Try to open the directory and add the path to
 
116
                 * the plugin subsystem. */
 
117
                if ((d = opendir(dir))) {
 
118
                        closedir(d);
 
119
                        plugin_add_path(dir);
 
120
                }
 
121
 
 
122
                dir = p;
 
123
        } while (dir);
 
124
 
 
125
        free(tmp);
 
126
}
 
127
 
 
128
static void load_plugins_from_directory(const char *dir)
 
129
{
 
130
        DIR *d;
 
131
        struct dirent *e;
 
132
 
 
133
        /* Try to open the directory. */
 
134
        if (!(d = opendir(dir)))
 
135
                return;
 
136
 
 
137
        /* Try to load every file as plugin. */
 
138
        while ((e = readdir(d))) {
 
139
                char fname[256];
 
140
                struct stat st;
 
141
                char *ext;
 
142
 
 
143
                /* Only take .so and .scm, but not glame.scm */
 
144
                if (!(ext = strrchr(e->d_name, '.'))
 
145
                    || !(strcmp(ext, ".scm") == 0
 
146
                         || strcmp(ext, ".la") == 0
 
147
                         || strcmp(ext, ".so") == 0)
 
148
                    || strcmp(e->d_name, "glame.scm") == 0)
 
149
                        continue;
 
150
 
 
151
                snprintf(fname, 255, "%s/%s", dir, e->d_name);
 
152
                if (stat(fname, &st) == -1)
 
153
                        continue;
 
154
                if (!S_ISREG(st.st_mode) || !(st.st_mode & S_IRUSR))
 
155
                        continue;
 
156
                glame_load_plugin(fname);
 
157
        }
 
158
 
 
159
        closedir(d);
 
160
}
 
161
 
 
162
static void load_plugins_from_path(const char *path)
 
163
{
 
164
        char *tmp, *dir, *p;
 
165
 
 
166
        if (!path || !(tmp = p = strdup(path)))
 
167
                return;
 
168
 
 
169
        dir = p;
 
170
        do {
 
171
                if ((p = strchr(p, ':')))
 
172
                        *p++ = '\0';
 
173
                load_plugins_from_directory(dir);
 
174
                dir = p;
 
175
        } while (dir);
 
176
 
 
177
        free(tmp);
 
178
}
 
179
 
 
180
static int plugins_register()
 
181
{
 
182
        /* Add plugin paths. */
 
183
        if (!access("cglame.c", F_OK))
 
184
                add_plugin_path("./plugins/.libs");
 
185
        add_plugin_path(PKGLIBDIR);
 
186
#ifdef HAVE_LADSPA
 
187
        add_plugin_path(getenv("LADSPA_PATH"));
 
188
        add_plugin_path("/usr/lib/ladspa");
 
189
        add_plugin_path("/usr/local/lib/ladspa");
 
190
#endif
 
191
 
 
192
        /* First initialize the builtin plugins */
 
193
        register_builtins();
 
194
 
 
195
        /* Plugins from default paths - and "debug path" (first) */
 
196
        if (!access("cglame.c", F_OK))
 
197
                load_plugins_from_path("./plugins/.libs");
 
198
        load_plugins_from_path(PKGLIBDIR);
 
199
#ifdef HAVE_LADSPA
 
200
        load_plugins_from_path(getenv("LADSPA_PATH"));
 
201
        load_plugins_from_path("/usr/lib/ladspa");
 
202
        load_plugins_from_path("/usr/local/lib/ladspa");
 
203
#endif
 
204
 
 
205
        return 0;
 
206
}
 
207
 
 
208
static void glame_cleanup()
 
209
{
 
210
        swapfile_close();
 
211
}
 
212
 
 
213
static void (*glame_main)();
 
214
static void init_after_guile(int argc, char **argv)
 
215
{
 
216
#if !defined NDEBUG
 
217
        /* We dont like guiles signal handlers for debugging
 
218
         * purposes. */
 
219
        scm_restore_signals();
 
220
#endif
 
221
 
 
222
        lt_dlinit();
 
223
 
 
224
        /* Init lowlevel GLAME subsystems. */
 
225
        glsimd_init(0);
 
226
        if (hash_alloc() == -1)
 
227
                exit(1);
 
228
        atexit(glame_cleanup);
 
229
 
 
230
        /* Load non-scheme plugins. */
 
231
        if (plugins_register() == -1)
 
232
                exit(1);
 
233
 
 
234
        /* Init scripting. */
 
235
        if (glscript_init() == -1)
 
236
                exit(1);
 
237
        add_plugin_path(PKGSCRIPTSDIR);
 
238
        if (!access("cglame.c", F_OK))
 
239
                load_plugins_from_path("./plugins"); /* for .scm */
 
240
        load_plugins_from_path(PKGSCRIPTSDIR);
 
241
 
 
242
        /* Load configuration. */
 
243
        glame_config_load();
 
244
 
 
245
        /* Set defaults, where we dont have any reasonable place/time
 
246
         * to do this. */
 
247
        glame_config_get_long_with_default("swapfile/maxundo", 5);
 
248
        _GLAME_WBUFSIZE = glame_config_get_long_with_default(
 
249
                "filter/wbufsize", 1024);
 
250
 
 
251
        glame_main();
 
252
}
 
253
 
 
254
int glame_init(void (*main)(void), int argc, char **argv)
 
255
{
 
256
        glame_main = main;
 
257
        gh_enter(argc, argv, init_after_guile);
 
258
        return 0;
 
259
}
 
260
 
 
261
plugin_t *glame_create_plugin(filter_t *filter, const char *name)
 
262
{
 
263
        plugin_t *plugin;
 
264
 
 
265
        if (!name || !filter)
 
266
                return NULL;
 
267
        if (!(plugin = plugin_add(name)))
 
268
                return NULL;
 
269
        if (filter_register(filter, plugin) == -1) {
 
270
                _plugin_delete(plugin);
 
271
                return NULL;
 
272
        }
 
273
        return plugin;
 
274
}