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