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