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