more updates
[synfig.git] / synfig-core / trunk / src / synfig / module.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file module.cpp
3 **      \brief writeme
4 **
5 **      $Id: module.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SINFG_NO_ANGLE
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "module.h"
34 #include "general.h"
35 #include <ltdl.h>
36 #include <ETL/stringf>
37 #endif
38
39 /* === M A C R O S ========================================================= */
40
41 /* === G L O B A L S ======================================================= */
42
43 using namespace std;
44 using namespace etl;
45 using namespace sinfg;
46
47 Module::Book *sinfg::Module::book_;
48
49 /* === P R O C E D U R E S ================================================= */
50
51 bool
52 Module::subsys_init(const String &prefix)
53 {
54         #ifndef SINFG_LTDL_NO_STATIC
55         //LTDL_SET_PRELOADED_SYMBOLS();
56         #endif
57         
58         if(lt_dlinit())
59         {
60                 error(_("Errors on lt_dlinit()"));
61                 error(lt_dlerror());
62                 return false;
63         }
64
65         lt_dladdsearchdir(".");
66         lt_dladdsearchdir("~/.sinfg/modules");
67         lt_dladdsearchdir((prefix+"/lib/sinfg/modules").c_str());
68 #ifdef LIBDIR
69         lt_dladdsearchdir(LIBDIR"/sinfg/modules");
70 #endif
71 #ifdef __APPLE__
72         lt_dladdsearchdir("/Library/Frameworks/sinfg.framework/Resources/modules");
73 #endif
74         lt_dladdsearchdir("/usr/local/lib/sinfg/modules");
75         lt_dladdsearchdir(".");
76         
77         book_=new Book;
78         return true;
79 }
80
81 bool
82 Module::subsys_stop()
83 {
84         delete book_;
85         
86         lt_dlexit();
87         return true;
88 }
89
90 bool
91 register_default_modules()
92 {
93         return true;
94 }
95
96 Module::Book&
97 Module::book()
98 {
99         return *book_;
100 }
101
102 void
103 sinfg::Module::Register(Module::Handle mod)
104 {
105         book()[mod->Name()]=mod;
106 }
107
108 bool
109 sinfg::Module::Register(const String &module_name, ProgressCallback *callback)
110 {
111         lt_dlhandle module;
112
113         if(callback)callback->task(strprintf(_("Attempting to register \"%s\""),module_name.c_str()));
114
115         module=lt_dlopenext((string("lib")+module_name).c_str());
116         if(!module)module=lt_dlopenext(module_name.c_str());
117         
118         if(!module)
119         {
120                 if(callback)callback->error(strprintf(_("Unable to find module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
121                 return false;
122         }
123
124         if(callback)callback->task(strprintf(_("Found module \"%s\""),module_name.c_str()));
125         
126         Module::constructor_type constructor=NULL;
127         Handle mod;
128
129         if(!constructor)
130         {
131 //              if(callback)callback->task(string("looking for -> ")+module_name+"_LTX_new_instance()");
132                 constructor=(Module::constructor_type )lt_dlsym(module,(module_name+"_LTX_new_instance").c_str());
133         }
134
135         if(!constructor)
136         {
137 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
138                 constructor=(Module::constructor_type )lt_dlsym(module,(string("lib")+module_name+"_LTX_new_instance").c_str());
139         }
140         if(!constructor)
141         {
142 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
143                 constructor=(Module::constructor_type )lt_dlsym(module,(string("_lib")+module_name+"_LTX_new_instance").c_str());
144         }
145         if(!constructor)
146         {
147 //              if(callback)callback->task(string("looking for -> lib")+module_name+"_LTX_new_instance()");
148                 constructor=(Module::constructor_type )lt_dlsym(module,(string("_")+module_name+"_LTX_new_instance").c_str());
149         }
150         
151         if(constructor)
152         {
153 //              if(callback)callback->task(strprintf("Executing callback for \"%s\"",module_name.c_str()));
154                 mod=handle<Module>((*constructor)(callback));
155         }
156         else
157         {
158                 if(callback)callback->error(strprintf(_("Unable to find entrypoint in module \"%s\" (%s)"),module_name.c_str(),lt_dlerror()));
159                 return false;
160         }
161
162 //      if(callback)callback->task(strprintf("Done executing callback for \"%s\"",module_name.c_str()));
163
164         if(mod)
165         {
166 //              if(callback)callback->task(strprintf("Registering \"%s\"",module_name.c_str()));
167                 Register(mod);
168         }
169         else
170         {
171                 if(callback)callback->error(_("Entrypoint did not return a module."));
172                 return false;
173     }
174
175         if(callback)callback->task(strprintf(_("Success for \"%s\""),module_name.c_str()));
176         
177         return false;
178 }