/
module.cpp
211 lines (176 loc) · 5.56 KB
/
module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* === S Y N F I G ========================================================= */
/*! \file synfig/module.cpp
** \brief writeme
**
** $Id$
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
**
** This package 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 package 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.
** \endlegal
*/
/* ========================================================================= */
/* === H E A D E R S ======================================================= */
#ifdef USING_PCH
# include "pch.h"
#else
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ETL/stringf>
#include "module.h"
#include "general.h"
#include <synfig/localization.h>
#include "type.h"
#include <glibmm.h>
#ifndef USE_CF_BUNDLES
#include <ltdl.h>
#endif
#endif
/* === M A C R O S ========================================================= */
/* === G L O B A L S ======================================================= */
using namespace std;
using namespace etl;
using namespace synfig;
Module::Book *synfig::Module::book_;
/* === P R O C E D U R E S ================================================= */
bool
Module::subsys_init(const String &prefix)
{
#ifndef USE_CF_BUNDLES
#ifndef SYNFIG_LTDL_NO_STATIC
//LTDL_SET_PRELOADED_SYMBOLS();
#endif
if(lt_dlinit())
{
error(_("Errors on lt_dlinit()"));
error(lt_dlerror());
return false;
}
lt_dladdsearchdir(".");
if(getenv("HOME"))
lt_dladdsearchdir(strprintf("%s/.local/share/synfig/modules", getenv("HOME")).c_str());
lt_dladdsearchdir((Glib::locale_from_utf8(prefix) + \
ETL_DIRECTORY_SEPARATOR + "lib" + \
ETL_DIRECTORY_SEPARATOR + "synfig" + \
ETL_DIRECTORY_SEPARATOR + "modules" ).c_str());
#ifdef LIBDIR
lt_dladdsearchdir(LIBDIR"/synfig/modules");
#endif
#ifdef __APPLE__
lt_dladdsearchdir("/Library/Frameworks/synfig.framework/Resources/modules");
#endif
lt_dladdsearchdir("/usr/local/lib/synfig/modules");
lt_dladdsearchdir(".");
#endif
book_=new Book;
return true;
}
bool
Module::subsys_stop()
{
delete book_;
#ifndef USE_CF_BUNDLES
lt_dlexit();
#endif
return true;
}
void
Module::register_default_modules(ProgressCallback *callback)
{
#define REGISTER_MODULE(module) if (!Register(module, callback)) \
throw std::runtime_error(strprintf(_("Unable to load module '%s'"), module))
REGISTER_MODULE("lyr_freetype");
REGISTER_MODULE("mod_geometry");
REGISTER_MODULE("mod_gradient");
REGISTER_MODULE("mod_particle");
}
Module::Book&
Module::book()
{
return *book_;
}
void
synfig::Module::Register(Module::Handle mod)
{
book()[mod->Name()]=mod;
}
bool
synfig::Module::Register(const String &module_name, ProgressCallback *callback)
{
#ifndef USE_CF_BUNDLES
// reset error string
lt_dlerror();
lt_dlhandle module;
if(callback)callback->task(strprintf(_("Attempting to register \"%s\""),module_name.c_str()));
module=lt_dlopenext((string("lib")+module_name).c_str());
if(!module)module=lt_dlopenext(module_name.c_str());
Type::initialize_all();
if(!module)
{
if(callback)callback->warning(strprintf(_("Unable to find module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
return false;
}
if(callback)callback->task(strprintf(_("Found module \"%s\""),module_name.c_str()));
Module::constructor_type constructor=NULL;
Handle mod;
if(!constructor)
{
// if(callback)callback->task(string("looking for -> ")+module_name+"_LTX_new_instance()");
constructor=(Module::constructor_type )lt_dlsym(module,(module_name+"_LTX_new_instance").c_str());
}
if(!constructor)
{
// if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
constructor=(Module::constructor_type )lt_dlsym(module,(string("lib")+module_name+"_LTX_new_instance").c_str());
}
if(!constructor)
{
// if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
constructor=(Module::constructor_type )lt_dlsym(module,(string("_lib")+module_name+"_LTX_new_instance").c_str());
}
if(!constructor)
{
// if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
constructor=(Module::constructor_type )lt_dlsym(module,(string("_")+module_name+"_LTX_new_instance").c_str());
}
if(constructor)
{
// if(callback)callback->task(strprintf("Executing callback for \"%s\"",module_name.c_str()));
mod=handle<Module>((*constructor)(callback));
}
else
{
if(callback)callback->error(strprintf(_("Unable to find entrypoint in module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
return false;
}
// if(callback)callback->task(strprintf("Done executing callback for \"%s\"",module_name.c_str()));
if(mod)
{
// if(callback)callback->task(strprintf("Registering \"%s\"",module_name.c_str()));
Register(mod);
}
else
{
if(callback)callback->error(_("Entrypoint did not return a module."));
return false;
}
if(callback)callback->task(strprintf(_("Success for \"%s\""),module_name.c_str()));
#endif
return true;
}
//! Virtual Modules properties wrappers. Must be defined in the modules classes
const char * Module::Name() { return " "; }
const char * Module::Desc() { return " "; }
const char * Module::Author() { return " "; }
const char * Module::Version() { return " "; }
const char * Module::Copyright() { return SYNFIG_COPYRIGHT; }