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