Tidying.
[synfig.git] / synfig-core / trunk / 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 const char *
97 synfig::get_build_time()
98 {
99         return __TIME__;
100 }
101
102 extern const char *get_build_time();
103
104 bool
105 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
106 {
107         bool ret=true;
108
109         if(version!=SYNFIG_LIBRARY_VERSION)
110         {
111                 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
112                 ret=false;
113         }
114         if(vec_size!=sizeof(Vector))
115         {
116                 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
117                 ret=false;
118         }
119         if(color_size!=sizeof(Color))
120         {
121                 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
122                 ret=false;
123         }
124         if(canvas_size!=sizeof(Canvas))
125         {
126                 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
127                 ret=false;
128         }
129         if(layer_size!=sizeof(Layer))
130         {
131                 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
132                 ret=false;
133         }
134
135         return ret;
136 }
137
138 static void broken_pipe_signal (int /*sig*/)  {
139         synfig::warning("Broken Pipe...");
140 }
141
142 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
143 {
144         if(filename=="standard")
145         {
146                 return false;
147 /*
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");
162 #ifdef HAVE_LIBPNG
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");
165 #endif
166 #ifdef HAVE_OPENEXR
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");
169 #endif
170 */
171         }
172         else
173         {
174                 std::ifstream file(filename.c_str());
175                 if(!file)
176                 {
177                 //      warning("Cannot open "+filename);
178                         return false;
179                 }
180                 while(file)
181                 {
182                         String modulename;
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);
186                 }
187         }
188
189         return true;
190 }
191
192 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
193         ref_count_(synfig_ref_count_)
194 {
195         if(ref_count_.count())
196                 return;
197
198         synfig_ref_count_.reset();
199         ref_count_=synfig_ref_count_;
200
201         // Add initialization after this point
202
203 #ifdef ENABLE_NLS
204         bindtextdomain("synfig", LOCALEDIR);
205 #endif
206
207         String prefix=basepath+"/..";
208         unsigned int i;
209 #ifdef _DEBUG
210         std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
211 #endif
212
213 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
214         signal(SIGPIPE, broken_pipe_signal);
215 #endif
216
217         //_config_search_path=new vector"string.h"();
218
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\""));
224
225         if(cb)cb->task(_("Starting Subsystem \"Layers\""));
226         if(!Layer::subsys_init())
227         {
228                 Module::subsys_stop();
229                 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
230         }
231
232         if(cb)cb->task(_("Starting Subsystem \"Targets\""));
233         if(!Target::subsys_init())
234         {
235                 Layer::subsys_stop();
236                 Module::subsys_stop();
237                 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
238         }
239
240         if(cb)cb->task(_("Starting Subsystem \"Importers\""));
241         if(!Importer::subsys_init())
242         {
243                 Target::subsys_stop();
244                 Layer::subsys_stop();
245                 Module::subsys_stop();
246                 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
247         }
248
249         if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
250         if(!ValueNode::subsys_init())
251         {
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\""));
257         }
258
259         // Load up the list importer
260         Importer::book()[String("lst")]=ListImporter::create;
261
262         // Load up the modules
263         std::list<String> modules_to_load;
264         std::vector<String> locations;
265
266         if(!getenv("SYNFIG_MODULE_LIST"))
267         {
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);
274         #ifdef SYSCONFDIR
275                 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
276         #endif
277         #ifdef __APPLE__
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);
281         #endif
282         #ifdef WIN32
283                 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
284         #endif
285         }
286         else
287         {
288                 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
289         }
290 /*
291         const char *locations[]=
292         {
293                 "standard",     //0
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,
299 #ifdef SYSCONFDIR
300                 SYSCONFDIR"/"MODULE_LIST_FILENAME,
301 #endif
302 #ifdef __APPLE__
303                 "/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME,
304                 "/Library/SYNFIG/"MODULE_LIST_FILENAME,
305                 "~/Library/SYNFIG/"MODULE_LIST_FILENAME,
306 #endif
307 #ifdef WIN32
308                 "C:\\Program Files\\SYNFIG\\etc\\"MODULE_LIST_FILENAME,
309 #endif
310         };
311 */
312
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()));
316
317         std::list<String>::iterator iter;
318
319         for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
320         {
321                 Module::Register(*iter,cb);
322                 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
323         }
324
325 //      load_modules(cb);
326
327         if(cb)cb->amount_complete(100, 100);
328         if(cb)cb->task(_("DONE"));
329 }
330
331 synfig::Main::~Main()
332 {
333         ref_count_.detach();
334         if(!synfig_ref_count_.unique())
335                 return;
336         synfig_ref_count_.detach();
337
338         // Add deinitialization after this point
339
340         if(get_open_canvas_map().size())
341         {
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)
345                 {
346                         synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
347                 }
348         }
349
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");
363         
364 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
365         signal(SIGPIPE, SIG_DFL);
366 #endif
367 }
368
369 static const String
370 current_time()
371 {
372         const int buflen = 50;
373         time_t t;
374         struct tm *lt;
375         char b[buflen];
376         time(&t);
377         lt = localtime(&t);
378         strftime(b, buflen, " [%X] ", lt);
379         return String(b);
380 }
381
382 void
383 synfig::error(const char *format,...)
384 {
385         va_list args;
386         va_start(args,format);
387         error(vstrprintf(format,args));
388 }
389
390 void
391 synfig::error(const String &str)
392 {
393         static Mutex mutex; Mutex::Lock lock(mutex);
394         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
395 }
396
397 void
398 synfig::warning(const char *format,...)
399 {
400         va_list args;
401         va_start(args,format);
402         warning(vstrprintf(format,args));
403 }
404
405 void
406 synfig::warning(const String &str)
407 {
408         static Mutex mutex; Mutex::Lock lock(mutex);
409         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
410 }
411
412 void
413 synfig::info(const char *format,...)
414 {
415         va_list args;
416         va_start(args,format);
417         info(vstrprintf(format,args));
418 }
419
420 void
421 synfig::info(const String &str)
422 {
423         static Mutex mutex; Mutex::Lock lock(mutex);
424         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;
425 }