port 7.2 mods: align_edits foreground plugin refresh_frame tweak, rework soundlevel...
[goodguy/cinelerra.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 #include <sys/time.h>
46 #include <sys/resource.h>
47
48 #ifdef LEAKER
49 #if 0  // track allocators
50 #include <execinfo.h>
51 #define BT_BUF_SIZE 100
52 static void leaker()
53 {
54         void *buffer[BT_BUF_SIZE];
55         int nptrs = backtrace(buffer, BT_BUF_SIZE);
56         char **trace = backtrace_symbols(buffer, nptrs);
57         if( !trace ) return;
58         for( int i=0; i<nptrs; ) printf("%s ", trace[i++]);
59         printf("\n");
60         free(trace);
61 }
62 #define STRB ;leaker()
63 #else
64 #define STRB
65 #endif
66 #define STRC(v) printf("==new %p from %p sz %jd\n", v, __builtin_return_address(0), n)STRB
67 #define STRD(v) printf("==del %p from %p\n", v, __builtin_return_address(0))
68 void *operator new(size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
69 void operator delete(void *t) { STRD(t); free(t); }
70 void operator delete(void *t,size_t n) { STRD(t); free(t); }
71 void *operator new[](size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
72 void operator delete[](void *t) { STRD(t); free(t); }
73 void operator delete[](void *t,size_t n) { STRD(t); free(t); }
74 #endif
75
76 enum
77 {
78         DO_GUI,
79         DO_DEAMON,
80         DO_DEAMON_FG,
81         DO_BRENDER,
82         DO_USAGE,
83         DO_BATCHRENDER
84 };
85
86 #include "thread.h"
87
88
89 class CommandLineThread : public Thread
90 {
91 public:
92         CommandLineThread(ArrayList<char*> *filenames,
93                 MWindow *mwindow)
94         {
95                 this->filenames = filenames;
96                 this->mwindow = mwindow;
97         }
98
99
100         ~CommandLineThread()
101         {
102         }
103
104         void run()
105         {
106 //PRINT_TRACE
107                 mwindow->gui->lock_window("main");
108 //PRINT_TRACE
109                 mwindow->load_filenames(filenames, LOADMODE_REPLACE);
110 //PRINT_TRACE
111                 if( filenames->size() == 1 )
112                         mwindow->gui->mainmenu->add_load(filenames->get(0));
113 //PRINT_TRACE
114                 mwindow->gui->unlock_window();
115 //PRINT_TRACE
116         }
117
118         MWindow *mwindow;
119         ArrayList<char*> *filenames;
120 };
121
122 long cin_timezone;
123
124 static float get_layout_scale()
125 {
126         char config_path[BCTEXTLEN];
127         sprintf(config_path,"%s/%s", File::get_config_path(), CONFIG_FILE);
128         FILE *fp = fopen(config_path,"r");
129         if( !fp ) return 0;
130         float scale = 0;
131         char line[BCTEXTLEN];
132         line[BCTEXTLEN-1] = 0;
133         while( fgets(line, BCTEXTLEN-1, fp) ) {
134                 if( !strncmp(line, "LAYOUT_SCALE ",12+1) ) {
135                         scale = atof(line+12);
136                         break;
137                 }
138         }
139         fclose(fp);
140         return scale;
141 }
142
143 int main(int argc, char *argv[])
144 {
145 // handle command line arguments first
146         srand(time(0));
147         ArrayList<char*> filenames;
148         filenames.set_array_delete();
149         FileSystem fs;
150
151         time_t st; time(&st);
152         struct tm ltm, gtm;
153         localtime_r(&st, &ltm);
154         gmtime_r(&st, &gtm);
155         int tzofs = ltm.tm_hour - gtm.tm_hour;
156         cin_timezone = tzofs * 60*60;
157
158         int operation = DO_GUI;
159         int deamon_port = DEAMON_PORT;
160         char deamon_path[BCTEXTLEN];
161         char config_path[BCTEXTLEN];
162         char batch_path[BCTEXTLEN];
163         int nice_value = 20;
164         int load_perpetual = 1;
165         config_path[0] = 0;
166         batch_path[0] = 0;
167         deamon_path[0] = 0;
168         Units::init();
169
170         File::init_cin_path();
171         const char *locale_path = File::get_locale_path();
172         const char *cin = File::get_cin();
173
174         bindtextdomain(cin, locale_path);
175         textdomain(cin);
176         setlocale(LC_MESSAGES, "");
177
178 #ifdef X_HAVE_UTF8_STRING
179         char *loc = setlocale(LC_CTYPE, "");
180         if( loc ) {
181                 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
182                 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
183
184                 // Extract from locale language & region
185                 char locbuf[32], *p;
186                 locbuf[0] = 0;
187                 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
188                         strncpy(locbuf, loc, p - loc);
189                         locbuf[p - loc] = 0;
190                 }
191                 else if( strlen(loc) < sizeof(locbuf)-1 )
192                         strcpy(locbuf, loc);
193
194                 // Locale 'C' does not give useful language info - assume en
195                 if( !locbuf[0] || locbuf[0] == 'C' )
196                         strcpy(locbuf, "en");
197
198                 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
199                         *p++ = 0;
200                         strcpy(BC_Resources::language, locbuf);
201                         if( strlen(p) < LEN_LANG )
202                                 strcpy(BC_Resources::region, p);
203                 }
204                 else if( strlen(locbuf) < LEN_LANG )
205                         strcpy(BC_Resources::language, locbuf);
206         }
207         else
208                 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
209 #else
210         setlocale(LC_CTYPE, "");
211 #endif
212         tzset();
213
214         int load_backup = 0;
215         int start_remote_control = 0;
216
217         for( int i = 1; i < argc; i++ ) {
218                 if( !strcmp(argv[i], "-h") ) {
219                         operation = DO_USAGE;
220                 }
221                 else if( !strcmp(argv[i], "-z") ) {
222                         start_remote_control = 1;
223                 }
224                 else if( !strcmp(argv[i], "-r") ) {
225                         operation = DO_BATCHRENDER;
226                         if( argc > i + 1 ) {
227                                 if( argv[i + 1][0] != '-' ) {
228                                         strcpy(batch_path, argv[i + 1]);
229                                         i++;
230                                 }
231                         }
232                 }
233                 else if( !strcmp(argv[i], "-c") ) {
234                         if( argc > i + 1 ) {
235                                 strcpy(config_path, argv[i + 1]);
236                                 i++;
237                         }
238                         else {
239                                 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
240                         }
241                 }
242                 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
243                         operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
244                         if( argc > i + 1 ) {
245                                 if( atol(argv[i + 1]) > 0 ) {
246                                         deamon_port = atol(argv[i + 1]);
247                                         i++;
248                                 }
249                         }
250                 }
251                 else if( !strcmp(argv[i], "-b") ) {
252                         operation = DO_BRENDER;
253                         if( i > argc - 2 ) {
254                                 fprintf(stderr, _("-b may not be used by the user.\n"));
255                                 exit(1);
256                         }
257                         else
258                                 strcpy(deamon_path, argv[i + 1]);
259                 }
260                 else if( !strcmp(argv[i], "-n") ) {
261                         if( argc > i + 1 ) {
262                                 nice_value = atol(argv[i + 1]);
263                                 i++;
264                         }
265                 }
266                 else if( !strcmp(argv[i], "-x") ) {
267                         load_backup = 1;
268                 }
269                 else if( !strcmp(argv[i], "-S") ) {
270                         load_perpetual = 0;
271                 }
272                 else {
273                         char *new_filename;
274                         new_filename = new char[BCTEXTLEN];
275                         strcpy(new_filename, argv[i]);
276                         fs.complete_path(new_filename);
277                         filenames.append(new_filename);
278                 }
279         }
280
281         float scale = operation == DO_GUI ?
282                 get_layout_scale() : 1;
283         // runs XInitThreads
284         BC_WindowBase::init_resources(scale);
285
286         if( operation == DO_GUI ||
287             operation == DO_DEAMON || operation == DO_DEAMON_FG ||
288             operation == DO_USAGE  || operation == DO_BATCHRENDER) {
289
290 #ifndef REPOMAINTXT
291 #define REPOMAINTXT ""
292 #endif
293 #ifndef COPYRIGHTTEXT1
294 #define COPYRIGHTTEXT1 ""
295 #endif
296 #ifndef COPYRIGHTTEXT2
297 #define COPYRIGHTTEXT2 ""
298 #endif
299                 fprintf(stderr, "%s %s - %s\n%s",
300                         PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
301                         REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
302                 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
303                         "and you are welcome to change it and/or distribute copies of it under\n"
304                         "certain conditions. There is absolutely no warranty for %s.\n\n",
305                         PROGRAM_NAME, PROGRAM_NAME);
306         }
307
308         switch( operation ) {
309                 case DO_USAGE:
310                         printf(_("\nUsage:\n"));
311                         printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
312                         printf(_("-d = Run in the background as renderfarm client.  The port (400) is optional.\n"));
313                         printf(_("-f = Run in the foreground as renderfarm client.  Substitute for -d.\n"));
314                         printf(_("-n = Nice value if running as renderfarm client. (19)\n"));
315                         printf(_("-c = Configuration file to use instead of %s/%s.\n"),
316                                 File::get_config_path(), CONFIG_FILE);
317                         printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI.  batch file is optional.\n"),
318                                 File::get_config_path(), BATCH_PATH);
319                         printf(_("-S = do not reload perpetual session\n"));
320                         printf(_("-x = reload from backup\n"));
321                         printf(_("filenames = files to load\n\n\n"));
322                         exit(0);
323                         break;
324
325                 case DO_DEAMON:
326                 case DO_DEAMON_FG: {
327                         if( operation == DO_DEAMON ) {
328                                 int pid = fork();
329
330                                 if( pid ) {
331 // Redhat 9 requires _exit instead of exit here.
332                                         _exit(0);
333                                 }
334                         }
335
336                         RenderFarmClient client(deamon_port,
337                                 0,
338                                 nice_value,
339                                 config_path);
340                         client.main_loop();
341                         break; }
342
343 // Same thing without detachment
344                 case DO_BRENDER: {
345                         RenderFarmClient client(0,
346                                 deamon_path,
347                                 20,
348                                 config_path);
349                         client.main_loop();
350                         break; }
351
352                 case DO_BATCHRENDER: {
353                         BatchRenderThread *thread = new BatchRenderThread(0);
354                         thread->start_rendering(config_path,
355                                 batch_path);
356                         break; }
357
358                 case DO_GUI: {
359                         int restart = 0, done = 0;
360                         while( !done ) {
361                                 BC_WindowBase::get_resources()->vframe_shm = 0;
362                                 MWindow mwindow;
363                                 mwindow.create_objects(1, !filenames.total, config_path);
364                                 CommandLineThread *thread  = 0;
365                                 if( mwindow.preferences->perpetual_session && load_perpetual )
366                                         mwindow.load_undo_data();
367 //SET_TRACE
368 // load the initial files on seperate tracks
369 // use a new thread so it doesn't block the GUI
370                                 if( filenames.total ) {
371                                         thread  = new CommandLineThread(&filenames, &mwindow);
372                                         thread->start();
373 //PRINT_TRACE
374                                 }
375                                 if( load_backup ) {
376                                         load_backup = 0;
377                                         mwindow.gui->lock_window("main");
378                                         mwindow.load_backup();
379                                         mwindow.gui->unlock_window();
380                                 }
381                                 if( start_remote_control ) {
382                                         start_remote_control = 0;
383                                         mwindow.gui->remote_control->activate();
384                                 }
385 // run the program
386 //PRINT_TRACE
387                                 mwindow.start();
388                                 mwindow.run();
389 //PRINT_TRACE
390                                 restart = mwindow.restart();
391                                 if( restart ) {
392                                         mwindow.save_backup();
393                                         load_backup = 1;
394                                         start_remote_control =
395                                                 mwindow.gui->remote_control->is_active();
396                                 }
397                                 if( restart <= 0 )
398                                         done = 1;
399
400                                 mwindow.save_defaults();
401                                 if( mwindow.preferences->perpetual_session )
402                                         mwindow.save_undo_data();
403 //PRINT_TRACE
404                                 filenames.remove_all_objects();
405                                 delete thread;
406                         }
407
408                         if( restart < 0 ) {
409                                 char exe_path[BCTEXTLEN];
410                                 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
411                                 if( len < 0 ) break;
412                                 exe_path[len] = 0;
413                                 char *av[4] = { 0, };  int ac = 0;
414                                 av[ac++] = exe_path;
415                                 if( load_backup ) av[ac++] = (char*) "-x";
416                                 if( start_remote_control ) av[ac++] = (char*) "-z";
417                                 av[ac++] = 0;
418                                 execv(exe_path, av);
419                         }
420 //SET_TRACE
421 DISABLE_BUFFER
422                 break; }
423         }
424
425         filenames.remove_all_objects();
426         Units::finit();
427         BC_WindowBase::finit_resources();
428
429         time_t et; time(&et);
430         long dt = et - st;
431         printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
432         struct rusage ru;
433         getrusage(RUSAGE_SELF, &ru);
434         long usr_ms = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
435         long us = usr_ms/1000;  int ums = usr_ms%1000;
436         long sys_ms = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
437         long ss = sys_ms/1000;  int sms = sys_ms%1000;
438         printf("Cpu time: user: %ld:%02ld:%02ld.%03d sys: %ld:%02ld:%02ld.%03d\n",
439                 us/3600, us%3600/60, us%60, ums, ss/3600, ss%3600/60, ss%60, sms);
440         return 0;
441 }
442