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