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