MatN prevents continuous reload of plugins for AppImage + Andrew libaom compile mod
[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 /*******************************************************************
146 * @mainpage
147 * Cinelerra-GG is a professional video editor for Linux. It is timeline
148 * based, supports hundreds of (de)coders, LADSPA and LV2 audio plugins,
149 * FFMPEG audio/video plugins, plus built-in audio/video plugins.
150 * Screen sizes up to 8K, any frame rate, extensive compositing features,
151 * title generator and more.
152 *
153 ********************************************************************/
154 int main(int argc, char *argv[])
155 {
156 // handle command line arguments first
157         srand(time(0));
158         ArrayList<char*> filenames;
159         filenames.set_array_delete();
160         FileSystem fs;
161
162         time_t st; time(&st);
163         struct tm ltm, gtm;
164         localtime_r(&st, &ltm);
165         gmtime_r(&st, &gtm);
166         int tzofs = ltm.tm_hour - gtm.tm_hour;
167         cin_timezone = tzofs * 60*60;
168
169         int operation = DO_GUI;
170         int deamon_port = DEAMON_PORT;
171         char deamon_path[BCTEXTLEN];
172         char config_path[BCTEXTLEN];
173         char batch_path[BCTEXTLEN];
174         int nice_value = 20;
175         int load_perpetual = 1;
176         config_path[0] = 0;
177         batch_path[0] = 0;
178         deamon_path[0] = 0;
179         Units::init();
180         const char *lang = getenv("LANGUAGE");
181         if( lang ) lang = cstrdup(lang);
182         File::init_cin_env_vars();
183         const char *locale_path = File::get_locale_path();
184         const char *cin = File::get_cin();
185
186         bindtextdomain(cin, locale_path);
187         textdomain(cin);
188         setlocale(LC_MESSAGES, "");
189
190 #ifdef X_HAVE_UTF8_STRING
191         char *loc = setlocale(LC_CTYPE, "");
192         if( loc ) {
193                 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
194                 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
195
196                 // Extract from locale language & region
197                 char locbuf[32], *p;
198                 locbuf[0] = 0;
199                 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
200                         strncpy(locbuf, loc, p - loc);
201                         locbuf[p - loc] = 0;
202                 }
203                 else if( strlen(loc) < sizeof(locbuf)-1 )
204                         strcpy(locbuf, loc);
205
206                 // Locale 'C' does not give useful language info - assume en
207                 if( !locbuf[0] || locbuf[0] == 'C' )
208                         strcpy(locbuf, "en");
209
210                 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
211                         *p++ = 0;
212                         strcpy(BC_Resources::language, locbuf);
213                         if( strlen(p) < LEN_LANG )
214                                 strcpy(BC_Resources::region, p);
215                 }
216                 else if( strlen(locbuf) < LEN_LANG )
217                         strcpy(BC_Resources::language, locbuf);
218         }
219         else
220                 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
221 #else
222         setlocale(LC_CTYPE, "");
223 #endif
224         tzset();
225
226         int load_backup = 0;
227         int start_remote_control = 0;
228
229         for( int i = 1; i < argc; i++ ) {
230                 if( !strcmp(argv[i], "-h") ) {
231                         operation = DO_USAGE;
232                 }
233                 else if( !strcmp(argv[i], "-z") ) {
234                         start_remote_control = 1;
235                 }
236                 else if( !strcmp(argv[i], "-r") ) {
237                         operation = DO_BATCHRENDER;
238                         if( argc > i + 1 ) {
239                                 if( argv[i + 1][0] != '-' ) {
240                                         strcpy(batch_path, argv[i + 1]);
241                                         i++;
242                                 }
243                         }
244                 }
245                 else if( !strcmp(argv[i], "-c") ) {
246                         if( argc > i + 1 ) {
247                                 strcpy(config_path, argv[i + 1]);
248                                 i++;
249                         }
250                         else {
251                                 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
252                         }
253                 }
254                 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
255                         operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
256                         if( argc > i + 1 ) {
257                                 if( atol(argv[i + 1]) > 0 ) {
258                                         deamon_port = atol(argv[i + 1]);
259                                         i++;
260                                 }
261                         }
262                 }
263                 else if( !strcmp(argv[i], "-b") ) {
264                         operation = DO_BRENDER;
265                         if( i > argc - 2 ) {
266                                 fprintf(stderr, _("-b may not be used by the user.\n"));
267                                 exit(1);
268                         }
269                         else
270                                 strcpy(deamon_path, argv[i + 1]);
271                 }
272                 else if( !strcmp(argv[i], "-n") ) {
273                         if( argc > i + 1 ) {
274                                 nice_value = atol(argv[i + 1]);
275                                 i++;
276                         }
277                 }
278                 else if( !strcmp(argv[i], "-x") ) {
279                         load_backup = 1;
280                 }
281                 else if( !strcmp(argv[i], "-S") ) {
282                         load_perpetual = 0;
283                 }
284                 else {
285                         char *new_filename;
286                         new_filename = new char[BCTEXTLEN];
287                         strcpy(new_filename, argv[i]);
288                         fs.complete_path(new_filename);
289                         filenames.append(new_filename);
290                 }
291         }
292
293         float scale = operation == DO_GUI ?
294                 get_layout_scale() : 1;
295         // runs XInitThreads
296         BC_WindowBase::init_resources(scale);
297
298         if( operation == DO_GUI ||
299             operation == DO_DEAMON || operation == DO_DEAMON_FG ||
300             operation == DO_USAGE  || operation == DO_BATCHRENDER) {
301
302 #ifndef REPOMAINTXT
303 #define REPOMAINTXT ""
304 #endif
305 #ifndef COPYRIGHTTEXT1
306 #define COPYRIGHTTEXT1 ""
307 #endif
308 #ifndef COPYRIGHTTEXT2
309 #define COPYRIGHTTEXT2 ""
310 #endif
311                 fprintf(stderr, "%s %s - %s\n%s",
312                         PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
313                         REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
314                 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
315                         "and you are welcome to change it and/or distribute copies of it under\n"
316                         "certain conditions. There is absolutely no warranty for %s.\n\n",
317                         PROGRAM_NAME, PROGRAM_NAME);
318         }
319
320         switch( operation ) {
321                 case DO_USAGE:
322                         printf(_("\nUsage:\n"));
323                         printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
324                         printf(_("-d = Run in the background as renderfarm client.  The port (400) is optional.\n"));
325                         printf(_("-f = Run in the foreground as renderfarm client.  Substitute for -d.\n"));
326                         printf(_("-n = Nice value if running as renderfarm client. (19)\n"));
327                         printf(_("-c = Configuration file to use instead of %s/%s.\n"),
328                                 File::get_config_path(), CONFIG_FILE);
329                         printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI.  batch file is optional.\n"),
330                                 File::get_config_path(), BATCH_PATH);
331                         printf(_("-S = do not reload perpetual session\n"));
332                         printf(_("-x = reload from backup\n"));
333                         printf(_("filenames = files to load\n\n\n"));
334                         exit(0);
335                         break;
336
337                 case DO_DEAMON:
338                 case DO_DEAMON_FG: {
339                         if( operation == DO_DEAMON ) {
340                                 int pid = fork();
341
342                                 if( pid ) {
343 // Redhat 9 requires _exit instead of exit here.
344                                         _exit(0);
345                                 }
346                         }
347
348                         RenderFarmClient client(deamon_port,
349                                 0,
350                                 nice_value,
351                                 config_path);
352                         client.main_loop();
353                         break; }
354
355 // Same thing without detachment
356                 case DO_BRENDER: {
357                         RenderFarmClient client(0,
358                                 deamon_path,
359                                 20,
360                                 config_path);
361                         client.main_loop();
362                         break; }
363
364                 case DO_BATCHRENDER: {
365                         BatchRenderThread *thread = new BatchRenderThread(0);
366                         thread->start_rendering(config_path,
367                                 batch_path);
368                         break; }
369
370                 case DO_GUI: {
371                         int restart = 0, done = 0;
372                         while( !done ) {
373                                 BC_WindowBase::get_resources()->vframe_shm = 0;
374                                 MWindow mwindow;
375                                 mwindow.create_objects(1, !filenames.total, config_path);
376                                 CommandLineThread *thread  = 0;
377                                 if( load_perpetual )
378                                         mwindow.load_undo_data();
379 //SET_TRACE
380 // load the initial files on seperate tracks
381 // use a new thread so it doesn't block the GUI
382                                 if( filenames.total ) {
383                                         thread  = new CommandLineThread(&filenames, &mwindow);
384                                         thread->start();
385 //PRINT_TRACE
386                                 }
387                                 if( load_backup ) {
388                                         load_backup = 0;
389                                         mwindow.gui->lock_window("main");
390                                         mwindow.load_backup();
391                                         mwindow.gui->unlock_window();
392                                 }
393                                 if( start_remote_control ) {
394                                         start_remote_control = 0;
395                                         mwindow.gui->remote_control->activate();
396                                 }
397 // run the program
398 //PRINT_TRACE
399                                 mwindow.start();
400                                 mwindow.run();
401 //PRINT_TRACE
402                                 restart = mwindow.restart();
403                                 if( restart ) {
404                                         mwindow.save_backup();
405                                         load_backup = 1;
406                                         start_remote_control =
407                                                 mwindow.gui->remote_control->is_active();
408                                 }
409                                 if( restart <= 0 )
410                                         done = 1;
411                                 mwindow.save_defaults();
412                                 mwindow.save_undo_data();
413 //PRINT_TRACE
414                                 filenames.remove_all_objects();
415                                 delete thread;
416                         }
417
418                         if( restart < 0 ) {
419                                 if( lang ) // reset to cmdline state
420                                         setenv("LANGUAGE", lang, 1);
421                                 else
422                                         unsetenv("LANGUAGE");
423                                 char exe_path[BCTEXTLEN];
424                                 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
425                                 if( len < 0 ) break;
426                                 exe_path[len] = 0;
427                                 char *av[4] = { 0, };  int ac = 0;
428                                 av[ac++] = exe_path;
429                                 if( load_backup ) av[ac++] = (char*) "-x";
430                                 if( start_remote_control ) av[ac++] = (char*) "-z";
431                                 av[ac++] = 0;
432                                 execv(exe_path, av);
433                         }
434 //SET_TRACE
435 DISABLE_BUFFER
436                 break; }
437         }
438
439         filenames.remove_all_objects();
440         delete [] lang;
441         Units::finit();
442         BC_WindowBase::finit_resources();
443
444         time_t et; time(&et);
445         long dt = et - st;
446         printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
447         struct rusage ru;
448         getrusage(RUSAGE_SELF, &ru);
449         long usr_ms = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
450         long us = usr_ms/1000;  int ums = usr_ms%1000;
451         long sys_ms = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
452         long ss = sys_ms/1000;  int sms = sys_ms%1000;
453         printf("Cpu time: user: %ld:%02ld:%02ld.%03d sys: %ld:%02ld:%02ld.%03d\n",
454                 us/3600, us%3600/60, us%60, ums, ss/3600, ss%3600/60, ss%60, sms);
455         return 0;
456 }
457