add proxy index builds, perperual session, backup.prev, disarmed track indication...
[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
104 int main(int argc, char *argv[])
105 {
106 // handle command line arguments first
107         srand(time(0));
108         ArrayList<char*> filenames;
109         FileSystem fs;
110
111
112         int operation = DO_GUI;
113         int deamon_port = DEAMON_PORT;
114         char deamon_path[BCTEXTLEN];
115         char config_path[BCTEXTLEN];
116         char batch_path[BCTEXTLEN];
117         int nice_value = 20;
118         int load_perpetual = 1;
119         config_path[0] = 0;
120         batch_path[0] = 0;
121         deamon_path[0] = 0;
122         Units::init();
123
124         File::init_cin_path();
125         const char *locale_path = File::get_locale_path();
126         const char *cin = File::get_cin();
127
128         bindtextdomain(cin, locale_path);
129         textdomain(cin);
130         setlocale(LC_MESSAGES, "");
131
132 #ifdef X_HAVE_UTF8_STRING
133         char *loc = setlocale(LC_CTYPE, "");
134         if( loc ) {
135                 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
136                 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
137
138                 // Extract from locale language & region
139                 char locbuf[32], *p;
140                 locbuf[0] = 0;
141                 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
142                         strncpy(locbuf, loc, p - loc);
143                         locbuf[p - loc] = 0;
144                 }
145                 else if( strlen(loc) < sizeof(locbuf)-1 )
146                         strcpy(locbuf, loc);
147
148                 // Locale 'C' does not give useful language info - assume en
149                 if( !locbuf[0] || locbuf[0] == 'C' )
150                         strcpy(locbuf, "en");
151
152                 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
153                         *p++ = 0;
154                         strcpy(BC_Resources::language, locbuf);
155                         if( strlen(p) < LEN_LANG )
156                                 strcpy(BC_Resources::region, p);
157                 }
158                 else if( strlen(locbuf) < LEN_LANG )
159                         strcpy(BC_Resources::language, locbuf);
160         }
161         else
162                 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
163 #else
164         setlocale(LC_CTYPE, "");
165 #endif
166         tzset();
167
168         int load_backup = 0;
169         int start_remote_control = 0;
170
171         for( int i = 1; i < argc; i++ ) {
172                 if( !strcmp(argv[i], "-h") ) {
173                         operation = DO_USAGE;
174                 }
175                 else if( !strcmp(argv[i], "-z") ) {
176                         start_remote_control = 1;
177                 }
178                 else if( !strcmp(argv[i], "-r") ) {
179                         operation = DO_BATCHRENDER;
180                         if( argc > i + 1 ) {
181                                 if( argv[i + 1][0] != '-' ) {
182                                         strcpy(batch_path, argv[i + 1]);
183                                         i++;
184                                 }
185                         }
186                 }
187                 else if( !strcmp(argv[i], "-c") ) {
188                         if( argc > i + 1 ) {
189                                 strcpy(config_path, argv[i + 1]);
190                                 i++;
191                         }
192                         else {
193                                 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
194                         }
195                 }
196                 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
197                         operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
198                         if( argc > i + 1 ) {
199                                 if( atol(argv[i + 1]) > 0 ) {
200                                         deamon_port = atol(argv[i + 1]);
201                                         i++;
202                                 }
203                         }
204                 }
205                 else if( !strcmp(argv[i], "-b") ) {
206                         operation = DO_BRENDER;
207                         if( i > argc - 2 ) {
208                                 fprintf(stderr, _("-b may not be used by the user.\n"));
209                                 exit(1);
210                         }
211                         else
212                                 strcpy(deamon_path, argv[i + 1]);
213                 }
214                 else if( !strcmp(argv[i], "-n") ) {
215                         if( argc > i + 1 ) {
216                                 nice_value = atol(argv[i + 1]);
217                                 i++;
218                         }
219                 }
220                 else if( !strcmp(argv[i], "-x") ) {
221                         load_backup = 1;
222                 }
223                 else if( !strcmp(argv[i], "-S") ) {
224                         load_perpetual = 0;
225                 }
226                 else {
227                         char *new_filename;
228                         new_filename = new char[BCTEXTLEN];
229                         strcpy(new_filename, argv[i]);
230                         fs.complete_path(new_filename);
231                         filenames.append(new_filename);
232                 }
233         }
234
235
236
237         if( operation == DO_GUI ||
238             operation == DO_DEAMON || operation == DO_DEAMON_FG ||
239             operation == DO_USAGE  || operation == DO_BATCHRENDER) {
240
241 #ifndef REPOMAINTXT
242 #define REPOMAINTXT ""
243 #endif
244 #ifndef COPYRIGHTTEXT1
245 #define COPYRIGHTTEXT1 ""
246 #endif
247 #ifndef COPYRIGHTTEXT2
248 #define COPYRIGHTTEXT2 ""
249 #endif
250                 fprintf(stderr, "%s %s - %s\n%s",
251                         PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
252                         REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
253                 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
254                         "and you are welcome to change it and/or distribute copies of it under\n"
255                         "certain conditions. There is absolutely no warranty for %s.\n\n",
256                         PROGRAM_NAME, PROGRAM_NAME);
257         }
258
259         switch( operation ) {
260                 case DO_USAGE:
261                         printf(_("\nUsage:\n"));
262                         printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
263                         printf(_("-d = Run in the background as renderfarm client.  The port (400) is optional.\n"));
264                         printf(_("-f = Run in the foreground as renderfarm client.  Substitute for -d.\n"));
265                         printf(_("-n = Nice value if running as renderfarm client. (20)\n"));
266                         printf(_("-c = Configuration file to use instead of %s/%s.\n"),
267                                 File::get_config_path(), CONFIG_FILE);
268                         printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI.  batch file is optional.\n"),
269                                 File::get_config_path(), BATCH_PATH);
270                         printf(_("-S = do not reload perpetual session\n"));
271                         printf(_("-x = reload from backup\n"));
272                         printf(_("filenames = files to load\n\n\n"));
273                         exit(0);
274                         break;
275
276                 case DO_DEAMON:
277                 case DO_DEAMON_FG: {
278                         if( operation == DO_DEAMON ) {
279                                 int pid = fork();
280
281                                 if( pid ) {
282 // Redhat 9 requires _exit instead of exit here.
283                                         _exit(0);
284                                 }
285                         }
286
287                         RenderFarmClient client(deamon_port,
288                                 0,
289                                 nice_value,
290                                 config_path);
291                         client.main_loop();
292                         break; }
293
294 // Same thing without detachment
295                 case DO_BRENDER: {
296                         RenderFarmClient client(0,
297                                 deamon_path,
298                                 20,
299                                 config_path);
300                         client.main_loop();
301                         break; }
302
303                 case DO_BATCHRENDER: {
304                         BatchRenderThread *thread = new BatchRenderThread;
305                         thread->start_rendering(config_path,
306                                 batch_path);
307                         break; }
308
309                 case DO_GUI: {
310                         int restart = 0, done = 0;
311                         while( !done ) {
312                                 BC_WindowBase::get_resources()->vframe_shm = 0;
313                                 MWindow mwindow;
314                                 mwindow.create_objects(1, !filenames.total, config_path);
315                                 CommandLineThread *thread  = 0;
316                                 if( mwindow.preferences->perpetual_session && load_perpetual )
317                                         mwindow.load_undo_data();
318 //SET_TRACE
319 // load the initial files on seperate tracks
320 // use a new thread so it doesn't block the GUI
321                                 if( filenames.total ) {
322                                         thread  = new CommandLineThread(&filenames, &mwindow);
323                                         thread->start();
324 //PRINT_TRACE
325                                 }
326                                 if( load_backup ) {
327                                         load_backup = 0;
328                                         mwindow.gui->lock_window("main");
329                                         mwindow.load_backup();
330                                         mwindow.gui->unlock_window();
331                                 }
332                                 if( start_remote_control ) {
333                                         start_remote_control = 0;
334                                         mwindow.gui->remote_control->activate();
335                                 }
336 // run the program
337 //PRINT_TRACE
338                                 mwindow.start();
339                                 mwindow.run();
340 //PRINT_TRACE
341                                 restart = mwindow.restart();
342                                 if( restart ) {
343                                         mwindow.save_backup();
344                                         load_backup = 1;
345                                         start_remote_control =
346                                                 mwindow.gui->remote_control->is_active();
347                                 }
348                                 if( restart <= 0 )
349                                         done = 1;
350
351                                 mwindow.save_defaults();
352                                 if( mwindow.preferences->perpetual_session )
353                                         mwindow.save_undo_data();
354 //PRINT_TRACE
355                                 filenames.remove_all_objects();
356                                 delete thread;
357                         }
358
359                         if( restart < 0 ) {
360                                 char exe_path[BCTEXTLEN];
361                                 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
362                                 if( len < 0 ) break;
363                                 exe_path[len] = 0;
364                                 char *av[4] = { 0, };  int ac = 0;
365                                 av[ac++] = exe_path;
366                                 if( load_backup ) av[ac++] = (char*) "-x";
367                                 if( start_remote_control ) av[ac++] = (char*) "-z";
368                                 av[ac++] = 0;
369                                 execv(exe_path, av);
370                         }
371 //SET_TRACE
372 DISABLE_BUFFER
373                 break; }
374         }
375
376         filenames.remove_all_objects();
377         Units::finit();
378
379         return 0;
380 }
381