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