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