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