Remove ancient trunk folder from svn repository
[synfig.git] / synfig-core / src / synfig / main.cpp
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
10 **
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.
15 **
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
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 //#define SYNFIG_NO_ANGLE
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
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"
50
51 #include "main.h"
52 #include "loadcanvas.h"
53
54 #include "guid.h"
55
56 #include "mutex.h"
57
58 #ifdef HAVE_SIGNAL_H
59 #include <signal.h>
60 #endif
61
62 #endif
63
64 using namespace std;
65 using namespace etl;
66 using namespace synfig;
67
68 /* === M A C R O S ========================================================= */
69
70 #define MODULE_LIST_FILENAME    "synfig_modules.cfg"
71
72 /* === S T A T I C S ======================================================= */
73
74 static etl::reference_counter synfig_ref_count_(0);
75
76 /* === P R O C E D U R E S ================================================= */
77
78 /* === M E T H O D S ======================================================= */
79
80 const char *
81 synfig::get_version()
82 {
83 #ifdef VERSION
84         return VERSION;
85 #else
86         return "Unknown";
87 #endif
88 }
89
90 const char *
91 synfig::get_build_date()
92 {
93         return __DATE__;
94 }
95
96 bool
97 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
98 {
99         bool ret=true;
100
101         if(version!=SYNFIG_LIBRARY_VERSION)
102         {
103                 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
104                 ret=false;
105         }
106         if(vec_size!=sizeof(Vector))
107         {
108                 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
109                 ret=false;
110         }
111         if(color_size!=sizeof(Color))
112         {
113                 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
114                 ret=false;
115         }
116         if(canvas_size!=sizeof(Canvas))
117         {
118                 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
119                 ret=false;
120         }
121         if(layer_size!=sizeof(Layer))
122         {
123                 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
124                 ret=false;
125         }
126
127         return ret;
128 }
129
130 static void broken_pipe_signal (int /*sig*/)  {
131         synfig::warning("Broken Pipe...");
132 }
133
134 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
135 {
136         std::ifstream file(filename.c_str());
137
138         if(!file)
139         {
140                 // warning("Cannot open "+filename);
141                 return false;
142         }
143
144         while(file)
145         {
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);
150         }
151
152         return true;
153 }
154
155 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
156         ref_count_(synfig_ref_count_)
157 {
158         if(ref_count_.count())
159                 return;
160
161         synfig_ref_count_.reset();
162         ref_count_=synfig_ref_count_;
163
164         // Add initialization after this point
165
166 #ifdef ENABLE_NLS
167         bindtextdomain("synfig", LOCALEDIR);
168         bind_textdomain_codeset("synfig", "UTF-8");
169 #endif
170
171         String prefix=basepath+"/..";
172         unsigned int i;
173 #ifdef _DEBUG
174         std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
175 #endif
176
177 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
178         signal(SIGPIPE, broken_pipe_signal);
179 #endif
180
181         //_config_search_path=new vector"string.h"();
182
183         // Init the subsystems
184         if(cb)cb->amount_complete(0, 100);
185         if(cb)cb->task(_("Starting Subsystem \"Modules\""));
186         if(!Module::subsys_init(prefix))
187                 throw std::runtime_error(_("Unable to initialize subsystem \"Module\""));
188
189         if(cb)cb->task(_("Starting Subsystem \"Layers\""));
190         if(!Layer::subsys_init())
191         {
192                 Module::subsys_stop();
193                 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
194         }
195
196         if(cb)cb->task(_("Starting Subsystem \"Targets\""));
197         if(!Target::subsys_init())
198         {
199                 Layer::subsys_stop();
200                 Module::subsys_stop();
201                 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
202         }
203
204         if(cb)cb->task(_("Starting Subsystem \"Importers\""));
205         if(!Importer::subsys_init())
206         {
207                 Target::subsys_stop();
208                 Layer::subsys_stop();
209                 Module::subsys_stop();
210                 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
211         }
212
213         if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
214         if(!ValueNode::subsys_init())
215         {
216                 Importer::subsys_stop();
217                 Target::subsys_stop();
218                 Layer::subsys_stop();
219                 Module::subsys_stop();
220                 throw std::runtime_error(_("Unable to initialize subsystem \"ValueNodes\""));
221         }
222
223         // Load up the list importer
224         Importer::book()[String("lst")]=ListImporter::create;
225
226         // Load up the modules
227         std::list<String> modules_to_load;
228         std::vector<String> locations;
229
230         if(getenv("SYNFIG_MODULE_LIST"))
231                 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
232         else
233         {
234                 locations.push_back("./"MODULE_LIST_FILENAME);
235                 locations.push_back("../etc/"MODULE_LIST_FILENAME);
236                 if(getenv("HOME"))
237                         locations.push_back(strprintf("%s/.synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
238         #ifdef SYSCONFDIR
239                 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
240         #endif
241                 locations.push_back(prefix+"/etc/"+MODULE_LIST_FILENAME);
242                 locations.push_back("/usr/local/etc/"MODULE_LIST_FILENAME);
243         #ifdef __APPLE__
244                 locations.push_back("/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME);
245                 locations.push_back("/Library/Synfig/"MODULE_LIST_FILENAME);
246                 if(getenv("HOME"))
247                         locations.push_back(strprintf("%s/Library/Synfig/%s", getenv("HOME"), MODULE_LIST_FILENAME));
248         #endif
249         #ifdef WIN32
250                 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
251         #endif
252         }
253
254         for(i=0;i<locations.size();i++)
255                 if(retrieve_modules_to_load(locations[i],modules_to_load))
256                 {
257                         synfig::info(_("Loading modules from %s"), locations[i].c_str());
258                         if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i].c_str()));
259                         break;
260                 }
261
262         if (i == locations.size())
263         {
264                 Importer::subsys_stop();
265                 Target::subsys_stop();
266                 Layer::subsys_stop();
267                 Module::subsys_stop();
268                 throw std::runtime_error(strprintf(_("Unable to open module list file '%s'"), MODULE_LIST_FILENAME));
269         }
270
271         std::list<String>::iterator iter;
272
273         Module::register_default_modules(cb);
274
275         for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
276         {
277                 Module::Register(*iter,cb);
278                 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
279         }
280
281         if(cb)cb->amount_complete(100, 100);
282         if(cb)cb->task(_("DONE"));
283 }
284
285 synfig::Main::~Main()
286 {
287         ref_count_.detach();
288         if(!synfig_ref_count_.unique())
289                 return;
290         synfig_ref_count_.detach();
291
292         // Add deinitialization after this point
293
294         if(get_open_canvas_map().size())
295         {
296                 synfig::warning("Canvases still open!");
297                 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
298                 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
299                 {
300                         synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
301                 }
302         }
303
304         // synfig::info("ValueNode::subsys_stop()");
305         ValueNode::subsys_stop();
306         // synfig::info("Importer::subsys_stop()");
307         Importer::subsys_stop();
308         // synfig::info("Target::subsys_stop()");
309         Target::subsys_stop();
310         // synfig::info("Layer::subsys_stop()");
311         Layer::subsys_stop();
312         /*! \todo For some reason, uncommenting the next line will cause things to crash.
313                           This needs to be looked into at some point. */
314         // synfig::info("Module::subsys_stop()");
315         // Module::subsys_stop();
316         // synfig::info("Exiting");
317
318 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
319         signal(SIGPIPE, SIG_DFL);
320 #endif
321 }
322
323 static const String
324 current_time()
325 {
326         const int buflen = 50;
327         time_t t;
328         struct tm *lt;
329         char b[buflen];
330         time(&t);
331         lt = localtime(&t);
332         strftime(b, buflen, " [%X] ", lt);
333         return String(b);
334 }
335
336 void
337 synfig::error(const char *format,...)
338 {
339         va_list args;
340         va_start(args,format);
341         error(vstrprintf(format,args));
342 }
343
344 void
345 synfig::error(const String &str)
346 {
347         static Mutex mutex; Mutex::Lock lock(mutex);
348         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
349 }
350
351 void
352 synfig::warning(const char *format,...)
353 {
354         va_list args;
355         va_start(args,format);
356         warning(vstrprintf(format,args));
357 }
358
359 void
360 synfig::warning(const String &str)
361 {
362         static Mutex mutex; Mutex::Lock lock(mutex);
363         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
364 }
365
366 void
367 synfig::info(const char *format,...)
368 {
369         va_list args;
370         va_start(args,format);
371         info(vstrprintf(format,args));
372 }
373
374 void
375 synfig::info(const String &str)
376 {
377         static Mutex mutex; Mutex::Lock lock(mutex);
378         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;
379 }