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