d67b6ad1a5342b921c61ad54476768c208dda304
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / batchrender.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "batchrender.h"
24 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
26 #include "confirmsave.h"
27 #include "cstrdup.h"
28 #include "bchash.h"
29 #include "edits.h"
30 #include "edit.h"
31 #include "edl.h"
32 #include "edlsession.h"
33 #include "errorbox.h"
34 #include "file.h"
35 #include "filesystem.h"
36 #include "filexml.h"
37 #include "keyframe.h"
38 #include "keys.h"
39 #include "labels.h"
40 #include "language.h"
41 #include "mainerror.h"
42 #include "mainundo.h"
43 #include "mainsession.h"
44 #include "mutex.h"
45 #include "mwindow.h"
46 #include "mwindowgui.h"
47 #include "packagedispatcher.h"
48 #include "packagerenderer.h"
49 #include "plugin.h"
50 #include "pluginset.h"
51 #include "preferences.h"
52 #include "render.h"
53 #include "theme.h"
54 #include "tracks.h"
55 #include "transportque.h"
56 #include "vframe.h"
57
58 // Farmed is not present if not preferences->use_renderfarm
59 int BatchRenderThread::column_widths[] = { 42, 42, 42, 222, 222, 150 };
60 const char *BatchRenderThread::column_titles[] = {
61         N_("Enabled"), N_("Labeled"), N_("Farmed"), N_("Output"), N_("EDL"), N_("Elapsed")
62 };
63
64 BatchRenderMenuItem::BatchRenderMenuItem(MWindow *mwindow)
65  : BC_MenuItem(_("Batch Render..."), _("Shift-B"), 'B')
66 {
67         set_shift(1);
68         this->mwindow = mwindow;
69 }
70
71 int BatchRenderMenuItem::handle_event()
72 {
73         mwindow->batch_render->start();
74         return 1;
75 }
76
77
78 BatchRenderJob::BatchRenderJob(Preferences *preferences, int labeled, int farmed)
79 {
80         this->preferences = preferences;
81         this->labeled = labeled;
82         this->farmed = farmed >= 0 ? farmed : preferences->use_renderfarm;
83         asset = new Asset;
84         edl_path[0] = 0;
85         enabled = 1;
86         elapsed = 0;
87 }
88
89 BatchRenderJob::~BatchRenderJob()
90 {
91         asset->Garbage::remove_user();
92 }
93
94 void BatchRenderJob::copy_from(BatchRenderJob *src)
95 {
96         enabled = src->enabled;
97         farmed = src->farmed;
98         labeled = src->labeled;
99         asset->copy_from(src->asset, 0);
100         strcpy(edl_path, src->edl_path);
101         elapsed = 0;
102 }
103
104 void BatchRenderJob::load(FileXML *file)
105 {
106         int result = 0;
107
108         enabled = file->tag.get_property("ENABLED", enabled);
109         farmed = file->tag.get_property("FARMED", farmed);
110         labeled = file->tag.get_property("STRATEGY", labeled);
111         edl_path[0] = 0;
112         file->tag.get_property("EDL_PATH", edl_path);
113         elapsed = file->tag.get_property("ELAPSED", elapsed);
114
115         result = file->read_tag();
116         if( !result ) {
117                 if( file->tag.title_is("ASSET") ) {
118                         file->tag.get_property("SRC", asset->path);
119                         asset->read(file, 0);
120 // The compression parameters are stored in the defaults to reduce
121 // coding maintenance.  The defaults must now be stuffed into the XML for
122 // unique storage.
123                         BC_Hash defaults;
124                         defaults.load_string(file->read_text());
125                         asset->load_defaults(&defaults,
126                                 "", 0, 1, 0, 0, 0);
127                 }
128         }
129 }
130
131 void BatchRenderJob::save(FileXML *file)
132 {
133         file->tag.set_property("ENABLED", enabled);
134         file->tag.set_property("FARMED", farmed);
135         file->tag.set_property("LABELED", labeled);
136         file->tag.set_property("EDL_PATH", edl_path);
137         file->tag.set_property("ELAPSED", elapsed);
138         file->append_tag();
139         file->append_newline();
140         asset->write(file, 0, "");
141
142 // The compression parameters are stored in the defaults to reduce
143 // coding maintenance.  The defaults must now be stuffed into the XML for
144 // unique storage.
145         BC_Hash defaults;
146         asset->save_defaults(&defaults, "", 0, 1, 0, 0, 0);
147         char *string;
148         defaults.save_string(string);
149         file->append_text(string);
150         free(string);
151         file->tag.set_title("/JOB");
152         file->append_tag();
153         file->append_newline();
154 }
155
156 int BatchRenderJob::get_strategy()
157 {
158 // if set, overrides farmed, labeled
159         int use_renderfarm = farmed && preferences->use_renderfarm ? 1 : 0;
160         return Render::get_strategy(use_renderfarm, labeled);
161 }
162
163
164 BatchRenderThread::BatchRenderThread(MWindow *mwindow)
165  : BC_DialogThread()
166 {
167         this->mwindow = mwindow;
168         current_job = 0;
169         rendering_job = -1;
170         is_rendering = 0;
171         default_job = 0;
172         boot_defaults = 0;
173         preferences = 0;
174         warn = 1;
175         render = 0;
176         batch_path[0] = 0;
177 }
178
179 BatchRenderThread::BatchRenderThread()
180  : BC_DialogThread()
181 {
182         mwindow = 0;
183         current_job = 0;
184         rendering_job = -1;
185         is_rendering = 0;
186         default_job = 0;
187         boot_defaults = 0;
188         preferences = 0;
189         warn = 1;
190         render = 0;
191         batch_path[0] = 0;
192 }
193
194 BatchRenderThread::~BatchRenderThread()
195 {
196         close_window();
197         delete boot_defaults;
198         delete preferences;
199         delete render;
200 }
201
202 void BatchRenderThread::reset(const char *path)
203 {
204         if( path ) {
205                 strcpy(batch_path, path);
206                 warn = 1;
207         }
208         current_job = 0;
209         rendering_job = -1;
210         delete default_job;  default_job = 0;
211         jobs.remove_all_objects();
212 }
213
214 void BatchRenderThread::handle_close_event(int result)
215 {
216 // Save settings
217         save_jobs(batch_path);
218         save_defaults(mwindow->defaults);
219         reset();
220 }
221
222 BC_Window* BatchRenderThread::new_gui()
223 {
224         current_start = 0.0;
225         current_end = 0.0;
226         default_job = new BatchRenderJob(mwindow->preferences);
227         load_jobs(batch_path, mwindow->preferences);
228         load_defaults(mwindow->defaults);
229         this->gui = new BatchRenderGUI(mwindow, this,
230                 mwindow->session->batchrender_x, mwindow->session->batchrender_y,
231                 mwindow->session->batchrender_w, mwindow->session->batchrender_h);
232         this->gui->create_objects();
233         return this->gui;
234 }
235
236
237 void BatchRenderThread::load_jobs(char *path, Preferences *preferences)
238 {
239         FileXML file;
240         int result = 0;
241
242         jobs.remove_all_objects();
243         if( !path ) path = batch_path;
244         if( !path[0] ) create_path(path);
245         file.read_from_file(path);
246
247         while( !result ) {
248                 if( !(result = file.read_tag()) ) {
249                         if( file.tag.title_is("JOBS") ) {
250                                 warn = file.tag.get_property("WARN", 1);
251                         }
252                         else if( file.tag.title_is("JOB") ) {
253                                 BatchRenderJob *job =  new BatchRenderJob(preferences);
254                                 jobs.append(job);
255                                 job->load(&file);
256                         }
257                 }
258         }
259 }
260
261 void BatchRenderThread::save_jobs(char *path)
262 {
263         FileXML file;
264         file.tag.set_title("JOBS");
265         file.tag.set_property("WARN", warn);
266         file.append_tag();
267         file.append_newline();
268
269         for( int i = 0; i < jobs.total; i++ ) {
270                 file.tag.set_title("JOB");
271                 jobs.values[i]->save(&file);
272         }
273         file.tag.set_title("/JOBS");
274         file.append_tag();
275         file.append_newline();
276
277         if( !path ) path = batch_path;
278         if( !path[0] ) create_path(path);
279         file.write_to_file(path);
280 }
281
282 void BatchRenderThread::load_defaults(BC_Hash *defaults)
283 {
284         if( default_job ) {
285                 default_job->asset->load_defaults(defaults,
286                         "BATCHRENDER_", 1, 1, 1, 1, 1);
287         }
288
289         for( int i = 0; i < BATCHRENDER_COLUMNS; i++ ) {
290                 char string[BCTEXTLEN];
291                 sprintf(string, "BATCHRENDER_COLUMN%d", i);
292                 list_width[i] = defaults->get(string, column_widths[i]);
293         }
294 }
295
296 void BatchRenderThread::save_defaults(BC_Hash *defaults)
297 {
298         if( default_job ) {
299                 default_job->asset->save_defaults(defaults,
300                         "BATCHRENDER_", 1, 1, 1, 1, 1);
301         }
302         for( int i=0; i<BATCHRENDER_COLUMNS; ++i ) {
303                 char string[BCTEXTLEN];
304                 sprintf(string, "BATCHRENDER_COLUMN%d", i);
305                 defaults->update(string, list_width[i]);
306         }
307 //      defaults->update("BATCHRENDER_JOB", current_job);
308         if( mwindow )
309                 mwindow->save_defaults();
310         else
311                 defaults->save();
312 }
313
314 char* BatchRenderThread::create_path(char *string)
315 {
316         FileSystem fs;
317         sprintf(string, "%s/", File::get_config_path());
318         fs.complete_path(string);
319         strcat(string, BATCH_PATH);
320         return string;
321 }
322
323 void BatchRenderThread::new_job()
324 {
325         BatchRenderJob *result = new BatchRenderJob(mwindow->preferences);
326         result->copy_from(get_current_job());
327         jobs.append(result);
328         current_job = jobs.total - 1;
329         gui->create_list(1);
330         gui->change_job();
331 }
332
333 void BatchRenderThread::delete_job()
334 {
335         if( current_job < jobs.total && current_job >= 0 ) {
336                 jobs.remove_object_number(current_job);
337                 if( current_job > 0 ) current_job--;
338                 gui->create_list(1);
339                 gui->change_job();
340         }
341 }
342
343 void BatchRenderThread::use_current_edl()
344 {
345 // printf("BatchRenderThread::use_current_edl %d %p %s\n",
346 // __LINE__,
347 // mwindow->edl->path,
348 // mwindow->edl->path);
349
350         strcpy(get_current_edl(), mwindow->edl->path);
351         gui->create_list(1);
352         gui->edl_path_text->update(get_current_edl());
353 }
354
355 void BatchRenderThread::update_selected_edl()
356 {
357         FileXML xml_file;
358         char *path = get_current_edl();
359         EDL *edl = mwindow->edl;
360         edl->save_xml(&xml_file, path);
361         xml_file.terminate_string();
362         if( xml_file.write_to_file(path) ) {
363                 char msg[BCTEXTLEN];
364                 sprintf(msg, _("Unable to save: %s"), path);
365                 MainError::show_error(msg);
366         }
367 }
368
369 BatchRenderJob* BatchRenderThread::get_current_job()
370 {
371         return current_job >= 0 && current_job < jobs.total ?
372                 jobs.values[current_job] : default_job;
373 }
374
375
376 Asset* BatchRenderThread::get_current_asset()
377 {
378         return get_current_job()->asset;
379 }
380
381 char* BatchRenderThread::get_current_edl()
382 {
383         return get_current_job()->edl_path;
384 }
385
386
387 // Test EDL files for existence
388 int BatchRenderThread::test_edl_files()
389 {
390         int not_equiv = 0, ret = 0;
391         const char *path = 0;
392
393         for( int i=0; !ret && i<jobs.size(); ++i ) {
394                 if( !jobs.values[i]->enabled ) continue;
395                 path = jobs.values[i]->edl_path;
396                 int is_script = *path == '@' ? 1 : 0;
397                 if( is_script ) ++path;
398                 FILE *fp = fopen(path, "r");
399                 if( fp ) {
400                         if( warn && mwindow && !is_script ) {
401                                 char *bfr = 0;  size_t sz = 0;
402                                 struct stat st;
403                                 if( !fstat(fileno(fp), &st) ) {
404                                         sz = st.st_size;
405                                         bfr = new char[sz+1];
406                                         if( fread(bfr, 1, sz+1, fp) != sz )
407                                                 ret = 1;
408                                         else
409                                                 bfr[sz] = 0;
410                                 }
411                                 if( !ret ) {
412                                         EDL *edl = new EDL; edl->create_objects();
413                                         XMLBuffer data(bfr, sz, 0);
414                                         { FileXML file;
415                                         file.set_shared_input(&data);
416                                         edl->load_xml(&file, LOAD_ALL); }
417                                         double pos = edl->equivalent_output(mwindow->edl);
418                                         if( pos >= 0 ) ++not_equiv;
419                                         edl->remove_user();
420                                 }
421                                 delete [] bfr;
422                         }
423                         fclose(fp);
424                 }
425                 else
426                         ret = 1;
427         }
428
429         if( ret ) {
430                 char string[BCTEXTLEN];
431                 sprintf(string, _("EDL %s not found.\n"), path);
432                 if( mwindow ) {
433                         ErrorBox error_box(_(PROGRAM_NAME ": Error"),
434                                 mwindow->gui->get_abs_cursor_x(1),
435                                 mwindow->gui->get_abs_cursor_y(1));
436                         error_box.create_objects(string);
437                         error_box.run_window();
438                         gui->button_enable();
439                 }
440                 else {
441                         fprintf(stderr, "%s", string);
442                 }
443                 is_rendering = 0;
444         }
445         else if( warn && mwindow && not_equiv > 0 ) {
446                 fprintf(stderr, _("%d job EDLs do not match session edl\n"), not_equiv);
447                 char string[BCTEXTLEN], *sp = string;
448                 sp += sprintf(sp, _("%d job EDLs do not match session edl\n"),not_equiv);
449                 sp += sprintf(sp, _("press cancel to abandon batch render"));
450                 mwindow->show_warning(&warn, string);
451                 if( mwindow->wait_warning() ) {
452                         gui->button_enable();
453                         is_rendering = 0;
454                         ret = 1;
455                 }
456                 gui->warning->update(warn);
457         }
458
459         return ret;
460 }
461
462 void BatchRenderThread::calculate_dest_paths(ArrayList<char*> *paths,
463         Preferences *preferences)
464 {
465         for( int i = 0; i < jobs.total; i++ ) {
466                 BatchRenderJob *job = jobs.values[i];
467                 if( job->enabled && *job->edl_path != '@' ) {
468                         PackageDispatcher *packages = new PackageDispatcher;
469
470 // Load EDL
471                         TransportCommand *command = new TransportCommand;
472                         FileXML *file = new FileXML;
473                         file->read_from_file(job->edl_path);
474
475 // Use command to calculate range.
476                         command->command = NORMAL_FWD;
477                         command->get_edl()->load_xml(file,
478                                 LOAD_ALL);
479                         command->change_type = CHANGE_ALL;
480                         command->set_playback_range();
481                         command->playback_range_adjust_inout();
482
483 // Create test packages
484                         packages->create_packages(mwindow,
485                                 command->get_edl(),
486                                 preferences,
487                                 job->get_strategy(),
488                                 job->asset,
489                                 command->start_position,
490                                 command->end_position,
491                                 0);
492
493 // Append output paths allocated to total
494                         packages->get_package_paths(paths);
495
496 // Delete package harness
497                         delete packages;
498                         delete command;
499                         delete file;
500                 }
501         }
502 }
503
504
505 void BatchRenderThread::start_rendering(char *config_path,
506         char *batch_path)
507 {
508         BC_Hash *boot_defaults;
509         Preferences *preferences;
510         Render *render;
511         BC_Signals *signals = new BC_Signals;
512         // XXX the above stuff is leaked,
513 //PRINT_TRACE
514 // Initialize stuff which MWindow does.
515         signals->initialize("/tmp/cinelerra_batch%d.dmp");
516         boot_defaults = 0;
517         MWindow::init_defaults(boot_defaults, config_path);
518         load_defaults(boot_defaults);
519         preferences = new Preferences;
520         preferences->load_defaults(boot_defaults);
521         BC_Signals::set_trap_hook(trap_hook, this);
522         BC_Signals::set_catch_segv(preferences->trap_sigsegv);
523         BC_Signals::set_catch_intr(0);
524         if( preferences->trap_sigsegv ) {
525                 BC_Trace::enable_locks();
526         }
527         else {
528                 BC_Trace::disable_locks();
529         }
530
531         MWindow::init_plugins(0, preferences);
532         char font_path[BCTEXTLEN];
533         strcpy(font_path, preferences->plugin_dir);
534         strcat(font_path, "/" FONT_SEARCHPATH);
535         BC_Resources::init_fontconfig(font_path);
536         BC_WindowBase::get_resources()->vframe_shm = 1;
537
538 //PRINT_TRACE
539         strcpy(this->batch_path, batch_path);
540         load_jobs(batch_path, preferences);
541         save_jobs(batch_path);
542         save_defaults(boot_defaults);
543
544 //PRINT_TRACE
545 // Test EDL files for existence
546         if( test_edl_files() ) return;
547
548 //PRINT_TRACE
549
550 // Predict all destination paths
551         ArrayList<char*> paths;
552         paths.set_array_delete();
553         calculate_dest_paths(&paths, preferences);
554
555 //PRINT_TRACE
556         int result = ConfirmSave::test_files(0, &paths);
557         paths.remove_all_objects();
558 // Abort on any existing file because it's so hard to set this up.
559         if( result ) return;
560
561 //PRINT_TRACE
562         render = new Render(0);
563 //PRINT_TRACE
564         render->start_batches(&jobs, boot_defaults, preferences);
565 //PRINT_TRACE
566 }
567
568 void BatchRenderThread::start_rendering()
569 {
570         if( is_rendering ) return;
571         is_rendering = 1;
572
573         save_jobs(batch_path);
574         save_defaults(mwindow->defaults);
575         gui->button_disable();
576
577 // Test EDL files for existence
578         if( test_edl_files() ) return;
579
580 // Predict all destination paths
581         ArrayList<char*> paths;
582         calculate_dest_paths(&paths,
583                 mwindow->preferences);
584
585 // Test destination files for overwrite
586         int result = ConfirmSave::test_files(mwindow, &paths);
587         paths.remove_all_objects();
588
589 // User cancelled
590         if( result ) {
591                 is_rendering = 0;
592                 gui->button_enable();
593                 return;
594         }
595
596         mwindow->render->start_batches(&jobs);
597 }
598
599 void BatchRenderThread::stop_rendering()
600 {
601         if( !is_rendering ) return;
602         mwindow->render->stop_operation();
603         is_rendering = 0;
604 }
605
606 void BatchRenderThread::update_active(int number)
607 {
608         gui->lock_window("BatchRenderThread::update_active");
609         if( number >= 0 ) {
610                 current_job = number;
611                 rendering_job = number;
612         }
613         else {
614                 rendering_job = -1;
615                 is_rendering = 0;
616         }
617         gui->create_list(1);
618         gui->unlock_window();
619 }
620
621 void BatchRenderThread::update_done(int number,
622         int create_list,
623         double elapsed_time)
624 {
625         gui->lock_window("BatchRenderThread::update_done");
626         if( number < 0 ) {
627                 gui->button_enable();
628         }
629         else {
630                 jobs.values[number]->enabled = 0;
631                 jobs.values[number]->elapsed = elapsed_time;
632                 if( create_list ) gui->create_list(1);
633         }
634         gui->unlock_window();
635 }
636
637 void BatchRenderThread::move_batch(int src, int dst)
638 {
639         BatchRenderJob *src_job = jobs.values[src];
640         if( dst < 0 ) dst = jobs.total - 1;
641
642         if( dst != src ) {
643                 for( int i = src; i < jobs.total - 1; i++ )
644                         jobs.values[i] = jobs.values[i + 1];
645 //              if( dst > src ) dst--;
646                 for( int i = jobs.total - 1; i > dst; i-- )
647                         jobs.values[i] = jobs.values[i - 1];
648                 jobs.values[dst] = src_job;
649                 gui->create_list(1);
650         }
651 }
652
653 void BatchRenderThread::trap_hook(FILE *fp, void *vp)
654 {
655         MWindow *mwindow = ((BatchRenderThread *)vp)->mwindow;
656         fprintf(fp, "\nEDL:\n");
657         mwindow->dump_edl(fp);
658         fprintf(fp, "\nUNDO:\n");
659         mwindow->dump_undo(fp);
660         fprintf(fp, "\nEXE:\n");
661         mwindow->dump_exe(fp);
662 }
663
664
665
666
667
668 BatchRenderGUI::BatchRenderGUI(MWindow *mwindow,
669         BatchRenderThread *thread, int x, int y, int w, int h)
670  : BC_Window(_(PROGRAM_NAME ": Batch Render"),
671         x, y, w, h, 730, 400, 1, 0, 1)
672 {
673         this->mwindow = mwindow;
674         this->thread = thread;
675         use_renderfarm = 0;
676 }
677
678 BatchRenderGUI::~BatchRenderGUI()
679 {
680         lock_window("BatchRenderGUI::~BatchRenderGUI");
681         loadlist_batch->stop();
682         savelist_batch->stop();
683         delete format_tools;
684         unlock_window();
685 }
686
687
688 void BatchRenderGUI::create_objects()
689 {
690         lock_window("BatchRenderGUI::create_objects");
691         mwindow->theme->get_batchrender_sizes(this, get_w(), get_h());
692         create_list(0);
693
694         int x = mwindow->theme->batchrender_x1;
695         int y = 5;
696         int x1 = x, x2 = get_w()/2 + 10; // mwindow->theme->batchrender_x2;
697         int y1 = 5, y2 = 5;
698
699 // output file
700         add_subwindow(output_path_title = new BC_Title(x1, y1, _("Output path:")));
701         y1 += output_path_title->get_h() + mwindow->theme->widget_border;
702
703         format_tools = new BatchFormat(mwindow, this, thread->get_current_asset());
704         format_tools->set_w(get_w() / 2);
705         BatchRenderJob *current_job = thread->get_current_job();
706         format_tools->create_objects(x1, y1, 1, 1, 1, 1, 0, 1, 0, 0,
707                         &current_job->labeled, 0);
708         if( mwindow->preferences->use_renderfarm ) {
709                 use_renderfarm = new BatchRenderUseFarm(thread, x1, y1,
710                         &current_job->farmed);
711                 add_subwindow(use_renderfarm);
712                 y1 += use_renderfarm->get_h() + 10;
713         }
714 // input EDL
715         add_subwindow(edl_path_title = new BC_Title(x2, y2, _("EDL Path:")));
716         y2 += edl_path_title->get_h() + mwindow->theme->widget_border;
717
718         x = x2;  y = y2;
719         add_subwindow(edl_path_text = new BatchRenderEDLPath( thread,
720                 x, y, get_w()-x - 40, thread->get_current_edl()));
721         x =  x2 + edl_path_text->get_w();
722         add_subwindow(edl_path_browse = new BrowseButton(
723                 mwindow->theme, this, edl_path_text, x, y, thread->get_current_edl(),
724                 _("Input EDL"), _("Select an EDL to load:"), 0));
725         y2 = y + edl_path_browse->get_h() + mwindow->theme->widget_border;
726
727         x = x2;  y = y2;
728         add_subwindow(update_selected_edl = new BatchRenderUpdateEDL(thread, x, y));
729         y += update_selected_edl->get_h() + mwindow->theme->widget_border;
730         add_subwindow(use_current_edl = new BatchRenderCurrentEDL(thread, x, y));
731         y += use_current_edl->get_h() + mwindow->theme->widget_border;
732         if( !mwindow->edl || !mwindow->edl->path[0] ) use_current_edl->disable();
733         add_subwindow(new_batch = new BatchRenderNew(thread, x, y));
734         x += new_batch->get_w() + mwindow->theme->widget_border;
735         add_subwindow(delete_batch = new BatchRenderDelete(thread, x, y));
736         x = x2;  y += delete_batch->get_h() + mwindow->theme->widget_border;
737         add_subwindow(savelist_batch = new BatchRenderSaveList(thread, x, y));
738         x += savelist_batch->get_w() + mwindow->theme->widget_border;
739         add_subwindow(loadlist_batch = new BatchRenderLoadList(thread, x, y));
740         y += loadlist_batch->get_h() + mwindow->theme->widget_border;
741         add_subwindow(warning = new BatchRenderWarning(thread, x2, y));
742         y2 = y + warning->get_h() + mwindow->theme->widget_border;
743         if( y2 > y1 ) y1 = y2;
744         x = mwindow->theme->batchrender_x1, y = y1;
745
746         add_subwindow(list_title = new BC_Title(x, y, _("Batches to render:")));
747         x1 = x + list_title->get_w() + mwindow->theme->widget_border;;
748         add_subwindow(batch_path = new BC_Title(x1, y, thread->batch_path, MEDIUMFONT));
749         y += list_title->get_h() + mwindow->theme->widget_border;
750         y1 = get_h();
751         y1 -= 15 + BC_GenericButton::calculate_h() + mwindow->theme->widget_border;
752         add_subwindow(batch_list = new BatchRenderList(thread, x, y,
753                 get_w() - x - 10, y1 - y));
754         y += batch_list->get_h() + mwindow->theme->widget_border;
755
756         add_subwindow(start_button = new BatchRenderStart(thread, x, y));
757         x = get_w() / 2 - BC_GenericButton::calculate_w(this, _("Stop")) / 2;
758         add_subwindow(stop_button = new BatchRenderStop(thread, x, y));
759         x = get_w() - BC_GenericButton::calculate_w(this, _("Close")) - 10;
760         add_subwindow(cancel_button = new BatchRenderCancel(thread, x, y));
761
762         show_window(1);
763         unlock_window();
764 }
765
766 void BatchRenderGUI::button_disable()
767 {
768         new_batch->disable();
769         delete_batch->disable();
770         use_current_edl->disable();
771         update_selected_edl->disable();
772 }
773
774 void BatchRenderGUI::button_enable()
775 {
776         new_batch->enable();
777         delete_batch->enable();
778         if( mwindow->edl && mwindow->edl->path[0] )
779                 use_current_edl->enable();
780         update_selected_edl->enable();
781 }
782
783 int BatchRenderGUI::resize_event(int w, int h)
784 {
785         mwindow->session->batchrender_w = w;
786         mwindow->session->batchrender_h = h;
787         mwindow->theme->get_batchrender_sizes(this, w, h);
788
789         int x = mwindow->theme->batchrender_x1;
790         int y = 5;
791         int x1 = x, x2 = get_w()/2 + 10; // mwindow->theme->batchrender_x2;
792         int y1 = 5, y2 = 5;
793
794 // output file
795         output_path_title->reposition_window(x1, y1);
796         y1 += output_path_title->get_h() + mwindow->theme->widget_border;
797         format_tools->reposition_window(x1, y1);
798         if( use_renderfarm )
799                 use_renderfarm->reposition_window(x1, y1);
800 // input EDL
801         x = x2, y = y2;
802         edl_path_title->reposition_window(x, y);
803         y += edl_path_title->get_h() + mwindow->theme->widget_border;
804         edl_path_text->reposition_window(x, y, w - x - 40);
805         x += edl_path_text->get_w();
806         edl_path_browse->reposition_window(x, y);
807         y2 = y + edl_path_browse->get_h() + mwindow->theme->widget_border;
808
809         x = x2;  y = y2;
810         update_selected_edl->reposition_window(x, y);
811         y += update_selected_edl->get_h() + mwindow->theme->widget_border;
812         use_current_edl->reposition_window(x, y);
813         y += use_current_edl->get_h() + mwindow->theme->widget_border;
814         new_batch->reposition_window(x, y);
815         x += new_batch->get_w() + mwindow->theme->widget_border;
816         delete_batch->reposition_window(x, y);
817
818         x = x2;  y += delete_batch->get_h() + mwindow->theme->widget_border;
819         savelist_batch->reposition_window(x, y);
820         x += savelist_batch->get_w() + mwindow->theme->widget_border;
821         loadlist_batch->reposition_window(x, y);
822         y += loadlist_batch->get_h() + mwindow->theme->widget_border;
823         warning->reposition_window(x2, y);
824
825         y1 = 15 + BC_GenericButton::calculate_h() + mwindow->theme->widget_border;
826         y2 = get_h() - y1 - batch_list->get_h();
827         y2 -= list_title->get_h() + mwindow->theme->widget_border;
828
829         x = mwindow->theme->batchrender_x1;  y = y2;
830         list_title->reposition_window(x, y);
831         y += list_title->get_h() + mwindow->theme->widget_border;
832         batch_list->reposition_window(x, y, w - x - 10, h - y - y1);
833         y += batch_list->get_h() + mwindow->theme->widget_border;
834
835         start_button->reposition_window(x, y);
836         x = w / 2 - stop_button->get_w() / 2;
837         stop_button->reposition_window(x, y);
838         x = w - cancel_button->get_w() - 10;
839         cancel_button->reposition_window(x, y);
840         return 1;
841 }
842
843 int BatchRenderGUI::translation_event()
844 {
845         mwindow->session->batchrender_x = get_x();
846         mwindow->session->batchrender_y = get_y();
847         return 1;
848 }
849
850 int BatchRenderGUI::close_event()
851 {
852 // Stop batch rendering
853         unlock_window();
854         thread->stop_rendering();
855         lock_window("BatchRenderGUI::close_event");
856         set_done(1);
857         return 1;
858 }
859
860 void BatchRenderGUI::create_list(int update_widget)
861 {
862         for( int i = 0; i < BATCHRENDER_COLUMNS; i++ ) {
863                 list_items[i].remove_all_objects();
864         }
865
866         const char **column_titles = BatchRenderThread::column_titles;
867         list_columns = 0;
868         list_titles[list_columns] = _(column_titles[ENABLED_COL]);
869         list_width[list_columns++] = thread->list_width[ENABLED_COL];
870         list_titles[list_columns] = _(column_titles[LABELED_COL]);
871         list_width[list_columns++] = thread->list_width[LABELED_COL];
872         if( mwindow->preferences->use_renderfarm ) {
873                 list_titles[list_columns] = _(column_titles[FARMED_COL]);
874                 list_width[list_columns++] = thread->list_width[FARMED_COL];
875         }
876         list_titles[list_columns] = _(column_titles[OUTPUT_COL]);
877         list_width[list_columns++] = thread->list_width[OUTPUT_COL];
878         list_titles[list_columns] = _(column_titles[EDL_COL]);
879         list_width[list_columns++] = thread->list_width[EDL_COL];
880         list_titles[list_columns] = _(column_titles[ELAPSED_COL]);
881         list_width[list_columns++] = thread->list_width[ELAPSED_COL];
882
883         for( int i = 0; i < thread->jobs.total; i++ ) {
884                 BatchRenderJob *job = thread->jobs.values[i];
885                 char string[BCTEXTLEN];
886                 BC_ListBoxItem *enabled = new BC_ListBoxItem(job->enabled ? "X" : " ");
887                 BC_ListBoxItem *labeled = new BC_ListBoxItem(job->labeled ? "X" : " ");
888                 BC_ListBoxItem *farmed  = !mwindow->preferences->use_renderfarm ? 0 :
889                         new BC_ListBoxItem(job->farmed  ? "X" : " ");
890                 BC_ListBoxItem *out_path = new BC_ListBoxItem(job->asset->path);
891                 BC_ListBoxItem *edl_path = new BC_ListBoxItem(job->edl_path);
892                 BC_ListBoxItem *elapsed = new BC_ListBoxItem(!job->elapsed ? _("Unknown") :
893                         Units::totext(string, job->elapsed, TIME_HMS2));
894                 int col = 0;
895                 list_items[col++].append(enabled);
896                 list_items[col++].append(labeled);
897                 if( farmed ) list_items[col++].append(farmed);
898                 list_items[col++].append(out_path);
899                 list_items[col++].append(edl_path);
900                 list_items[col].append(elapsed);
901                 if( i == thread->current_job ) {
902                         enabled->set_selected(1);
903                         labeled->set_selected(1);
904                         if( farmed ) farmed->set_selected(1);
905                         out_path->set_selected(1);
906                         edl_path->set_selected(1);
907                         elapsed->set_selected(1);
908                 }
909                 if( i == thread->rendering_job ) {
910                         enabled->set_color(RED);
911                         labeled->set_color(RED);
912                         if( farmed ) farmed->set_color(RED);
913                         out_path->set_color(RED);
914                         edl_path->set_color(RED);
915                         elapsed->set_color(RED);
916                 }
917         }
918
919         if( update_widget ) {
920                 batch_list->update(list_items, list_titles, list_width, list_columns,
921                         batch_list->get_xposition(), batch_list->get_yposition(),
922                         batch_list->get_highlighted_item(), 1, 1);
923         }
924 }
925
926 void BatchRenderGUI::change_job()
927 {
928         BatchRenderJob *job = thread->get_current_job();
929         format_tools->update(job->asset, &job->labeled);
930         if( use_renderfarm ) use_renderfarm->update(&job->farmed);
931         edl_path_text->update(job->edl_path);
932 }
933
934
935 BatchFormat::BatchFormat(MWindow *mwindow, BatchRenderGUI *gui, Asset *asset)
936  : FormatTools(mwindow, gui, asset)
937 {
938         this->gui = gui;
939         this->mwindow = mwindow;
940 }
941
942 BatchFormat::~BatchFormat()
943 {
944 }
945
946
947 int BatchFormat::handle_event()
948 {
949         gui->create_list(1);
950         return 1;
951 }
952
953 BatchRenderEDLPath::BatchRenderEDLPath(BatchRenderThread *thread,
954         int x, int y, int w, char *text)
955  : BC_TextBox(x, y, w, 1, text)
956 {
957         this->thread = thread;
958 }
959
960
961 int BatchRenderEDLPath::handle_event()
962 {
963         calculate_suggestions();
964         strcpy(thread->get_current_edl(), get_text());
965         thread->gui->create_list(1);
966         return 1;
967 }
968
969 BatchRenderNew::BatchRenderNew(BatchRenderThread *thread,
970         int x,
971         int y)
972  : BC_GenericButton(x, y, _("New"))
973 {
974         this->thread = thread;
975 }
976
977 int BatchRenderNew::handle_event()
978 {
979         thread->new_job();
980         return 1;
981 }
982
983 BatchRenderDelete::BatchRenderDelete(BatchRenderThread *thread, int x, int y)
984  : BC_GenericButton(x, y, _("Delete"))
985 {
986         this->thread = thread;
987 }
988
989 int BatchRenderDelete::handle_event()
990 {
991         thread->delete_job();
992         return 1;
993 }
994
995
996
997 BatchRenderSaveList::BatchRenderSaveList(BatchRenderThread *thread, int x, int y)
998  : BC_GenericButton(x, y, _("Save Jobs"))
999 {
1000         this->thread = thread;
1001         set_tooltip(_("Save a Batch Render List"));
1002         gui = 0;
1003         startup_lock = new Mutex("BatchRenderSaveList::startup_lock");
1004 }
1005
1006 BatchRenderSaveList::~BatchRenderSaveList()
1007 {
1008         stop();
1009         delete startup_lock;
1010 }
1011
1012 void BatchRenderSaveList::stop()
1013 {
1014         startup_lock->lock("BatchRenderSaveList::~BrowseButton");
1015         if( gui ) gui->set_done(1);
1016         startup_lock->unlock();
1017         Thread::join();
1018 }
1019
1020 int BatchRenderSaveList::handle_event()
1021 {
1022         if( Thread::running() ) {
1023                 if( gui ) {
1024                         gui->lock_window();
1025                         gui->raise_window();
1026                         gui->unlock_window();
1027                 }
1028                 return 1;
1029         }
1030         startup_lock->lock("BatchRenderSaveList::handle_event 1");
1031         Thread::start();
1032         startup_lock->lock("BatchRenderSaveList::handle_event 2");
1033         startup_lock->unlock();
1034         return 1;
1035 }
1036
1037 void BatchRenderSaveList::run()
1038 {
1039         char default_path[BCTEXTLEN];
1040         sprintf(default_path, "~");
1041         thread->mwindow->defaults->get("DEFAULT_BATCHLOADPATH", default_path);
1042         BC_FileBox filewindow(100, 100, default_path, _("Save Batch Render List"),
1043                         _("Enter a Batch Render filename to save as:"),
1044                         0, 0, 0, 0);
1045         gui = &filewindow;
1046
1047         startup_lock->unlock();
1048         filewindow.create_objects();
1049
1050         int result2 = filewindow.run_window();
1051         if( !result2 ) {
1052                 strcpy(thread->batch_path, filewindow.get_submitted_path());
1053                 thread->gui->lock_window("BatchRenderSaveList::run");
1054                 thread->gui->batch_path->update(thread->batch_path);
1055                 thread->gui->unlock_window();
1056                 thread->mwindow->defaults->update("DEFAULT_BATCHLOADPATH", thread->batch_path);
1057                 thread->save_jobs(thread->batch_path);
1058         }
1059
1060         startup_lock->lock("BatchRenderLoadList::run");
1061         gui = 0;
1062         startup_lock->unlock();
1063 }
1064
1065 int BatchRenderSaveList::keypress_event() {
1066         if( get_keypress() == 's' ||
1067             get_keypress() == 'S' ) return handle_event();
1068         return 0;
1069 }
1070
1071
1072 BatchRenderLoadList::BatchRenderLoadList(BatchRenderThread *thread,
1073         int x,
1074         int y)
1075   : BC_GenericButton(x, y, _("Load Jobs")),
1076     Thread()
1077 {
1078         this->thread = thread;
1079         set_tooltip(_("Load a previously saved Batch Render List"));
1080         gui = 0;
1081         startup_lock = new Mutex("BatchRenderLoadList::startup_lock");
1082 }
1083
1084 BatchRenderLoadList::~BatchRenderLoadList()
1085 {
1086         stop();
1087         delete startup_lock;
1088 }
1089
1090 void BatchRenderLoadList::stop()
1091 {
1092         startup_lock->lock("BatchRenderLoadList::~BrowseButton");
1093         if( gui ) gui->set_done(1);
1094         startup_lock->unlock();
1095         Thread::join();
1096 }
1097
1098 int BatchRenderLoadList::handle_event()
1099 {
1100         if( Thread::running() ) {
1101                 if( gui ) {
1102                         gui->lock_window();
1103                         gui->raise_window();
1104                         gui->unlock_window();
1105                 }
1106                 return 1;
1107         }
1108         startup_lock->lock("BatchRenderLoadList::handle_event 1");
1109         Thread::start();
1110         startup_lock->lock("BatchRenderLoadList::handle_event 2");
1111         startup_lock->unlock();
1112         return 1;
1113 }
1114
1115 void BatchRenderLoadList::run()
1116 {
1117         char default_path[BCTEXTLEN];
1118         sprintf(default_path, "~");
1119         thread->mwindow->defaults->get("DEFAULT_BATCHLOADPATH", default_path);
1120         BC_FileBox filewindow(100, 100, default_path, _("Load Batch Render List"),
1121                         _("Enter a Batch Render filename to load from:"),
1122                         0, 0, 0, 0);
1123         gui = &filewindow;
1124
1125         startup_lock->unlock();
1126         filewindow.create_objects();
1127
1128         int result2 = filewindow.run_window();
1129         if( !result2 ) {
1130                 strcpy(thread->batch_path, filewindow.get_submitted_path());
1131                 thread->gui->batch_path->update(thread->batch_path);
1132                 thread->mwindow->defaults->update("DEFAULT_BATCHLOADPATH", thread->batch_path);
1133                 thread->load_jobs(thread->batch_path, thread->mwindow->preferences);
1134                 thread->gui->create_list(1);
1135                 thread->current_job = 0;
1136                 thread->gui->change_job();
1137         }
1138
1139         startup_lock->lock("BatchRenderLoadList::run");
1140         gui = 0;
1141         startup_lock->unlock();
1142 }
1143
1144 int BatchRenderLoadList::keypress_event() {
1145         if( get_keypress() == 'o' ||
1146             get_keypress() == 'O' ) return handle_event();
1147         return 0;
1148 }
1149
1150 BatchRenderCurrentEDL::BatchRenderCurrentEDL(BatchRenderThread *thread,
1151         int x,
1152         int y)
1153  : BC_GenericButton(x, y, _("Use Current EDL"))
1154 {
1155         this->thread = thread;
1156 }
1157
1158 int BatchRenderCurrentEDL::handle_event()
1159 {
1160         thread->use_current_edl();
1161         return 1;
1162 }
1163
1164 BatchRenderUpdateEDL::BatchRenderUpdateEDL(BatchRenderThread *thread,
1165         int x,
1166         int y)
1167  : BC_GenericButton(x, y, _("Save to EDL Path"))
1168 {
1169         this->thread = thread;
1170 }
1171
1172 int BatchRenderUpdateEDL::handle_event()
1173 {
1174         thread->update_selected_edl();
1175         return 1;
1176 }
1177
1178
1179 BatchRenderList::BatchRenderList(BatchRenderThread *thread,
1180         int x, int y, int w, int h)
1181  : BC_ListBox(x, y, w, h, LISTBOX_TEXT, thread->gui->list_items,
1182         thread->gui->list_titles, thread->gui->list_width, thread->gui->list_columns,
1183         0, 0, LISTBOX_SINGLE, ICON_LEFT, 1)
1184 {
1185         this->thread = thread;
1186         dragging_item = 0;
1187         set_process_drag(0);
1188 }
1189
1190 int BatchRenderList::handle_event()
1191 {
1192         return 1;
1193 }
1194
1195 int BatchRenderList::selection_changed()
1196 {
1197         thread->current_job = get_selection_number(0, 0);
1198         thread->gui->change_job();
1199         int cursor_x = get_cursor_x();
1200         BatchRenderJob *job = thread->get_current_job();
1201         int col_x = 0, changed = 1;
1202         if( cursor_x < (col_x += thread->list_width[ENABLED_COL]) )
1203                 job->enabled = !job->enabled;
1204         else if( cursor_x < (col_x += thread->list_width[LABELED_COL]) )
1205                 job->labeled = job->edl_path[0] != '@' ? !job->labeled : 0;
1206         else if( thread->gui->use_renderfarm &&
1207                  cursor_x < (col_x += thread->list_width[FARMED_COL]) )
1208                 job->farmed = job->edl_path[0] != '@' ? !job->farmed : 0;
1209         else
1210                 changed = 0;
1211         if( changed ) {
1212                 thread->gui->create_list(1);
1213                 thread->gui->change_job();
1214         }
1215         return 1;
1216 }
1217
1218 int BatchRenderList::column_resize_event()
1219 {
1220         int col = 0;
1221         thread->list_width[ENABLED_COL] = get_column_width(col++);
1222         thread->list_width[LABELED_COL] = get_column_width(col++);
1223         if( thread->gui->use_renderfarm )
1224                 thread->list_width[FARMED_COL] = get_column_width(col++);
1225         thread->list_width[OUTPUT_COL] = get_column_width(col++);
1226         thread->list_width[EDL_COL] = get_column_width(col++);
1227         thread->list_width[ELAPSED_COL] = get_column_width(col);
1228         return 1;
1229 }
1230
1231 int BatchRenderList::drag_start_event()
1232 {
1233         if( BC_ListBox::drag_start_event() ) {
1234                 dragging_item = 1;
1235                 return 1;
1236         }
1237
1238         return 0;
1239 }
1240
1241 int BatchRenderList::drag_motion_event()
1242 {
1243         if( BC_ListBox::drag_motion_event() ) {
1244                 return 1;
1245         }
1246         return 0;
1247 }
1248
1249 int BatchRenderList::drag_stop_event()
1250 {
1251         if( dragging_item ) {
1252                 int src = get_selection_number(0, 0);
1253                 int dst = get_highlighted_item();
1254                 if( src != dst ) {
1255                         thread->move_batch(src, dst);
1256                 }
1257                 BC_ListBox::drag_stop_event();
1258                 dragging_item = 0;
1259         }
1260         return 0;
1261 }
1262
1263
1264
1265 BatchRenderStart::BatchRenderStart(BatchRenderThread *thread, int x, int y)
1266  : BC_GenericButton(x, y, _("Start"))
1267 {
1268         this->thread = thread;
1269 }
1270
1271 int BatchRenderStart::handle_event()
1272 {
1273         thread->start_rendering();
1274         return 1;
1275 }
1276
1277 BatchRenderStop::BatchRenderStop(BatchRenderThread *thread, int x, int y)
1278  : BC_GenericButton(x, y, _("Stop"))
1279 {
1280         this->thread = thread;
1281 }
1282
1283 int BatchRenderStop::handle_event()
1284 {
1285         unlock_window();
1286         thread->stop_rendering();
1287         lock_window("BatchRenderStop::handle_event");
1288         return 1;
1289 }
1290
1291
1292 BatchRenderWarning::BatchRenderWarning(BatchRenderThread *thread, int x, int y)
1293  : BC_CheckBox(x, y, thread->warn, _("warn if jobs/session mismatched"))
1294 {
1295         this->thread = thread;
1296 }
1297
1298 int BatchRenderWarning::handle_event()
1299 {
1300         thread->warn = get_value();
1301         return 1;
1302 }
1303
1304 BatchRenderCancel::BatchRenderCancel(BatchRenderThread *thread, int x, int y)
1305  : BC_GenericButton(x, y, _("Close"))
1306 {
1307         this->thread = thread;
1308 }
1309
1310 int BatchRenderCancel::handle_event()
1311 {
1312         unlock_window();
1313         thread->stop_rendering();
1314         lock_window("BatchRenderCancel::handle_event");
1315         thread->gui->set_done(1);
1316         return 1;
1317 }
1318
1319 int BatchRenderCancel::keypress_event()
1320 {
1321         if( get_keypress() == ESC ) {
1322                 unlock_window();
1323                 thread->stop_rendering();
1324                 lock_window("BatchRenderCancel::keypress_event");
1325                 thread->gui->set_done(1);
1326                 return 1;
1327         }
1328         return 0;
1329 }
1330
1331 BatchRenderUseFarm::BatchRenderUseFarm(BatchRenderThread *thread, int x, int y, int *output)
1332  : BC_CheckBox(x, y, *output, _("Use render farm"))
1333 {
1334         this->thread = thread;
1335         this->output = output;
1336 }
1337
1338 int BatchRenderUseFarm::handle_event()
1339 {
1340         *output = get_value();
1341         thread->gui->create_list(1);
1342         return 1;
1343 }
1344
1345 void BatchRenderUseFarm::update(int *output)
1346 {
1347         this->output = output;
1348         BC_CheckBox::update(*output);
1349 }
1350