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