b905c2a03aeaa58e8748553613a41119128495e0
[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 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
81
82
83
84
85
86
87 const char *
88 synfig::get_version()
89 {
90 #ifdef VERSION
91         return VERSION;
92 #else
93         return "Unknown";
94 #endif
95 }
96
97 const char *
98 synfig::get_build_date()
99 {
100         return __DATE__;
101 }
102
103 const char *
104 synfig::get_build_time()
105 {
106         return __TIME__;
107 }
108
109 extern const char *get_build_time();
110
111 bool
112 synfig::check_version_(int version,int vec_size, int color_size,int canvas_size,int layer_size)
113 {
114         bool ret=true;
115
116         if(version!=SYNFIG_LIBRARY_VERSION)
117         {
118                 synfig::error(_("API Version mismatch (LIB:%d, PROG:%d)"),SYNFIG_LIBRARY_VERSION,version);
119                 ret=false;
120         }
121         if(vec_size!=sizeof(Vector))
122         {
123                 synfig::error(_("Size of Vector mismatch (app:%d, lib:%d)"),vec_size,sizeof(Vector));
124                 ret=false;
125         }
126         if(color_size!=sizeof(Color))
127         {
128                 synfig::error(_("Size of Color mismatch (app:%d, lib:%d)"),color_size,sizeof(Color));
129                 ret=false;
130         }
131         if(canvas_size!=sizeof(Canvas))
132         {
133                 synfig::error(_("Size of Canvas mismatch (app:%d, lib:%d)"),canvas_size,sizeof(Canvas));
134                 ret=false;
135         }
136         if(layer_size!=sizeof(Layer))
137         {
138                 synfig::error(_("Size of Layer mismatch (app:%d, lib:%d)"),layer_size,sizeof(Layer));
139                 ret=false;
140         }
141
142         return ret;
143 }
144
145 static void broken_pipe_signal (int /*sig*/)  {
146         synfig::warning("Broken Pipe...");
147 }
148
149 bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load)
150 {
151         if(filename=="standard")
152         {
153                 return false;
154 /*
155                 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_bmp")==modules_to_load.end())
156                         modules_to_load.push_back("trgt_bmp");
157                 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_gif")==modules_to_load.end())
158                         modules_to_load.push_back("trgt_gif");
159                 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_dv")==modules_to_load.end())
160                         modules_to_load.push_back("trgt_dv");
161                 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_ffmpeg")==modules_to_load.end())
162                         modules_to_load.push_back("mod_ffmpeg");
163                 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_imagemagick")==modules_to_load.end())
164                         modules_to_load.push_back("mod_imagemagick");
165                 if(find(modules_to_load.begin(),modules_to_load.end(),"lyr_std")==modules_to_load.end())
166                         modules_to_load.push_back("lyr_std");
167                 if(find(modules_to_load.begin(),modules_to_load.end(),"lyr_freetype")==modules_to_load.end())
168                         modules_to_load.push_back("lyr_freetype");
169 #ifdef HAVE_LIBPNG
170                 if(find(modules_to_load.begin(),modules_to_load.end(),"trgt_png")==modules_to_load.end())
171                         modules_to_load.push_back("trgt_png");
172 #endif
173 #ifdef HAVE_OPENEXR
174                 if(find(modules_to_load.begin(),modules_to_load.end(),"mod_openexr")==modules_to_load.end())
175                         modules_to_load.push_back("mod_openexr");
176 #endif
177 */
178         }
179         else
180         {
181                 std::ifstream file(filename.c_str());
182                 if(!file)
183                 {
184                 //      warning("Cannot open "+filename);
185                         return false;
186                 }
187                 while(file)
188                 {
189                         String modulename;
190                         getline(file,modulename);
191                         if(!modulename.empty() && find(modules_to_load.begin(),modules_to_load.end(),modulename)==modules_to_load.end())
192                                 modules_to_load.push_back(modulename);
193                 }
194         }
195
196
197
198         return true;
199 }
200
201
202
203
204
205 synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
206         ref_count_(synfig_ref_count_)
207 {
208         if(ref_count_.count())
209                 return;
210
211         synfig_ref_count_.reset();
212         ref_count_=synfig_ref_count_;
213
214         // Add initialization after this point
215
216 #ifdef ENABLE_NLS
217         bindtextdomain("synfig", LOCALEDIR);
218 #endif
219
220         String prefix=basepath+"/..";
221         unsigned int i;
222 #ifdef _DEBUG
223         std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
224 #endif
225
226 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
227         signal(SIGPIPE, broken_pipe_signal);
228 #endif
229
230         //_config_search_path=new vector"string.h"();
231
232         // Init the subsystems
233         if(cb)cb->amount_complete(0, 100);
234         if(cb)cb->task(_("Starting Subsystem \"Modules\""));
235         if(!Module::subsys_init(prefix))
236                 throw std::runtime_error(_("Unable to initialize subsystem \"Module\""));
237
238         if(cb)cb->task(_("Starting Subsystem \"Layers\""));
239         if(!Layer::subsys_init())
240         {
241                 Module::subsys_stop();
242                 throw std::runtime_error(_("Unable to initialize subsystem \"Layers\""));
243         }
244
245         if(cb)cb->task(_("Starting Subsystem \"Targets\""));
246         if(!Target::subsys_init())
247         {
248                 Layer::subsys_stop();
249                 Module::subsys_stop();
250                 throw std::runtime_error(_("Unable to initialize subsystem \"Targets\""));
251         }
252
253         if(cb)cb->task(_("Starting Subsystem \"Importers\""));
254         if(!Importer::subsys_init())
255         {
256                 Target::subsys_stop();
257                 Layer::subsys_stop();
258                 Module::subsys_stop();
259                 throw std::runtime_error(_("Unable to initialize subsystem \"Importers\""));
260         }
261
262         if(cb)cb->task(_("Starting Subsystem \"ValueNodes\""));
263         if(!ValueNode::subsys_init())
264         {
265                 Importer::subsys_stop();
266                 Target::subsys_stop();
267                 Layer::subsys_stop();
268                 Module::subsys_stop();
269                 throw std::runtime_error(_("Unable to initialize subsystem \"ValueNodes\""));
270         }
271
272         // Load up the list importer
273         Importer::book()[String("lst")]=ListImporter::create;
274
275
276
277         // Load up the modules
278         std::list<String> modules_to_load;
279         std::vector<String> locations;
280
281         if(!getenv("SYNFIG_MODULE_LIST"))
282         {
283                 locations.push_back("standard");
284                 locations.push_back("./"MODULE_LIST_FILENAME);  //1
285                 locations.push_back("../etc/"MODULE_LIST_FILENAME);     //1
286                 locations.push_back("~/.synfig/"MODULE_LIST_FILENAME); //2
287                 locations.push_back(prefix+"/etc/"+MODULE_LIST_FILENAME); //3
288                 locations.push_back("/usr/local/etc/"MODULE_LIST_FILENAME);
289         #ifdef SYSCONFDIR
290                 locations.push_back(SYSCONFDIR"/"MODULE_LIST_FILENAME);
291         #endif
292         #ifdef __APPLE__
293                 locations.push_back("/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME);
294                 locations.push_back("/Library/Synfig/"MODULE_LIST_FILENAME);
295                 locations.push_back("~/Library/Synfig/"MODULE_LIST_FILENAME);
296         #endif
297         #ifdef WIN32
298                 locations.push_back("C:\\Program Files\\Synfig\\etc\\"MODULE_LIST_FILENAME);
299         #endif
300         }
301         else
302         {
303                 locations.push_back(getenv("SYNFIG_MODULE_LIST"));
304         }
305 /*
306         const char *locations[]=
307         {
308                 "standard",     //0
309                 "./"MODULE_LIST_FILENAME,       //1
310                 "../etc/"MODULE_LIST_FILENAME,  //1
311                 "~/.synfig/"MODULE_LIST_FILENAME, //2
312                 "/usr/local/lib/synfig/modules/"MODULE_LIST_FILENAME, //3
313                 "/usr/local/etc/"MODULE_LIST_FILENAME,
314 #ifdef SYSCONFDIR
315                 SYSCONFDIR"/"MODULE_LIST_FILENAME,
316 #endif
317 #ifdef __APPLE__
318                 "/Library/Frameworks/synfig.framework/Resources/"MODULE_LIST_FILENAME,
319                 "/Library/SYNFIG/"MODULE_LIST_FILENAME,
320                 "~/Library/SYNFIG/"MODULE_LIST_FILENAME,
321 #endif
322 #ifdef WIN32
323                 "C:\\Program Files\\SYNFIG\\etc\\"MODULE_LIST_FILENAME,
324 #endif
325         };
326 */
327
328         for(i=0;i<locations.size();i++)
329                 if(retrieve_modules_to_load(locations[i],modules_to_load))
330                         if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i].c_str()));
331
332         std::list<String>::iterator iter;
333
334         for(i=0,iter=modules_to_load.begin();iter!=modules_to_load.end();++iter,i++)
335         {
336                 Module::Register(*iter,cb);
337                 if(cb)cb->amount_complete((i+1)*100,modules_to_load.size()*100);
338         }
339
340 //      load_modules(cb);
341
342
343         if(cb)cb->amount_complete(100, 100);
344         if(cb)cb->task(_("DONE"));
345 }
346
347 synfig::Main::~Main()
348 {
349         ref_count_.detach();
350         if(!synfig_ref_count_.unique())
351                 return;
352         synfig_ref_count_.detach();
353
354         // Add deinitialization after this point
355
356         if(get_open_canvas_map().size())
357         {
358                 synfig::warning("Canvases still open!");
359                 std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
360                 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
361                 {
362                         synfig::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
363                 }
364         }
365
366         // synfig::info("ValueNode::subsys_stop()");
367         ValueNode::subsys_stop();
368         // synfig::info("Importer::subsys_stop()");
369         Importer::subsys_stop();
370         // synfig::info("Target::subsys_stop()");
371         Target::subsys_stop();
372         // synfig::info("Layer::subsys_stop()");
373         Layer::subsys_stop();
374         /*! \todo For some reason, uncommenting the next line will cause things to crash.
375                           This needs to be looked into at some point. */
376         // synfig::info("Module::subsys_stop()");
377         // Module::subsys_stop();
378         // synfig::info("Exiting");
379         
380 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
381         signal(SIGPIPE, SIG_DFL);
382 #endif
383 }
384
385 static const String
386 current_time()
387 {
388         const int buflen = 50;
389         time_t t;
390         struct tm *lt;
391         char b[buflen];
392         time(&t);
393         lt = localtime(&t);
394         strftime(b, buflen, " [%X] ", lt);
395         return String(b);
396 }
397
398 void
399 synfig::error(const char *format,...)
400 {
401         va_list args;
402         va_start(args,format);
403         error(vstrprintf(format,args));
404 }
405
406 void
407 synfig::error(const String &str)
408 {
409         static Mutex mutex; Mutex::Lock lock(mutex);
410         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("error")<<": "+str<<endl;
411 }
412
413 void
414 synfig::warning(const char *format,...)
415 {
416         va_list args;
417         va_start(args,format);
418         warning(vstrprintf(format,args));
419 }
420
421 void
422 synfig::warning(const String &str)
423 {
424         static Mutex mutex; Mutex::Lock lock(mutex);
425         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("warning")<<": "+str<<endl;
426 }
427
428 void
429 synfig::info(const char *format,...)
430 {
431         va_list args;
432         va_start(args,format);
433         info(vstrprintf(format,args));
434 }
435
436 void
437 synfig::info(const String &str)
438 {
439         static Mutex mutex; Mutex::Lock lock(mutex);
440         cerr<<"synfig("<<getpid()<<")"<<current_time()<<_("info")<<": "+str<<endl;
441 }