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