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