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 ======================================================= */
26 //#define SYNFIG_NO_ANGLE
43 #include <ETL/stringf>
44 #include "listimporter.h"
49 #include "valuenode.h"
52 #include "loadcanvas.h"
66 using namespace synfig;
68 /* === M A C R O S ========================================================= */
70 #define MODULE_LIST_FILENAME "synfig_modules.cfg"
72 /* === S T A T I C S ======================================================= */
74 static etl::reference_counter synfig_ref_count_(0);
76 /* === P R O C E D U R E S ================================================= */
78 /* === M E T H O D S ======================================================= */
91 synfig::get_build_date()
97 synfig::get_build_time()
102 extern const char *get_build_time();
105 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
109 if(version!=SYNFIG_LIBRARY_VERSION)
111 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
114 if(vec_size!=sizeof(Vector))
116 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
119 if(color_size!=sizeof(Color))
121 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
124 if(canvas_size!=sizeof(Canvas))
126 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
129 if(layer_size!=sizeof(Layer))
131 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
138 static void broken_pipe_signal (int /*sig*/) {
139 synfig::warning("Broken Pipe...");
142 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
144 if(filename=="standard")
148 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_bmp")==modules_to_load.end())
149 modules_to_load.push_back("trgt_bmp");
150 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_gif")==modules_to_load.end())
151 modules_to_load.push_back("trgt_gif");
152 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_dv")==modules_to_load.end())
153 modules_to_load.push_back("trgt_dv");
154 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_ffmpeg")==modules_to_load.end())
155 modules_to_load.push_back("mod_ffmpeg");
156 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_imagemagick")==modules_to_load.end())
157 modules_to_load.push_back("mod_imagemagick");
158 if(find(modules_to_load.begin(),modules_to_load.end(),"lyr_std")==modules_to_load.end())
159 modules_to_load.push_back("lyr_std");
160 if(find(modules_to_load.begin(),modules_to_load.end(),"lyr_freetype")==modules_to_load.end())
161 modules_to_load.push_back("lyr_freetype");
163 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_png")==modules_to_load.end())
164 modules_to_load.push_back("trgt_png");
167 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_openexr")==modules_to_load.end())
168 modules_to_load.push_back("mod_openexr");
174 std::ifstream file(filename.c_str());
177 // warning("Cannot open "+filename);
183 getline(file,modulename);
184 if(!modulename.empty() && find(modules_to_load.begin(),modules_to_load.end(),modulename)==modules_to_load.end())
185 modules_to_load.push_back(modulename);
192 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
193 ref_count_(synfig_ref_count_)
195 if(ref_count_.count())
198 synfig_ref_count_.reset();
199 ref_count_=synfig_ref_count_;
201 // Add initialization after this point
204 bindtextdomain("synfig", LOCALEDIR);
207 String prefix=basepath+"/..";
210 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
213 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
214 signal(SIGPIPE, broken_pipe_signal);
217 //_config_search_path=new vector"string.h"();
219 // Init the subsystems
220 if(cb)cb->amount_complete(0, 100);
221 if(cb)cb->task(_("Starting Subsystem \"Modules\""));
222 if(!Module::subsys_init(prefix))
223 throw std::runtime_error(_("Unable to initialize subsystem \"Module\""));
225 if(cb)cb->task(_("Starting Subsystem \"Layers\""));
226 if(!Layer::subsys_init())
228 Module::subsys_stop();
229 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
232 if(cb)cb->task(_("Starting Subsystem \"Targets\""));
233 if(!Target::subsys_init())
235 Layer::subsys_stop();
236 Module::subsys_stop();
237 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
240 if(cb)cb->task(_("Starting Subsystem \"Importers\""));
241 if(!Importer::subsys_init())
243 Target::subsys_stop();
244 Layer::subsys_stop();
245 Module::subsys_stop();
246 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
249 if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
250 if(!ValueNode::subsys_init())
252 Importer::subsys_stop();
253 Target::subsys_stop();
254 Layer::subsys_stop();
255 Module::subsys_stop();
256 throw std::runtime_error(_("Unable to initialize subsystem \"ValueNodes\""));
259 // Load up the list importer
260 Importer::book()[String("lst")]=ListImporter::create;
262 // Load up the modules
263 std::list<String> modules_to_load;
264 std::vector<String> locations;
266 if(!getenv("SYNFIG_MODULE_LIST"))
268 locations.push_back("standard");
269 locations.push_back("./"MODULE_LIST_FILENAME); //1
270 locations.push_back("../etc/"MODULE_LIST_FILENAME); //1
271 locations.push_back("~/.synfig/"MODULE_LIST_FILENAME); //2
272 locations.push_back(prefix+"/etc/"+MODULE_LIST_FILENAME); //3
273 locations.push_back("/usr/local/etc/"MODULE_LIST_FILENAME);
275 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
278 locations.push_back("/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME);
279 locations.push_back("/Library/Synfig/"MODULE_LIST_FILENAME);
280 locations.push_back("~/Library/Synfig/"MODULE_LIST_FILENAME);
283 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
288 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
291 const char *locations[]=
294 "./"MODULE_LIST_FILENAME, //1
295 "../etc/"MODULE_LIST_FILENAME, //1
296 "~/.synfig/"MODULE_LIST_FILENAME, //2
297 "/usr/local/lib/synfig/modules/"MODULE_LIST_FILENAME, //3
298 "/usr/local/etc/"MODULE_LIST_FILENAME,
300 SYSCONFDIR"/"MODULE_LIST_FILENAME,
303 "/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME,
304 "/Library/SYNFIG/"MODULE_LIST_FILENAME,
305 "~/Library/SYNFIG/"MODULE_LIST_FILENAME,
308 "C:\\Program Files\\SYNFIG\\etc\\"MODULE_LIST_FILENAME,
313 for(i=0;i<locations.size();i++)
314 if(retrieve_modules_to_load(locations[i],modules_to_load))
315 if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i].c_str()));
317 std::list<String>::iterator iter;
319 for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
321 Module::Register(*iter,cb);
322 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
327 if(cb)cb->amount_complete(100, 100);
328 if(cb)cb->task(_("DONE"));
331 synfig::Main::~Main()
334 if(!synfig_ref_count_.unique())
336 synfig_ref_count_.detach();
338 // Add deinitialization after this point
340 if(get_open_canvas_map().size())
342 synfig::warning("Canvases still open!");
343 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
344 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
346 synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
350 // synfig::info("ValueNode::subsys_stop()");
351 ValueNode::subsys_stop();
352 // synfig::info("Importer::subsys_stop()");
353 Importer::subsys_stop();
354 // synfig::info("Target::subsys_stop()");
355 Target::subsys_stop();
356 // synfig::info("Layer::subsys_stop()");
357 Layer::subsys_stop();
358 /*! \todo For some reason, uncommenting the next line will cause things to crash.
359 This needs to be looked into at some point. */
360 // synfig::info("Module::subsys_stop()");
361 // Module::subsys_stop();
362 // synfig::info("Exiting");
364 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
365 signal(SIGPIPE, SIG_DFL);
372 const int buflen = 50;
378 strftime(b, buflen, " [%X] ", lt);
383 synfig::error(const char *format,...)
386 va_start(args,format);
387 error(vstrprintf(format,args));
391 synfig::error(const String &str)
393 static Mutex mutex; Mutex::Lock lock(mutex);
394 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
398 synfig::warning(const char *format,...)
401 va_start(args,format);
402 warning(vstrprintf(format,args));
406 synfig::warning(const String &str)
408 static Mutex mutex; Mutex::Lock lock(mutex);
409 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
413 synfig::info(const char *format,...)
416 va_start(args,format);
417 info(vstrprintf(format,args));
421 synfig::info(const String &str)
423 static Mutex mutex; Mutex::Lock lock(mutex);
424 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;