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"
28 #include "filesystem.h"
31 #include "loadfile.inc"
35 #include "mwindowgui.h"
36 #include "pluginserver.h"
37 #include "preferences.h"
38 #include "renderfarmclient.h"
40 #include "versioninfo.h"
46 #include <sys/resource.h>
49 #if 0 // track allocators
51 #define BT_BUF_SIZE 100
54 void *buffer[BT_BUF_SIZE];
55 int nptrs = backtrace(buffer, BT_BUF_SIZE);
56 char **trace = backtrace_symbols(buffer, nptrs);
58 for( int i=0; i<nptrs; ) printf("%s ", trace[i++]);
62 #define STRB ;leaker()
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); }
89 class CommandLineThread : public Thread
92 CommandLineThread(ArrayList<char*> *filenames,
95 this->filenames = filenames;
96 this->mwindow = mwindow;
107 mwindow->gui->lock_window("main");
109 mwindow->load_filenames(filenames, LOADMODE_REPLACE);
111 if( filenames->size() == 1 )
112 mwindow->gui->mainmenu->add_load(filenames->get(0));
114 mwindow->gui->unlock_window();
119 ArrayList<char*> *filenames;
124 int main(int argc, char *argv[])
126 // handle command line arguments first
128 ArrayList<char*> filenames;
131 time_t st; time(&st);
133 localtime_r(&st, <m);
135 int tzofs = ltm.tm_hour - gtm.tm_hour;
136 cin_timezone = tzofs * 60*60;
138 int operation = DO_GUI;
139 int deamon_port = DEAMON_PORT;
140 char deamon_path[BCTEXTLEN];
141 char config_path[BCTEXTLEN];
142 char batch_path[BCTEXTLEN];
144 int load_perpetual = 1;
150 File::init_cin_path();
151 const char *locale_path = File::get_locale_path();
152 const char *cin = File::get_cin();
154 bindtextdomain(cin, locale_path);
156 setlocale(LC_MESSAGES, "");
158 #ifdef X_HAVE_UTF8_STRING
159 char *loc = setlocale(LC_CTYPE, "");
161 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
162 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
164 // Extract from locale language & region
167 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
168 strncpy(locbuf, loc, p - loc);
171 else if( strlen(loc) < sizeof(locbuf)-1 )
174 // Locale 'C' does not give useful language info - assume en
175 if( !locbuf[0] || locbuf[0] == 'C' )
176 strcpy(locbuf, "en");
178 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
180 strcpy(BC_Resources::language, locbuf);
181 if( strlen(p) < LEN_LANG )
182 strcpy(BC_Resources::region, p);
184 else if( strlen(locbuf) < LEN_LANG )
185 strcpy(BC_Resources::language, locbuf);
188 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
190 setlocale(LC_CTYPE, "");
195 int start_remote_control = 0;
197 for( int i = 1; i < argc; i++ ) {
198 if( !strcmp(argv[i], "-h") ) {
199 operation = DO_USAGE;
201 else if( !strcmp(argv[i], "-z") ) {
202 start_remote_control = 1;
204 else if( !strcmp(argv[i], "-r") ) {
205 operation = DO_BATCHRENDER;
207 if( argv[i + 1][0] != '-' ) {
208 strcpy(batch_path, argv[i + 1]);
213 else if( !strcmp(argv[i], "-c") ) {
215 strcpy(config_path, argv[i + 1]);
219 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
222 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
223 operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
225 if( atol(argv[i + 1]) > 0 ) {
226 deamon_port = atol(argv[i + 1]);
231 else if( !strcmp(argv[i], "-b") ) {
232 operation = DO_BRENDER;
234 fprintf(stderr, _("-b may not be used by the user.\n"));
238 strcpy(deamon_path, argv[i + 1]);
240 else if( !strcmp(argv[i], "-n") ) {
242 nice_value = atol(argv[i + 1]);
246 else if( !strcmp(argv[i], "-x") ) {
249 else if( !strcmp(argv[i], "-S") ) {
254 new_filename = new char[BCTEXTLEN];
255 strcpy(new_filename, argv[i]);
256 fs.complete_path(new_filename);
257 filenames.append(new_filename);
263 if( operation == DO_GUI ||
264 operation == DO_DEAMON || operation == DO_DEAMON_FG ||
265 operation == DO_USAGE || operation == DO_BATCHRENDER) {
268 #define REPOMAINTXT ""
270 #ifndef COPYRIGHTTEXT1
271 #define COPYRIGHTTEXT1 ""
273 #ifndef COPYRIGHTTEXT2
274 #define COPYRIGHTTEXT2 ""
276 fprintf(stderr, "%s %s - %s\n%s",
277 PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
278 REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
279 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
280 "and you are welcome to change it and/or distribute copies of it under\n"
281 "certain conditions. There is absolutely no warranty for %s.\n\n",
282 PROGRAM_NAME, PROGRAM_NAME);
285 switch( operation ) {
287 printf(_("\nUsage:\n"));
288 printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
289 printf(_("-d = Run in the background as renderfarm client. The port (400) is optional.\n"));
290 printf(_("-f = Run in the foreground as renderfarm client. Substitute for -d.\n"));
291 printf(_("-n = Nice value if running as renderfarm client. (19)\n"));
292 printf(_("-c = Configuration file to use instead of %s/%s.\n"),
293 File::get_config_path(), CONFIG_FILE);
294 printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI. batch file is optional.\n"),
295 File::get_config_path(), BATCH_PATH);
296 printf(_("-S = do not reload perpetual session\n"));
297 printf(_("-x = reload from backup\n"));
298 printf(_("filenames = files to load\n\n\n"));
304 if( operation == DO_DEAMON ) {
308 // Redhat 9 requires _exit instead of exit here.
313 RenderFarmClient client(deamon_port,
320 // Same thing without detachment
322 RenderFarmClient client(0,
329 case DO_BATCHRENDER: {
330 BatchRenderThread *thread = new BatchRenderThread(0);
331 thread->start_rendering(config_path,
336 int restart = 0, done = 0;
338 BC_WindowBase::get_resources()->vframe_shm = 0;
340 mwindow.create_objects(1, !filenames.total, config_path);
341 CommandLineThread *thread = 0;
342 if( mwindow.preferences->perpetual_session && load_perpetual )
343 mwindow.load_undo_data();
345 // load the initial files on seperate tracks
346 // use a new thread so it doesn't block the GUI
347 if( filenames.total ) {
348 thread = new CommandLineThread(&filenames, &mwindow);
354 mwindow.gui->lock_window("main");
355 mwindow.load_backup();
356 mwindow.gui->unlock_window();
358 if( start_remote_control ) {
359 start_remote_control = 0;
360 mwindow.gui->remote_control->activate();
367 restart = mwindow.restart();
369 mwindow.save_backup();
371 start_remote_control =
372 mwindow.gui->remote_control->is_active();
377 mwindow.save_defaults();
378 if( mwindow.preferences->perpetual_session )
379 mwindow.save_undo_data();
381 filenames.remove_all_objects();
386 char exe_path[BCTEXTLEN];
387 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
390 char *av[4] = { 0, }; int ac = 0;
392 if( load_backup ) av[ac++] = (char*) "-x";
393 if( start_remote_control ) av[ac++] = (char*) "-z";
402 filenames.remove_all_objects();
405 time_t et; time(&et);
407 printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);
409 getrusage(RUSAGE_SELF, &ru);
410 long usr_ms = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
411 long us = usr_ms/1000; int ums = usr_ms%1000;
412 long sys_ms = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
413 long ss = sys_ms/1000; int sms = sys_ms%1000;
414 printf("Cpu time: user: %ld:%02ld:%02ld.%03d sys: %ld:%02ld:%02ld.%03d\n",
415 us/3600, us%3600/60, us%60, ums, ss/3600, ss%3600/60, ss%60, sms);