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