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