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