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 int main(int argc, char *argv[])
147 // handle command line arguments first
149 ArrayList<char*> filenames;
150 filenames.set_array_delete();
153 time_t st; time(&st);
155 localtime_r(&st, <m);
157 int tzofs = ltm.tm_hour - gtm.tm_hour;
158 cin_timezone = tzofs * 60*60;
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];
166 int load_perpetual = 1;
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();
177 bindtextdomain(cin, locale_path);
179 setlocale(LC_MESSAGES, "");
181 #ifdef X_HAVE_UTF8_STRING
182 char *loc = setlocale(LC_CTYPE, "");
184 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
185 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
187 // Extract from locale language & region
190 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
191 strncpy(locbuf, loc, p - loc);
194 else if( strlen(loc) < sizeof(locbuf)-1 )
197 // Locale 'C' does not give useful language info - assume en
198 if( !locbuf[0] || locbuf[0] == 'C' )
199 strcpy(locbuf, "en");
201 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
203 strcpy(BC_Resources::language, locbuf);
204 if( strlen(p) < LEN_LANG )
205 strcpy(BC_Resources::region, p);
207 else if( strlen(locbuf) < LEN_LANG )
208 strcpy(BC_Resources::language, locbuf);
211 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
213 setlocale(LC_CTYPE, "");
218 int start_remote_control = 0;
220 for( int i = 1; i < argc; i++ ) {
221 if( !strcmp(argv[i], "-h") ) {
222 operation = DO_USAGE;
224 else if( !strcmp(argv[i], "-z") ) {
225 start_remote_control = 1;
227 else if( !strcmp(argv[i], "-r") ) {
228 operation = DO_BATCHRENDER;
230 if( argv[i + 1][0] != '-' ) {
231 strcpy(batch_path, argv[i + 1]);
236 else if( !strcmp(argv[i], "-c") ) {
238 strcpy(config_path, argv[i + 1]);
242 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
245 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
246 operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
248 if( atol(argv[i + 1]) > 0 ) {
249 deamon_port = atol(argv[i + 1]);
254 else if( !strcmp(argv[i], "-b") ) {
255 operation = DO_BRENDER;
257 fprintf(stderr, _("-b may not be used by the user.\n"));
261 strcpy(deamon_path, argv[i + 1]);
263 else if( !strcmp(argv[i], "-n") ) {
265 nice_value = atol(argv[i + 1]);
269 else if( !strcmp(argv[i], "-x") ) {
272 else if( !strcmp(argv[i], "-S") ) {
277 new_filename = new char[BCTEXTLEN];
278 strcpy(new_filename, argv[i]);
279 fs.complete_path(new_filename);
280 filenames.append(new_filename);
284 float scale = operation == DO_GUI ?
285 get_layout_scale() : 1;
287 BC_WindowBase::init_resources(scale);
289 if( operation == DO_GUI ||
290 operation == DO_DEAMON || operation == DO_DEAMON_FG ||
291 operation == DO_USAGE || operation == DO_BATCHRENDER) {
294 #define REPOMAINTXT ""
296 #ifndef COPYRIGHTTEXT1
297 #define COPYRIGHTTEXT1 ""
299 #ifndef COPYRIGHTTEXT2
300 #define COPYRIGHTTEXT2 ""
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);
311 switch( operation ) {
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"));
330 if( operation == DO_DEAMON ) {
334 // Redhat 9 requires _exit instead of exit here.
339 RenderFarmClient client(deamon_port,
346 // Same thing without detachment
348 RenderFarmClient client(0,
355 case DO_BATCHRENDER: {
356 BatchRenderThread *thread = new BatchRenderThread(0);
357 thread->start_rendering(config_path,
362 int restart = 0, done = 0;
364 BC_WindowBase::get_resources()->vframe_shm = 0;
366 mwindow.create_objects(1, !filenames.total, config_path);
367 CommandLineThread *thread = 0;
369 mwindow.load_undo_data();
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);
380 mwindow.gui->lock_window("main");
381 mwindow.load_backup();
382 mwindow.gui->unlock_window();
384 if( start_remote_control ) {
385 start_remote_control = 0;
386 mwindow.gui->remote_control->activate();
393 restart = mwindow.restart();
395 mwindow.save_backup();
397 start_remote_control =
398 mwindow.gui->remote_control->is_active();
402 mwindow.save_defaults();
403 mwindow.save_undo_data();
405 filenames.remove_all_objects();
410 if( lang ) // reset to cmdline state
411 setenv("LANGUAGE", lang, 1);
413 unsetenv("LANGUAGE");
414 char exe_path[BCTEXTLEN];
415 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
418 char *av[4] = { 0, }; int ac = 0;
420 if( load_backup ) av[ac++] = (char*) "-x";
421 if( start_remote_control ) av[ac++] = (char*) "-z";
430 filenames.remove_all_objects();
433 BC_WindowBase::finit_resources();
435 time_t et; time(&et);
437 printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
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);