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