7f860cefed4cf3584c104849e4c6abe33197b982
[goodguy/history.git] / cinelerra-5.1 / cinelerra / main.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "aboutprefs.h"
22 #include "arraylist.h"
23 #include "batchrender.h"
24 #include "bcsignals.h"
25 #include "edl.h"
26 #include "file.h"
27 #include "filexml.h"
28 #include "filesystem.h"
29 #include "language.h"
30 #include "langinfo.h"
31 #include "loadfile.inc"
32 #include "mainmenu.h"
33 #include "mutex.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "pluginserver.h"
37 #include "preferences.h"
38 #include "renderfarmclient.h"
39 #include "units.h"
40 #include "versioninfo.h"
41
42 #include <locale.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #ifdef LEAKER
47 #define STRC(v) printf("==new %p from %p sz %jd\n", v, __builtin_return_address(0), n)
48 #define STRD(v) printf("==del %p from %p\n", v, __builtin_return_address(0))
49 void *operator new(size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
50 void operator delete(void *t) { STRD(t); free(t); }
51 void operator delete(void *t,size_t n) { STRD(t); free(t); }
52 void *operator new[](size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
53 void operator delete[](void *t) { STRD(t); free(t); }
54 void operator delete[](void *t,size_t n) { STRD(t); free(t); }
55 #endif
56
57 enum
58 {
59         DO_GUI,
60         DO_DEAMON,
61         DO_DEAMON_FG,
62         DO_BRENDER,
63         DO_USAGE,
64         DO_BATCHRENDER
65 };
66
67 #include "thread.h"
68
69
70 class CommandLineThread : public Thread
71 {
72 public:
73         CommandLineThread(ArrayList<char*> *filenames,
74                 MWindow *mwindow)
75         {
76                 this->filenames = filenames;
77                 this->mwindow = mwindow;
78         }
79
80
81         ~CommandLineThread()
82         {
83         }
84
85         void run()
86         {
87 //PRINT_TRACE
88                 mwindow->gui->lock_window("main");
89 //PRINT_TRACE
90                 mwindow->load_filenames(filenames, LOADMODE_REPLACE);
91 //PRINT_TRACE
92                 if( filenames->size() == 1 )
93                         mwindow->gui->mainmenu->add_load(filenames->get(0));
94 //PRINT_TRACE
95                 mwindow->gui->unlock_window();
96 //PRINT_TRACE
97         }
98
99         MWindow *mwindow;
100         ArrayList<char*> *filenames;
101 };
102
103 long cin_timezone;
104
105 int main(int argc, char *argv[])
106 {
107 // handle command line arguments first
108         srand(time(0));
109         ArrayList<char*> filenames;
110         FileSystem fs;
111
112         time_t st; time(&st);
113         struct tm ltm, gtm;
114         localtime_r(&st, &ltm);
115         gmtime_r(&st, &gtm);
116         int tzofs = ltm.tm_hour - gtm.tm_hour;
117         cin_timezone = tzofs * 60*60;
118
119         int operation = DO_GUI;
120         int deamon_port = DEAMON_PORT;
121         char deamon_path[BCTEXTLEN];
122         char config_path[BCTEXTLEN];
123         char batch_path[BCTEXTLEN];
124         int nice_value = 20;
125         int load_perpetual = 1;
126         config_path[0] = 0;
127         batch_path[0] = 0;
128         deamon_path[0] = 0;
129         Units::init();
130
131         File::init_cin_path();
132         const char *locale_path = File::get_locale_path();
133         const char *cin = File::get_cin();
134
135         bindtextdomain(cin, locale_path);
136         textdomain(cin);
137         setlocale(LC_MESSAGES, "");
138
139 #ifdef X_HAVE_UTF8_STRING
140         char *loc = setlocale(LC_CTYPE, "");
141         if( loc ) {
142                 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
143                 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
144
145                 // Extract from locale language & region
146                 char locbuf[32], *p;
147                 locbuf[0] = 0;
148                 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
149                         strncpy(locbuf, loc, p - loc);
150                         locbuf[p - loc] = 0;
151                 }
152                 else if( strlen(loc) < sizeof(locbuf)-1 )
153                         strcpy(locbuf, loc);
154
155                 // Locale 'C' does not give useful language info - assume en
156                 if( !locbuf[0] || locbuf[0] == 'C' )
157                         strcpy(locbuf, "en");
158
159                 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
160                         *p++ = 0;
161                         strcpy(BC_Resources::language, locbuf);
162                         if( strlen(p) < LEN_LANG )
163                                 strcpy(BC_Resources::region, p);
164                 }
165                 else if( strlen(locbuf) < LEN_LANG )
166                         strcpy(BC_Resources::language, locbuf);
167         }
168         else
169                 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
170 #else
171         setlocale(LC_CTYPE, "");
172 #endif
173         tzset();
174
175         int load_backup = 0;
176         int start_remote_control = 0;
177
178         for( int i = 1; i < argc; i++ ) {
179                 if( !strcmp(argv[i], "-h") ) {
180                         operation = DO_USAGE;
181                 }
182                 else if( !strcmp(argv[i], "-z") ) {
183                         start_remote_control = 1;
184                 }
185                 else if( !strcmp(argv[i], "-r") ) {
186                         operation = DO_BATCHRENDER;
187                         if( argc > i + 1 ) {
188                                 if( argv[i + 1][0] != '-' ) {
189                                         strcpy(batch_path, argv[i + 1]);
190                                         i++;
191                                 }
192                         }
193                 }
194                 else if( !strcmp(argv[i], "-c") ) {
195                         if( argc > i + 1 ) {
196                                 strcpy(config_path, argv[i + 1]);
197                                 i++;
198                         }
199                         else {
200                                 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
201                         }
202                 }
203                 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
204                         operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
205                         if( argc > i + 1 ) {
206                                 if( atol(argv[i + 1]) > 0 ) {
207                                         deamon_port = atol(argv[i + 1]);
208                                         i++;
209                                 }
210                         }
211                 }
212                 else if( !strcmp(argv[i], "-b") ) {
213                         operation = DO_BRENDER;
214                         if( i > argc - 2 ) {
215                                 fprintf(stderr, _("-b may not be used by the user.\n"));
216                                 exit(1);
217                         }
218                         else
219                                 strcpy(deamon_path, argv[i + 1]);
220                 }
221                 else if( !strcmp(argv[i], "-n") ) {
222                         if( argc > i + 1 ) {
223                                 nice_value = atol(argv[i + 1]);
224                                 i++;
225                         }
226                 }
227                 else if( !strcmp(argv[i], "-x") ) {
228                         load_backup = 1;
229                 }
230                 else if( !strcmp(argv[i], "-S") ) {
231                         load_perpetual = 0;
232                 }
233                 else {
234                         char *new_filename;
235                         new_filename = new char[BCTEXTLEN];
236                         strcpy(new_filename, argv[i]);
237                         fs.complete_path(new_filename);
238                         filenames.append(new_filename);
239                 }
240         }
241
242
243
244         if( operation == DO_GUI ||
245             operation == DO_DEAMON || operation == DO_DEAMON_FG ||
246             operation == DO_USAGE  || operation == DO_BATCHRENDER) {
247
248 #ifndef REPOMAINTXT
249 #define REPOMAINTXT ""
250 #endif
251 #ifndef COPYRIGHTTEXT1
252 #define COPYRIGHTTEXT1 ""
253 #endif
254 #ifndef COPYRIGHTTEXT2
255 #define COPYRIGHTTEXT2 ""
256 #endif
257                 fprintf(stderr, "%s %s - %s\n%s",
258                         PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
259                         REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
260                 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
261                         "and you are welcome to change it and/or distribute copies of it under\n"
262                         "certain conditions. There is absolutely no warranty for %s.\n\n",
263                         PROGRAM_NAME, PROGRAM_NAME);
264         }
265
266         switch( operation ) {
267                 case DO_USAGE:
268                         printf(_("\nUsage:\n"));
269                         printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
270                         printf(_("-d = Run in the background as renderfarm client.  The port (400) is optional.\n"));
271                         printf(_("-f = Run in the foreground as renderfarm client.  Substitute for -d.\n"));
272                         printf(_("-n = Nice value if running as renderfarm client. (20)\n"));
273                         printf(_("-c = Configuration file to use instead of %s/%s.\n"),
274                                 File::get_config_path(), CONFIG_FILE);
275                         printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI.  batch file is optional.\n"),
276                                 File::get_config_path(), BATCH_PATH);
277                         printf(_("-S = do not reload perpetual session\n"));
278                         printf(_("-x = reload from backup\n"));
279                         printf(_("filenames = files to load\n\n\n"));
280                         exit(0);
281                         break;
282
283                 case DO_DEAMON:
284                 case DO_DEAMON_FG: {
285                         if( operation == DO_DEAMON ) {
286                                 int pid = fork();
287
288                                 if( pid ) {
289 // Redhat 9 requires _exit instead of exit here.
290                                         _exit(0);
291                                 }
292                         }
293
294                         RenderFarmClient client(deamon_port,
295                                 0,
296                                 nice_value,
297                                 config_path);
298                         client.main_loop();
299                         break; }
300
301 // Same thing without detachment
302                 case DO_BRENDER: {
303                         RenderFarmClient client(0,
304                                 deamon_path,
305                                 20,
306                                 config_path);
307                         client.main_loop();
308                         break; }
309
310                 case DO_BATCHRENDER: {
311                         BatchRenderThread *thread = new BatchRenderThread;
312                         thread->start_rendering(config_path,
313                                 batch_path);
314                         break; }
315
316                 case DO_GUI: {
317                         int restart = 0, done = 0;
318                         while( !done ) {
319                                 BC_WindowBase::get_resources()->vframe_shm = 0;
320                                 MWindow mwindow;
321                                 mwindow.create_objects(1, !filenames.total, config_path);
322                                 CommandLineThread *thread  = 0;
323                                 if( mwindow.preferences->perpetual_session && load_perpetual )
324                                         mwindow.load_undo_data();
325 //SET_TRACE
326 // load the initial files on seperate tracks
327 // use a new thread so it doesn't block the GUI
328                                 if( filenames.total ) {
329                                         thread  = new CommandLineThread(&filenames, &mwindow);
330                                         thread->start();
331 //PRINT_TRACE
332                                 }
333                                 if( load_backup ) {
334                                         load_backup = 0;
335                                         mwindow.gui->lock_window("main");
336                                         mwindow.load_backup();
337                                         mwindow.gui->unlock_window();
338                                 }
339                                 if( start_remote_control ) {
340                                         start_remote_control = 0;
341                                         mwindow.gui->remote_control->activate();
342                                 }
343 // run the program
344 //PRINT_TRACE
345                                 mwindow.start();
346                                 mwindow.run();
347 //PRINT_TRACE
348                                 restart = mwindow.restart();
349                                 if( restart ) {
350                                         mwindow.save_backup();
351                                         load_backup = 1;
352                                         start_remote_control =
353                                                 mwindow.gui->remote_control->is_active();
354                                 }
355                                 if( restart <= 0 )
356                                         done = 1;
357
358                                 mwindow.save_defaults();
359                                 if( mwindow.preferences->perpetual_session )
360                                         mwindow.save_undo_data();
361 //PRINT_TRACE
362                                 filenames.remove_all_objects();
363                                 delete thread;
364                         }
365
366                         if( restart < 0 ) {
367                                 char exe_path[BCTEXTLEN];
368                                 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
369                                 if( len < 0 ) break;
370                                 exe_path[len] = 0;
371                                 char *av[4] = { 0, };  int ac = 0;
372                                 av[ac++] = exe_path;
373                                 if( load_backup ) av[ac++] = (char*) "-x";
374                                 if( start_remote_control ) av[ac++] = (char*) "-z";
375                                 av[ac++] = 0;
376                                 execv(exe_path, av);
377                         }
378 //SET_TRACE
379 DISABLE_BUFFER
380                 break; }
381         }
382
383         filenames.remove_all_objects();
384         Units::finit();
385
386         time_t et; time(&et);
387         long dt = et - st;
388         printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
389         return 0;
390 }
391