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