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