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