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