Fixed some more confidential notices
[synfig.git] / synfig-core / tags / stable / src / sinfg / 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         const char *locations[]=
283         {
284                 "standard",     //0
285                 "./"MODULE_LIST_FILENAME,       //1
286                 "../etc/"MODULE_LIST_FILENAME,  //1
287                 "~/.sinfg/"MODULE_LIST_FILENAME, //2
288                 "/usr/local/lib/sinfg/modules/"MODULE_LIST_FILENAME, //3
289                 "/usr/local/etc/"MODULE_LIST_FILENAME,
290 #ifdef SYSCONFDIR
291                 SYSCONFDIR"/"MODULE_LIST_FILENAME,
292 #endif
293 #ifdef __APPLE__
294                 "/Library/Frameworks/sinfg.framework/Resources/"MODULE_LIST_FILENAME,
295                 "/Library/SINFG/"MODULE_LIST_FILENAME,
296                 "~/Library/SINFG/"MODULE_LIST_FILENAME,
297 #endif
298 #ifdef WIN32
299                 "C:\\Program Files\\SINFG\\etc\\"MODULE_LIST_FILENAME,
300 #endif
301         };
302         String module_path=prefix+"/etc/"+MODULE_LIST_FILENAME;
303         locations[3]=module_path.c_str();
304         
305         for(i=0;i<(signed)(sizeof(locations)/sizeof(char*));i++)
306                 if(retrieve_modules_to_load(locations[i],modules_to_load))
307                         if(cb)cb->task(strprintf(_("Loading modules from %s"),locations[i]));
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         CHECK_EXPIRE_TIME();
320
321         
322         if(cb)cb->amount_complete(100, 100);
323         if(cb)cb->task(_("DONE"));
324 }
325
326 sinfg::Main::~Main()
327 {
328         ref_count_.detach();
329         if(!sinfg_ref_count_.unique())
330                 return;
331         sinfg_ref_count_.detach();
332
333         // Add deinitialization after this point
334
335         if(get_open_canvas_map().size())
336         {
337                 sinfg::warning("Canvases still open!");
338                 std::map<sinfg::String, etl::loose_handle<Canvas> >::iterator iter;
339                 for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
340                 {
341                         sinfg::warning("%s: count()=%d",iter->first.c_str(), iter->second.count());
342                 }
343         }
344                 
345         ValueNode::subsys_stop();
346         Importer::subsys_stop();
347         Target::subsys_stop();
348         Layer::subsys_stop();
349
350         /*! \fixme For some reason, uncommenting the next
351         **      line will cause things to crash. This needs to be
352         **      looked into at some point. */
353         //Module::subsys_stop();
354
355 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
356         signal(SIGPIPE, SIG_DFL);
357 #endif
358 }
359
360
361
362
363
364
365
366
367
368 void
369 sinfg::error(const char *format,...)
370 {
371         va_list args;
372         va_start(args,format);
373         error(vstrprintf(format,args));
374 }
375
376 void
377 sinfg::error(const String &str)
378 {
379         static Mutex mutex; Mutex::Lock lock(mutex);
380         cerr<<"sinfg("<<getpid()<<"): "<<_("error")<<": "+str<<endl;
381 }
382
383 void
384 sinfg::warning(const char *format,...)
385 {
386         va_list args;
387         va_start(args,format);
388         warning(vstrprintf(format,args));
389 }
390
391 void
392 sinfg::warning(const String &str)
393 {
394         static Mutex mutex; Mutex::Lock lock(mutex);
395         cerr<<"sinfg("<<getpid()<<"): "<<_("warning")<<": "+str<<endl;
396 }
397
398 void
399 sinfg::info(const char *format,...)
400 {
401         va_list args;
402         va_start(args,format);
403         info(vstrprintf(format,args));
404 }
405
406 void
407 sinfg::info(const String &str)
408 {
409         static Mutex mutex; Mutex::Lock lock(mutex);
410         cerr<<"sinfg("<<getpid()<<"): "<<_("info")<<": "+str<<endl;
411 }