remove filefork
[goodguy/history.git] / cinelerra-5.0 / cinelerra / mwindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  * 
19  */
20
21 #include "asset.h"
22 #include "assets.h"
23 #include "audioalsa.h"
24 #include "awindowgui.h"
25 #include "awindow.h"
26 #include "batchrender.h"
27 #include "bcdisplayinfo.h"
28 #include "bcsignals.h"
29 #include "bctimer.h"
30 #include "bdcreate.h"
31 #include "brender.h"
32 #include "cache.h"
33 #include "channel.h"
34 #include "channeldb.h"
35 #include "channelinfo.h"
36 #include "clip.h"
37 #include "bccmodels.h"
38 #include "commercials.h"
39 #include "cplayback.h"
40 #include "ctimebar.h"
41 #include "cwindowgui.h"
42 #include "cwindow.h"
43 #include "bchash.h"
44 #include "devicedvbinput.inc"
45 #include "dvdcreate.h"
46 #include "editpanel.h"
47 #include "edl.h"
48 #include "edlsession.h"
49 #include "errorbox.h"
50 #include "fileformat.h"
51 #include "file.h"
52 #include "filesystem.h"
53 #include "filexml.h"
54 #include "format.inc"
55 #include "framecache.h"
56 #include "gwindow.h"
57 #include "gwindowgui.h"
58 #include "keyframegui.h"
59 #include "indexfile.h"
60 #include "language.h"
61 #include "levelwindowgui.h"
62 #include "levelwindow.h"
63 #include "loadfile.inc"
64 #include "localsession.h"
65 #include "maincursor.h"
66 #include "mainerror.h"
67 #include "mainindexes.h"
68 #include "mainmenu.h"
69 #include "mainprogress.h"
70 #include "mainsession.h"
71 #include "mainundo.h"
72 #include "mbuttons.h"
73 #include "mutex.h"
74 #include "mwindowgui.h"
75 #include "mwindow.h"
76 #include "nestededls.h"
77 #include "new.h"
78 #include "patchbay.h"
79 #include "playback3d.h"
80 #include "playbackengine.h"
81 #include "plugin.h"
82 #include "pluginserver.h"
83 #include "pluginset.h"
84 #include "preferences.h"
85 #include "record.h"
86 #include "recordmonitor.h"
87 #include "recordlabel.h"
88 #include "removefile.h"
89 #include "render.h"
90 #include "samplescroll.h"
91 #include "sha1.h"
92 #include "sighandler.h"
93 #include "splashgui.h"
94 #include "statusbar.h"
95 #include "theme.h"
96 #include "threadloader.h"
97 #include "timebar.h"
98 #include "tipwindow.h"
99 #include "trackcanvas.h"
100 #include "track.h"
101 #include "tracking.h"
102 #include "trackscroll.h"
103 #include "tracks.h"
104 #include "transition.h"
105 #include "transportque.h"
106 #include "vframe.h"
107 #include "videodevice.inc"
108 #include "videowindow.h"
109 #include "vplayback.h"
110 #include "vwindowgui.h"
111 #include "vwindow.h"
112 #include "wavecache.h"
113 #include "wwindow.h"
114 #include "zoombar.h"
115
116 #include <string.h>
117 #include <sys/stat.h>
118
119
120 extern "C"
121 {
122
123
124
125
126 // Hack for libdv to remove glib dependancy
127
128 // void
129 // g_log (const char    *log_domain,
130 //        int  log_level,
131 //        const char    *format,
132 //        ...)
133 // {
134 // }
135 // 
136 // void
137 // g_logv (const char    *log_domain,
138 //        int  log_level,
139 //        const char    *format,
140 //        ...)
141 // {
142 // }
143 // 
144
145
146 // Hack for XFree86 4.1.0
147
148 int atexit(void (*function)(void))
149 {
150         return 0;
151 }
152
153
154
155 }
156
157
158
159 ArrayList<PluginServer*>* MWindow::plugindb = 0;
160 Commercials* MWindow::commercials = 0;
161
162
163 MWindow::MWindow()
164  : Thread(1, 0, 0)
165 {
166         plugin_gui_lock = new Mutex("MWindow::plugin_gui_lock");
167         dead_plugin_lock = new Mutex("MWindow::dead_plugin_lock");
168         vwindows_lock = new Mutex("MWindow::vwindows_lock");
169         brender_lock = new Mutex("MWindow::brender_lock");
170         keyframe_gui_lock = new Mutex("MWindow::keyframe_gui_lock");
171
172         playback_3d = 0;
173         splash_window = 0;
174         undo = 0;
175         defaults = 0;
176         assets = 0;
177         //commercials = 0;
178         commercial_active = 0;
179         audio_cache = 0;
180         video_cache = 0;
181         frame_cache = 0;
182         wave_cache = 0;
183         preferences = 0;
184         session = 0;
185         theme = 0;
186         mainindexes = 0;
187         mainprogress = 0;
188         brender = 0;
189         channeldb_buz =  new ChannelDB;
190         channeldb_v4l2jpeg =  new ChannelDB;
191         //file_server = 0;
192         plugin_guis = 0;
193         dead_plugins = 0;
194         keyframe_threads = 0;
195         create_bd = 0;
196         create_dvd = 0;
197         batch_render = 0;
198         render = 0;
199         edl = 0;
200         gui = 0;
201         cwindow = 0;
202         awindow = 0;
203         gwindow = 0;
204         twindow = 0;
205         wwindow = 0;
206         lwindow = 0;
207         sighandler = 0;
208         reload_status = 0;
209         screens = 1;
210         in_destructor = 0;
211 }
212
213
214 // Need to delete brender temporary here.
215 MWindow::~MWindow()
216 {
217         in_destructor = 1;
218 //printf("MWindow::~MWindow %d\n", __LINE__);
219         gui->stop_drawing();
220         gui->remote_control->deactivate();
221         gui->record->stop();
222         gui->channel_info->stop();
223         brender_lock->lock("MWindow::quit");
224         delete brender;         brender = 0;
225         brender_lock->unlock();
226         delete create_bd;       create_bd = 0;
227         delete create_dvd;      create_dvd = 0;
228         delete batch_render;    batch_render = 0;
229         commit_commercial();
230         if( commercials && !commercials->remove_user() ) commercials = 0;
231
232 // Save defaults for open plugins
233         plugin_gui_lock->lock("MWindow::~MWindow");
234         for(int i = 0; i < plugin_guis->size(); i++)
235         {
236                 plugin_guis->get(i)->hide_gui();
237         }
238         plugin_gui_lock->unlock();
239         hide_keyframe_guis();
240         clean_indexes();
241         save_defaults();
242 // Give up and go to a movie
243 //  cant run valgrind if this is used
244 //      if( !reload_status ) exit(0);
245
246         gui->del_keyboard_listener(
247                 (int (BC_WindowBase::*)(BC_WindowBase *))
248                 &MWindowGUI::keyboard_listener);
249 #if 0
250 // release the hounds
251         if( awindow && awindow->gui ) awindow->gui->close(0);
252         if( cwindow && cwindow->gui ) cwindow->gui->close(0);
253         if( lwindow && lwindow->gui ) lwindow->gui->close(0);
254         if( gwindow && gwindow->gui ) gwindow->gui->close(0);
255         if( twindow && twindow->is_running() ) twindow->close_window();
256         if( wwindow && wwindow->is_running() ) wwindow->close_window();
257         vwindows.remove_all_objects();
258         gui->close(0);
259         if( awindow ) awindow->join();
260         if( cwindow ) cwindow->join();
261         if( lwindow ) lwindow->join();
262         if( twindow ) twindow->join();
263         if( wwindow ) wwindow->join();
264         if( gwindow ) gwindow->join();
265         join();
266 #else
267 // one at a time, or nouveau chokes
268 #define close_gui(win) if( win ) { \
269   if( win->gui ) win->gui->close(0); \
270   win->join(); }
271         close_gui(awindow);
272         close_gui(cwindow);
273         close_gui(lwindow);
274         close_gui(gwindow);
275         close_gui(twindow);
276         close_gui(wwindow);
277         vwindows.remove_all_objects();
278         gui->close(0);
279         join();
280 #endif
281         reset_caches();
282         delete_plugins();
283         dead_plugins->remove_all();
284         finit_error();
285         keyframe_threads->remove_all_objects();
286         if( !edl->Garbage::remove_user() ) edl = 0;
287         colormodels.remove_all_objects();
288         delete gui;             gui = 0;
289         delete render;          render = 0;
290         delete awindow;         awindow = 0;
291         delete lwindow;         lwindow = 0;
292         delete twindow;         twindow = 0;
293         delete wwindow;         wwindow = 0;
294         delete gwindow;         gwindow = 0;
295         // must be last or nouveau chokes
296         delete cwindow;         cwindow = 0;
297         //delete file_server;  file_server = 0; // reusable
298         delete mainindexes;     mainindexes = 0;
299         delete mainprogress;    mainprogress = 0;
300         delete audio_cache;     audio_cache = 0;  // delete the cache after the assets
301         delete video_cache;     video_cache = 0;  // delete the cache after the assets
302         delete frame_cache;     frame_cache = 0;
303         delete wave_cache;      wave_cache = 0;
304         delete plugin_guis;     plugin_guis = 0;
305         delete dead_plugins;    dead_plugins = 0;
306         delete keyframe_threads;  keyframe_threads = 0;
307         delete undo;            undo = 0;
308         delete preferences;     preferences = 0;
309         delete session;         session = 0;
310         delete defaults;        defaults = 0;
311         delete assets;          assets = 0;
312         delete splash_window;   splash_window = 0;
313         delete theme;           theme = 0;
314         delete channeldb_buz;
315         delete channeldb_v4l2jpeg;
316 // This must be last thread to exit
317         delete playback_3d;     playback_3d = 0;
318         delete dead_plugin_lock;
319         delete plugin_gui_lock;
320         delete vwindows_lock;
321         delete brender_lock;
322         delete keyframe_gui_lock;
323         sighandler->terminate();
324         delete sighandler;
325 }
326
327
328 void MWindow::quit(int unlock)
329 {
330         stop_playback(1);
331         if(unlock) gui->unlock_window();
332
333         brender_lock->lock("MWindow::quit");
334         delete brender;         brender = 0;
335         brender_lock->unlock();
336
337         interrupt_indexes();
338         clean_indexes();
339         save_defaults();
340         gui->set_done(0);
341         if(unlock) gui->lock_window("MWindow::quit");
342 }
343
344 void MWindow::init_error()
345 {
346         MainError::init_error(this);
347 }
348
349 void MWindow::finit_error()
350 {
351         MainError::finit_error();
352 }
353
354 void MWindow::create_defaults_path(char *string, const char *config_file)
355 {
356 // set the .bcast path
357         FileSystem fs;
358
359         sprintf(string, "%s", BCASTDIR);
360         fs.complete_path(string);
361         if(!fs.is_dir(string)) 
362                 fs.create_dir(string); 
363
364 // load the defaults
365         strcat(string, config_file);
366 }
367
368 void MWindow::init_defaults(BC_Hash* &defaults, char *config_path)
369 {
370         char path[BCTEXTLEN];
371 // Use user supplied path
372         if(config_path[0])
373                 strcpy(path, config_path);
374         else
375                 create_defaults_path(path, CONFIG_FILE);
376
377         defaults = new BC_Hash(path);
378         defaults->load();
379 }
380
381 int MWindow::load_plugin_index(MWindow *mwindow, char *path)
382 {
383 // load index
384         FILE *fp = fopen(path, "r");
385         if( !fp ) return 1;
386         char index_line[BCTEXTLEN];
387         int index_version = -1, ret = 0;
388         if( !fgets(index_line, BCTEXTLEN, fp) ||
389             sscanf(index_line, "%d", &index_version) != 1 ||
390             index_version != PLUGIN_FILE_VERSION ) ret = 1;
391
392         while( !ret && !feof(fp)) {
393                 if( !fgets(index_line, BCTEXTLEN, fp) ) break;
394                 if( index_line[0] == ';' ) continue;
395                 if( index_line[0] == '#' ) continue;
396                 int type = PLUGIN_TYPE_UNKNOWN;
397                 char path[BCTEXTLEN], title[BCTEXTLEN];
398                 if( PluginServer::scan_table(index_line, type, path, title) ) continue;
399                 PluginServer *server = 0;
400                 switch( type ) {
401                 case PLUGIN_TYPE_BUILTIN:
402                 case PLUGIN_TYPE_LADSPA:
403                         server = new PluginServer(mwindow, path, type);
404                         break;
405                 case PLUGIN_TYPE_FFMPEG: // skip "ff_..."
406                         server = new_ffmpeg_server(mwindow, path+3);
407                         break;
408                 }
409                 if( !server ) continue;
410 // Create plugin server from index entry
411                 server->set_title(title);
412                 if( server->read_table(index_line) ) {
413                         delete server;
414                         ret = 1;  break;
415                 }
416                 plugindb->append(server);
417         }
418
419         fclose(fp);
420         return ret;
421 }
422
423 void MWindow::init_plugin_index(MWindow *mwindow, Preferences *preferences, FILE *fp,
424         const char *plug_dir, const char *plug_path, int &idx)
425 {
426         char plugin_path[BCTEXTLEN];
427         if( *plug_path != '/' )
428                 sprintf(plugin_path, "%s/%s", plug_dir, plug_path);
429         else
430                 strcpy(plugin_path, plug_path);
431         FileSystem fs;
432         fs.set_filter( "[*.plugin][*.so]" );
433         int result = fs.update(plugin_path);
434         if( result || !fs.dir_list.total ) return;
435         int vis_id = idx++;
436
437         for( int i=0; i<fs.dir_list.total; ++i ) {
438                 char *fs_path = fs.dir_list.values[i]->path;
439                 char *base_path = FileSystem::basepath(fs_path), *bp = base_path;
440                 const char *dp = plug_dir;
441                 while( *bp && *dp && *bp == *dp ) { ++bp; ++dp; }
442                 strcpy(plugin_path, !*dp && *bp == '/' ? bp+1 : base_path);
443                 delete [] base_path;
444                 if( fs.is_dir(fs_path) ) {
445                         init_plugin_index(mwindow, preferences, fp, plug_dir, plugin_path, idx);
446                         continue;
447                 }
448                 if( plugin_exists(plugin_path) ) continue;
449                 PluginServer *server = new PluginServer(mwindow, plugin_path, PLUGIN_TYPE_UNKNOWN);
450                 result = server->open_plugin(1, preferences, 0, 0);
451                 if( !result ) {
452                         server->write_table(fp,vis_id);
453                         server->close_plugin();
454                         server->delete_this();
455                         continue;
456                 }
457                 if( result != PLUGINSERVER_IS_LAD ) continue;
458                 int lad_index = 0;
459                 for(;;) {
460                         PluginServer *server = new PluginServer(mwindow, plugin_path, PLUGIN_TYPE_LADSPA);
461                         server->set_lad_index(lad_index++);
462                         result = server->open_plugin(1, preferences, 0, 0);
463                         if( result ) break;
464                         server->write_table(fp, PLUGIN_LADSPA_ID);
465                         server->close_plugin();
466                         server->delete_this();
467                 }
468         }
469 }
470
471 int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
472 {
473         init_ffmpeg();
474         if( !plugindb ) plugindb = new ArrayList<PluginServer*>;
475         char index_path[BCTEXTLEN];
476         sprintf(index_path, "%s/%s", preferences->plugin_dir, PLUGIN_FILE);
477         if( !load_plugin_index(mwindow, index_path) ) return 1;
478         FILE *fp = fopen(index_path,"w");
479         if( !fp ) {
480                 fprintf(stderr,_("MWindow::init_plugins: "
481                         "can't create plugin index: %s\n"), index_path);
482                 return 1;
483         }
484         fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
485         char *plug_path = FileSystem::basepath(preferences->plugin_dir);
486         int dir_id = PLUGIN_IDS;
487         init_plugin_index(mwindow, preferences, fp, plug_path, ".", dir_id);
488         init_ffmpeg_index(mwindow, preferences, fp);
489         fclose(fp);
490         delete [] plug_path;
491         return load_plugin_index(mwindow, index_path);
492 }
493
494 void MWindow::delete_plugins()
495 {
496         plugindb->remove_all_objects();
497         delete plugindb;
498         plugindb = 0;
499 }
500
501 void MWindow::search_plugindb(int do_audio, 
502                 int do_video, 
503                 int is_realtime, 
504                 int is_transition,
505                 int is_theme,
506                 ArrayList<PluginServer*> &results)
507 {
508 // Get plugins
509         for(int i = 0; i < MWindow::plugindb->total; i++)
510         {
511                 PluginServer *current = MWindow::plugindb->values[i];
512
513                 if(current->audio == do_audio &&
514                         current->video == do_video &&
515                         (current->realtime == is_realtime || is_realtime < 0) &&
516                         current->transition == is_transition &&
517                         current->theme == is_theme)
518                         results.append(current);
519         }
520
521 // Alphabetize list by title
522         int done = 0;
523         while(!done)
524         {
525                 done = 1;
526                 
527                 for(int i = 0; i < results.total - 1; i++)
528                 {
529                         PluginServer *value1 = results.values[i];
530                         PluginServer *value2 = results.values[i + 1];
531                         if(strcmp(_(value1->title), _(value2->title)) > 0)
532                         {
533                                 done = 0;
534                                 results.values[i] = value2;
535                                 results.values[i + 1] = value1;
536                         }
537                 }
538         }
539 }
540
541 PluginServer* MWindow::scan_plugindb(char *title,
542                 int data_type)
543 {
544 //      if(data_type < 0)
545 //      {
546 //              printf("MWindow::scan_plugindb data_type < 0\n");
547 //              return 0;
548 //      }
549
550         for(int i = 0; i < plugindb->total; i++)
551         {
552                 PluginServer *server = plugindb->values[i];
553                 if(server->title &&
554                         !strcasecmp(server->title, title) &&
555                         (data_type < 0 ||
556                                 (data_type == TRACK_AUDIO && server->audio) ||
557                                 (data_type == TRACK_VIDEO && server->video))) 
558                         return plugindb->values[i];
559         }
560         return 0;
561 }
562
563 int MWindow::plugin_exists(char *plugin_path)
564 {
565         for( int i=0; i<plugindb->total; ++i ) {
566                 PluginServer *server = plugindb->values[i];
567                 if( !strcmp(plugin_path, server->get_path()) ) return 1;
568         }
569         return 0;
570 }
571
572 void MWindow::init_preferences()
573 {
574         preferences = new Preferences;
575         preferences->load_defaults(defaults);
576         session = new MainSession(this);
577         session->load_defaults(defaults);
578         // set x11_host, screens, window_config
579         screens = session->set_default_x11_host();
580         BC_Signals::set_trap_path("/tmp/cinelerra_%d.dmp");
581         BC_Signals::set_trap_hook(trap_hook, this);
582         BC_Signals::set_catch_segv(preferences->trap_sigsegv);
583         BC_Signals::set_catch_intr(preferences->trap_sigintr);
584 }
585
586 void MWindow::clean_indexes()
587 {
588         FileSystem fs;
589         int total_excess;
590         long oldest = 0;
591         int oldest_item = -1;
592         int result;
593         char string[BCTEXTLEN];
594         char string2[BCTEXTLEN];
595
596 // Delete extra indexes
597         fs.set_filter("*.idx");
598         fs.complete_path(preferences->index_directory);
599         fs.update(preferences->index_directory);
600 //printf("MWindow::clean_indexes 1 %d\n", fs.dir_list.total);
601
602 // Eliminate directories
603         result = 1;
604         while(result)
605         {
606                 result = 0;
607                 for(int i = 0; i < fs.dir_list.total && !result; i++)
608                 {
609                         fs.join_names(string, preferences->index_directory, fs.dir_list.values[i]->name);
610                         if(fs.is_dir(string))
611                         {
612                                 delete fs.dir_list.values[i];
613                                 fs.dir_list.remove_number(i);
614                                 result = 1;
615                         }
616                 }
617         }
618         total_excess = fs.dir_list.total - preferences->index_count;
619
620 //printf("MWindow::clean_indexes 2 %d\n", fs.dir_list.total);
621         while(total_excess > 0)
622         {
623 // Get oldest
624                 for(int i = 0; i < fs.dir_list.total; i++)
625                 {
626                         fs.join_names(string, preferences->index_directory, fs.dir_list.values[i]->name);
627
628                         if(i == 0 || fs.get_date(string) <= oldest)
629                         {
630                                 oldest = fs.get_date(string);
631                                 oldest_item = i;
632                         }
633                 }
634
635                 if(oldest_item >= 0)
636                 {
637 // Remove index file
638                         fs.join_names(string, 
639                                 preferences->index_directory, 
640                                 fs.dir_list.values[oldest_item]->name);
641 //printf("MWindow::clean_indexes 1 %s\n", string);
642                         if(remove(string))
643                                 perror("delete_indexes");
644                         delete fs.dir_list.values[oldest_item];
645                         fs.dir_list.remove_number(oldest_item);
646
647 // Remove table of contents if it exists
648                         strcpy(string2, string);
649                         char *ptr = strrchr(string2, '.');
650                         if(ptr)
651                         {
652 //printf("MWindow::clean_indexes 2 %s\n", string2);
653                                 sprintf(ptr, ".toc");
654                                 remove(string2);
655                                 sprintf(ptr, ".mkr");
656                                 remove(string2);
657                         }
658                 }
659
660                 total_excess--;
661         }
662 }
663
664 void MWindow::init_awindow()
665 {
666         awindow = new AWindow(this);
667         awindow->create_objects();
668 }
669
670 void MWindow::init_gwindow()
671 {
672         gwindow = new GWindow(this);
673         gwindow->create_objects();
674 }
675
676 void MWindow::init_tipwindow()
677 {
678         if( !twindow )
679                 twindow = new TipWindow(this);
680         twindow->start();
681 }
682
683 void MWindow::show_warning(int *do_warning, const char *text)
684 {
685         if( do_warning && !*do_warning ) return;
686         if( !wwindow )
687                 wwindow = new WWindow(this);
688         wwindow->show_warning(do_warning, text);
689 }
690
691 void MWindow::init_theme()
692 {
693         Timer timer;
694         theme = 0;
695
696 // Replace blond theme with SUV since it doesn't work
697         if(!strcasecmp(preferences->theme, "Blond"))
698                 strcpy(preferences->theme, DEFAULT_THEME);
699
700         PluginServer *theme_plugin = 0;
701         for(int i = 0; i < plugindb->total && !theme_plugin; i++) {
702                 if( plugindb->values[i]->theme &&
703                     !strcasecmp(preferences->theme, plugindb->values[i]->title) )
704                         theme_plugin = plugindb->values[i];
705         }
706
707         if( !theme_plugin )
708                 fprintf(stderr, _("MWindow::init_theme: prefered theme %s not found.\n"),
709                          preferences->theme);
710
711         if( !theme_plugin && strcasecmp(preferences->theme, DEFAULT_THEME) ) {
712                 fprintf(stderr, _("MWindow::init_theme: trying default theme %s\n"),
713                         DEFAULT_THEME);
714                 for(int i = 0; i < plugindb->total && !theme_plugin; i++) {
715                         if( plugindb->values[i]->theme &&
716                             !strcasecmp(DEFAULT_THEME, plugindb->values[i]->title) )
717                                 theme_plugin = plugindb->values[i];
718                 }
719         }
720
721         if(!theme_plugin) {
722                 fprintf(stderr, _("MWindow::init_theme: theme_plugin not found.\n"));
723                 exit(1);
724         }
725
726         PluginServer plugin = *theme_plugin;
727         if( plugin.open_plugin(0, preferences, 0, 0) ) {
728                 fprintf(stderr, _("MWindow::init_theme: unable to load theme %s\n"),
729                         theme_plugin->title);
730                 exit(1);
731         }
732
733         theme = plugin.new_theme();
734         theme->mwindow = this;
735         strcpy(theme->path, plugin.path);
736         plugin.close_plugin();
737
738 // Load default images & settings
739         theme->Theme::initialize();
740 // Load user images & settings
741         theme->initialize();
742 // Create menus with user colors
743         theme->build_menus();
744         init_menus();
745
746         theme->check_used();
747
748 //printf("MWindow::init_theme %d total_time=%d\n", __LINE__, (int)timer.get_difference());
749 }
750
751 void MWindow::init_3d()
752 {
753         playback_3d = new Playback3D(this);
754         playback_3d->create_objects();
755 }
756
757 void MWindow::init_edl()
758 {
759         edl = new EDL;
760         edl->create_objects();
761         edl->load_defaults(defaults);
762         edl->create_default_tracks();
763         edl->tracks->update_y_pixels(theme);
764 }
765
766 void MWindow::init_compositor()
767 {
768         cwindow = new CWindow(this);
769         cwindow->create_objects();
770 }
771
772 void MWindow::init_levelwindow()
773 {
774         lwindow = new LevelWindow(this);
775         lwindow->create_objects();
776 }
777
778 VWindow *MWindow::get_viewer(int start_it, int idx)
779 {
780         vwindows_lock->lock("MWindow::get_viewer");
781         VWindow *vwindow = idx >= 0 && idx < vwindows.size() ? vwindows.get(idx) : 0;
782         if( !vwindow ) idx = vwindows.size();
783         while( !vwindow && --idx >= 0 ) {
784                 VWindow *vwin = vwindows.get(idx);
785                 if( !vwin->is_running() || !vwin->get_edl() )
786                         vwindow = vwin;
787         }
788         if( !vwindow ) {
789                 vwindow = new VWindow(this);
790                 vwindow->load_defaults();
791                 vwindow->create_objects();
792                 vwindows.append(vwindow);
793         }
794         vwindows_lock->unlock();
795         if( start_it ) vwindow->start();
796         return vwindow;
797 }
798
799 void MWindow::init_cache()
800 {
801         audio_cache = new CICache(preferences);
802         video_cache = new CICache(preferences);
803         frame_cache = new FrameCache;
804         wave_cache = new WaveCache;
805 }
806
807 void MWindow::init_channeldb()
808 {
809         channeldb_buz->load("channeldb_buz");
810         channeldb_v4l2jpeg->load("channeldb_v4l2jpeg");
811 }
812
813 void MWindow::init_menus()
814 {
815         char string[BCTEXTLEN];
816         BC_CModels::to_text(string, BC_RGB888);
817         colormodels.append(new ColormodelItem(string, BC_RGB888));
818         BC_CModels::to_text(string, BC_RGBA8888);
819         colormodels.append(new ColormodelItem(string, BC_RGBA8888));
820 //      BC_CModels::to_text(string, BC_RGB161616);
821 //      colormodels.append(new ColormodelItem(string, BC_RGB161616));
822 //      BC_CModels::to_text(string, BC_RGBA16161616);
823 //      colormodels.append(new ColormodelItem(string, BC_RGBA16161616));
824         BC_CModels::to_text(string, BC_RGB_FLOAT);
825         colormodels.append(new ColormodelItem(string, BC_RGB_FLOAT));
826         BC_CModels::to_text(string, BC_RGBA_FLOAT);
827         colormodels.append(new ColormodelItem(string, BC_RGBA_FLOAT));
828         BC_CModels::to_text(string, BC_YUV888);
829         colormodels.append(new ColormodelItem(string, BC_YUV888));
830         BC_CModels::to_text(string, BC_YUVA8888);
831         colormodels.append(new ColormodelItem(string, BC_YUVA8888));
832 //      BC_CModels::to_text(string, BC_YUV161616);
833 //      colormodels.append(new ColormodelItem(string, BC_YUV161616));
834 //      BC_CModels::to_text(string, BC_YUVA16161616);
835 //      colormodels.append(new ColormodelItem(string, BC_YUVA16161616));
836 }
837
838 void MWindow::init_indexes()
839 {
840         mainindexes = new MainIndexes(this);
841         mainindexes->start_loop();
842 }
843
844 void MWindow::init_gui()
845 {
846         gui = new MWindowGUI(this);
847         gui->lock_window("MWindow::init_gui");
848         gui->create_objects();
849         gui->unlock_window();
850         gui->load_defaults(defaults);
851 }
852
853 void MWindow::init_signals()
854 {
855         sighandler = new SigHandler;
856         sighandler->initialize();
857 ENABLE_BUFFER
858 }
859
860 void MWindow::init_render()
861 {
862         render = new Render(this);
863         create_bd = new CreateBD_Thread(this);
864         create_dvd = new CreateDVD_Thread(this);
865         batch_render = new BatchRenderThread(this);
866 }
867
868 void MWindow::init_brender()
869 {
870         if(preferences->use_brender && !brender)
871         {
872                 brender_lock->lock("MWindow::init_brender 1");
873                 brender = new BRender(this);
874                 brender->initialize();
875                 session->brender_end = 0;
876                 brender_lock->unlock();
877         }
878         else
879         if(!preferences->use_brender && brender)
880         {
881                 brender_lock->lock("MWindow::init_brender 2");
882                 delete brender;
883                 brender = 0;
884                 session->brender_end = 0;
885                 brender_lock->unlock();
886         }
887         if(brender) brender->restart(edl);
888 }
889
890 void MWindow::restart_brender()
891 {
892 //printf("MWindow::restart_brender 1\n");
893         if(brender) brender->restart(edl);
894 }
895
896 void MWindow::stop_brender()
897 {
898         if(brender) brender->stop();
899 }
900
901 int MWindow::brender_available(int position)
902 {
903         int result = 0;
904         brender_lock->lock("MWindow::brender_available 1");
905         if(brender)
906         {
907                 if(brender->map_valid)
908                 {
909                         brender->map_lock->lock("MWindow::brender_available 2");
910                         if(position < brender->map_size &&
911                                 position >= 0)
912                         {
913 //printf("MWindow::brender_available 1 %d %d\n", position, brender->map[position]);
914                                 if(brender->map[position] == BRender::RENDERED)
915                                         result = 1;
916                         }
917                         brender->map_lock->unlock();
918                 }
919         }
920         brender_lock->unlock();
921         return result;
922 }
923
924 void MWindow::set_brender_start()
925 {
926         edl->session->brender_start = edl->local_session->get_selectionstart(1);
927         restart_brender();
928         gui->draw_overlays(1);
929 }
930
931
932 int MWindow::has_commercials()
933 {
934         return theme->use_commercials;
935 }
936
937 void MWindow::init_commercials()
938 {
939         if( !commercials ) {
940                 commercials = new Commercials(this);
941                 commercial_active = 0;
942         }
943         else
944                 commercials->add_user();
945 }
946
947 void MWindow::commit_commercial()
948 {
949         if( !commercial_active ) return;
950         commercial_active = 0;
951         if( !commercials ) return;
952         commercials->commitDb();
953 }
954
955 void MWindow::undo_commercial()
956 {
957         if( !commercial_active ) return;
958         commercial_active = 0;
959         if( !commercials ) return;
960         commercials->undoDb();
961 }
962
963 int MWindow::put_commercial()
964 {
965         double start = edl->local_session->get_selectionstart();
966         double end = edl->local_session->get_selectionend();
967         if( start >= end ) return 0;
968
969         const char *errmsg = 0;
970         int count = 0;
971         Tracks *tracks = edl->tracks;
972         int result = 0;
973         //check it
974         for(Track *track=tracks->first; track && !errmsg; track=track->next) {
975                 if( track->data_type != TRACK_VIDEO ) continue;
976                 if( !track->record ) continue;
977                 if( count > 0 ) { errmsg = _("multiple video tracks"); break; }
978                 ++count;
979                 int64_t units_start = track->to_units(start,0);
980                 int64_t units_end = track->to_units(end,0);
981                 Edits *edits = track->edits;
982                 Edit *edit1 = edits->editof(units_start, PLAY_FORWARD, 0);
983                 Edit *edit2 = edits->editof(units_end, PLAY_FORWARD, 0);
984                 if(!edit1 && !edit2) continue;  // nothing selected
985                 if(!edit2) {                    // edit2 beyond end of track
986                         edit2 = edits->last;
987                         units_end = edits->length();
988                 }
989                 if(edit1 != edit2) { errmsg = _("crosses edits"); break; }
990                 Indexable *indexable = edit1->get_source();
991                 if( !indexable->is_asset ) { errmsg = _("not asset"); break; }
992         }
993         //run it
994         for(Track *track=tracks->first; track && !errmsg; track=track->next) {
995                 if( track->data_type != TRACK_VIDEO ) continue;
996                 if( !track->record ) continue;
997                 int64_t units_start = track->to_units(start,0);
998                 int64_t units_end = track->to_units(end,0);
999                 Edits *edits = track->edits;
1000                 Edit *edit1 = edits->editof(units_start, PLAY_FORWARD, 0);
1001                 Edit *edit2 = edits->editof(units_end, PLAY_FORWARD, 0);
1002                 if(!edit1 && !edit2) continue;  // nothing selected
1003                 if(!edit2) {                    // edit2 beyond end of track
1004                         edit2 = edits->last;
1005                         units_end = edits->length();
1006                 }
1007                 Indexable *indexable = edit1->get_source();
1008                 Asset *asset = (Asset *)indexable;
1009                 File *file = video_cache->check_out(asset, edl);
1010                 if( !file ) { errmsg = _("no file"); break; }
1011                 int64_t edit_length = units_end - units_start;
1012                 int64_t edit_start = units_start - edit1->startproject + edit1->startsource;
1013                 result = commercials->put_clip(file, edit1->channel,
1014                         track->from_units(edit_start), track->from_units(edit_length));
1015                 video_cache->check_in(asset);
1016                 if( result ) { errmsg = _("db failed"); break; }
1017         }
1018         if( errmsg ) {
1019                 char string[BCTEXTLEN];
1020                 sprintf(string, _("put_commercial: %s"), errmsg);
1021                 MainError::show_error(string);
1022                 undo_commercial();
1023                 result = 1;
1024         }
1025         return result;
1026 }
1027
1028 void MWindow::stop_playback(int wait)
1029 {
1030         int locked  = gui->get_window_lock();
1031         if( locked ) gui->unlock_window();
1032
1033         cwindow->playback_engine->que->send_command(STOP,
1034                 CHANGE_NONE, 
1035                 0,
1036                 0);
1037         cwindow->playback_engine->interrupt_playback(wait);
1038
1039         for(int i = 0; i < vwindows.size(); i++) {
1040                 VWindow *vwindow = vwindows.get(i);
1041                 if( !vwindow->is_running() ) continue;
1042                 vwindow->playback_engine->que->send_command(STOP, CHANGE_NONE, 0, 0);
1043                 vwindow->playback_engine->interrupt_playback(wait);
1044         }
1045         if( locked ) gui->lock_window("MWindow::stop_playback");
1046 }
1047
1048 int MWindow::load_filenames(ArrayList<char*> *filenames, 
1049         int load_mode,
1050         int update_filename)
1051 {
1052         ArrayList<EDL*> new_edls;
1053         ArrayList<Asset*> new_assets;
1054         ArrayList<File*> new_files;
1055         const int debug = 0;
1056 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1057
1058 //      save_defaults();
1059         gui->start_hourglass();
1060
1061 // Need to stop playback since tracking depends on the EDL not getting
1062 // deleted.
1063         stop_playback(1);
1064
1065         undo->update_undo_before();
1066
1067
1068 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1069
1070 // Define new_edls and new_assets to load
1071         int result = 0, ftype = -1;
1072         for(int i = 0; i < filenames->total; i++)
1073         {
1074 // Get type of file
1075                 File *new_file = new File;
1076                 Asset *new_asset = new Asset(filenames->values[i]);
1077                 EDL *new_edl = new EDL;
1078                 char string[BCTEXTLEN];
1079
1080                 new_edl->create_objects();
1081                 new_edl->copy_session(edl);
1082                 new_file->set_program(edl->session->program_no);
1083
1084                 sprintf(string, _("Loading %s"), new_asset->path);
1085                 gui->show_message(string);
1086 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1087
1088                 ftype = new_file->open_file(preferences, new_asset, 1, 0);
1089 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1090
1091                 result = 1;
1092                 switch(ftype)
1093                 {
1094 // Convert media file to EDL
1095                         case FILE_OK:
1096 // Warn about odd image dimensions
1097                                 if(new_asset->video_data &&
1098                                         ((new_asset->width % 2) ||
1099                                         (new_asset->height % 2)))
1100                                 {
1101                                         char string[BCTEXTLEN];
1102                                         sprintf(string, _("%s's resolution is %dx%d.\nImages with odd dimensions may not decode properly."),
1103                                                 new_asset->path,
1104                                                 new_asset->width,
1105                                                 new_asset->height);
1106                                         MainError::show_error(string);
1107                                 }
1108
1109                                 if(new_asset->program >= 0 &&
1110                                         edl->session->program_no != new_asset->program)
1111                                 {
1112                                         char string[BCTEXTLEN];
1113                                         sprintf(string, _("%s's index was built for program number %d\n"
1114                                                 "Playback preference is %d.\n  Using program %d."),
1115                                                 new_asset->path, new_asset->program,
1116                                                 edl->session->program_no, new_asset->program);
1117                                         MainError::show_error(string);
1118                                 }
1119
1120
1121                                 if(load_mode != LOADMODE_RESOURCESONLY)
1122                                 {
1123 SET_TRACE
1124                                         RecordLabels *labels = edl->session->label_cells ?
1125                                                 new RecordLabels(new_file) : 0;
1126 SET_TRACE
1127                                         asset_to_edl(new_edl, new_asset, labels);
1128 SET_TRACE
1129                                         new_edls.append(new_edl);
1130 SET_TRACE
1131                                         new_asset->Garbage::remove_user();
1132                                         delete labels;
1133                                         new_asset = 0;
1134                                 }
1135                                 else
1136                                 {
1137                                         new_assets.append(new_asset);
1138                                         new_asset = 0;
1139                                 }
1140
1141 // Set filename to nothing for assets since save EDL would overwrite them.
1142                                 if(load_mode == LOADMODE_REPLACE || 
1143                                         load_mode == LOADMODE_REPLACE_CONCATENATE)
1144                                 {
1145                                         set_filename("");
1146 // Reset timeline position
1147                                         for(int i = 0; i < TOTAL_PANES; i++)
1148                                         {
1149                                                 new_edl->local_session->view_start[i] = 0;
1150                                                 new_edl->local_session->track_start[i] = 0;
1151                                         }
1152                                 }
1153
1154                                 result = 0;
1155                                 break;
1156
1157 // File not found
1158                         case FILE_NOT_FOUND:
1159                                 sprintf(string, _("Failed to open %s"), new_asset->path);
1160                                 gui->show_message(string, theme->message_error);
1161                                 break;
1162
1163 // Unknown format
1164                         case FILE_UNRECOGNIZED_CODEC:
1165                         {
1166 // Test index file
1167                                 IndexFile indexfile(this, new_asset);
1168                                 result = indexfile.open_index();
1169                                 if(!result)
1170                                 {
1171                                         indexfile.close_index();
1172                                 }
1173
1174 // Test existing EDLs
1175                                 if(result)
1176                                 {
1177                                         for(int j = 0; j < new_edls.total + 1; j++)
1178                                         {
1179                                                 Asset *old_asset;
1180                                                 if(j == new_edls.total)
1181                                                 {
1182                                                         old_asset = edl->assets->get_asset(new_asset->path);
1183                                                         if( old_asset )
1184                                                         {
1185                                                                 *new_asset = *old_asset;
1186                                                                 result = 0;
1187                                                         }
1188                                                 }
1189                                                 else
1190                                                 {
1191                                                         old_asset = new_edls.values[j]->assets->get_asset(new_asset->path);
1192                                                         if( old_asset )
1193                                                         {
1194                                                                 *new_asset = *old_asset;
1195                                                                 result = 0;
1196                                                         }
1197                                                 }
1198                                         }
1199                                 }
1200
1201 // Prompt user
1202                                 if(result)
1203                                 {
1204                                         char string[BCTEXTLEN];
1205                                         FileSystem fs;
1206                                         fs.extract_name(string, new_asset->path);
1207
1208                                         strcat(string, _("'s format couldn't be determined."));
1209                                         new_asset->audio_data = 1;
1210                                         new_asset->format = FILE_PCM;
1211                                         new_asset->channels = defaults->get("AUDIO_CHANNELS", 2);
1212                                         new_asset->sample_rate = defaults->get("SAMPLE_RATE", 44100);
1213                                         new_asset->bits = defaults->get("AUDIO_BITS", 16);
1214                                         new_asset->byte_order = defaults->get("BYTE_ORDER", 1);
1215                                         new_asset->signed_ = defaults->get("SIGNED_", 1);
1216                                         new_asset->header = defaults->get("HEADER", 0);
1217
1218                                         FileFormat fwindow(this);
1219                                         fwindow.create_objects(new_asset, string);
1220                                         result = fwindow.run_window();
1221
1222
1223                                         defaults->update("AUDIO_CHANNELS", new_asset->channels);
1224                                         defaults->update("SAMPLE_RATE", new_asset->sample_rate);
1225                                         defaults->update("AUDIO_BITS", new_asset->bits);
1226                                         defaults->update("BYTE_ORDER", new_asset->byte_order);
1227                                         defaults->update("SIGNED_", new_asset->signed_);
1228                                         defaults->update("HEADER", new_asset->header);
1229                                         save_defaults();
1230                                 }
1231
1232 // Append to list
1233                                 if(!result)
1234                                 {
1235 // Recalculate length
1236                                         delete new_file;
1237                                         new_file = new File;
1238                                         result = new_file->open_file(preferences, new_asset, 1, 0);
1239
1240                                         if(load_mode != LOADMODE_RESOURCESONLY)
1241                                         {
1242                                                 RecordLabels *labels = edl->session->label_cells ?
1243                                                         new RecordLabels(new_file) : 0;
1244                                                 asset_to_edl(new_edl, new_asset, labels);
1245                                                 new_edls.append(new_edl);
1246                                                 new_asset->Garbage::remove_user();
1247                                                 delete labels;
1248                                                 new_asset = 0;
1249                                         }
1250                                         else
1251                                         {
1252                                                 new_assets.append(new_asset);
1253                                                 new_asset = 0;
1254                                         }
1255                                 }
1256                                 else
1257                                 {
1258                                         result = 1;
1259                                 }
1260                                 break;
1261                         }
1262
1263                         case FILE_IS_XML:
1264                         {
1265                                 FileXML xml_file;
1266 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1267                                 xml_file.read_from_file(filenames->values[i]);
1268 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1269
1270                                 if(load_mode == LOADMODE_NESTED)
1271                                 {
1272 // Load temporary EDL for nesting.
1273                                         EDL *nested_edl = new EDL;
1274                                         nested_edl->create_objects();
1275                                         nested_edl->set_path(filenames->values[i]);
1276                                         nested_edl->load_xml(&xml_file, LOAD_ALL);
1277 //printf("MWindow::load_filenames %p %s\n", nested_edl, nested_edl->project_path);
1278                                         edl_to_nested(new_edl, nested_edl);
1279                                         nested_edl->Garbage::remove_user();
1280                                 }
1281                                 else
1282                                 {
1283 // Load EDL for pasting
1284                                         new_edl->load_xml(&xml_file, LOAD_ALL);
1285 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1286                                         test_plugins(new_edl, filenames->values[i]);
1287 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1288
1289                                         if(load_mode == LOADMODE_REPLACE || 
1290                                                 load_mode == LOADMODE_REPLACE_CONCATENATE)
1291                                         {
1292                                                 strcpy(session->filename, filenames->values[i]);
1293                                                 strcpy(new_edl->local_session->clip_title, 
1294                                                         filenames->values[i]);
1295                                                 if(update_filename)
1296                                                         set_filename(new_edl->local_session->clip_title);
1297                                         }
1298                                 }               
1299
1300                                 new_edls.append(new_edl);
1301                                 result = 0;
1302                                 break;
1303                         }
1304                 }
1305
1306 // edls are in new_edls
1307                 if( result && new_edl ) new_edl->Garbage::remove_user();
1308 // assets are copied
1309                 if( new_asset ) new_asset->Garbage::remove_user();
1310
1311 // Store for testing index
1312                 new_files.append(new_file);
1313         }
1314
1315 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1316
1317
1318         if(!result) gui->statusbar->default_message();
1319
1320
1321
1322
1323
1324
1325
1326 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1327
1328 // Paste them.
1329 // Don't back up here.
1330         if(new_edls.size())
1331         {
1332 // For pasting, clear the active region
1333                 if(load_mode == LOADMODE_PASTE ||
1334                         load_mode == LOADMODE_NESTED)
1335                 {
1336                         double start = edl->local_session->get_selectionstart();
1337                         double end = edl->local_session->get_selectionend();
1338                         if(!EQUIV(start, end))
1339                                 edl->clear(start, 
1340                                         end,
1341                                         edl->session->labels_follow_edits,
1342                                         edl->session->plugins_follow_edits,
1343                                         edl->session->autos_follow_edits);
1344                 }
1345
1346                 paste_edls(&new_edls, 
1347                         load_mode,
1348                         0,
1349                         -1,
1350                         edl->session->labels_follow_edits, 
1351                         edl->session->plugins_follow_edits,
1352                         edl->session->autos_follow_edits);
1353         }
1354
1355
1356
1357
1358 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1359
1360 // Add new assets to EDL and schedule assets for index building.
1361         int got_indexes = 0;
1362         for(int i = 0; i < new_edls.size(); i++)
1363         {
1364                 EDL *new_edl = new_edls.get(i);
1365                 for(int j = 0; j < new_edl->nested_edls->size(); j++)
1366                 {
1367                         mainindexes->add_next_asset(0, 
1368                                 new_edl->nested_edls->get(j));
1369                         got_indexes = 1;
1370                         edl->nested_edls->update_index(new_edl->nested_edls->get(j));
1371                 }
1372
1373         }
1374
1375 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1376         if(new_assets.size())
1377         {
1378                 for(int i = 0; i < new_assets.size(); i++)
1379                 {
1380                         Asset *new_asset = new_assets.get(i);
1381
1382                         File *new_file = 0;
1383                         int got_it = 0;
1384                         for(int j = 0; j < new_files.size(); j++)
1385                         {
1386                                 new_file = new_files.get(j);
1387                                 if(!strcmp(new_file->asset->path,
1388                                         new_asset->path))
1389                                 {
1390                                         got_it = 1;
1391                                         break;
1392                                 }
1393                         }
1394
1395                         mainindexes->add_next_asset(got_it ? new_file : 0, new_asset);
1396                         got_indexes = 1;
1397                         edl->assets->update(new_asset);
1398
1399                 }
1400
1401
1402         }
1403 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1404
1405 // Start examining next batch of index files
1406         if(got_indexes) mainindexes->start_build();
1407 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1408
1409 // Open plugin GUIs
1410         Track *track = edl->tracks->first;
1411         while(track)
1412         {
1413                 for(int j = 0; j < track->plugin_set.size(); j++)
1414                 {
1415                         PluginSet *plugins = track->plugin_set.get(j);
1416                         Plugin *plugin = plugins->get_first_plugin();
1417
1418                         while(plugin)
1419                         {
1420                                 if(load_mode == LOADMODE_REPLACE ||
1421                                         load_mode == LOADMODE_REPLACE_CONCATENATE)
1422                                 {
1423                                         if(plugin->plugin_type == PLUGIN_STANDALONE &&
1424                                                 plugin->show)
1425                                         {
1426                                                 show_plugin(plugin);
1427                                         }
1428                                 }
1429                                 else
1430                                 {
1431                                         plugin->show = 0;
1432                                 }
1433
1434                                 plugin = (Plugin*)plugin->next;
1435                         }
1436                 }
1437
1438                 track = track->next;
1439         }
1440
1441         // if just opening one new resource in replace mode
1442         if( load_mode == LOADMODE_REPLACE && new_edls.size() == 1 &&
1443                 ftype != FILE_IS_XML )
1444         {
1445                 select_asset(0, 0);
1446                 edl->local_session->preview_start = 0;
1447                 edl->local_session->preview_end = edl->tracks->total_playable_length();
1448                 edl->local_session->loop_playback = 0;
1449                 edl->local_session->set_selectionstart(0);
1450                 edl->local_session->set_selectionend(0);
1451                 fit_selection();
1452                 goto_start();
1453         }
1454
1455
1456         update_project(load_mode);
1457
1458 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1459
1460         for(int i = 0; i < new_edls.size(); i++)
1461         {
1462                 new_edls.get(i)->remove_user();
1463         }
1464 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1465
1466         new_edls.remove_all();
1467
1468         for(int i = 0; i < new_assets.size(); i++)
1469         {
1470                 new_assets.get(i)->Garbage::remove_user();
1471         }
1472
1473         new_assets.remove_all();
1474         new_files.remove_all_objects();
1475
1476         undo->update_undo_after(_("load"), LOAD_ALL);
1477
1478         if(load_mode == LOADMODE_REPLACE ||
1479                 load_mode == LOADMODE_REPLACE_CONCATENATE)
1480         {
1481                 session->changes_made = 0;
1482         }
1483         else
1484         {
1485                 session->changes_made = 1;
1486         }
1487
1488         gui->stop_hourglass();
1489
1490
1491 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
1492         return 0;
1493 }
1494
1495
1496
1497
1498 void MWindow::test_plugins(EDL *new_edl, char *path)
1499 {
1500         char string[BCTEXTLEN];
1501
1502 // Do a check weather plugins exist
1503         for(Track *track = new_edl->tracks->first; track; track = track->next)
1504         {
1505                 for(int k = 0; k < track->plugin_set.total; k++)
1506                 {
1507                         PluginSet *plugin_set = track->plugin_set.values[k];
1508                         for(Plugin *plugin = (Plugin*)plugin_set->first; 
1509                                 plugin; 
1510                                 plugin = (Plugin*)plugin->next)
1511                         {
1512                                 if(plugin->plugin_type == PLUGIN_STANDALONE)
1513                                 {
1514 // ok we need to find it in plugindb
1515                                         int plugin_found = 0;
1516
1517                                         for(int j = 0; j < plugindb->size(); j++)
1518                                         {
1519                                                 PluginServer *server = plugindb->get(j);
1520                                                 if(server->title &&
1521                                                         !strcasecmp(server->title, plugin->title) &&
1522                                                         ((track->data_type == TRACK_AUDIO && server->audio) ||
1523                                                         (track->data_type == TRACK_VIDEO && server->video)) &&
1524                                                         (!server->transition))
1525                                                         plugin_found = 1;
1526                                         }
1527
1528                                         if (!plugin_found) 
1529                                         {
1530                                                 sprintf(string, 
1531         _("The %s '%s' in file '%s' is not part of your installation of Cinelerra.\n"
1532           "The project won't be rendered as it was meant and Cinelerra might crash.\n"),
1533                                                         "effect", plugin->title, 
1534                                                         path); 
1535                                                 MainError::show_error(string);
1536                                         }
1537                                 }
1538                         }
1539                 }
1540
1541                 for(Edit *edit = (Edit*)track->edits->first; 
1542                         edit; 
1543                         edit = (Edit*)edit->next)
1544                 {
1545                         if (edit->transition)
1546                         {
1547 // ok we need to find transition in plugindb
1548
1549                                 int transition_found = 0;
1550                                 for(int j = 0; j < plugindb->size(); j++)
1551                                 {
1552                                         PluginServer *server = plugindb->get(j);
1553                                         if(server->title &&
1554                                                 !strcasecmp(server->title, edit->transition->title) &&
1555                                                 ((track->data_type == TRACK_AUDIO && server->audio) ||
1556                                                 (track->data_type == TRACK_VIDEO && server->video)) &&
1557                                                 (server->transition))
1558                                                 transition_found = 1;
1559                                 }
1560
1561                                 if (!transition_found) 
1562                                 {
1563                                         sprintf(string, 
1564         _("The %s '%s' in file '%s' is not part of your installation of Cinelerra.\n"
1565           "The project won't be rendered as it was meant and Cinelerra might crash.\n"),
1566                                                 "transition", edit->transition->title, 
1567                                                 path); 
1568                                         MainError::show_error(string);
1569                                 }
1570                         }
1571                 }
1572         }
1573 }
1574
1575
1576 void MWindow::init_shm()
1577 {
1578 // Fix shared memory
1579         FILE *fd = fopen("/proc/sys/kernel/shmmax", "w");
1580         if(fd) {
1581                 fprintf(fd, "0x7fffffff");
1582                 fclose(fd);
1583         }
1584         fd = 0;
1585
1586         fd = fopen("/proc/sys/kernel/shmmax", "r");
1587         if(!fd) {
1588                 MainError::show_error("MWindow::init_shm: couldn't open /proc/sys/kernel/shmmax for reading.\n");
1589                 return;
1590         }
1591
1592         int64_t result = 0;
1593         fscanf(fd, _LD, &result);
1594         fclose(fd);
1595         fd = 0;
1596         if(result < 0x7fffffff) {
1597                 char string[BCTEXTLEN];
1598                 sprintf(string, _("MWindow::init_shm: /proc/sys/kernel/shmmax is 0x" _LX ".\n"
1599                         "you probably need to be root, or:\n"
1600                         "as root, run: echo 0x7fffffff > /proc/sys/kernel/shmmax\n"
1601                         "before trying to start cinelerra.\n"
1602                         "It should be at least 0x7fffffff for Cinelerra.\n"), result);
1603                 MainError::show_error(string);
1604         }
1605 }
1606
1607
1608 void MWindow::create_objects(int want_gui, 
1609         int want_new,
1610         char *config_path)
1611 {
1612         FileSystem fs;
1613         const int debug = 0;
1614         if(debug) PRINT_TRACE
1615
1616 // For some reason, init_signals must come after show_splash or the signals won't
1617 // get trapped.
1618         init_signals();
1619         if(debug) PRINT_TRACE
1620         init_3d();
1621
1622         if(debug) PRINT_TRACE
1623 //      show_splash();
1624
1625         if(debug) PRINT_TRACE
1626         init_defaults(defaults, config_path);
1627         init_preferences();
1628         init_plugins(this, preferences);
1629         if(debug) PRINT_TRACE
1630         if(splash_window) splash_window->operation->update(_("Initializing GUI"));
1631         if(debug) PRINT_TRACE
1632         init_theme();
1633         if(debug) PRINT_TRACE
1634         init_error();
1635
1636         char string[BCTEXTLEN];
1637         strcpy(string, preferences->plugin_dir);
1638         strcat(string, "/fonts");
1639         BC_Resources::init_fontconfig(string);
1640         if(debug) PRINT_TRACE
1641
1642 // Initialize before too much else is running
1643 // Preferences & theme are required for building MPEG table of contents
1644
1645 // Default project created here
1646         init_edl();
1647         if(debug) PRINT_TRACE
1648
1649         init_cache();
1650         if(debug) PRINT_TRACE
1651
1652         Timer timer;
1653
1654         init_compositor();
1655         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1656
1657 //printf("MWindow::create_objects %d session->show_vwindow=%d\n", __LINE__, session->show_vwindow);
1658         if(session->show_vwindow)
1659                 get_viewer(1, DEFAULT_VWINDOW);
1660         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1661
1662         init_gui();
1663         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1664
1665         init_awindow();
1666         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1667
1668         init_levelwindow();
1669         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1670
1671         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1672
1673         init_indexes();
1674         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1675
1676         init_channeldb();
1677
1678         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1679         init_gwindow();
1680         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1681         init_render();
1682         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1683         init_brender();
1684         init_commercials();
1685         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1686         mainprogress = new MainProgress(this, gui);
1687         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1688         undo = new MainUndo(this);
1689
1690         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1691
1692         plugin_guis = new ArrayList<PluginServer*>;
1693         dead_plugins = new ArrayList<PluginServer*>;
1694         keyframe_threads = new ArrayList<KeyFrameThread*>;
1695
1696         if(debug) printf("MWindow::create_objects %d vwindows=%d show_vwindow=%d\n", 
1697                 __LINE__, 
1698                 vwindows.size(),
1699                 session->show_vwindow);
1700
1701 // Show all vwindows
1702 //      if(session->show_vwindow) {
1703 //              for(int j = 0; j < vwindows.size(); j++) {
1704 //                      VWindow *vwindow = vwindows.get(j);
1705 //                      if( !vwindow->is_running() ) continue;
1706 //                      if(debug) printf("MWindow::create_objects %d vwindow=%p\n", 
1707 //                              __LINE__, 
1708 //                              vwindow);
1709 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1710 //                      vwindow->gui->lock_window("MWindow::create_objects 1");
1711 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1712 //                      vwindow->gui->show_window();
1713 //                      if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1714 //                      vwindow->gui->unlock_window();
1715 //              }
1716 //      }
1717
1718
1719         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1720
1721         if(session->show_cwindow) 
1722         {
1723                 cwindow->gui->lock_window("MWindow::create_objects 1");
1724                 cwindow->gui->show_window();
1725                 cwindow->gui->unlock_window();
1726         }
1727
1728         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1729         if(session->show_awindow)
1730         {
1731                 awindow->gui->lock_window("MWindow::create_objects 1");
1732                 awindow->gui->show_window();
1733                 awindow->gui->unlock_window();
1734         }
1735
1736         if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
1737         if(session->show_lwindow)
1738         {
1739                 lwindow->gui->lock_window("MWindow::create_objects 1");
1740                 lwindow->gui->show_window();
1741                 lwindow->gui->unlock_window();
1742         }
1743
1744         if(debug) printf("MWindow::create_objects %d total_time=%d gwindow=%p\n", 
1745                 __LINE__, 
1746                 (int)timer.get_difference(),
1747                 gwindow->gui);
1748         if(session->show_gwindow)
1749         {
1750                 gwindow->gui->lock_window("MWindow::create_objects 1");
1751                 gwindow->gui->show_window();
1752                 gwindow->gui->unlock_window();
1753         }
1754
1755         if(debug) PRINT_TRACE
1756
1757         gui->lock_window("MWindow::create_objects 1");
1758         gui->mainmenu->load_defaults(defaults);
1759         gui->mainmenu->update_toggles(0);
1760         gui->update_patchbay();
1761         gui->draw_canvas(0, 0);
1762         gui->draw_cursor(1);
1763         gui->show_window();
1764         gui->raise_window();
1765         gui->unlock_window();
1766
1767         if(debug) PRINT_TRACE
1768
1769
1770
1771         if(preferences->use_tipwindow)
1772                 init_tipwindow();
1773         if(debug) PRINT_TRACE
1774
1775         gui->add_keyboard_listener(
1776                 (int (BC_WindowBase::*)(BC_WindowBase *))
1777                 &MWindowGUI::keyboard_listener);
1778
1779         hide_splash();
1780         init_shm();
1781         if(debug) PRINT_TRACE
1782
1783         BC_WindowBase::get_resources()->vframe_shm = 1;
1784
1785 }
1786
1787
1788 void MWindow::show_splash()
1789 {
1790 #include "data/heroine_logo11_png.h"
1791         VFrame *frame = new VFrame(heroine_logo11_png);
1792         BC_DisplayInfo display_info;
1793         splash_window = new SplashGUI(frame, 
1794                 display_info.get_root_w() / 2 - frame->get_w() / 2,
1795                 display_info.get_root_h() / 2 - frame->get_h() / 2);
1796         splash_window->create_objects();
1797 }
1798
1799 void MWindow::hide_splash()
1800 {
1801         if(splash_window)
1802                 delete splash_window;
1803         splash_window = 0;
1804 }
1805
1806
1807 void MWindow::start()
1808 {
1809 ENABLE_BUFFER
1810 //PRINT_TRACE
1811 //      vwindows.get(DEFAULT_VWINDOW)->start();
1812         awindow->start();
1813 //PRINT_TRACE
1814         cwindow->start();
1815 //PRINT_TRACE
1816         lwindow->start();
1817 //PRINT_TRACE
1818         gwindow->start();
1819 //PRINT_TRACE
1820 //      Thread::start();
1821 //PRINT_TRACE
1822         playback_3d->start();
1823 //PRINT_TRACE
1824 }
1825
1826 void MWindow::run()
1827 {
1828         gui->run_window();
1829 }
1830
1831 void MWindow::show_vwindow()
1832 {
1833         int total_running = 0;
1834         session->show_vwindow = 1;
1835
1836 // Raise all windows which are visible
1837         for(int j = 0; j < vwindows.size(); j++) {
1838                 VWindow *vwindow = vwindows.get(j);
1839                 if( !vwindow->is_running() ) continue;
1840                 vwindow->gui->lock_window("MWindow::show_vwindow");
1841                 vwindow->gui->show_window(0);
1842                 vwindow->gui->raise_window();
1843                 vwindow->gui->flush();
1844                 vwindow->gui->unlock_window();
1845                 total_running++;
1846         }
1847
1848 //printf("MWindow::show_vwindow %d %d\n", __LINE__, vwindows.size());
1849 // If no windows visible
1850         if(!total_running)
1851         {
1852                 get_viewer(1, DEFAULT_VWINDOW);
1853         }
1854
1855         gui->mainmenu->show_vwindow->set_checked(1);
1856 }
1857
1858 void MWindow::show_awindow()
1859 {
1860         session->show_awindow = 1;
1861         awindow->gui->lock_window("MWindow::show_awindow");
1862         awindow->gui->show_window();
1863         awindow->gui->raise_window();
1864         awindow->gui->flush();
1865         awindow->gui->unlock_window();
1866         gui->mainmenu->show_awindow->set_checked(1);
1867 }
1868
1869 char *MWindow::get_cwindow_display()
1870 {
1871         char *x11_host = screens < 2 || session->window_config == 0 ?
1872                 session->a_x11_host : session->b_x11_host;
1873         return *x11_host ? x11_host : 0;
1874 }
1875
1876 void MWindow::show_cwindow()
1877 {
1878         session->show_cwindow = 1;
1879         cwindow->show_window();
1880         gui->mainmenu->show_cwindow->set_checked(1);
1881 }
1882
1883 void MWindow::show_gwindow()
1884 {
1885         session->show_gwindow = 1;
1886
1887         gwindow->gui->lock_window("MWindow::show_gwindow");
1888         gwindow->gui->show_window();
1889         gwindow->gui->raise_window();
1890         gwindow->gui->flush();
1891         gwindow->gui->unlock_window();
1892
1893         gui->mainmenu->show_gwindow->set_checked(1);
1894 }
1895
1896 void MWindow::show_lwindow()
1897 {
1898         session->show_lwindow = 1;
1899         lwindow->gui->lock_window("MWindow::show_lwindow");
1900         lwindow->gui->show_window();
1901         lwindow->gui->raise_window();
1902         lwindow->gui->flush();
1903         lwindow->gui->unlock_window();
1904         gui->mainmenu->show_lwindow->set_checked(1);
1905 }
1906
1907 int MWindow::tile_windows(int window_config)
1908 {
1909         int need_reload = 0;
1910         int play_config = window_config==0 ? 0 : 1;
1911         edl->session->playback_config->load_defaults(defaults, play_config);
1912         session->default_window_positions(window_config);
1913         if( screens == 1 ) {
1914                 gui->default_positions();
1915                 sync_parameters(CHANGE_ALL);
1916         }
1917         else
1918                 need_reload = 1;
1919         return need_reload;
1920 }
1921
1922 void MWindow::toggle_loop_playback()
1923 {
1924         edl->local_session->loop_playback = !edl->local_session->loop_playback;
1925         set_loop_boundaries();
1926         save_backup();
1927
1928         gui->draw_overlays(1);
1929         sync_parameters(CHANGE_PARAMS);
1930 }
1931
1932 //void MWindow::set_titles(int value)
1933 //{
1934 //      edl->session->show_titles = value;
1935 //      trackmovement(edl->local_session->track_start);
1936 //}
1937
1938 void MWindow::set_screens(int value)
1939 {
1940         screens = value;
1941 }
1942
1943 void MWindow::set_auto_keyframes(int value, int lock_mwindow, int lock_cwindow)
1944 {
1945         if(lock_mwindow) gui->lock_window("MWindow::set_auto_keyframes");
1946         edl->session->auto_keyframes = value;
1947         gui->mbuttons->edit_panel->keyframe->update(value);
1948         gui->flush();
1949         if(lock_mwindow) gui->unlock_window();
1950
1951         if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_auto_keyframes");
1952         cwindow->gui->edit_panel->keyframe->update(value);
1953         cwindow->gui->flush();
1954         if(lock_cwindow) cwindow->gui->unlock_window();
1955 }
1956
1957 void MWindow::set_keyframe_type(int mode)
1958 {
1959         gui->lock_window("MWindow::set_keyframe_type");
1960         edl->local_session->floatauto_type = mode;
1961         gui->mainmenu->update_toggles(0);
1962         gui->unlock_window();
1963 }
1964
1965 int MWindow::set_editing_mode(int new_editing_mode, int lock_mwindow, int lock_cwindow)
1966 {
1967         if(lock_mwindow) gui->lock_window("MWindow::set_editing_mode");
1968         edl->session->editing_mode = new_editing_mode;
1969         gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
1970         gui->mbuttons->edit_panel->update();
1971         gui->set_editing_mode(1);
1972         if(lock_mwindow) gui->unlock_window();
1973
1974
1975         if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_editing_mode");
1976         cwindow->gui->edit_panel->update();
1977         cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
1978         if(lock_cwindow) cwindow->gui->unlock_window();
1979         return 0;
1980 }
1981
1982
1983 void MWindow::sync_parameters(int change_type)
1984 {
1985         if( in_destructor ) return;
1986
1987 // Sync engines which are playing back
1988         if(cwindow->playback_engine->is_playing_back)
1989         {
1990                 if(change_type == CHANGE_PARAMS)
1991                 {
1992 // TODO: block keyframes until synchronization is done
1993                         cwindow->playback_engine->sync_parameters(edl);
1994                 }
1995                 else
1996 // Stop and restart
1997                 {
1998                         int command = cwindow->playback_engine->command->command;
1999                         cwindow->playback_engine->que->send_command(STOP,
2000                                 CHANGE_NONE, 
2001                                 0,
2002                                 0);
2003 // Waiting for tracking to finish would make the restart position more
2004 // accurate but it can't lock the window to stop tracking for some reason.
2005 // Not waiting for tracking gives a faster response but restart position is
2006 // only as accurate as the last tracking update.
2007                         cwindow->playback_engine->interrupt_playback(0);
2008                         cwindow->playback_engine->que->send_command(command,
2009                                         change_type, 
2010                                         edl,
2011                                         1,
2012                                         0);
2013                 }
2014         }
2015         else
2016         {
2017                 cwindow->playback_engine->que->send_command(CURRENT_FRAME, 
2018                                                         change_type,
2019                                                         edl,
2020                                                         1);
2021         }
2022 }
2023
2024 void MWindow::age_caches()
2025 {
2026 // printf("MWindow::age_caches %d %lld %lld %lld %lld\n", 
2027 // __LINE__, 
2028 // preferences->cache_size,
2029 // audio_cache->get_memory_usage(1),
2030 // video_cache->get_memory_usage(1),
2031 // frame_cache->get_memory_usage(), 
2032 // wave_cache->get_memory_usage(),
2033 // memory_usage);
2034         frame_cache->age_cache(512);
2035         wave_cache->age_cache(8192);
2036
2037         int64_t memory_usage;
2038         int64_t prev_memory_usage = 0;
2039         int result = 0;
2040         int64_t video_size = (int64_t)(preferences->cache_size * 0.80);
2041         while( !result && prev_memory_usage !=
2042                 (memory_usage=video_cache->get_memory_usage(1)) &&
2043                 memory_usage > video_size ) {
2044                 video_cache->delete_oldest();
2045                 prev_memory_usage = memory_usage;
2046         }
2047
2048         prev_memory_usage = 0;
2049         result = 0;
2050         int64_t audio_size = (int64_t)(preferences->cache_size * 0.20);
2051         while( !result && prev_memory_usage !=
2052                 (memory_usage=audio_cache->get_memory_usage(1)) &&
2053                 memory_usage > audio_size ) {
2054                 audio_cache->delete_oldest();
2055                 prev_memory_usage = memory_usage;
2056         }
2057 }
2058
2059 void MWindow::reset_android_remote()
2060 {
2061         gui->use_android_remote(preferences->android_remote);
2062 }
2063
2064
2065 void MWindow::show_keyframe_gui(Plugin *plugin)
2066 {
2067         keyframe_gui_lock->lock("MWindow::show_keyframe_gui");
2068 // Find existing thread
2069         for(int i = 0; i < keyframe_threads->size(); i++)
2070         {
2071                 if(keyframe_threads->get(i)->plugin == plugin)
2072                 {
2073                         keyframe_threads->get(i)->start_window(plugin, 0);
2074                         keyframe_gui_lock->unlock();
2075                         return;
2076                 }
2077         }
2078
2079 // Find unused thread
2080         for(int i = 0; i < keyframe_threads->size(); i++)
2081         {
2082                 if(!keyframe_threads->get(i)->plugin)
2083                 {
2084                         keyframe_threads->get(i)->start_window(plugin, 0);
2085                         keyframe_gui_lock->unlock();
2086                         return;
2087                 }
2088         }
2089
2090 // Create new thread
2091         KeyFrameThread *thread = new KeyFrameThread(this);
2092         keyframe_threads->append(thread);
2093         thread->start_window(plugin, 0);
2094
2095         keyframe_gui_lock->unlock();
2096 }
2097
2098
2099
2100
2101
2102 void MWindow::show_plugin(Plugin *plugin)
2103 {
2104         int done = 0;
2105
2106 SET_TRACE
2107 // Remove previously deleted plugin GUIs
2108         dead_plugin_lock->lock("MWindow::delete_plugin");
2109         for(int i = 0; i < dead_plugins->size(); i++)
2110         {
2111                 delete dead_plugins->get(i);
2112         }
2113         dead_plugins->remove_all();
2114         dead_plugin_lock->unlock();
2115
2116 //printf("MWindow::show_plugin %d\n", __LINE__);
2117 SET_TRACE
2118
2119
2120         plugin_gui_lock->lock("MWindow::show_plugin");
2121         for(int i = 0; i < plugin_guis->total; i++)
2122         {
2123 // Pointer comparison
2124                 if(plugin_guis->values[i]->plugin == plugin)
2125                 {
2126                         plugin_guis->values[i]->raise_window();
2127                         done = 1;
2128                         break;
2129                 }
2130         }
2131 SET_TRACE
2132
2133 //printf("MWindow::show_plugin 1\n");
2134         if(!done)
2135         {
2136                 if(!plugin->track)
2137                 {
2138                         printf("MWindow::show_plugin track not defined.\n");
2139                 }
2140                 PluginServer *server = scan_plugindb(plugin->title,
2141                         plugin->track->data_type);
2142
2143 //printf("MWindow::show_plugin %p %d\n", server, server->uses_gui);
2144                 if(server && server->uses_gui)
2145                 {
2146                         PluginServer *gui = plugin_guis->append(new PluginServer(*server));
2147 // Needs mwindow to do GUI
2148                         gui->set_mwindow(this);
2149                         gui->open_plugin(0, preferences, edl, plugin);
2150                         gui->show_gui();
2151                         plugin->show = 1;
2152                 }
2153         }
2154         plugin_gui_lock->unlock();
2155 //printf("MWindow::show_plugin %d\n", __LINE__);
2156 SET_TRACE
2157 //sleep(1);
2158 //printf("MWindow::show_plugin 2\n");
2159 }
2160
2161 void MWindow::hide_plugin(Plugin *plugin, int lock)
2162 {
2163         plugin->show = 0;
2164 // Update the toggle
2165         gui->lock_window("MWindow::hide_plugin");
2166         gui->update(0, 1, 0, 0, 0, 0, 0);
2167         gui->unlock_window();
2168
2169         if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
2170         for(int i = 0; i < plugin_guis->total; i++)
2171         {
2172                 if(plugin_guis->values[i]->plugin == plugin)
2173                 {
2174                         PluginServer *ptr = plugin_guis->values[i];
2175                         plugin_guis->remove(ptr);
2176                         if(lock) plugin_gui_lock->unlock();
2177 // Last command executed in client side close
2178 // Schedule for deletion
2179                         ptr->hide_gui();
2180                         delete_plugin(ptr);
2181 //sleep(1);
2182 //                      return;
2183                 }
2184         }
2185         if(lock) plugin_gui_lock->unlock();
2186 }
2187
2188 void MWindow::delete_plugin(PluginServer *plugin)
2189 {
2190         dead_plugin_lock->lock("MWindow::delete_plugin");
2191         dead_plugins->append(plugin);
2192         dead_plugin_lock->unlock();
2193 }
2194
2195 void MWindow::hide_plugins()
2196 {
2197         plugin_gui_lock->lock("MWindow::hide_plugins 1");
2198         while(plugin_guis->size())
2199         {
2200                 PluginServer *ptr = plugin_guis->get(0);
2201                 plugin_guis->remove(ptr);
2202                 plugin_gui_lock->unlock();
2203 // Last command executed in client side close
2204 // Schedule for deletion
2205                 ptr->hide_gui();
2206                 delete_plugin(ptr);
2207                 plugin_gui_lock->lock("MWindow::hide_plugins 2");
2208         }
2209         plugin_gui_lock->unlock();
2210
2211         hide_keyframe_guis();
2212 }
2213
2214 void MWindow::hide_keyframe_guis()
2215 {
2216         keyframe_gui_lock->lock("MWindow::hide_keyframe_guis");
2217         for(int i = 0; i < keyframe_threads->size(); i++)
2218         {
2219                 keyframe_threads->get(i)->close_window();
2220         }
2221         keyframe_gui_lock->unlock();
2222 }
2223
2224 void MWindow::hide_keyframe_gui(Plugin *plugin)
2225 {
2226         keyframe_gui_lock->lock("MWindow::hide_keyframe_gui");
2227         for(int i = 0; i < keyframe_threads->size(); i++)
2228         {
2229                 if(keyframe_threads->get(i)->plugin == plugin)
2230                 {
2231                         keyframe_threads->get(i)->close_window();
2232                         break;
2233                 }
2234         }
2235         keyframe_gui_lock->unlock();
2236 }
2237
2238 void MWindow::update_keyframe_guis()
2239 {
2240 // Send new configuration to keyframe GUI's
2241         keyframe_gui_lock->lock("MWindow::update_keyframe_guis");
2242         for(int i = 0; i < keyframe_threads->size(); i++)
2243         {
2244                 KeyFrameThread *ptr = keyframe_threads->get(i);
2245                 if(edl->tracks->plugin_exists(ptr->plugin))
2246                         ptr->update_gui(1);
2247                 else
2248                 {
2249                         ptr->close_window();
2250                 }
2251         }
2252         keyframe_gui_lock->unlock();
2253 }
2254
2255 void MWindow::update_plugin_guis(int do_keyframe_guis)
2256 {
2257 // Send new configuration to plugin GUI's
2258         plugin_gui_lock->lock("MWindow::update_plugin_guis");
2259
2260         for(int i = 0; i < plugin_guis->size(); i++)
2261         {
2262                 PluginServer *ptr = plugin_guis->get(i);
2263                 if(edl->tracks->plugin_exists(ptr->plugin))
2264                         ptr->update_gui();
2265                 else
2266                 {
2267 // Schedule for deletion if no plugin
2268                         plugin_guis->remove_number(i);
2269                         i--;
2270                         
2271                         ptr->hide_gui();
2272                         delete_plugin(ptr);
2273                 }
2274         }
2275
2276
2277 // Change plugin variable if not visible
2278         Track *track = edl->tracks->first;
2279         while(track)
2280         {
2281                 for(int i = 0; i < track->plugin_set.size(); i++)
2282                 {
2283                         Plugin *plugin = (Plugin*)track->plugin_set.get(i)->first;
2284                         while(plugin)
2285                         {
2286                                 int got_it = 0;
2287                                 for(int i = 0; i < plugin_guis->size(); i++)
2288                                 {
2289                                         PluginServer *server = plugin_guis->get(i);
2290                                         if(server->plugin == plugin) 
2291                                         {
2292                                                 got_it = 1;
2293                                                 break;
2294                                         }
2295                                 }
2296                                 
2297                                 if(!got_it) plugin->show = 0;
2298
2299                                 plugin = (Plugin*)plugin->next;
2300                         }
2301                 }
2302                 
2303                 track = track->next;
2304         }
2305
2306         plugin_gui_lock->unlock();
2307
2308
2309         if(do_keyframe_guis) update_keyframe_guis();
2310 }
2311
2312 int MWindow::plugin_gui_open(Plugin *plugin)
2313 {
2314         int result = 0;
2315         plugin_gui_lock->lock("MWindow::plugin_gui_open");
2316         for(int i = 0; i < plugin_guis->total; i++)
2317         {
2318                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
2319                 {
2320                         result = 1;
2321                         break;
2322                 }
2323         }
2324         plugin_gui_lock->unlock();
2325         return result;
2326 }
2327
2328 void MWindow::render_plugin_gui(void *data, Plugin *plugin)
2329 {
2330         plugin_gui_lock->lock("MWindow::render_plugin_gui");
2331         for(int i = 0; i < plugin_guis->total; i++)
2332         {
2333                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
2334                 {
2335                         plugin_guis->values[i]->render_gui(data);
2336                         break;
2337                 }
2338         }
2339         plugin_gui_lock->unlock();
2340 }
2341
2342 void MWindow::render_plugin_gui(void *data, int size, Plugin *plugin)
2343 {
2344         plugin_gui_lock->lock("MWindow::render_plugin_gui");
2345         for(int i = 0; i < plugin_guis->total; i++)
2346         {
2347                 if(plugin_guis->values[i]->plugin->identical_location(plugin))
2348                 {
2349                         plugin_guis->values[i]->render_gui(data, size);
2350                         break;
2351                 }
2352         }
2353         plugin_gui_lock->unlock();
2354 }
2355
2356
2357 void MWindow::update_plugin_states()
2358 {
2359         plugin_gui_lock->lock("MWindow::update_plugin_states");
2360         for(int i = 0; i < plugin_guis->total; i++)
2361         {
2362                 int result = 0;
2363 // Get a plugin GUI
2364                 Plugin *src_plugin = plugin_guis->values[i]->plugin;
2365                 PluginServer *src_plugingui = plugin_guis->values[i];
2366
2367 // Search for plugin in EDL.  Only the master EDL shows plugin GUIs.
2368                 for(Track *track = edl->tracks->first; 
2369                         track && !result; 
2370                         track = track->next)
2371                 {
2372                         for(int j = 0; 
2373                                 j < track->plugin_set.total && !result; 
2374                                 j++)
2375                         {
2376                                 PluginSet *plugin_set = track->plugin_set.values[j];
2377                                 for(Plugin *plugin = (Plugin*)plugin_set->first; 
2378                                         plugin && !result; 
2379                                         plugin = (Plugin*)plugin->next)
2380                                 {
2381                                         if(plugin == src_plugin &&
2382                                                 !strcmp(plugin->title, src_plugingui->title)) result = 1;
2383                                 }
2384                         }
2385                 }
2386
2387
2388 // Doesn't exist anymore
2389                 if(!result)
2390                 {
2391                         hide_plugin(src_plugin, 0);
2392                         i--;
2393                 }
2394         }
2395         plugin_gui_lock->unlock();
2396 }
2397
2398
2399 void MWindow::update_plugin_titles()
2400 {
2401         for(int i = 0; i < plugin_guis->total; i++)
2402         {
2403                 plugin_guis->values[i]->update_title();
2404         }
2405 }
2406
2407 int MWindow::asset_to_edl(EDL *new_edl, 
2408         Asset *new_asset, 
2409         RecordLabels *labels)
2410 {
2411 const int debug = 0;
2412 if(debug) printf("MWindow::asset_to_edl %d new_asset->layers=%d\n", 
2413 __LINE__,
2414 new_asset->layers);
2415 // Keep frame rate, sample rate, and output size unchanged.
2416 // These parameters would revert the project if VWindow displayed an asset
2417 // of different size than the project.
2418         if(new_asset->video_data)
2419         {
2420                 new_edl->session->video_tracks = new_asset->layers;
2421         }
2422         else
2423                 new_edl->session->video_tracks = 0;
2424
2425 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
2426
2427
2428
2429
2430
2431         if(new_asset->audio_data)
2432         {
2433                 new_edl->session->audio_tracks = new_asset->channels;
2434         }
2435         else
2436                 new_edl->session->audio_tracks = 0;
2437 //printf("MWindow::asset_to_edl 2 %d %d\n", new_edl->session->video_tracks, new_edl->session->audio_tracks);
2438
2439 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
2440         new_edl->create_default_tracks();
2441 //printf("MWindow::asset_to_edl 2 %d %d\n", new_edl->session->video_tracks, new_edl->session->audio_tracks);
2442 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
2443
2444
2445
2446 //printf("MWindow::asset_to_edl 3\n");
2447         new_edl->insert_asset(new_asset,
2448                 0,
2449                 0, 
2450                 0, 
2451                 labels);
2452 //printf("MWindow::asset_to_edl 3\n");
2453 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
2454
2455
2456
2457
2458
2459         char string[BCTEXTLEN];
2460         FileSystem fs;
2461         fs.extract_name(string, new_asset->path);
2462 //printf("MWindow::asset_to_edl 3\n");
2463
2464         strcpy(new_edl->local_session->clip_title, string);
2465 //printf("MWindow::asset_to_edl 4 %s\n", string);
2466 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
2467
2468         return 0;
2469 }
2470
2471 int MWindow::edl_to_nested(EDL *new_edl, 
2472         EDL *nested_edl)
2473 {
2474
2475 // Keep frame rate, sample rate, and output size unchanged.
2476 // These parameters would revert the project if VWindow displayed an asset
2477 // of different size than the project.
2478
2479
2480
2481 // Nest all video & audio outputs
2482         new_edl->session->video_tracks = 1;
2483         new_edl->session->audio_tracks = nested_edl->session->audio_channels;
2484         new_edl->create_default_tracks();
2485
2486
2487
2488         new_edl->insert_asset(0,
2489                 nested_edl,
2490                 0, 
2491                 0, 
2492                 0);
2493
2494         char string[BCTEXTLEN];
2495         FileSystem fs;
2496         fs.extract_name(string, nested_edl->path);
2497 //printf("MWindow::edl_to_nested %p %s\n", nested_edl, nested_edl->path);
2498
2499         strcpy(new_edl->local_session->clip_title, string);
2500
2501         return 0;
2502 }
2503
2504 // Reset everything after a load.
2505 void MWindow::update_project(int load_mode)
2506 {
2507         const int debug = 0;
2508         
2509         if(debug) PRINT_TRACE
2510         restart_brender();
2511         edl->tracks->update_y_pixels(theme);
2512
2513         if(debug) PRINT_TRACE
2514
2515         if(load_mode == LOADMODE_REPLACE ||
2516                 load_mode == LOADMODE_REPLACE_CONCATENATE)
2517         {
2518                 gui->load_panes();
2519         }
2520
2521         gui->update(1, 1, 1, 1, 1, 1, 1);
2522         if(debug) PRINT_TRACE
2523         gui->unlock_window();
2524
2525         cwindow->gui->lock_window("MWindow::update_project 1");
2526         cwindow->update(0, 0, 1, 1, 1);
2527         cwindow->gui->unlock_window();
2528
2529         if(debug) PRINT_TRACE
2530
2531 // Close all the vwindows
2532         if(load_mode == LOADMODE_REPLACE ||
2533                 load_mode == LOADMODE_REPLACE_CONCATENATE) {
2534                 if(debug) PRINT_TRACE
2535                 int first_vwindow = 0;
2536                 if(session->show_vwindow) first_vwindow = 1;
2537 // Change visible windows to no source
2538                 for(int i = 0; i < first_vwindow && i < vwindows.size(); i++) {
2539                         VWindow *vwindow = vwindows.get(i);
2540                         if( !vwindow->is_running() ) continue;
2541                         vwindow->change_source(-1);
2542                 }
2543
2544 // Close remaining windows
2545                 for(int i = first_vwindow; i < vwindows.size(); i++) {
2546                         VWindow *vwindow = vwindows.get(i);
2547                         if( !vwindow->is_running() ) continue;
2548                         vwindow->close_window();
2549                 }
2550                 if(debug) PRINT_TRACE
2551         }
2552         else if(vwindows.size()) {
2553                 VWindow *vwindow = vwindows.get(DEFAULT_VWINDOW);
2554                 if( vwindow->is_running() ) {
2555                         vwindow->gui->lock_window("MWindow::update_project");
2556                         vwindow->update(1);
2557                         vwindow->gui->unlock_window();
2558                 }
2559         }
2560
2561         if(debug) PRINT_TRACE
2562         cwindow->gui->lock_window("MWindow::update_project 2");
2563         cwindow->gui->timebar->update(0);
2564         cwindow->gui->unlock_window();
2565
2566         if(debug) PRINT_TRACE
2567         cwindow->playback_engine->que->send_command(CURRENT_FRAME, 
2568                 CHANGE_ALL,
2569                 edl,
2570                 1);
2571
2572         if(debug) PRINT_TRACE
2573
2574         awindow->gui->lock_window("MWindow::update_project");
2575         awindow->gui->update_assets();
2576         awindow->gui->flush();
2577         awindow->gui->unlock_window();
2578
2579         if(debug) PRINT_TRACE
2580
2581         gui->lock_window("MWindow::update_project");
2582         gui->flush();
2583         if(debug) PRINT_TRACE
2584 }
2585
2586 void MWindow::remove_indexfile(Indexable *indexable)
2587 {
2588         if( !indexable->is_asset ) return;
2589         Asset *asset = (Asset *)indexable;
2590 // Erase file
2591         IndexFile::delete_index(preferences, asset, ".toc");
2592         IndexFile::delete_index(preferences, asset, ".idx");
2593         IndexFile::delete_index(preferences, asset, ".mkr");
2594 }
2595
2596 void MWindow::rebuild_indices()
2597 {
2598         for(int i = 0; i < session->drag_assets->total; i++)
2599         {
2600                 Indexable *indexable = session->drag_assets->values[i];
2601 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
2602                 remove_indexfile(indexable);
2603 // Schedule index build
2604                 IndexState *index_state = indexable->index_state;
2605                 index_state->index_status = INDEX_NOTTESTED;
2606                 if( indexable->is_asset ) {
2607                         Asset *asset = (Asset *)indexable;
2608                         if( asset->format != FILE_PCM )
2609                                 asset->reset_audio();
2610                         asset->reset_video();
2611                 }
2612                 mainindexes->add_next_asset(0, indexable);
2613         }
2614         mainindexes->start_build();
2615 }
2616
2617
2618 void MWindow::save_backup()
2619 {
2620         FileXML file;
2621         edl->set_path(session->filename);
2622         edl->save_xml(&file, 
2623                 BACKUP_PATH,
2624                 0,
2625                 0);
2626         file.terminate_string();
2627         char path[BCTEXTLEN];
2628         FileSystem fs;
2629         strcpy(path, BACKUP_PATH);
2630         fs.complete_path(path);
2631
2632         if(file.write_to_file(path))
2633         {
2634                 char string2[256];
2635                 sprintf(string2, _("Couldn't open %s for writing."), BACKUP_PATH);
2636                 gui->show_message(string2);
2637         }
2638 }
2639
2640 void MWindow::load_backup()
2641 {
2642         ArrayList<char*> path_list;
2643         path_list.set_array_delete();
2644         char *out_path;
2645         char string[BCTEXTLEN];
2646         strcpy(string, BACKUP_PATH);
2647         FileSystem fs;
2648         fs.complete_path(string);
2649         
2650         path_list.append(out_path = new char[strlen(string) + 1]);
2651         strcpy(out_path, string);
2652         
2653         load_filenames(&path_list, LOADMODE_REPLACE, 0);
2654         edl->local_session->clip_title[0] = 0;
2655 // This is unique to backups since the path of the backup is different than the
2656 // path of the project.
2657         set_filename(edl->path);
2658         path_list.remove_all_objects();
2659         save_backup();
2660 }
2661
2662 static inline int gcd(int m, int n)
2663 {
2664         int r;
2665         if( m < n ) { r = m;  m = n;  n = r; }
2666         while( (r = m % n) != 0 ) { m = n;  n = r; }
2667         return n;
2668 }
2669
2670 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
2671 {
2672         w = 1;  h = 1;
2673         if(!width || !height) return 1;
2674         double ar = (double)width / height;
2675 // square-ish pixels
2676         if( EQUIV(ar, 1.0000) ) return 0;
2677         if( EQUIV(ar, 1.3333) ) { w = 4;  h = 3;  return 0; }
2678         if( EQUIV(ar, 1.7777) ) { w = 16; h = 9;  return 0; }
2679         if( EQUIV(ar, 2.1111) ) { w = 19; h = 9;  return 0; }
2680         if( EQUIV(ar, 2.2222) ) { w = 20; h = 9;  return 0; }
2681         if( EQUIV(ar, 2.3333) ) { w = 21; h = 9;  return 0; }
2682         int ww = width, hh = height;
2683         // numerator, denominator must be under mx
2684         int mx = 255, n = gcd(ww, hh);
2685         if( n > 1 ) { ww /= n; hh /= n; }
2686         // search near height in case extra/missing lines
2687         if( ww >= mx || hh >= mx ) {
2688                 double err = height;  // +/- 2 percent height
2689                 for( int m=2*height/100, i=1; m>0; i=i>0 ? -i : (--m, -i+1) ) {
2690                         int iw = width, ih = height+i;
2691                         if( (n=gcd(iw, ih)) > 1 ) {
2692                                 int u = iw/n;  if( u >= mx ) continue;
2693                                 int v = ih/n;  if( v >= mx ) continue;
2694                                 double r = (double) u/v, er = fabs(ar-r);
2695                                 if( er >= err ) continue;
2696                                 err = er;  ww = u;  hh = v;
2697                         }
2698                 }
2699         }
2700
2701         w = ww;  h = hh;
2702         return 0;
2703 }
2704
2705 void MWindow::reset_caches()
2706 {
2707         frame_cache->remove_all();
2708         wave_cache->remove_all();
2709         audio_cache->remove_all();
2710         video_cache->remove_all();
2711         if( cwindow->playback_engine && cwindow->playback_engine->audio_cache )
2712                 cwindow->playback_engine->audio_cache->remove_all();
2713         if( cwindow->playback_engine && cwindow->playback_engine->video_cache )
2714                 cwindow->playback_engine->video_cache->remove_all();
2715         
2716         for(int i = 0; i < vwindows.size(); i++) {
2717                 VWindow *vwindow = vwindows.get(i);
2718                 if( !vwindow->is_running() ) continue;
2719                 if(vwindow->playback_engine && vwindow->playback_engine->audio_cache)
2720                         vwindow->playback_engine->audio_cache->remove_all();
2721                 if(vwindow->playback_engine && vwindow->playback_engine->video_cache)
2722                         vwindow->playback_engine->video_cache->remove_all();
2723         }
2724 }
2725
2726 void MWindow::remove_asset_from_caches(Asset *asset)
2727 {
2728         frame_cache->remove_asset(asset);
2729         wave_cache->remove_asset(asset);
2730         audio_cache->delete_entry(asset);
2731         video_cache->delete_entry(asset);
2732         if( cwindow->playback_engine && cwindow->playback_engine->audio_cache )
2733                 cwindow->playback_engine->audio_cache->delete_entry(asset);
2734         if( cwindow->playback_engine && cwindow->playback_engine->video_cache )
2735                 cwindow->playback_engine->video_cache->delete_entry(asset);
2736         for(int i = 0; i < vwindows.size(); i++) {
2737                 VWindow *vwindow = vwindows.get(i);
2738                 if( !vwindow->is_running() ) continue;
2739                 if(vwindow->playback_engine && vwindow->playback_engine->audio_cache)
2740                         vwindow->playback_engine->audio_cache->delete_entry(asset);
2741                 if(vwindow->playback_engine && vwindow->playback_engine->video_cache)
2742                         vwindow->playback_engine->video_cache->delete_entry(asset);
2743         }
2744 }
2745
2746
2747
2748 void MWindow::remove_assets_from_project(int push_undo)
2749 {
2750         for(int i = 0; i < session->drag_assets->total; i++) {
2751                 Indexable *indexable = session->drag_assets->values[i];
2752                 if(indexable->is_asset) remove_asset_from_caches((Asset*)indexable);
2753         }
2754
2755 // Remove from VWindow.
2756         for(int i = 0; i < session->drag_clips->total; i++) {
2757                 for(int j = 0; j < vwindows.size(); j++) {
2758                         VWindow *vwindow = vwindows.get(j);
2759                         if( !vwindow->is_running() ) continue;
2760                         if(session->drag_clips->values[i] == vwindow->get_edl()) {
2761                                 vwindow->gui->lock_window("MWindow::remove_assets_from_project 1");
2762                                 vwindow->delete_source(1, 1);
2763                                 vwindow->gui->unlock_window();
2764                         }
2765                 }
2766         }
2767         
2768         for(int i = 0; i < session->drag_assets->size(); i++) {
2769                 for(int j = 0; j < vwindows.size(); j++) {
2770                         VWindow *vwindow = vwindows.get(j);
2771                         if( !vwindow->is_running() ) continue;
2772                         if(session->drag_assets->get(i) == vwindow->get_source()) {
2773                                 vwindow->gui->lock_window("MWindow::remove_assets_from_project 2");
2774                                 vwindow->delete_source(1, 1);
2775                                 vwindow->gui->unlock_window();
2776                         }
2777                 }
2778         }
2779         
2780         for(int i = 0; i < session->drag_assets->size(); i++) {
2781                 Indexable *indexable = session->drag_assets->values[i];
2782                 remove_indexfile(indexable);
2783         }
2784
2785 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
2786         if(push_undo) undo->update_undo_before();
2787         edl->remove_from_project(session->drag_assets);
2788         edl->remove_from_project(session->drag_clips);
2789         save_backup();
2790         if(push_undo) undo->update_undo_after(_("remove assets"), LOAD_ALL);
2791         restart_brender();
2792
2793         gui->lock_window("MWindow::remove_assets_from_project 3");
2794         gui->update(1,
2795                 1,
2796                 1,
2797                 1,
2798                 0, 
2799                 1,
2800                 0);
2801         gui->unlock_window();
2802
2803         awindow->gui->lock_window("MWindow::remove_assets_from_project 4");
2804         awindow->gui->update_assets();
2805         awindow->gui->flush();
2806         awindow->gui->unlock_window();
2807
2808 // Removes from playback here
2809         sync_parameters(CHANGE_ALL);
2810 }
2811
2812 void MWindow::remove_assets_from_disk()
2813 {
2814 // Remove from disk
2815         for(int i = 0; i < session->drag_assets->total; i++)
2816         {
2817                 remove(session->drag_assets->values[i]->path);
2818         }
2819
2820         remove_assets_from_project(1);
2821 }
2822
2823 void MWindow::dump_plugins(FILE *fp)
2824 {
2825         if( !plugindb ) return;
2826         for(int i = 0; i < plugindb->total; i++)
2827         {
2828                 fprintf(fp, "type=%d audio=%d video=%d rt=%d multi=%d"
2829                         " synth=%d transition=%d theme=%d %s\n",
2830                         plugindb->values[i]->plugin_type,
2831                         plugindb->values[i]->audio,
2832                         plugindb->values[i]->video,
2833                         plugindb->values[i]->realtime,
2834                         plugindb->values[i]->multichannel,
2835                         plugindb->values[i]->get_synthesis(),
2836                         plugindb->values[i]->transition,
2837                         plugindb->values[i]->theme,
2838                         plugindb->values[i]->title);
2839         }
2840 }
2841
2842 void MWindow::dump_edl(FILE *fp)
2843 {
2844         if( !edl ) return;
2845         edl->dump(fp);
2846 }
2847
2848 void MWindow::dump_undo(FILE *fp)
2849 {
2850         if( !undo ) return;
2851         undo->dump(fp);
2852 }
2853
2854 void MWindow::dump_exe(FILE *fp)
2855 {
2856         char proc_path[BCTEXTLEN], exe_path[BCTEXTLEN];
2857         sprintf(proc_path, "/proc/%d/exe", (int)getpid());
2858         int len = readlink(proc_path, exe_path, sizeof(exe_path));
2859         if( len < 0 ) { fprintf(fp,"readlink: %m\n"); return; }
2860         exe_path[len] = 0;
2861         struct stat st;
2862         if( stat(exe_path,&st) ) { fprintf(fp,"stat: %m\n"); return; }
2863         fprintf(fp, "path: %s = %9jd bytes\n",exe_path,st.st_size);
2864         int fd = open(exe_path,O_RDONLY);
2865         if( fd < 0 ) { fprintf(fp,"open: %m\n"); return; }
2866         uint8_t buf[65536];  SHA1 sha1;
2867         while( (len=read(fd,buf,sizeof(buf))) > 0 ) {
2868                 sha1.addBytes(buf, len);
2869         }
2870         close(fd);
2871         fprintf(fp, "SHA1: ");
2872         uint8_t digest[20];  sha1.computeHash(digest);
2873         for( int i=0; i<20; ++i ) fprintf(fp, "%02x", digest[i]);
2874         fprintf(fp, "\n");
2875 }
2876
2877
2878 void MWindow::trap_hook(FILE *fp, void *vp)
2879 {
2880         MWindow *mwindow = (MWindow *)vp;
2881         fprintf(fp, "\nEXE:\n");
2882         mwindow->dump_exe(fp);
2883         fprintf(fp, "\nPLUGINS:\n");
2884         mwindow->dump_plugins(fp);
2885         fprintf(fp, "\nEDL:\n");
2886         mwindow->dump_edl(fp);
2887         fprintf(fp, "\nUNDO:\n");
2888         mwindow->dump_undo(fp);
2889 }
2890
2891
2892
2893
2894
2895
2896 int MWindow::save_defaults()
2897 {
2898         gui->save_defaults(defaults);
2899         edl->save_defaults(defaults);
2900         session->save_defaults(defaults);
2901         preferences->save_defaults(defaults);
2902
2903         for(int i = 0; i < plugin_guis->total; i++)
2904         {
2905 // Pointer comparison
2906                 plugin_guis->values[i]->save_defaults();
2907         }
2908         awindow->save_defaults(defaults);
2909
2910         defaults->save();
2911         return 0;
2912 }
2913
2914 int MWindow::run_script(FileXML *script)
2915 {
2916         int result = 0, result2 = 0;
2917         while(!result && !result2)
2918         {
2919                 result = script->read_tag();
2920                 if(!result)
2921                 {
2922                         if(script->tag.title_is("new_project"))
2923                         {
2924 // Run new in immediate mode.
2925 //                              gui->mainmenu->new_project->run_script(script);
2926                         }
2927                         else
2928                         if(script->tag.title_is("record"))
2929                         {
2930 // Run record as a thread.  It is a terminal command.
2931                                 ;
2932 // Will read the complete scipt file without letting record read it if not
2933 // terminated.
2934                                 result2 = 1;
2935                         }
2936                         else
2937                         {
2938                                 printf("MWindow::run_script: Unrecognized command: %s\n",script->tag.get_title() );
2939                         }
2940                 }
2941         }
2942         return result2;
2943 }
2944
2945 // ================================= synchronization
2946
2947
2948 int MWindow::interrupt_indexes()
2949 {
2950         mainindexes->interrupt_build();
2951         return 0; 
2952 }
2953
2954
2955
2956 void MWindow::next_time_format()
2957 {
2958         switch(edl->session->time_format)
2959         {
2960                 case TIME_HMS: edl->session->time_format = TIME_HMSF; break;
2961                 case TIME_HMSF: edl->session->time_format = TIME_SAMPLES; break;
2962                 case TIME_SAMPLES: edl->session->time_format = TIME_SAMPLES_HEX; break;
2963                 case TIME_SAMPLES_HEX: edl->session->time_format = TIME_FRAMES; break;
2964                 case TIME_FRAMES: edl->session->time_format = TIME_FEET_FRAMES; break;
2965                 case TIME_FEET_FRAMES: edl->session->time_format = TIME_SECONDS; break;
2966                 case TIME_SECONDS: edl->session->time_format = TIME_HMS; break;
2967         }
2968
2969         time_format_common();
2970 }
2971
2972 void MWindow::prev_time_format()
2973 {
2974         switch(edl->session->time_format)
2975         {
2976                 case TIME_HMS: edl->session->time_format = TIME_SECONDS; break;
2977                 case TIME_SECONDS: edl->session->time_format = TIME_FEET_FRAMES; break;
2978                 case TIME_FEET_FRAMES: edl->session->time_format = TIME_FRAMES; break;
2979                 case TIME_FRAMES: edl->session->time_format = TIME_SAMPLES_HEX; break;
2980                 case TIME_SAMPLES_HEX: edl->session->time_format = TIME_SAMPLES; break;
2981                 case TIME_SAMPLES: edl->session->time_format = TIME_HMSF; break;
2982                 case TIME_HMSF: edl->session->time_format = TIME_HMS; break;
2983         }
2984
2985         time_format_common();
2986 }
2987
2988 void MWindow::time_format_common()
2989 {
2990         gui->lock_window("MWindow::next_time_format");
2991         gui->redraw_time_dependancies();
2992
2993
2994         char string[BCTEXTLEN], string2[BCTEXTLEN];
2995         sprintf(string, _("Using %s"), Units::print_time_format(edl->session->time_format, string2));
2996         gui->show_message(string);
2997         gui->flush();
2998         gui->unlock_window();
2999 }
3000
3001
3002 int MWindow::set_filename(const char *filename)
3003 {
3004         if( filename != session->filename )
3005                 strcpy(session->filename, filename);
3006         if( filename != edl->path )
3007                 strcpy(edl->path, filename);
3008
3009         if(gui)
3010         {
3011                 if(filename[0] == 0)
3012                 {
3013                         gui->set_title(PROGRAM_NAME);
3014                 }
3015                 else
3016                 {
3017                         FileSystem dir;
3018                         char string[BCTEXTLEN], string2[BCTEXTLEN];
3019                         dir.extract_name(string, filename);
3020                         sprintf(string2, PROGRAM_NAME ": %s", string);
3021                         gui->set_title(string2);
3022                 }
3023         }
3024         return 0; 
3025 }
3026
3027
3028
3029
3030
3031
3032
3033
3034 int MWindow::set_loop_boundaries()
3035 {
3036         double start = edl->local_session->get_selectionstart();
3037         double end = edl->local_session->get_selectionend();
3038         
3039         if(start != 
3040                 end) 
3041         {
3042                 ;
3043         }
3044         else
3045         if(edl->tracks->total_length())
3046         {
3047                 start = 0;
3048                 end = edl->tracks->total_length();
3049         }
3050         else
3051         {
3052                 start = end = 0;
3053         }
3054
3055         if(edl->local_session->loop_playback && start != end)
3056         {
3057                 edl->local_session->loop_start = start;
3058                 edl->local_session->loop_end = end;
3059         }
3060         return 0; 
3061 }
3062
3063
3064
3065
3066
3067
3068
3069 int MWindow::reset_meters()
3070 {
3071         cwindow->gui->lock_window("MWindow::reset_meters 1");
3072         cwindow->gui->meters->reset_meters();
3073         cwindow->gui->unlock_window();
3074
3075         for(int j = 0; j < vwindows.size(); j++) {
3076                 VWindow *vwindow = vwindows.get(j);
3077                 if( !vwindow->is_running() ) continue;
3078                 vwindow->gui->lock_window("MWindow::reset_meters 2");
3079                 vwindow->gui->meters->reset_meters();
3080                 vwindow->gui->unlock_window();
3081         }
3082
3083         lwindow->gui->lock_window("MWindow::reset_meters 3");
3084         lwindow->gui->panel->reset_meters();
3085         lwindow->gui->unlock_window();
3086
3087         gui->lock_window("MWindow::reset_meters 4");
3088         gui->reset_meters();
3089         gui->unlock_window();
3090         return 0; 
3091 }
3092
3093
3094 void MWindow::resync_guis()
3095 {
3096 // Update GUIs
3097         restart_brender();
3098         gui->lock_window("MWindow::resync_guis");
3099         gui->update(1, 1, 1, 1, 1, 1, 0);
3100         gui->unlock_window();
3101
3102         cwindow->gui->lock_window("MWindow::resync_guis");
3103         cwindow->gui->resize_event(cwindow->gui->get_w(), 
3104                 cwindow->gui->get_h());
3105         int channels = edl->session->audio_channels;
3106         cwindow->gui->meters->set_meters(channels, 1);
3107         cwindow->gui->flush();
3108         cwindow->gui->unlock_window();
3109
3110         for(int i = 0; i < vwindows.size(); i++) {
3111                 VWindow *vwindow = vwindows.get(i);
3112                 if( !vwindow->is_running() ) continue;
3113                 vwindow->gui->lock_window("MWindow::resync_guis");
3114                 vwindow->gui->resize_event(vwindow->gui->get_w(), 
3115                         vwindow->gui->get_h());
3116                 vwindow->gui->meters->set_meters(channels, 1);
3117                 vwindow->gui->flush();
3118                 vwindow->gui->unlock_window();
3119         }
3120
3121         lwindow->gui->lock_window("MWindow::resync_guis");
3122         lwindow->gui->panel->set_meters(channels, 1);
3123         lwindow->gui->flush();
3124         lwindow->gui->unlock_window();
3125
3126 // Warn user
3127         if(((edl->session->output_w % 4) ||
3128                 (edl->session->output_h % 4)) &&
3129                 edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
3130         {
3131                 MainError::show_error(
3132                         _("This project's dimensions are not multiples of 4 so\n"
3133                         "it can't be rendered by OpenGL."));
3134         }
3135
3136
3137 // Flash frame
3138         sync_parameters(CHANGE_ALL);
3139 }
3140
3141 int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tracks)
3142 {
3143         File *file = new File;
3144         EDLSession *session = edl->session;
3145         double old_framerate = session->frame_rate;
3146         double old_samplerate = session->sample_rate;
3147         int result = file->open_file(preferences, asset, 1, 0);
3148         if( !result && delete_tracks > 0 )
3149                 undo->update_undo_before();
3150         if( !result && asset->get_video_layers() > 0 ) {
3151                 // try to get asset up to date, may fail
3152                 file->select_video_stream(asset, vstream);
3153                 // either way use what was/is there.
3154                 double framerate = asset->get_frame_rate();
3155                 int width = asset->get_w();
3156                 int height = asset->get_h();
3157                 // must be multiple of 4 for opengl
3158                 width = (width+3) & ~3;  height = (height+3) & ~3;
3159                 int driver = session->playback_config->vconfig->driver;
3160                 int color_model = file->get_best_colormodel(asset, driver);
3161                 color_model = BC_CModels::is_yuv(color_model) ?
3162                                 BC_CModels::has_alpha(color_model) ? BC_YUVA8888 : BC_YUV888 :
3163                         BC_CModels::is_float(color_model) ?
3164                                 BC_CModels::has_alpha(color_model) ? BC_RGBA_FLOAT : BC_RGB_FLOAT :
3165                                 BC_CModels::has_alpha(color_model) ? BC_RGBA8888 : BC_RGB888;
3166                 session->color_model = color_model;
3167                 session->output_w = width;
3168                 session->output_h = height;
3169                 session->frame_rate = framerate;
3170                 // not, asset->actual_width/actual_height
3171                 asset->width = session->output_w;
3172                 asset->height = session->output_h;
3173                 asset->frame_rate = session->frame_rate;
3174                 create_aspect_ratio(session->aspect_w, session->aspect_h,
3175                         session->output_w, session->output_h);
3176                 Track *track = edl->tracks->first;
3177                 for( Track *next_track=0; track; track=next_track ) {
3178                         next_track = track->next;
3179                         if( track->data_type != TRACK_VIDEO ) continue;
3180                         if( delete_tracks ) {
3181                                 Edit *edit = track->edits->first;
3182                                 for( Edit *next_edit=0; edit; edit=next_edit ) {
3183                                         next_edit = edit->next;
3184                                         if( edit->channel != vstream ||
3185                                             !edit->asset || !edit->asset->is_asset ||
3186                                             *asset != *edit->asset )
3187                                                 delete edit;
3188                                 }
3189                         }
3190                         if( track->edits->first ) {
3191                                 track->track_w = edl->session->output_w;
3192                                 track->track_h = edl->session->output_h;
3193                         }
3194                         else if( delete_tracks )
3195                                 edl->tracks->delete_track(track);
3196                 }
3197                 edl->resample(old_framerate, session->frame_rate, TRACK_VIDEO);
3198         }
3199         if( !result && asset->channels > 0 ) {
3200                 session->sample_rate = asset->get_sample_rate();
3201                 int64_t channel_mask = 0;
3202                 int astrm = file->get_audio_for_video(vstream, astream, channel_mask);
3203                 if( astrm >= 0 ) file->select_audio_stream(asset, astrm);
3204                 if( astrm < 0 || !channel_mask ) channel_mask = (1<<asset->channels)-1;
3205                 int channels = 0;
3206                 for( uint64_t mask=channel_mask; mask!=0; mask>>=1 ) channels += mask & 1;
3207                 if( channels > 6 ) channels = 6;
3208                 session->audio_tracks = session->audio_channels = channels;
3209                 switch( channels ) {
3210                 case 6:
3211                         session->achannel_positions[0] = 90;
3212                         session->achannel_positions[1] = 150;
3213                         session->achannel_positions[2] = 30;
3214                         session->achannel_positions[3] = 210;
3215                         session->achannel_positions[4] = 330;
3216                         session->achannel_positions[5] = 270;
3217                         break;
3218                 case 2:
3219                         session->achannel_positions[0] = 180;
3220                         session->achannel_positions[1] = 0;
3221                         break;
3222                 }
3223                 remap_audio(MWindow::AUDIO_1_TO_1);
3224
3225                 if( delete_tracks ) {
3226                         Track *track = edl->tracks->first;
3227                         for( Track *next_track=0; track; track=next_track ) {
3228                                 next_track = track->next;
3229                                 if( track->data_type != TRACK_AUDIO ) continue;
3230                                 Edit *edit = track->edits->first;
3231                                         for( Edit *next_edit=0; edit; edit=next_edit ) {
3232                                         next_edit = edit->next;
3233                                         if( !((1<<edit->channel) & channel_mask) ||
3234                                             !edit->asset || !edit->asset->is_asset ||
3235                                             *asset != *edit->asset )
3236                                                 delete edit;
3237                                 }
3238                                 if( !track->edits->first )
3239                                         edl->tracks->delete_track(track);
3240                         }
3241                 }
3242                 edl->rechannel();
3243                 edl->resample(old_samplerate, session->sample_rate, TRACK_AUDIO);
3244         }
3245         delete file;
3246         if( !result && delete_tracks > 0 ) {
3247                 save_backup();
3248                 undo->update_undo_after(_("select asset"), LOAD_ALL);
3249         }
3250         resync_guis();
3251         return result;
3252 }
3253
3254 int MWindow::select_asset(int vtrack, int delete_tracks)
3255 {
3256         Track *track = edl->tracks->get(vtrack, TRACK_VIDEO);
3257         if( !track ) return 1;
3258         Edit *edit = track->edits->first;
3259         if( !edit ) return 1;
3260         Asset *asset = edit->asset;
3261         if( !asset || !asset->is_asset ) return 1;
3262         return select_asset(asset, edit->channel, -1, delete_tracks);
3263 }
3264
3265 void MWindow::dump_plugindb(FILE *fp)
3266 {
3267         if( !plugindb ) return;
3268         for(int i = 0; i < plugindb->total; i++)
3269                 plugindb->values[i]->dump(fp);
3270 }
3271