3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
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.
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.
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
21 #include "aboutprefs.h"
22 #include "arraylist.h"
23 #include "batchrender.h"
24 #include "bcsignals.h"
29 #include "filesystem.h"
32 #include "loadfile.inc"
36 #include "mwindowgui.h"
37 #include "pluginserver.h"
38 #include "preferences.h"
39 #include "renderfarmclient.h"
41 #include "versioninfo.h"
47 #include <sys/resource.h>
50 #if 0 // track allocators
52 #define BT_BUF_SIZE 100
55 void *buffer[BT_BUF_SIZE];
56 int nptrs = backtrace(buffer, BT_BUF_SIZE);
57 char **trace = backtrace_symbols(buffer, nptrs);
59 for( int i=0; i<nptrs; ) printf("%s ", trace[i++]);
63 #define STRB ;leaker()
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); }
90 class CommandLineThread : public Thread
93 CommandLineThread(ArrayList<char*> *filenames,
96 this->filenames = filenames;
97 this->mwindow = mwindow;
108 mwindow->gui->lock_window("main");
110 mwindow->load_filenames(filenames,
111 LOADMODE_REPLACE, LOADMODE_EDL_CLIP);
113 if( filenames->size() == 1 )
114 mwindow->gui->mainmenu->add_load(filenames->get(0));
116 mwindow->gui->unlock_window();
121 ArrayList<char*> *filenames;
126 static float get_layout_scale()
128 char config_path[BCTEXTLEN];
129 sprintf(config_path,"%s/%s", File::get_config_path(), CONFIG_FILE);
130 FILE *fp = fopen(config_path,"r");
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);
145 /*******************************************************************
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.
153 ********************************************************************/
154 int main(int argc, char *argv[])
156 // handle command line arguments first
158 ArrayList<char*> filenames;
159 filenames.set_array_delete();
162 time_t st; time(&st);
164 localtime_r(&st, <m);
166 int tzofs = ltm.tm_hour - gtm.tm_hour;
167 cin_timezone = tzofs * 60*60;
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];
175 int load_perpetual = 1;
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();
186 bindtextdomain(cin, locale_path);
188 setlocale(LC_MESSAGES, "");
190 #ifdef X_HAVE_UTF8_STRING
191 char *loc = setlocale(LC_CTYPE, "");
193 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
194 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
196 // Extract from locale language & region
199 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
200 strncpy(locbuf, loc, p - loc);
203 else if( strlen(loc) < sizeof(locbuf)-1 )
206 // Locale 'C' does not give useful language info - assume en
207 if( !locbuf[0] || locbuf[0] == 'C' )
208 strcpy(locbuf, "en");
210 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
212 strcpy(BC_Resources::language, locbuf);
213 if( strlen(p) < LEN_LANG )
214 strcpy(BC_Resources::region, p);
216 else if( strlen(locbuf) < LEN_LANG )
217 strcpy(BC_Resources::language, locbuf);
220 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
222 setlocale(LC_CTYPE, "");
227 int start_remote_control = 0;
229 for( int i = 1; i < argc; i++ ) {
230 if( !strcmp(argv[i], "-h") ) {
231 operation = DO_USAGE;
233 else if( !strcmp(argv[i], "-z") ) {
234 start_remote_control = 1;
236 else if( !strcmp(argv[i], "-r") ) {
237 operation = DO_BATCHRENDER;
239 if( argv[i + 1][0] != '-' ) {
240 strcpy(batch_path, argv[i + 1]);
245 else if( !strcmp(argv[i], "-c") ) {
247 strcpy(config_path, argv[i + 1]);
251 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
254 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
255 operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
257 if( atol(argv[i + 1]) > 0 ) {
258 deamon_port = atol(argv[i + 1]);
263 else if( !strcmp(argv[i], "-b") ) {
264 operation = DO_BRENDER;
266 fprintf(stderr, _("-b may not be used by the user.\n"));
270 strcpy(deamon_path, argv[i + 1]);
272 else if( !strcmp(argv[i], "-n") ) {
274 nice_value = atol(argv[i + 1]);
278 else if( !strcmp(argv[i], "-x") ) {
281 else if( !strcmp(argv[i], "-S") ) {
286 new_filename = new char[BCTEXTLEN];
287 strcpy(new_filename, argv[i]);
288 fs.complete_path(new_filename);
289 filenames.append(new_filename);
293 float scale = operation == DO_GUI ?
294 get_layout_scale() : 1;
296 BC_WindowBase::init_resources(scale);
298 if( operation == DO_GUI ||
299 operation == DO_DEAMON || operation == DO_DEAMON_FG ||
300 operation == DO_USAGE || operation == DO_BATCHRENDER) {
303 #define REPOMAINTXT ""
305 #ifndef COPYRIGHTTEXT1
306 #define COPYRIGHTTEXT1 ""
308 #ifndef COPYRIGHTTEXT2
309 #define COPYRIGHTTEXT2 ""
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);
320 switch( operation ) {
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"));
339 if( operation == DO_DEAMON ) {
343 // Redhat 9 requires _exit instead of exit here.
348 RenderFarmClient client(deamon_port,
355 // Same thing without detachment
357 RenderFarmClient client(0,
364 case DO_BATCHRENDER: {
365 BatchRenderThread *thread = new BatchRenderThread(0);
366 thread->start_rendering(config_path,
371 int restart = 0, done = 0;
373 BC_WindowBase::get_resources()->vframe_shm = 0;
375 mwindow.create_objects(1, !filenames.total, config_path);
376 CommandLineThread *thread = 0;
378 mwindow.load_undo_data();
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);
389 mwindow.gui->lock_window("main");
390 mwindow.load_backup();
391 mwindow.gui->unlock_window();
393 if( start_remote_control ) {
394 start_remote_control = 0;
395 mwindow.gui->remote_control->activate();
402 restart = mwindow.restart();
404 mwindow.save_backup();
406 start_remote_control =
407 mwindow.gui->remote_control->is_active();
411 mwindow.save_defaults();
412 mwindow.save_undo_data();
414 filenames.remove_all_objects();
419 if( lang ) // reset to cmdline state
420 setenv("LANGUAGE", lang, 1);
422 unsetenv("LANGUAGE");
423 char exe_path[BCTEXTLEN];
424 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
427 char *av[4] = { 0, }; int ac = 0;
429 if( load_backup ) av[ac++] = (char*) "-x";
430 if( start_remote_control ) av[ac++] = (char*) "-z";
439 filenames.remove_all_objects();
442 BC_WindowBase::finit_resources();
444 time_t et; time(&et);
446 printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
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);