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