Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / synfig / main.cpp
blob61a263b1c574f0e50140f3d1bee01491ca0ed5d3
1 /* === S Y N F I G ========================================================= */
2 /*! \file synfig/main.cpp
3 ** \brief \writeme
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 //#define SYNFIG_NO_ANGLE
28 #ifdef USING_PCH
29 # include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
35 #include <iostream>
36 #include "version.h"
37 #include "general.h"
38 #include "module.h"
39 #include <cstdlib>
40 #include <ltdl.h>
41 #include <stdexcept>
42 #include "target.h"
43 #include <ETL/stringf>
44 #include "listimporter.h"
45 #include "color.h"
46 #include "vector.h"
47 #include <fstream>
48 #include "layer.h"
49 #include "valuenode.h"
51 #include "main.h"
52 #include "loadcanvas.h"
54 #include "guid.h"
56 #include "mutex.h"
58 #ifdef HAVE_SIGNAL_H
59 #include <signal.h>
60 #endif
62 #endif
64 using namespace std;
65 using namespace etl;
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 ======================================================= */
80 const char *
81 synfig::get_version()
83 #ifdef VERSION
84 return VERSION;
85 #else
86 return "Unknown";
87 #endif
90 const char *
91 synfig::get_build_date()
93 return __DATE__;
96 bool
97 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
99 bool ret=true;
101 if(version!=SYNFIG_LIBRARY_VERSION)
103 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
104 ret=false;
106 if(vec_size!=sizeof(Vector))
108 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
109 ret=false;
111 if(color_size!=sizeof(Color))
113 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
114 ret=false;
116 if(canvas_size!=sizeof(Canvas))
118 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
119 ret=false;
121 if(layer_size!=sizeof(Layer))
123 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
124 ret=false;
127 return ret;
130 static void broken_pipe_signal (int /*sig*/) {
131 synfig::warning("Broken Pipe...");
134 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
136 std::ifstream file(filename.c_str());
138 if(!file)
140 // warning("Cannot open "+filename);
141 return false;
144 while(file)
146 String modulename;
147 getline(file,modulename);
148 if(!modulename.empty() && find(modules_to_load.begin(),modules_to_load.end(),modulename)==modules_to_load.end())
149 modules_to_load.push_back(modulename);
152 return true;
155 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
156 ref_count_(synfig_ref_count_)
158 if(ref_count_.count())
159 return;
161 synfig_ref_count_.reset();
162 ref_count_=synfig_ref_count_;
164 // Add initialization after this point
166 #ifdef ENABLE_NLS
167 bindtextdomain("synfig", LOCALEDIR);
168 #endif
170 String prefix=basepath+"/..";
171 unsigned int i;
172 #ifdef _DEBUG
173 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
174 #endif
176 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
177 signal(SIGPIPE, broken_pipe_signal);
178 #endif
180 //_config_search_path=new vector"string.h"();
182 // Init the subsystems
183 if(cb)cb->amount_complete(0, 100);
184 if(cb)cb->task(_("Starting Subsystem \"Modules\""));
185 if(!Module::subsys_init(prefix))
186 throw std::runtime_error(_("Unable to initialize subsystem \"Module\""));
188 if(cb)cb->task(_("Starting Subsystem \"Layers\""));
189 if(!Layer::subsys_init())
191 Module::subsys_stop();
192 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
195 if(cb)cb->task(_("Starting Subsystem \"Targets\""));
196 if(!Target::subsys_init())
198 Layer::subsys_stop();
199 Module::subsys_stop();
200 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
203 if(cb)cb->task(_("Starting Subsystem \"Importers\""));
204 if(!Importer::subsys_init())
206 Target::subsys_stop();
207 Layer::subsys_stop();
208 Module::subsys_stop();
209 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
212 if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
213 if(!ValueNode::subsys_init())
215 Importer::subsys_stop();
216 Target::subsys_stop();
217 Layer::subsys_stop();
218 Module::subsys_stop();
219 throw std::runtime_error(_("Unable to initialize subsystem \"ValueNodes\""));
222 // Load up the list importer
223 Importer::book()[String("lst")]=ListImporter::create;
225 // Load up the modules
226 std::list<String> modules_to_load;
227 std::vector<String> locations;
229 if(getenv("SYNFIG_MODULE_LIST"))
230 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
231 else
233 locations.push_back("./"MODULE_LIST_FILENAME);
234 locations.push_back("../etc/"MODULE_LIST_FILENAME);
235 if(getenv("HOME"))
236 locations.push_back(strprintf("%s/.synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
237 #ifdef SYSCONFDIR
238 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
239 #endif
240 locations.push_back(prefix+"/etc/"+MODULE_LIST_FILENAME);
241 locations.push_back("/usr/local/etc/"MODULE_LIST_FILENAME);
242 #ifdef __APPLE__
243 locations.push_back("/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME);
244 locations.push_back("/Library/Synfig/"MODULE_LIST_FILENAME);
245 if(getenv("HOME"))
246 locations.push_back(strprintf("%s/Library/Synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
247 #endif
248 #ifdef WIN32
249 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
250 #endif
253 for(i=0;i<locations.size();i++)
254 if(retrieve_modules_to_load(locations[i],modules_to_load))
256 synfig::info(_("Loading modules from %s"), locations[i].c_str());
257 if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i].c_str()));
258 break;
261 if (i == locations.size())
263 Importer::subsys_stop();
264 Target::subsys_stop();
265 Layer::subsys_stop();
266 Module::subsys_stop();
267 throw std::runtime_error(strprintf(_("Unable to open module list file '%s'"), MODULE_LIST_FILENAME));
270 std::list<String>::iterator iter;
272 Module::register_default_modules(cb);
274 for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
276 Module::Register(*iter,cb);
277 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
280 if(cb)cb->amount_complete(100, 100);
281 if(cb)cb->task(_("DONE"));
284 synfig::Main::~Main()
286 ref_count_.detach();
287 if(!synfig_ref_count_.unique())
288 return;
289 synfig_ref_count_.detach();
291 // Add deinitialization after this point
293 if(get_open_canvas_map().size())
295 synfig::warning("Canvases still open!");
296 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
297 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
299 synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
303 // synfig::info("ValueNode::subsys_stop()");
304 ValueNode::subsys_stop();
305 // synfig::info("Importer::subsys_stop()");
306 Importer::subsys_stop();
307 // synfig::info("Target::subsys_stop()");
308 Target::subsys_stop();
309 // synfig::info("Layer::subsys_stop()");
310 Layer::subsys_stop();
311 /*! \todo For some reason, uncommenting the next line will cause things to crash.
312 This needs to be looked into at some point. */
313 // synfig::info("Module::subsys_stop()");
314 // Module::subsys_stop();
315 // synfig::info("Exiting");
317 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
318 signal(SIGPIPE, SIG_DFL);
319 #endif
322 static const String
323 current_time()
325 const int buflen = 50;
326 time_t t;
327 struct tm *lt;
328 char b[buflen];
329 time(&t);
330 lt = localtime(&t);
331 strftime(b, buflen, " [%X] ", lt);
332 return String(b);
335 void
336 synfig::error(const char *format,...)
338 va_list args;
339 va_start(args,format);
340 error(vstrprintf(format,args));
343 void
344 synfig::error(const String &str)
346 static Mutex mutex; Mutex::Lock lock(mutex);
347 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
350 void
351 synfig::warning(const char *format,...)
353 va_list args;
354 va_start(args,format);
355 warning(vstrprintf(format,args));
358 void
359 synfig::warning(const String &str)
361 static Mutex mutex; Mutex::Lock lock(mutex);
362 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
365 void
366 synfig::info(const char *format,...)
368 va_list args;
369 va_start(args,format);
370 info(vstrprintf(format,args));
373 void
374 synfig::info(const String &str)
376 static Mutex mutex; Mutex::Lock lock(mutex);
377 cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;