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