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