Remove ancient trunk folder from svn repository
[synfig.git] / synfig-core / src / synfig / module.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file synfig/module.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #define SYNFIG_NO_ANGLE
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "module.h"
35 #include "general.h"
36 #include <ETL/stringf>
37
38 #ifndef USE_CF_BUNDLES
39 #include <ltdl.h>
40 #endif
41
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51
52 Module::Book *synfig::Module::book_;
53
54 /* === P R O C E D U R E S ================================================= */
55
56 bool
57 Module::subsys_init(const String &prefix)
58 {
59 #ifndef USE_CF_BUNDLES
60         #ifndef SYNFIG_LTDL_NO_STATIC
61         //LTDL_SET_PRELOADED_SYMBOLS();
62         #endif
63
64         if(lt_dlinit())
65         {
66                 error(_("Errors on lt_dlinit()"));
67                 error(lt_dlerror());
68                 return false;
69         }
70
71         lt_dladdsearchdir(".");
72         if(getenv("HOME"))
73                 lt_dladdsearchdir(strprintf("%s/.synfig/modules", getenv("HOME")).c_str());
74         lt_dladdsearchdir((prefix+"/lib/synfig/modules").c_str());
75 #ifdef LIBDIR
76         lt_dladdsearchdir(LIBDIR"/synfig/modules");
77 #endif
78 #ifdef __APPLE__
79         lt_dladdsearchdir("/Library/Frameworks/synfig.framework/Resources/modules");
80 #endif
81         lt_dladdsearchdir("/usr/local/lib/synfig/modules");
82         lt_dladdsearchdir(".");
83 #endif
84         book_=new Book;
85         return true;
86 }
87
88 bool
89 Module::subsys_stop()
90 {
91         delete book_;
92
93 #ifndef USE_CF_BUNDLES
94         lt_dlexit();
95 #endif
96         return true;
97 }
98
99 void
100 Module::register_default_modules(ProgressCallback *callback)
101 {
102         #define REGISTER_MODULE(module) if (!Register(module, callback)) \
103                                                                                 throw std::runtime_error(strprintf(_("Unable to load module '%s'"), module))
104         REGISTER_MODULE("lyr_freetype");
105         REGISTER_MODULE("mod_geometry");
106         REGISTER_MODULE("mod_gradient");
107         REGISTER_MODULE("mod_particle");
108 }
109
110 Module::Book&
111 Module::book()
112 {
113         return *book_;
114 }
115
116 void
117 synfig::Module::Register(Module::Handle mod)
118 {
119         book()[mod->Name()]=mod;
120 }
121
122 bool
123 synfig::Module::Register(const String &module_name, ProgressCallback *callback)
124 {
125 #ifndef USE_CF_BUNDLES
126         lt_dlhandle module;
127
128         if(callback)callback->task(strprintf(_("Attempting to register \"%s\""),module_name.c_str()));
129
130         module=lt_dlopenext((string("lib")+module_name).c_str());
131         if(!module)module=lt_dlopenext(module_name.c_str());
132
133         if(!module)
134         {
135                 if(callback)callback->warning(strprintf(_("Unable to find module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
136                 return false;
137         }
138
139         if(callback)callback->task(strprintf(_("Found module \"%s\""),module_name.c_str()));
140
141         Module::constructor_type constructor=NULL;
142         Handle mod;
143
144         if(!constructor)
145         {
146 //              if(callback)callback->task(string("looking for -> ")+module_name+"_LTX_new_instance()");
147                 constructor=(Module::constructor_type )lt_dlsym(module,(module_name+"_LTX_new_instance").c_str());
148         }
149
150         if(!constructor)
151         {
152 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
153                 constructor=(Module::constructor_type )lt_dlsym(module,(string("lib")+module_name+"_LTX_new_instance").c_str());
154         }
155         if(!constructor)
156         {
157 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
158                 constructor=(Module::constructor_type )lt_dlsym(module,(string("_lib")+module_name+"_LTX_new_instance").c_str());
159         }
160         if(!constructor)
161         {
162 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
163                 constructor=(Module::constructor_type )lt_dlsym(module,(string("_")+module_name+"_LTX_new_instance").c_str());
164         }
165
166         if(constructor)
167         {
168 //              if(callback)callback->task(strprintf("Executing callback for \"%s\"",module_name.c_str()));
169                 mod=handle<Module>((*constructor)(callback));
170         }
171         else
172         {
173                 if(callback)callback->error(strprintf(_("Unable to find entrypoint in module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
174                 return false;
175         }
176
177 //      if(callback)callback->task(strprintf("Done executing callback for \"%s\"",module_name.c_str()));
178
179         if(mod)
180         {
181 //              if(callback)callback->task(strprintf("Registering \"%s\"",module_name.c_str()));
182                 Register(mod);
183         }
184         else
185         {
186                 if(callback)callback->error(_("Entrypoint did not return a module."));
187                 return false;
188     }
189
190         if(callback)callback->task(strprintf(_("Success for \"%s\""),module_name.c_str()));
191
192 #endif
193         return true;
194 }