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