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