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"
47 #define STRC(v) printf("==new %p from %p sz %jd\n", v, __builtin_return_address(0), n)
48 #define STRD(v) printf("==del %p from %p\n", v, __builtin_return_address(0))
49 void *operator new(size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
50 void operator delete(void *t) { STRD(t); free(t); }
51 void operator delete(void *t,size_t n) { STRD(t); free(t); }
52 void *operator new[](size_t n) { void *vp = malloc(n); STRC(vp); bzero(vp,n); return vp; }
53 void operator delete[](void *t) { STRD(t); free(t); }
54 void operator delete[](void *t,size_t n) { STRD(t); free(t); }
70 class CommandLineThread : public Thread
73 CommandLineThread(ArrayList<char*> *filenames,
76 this->filenames = filenames;
77 this->mwindow = mwindow;
88 mwindow->gui->lock_window("main");
90 mwindow->load_filenames(filenames, LOADMODE_REPLACE);
92 if( filenames->size() == 1 )
93 mwindow->gui->mainmenu->add_load(filenames->get(0));
95 mwindow->gui->unlock_window();
100 ArrayList<char*> *filenames;
105 int main(int argc, char *argv[])
107 // handle command line arguments first
109 ArrayList<char*> filenames;
112 time_t st; time(&st);
114 localtime_r(&st, <m);
116 int tzofs = ltm.tm_hour - gtm.tm_hour;
117 cin_timezone = tzofs * 60*60;
119 int operation = DO_GUI;
120 int deamon_port = DEAMON_PORT;
121 char deamon_path[BCTEXTLEN];
122 char config_path[BCTEXTLEN];
123 char batch_path[BCTEXTLEN];
125 int load_perpetual = 1;
131 File::init_cin_path();
132 const char *locale_path = File::get_locale_path();
133 const char *cin = File::get_cin();
135 bindtextdomain(cin, locale_path);
137 setlocale(LC_MESSAGES, "");
139 #ifdef X_HAVE_UTF8_STRING
140 char *loc = setlocale(LC_CTYPE, "");
142 strcpy(BC_Resources::encoding, nl_langinfo(CODESET));
143 BC_Resources::locale_utf8 = !strcmp(BC_Resources::encoding, "UTF-8");
145 // Extract from locale language & region
148 if( (p = strchr(loc, '.')) != 0 && (p - loc) < (int)sizeof(locbuf)-1 ) {
149 strncpy(locbuf, loc, p - loc);
152 else if( strlen(loc) < sizeof(locbuf)-1 )
155 // Locale 'C' does not give useful language info - assume en
156 if( !locbuf[0] || locbuf[0] == 'C' )
157 strcpy(locbuf, "en");
159 if( (p = strchr(locbuf, '_')) && p - locbuf < LEN_LANG ) {
161 strcpy(BC_Resources::language, locbuf);
162 if( strlen(p) < LEN_LANG )
163 strcpy(BC_Resources::region, p);
165 else if( strlen(locbuf) < LEN_LANG )
166 strcpy(BC_Resources::language, locbuf);
169 printf(_(PROGRAM_NAME ": Could not set locale.\n"));
171 setlocale(LC_CTYPE, "");
176 int start_remote_control = 0;
178 for( int i = 1; i < argc; i++ ) {
179 if( !strcmp(argv[i], "-h") ) {
180 operation = DO_USAGE;
182 else if( !strcmp(argv[i], "-z") ) {
183 start_remote_control = 1;
185 else if( !strcmp(argv[i], "-r") ) {
186 operation = DO_BATCHRENDER;
188 if( argv[i + 1][0] != '-' ) {
189 strcpy(batch_path, argv[i + 1]);
194 else if( !strcmp(argv[i], "-c") ) {
196 strcpy(config_path, argv[i + 1]);
200 fprintf(stderr, _("%s: -c needs a filename.\n"), argv[0]);
203 else if( !strcmp(argv[i], "-d") || !strcmp(argv[i], "-f") ) {
204 operation = !strcmp(argv[i], "-d") ? DO_DEAMON : DO_DEAMON_FG;
206 if( atol(argv[i + 1]) > 0 ) {
207 deamon_port = atol(argv[i + 1]);
212 else if( !strcmp(argv[i], "-b") ) {
213 operation = DO_BRENDER;
215 fprintf(stderr, _("-b may not be used by the user.\n"));
219 strcpy(deamon_path, argv[i + 1]);
221 else if( !strcmp(argv[i], "-n") ) {
223 nice_value = atol(argv[i + 1]);
227 else if( !strcmp(argv[i], "-x") ) {
230 else if( !strcmp(argv[i], "-S") ) {
235 new_filename = new char[BCTEXTLEN];
236 strcpy(new_filename, argv[i]);
237 fs.complete_path(new_filename);
238 filenames.append(new_filename);
244 if( operation == DO_GUI ||
245 operation == DO_DEAMON || operation == DO_DEAMON_FG ||
246 operation == DO_USAGE || operation == DO_BATCHRENDER) {
249 #define REPOMAINTXT ""
251 #ifndef COPYRIGHTTEXT1
252 #define COPYRIGHTTEXT1 ""
254 #ifndef COPYRIGHTTEXT2
255 #define COPYRIGHTTEXT2 ""
257 fprintf(stderr, "%s %s - %s\n%s",
258 PROGRAM_NAME,CINELERRA_VERSION, AboutPrefs::build_timestamp,
259 REPOMAINTXT COPYRIGHTTEXT1 COPYRIGHTTEXT2);
260 fprintf(stderr, "%s is free software, covered by the GNU General Public License,\n"
261 "and you are welcome to change it and/or distribute copies of it under\n"
262 "certain conditions. There is absolutely no warranty for %s.\n\n",
263 PROGRAM_NAME, PROGRAM_NAME);
266 switch( operation ) {
268 printf(_("\nUsage:\n"));
269 printf(_("%s [-f] [-c configuration] [-d port] [-n nice] [-r batch file] [filenames]\n\n"), argv[0]);
270 printf(_("-d = Run in the background as renderfarm client. The port (400) is optional.\n"));
271 printf(_("-f = Run in the foreground as renderfarm client. Substitute for -d.\n"));
272 printf(_("-n = Nice value if running as renderfarm client. (19)\n"));
273 printf(_("-c = Configuration file to use instead of %s/%s.\n"),
274 File::get_config_path(), CONFIG_FILE);
275 printf(_("-r = batch render the contents of the batch file (%s/%s) with no GUI. batch file is optional.\n"),
276 File::get_config_path(), BATCH_PATH);
277 printf(_("-S = do not reload perpetual session\n"));
278 printf(_("-x = reload from backup\n"));
279 printf(_("filenames = files to load\n\n\n"));
285 if( operation == DO_DEAMON ) {
289 // Redhat 9 requires _exit instead of exit here.
294 RenderFarmClient client(deamon_port,
301 // Same thing without detachment
303 RenderFarmClient client(0,
310 case DO_BATCHRENDER: {
311 BatchRenderThread *thread = new BatchRenderThread;
312 thread->start_rendering(config_path,
317 int restart = 0, done = 0;
319 BC_WindowBase::get_resources()->vframe_shm = 0;
321 mwindow.create_objects(1, !filenames.total, config_path);
322 CommandLineThread *thread = 0;
323 if( mwindow.preferences->perpetual_session && load_perpetual )
324 mwindow.load_undo_data();
326 // load the initial files on seperate tracks
327 // use a new thread so it doesn't block the GUI
328 if( filenames.total ) {
329 thread = new CommandLineThread(&filenames, &mwindow);
335 mwindow.gui->lock_window("main");
336 mwindow.load_backup();
337 mwindow.gui->unlock_window();
339 if( start_remote_control ) {
340 start_remote_control = 0;
341 mwindow.gui->remote_control->activate();
348 restart = mwindow.restart();
350 mwindow.save_backup();
352 start_remote_control =
353 mwindow.gui->remote_control->is_active();
358 mwindow.save_defaults();
359 if( mwindow.preferences->perpetual_session )
360 mwindow.save_undo_data();
362 filenames.remove_all_objects();
367 char exe_path[BCTEXTLEN];
368 int len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)-1);
371 char *av[4] = { 0, }; int ac = 0;
373 if( load_backup ) av[ac++] = (char*) "-x";
374 if( start_remote_control ) av[ac++] = (char*) "-z";
383 filenames.remove_all_objects();
386 time_t et; time(&et);
388 printf("Session time: %ld:%02ld:%02ld\n", dt/3600, dt%3600/60, dt%60);