1 /* === S Y N F I G ========================================================= */
2 /*! \file synfig/main.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
41 #include <ETL/stringf>
42 #include "listimporter.h"
47 #include "valuenode.h"
50 #include "loadcanvas.h"
64 using namespace synfig;
66 /* === M A C R O S ========================================================= */
68 #define MODULE_LIST_FILENAME "synfig_modules.cfg"
70 /* === S T A T I C S ======================================================= */
72 static etl::reference_counter synfig_ref_count_(0);
74 /* === P R O C E D U R E S ================================================= */
76 /* === M E T H O D S ======================================================= */
89 synfig::get_build_date()
95 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
99 if(version!=SYNFIG_LIBRARY_VERSION)
101 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
104 if(vec_size!=sizeof(Vector))
106 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
109 if(color_size!=sizeof(Color))
111 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
114 if(canvas_size!=sizeof(Canvas))
116 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
119 if(layer_size!=sizeof(Layer))
121 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
128 static void broken_pipe_signal (int /*sig*/) {
129 synfig::warning("Broken Pipe...");
132 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
134 std::ifstream file(filename.c_str());
138 // warning("Cannot open "+filename);
145 getline(file,modulename);
146 if(!modulename.empty() && find(modules_to_load.begin(),modules_to_load.end(),modulename)==modules_to_load.end())
147 modules_to_load.push_back(modulename);
153 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
154 ref_count_(synfig_ref_count_)
156 if(ref_count_.count())
159 synfig_ref_count_.reset();
160 ref_count_=synfig_ref_count_;
162 // Add initialization after this point
165 bindtextdomain("synfig", LOCALEDIR);
166 bind_textdomain_codeset("synfig", "UTF-8");
169 String prefix=basepath+"/..";
172 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
175 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
176 signal(SIGPIPE, broken_pipe_signal);
179 //_config_search_path=new vector"string.h"();
181 // Init the subsystems
182 if(cb)cb->amount_complete(0, 100);
183 if(cb)cb->task(_("Starting Subsystem \"Modules\""));
184 if(!Module::subsys_init(prefix))
185 throw std::runtime_error(_("Unable to initialize subsystem \"Module\""));
187 if(cb)cb->task(_("Starting Subsystem \"Layers\""));
188 if(!Layer::subsys_init())
190 Module::subsys_stop();
191 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
194 if(cb)cb->task(_("Starting Subsystem \"Targets\""));
195 if(!Target::subsys_init())
197 Layer::subsys_stop();
198 Module::subsys_stop();
199 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
202 if(cb)cb->task(_("Starting Subsystem \"Importers\""));
203 if(!Importer::subsys_init())
205 Target::subsys_stop();
206 Layer::subsys_stop();
207 Module::subsys_stop();
208 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
211 if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
212 if(!ValueNode::subsys_init())
214 Importer::subsys_stop();
215 Target::subsys_stop();
216 Layer::subsys_stop();
217 Module::subsys_stop();
218 throw std::runtime_error(_("Unable to initialize subsystem \"ValueNodes\""));
221 // Load up the list importer
222 Importer::book()[String("lst")]=ListImporter::create;
224 // Load up the modules
225 std::list<String> modules_to_load;
226 std::vector<String> locations;
228 if(getenv("SYNFIG_MODULE_LIST"))
229 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
232 locations.push_back("./"MODULE_LIST_FILENAME);
233 locations.push_back("../etc/"MODULE_LIST_FILENAME);
235 locations.push_back(strprintf("%s/.synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
237 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
239 locations.push_back(prefix+"/etc/"+MODULE_LIST_FILENAME);
240 locations.push_back("/usr/local/etc/"MODULE_LIST_FILENAME);
242 locations.push_back("/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME);
243 locations.push_back("/Library/Synfig/"MODULE_LIST_FILENAME);
245 locations.push_back(strprintf("%s/Library/Synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
248 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
252 for(i=0;i<locations.size();i++)
253 if(retrieve_modules_to_load(locations[i],modules_to_load))
255 synfig::info(_("Loading modules from %s"), locations[i].c_str());
256 if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i].c_str()));
260 if (i == locations.size())
262 Importer::subsys_stop();
263 Target::subsys_stop();
264 Layer::subsys_stop();
265 Module::subsys_stop();
266 throw std::runtime_error(strprintf(_("Unable to open module list file '%s'"), MODULE_LIST_FILENAME));
269 std::list<String>::iterator iter;
271 Module::register_default_modules(cb);
273 for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
275 Module::Register(*iter,cb);
276 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
279 if(cb)cb->amount_complete(100, 100);
280 if(cb)cb->task(_("DONE"));
283 synfig::Main::~Main()
286 if(!synfig_ref_count_.unique())
288 synfig_ref_count_.detach();
290 // Add deinitialization after this point
292 if(get_open_canvas_map().size())
294 synfig::warning("Canvases still open!");
295 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
296 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
298 synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
302 // synfig::info("ValueNode::subsys_stop()");
303 ValueNode::subsys_stop();
304 // synfig::info("Importer::subsys_stop()");
305 Importer::subsys_stop();
306 // synfig::info("Target::subsys_stop()");
307 Target::subsys_stop();
308 // synfig::info("Layer::subsys_stop()");
309 Layer::subsys_stop();
310 /*! \todo For some reason, uncommenting the next line will cause things to crash.
311 This needs to be looked into at some point. */
312 // synfig::info("Module::subsys_stop()");
313 // Module::subsys_stop();
314 // synfig::info("Exiting");
316 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
317 signal(SIGPIPE, SIG_DFL);
324 const int buflen = 50;
330 strftime(b, buflen, " [%X] ", lt);
335 synfig::error(const char *format,...)
338 va_start(args,format);
339 error(vstrprintf(format,args));
343 synfig::error(const String &str)
345 static Mutex mutex; Mutex::Lock lock(mutex);
346 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
350 synfig::warning(const char *format,...)
353 va_start(args,format);
354 warning(vstrprintf(format,args));
358 synfig::warning(const String &str)
360 static Mutex mutex; Mutex::Lock lock(mutex);
361 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
365 synfig::info(const char *format,...)
368 va_start(args,format);
369 info(vstrprintf(format,args));
373 synfig::info(const String &str)
375 static Mutex mutex; Mutex::Lock lock(mutex);
376 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;