Add back 2 patches for histogram and overlayframe that are working correctly and...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 070353020f8195996b02b77b0a371d316ca94b38..1291b99213fce9781c2fe883d733e9bb65cd6189 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * CINELERRA
  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
 /*
  * CINELERRA
  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
+ * Copyright (C) 2003-2016 Cinelerra CV contributors
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,6 +28,7 @@
 #include "awindowgui.h"
 #include "awindow.h"
 #include "batchrender.h"
 #include "awindowgui.h"
 #include "awindow.h"
 #include "batchrender.h"
+#include "bccmodels.h"
 #include "bcdisplayinfo.h"
 #include "bcprogressbox.h"
 #include "bcsignals.h"
 #include "bcdisplayinfo.h"
 #include "bcprogressbox.h"
 #include "bcsignals.h"
@@ -40,9 +42,9 @@
 #include "channelinfo.h"
 #include "clip.h"
 #include "clipedls.h"
 #include "channelinfo.h"
 #include "clip.h"
 #include "clipedls.h"
-#include "bccmodels.h"
 #include "commercials.h"
 #include "confirmsave.h"
 #include "commercials.h"
 #include "confirmsave.h"
+#include "convert.h"
 #include "cplayback.h"
 #include "ctimebar.h"
 #include "cwindowgui.h"
 #include "cplayback.h"
 #include "ctimebar.h"
 #include "cwindowgui.h"
@@ -80,6 +82,7 @@
 #include "mainsession.h"
 #include "mainundo.h"
 #include "mbuttons.h"
 #include "mainsession.h"
 #include "mainundo.h"
 #include "mbuttons.h"
+#include "mixersalign.h"
 #include "mutex.h"
 #include "mwindowgui.h"
 #include "mwindow.h"
 #include "mutex.h"
 #include "mwindowgui.h"
 #include "mwindow.h"
 #include "savefile.inc"
 #include "samplescroll.h"
 #include "sha1.h"
 #include "savefile.inc"
 #include "samplescroll.h"
 #include "sha1.h"
+#include "shuttle.h"
 #include "sighandler.h"
 #include "splashgui.h"
 #include "statusbar.h"
 #include "sighandler.h"
 #include "splashgui.h"
 #include "statusbar.h"
 #include "vframe.h"
 #include "vtrack.h"
 #include "versioninfo.h"
 #include "vframe.h"
 #include "vtrack.h"
 #include "versioninfo.h"
+#include "vicon.h"
 #include "videodevice.inc"
 #include "videowindow.h"
 #include "vplayback.h"
 #include "vwindowgui.h"
 #include "vwindow.h"
 #include "wavecache.h"
 #include "videodevice.inc"
 #include "videowindow.h"
 #include "vplayback.h"
 #include "vwindowgui.h"
 #include "vwindow.h"
 #include "wavecache.h"
+#include "wintv.h"
 #include "wwindow.h"
 #include "wwindow.h"
+#include "x10tv.h"
 #include "zoombar.h"
 #include "zwindow.h"
 #include "zwindowgui.h"
 #include "zoombar.h"
 #include "zwindow.h"
 #include "zwindowgui.h"
@@ -176,7 +183,6 @@ extern "C"
 
 }
 
 
 }
 
-
 extern long cin_timezone;
 
 ArrayList<PluginServer*>* MWindow::plugindb = 0;
 extern long cin_timezone;
 
 ArrayList<PluginServer*>* MWindow::plugindb = 0;
@@ -197,6 +203,7 @@ MWindow::MWindow()
        playback_3d = 0;
        splash_window = 0;
        undo = 0;
        playback_3d = 0;
        splash_window = 0;
        undo = 0;
+       undo_command = COMMAND_NONE;
        defaults = 0;
        assets = 0;
        //commercials = 0;
        defaults = 0;
        assets = 0;
        //commercials = 0;
@@ -221,6 +228,7 @@ MWindow::MWindow()
        create_bd = 0;
        create_dvd = 0;
        batch_render = 0;
        create_bd = 0;
        create_dvd = 0;
        batch_render = 0;
+       convert_render = 0;
        render = 0;
        edl = 0;
        gui = 0;
        render = 0;
        edl = 0;
        gui = 0;
@@ -235,7 +243,12 @@ MWindow::MWindow()
        screens = 1;
        in_destructor = 0;
        speed_edl = 0;
        screens = 1;
        in_destructor = 0;
        speed_edl = 0;
-       proxy_beep = 0;
+       beeper = 0;
+       redraw_tracks = 0;
+       shuttle = 0;
+       wintv = 0;
+       x10tv = 0;
+       mixers_align = 0;
 }
 
 
 }
 
 
@@ -245,18 +258,36 @@ MWindow::~MWindow()
        run_lock->lock("MWindow::~MWindow");
        in_destructor = 1;
 //printf("MWindow::~MWindow %d\n", __LINE__);
        run_lock->lock("MWindow::~MWindow");
        in_destructor = 1;
 //printf("MWindow::~MWindow %d\n", __LINE__);
+       if( wwindow && wwindow->is_running() )
+               wwindow->close_window();
+       if( twindow && twindow->is_running() )
+               twindow->close_window();
        gui->remote_control->deactivate();
        gui->record->stop();
 #ifdef HAVE_DVB
        gui->channel_info->stop();
 #endif
        gui->remote_control->deactivate();
        gui->record->stop();
 #ifdef HAVE_DVB
        gui->channel_info->stop();
 #endif
-       delete proxy_beep;
-       delete create_bd;       create_bd = 0;
-       delete create_dvd;      create_dvd = 0;
+       delete beeper;          beeper = 0;
+       delete redraw_tracks;   redraw_tracks = 0;
+       delete create_bd;       create_bd = 0;
+       delete create_dvd;      create_dvd = 0;
+#ifdef HAVE_SHUTTLE
+       delete shuttle;         shuttle = 0;
+#endif
+#ifdef HAVE_WINTV
+       delete wintv;           wintv = 0;
+#endif
+#ifdef HAVE_X10TV
+       delete x10tv;           x10tv = 0;
+#endif
        delete batch_render;    batch_render = 0;
        delete batch_render;    batch_render = 0;
+       delete convert_render;  convert_render = 0;
        delete render;          render = 0;
        delete render;          render = 0;
+       delete mixers_align;    mixers_align = 0;
+#ifdef HAVE_COMMERCIALS
        commit_commercial();
        if( commercials && !commercials->remove_user() ) commercials = 0;
        commit_commercial();
        if( commercials && !commercials->remove_user() ) commercials = 0;
+#endif
        close_mixers();
        if( speed_edl ) { speed_edl->remove_user();  speed_edl = 0; }
 // Save defaults for open plugins
        close_mixers();
        if( speed_edl ) { speed_edl->remove_user();  speed_edl = 0; }
 // Save defaults for open plugins
@@ -268,6 +299,7 @@ MWindow::~MWindow()
        plugin_gui_lock->unlock();
        hide_keyframe_guis();
        clean_indexes();
        plugin_gui_lock->unlock();
        hide_keyframe_guis();
        clean_indexes();
+       clean_backups();
        save_defaults();
 // Give up and go to a movie
 //  cant run valgrind if this is used
        save_defaults();
 // Give up and go to a movie
 //  cant run valgrind if this is used
@@ -275,22 +307,19 @@ MWindow::~MWindow()
        gui->del_keyboard_listener(
                (int (BC_WindowBase::*)(BC_WindowBase *))
                &MWindowGUI::keyboard_listener);
        gui->del_keyboard_listener(
                (int (BC_WindowBase::*)(BC_WindowBase *))
                &MWindowGUI::keyboard_listener);
+       reset_caches(0);
 #if 0
 // release the hounds
        if( awindow && awindow->gui ) awindow->gui->close(0);
        if( cwindow && cwindow->gui ) cwindow->gui->close(0);
        if( lwindow && lwindow->gui ) lwindow->gui->close(0);
        if( gwindow && gwindow->gui ) gwindow->gui->close(0);
 #if 0
 // release the hounds
        if( awindow && awindow->gui ) awindow->gui->close(0);
        if( cwindow && cwindow->gui ) cwindow->gui->close(0);
        if( lwindow && lwindow->gui ) lwindow->gui->close(0);
        if( gwindow && gwindow->gui ) gwindow->gui->close(0);
-       if( twindow && twindow->is_running() ) twindow->close_window();
-       if( wwindow && wwindow->is_running() ) wwindow->close_window();
        vwindows.remove_all_objects();
        zwindows.remove_all_objects();
        gui->close(0);
        if( awindow ) awindow->join();
        if( cwindow ) cwindow->join();
        if( lwindow ) lwindow->join();
        vwindows.remove_all_objects();
        zwindows.remove_all_objects();
        gui->close(0);
        if( awindow ) awindow->join();
        if( cwindow ) cwindow->join();
        if( lwindow ) lwindow->join();
-       if( twindow ) twindow->join();
-       if( wwindow ) wwindow->join();
        if( gwindow ) gwindow->join();
        join();
 #else
        if( gwindow ) gwindow->join();
        join();
 #else
@@ -302,14 +331,11 @@ MWindow::~MWindow()
        close_gui(cwindow);
        close_gui(lwindow);
        close_gui(gwindow);
        close_gui(cwindow);
        close_gui(lwindow);
        close_gui(gwindow);
-       close_gui(twindow);
-       close_gui(wwindow);
        vwindows.remove_all_objects();
        zwindows.remove_all_objects();
        gui->close(0);
        join();
 #endif
        vwindows.remove_all_objects();
        zwindows.remove_all_objects();
        gui->close(0);
        join();
 #endif
-       reset_caches();
        dead_plugins->remove_all_objects();
 // must delete theme before destroying plugindb
 //  theme destructor will be deleted by delete_plugins
        dead_plugins->remove_all_objects();
 // must delete theme before destroying plugindb
 //  theme destructor will be deleted by delete_plugins
@@ -327,8 +353,9 @@ MWindow::~MWindow()
        delete gui;             gui = 0;
        delete mainindexes;     mainindexes = 0;
        delete mainprogress;    mainprogress = 0;
        delete gui;             gui = 0;
        delete mainindexes;     mainindexes = 0;
        delete mainprogress;    mainprogress = 0;
-       delete audio_cache;     audio_cache = 0;  // delete the cache after the assets
-       delete video_cache;     video_cache = 0;  // delete the cache after the assets
+ // delete the caches after the assets
+       if( audio_cache ) { audio_cache->remove_user();  audio_cache = 0; }
+       if( video_cache ) { video_cache->remove_user();  video_cache = 0; }
        delete frame_cache;     frame_cache = 0;
        delete wave_cache;      wave_cache = 0;
        delete plugin_guis;     plugin_guis = 0;
        delete frame_cache;     frame_cache = 0;
        delete wave_cache;      wave_cache = 0;
        delete plugin_guis;     plugin_guis = 0;
@@ -355,7 +382,6 @@ MWindow::~MWindow()
        colormodels.remove_all_objects();
        interlace_project_modes.remove_all_objects();
        interlace_asset_modes.remove_all_objects();
        colormodels.remove_all_objects();
        interlace_project_modes.remove_all_objects();
        interlace_asset_modes.remove_all_objects();
-       interlace_asset_fixmethods.remove_all_objects();
        sighandler->terminate();
        delete sighandler;
        delete run_lock;
        sighandler->terminate();
        delete sighandler;
        delete run_lock;
@@ -484,6 +510,13 @@ void MWindow::init_defaults(BC_Hash* &defaults, char *config_path)
 
 void MWindow::check_language()
 {
 
 void MWindow::check_language()
 {
+       char pref_locale[BCSTRLEN];
+       strcpy(pref_locale, DEFAULT_LOCALE);
+       defaults->get("LOCALE",pref_locale);
+// set LANGUAGE if pref locale != sys
+       if( strcmp(pref_locale, DEFAULT_LOCALE) )
+               setenv("LANGUAGE", pref_locale, 1);
+
        char curr_lang[BCTEXTLEN]; curr_lang[0] = 0;
        const char *env_lang = getenv("LANGUAGE");
        if( !env_lang ) env_lang = getenv("LC_ALL");
        char curr_lang[BCTEXTLEN]; curr_lang[0] = 0;
        const char *env_lang = getenv("LANGUAGE");
        if( !env_lang ) env_lang = getenv("LC_ALL");
@@ -526,21 +559,46 @@ void MWindow::get_plugin_path(char *path, const char *plug_dir, const char *fs_p
        delete [] base_path;
 }
 
        delete [] base_path;
 }
 
-int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_dir)
+/**
+* @brief Load plugins according to an index file.
+* 
+* @details Builds an ArrayList of plugin servers only if there is no
+* mismatch for file layout version, index identifier, or timestamp of
+* the built-in plugins. If OK, add the plugin servers to the global list.
+* 
+* @note If an error is returned the index file needs to be rebuilt, and
+* then this function must be called again.
+* There are two types of index files, with the same layout internally.
+* One called "Cinelerra_plugins" for built-ins, ffmpeg and lv2 .
+* The other type "ladspa_plugins.index_id" where index_id is either the
+* path or the $CIN_BUILD identifier if the path is from the running
+* AppImage itself. If there are multiple ladspa directories in the
+* path, there will be multiple index files.
+* 
+* @return  -1 if file no open, 0 if OK, 1 if error. 
+*/
+int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_dir, const char *index_id)
 {
        if( !fp ) return -1;
 {
        if( !fp ) return -1;
+       struct stat st;
+       fstat (fileno(fp), &st);        // don't bother if the file has just been created.
+       if( st.st_size < 4 ) return 1;
+       
 // load index
        fseek(fp, 0, SEEK_SET);
        int ret = 0;
        char index_line[BCTEXTLEN];
 // load index
        fseek(fp, 0, SEEK_SET);
        int ret = 0;
        char index_line[BCTEXTLEN];
-       int index_version = -1, len = strlen(plugin_dir);
+       int index_version = -1, len = strlen(index_id);
        if( !fgets(index_line, BCTEXTLEN, fp) ||
            sscanf(index_line, "%d", &index_version) != 1 ||
            index_version != PLUGIN_FILE_VERSION ||
            !fgets(index_line, BCTEXTLEN, fp) ||
            (int)strlen(index_line)-1 != len || index_line[len] != '\n' ||
        if( !fgets(index_line, BCTEXTLEN, fp) ||
            sscanf(index_line, "%d", &index_version) != 1 ||
            index_version != PLUGIN_FILE_VERSION ||
            !fgets(index_line, BCTEXTLEN, fp) ||
            (int)strlen(index_line)-1 != len || index_line[len] != '\n' ||
-           strncmp(index_line, plugin_dir, len) != 0 ) ret = 1;
-
+           strncmp(index_line, index_id, len) != 0 ) {
+//             printf("index file mismatch, version %d, index id length %d, expected id %s, file id %s\n", index_version, len, index_id, index_line);
+               ret = 1;
+       }
+       
        ArrayList<PluginServer*> plugins;
        while( !ret && !feof(fp) && fgets(index_line, BCTEXTLEN, fp) ) {
                if( index_line[0] == ';' ) continue;
        ArrayList<PluginServer*> plugins;
        while( !ret && !feof(fp) && fgets(index_line, BCTEXTLEN, fp) ) {
                if( index_line[0] == ';' ) continue;
@@ -549,6 +607,7 @@ int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_di
                char path[BCTEXTLEN], title[BCTEXTLEN];
                int64_t mtime = 0;
                if( PluginServer::scan_table(index_line, type, path, title, mtime) ) {
                char path[BCTEXTLEN], title[BCTEXTLEN];
                int64_t mtime = 0;
                if( PluginServer::scan_table(index_line, type, path, title, mtime) ) {
+//                     printf("PluginServer::scan_table failed for %s\n", index_line);
                        ret = 1; continue;
                }
                PluginServer *server = 0;
                        ret = 1; continue;
                }
                PluginServer *server = 0;
@@ -559,6 +618,7 @@ int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_di
                        char plugin_path[BCTEXTLEN];  struct stat st;
                        sprintf(plugin_path, "%s/%s", plugin_dir, path);
                        if( stat(plugin_path, &st) || st.st_mtime != mtime ) {
                        char plugin_path[BCTEXTLEN];  struct stat st;
                        sprintf(plugin_path, "%s/%s", plugin_dir, path);
                        if( stat(plugin_path, &st) || st.st_mtime != mtime ) {
+//                             printf("Plugin %s index time %ld, file time %ld\n", plugin_path, mtime, st.st_mtime); 
                                ret = 1; continue;
                        }
                        server = new PluginServer(mwindow, plugin_path, type);
                                ret = 1; continue;
                        }
                        server = new PluginServer(mwindow, plugin_path, type);
@@ -575,6 +635,7 @@ int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_di
 // Create plugin server from index entry
                server->set_title(title);
                if( server->read_table(index_line) ) {
 // Create plugin server from index entry
                server->set_title(title);
                if( server->read_table(index_line) ) {
+//                     printf("server->read_table failed for title %s, %s\n", title, index_line);
                        ret = 1;  continue;
                }
        }
                        ret = 1;  continue;
                }
        }
@@ -614,16 +675,27 @@ int MWindow::check_plugin_index(ArrayList<PluginServer*> &plugins,
        return 0;
 }
 
        return 0;
 }
 
-
+/**
+* @brief Load built-in and LV2 plugins as specified in index file,
+*        rebuild the index file if needed.
+* @param[in]   mwindow: GUI class pointer, will be NULL for batch
+*               rendering or renderfarm client.
+* @param[in]   preferences: Information from cinelerra_rc file.
+*/
 int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
 {
        if( !plugindb )
                plugindb = new ArrayList<PluginServer*>;
        init_ffmpeg();
 int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
 {
        if( !plugindb )
                plugindb = new ArrayList<PluginServer*>;
        init_ffmpeg();
-       char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN];
+       char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN], index_id[BCTEXTLEN];
        create_defaults_path(index_path, PLUGIN_FILE);
        char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
        strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
        create_defaults_path(index_path, PLUGIN_FILE);
        char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
        strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
+       // index_id is 2nd line of the index file, normally full plugin path,
+       // but fixed value if AppImage because the path changes on each run.
+       // And if the second line does not match on the next run the index is rebuilt.
+       if( getenv("APPDIR") && getenv("CINGG_BUILD")) strcpy(index_id, getenv("CINGG_BUILD"));
+       else strcpy(index_id, plugin_path);
        FILE *fp = fopen(index_path,"a+");
        if( !fp ) {
                fprintf(stderr,_("MWindow::init_plugins: "
        FILE *fp = fopen(index_path,"a+");
        if( !fp ) {
                fprintf(stderr,_("MWindow::init_plugins: "
@@ -633,19 +705,19 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
        int fd = fileno(fp), ret = -1;
        if( !flock(fd, LOCK_EX) ) {
                fseek(fp, 0, SEEK_SET);
        int fd = fileno(fp), ret = -1;
        if( !flock(fd, LOCK_EX) ) {
                fseek(fp, 0, SEEK_SET);
-               ret = load_plugin_index(mwindow, fp, plugin_path);
+               ret = load_plugin_index(mwindow, fp, plugin_path, index_id);
        }
        if( ret > 0 ) {
                ftruncate(fd, 0);
                fseek(fp, 0, SEEK_SET);
        }
        if( ret > 0 ) {
                ftruncate(fd, 0);
                fseek(fp, 0, SEEK_SET);
-               printf("init plugin index: %s\n", plugin_path);
+               printf("build plugin index for: %s\n", plugin_path);
                fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
                fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
-               fprintf(fp, "%s\n", plugin_path);
+               fprintf(fp, "%s\n", index_id);
                init_plugin_index(mwindow, preferences, fp, plugin_path);
                init_ffmpeg_index(mwindow, preferences, fp);
                init_lv2_index(mwindow, preferences, fp);
                fseek(fp, 0, SEEK_SET);
                init_plugin_index(mwindow, preferences, fp, plugin_path);
                init_ffmpeg_index(mwindow, preferences, fp);
                init_lv2_index(mwindow, preferences, fp);
                fseek(fp, 0, SEEK_SET);
-               ret = load_plugin_index(mwindow, fp, plugin_path);
+               ret = load_plugin_index(mwindow, fp, plugin_path, index_id);
        }
        if( ret ) {
                fprintf(stderr,_("MWindow::init_plugins: "
        }
        if( ret ) {
                fprintf(stderr,_("MWindow::init_plugins: "
@@ -656,25 +728,42 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
        return ret;
 }
 
        return ret;
 }
 
+/**
+* @brief Load ladspa plugins as specified in index files, for each ladspa
+*        directory keep a separate index file. Rebuild index file(s) if needed.
+* @param[in]   mwindow: GUI class pointer, will be NULL for batch
+*               rendering or renderfarm client.
+* @param[in]   preferences: Information from cinelerra_rc file.
+*/
 int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
 {
 int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
 {
+#ifdef HAVE_LADSPA
        char *path = getenv("LADSPA_PATH");
        char *path = getenv("LADSPA_PATH");
+       char *appdir = getenv("APPDIR");
        char ladspa_path[BCTEXTLEN];
        char ladspa_path[BCTEXTLEN];
-       if( !path ) {
+       if( !path ) {                   // if no env var, use CinGG's own ladspa dir
                strncpy(ladspa_path, File::get_ladspa_path(), sizeof(ladspa_path));
                path = ladspa_path;
        }
        for( int len=0; *path; path+=len ) {
                char *cp = strchr(path,':');
                len = !cp ? strlen(path) : cp-path;
                strncpy(ladspa_path, File::get_ladspa_path(), sizeof(ladspa_path));
                path = ladspa_path;
        }
        for( int len=0; *path; path+=len ) {
                char *cp = strchr(path,':');
                len = !cp ? strlen(path) : cp-path;
-               char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN];
+               char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN], index_id[BCTEXTLEN];
                memcpy(plugin_path, path, len);  plugin_path[len] = 0;
                if( cp ) ++len;
                char *plugin_dir = FileSystem::basepath(plugin_path);
                strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
                create_defaults_path(index_path, LADSPA_FILE);
                cp = index_path + strlen(index_path);
                memcpy(plugin_path, path, len);  plugin_path[len] = 0;
                if( cp ) ++len;
                char *plugin_dir = FileSystem::basepath(plugin_path);
                strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
                create_defaults_path(index_path, LADSPA_FILE);
                cp = index_path + strlen(index_path);
-               for( char *bp=plugin_path; *bp!=0; ++bp )
+               // If the first part of the plugin_path matches the APPDIR, we are
+               // referring to CinGG's ladspa, replace the path by a fixed ID. APPDIR
+               // only exists if we are running as AppImage (with variable mount points).
+               if( appdir && strncmp(plugin_path, appdir, strlen(appdir)) == 0 )
+                       strcpy(index_id, getenv("CINGG_BUILD"));
+               else strcpy(index_id, plugin_path);
+
+               // Concatenate the path, replacing '/' with '_'. 
+               for( char *bp=index_id; *bp!=0; ++bp )
                        *cp++ = *bp=='/' ? '_' : *bp;
                *cp = 0;
                FILE *fp = fopen(index_path,"a+");
                        *cp++ = *bp=='/' ? '_' : *bp;
                *cp = 0;
                FILE *fp = fopen(index_path,"a+");
@@ -686,14 +775,17 @@ int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
                int fd = fileno(fp), ret = -1;
                if( !flock(fd, LOCK_EX) ) {
                        fseek(fp, 0, SEEK_SET);
                int fd = fileno(fp), ret = -1;
                if( !flock(fd, LOCK_EX) ) {
                        fseek(fp, 0, SEEK_SET);
-                       ret = load_plugin_index(mwindow, fp, plugin_path);
+                       ret = load_plugin_index(mwindow, fp, plugin_path, index_id);
                }
                if( ret > 0 ) {
                        ftruncate(fd, 0);
                        fseek(fp, 0, SEEK_SET);
                }
                if( ret > 0 ) {
                        ftruncate(fd, 0);
                        fseek(fp, 0, SEEK_SET);
-                       init_ladspa_index(mwindow, preferences, fp, plugin_path);
+                       printf("build ladspa plugin index for: %s\n", plugin_path);
+                       fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
+                       fprintf(fp, "%s\n", index_id);
+                       init_plugin_index(mwindow, preferences, fp, plugin_path);
                        fseek(fp, 0, SEEK_SET);
                        fseek(fp, 0, SEEK_SET);
-                       ret = load_plugin_index(mwindow, fp, plugin_path);
+                       ret = load_plugin_index(mwindow, fp, plugin_path, index_id);
                }
                if( ret ) {
                        fprintf(stderr,_("MWindow::init_ladspa_plugins: "
                }
                if( ret ) {
                        fprintf(stderr,_("MWindow::init_ladspa_plugins: "
@@ -702,6 +794,7 @@ int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
                }
                fclose(fp);
        }
                }
                fclose(fp);
        }
+#endif
        return 1;
 }
 
        return 1;
 }
 
@@ -725,7 +818,7 @@ void MWindow::scan_plugin_index(MWindow *mwindow, Preferences *preferences, FILE
        char plugin_path[BCTEXTLEN];
        sprintf(plugin_path, "%s/%s", plug_dir, plug_path);
        FileSystem fs;
        char plugin_path[BCTEXTLEN];
        sprintf(plugin_path, "%s/%s", plug_dir, plug_path);
        FileSystem fs;
-       fs.set_filter( "[*.plugin][*.so]" );
+       fs.set_filter( "[*.plugin][*.so][*.dll]" );
        int result = fs.update(plugin_path);
        if( result || !fs.dir_list.total ) return;
        int vis_id = idx++;
        int result = fs.update(plugin_path);
        if( result || !fs.dir_list.total ) return;
        int vis_id = idx++;
@@ -734,7 +827,7 @@ void MWindow::scan_plugin_index(MWindow *mwindow, Preferences *preferences, FILE
                char fs_path[BCTEXTLEN], path[BCTEXTLEN];
                get_plugin_path(fs_path, 0, fs.dir_list[i]->path);
                get_plugin_path(path, plug_dir, fs_path);
                char fs_path[BCTEXTLEN], path[BCTEXTLEN];
                get_plugin_path(fs_path, 0, fs.dir_list[i]->path);
                get_plugin_path(path, plug_dir, fs_path);
-               if( fs.is_dir(fs_path) ) {
+               if( fs.is_dir(fs_path) ) {      // recursively scan child directory
                        scan_plugin_index(mwindow, preferences, fp, plug_dir, path, idx);
                        continue;
                }
                        scan_plugin_index(mwindow, preferences, fp, plug_dir, path, idx);
                        continue;
                }
@@ -933,14 +1026,7 @@ void MWindow::init_preferences()
 {
        preferences = new Preferences;
        preferences->load_defaults(defaults);
 {
        preferences = new Preferences;
        preferences->load_defaults(defaults);
-       const char *lv2_path = getenv("LV2_PATH");
-       if( lv2_path && strcmp(lv2_path, preferences->lv2_path) ) {
-               strncpy(preferences->lv2_path, lv2_path, sizeof(preferences->lv2_path));
-               remove_plugin_index();
-       }
-       else if( !lv2_path && preferences->lv2_path[0] ) {
-               File::setenv_path("LV2_PATH",preferences->lv2_path, 0);
-       }
+       File::setenv_path("LV2_PATH",preferences->lv2_path, 1);
        session = new MainSession(this);
        session->load_defaults(defaults);
        // set x11_host, screens, window_config
        session = new MainSession(this);
        session->load_defaults(defaults);
        // set x11_host, screens, window_config
@@ -960,6 +1046,58 @@ void MWindow::init_preferences()
        YUV::yuv.yuv_set_colors(preferences->yuv_color_space, preferences->yuv_color_range);
 }
 
        YUV::yuv.yuv_set_colors(preferences->yuv_color_space, preferences->yuv_color_range);
 }
 
+void MWindow::clean_backups()
+{
+    FileSystem fs;
+    int total_excess;
+    long oldest = 0;
+    int oldest_item = -1;
+    char string[BCTEXTLEN];
+
+// Delete extra backups
+    fs.set_filter("backup*.prev_*");
+    fs.complete_path(preferences->index_directory);
+    fs.update(preferences->index_directory);
+
+    // set to 50 for now
+    // total_excess = fs.dir_list.total - preferences->index_count;
+    total_excess = fs.dir_list.total - 50;
+    printf("Total excess of backups: %i \n", total_excess);
+
+//printf("MWindow::clean_backups 1 %d\n", fs.dir_list.total);
+
+    while(total_excess > 0)
+    {
+// Get oldest
+       for(int i = 0; i < fs.dir_list.total; i++)
+       {
+           fs.join_names(string, preferences->index_directory, fs.dir_list[i]->name);
+
+           if(i == 0 || fs.get_date(string) <= oldest)
+           {
+               oldest = fs.get_date(string);
+               oldest_item = i;
+           }
+       }
+
+       if(oldest_item >= 0)
+       {
+// Remove backup file
+           fs.join_names(string,
+               preferences->index_directory,
+               fs.dir_list[oldest_item]->name);
+//printf("MWindow::clean_backups 1 %s\n", string);
+           if(remove(string))
+               perror("delete_backups");
+           delete fs.dir_list[oldest_item];
+           fs.dir_list.remove_number(oldest_item);
+
+       }
+
+       total_excess--;
+    }
+}
+
 void MWindow::clean_indexes()
 {
        FileSystem fs;
 void MWindow::clean_indexes()
 {
        FileSystem fs;
@@ -1183,8 +1321,12 @@ ZWindow *MWindow::get_mixer(Mixer *&mixer)
        zwindows_lock->lock("MWindow::get_mixer");
        if( !mixer ) mixer = edl->mixers.new_mixer();
        ZWindow *zwindow = 0;
        zwindows_lock->lock("MWindow::get_mixer");
        if( !mixer ) mixer = edl->mixers.new_mixer();
        ZWindow *zwindow = 0;
-       for( int i=0; !zwindow && i<zwindows.size(); ++i )
-               if( zwindows[i]->idx < 0 ) zwindow = zwindows[i];
+       for( int i=0; !zwindow && i<zwindows.size(); ++i ) {
+               ZWindow *zwdw = zwindows[i];
+               if( zwdw->running() ) continue;
+               if( zwdw->idx >= 0 ) continue;
+               zwindow = zwindows[i];
+       }
        if( !zwindow )
                zwindows.append(zwindow = new ZWindow(this));
        zwindow->idx = mixer->idx;
        if( !zwindow )
                zwindows.append(zwindow = new ZWindow(this));
        zwindow->idx = mixer->idx;
@@ -1192,10 +1334,23 @@ ZWindow *MWindow::get_mixer(Mixer *&mixer)
        return zwindow;
 }
 
        return zwindow;
 }
 
-void MWindow::del_mixer(ZWindow *zwindow)
+ZWindow *MWindow::get_mixer(int idx)
+{
+       ZWindow *zwindow = 0;
+       zwindows_lock->lock("MWindow::get_mixer");
+       for( int i=0; !zwindow && i<zwindows.size(); ++i ) {
+               ZWindow *zwdw = zwindows[i];
+               if( !zwdw->running() ) continue;
+               if( zwdw->idx != idx ) continue;
+               zwindow = zwindows[i];
+       }
+       zwindows_lock->unlock();
+       return zwindow;
+}
+
+void MWindow::close_mixer(ZWindow *zwindow)
 {
 {
-       zwindows_lock->lock("MWindow::del_mixer 0");
-       edl->mixers.del_mixer(zwindow->idx);
+       zwindows_lock->lock("MWindow::close_mixer 0");
        if( session->selected_zwindow >= 0 ) {
                int i = zwindows.number_of(zwindow);
                if( i >= 0 && i < session->selected_zwindow )
        if( session->selected_zwindow >= 0 ) {
                int i = zwindows.number_of(zwindow);
                if( i >= 0 && i < session->selected_zwindow )
@@ -1204,7 +1359,7 @@ void MWindow::del_mixer(ZWindow *zwindow)
                        session->selected_zwindow = -1;
        }
        zwindows_lock->unlock();
                        session->selected_zwindow = -1;
        }
        zwindows_lock->unlock();
-       gui->lock_window("MWindow::del_mixer 1");
+       gui->lock_window("MWindow::close_mixer 1");
        gui->update_mixers(0, -1);
        gui->unlock_window();
 }
        gui->update_mixers(0, -1);
        gui->unlock_window();
 }
@@ -1254,10 +1409,10 @@ void MWindow::update_mixer_tracks()
        zwindows_lock->unlock();
 }
 
        zwindows_lock->unlock();
 }
 
-void MWindow::queue_mixers(EDL *edl, int command, int wait_tracking,
-               int use_inout, int update_refresh, int toggle_audio, int loop_play)
+void MWindow::handle_mixers(EDL *edl, int command, int wait_tracking,
+               int use_inout, int toggle_audio, int loop_play, float speed)
 {
 {
-       zwindows_lock->lock("MWindow::queue_mixers");
+       zwindows_lock->lock("MWindow::handle_mixers");
        for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
                ZWindow *zwindow = zwindows[vidx];
                if( zwindow->idx < 0 ) continue;
        for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
                ZWindow *zwindow = zwindows[vidx];
                if( zwindow->idx < 0 ) continue;
@@ -1279,15 +1434,15 @@ void MWindow::queue_mixers(EDL *edl, int command, int wait_tracking,
                        k = mixer->mixer_ids.size();
                        while( --k >= 0 && track->get_mixer_id() != mixer->mixer_ids[k] );
                        if( k >= 0 ) {
                        k = mixer->mixer_ids.size();
                        while( --k >= 0 && track->get_mixer_id() != mixer->mixer_ids[k] );
                        if( k >= 0 ) {
-                               track->record = 1;
+                               track->armed = 1;
                                track->play = track->data_type == TRACK_VIDEO ? 1 : 0;
                        }
                        else
                                track->play = track->data_type == TRACK_VIDEO ? 1 : 0;
                        }
                        else
-                               track->record = track->play = 0;
+                               track->armed = track->play = 0;
                }
                zwindow->change_source(mixer_edl);
                }
                zwindow->change_source(mixer_edl);
-               zwindow->issue_command(command,
-                       wait_tracking, use_inout, update_refresh, toggle_audio, loop_play);
+               zwindow->handle_mixer(command, 0,
+                               use_inout, toggle_audio, loop_play, speed);
        }
        zwindows_lock->unlock();
 }
        }
        zwindows_lock->unlock();
 }
@@ -1295,7 +1450,7 @@ void MWindow::queue_mixers(EDL *edl, int command, int wait_tracking,
 void MWindow::refresh_mixers(int dir)
 {
        int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
 void MWindow::refresh_mixers(int dir)
 {
        int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
-       queue_mixers(edl,command,0,0,1,0,0);
+       handle_mixers(edl, command, 0, 0, 0, 0, 0);
 }
 
 void MWindow::stop_mixers()
 }
 
 void MWindow::stop_mixers()
@@ -1303,21 +1458,21 @@ void MWindow::stop_mixers()
        for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
                ZWindow *zwindow = zwindows[vidx];
                if( zwindow->idx < 0 ) continue;
        for( int vidx=0; vidx<zwindows.size(); ++vidx ) {
                ZWindow *zwindow = zwindows[vidx];
                if( zwindow->idx < 0 ) continue;
-               zwindow->issue_command(STOP, 0, 0, 0, 0, 0);
+               zwindow->handle_mixer(STOP, 0, 0, 0, 0, 0);
        }
 }
 
        }
 }
 
-void MWindow::close_mixers(int destroy)
+void MWindow::close_mixers(int result)
 {
        ArrayList<ZWindow*> closed;
        zwindows_lock->lock("MWindow::close_mixers");
        for( int i=zwindows.size(); --i>=0; ) {
                ZWindow *zwindow = zwindows[i];
                if( zwindow->idx < 0 ) continue;
 {
        ArrayList<ZWindow*> closed;
        zwindows_lock->lock("MWindow::close_mixers");
        for( int i=zwindows.size(); --i>=0; ) {
                ZWindow *zwindow = zwindows[i];
                if( zwindow->idx < 0 ) continue;
-               zwindow->destroy = destroy;
+               zwindow->idx = -1;
                ZWindowGUI *zgui = zwindow->zgui;
                zgui->lock_window("MWindow::select_zwindow 0");
                ZWindowGUI *zgui = zwindow->zgui;
                zgui->lock_window("MWindow::select_zwindow 0");
-               zgui->set_done(0);
+               zgui->set_done(result);
                zgui->unlock_window();
                closed.append(zwindow);
        }
                zgui->unlock_window();
                closed.append(zwindow);
        }
@@ -1328,17 +1483,17 @@ void MWindow::close_mixers(int destroy)
        }
 }
 
        }
 }
 
-ZWindow *MWindow::create_mixer(Indexable *indexable)
+ZWindow *MWindow::create_mixer(Indexable *indexable, double position)
 {
        ArrayList<Indexable*> new_assets;
        new_assets.append(indexable);
        Track *track = edl->tracks->last;
 {
        ArrayList<Indexable*> new_assets;
        new_assets.append(indexable);
        Track *track = edl->tracks->last;
-       load_assets(&new_assets, 0, LOADMODE_NEW_TRACKS, 0, 0, 0, 0, 0, 0);
+       load_assets(&new_assets, position, LOADMODE_NEW_TRACKS, 0, 0, 0, 0, 0, 1);
        track = !track ? edl->tracks->first : track->next;
        Mixer *mixer = 0;
        ZWindow *zwindow = get_mixer(mixer);
        while( track ) {
        track = !track ? edl->tracks->first : track->next;
        Mixer *mixer = 0;
        ZWindow *zwindow = get_mixer(mixer);
        while( track ) {
-               track->play = track->record = 0;
+               track->play = track->armed = 0;
                if( track->data_type == TRACK_VIDEO ) {
                        sprintf(track->title, _("Mixer %d"), zwindow->idx);
                }
                if( track->data_type == TRACK_VIDEO ) {
                        sprintf(track->title, _("Mixer %d"), zwindow->idx);
                }
@@ -1358,11 +1513,11 @@ ZWindow *MWindow::create_mixer(Indexable *indexable)
        return zwindow;
 }
 
        return zwindow;
 }
 
-void MWindow::create_mixers()
+void MWindow::create_mixers(double position)
 {
        if( !session->drag_assets->size() &&
            !session->drag_clips->size() ) return;
 {
        if( !session->drag_assets->size() &&
            !session->drag_clips->size() ) return;
-       undo->update_undo_before();
+       undo_before();
 
        select_zwindow(0);
        ArrayList<ZWindow *>new_mixers;
 
        select_zwindow(0);
        ArrayList<ZWindow *>new_mixers;
@@ -1370,13 +1525,13 @@ void MWindow::create_mixers()
        for( int i=0; i<session->drag_assets->size(); ++i ) {
                Indexable *indexable = session->drag_assets->get(i);
                if( !indexable->have_video() ) continue;
        for( int i=0; i<session->drag_assets->size(); ++i ) {
                Indexable *indexable = session->drag_assets->get(i);
                if( !indexable->have_video() ) continue;
-               ZWindow *zwindow = create_mixer(indexable);
+               ZWindow *zwindow = create_mixer(indexable, position);
                new_mixers.append(zwindow);
        }
        for( int i=0; i<session->drag_clips->size(); ++i ) {
                Indexable *indexable = (Indexable*)session->drag_clips->get(i);
                if( !indexable->have_video() ) continue;
                new_mixers.append(zwindow);
        }
        for( int i=0; i<session->drag_clips->size(); ++i ) {
                Indexable *indexable = (Indexable*)session->drag_clips->get(i);
                if( !indexable->have_video() ) continue;
-               ZWindow *zwindow = create_mixer(indexable);
+               ZWindow *zwindow = create_mixer(indexable, position);
                new_mixers.append(zwindow);
        }
 
                new_mixers.append(zwindow);
        }
 
@@ -1386,7 +1541,7 @@ void MWindow::create_mixers()
 
        refresh_mixers();
        save_backup();
 
        refresh_mixers();
        save_backup();
-       undo->update_undo_after(_("create mixers"), LOAD_ALL);
+       undo_after(_("create mixers"), LOAD_ALL);
        restart_brender();
        gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
        sync_parameters(CHANGE_ALL);
        restart_brender();
        gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
        sync_parameters(CHANGE_ALL);
@@ -1428,6 +1583,27 @@ int MWindow::select_zwindow(ZWindow *zwindow)
 
 void MWindow::tile_mixers()
 {
 
 void MWindow::tile_mixers()
 {
+       int x1 = session->tile_mixers_x;
+       int y1 = session->tile_mixers_y;
+       int x2 = x1 + session->tile_mixers_w;
+       int y2 = y1 + session->tile_mixers_h;
+       tile_mixers(x1, y1, x2, y2);
+}
+
+void MWindow::tile_mixers(int x1, int y1, int x2, int y2)
+{
+       if( x1 == x2 || y1 == y2 ) {
+// use top left quadrent
+               int sw = gui->get_screen_w(1, -1);
+               int sh = gui->get_screen_w(1, -1);
+               x1 = 1;     y1 = 1;
+               x2 = sw/2;  y2 = sh/2;
+       }
+       else {
+               if( x1 > x2 ) { int t = x1;  x1 = x2;  x2 = t; }
+               if( y1 > y2 ) { int t = y1;  y1 = y2;  y2 = t; }
+       }
+       int ow = edl->session->output_w, oh = edl->session->output_h;
        int nz = 0;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
        int nz = 0;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
@@ -1435,34 +1611,63 @@ void MWindow::tile_mixers()
                ++nz;
        }
        if( !nz ) return;
                ++nz;
        }
        if( !nz ) return;
-       int zn = ceil(sqrt(nz));
-       int x1 = 1 + gui->get_x(), x2 = cwindow->gui->get_x();
-       int y1 = 1, y2 = gui->get_y();
-       int rw = gui->get_root_w(0), rh = gui->get_root_h(0);
-       if( x1 < 0 ) x1 = 0;
-       if( y1 < 0 ) y1 = 0;
-       if( x2 > rw ) x2 = rw;
-       if( y2 > rh ) y2 = rh;
-       int dx = x2 - x1, dy = y2 - y1;
-       int zw = dx / zn;
        int lt = BC_DisplayInfo::get_left_border();
        int top = BC_DisplayInfo::get_top_border();
        int bw = lt + BC_DisplayInfo::get_right_border();  // borders
        int bh = top + BC_DisplayInfo::get_bottom_border();
        int lt = BC_DisplayInfo::get_left_border();
        int top = BC_DisplayInfo::get_top_border();
        int bw = lt + BC_DisplayInfo::get_right_border();  // borders
        int bh = top + BC_DisplayInfo::get_bottom_border();
+       int mw = xS(10+10), mh = yS(10+10); // canvas margins
+       int dx = x2 - x1, dy = y2 - y1;
+       int64_t sz = dx * dy, best_r = sz;
+       int bx = 1, by = nz;
+       for( int nx=1; nx<=nz; ++nx ) {
+               int ny = ceil((double)nz / nx);
+               int zw = dx / nx;
+               int ww = zw - bw;
+               int hh = (ww - mw) * oh / ow + mh;
+               int zh = hh + bh;
+               int64_t za = zw*nx * zh*ny;
+               int64_t r = sz - za;
+               if( r < 0 ) continue;
+               if( r >= best_r ) continue;
+               best_r = r;
+               bx = nx;  by = ny;
+       }
+       for( int ny=1; ny<=nz; ++ny ) {
+               int nx = ceil((double)nz / ny);
+               int zh = dy / ny;
+               int hh = zh - bh;
+               int ww = (hh - mh) * ow / oh + mw;
+               int zw = ww + bw;
+               int64_t za = zw*nx * zh*ny;
+               int64_t r = sz - za;
+               if( r < 0 ) continue;
+               if( r >= best_r ) continue;
+               best_r = r;
+               bx = nx;  by = ny;
+       }
+       int zw, zh, ww, hh;
+       if( bx < by ) {
+               zh = dy / by;
+               hh = zh - bh;
+               ww = (hh - mh) * ow / oh + mw;
+               zw = ww + bw;
+       }
+       else {
+               zw = dx / bx;
+               ww = zw - bw;
+               hh = (ww - mw) * oh / ow + mh;
+               zh = hh + bh;
+       }
+
        int zx = 0, zy = 0;  // window origins
        int zx = 0, zy = 0;  // window origins
-       int mw = 10+10, mh = 10+10; // canvas margins
-       int rsz = 0, n = 0, dz = 0;
-       int ow = edl->session->output_w, oh = edl->session->output_h;
+       int n = 0;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
                if( zwindow->idx < 0 ) continue;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
                if( zwindow->idx < 0 ) continue;
-               int ww = zw - bw, hh = (ww - mw) * oh / ow + mh, zh = hh + bh;
-               if( rsz < hh ) rsz = hh;
                int xx = zx + x1, yy = zy + y1;
                int mx = x2 - zw, my = y2 - zh;
                if( xx > mx ) xx = mx;
                if( yy > my ) yy = my;
                int xx = zx + x1, yy = zy + y1;
                int mx = x2 - zw, my = y2 - zh;
                if( xx > mx ) xx = mx;
                if( yy > my ) yy = my;
-               xx += lt + dz;  yy += top + dz;
                zwindow->reposition(xx,yy, ww,hh);
                if( zwindow->running() ) {
                        ZWindowGUI *gui = (ZWindowGUI *)zwindow->get_gui();
                zwindow->reposition(xx,yy, ww,hh);
                if( zwindow->running() ) {
                        ZWindowGUI *gui = (ZWindowGUI *)zwindow->get_gui();
@@ -1470,17 +1675,24 @@ void MWindow::tile_mixers()
                        gui->BC_WindowBase::reposition_window(xx,yy, ww,hh);
                        gui->unlock_window();
                }
                        gui->BC_WindowBase::reposition_window(xx,yy, ww,hh);
                        gui->unlock_window();
                }
-               if( ++n >= zn ) {
-                       n = 0;  rsz += bh;
-                       if( (zy += rsz) > (dy - rsz) ) dz += 10;
-                       rsz = 0;
-                       zx = 0;
+               if( ++n >= bx ) {
+                       zx = 0;  zy += zh;
+                       n = 0;
                }
                else
                        zx += zw;
        }
 }
 
                }
                else
                        zx += zw;
        }
 }
 
+void MWindow::set_gang_tracks(int v)
+{
+       edl->local_session->gang_tracks = v;
+       sync_parameters(CHANGE_PARAMS);
+       gui->update(1, 1, 0, 0, 1, 0, 0);
+       gui->flush();
+}
+
+
 void MWindow::init_cache()
 {
        audio_cache = new CICache(preferences);
 void MWindow::init_cache()
 {
        audio_cache = new CICache(preferences);
@@ -1527,9 +1739,6 @@ void MWindow::init_menus()
 #define ILACEASSETMODELISTADD(x) ilacemode_to_text(string, x); \
                            interlace_asset_modes.append(new InterlacemodeItem(string, x));
 
 #define ILACEASSETMODELISTADD(x) ilacemode_to_text(string, x); \
                            interlace_asset_modes.append(new InterlacemodeItem(string, x));
 
-#define ILACEFIXMETHODLISTADD(x) ilacefixmethod_to_text(string, x); \
-                           interlace_asset_fixmethods.append(new InterlacefixmethodItem(string, x));
-
        // Interlacing Modes
        ILACEASSETMODELISTADD(ILACE_MODE_UNDETECTED); // Not included in the list for the project options.
 
        // Interlacing Modes
        ILACEASSETMODELISTADD(ILACE_MODE_UNDETECTED); // Not included in the list for the project options.
 
@@ -1542,10 +1751,7 @@ void MWindow::init_menus()
        ILACEASSETMODELISTADD(ILACE_MODE_NOTINTERLACED);
        ILACEPROJECTMODELISTADD(ILACE_MODE_NOTINTERLACED);
 
        ILACEASSETMODELISTADD(ILACE_MODE_NOTINTERLACED);
        ILACEPROJECTMODELISTADD(ILACE_MODE_NOTINTERLACED);
 
-       // Interlacing Fixing Methods
-       ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_NONE);
-       ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_UPONE);
-       ILACEFIXMETHODLISTADD(ILACE_FIXMETHOD_DOWNONE);
+       mixers_align = new MixersAlign(this);
 }
 
 void MWindow::init_indexes()
 }
 
 void MWindow::init_indexes()
@@ -1581,6 +1787,40 @@ void MWindow::init_exportedl()
        exportedl = new ExportEDL(this);
 }
 
        exportedl = new ExportEDL(this);
 }
 
+
+void MWindow::init_shuttle()
+{
+#ifdef HAVE_SHUTTLE
+       int ret = Shuttle::probe();
+       if( ret >= 0 ) {
+               shuttle = new Shuttle(this);
+               if( shuttle->read_config_file() > 0 ) {
+                       printf("shuttle: bad config file\n");
+                       delete shuttle;  shuttle = 0;
+                       return;
+               }
+               shuttle->start(ret);
+       }
+#endif
+}
+void MWindow::init_wintv()
+{
+#ifdef HAVE_WINTV
+       wintv = WinTV::probe(this);
+       if( wintv )
+               wintv->start();
+#endif
+}
+void MWindow::init_x10tv()
+{
+#ifdef HAVE_X10TV
+       x10tv = X10TV::probe(this);
+       if( x10tv )
+               x10tv->start();
+#endif
+}
+
+
 void MWindow::init_brender()
 {
        if(preferences->use_brender && !brender)
 void MWindow::init_brender()
 {
        if(preferences->use_brender && !brender)
@@ -1723,7 +1963,7 @@ int MWindow::put_commercial()
        //check it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
        //check it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
-               if( !track->record ) continue;
+               if( !track->armed ) continue;
                if( count > 0 ) { errmsg = _("multiple video tracks"); break; }
                ++count;
                int64_t units_start = track->to_units(start,0);
                if( count > 0 ) { errmsg = _("multiple video tracks"); break; }
                ++count;
                int64_t units_start = track->to_units(start,0);
@@ -1743,7 +1983,7 @@ int MWindow::put_commercial()
        //run it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
        //run it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
-               if( !track->record ) continue;
+               if( !track->armed ) continue;
                int64_t units_start = track->to_units(start,0);
                int64_t units_end = track->to_units(end,0);
                Edits *edits = track->edits;
                int64_t units_start = track->to_units(start,0);
                int64_t units_end = track->to_units(end,0);
                Edits *edits = track->edits;
@@ -1799,21 +2039,110 @@ void MWindow::stop_transport()
        gui->stop_transport(gui->get_window_lock() ? "MWindow::stop_transport" : 0);
 }
 
        gui->stop_transport(gui->get_window_lock() ? "MWindow::stop_transport" : 0);
 }
 
+void MWindow::undo_before(const char *description, void *creator)
+{
+       undo->update_undo_before(description, creator);
+}
+
+void MWindow::undo_after(const char *description, uint32_t load_flags, int changes_made)
+{
+       undo->update_undo_after(description, load_flags, changes_made);
+}
+
 void MWindow::beep(double freq, double secs, double gain)
 {
 void MWindow::beep(double freq, double secs, double gain)
 {
-       if( !proxy_beep ) proxy_beep = new ProxyBeep(this);
-       proxy_beep->tone(freq, secs, gain);
+       if( !beeper ) beeper = new Beeper(this);
+       beeper->tone(freq, secs, gain);
+}
+
+Beeper::Beeper(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+       this->mwindow = mwindow;
+       audio = new AudioDevice(mwindow);
+       playing_audio = 0;
+       interrupted = -1;
+}
+
+Beeper::~Beeper()
+{
+       stop(0);
+       delete audio;
+}
+
+void Beeper::run()
+{
+       int channels = 2;
+       int64_t bfrsz = BEEP_SAMPLE_RATE;
+       EDL *edl = mwindow->edl;
+       EDLSession *session = edl->session;
+       AudioOutConfig *aconfig = session->playback_config->aconfig;
+       audio->open_output(aconfig, BEEP_SAMPLE_RATE, bfrsz, channels, 0);
+       audio->start_playback();
+
+       double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
+       const double two_pi = 2*M_PI;
+       int64_t audio_len = BEEP_SAMPLE_RATE * secs;
+       const double dt = two_pi * freq/BEEP_SAMPLE_RATE;
+       double th = 0;
+
+       audio_pos = 0;
+       playing_audio = 1;
+       while( !interrupted ) {
+               int len = audio_len - audio_pos;
+               if( len <= 0 ) break;
+               if( len > bfrsz ) len = bfrsz;
+               int k = audio_pos;
+               for( int i=0; i<len; ++i,++k,th+=dt ) {
+                       double t = th - two_pi;
+                       if( t >= 0 ) th = t;
+                       out0[i] = out1[i] = sin(th) * gain;
+               }
+               audio->write_buffer(out, channels, len);
+               audio_pos = k;
+       }
+
+       if( !interrupted )
+               audio->set_last_buffer();
+       audio->stop_audio(interrupted ? 0 : 1);
+       playing_audio = 0;
+
+       audio->close_all();
+}
+
+void Beeper::start()
+{
+       if( running() ) return;
+       audio_pos = -1;
+       interrupted = 0;
+       Thread::start();
+}
+
+void Beeper::stop(int wait)
+{
+       if( running() && !interrupted ) {
+               interrupted = 1;
+               audio->stop_audio(wait);
+       }
+       Thread::join();
+}
+
+void Beeper::tone(double freq, double secs, double gain)
+{
+       stop(0);
+       this->freq = freq;
+       this->secs = secs;
+       this->gain = gain;
+       start();
 }
 
 }
 
+
 int MWindow::load_filenames(ArrayList<char*> *filenames,
 int MWindow::load_filenames(ArrayList<char*> *filenames,
-       int load_mode,
-       int update_filename)
+               int load_mode, int edl_mode, int update_filename)
 {
        ArrayList<EDL*> new_edls;
        ArrayList<Asset*> new_assets;
        ArrayList<File*> new_files;
 {
        ArrayList<EDL*> new_edls;
        ArrayList<Asset*> new_assets;
        ArrayList<File*> new_files;
-       const int debug = 0;
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 //     save_defaults();
        gui->start_hourglass();
 
 //     save_defaults();
        gui->start_hourglass();
@@ -1823,11 +2152,9 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
        gui->unlock_window();
        stop_playback(1);
        gui->lock_window("MWindow::load_filenames 0");
        gui->unlock_window();
        stop_playback(1);
        gui->lock_window("MWindow::load_filenames 0");
+       undo_before();
 
 
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-       undo->update_undo_before();
-
-
+const int debug = 0;
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Define new_edls and new_assets to load
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Define new_edls and new_assets to load
@@ -1838,58 +2165,53 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                File *new_file = new File;
                Asset *new_asset = new Asset(filenames->get(i));
                EDL *new_edl = new EDL;
                File *new_file = new File;
                Asset *new_asset = new Asset(filenames->get(i));
                EDL *new_edl = new EDL;
-               char string[BCTEXTLEN];
-
                new_edl->create_objects();
                new_edl->copy_session(edl, -1);
                new_file->set_program(edl->session->program_no);
 
                new_edl->create_objects();
                new_edl->copy_session(edl, -1);
                new_file->set_program(edl->session->program_no);
 
+               char string[BCTEXTLEN];
                sprintf(string, _("Loading %s"), new_asset->path);
                gui->show_message(string);
                sprintf(string, _("Loading %s"), new_asset->path);
                gui->show_message(string);
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
                ftype = new_file->open_file(preferences, new_asset, 1, 0);
 
                ftype = new_file->open_file(preferences, new_asset, 1, 0);
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
                result = 1;
                switch( ftype ) {
 // Convert media file to EDL
 
                result = 1;
                switch( ftype ) {
 // Convert media file to EDL
-               case FILE_OK:
+               case FILE_OK: {
 // Warn about odd image dimensions
                        if( new_asset->video_data &&
                            ((new_asset->width % 2) || (new_asset->height % 2)) ) {
 // Warn about odd image dimensions
                        if( new_asset->video_data &&
                            ((new_asset->width % 2) || (new_asset->height % 2)) ) {
-                               char string[BCTEXTLEN];
-                               sprintf(string, _("%s's resolution is %dx%d.\nImages with odd dimensions may not decode properly."),
+                               eprintf(_("%s's resolution is %dx%d.\n"
+                                       "Images with odd dimensions may not decode properly."),
                                        new_asset->path, new_asset->width, new_asset->height);
                                        new_asset->path, new_asset->width, new_asset->height);
-                               MainError::show_error(string);
                        }
 
                        if( new_asset->program >= 0 &&
                            edl->session->program_no != new_asset->program ) {
                        }
 
                        if( new_asset->program >= 0 &&
                            edl->session->program_no != new_asset->program ) {
-                               char string[BCTEXTLEN];
-                               sprintf(string, _("%s's index was built for program number %d\n"
+                               eprintf(_("%s's index was built for program number %d\n"
                                        "Playback preference is %d.\n  Using program %d."),
                                        new_asset->path, new_asset->program,
                                        edl->session->program_no, new_asset->program);
                                        "Playback preference is %d.\n  Using program %d."),
                                        new_asset->path, new_asset->program,
                                        edl->session->program_no, new_asset->program);
-                               MainError::show_error(string);
                        }
 
                        }
 
-                       if( load_mode != LOADMODE_RESOURCESONLY ) {
-                               RecordLabels *labels = edl->session->label_cells ?
-                                       new RecordLabels(new_file) : 0;
-                               asset_to_edl(new_edl, new_asset, labels);
-                               new_edls.append(new_edl);
-                               new_edl->add_user();
-                               delete labels;
-                       }
-                       else {
+                       if( load_mode == LOADMODE_RESOURCESONLY ) {
                                new_assets.append(new_asset);
                                new_asset->add_user();
                                new_assets.append(new_asset);
                                new_asset->add_user();
+                               result = 0;
+                               break;
                        }
 
                        }
 
-// Set filename to nothing for assets since save EDL would overwrite them.
+                       RecordLabels *labels = edl->session->label_cells ?
+                               new RecordLabels(new_file) : 0;
+                       asset_to_edl(new_edl, new_asset, labels);
+                       new_edls.append(new_edl);
+                       new_edl->add_user();
+                       delete labels;
+
                        if( load_mode == LOADMODE_REPLACE ||
                            load_mode == LOADMODE_REPLACE_CONCATENATE ) {
                        if( load_mode == LOADMODE_REPLACE ||
                            load_mode == LOADMODE_REPLACE_CONCATENATE ) {
+// Set filename to nothing for assets since save EDL would overwrite them.
                                set_filename("");
 // Reset timeline position
                                for( int i=0; i<TOTAL_PANES; ++i ) {
                                set_filename("");
 // Reset timeline position
                                for( int i=0; i<TOTAL_PANES; ++i ) {
@@ -1897,16 +2219,22 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                        new_edl->local_session->track_start[i] = 0;
                                }
                        }
                                        new_edl->local_session->track_start[i] = 0;
                                }
                        }
-
                        result = 0;
                        result = 0;
-                       break;
-
-// File not found
-               case FILE_NOT_FOUND:
+                       break; }
+// File is a list and size of listed files don't match
+  case FILE_SIZE_DONT_MATCH: {
+   eprintf(_("File sizes don't match"));
+   sprintf(string, _("File sizes don't match"));
+   gui->show_message(string, theme->message_error);
+   gui->update_default_message();
+   break; }
+
+               case FILE_NOT_FOUND: {
+                       eprintf(_("Failed to open %s"), new_asset->path);
                        sprintf(string, _("Failed to open %s"), new_asset->path);
                        gui->show_message(string, theme->message_error);
                        gui->update_default_message();
                        sprintf(string, _("Failed to open %s"), new_asset->path);
                        gui->show_message(string, theme->message_error);
                        gui->update_default_message();
-                       break;
+                       break; }
 
 // Unknown format
                case FILE_UNRECOGNIZED_CODEC: {
 
 // Unknown format
                case FILE_UNRECOGNIZED_CODEC: {
@@ -1933,11 +2261,6 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Prompt user
                        if( result ) {
 
 // Prompt user
                        if( result ) {
-                               char string[BCTEXTLEN];
-                               FileSystem fs;
-                               fs.extract_name(string, new_asset->path);
-
-                               strcat(string, _("'s format couldn't be determined."));
                                new_asset->audio_data = 1;
                                new_asset->format = FILE_PCM;
                                new_asset->channels = defaults->get("AUDIO_CHANNELS", 2);
                                new_asset->audio_data = 1;
                                new_asset->format = FILE_PCM;
                                new_asset->channels = defaults->get("AUDIO_CHANNELS", 2);
@@ -1947,6 +2270,9 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                new_asset->signed_ = defaults->get("SIGNED_", 1);
                                new_asset->header = defaults->get("HEADER", 0);
 
                                new_asset->signed_ = defaults->get("SIGNED_", 1);
                                new_asset->header = defaults->get("HEADER", 0);
 
+                               FileSystem fs;
+                               fs.extract_name(string, new_asset->path);
+                               strcat(string, _("'s format couldn't be determined."));
                                FileFormat fwindow(this);
                                fwindow.create_objects(new_asset, string);
                                result = fwindow.run_window();
                                FileFormat fwindow(this);
                                fwindow.create_objects(new_asset, string);
                                result = fwindow.run_window();
@@ -1959,7 +2285,6 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                defaults->update("HEADER", new_asset->header);
                                save_defaults();
                        }
                                defaults->update("HEADER", new_asset->header);
                                save_defaults();
                        }
-
 // Append to list
                        if( !result ) {
 // Recalculate length
 // Append to list
                        if( !result ) {
 // Recalculate length
@@ -1980,16 +2305,15 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                        new_asset->add_user();
                                }
                        }
                                        new_asset->add_user();
                                }
                        }
-                       else {
-                               result = 1;
-                       }
                        break; }
 
                case FILE_IS_XML: {
                        FileXML xml_file;
                        break; }
 
                case FILE_IS_XML: {
                        FileXML xml_file;
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-                       xml_file.read_from_file(filenames->get(i));
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
+                       const char *filename = filenames->get(i);
+                       if( xml_file.read_from_file(filename, 1) ) {
+                               eprintf(_("Error: unable to open:\n  %s"), filename);
+                               break;
+                       }
                        const char *cin_version = 0;
                        while( !xml_file.read_tag() ) {
                                if( xml_file.tag.title_is("EDL") ) {
                        const char *cin_version = 0;
                        while( !xml_file.read_tag() ) {
                                if( xml_file.tag.title_is("EDL") ) {
@@ -1999,60 +2323,73 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                        }
                        xml_file.rewind();
                        if( !cin_version ) {
                        }
                        xml_file.rewind();
                        if( !cin_version ) {
-                               eprintf(_("XML file %s\n not from cinelerra."),filenames->get(i));
+                               eprintf(_("XML file %s\n not from cinelerra."),filename);
                                char string[BCTEXTLEN];
                                char string[BCTEXTLEN];
-                               sprintf(string,_("Unknown %s"), filenames->get(i));
+                               sprintf(string,_("Unknown %s"), filename);
                                gui->show_message(string);
                                gui->show_message(string);
-                               result = 1;
                                break;
                        }
                        if( strcmp(cin_version, CINELERRA_VERSION) &&
                            strcmp(cin_version, "Unify") &&
                            strcmp(cin_version, "5.1") ) {
                                break;
                        }
                        if( strcmp(cin_version, CINELERRA_VERSION) &&
                            strcmp(cin_version, "Unify") &&
                            strcmp(cin_version, "5.1") ) {
-                               char string[BCTEXTLEN];
-                               snprintf(string, sizeof(string),
-                                        _("Warning: XML from cinelerra version %s\n"
+                               eprintf(_("Warning: XML from cinelerra version %s\n"
                                        "Session data may be incompatible."), cin_version);
                                        "Session data may be incompatible."), cin_version);
-                               show_warning(&preferences->warn_version, string);
                        }
                        }
-                       if( load_mode == LOADMODE_NESTED ) {
-// Load temporary EDL for nesting.
-                               EDL *nested_edl = new EDL;
-                               nested_edl->create_objects();
-                               nested_edl->load_xml(&xml_file, LOAD_ALL);
-//printf("MWindow::load_filenames %p %s\n", nested_edl, nested_edl->project_path);
-                               new_edl->create_nested(nested_edl);
-                               new_edl->set_path(filenames->get(i));
-                               nested_edl->Garbage::remove_user();
+                       if( new_edl->load_xml(&xml_file, LOAD_ALL) ) {
+                               eprintf(_("Error: unable to load:\n  %s"), filename);
+                               break;
                        }
                        }
-                       else {
-// Load EDL for pasting
-                               new_edl->load_xml(&xml_file, LOAD_ALL);
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-                               test_plugins(new_edl, filenames->get(i));
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-
-                               if( load_mode == LOADMODE_REPLACE ||
-                                   load_mode == LOADMODE_REPLACE_CONCATENATE ) {
-                                       strcpy(session->filename, filenames->get(i));
-                                       strcpy(new_edl->local_session->clip_title,
-                                               filenames->get(i));
-                                       if(update_filename)
-                                               set_filename(new_edl->local_session->clip_title);
-                               }
-                               else if( load_mode == LOADMODE_RESOURCESONLY ) {
-                                       strcpy(new_edl->local_session->clip_title,
-                                               filenames->get(i));
-                                       struct stat st;
-                                       time_t t = !stat(filenames->get(i),&st) ?
-                                               st.st_mtime : time(&t);
-                                       ctime_r(&t, new_edl->local_session->clip_notes);
+                       test_plugins(new_edl, filename);
+                       int groups = new_edl->regroup(session->group_number);
+                       session->group_number += groups;
+                       switch( edl_mode ) {
+                       case LOADMODE_EDL_CLIP: {
+                               strcpy(new_edl->local_session->clip_title,
+                                       filenames->get(i));
+                                       struct stat st;
+                               time_t t = !stat(filenames->get(i),&st) ?
+                                               st.st_mtime : time(&t);
+                               ctime_r(&t, new_edl->local_session->clip_notes);
+                               switch( load_mode ) {
+                               case LOADMODE_REPLACE:
+                               case LOADMODE_REPLACE_CONCATENATE:
+                                       strcpy(session->filename, filename);
+                                       if( update_filename ) set_filename(filename);
+                                       break;
                                }
                                }
+                               result = 0;
+                               break; }
+                       case LOADMODE_EDL_NESTED: {
+                                EDL *nested_edl = new EDL;
+                               nested_edl->create_objects();
+                               nested_edl->copy_session(edl, -1);
+                                nested_edl->create_nested(new_edl);
+                                nested_edl->set_path(filename);
+                                new_edl->remove_user();
+                               new_edl = nested_edl;
+                               result = 0;
+                               break; }
+                       case LOADMODE_EDL_FILEREF: {
+                               result = create_ref(new_asset, new_edl);
+                               if( result ) break;
+                               new_assets.append(new_asset);
+                               new_asset->add_user();
+                               new_edl->remove_user();
+                               new_edl = new EDL;
+                               new_edl->create_objects();
+                               new_edl->copy_session(edl, -1);
+                               asset_to_edl(new_edl, new_asset);
+                               delete new_file;
+                               new_file = new File;
+                               result = new_file->open_file(preferences, new_asset, 1, 0);
+                               break; }
                        }
                        }
-
-                       new_edls.append(new_edl);
-                       new_edl->add_user();
-                       result = 0;
+                       if( !result ) {
+                               new_edls.append(new_edl);
+                               new_edl->add_user();
+                       }
+                       else
+                               eprintf(_("Error: Unable to load xml:\n  %s"), new_asset->path);
                        break; }
                }
 
                        break; }
                }
 
@@ -2065,114 +2402,76 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
-
        if(!result) {
                gui->reset_default_message();
                gui->default_message();
        }
 
        if(!result) {
                gui->reset_default_message();
                gui->default_message();
        }
 
-
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Paste them.
 // Don't back up here.
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Paste them.
 // Don't back up here.
-       if(new_edls.size())
-       {
+       if( new_edls.size() ) {
 // For pasting, clear the active region
 // For pasting, clear the active region
-               if(load_mode == LOADMODE_PASTE ||
-                       load_mode == LOADMODE_NESTED)
-               {
+               if( load_mode == LOADMODE_PASTE ) {
                        double start = edl->local_session->get_selectionstart();
                        double end = edl->local_session->get_selectionend();
                        if(!EQUIV(start, end))
                        double start = edl->local_session->get_selectionstart();
                        double end = edl->local_session->get_selectionend();
                        if(!EQUIV(start, end))
-                               edl->clear(start,
-                                       end,
+                               edl->clear(start, end,
                                        edl->session->labels_follow_edits,
                                        edl->session->plugins_follow_edits,
                                        edl->session->autos_follow_edits);
                                        edl->session->labels_follow_edits,
                                        edl->session->plugins_follow_edits,
                                        edl->session->autos_follow_edits);
+
+                       paste_edls(&new_edls, load_mode, 0, -1,
+                               edl->session->labels_follow_edits,
+                               edl->session->plugins_follow_edits,
+                               edl->session->autos_follow_edits,
+                               0); // overwrite
                }
                }
+               else if( load_mode == LOADMODE_NEW_TRACKS &&
+                        edl_mode != LOADMODE_EDL_CLIP )
+                       paste_edls(&new_edls, load_mode, 0, -1, 0, 0, 0, 0);
+               else if( load_mode != LOADMODE_RESOURCESONLY ||
+                        edl_mode == LOADMODE_EDL_CLIP )
+                       paste_edls(&new_edls, load_mode, 0, -1, 1, 1, 1, 0);
+               else
+                       paste_edls(&new_edls, LOADMODE_NOTHING, 0, -1, 0, 0, 0, 0);
 
 
-               paste_edls(&new_edls, load_mode, 0, -1,
-                       edl->session->labels_follow_edits,
-                       edl->session->plugins_follow_edits,
-                       edl->session->autos_follow_edits,
-                       0); // overwrite
        }
 
        }
 
-
-
-
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-
 // Add new assets to EDL and schedule assets for index building.
        int got_indexes = 0;
        for( int i=0; i<new_edls.size(); ++i ) {
                EDL *new_edl = new_edls[i];
                for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
 // Add new assets to EDL and schedule assets for index building.
        int got_indexes = 0;
        for( int i=0; i<new_edls.size(); ++i ) {
                EDL *new_edl = new_edls[i];
                for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
-                       mainindexes->add_next_asset(0, new_edl->nested_edls[j]);
+                       mainindexes->add_indexable(new_edl->nested_edls[j]);
                        edl->nested_edls.update_index(new_edl->nested_edls[j]);
                        got_indexes = 1;
                }
 
        }
 
                        edl->nested_edls.update_index(new_edl->nested_edls[j]);
                        got_indexes = 1;
                }
 
        }
 
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
        for( int i=0; i<new_assets.size(); ++i ) {
                Asset *new_asset = new_assets[i];
        for( int i=0; i<new_assets.size(); ++i ) {
                Asset *new_asset = new_assets[i];
-
-               File *new_file = 0;
-               int got_it = 0;
-               for( int j=0; j<new_files.size(); ++j ) {
-                       new_file = new_files[j];
-                       if( !strcmp(new_file->asset->path, new_asset->path) ) {
-                               got_it = 1;
-                               break;
-                       }
-               }
-
-               mainindexes->add_next_asset(got_it ? new_file : 0, new_asset);
+               mainindexes->add_indexable(new_asset);
                edl->assets->update(new_asset);
                got_indexes = 1;
        }
                edl->assets->update(new_asset);
                got_indexes = 1;
        }
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 // Start examining next batch of index files
        if(got_indexes) mainindexes->start_build();
 
 // Start examining next batch of index files
        if(got_indexes) mainindexes->start_build();
-if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 
 
+// reload renderengine edl or the plugin guis will flip out
+       sync_parameters(CHANGE_ALL);
 // Open plugin GUIs
 // Open plugin GUIs
-       Track *track = edl->tracks->first;
-       while(track)
-       {
-               for(int j = 0; j < track->plugin_set.size(); j++)
-               {
-                       PluginSet *plugins = track->plugin_set[j];
-                       Plugin *plugin = plugins->get_first_plugin();
-
-                       while(plugin)
-                       {
-                               if(load_mode == LOADMODE_REPLACE ||
-                                       load_mode == LOADMODE_REPLACE_CONCATENATE)
-                               {
-                                       if(plugin->plugin_type == PLUGIN_STANDALONE &&
-                                               plugin->show)
-                                       {
-                                               show_plugin(plugin);
-                                       }
-                               }
-
-                               plugin = (Plugin*)plugin->next;
-                       }
-               }
-
-               track = track->next;
-       }
+       show_plugins();
 
 
-       // if just opening one new resource in replace mode
-       if( ftype != FILE_IS_XML &&
-           ( load_mode == LOADMODE_REPLACE ||
-             load_mode == LOADMODE_REPLACE_CONCATENATE ) ) {
+       // opening new session
+       if( ( load_mode == LOADMODE_REPLACE ||
+             load_mode == LOADMODE_REPLACE_CONCATENATE ) &&
+           (ftype != FILE_IS_XML || edl_mode != LOADMODE_EDL_CLIP) ) {
                select_asset(0, 0);
                select_asset(0, 0);
+               edl->session->proxy_state = PROXY_INACTIVE;
                edl->session->proxy_scale = 1;
                edl->session->proxy_disabled_scale = 1;
                edl->session->proxy_use_scaler = 0;
                edl->session->proxy_scale = 1;
                edl->session->proxy_disabled_scale = 1;
                edl->session->proxy_use_scaler = 0;
@@ -2183,13 +2482,15 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                edl->local_session->loop_playback = 0;
                edl->local_session->set_selectionstart(0);
                edl->local_session->set_selectionend(0);
                edl->local_session->loop_playback = 0;
                edl->local_session->set_selectionstart(0);
                edl->local_session->set_selectionend(0);
+               edl->local_session->unset_inpoint();
+               edl->local_session-> unset_outpoint();
                set_brender_active(0, 0);
                fit_selection();
                goto_start();
        }
 
                set_brender_active(0, 0);
                fit_selection();
                goto_start();
        }
 
-       if( ( edl->session->proxy_auto_scale && edl->session->proxy_scale != 1 ) &&
-           ( load_mode != LOADMODE_REPLACE && load_mode != LOADMODE_REPLACE_CONCATENATE ) ) {
+       if( edl->session->proxy_state != PROXY_INACTIVE && edl->session->proxy_auto_scale &&
+           load_mode != LOADMODE_REPLACE && load_mode != LOADMODE_REPLACE_CONCATENATE ) {
                ArrayList<Indexable *> orig_idxbls;
                for( int i=0; i<new_assets.size(); ++i )
                        orig_idxbls.append(new_assets.get(i));
                ArrayList<Indexable *> orig_idxbls;
                for( int i=0; i<new_assets.size(); ++i )
                        orig_idxbls.append(new_assets.get(i));
@@ -2209,18 +2510,19 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                gui->unlock_window(); // to update progress bar
                int ret = render_proxy(orig_idxbls);
                gui->lock_window("MWindow::load_filenames");
                gui->unlock_window(); // to update progress bar
                int ret = render_proxy(orig_idxbls);
                gui->lock_window("MWindow::load_filenames");
-               if( ret >= 0 && edl->session->proxy_beep ) {
+               float gain = edl->session->proxy_beep;
+               if( ret >= 0 && gain > 0 ) {
                        if( ret > 0 )
                        if( ret > 0 )
-                               beep(2000., 1.5, 0.5);
+                               beep(2000., 1.5, gain);
                        else
                        else
-                               beep(4000., 0.25, 0.5);
+                               beep(4000., 0.25, gain);
                }
        }
 
 // need to update undo before project, since mwindow is unlocked & a new load
 // can begin here.  Should really prevent loading until we're done.
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                }
        }
 
 // need to update undo before project, since mwindow is unlocked & a new load
 // can begin here.  Should really prevent loading until we're done.
 if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
-       undo->update_undo_after(_("load"), LOAD_ALL);
+       undo_after(_("load"), LOAD_ALL);
 
        for(int i = 0; i < new_edls.size(); i++)
        {
 
        for(int i = 0; i < new_edls.size(); i++)
        {
@@ -2266,9 +2568,8 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
        Asset *format_asset = new Asset;
        format_asset->format = FILE_FFMPEG;
        format_asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
        Asset *format_asset = new Asset;
        format_asset->format = FILE_FFMPEG;
        format_asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
-       ProxyRender proxy_render(this, format_asset);
        int new_scale = edl->session->proxy_scale;
        int new_scale = edl->session->proxy_scale;
-       int use_scaler = edl->session->proxy_use_scaler;
+       ProxyRender proxy_render(this, format_asset, new_scale);
 
        for( int i=0; i<new_idxbls.size(); ++i ) {
                Indexable *orig = new_idxbls.get(i);
 
        for( int i=0; i<new_idxbls.size(); ++i ) {
                Indexable *orig = new_idxbls.get(i);
@@ -2285,8 +2586,7 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
 // render needed proxies
        int result = proxy_render.create_needed_proxies(new_scale);
        if( !result ) {
 // render needed proxies
        int result = proxy_render.create_needed_proxies(new_scale);
        if( !result ) {
-               add_proxy(use_scaler,
-                       &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
+               add_proxy(&proxy_render.orig_idxbls, &proxy_render.orig_proxies);
        }
        format_asset->remove_user();
        return !result ? proxy_render.needed_proxies.size() : -1;
        }
        format_asset->remove_user();
        return !result ? proxy_render.needed_proxies.size() : -1;
@@ -2295,34 +2595,42 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
 int MWindow::enable_proxy()
 {
        int ret = 0;
 int MWindow::enable_proxy()
 {
        int ret = 0;
-       if( edl->session->proxy_scale == 1 &&
-           edl->session->proxy_disabled_scale != 1 ) {
+       if( edl->session->proxy_state == PROXY_DISABLED ) {
                int new_scale = edl->session->proxy_disabled_scale;
                int new_use_scaler = edl->session->proxy_use_scaler;
                int new_scale = edl->session->proxy_disabled_scale;
                int new_use_scaler = edl->session->proxy_use_scaler;
-               edl->session->proxy_disabled_scale = 1;
                Asset *asset = new Asset;
                asset->format = FILE_FFMPEG;
                asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
                ret = to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
                Asset *asset = new Asset;
                asset->format = FILE_FFMPEG;
                asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
                ret = to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
-               if( ret > 0 )
-                       beep(2000., 1.5, 0.5);
+               if( ret > 0 ) {
+                       float gain = edl->session->proxy_beep;
+                       beep(2000., 1.5, gain);
+               }
+               edl->session->proxy_disabled_scale = 1;
+               edl->session->proxy_state = PROXY_ACTIVE;
+               gui->lock_window("MWindow::to_proxy");
+               update_project(LOADMODE_REPLACE);
+               gui->unlock_window();
        }
        return 1;
 }
 
 int MWindow::disable_proxy()
 {
        }
        return 1;
 }
 
 int MWindow::disable_proxy()
 {
-       if( edl->session->proxy_scale != 1 &&
-           edl->session->proxy_disabled_scale == 1 ) {
-               int new_scale = 1;
+       if( edl->session->proxy_state == PROXY_ACTIVE ) {
+               int old_scale = edl->session->proxy_scale, new_scale = 0;
                int new_use_scaler = edl->session->proxy_use_scaler;
                int new_use_scaler = edl->session->proxy_use_scaler;
-               edl->session->proxy_disabled_scale = edl->session->proxy_scale;
                Asset *asset = new Asset;
                asset->format = FILE_FFMPEG;
                asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
                to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
                Asset *asset = new Asset;
                asset->format = FILE_FFMPEG;
                asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
                to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
+               edl->session->proxy_state = PROXY_DISABLED;
+               edl->session->proxy_disabled_scale = old_scale;
+               gui->lock_window("MWindow::to_proxy");
+               update_project(LOADMODE_REPLACE);
+               gui->unlock_window();
        }
        return 1;
 }
        }
        return 1;
 }
@@ -2334,14 +2642,16 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
 
        edl->Garbage::add_user();
        save_backup();
 
        edl->Garbage::add_user();
        save_backup();
-       undo->update_undo_before(_("proxy"), this);
-       ProxyRender proxy_render(this, asset);
+       undo_before(_("proxy"), this);
+       int asset_scale = !new_scale ? 0 :
+                       !new_use_scaler ? 1 : new_scale;
+       ProxyRender proxy_render(this, asset, asset_scale);
 
 // revert project to original size from current size
 // remove all session proxy assets at the at the current proxy_scale
        int proxy_scale = edl->session->proxy_scale;
 
 
 // revert project to original size from current size
 // remove all session proxy assets at the at the current proxy_scale
        int proxy_scale = edl->session->proxy_scale;
 
-       if( proxy_scale > 1 ) {
+       if( edl->session->proxy_state == PROXY_ACTIVE ) {
                Asset *orig_asset = edl->assets->first;
                for( ; orig_asset; orig_asset=orig_asset->next ) {
                        char new_path[BCTEXTLEN];
                Asset *orig_asset = edl->assets->first;
                for( ; orig_asset; orig_asset=orig_asset->next ) {
                        char new_path[BCTEXTLEN];
@@ -2363,8 +2673,6 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
                        EDL *orig_nested = edl->nested_edls[i];
                        char new_path[BCTEXTLEN];
                for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
                        EDL *orig_nested = edl->nested_edls[i];
                        char new_path[BCTEXTLEN];
-                       if( !ProxyRender::from_proxy_path(new_path, orig_nested, proxy_scale) )
-                               continue;
                        proxy_render.to_proxy_path(new_path, orig_nested, proxy_scale);
 // test if proxy asset was already added to proxy_assets
                        int got_it = 0;
                        proxy_render.to_proxy_path(new_path, orig_nested, proxy_scale);
 // test if proxy asset was already added to proxy_assets
                        int got_it = 0;
@@ -2382,7 +2690,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                }
 
 // convert from the proxy assets to the original assets
                }
 
 // convert from the proxy assets to the original assets
-               edl->set_proxy(1, 0, &proxy_assets, &orig_idxbls);
+               edl->set_proxy(0, 0, &proxy_assets, &orig_idxbls);
 
 // remove the references
                for( int i=0; i<proxy_assets.size(); ++i ) {
 
 // remove the references
                for( int i=0; i<proxy_assets.size(); ++i ) {
@@ -2403,7 +2711,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
        confirm_paths.set_array_delete();
 
 // convert to new size if not original size
        confirm_paths.set_array_delete();
 
 // convert to new size if not original size
-       if( new_scale != 1 ) {
+       if( new_scale ) {
                FileSystem fs;
                Asset *orig = edl->assets->first;
                for( ; orig; orig=orig->next ) {
                FileSystem fs;
                Asset *orig = edl->assets->first;
                for( ; orig; orig=orig->next ) {
@@ -2431,7 +2739,10 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                                proxy_render.add_needed(orig_nested, proxy);
                        }
                }
                                proxy_render.add_needed(orig_nested, proxy);
                        }
                }
+               edl->session->proxy_state = PROXY_ACTIVE;
        }
        }
+       else
+               edl->session->proxy_state = PROXY_INACTIVE;
 
        int result = 0;
 // test for existing files
 
        int result = 0;
 // test for existing files
@@ -2440,25 +2751,21 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                confirm_paths.remove_all_objects();
        }
 
                confirm_paths.remove_all_objects();
        }
 
-       if( !result )
+       if( !result && new_scale )
                result = proxy_render.create_needed_proxies(new_scale);
 
                result = proxy_render.create_needed_proxies(new_scale);
 
-       if( !result ) // resize project
+       if( !result ) // resize project
                edl->set_proxy(new_scale, new_use_scaler,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
                edl->set_proxy(new_scale, new_use_scaler,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
-
-       undo->update_undo_after(_("proxy"), LOAD_ALL);
+       }
+       undo_after(_("proxy"), LOAD_ALL);
        edl->Garbage::remove_user();
        restart_brender();
 
        edl->Garbage::remove_user();
        restart_brender();
 
-       gui->lock_window("MWindow::to_proxy");
-       update_project(LOADMODE_REPLACE);
-       gui->unlock_window();
-
        return !result ? proxy_render.needed_proxies.size() : -1;
 }
 
        return !result ? proxy_render.needed_proxies.size() : -1;
 }
 
-void MWindow::test_plugins(EDL *new_edl, char *path)
+void MWindow::test_plugins(EDL *new_edl, const char *path)
 {
        char string[BCTEXTLEN];
 
 {
        char string[BCTEXTLEN];
 
@@ -2547,12 +2854,12 @@ void MWindow::create_objects(int want_gui,
        init_3d();
 
        if(debug) PRINT_TRACE
        init_3d();
 
        if(debug) PRINT_TRACE
-       show_splash();
 
        if(debug) PRINT_TRACE
        default_standard = default_std();
        init_defaults(defaults, config_path);
        check_language();
 
        if(debug) PRINT_TRACE
        default_standard = default_std();
        init_defaults(defaults, config_path);
        check_language();
+       show_splash();
        init_preferences();
        if(splash_window)
                splash_window->update_status(_("Initializing Plugins"));
        init_preferences();
        if(splash_window)
                splash_window->update_status(_("Initializing Plugins"));
@@ -2576,6 +2883,10 @@ void MWindow::create_objects(int want_gui,
        strcat(string, "/" FONT_SEARCHPATH);
        BC_Resources::init_fontconfig(string);
        if(debug) PRINT_TRACE
        strcat(string, "/" FONT_SEARCHPATH);
        BC_Resources::init_fontconfig(string);
        if(debug) PRINT_TRACE
+// use if plugged
+       init_x10tv();
+       if( !x10tv )
+               init_wintv();
 
 // Default project created here
        init_edl();
 
 // Default project created here
        init_edl();
@@ -2615,6 +2926,7 @@ void MWindow::create_objects(int want_gui,
        init_render();
        if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
 
        init_render();
        if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
 
+       init_shuttle();
        init_brender();
        init_exportedl();
        init_commercials();
        init_brender();
        init_exportedl();
        init_commercials();
@@ -2625,7 +2937,7 @@ void MWindow::create_objects(int want_gui,
 
        if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
 
 
        if(debug) printf("MWindow::create_objects %d total_time=%d\n", __LINE__, (int)timer.get_difference());
 
-       plugin_guis = new ArrayList<PluginServer*>;
+       plugin_guis = new PluginGUIs(this);
        dead_plugins = new ArrayList<PluginServer*>;
        keyframe_threads = new ArrayList<KeyFrameThread*>;
 
        dead_plugins = new ArrayList<PluginServer*>;
        keyframe_threads = new ArrayList<KeyFrameThread*>;
 
@@ -2699,7 +3011,9 @@ void MWindow::create_objects(int want_gui,
        gui->show_window();
        gui->raise_window();
        gui->unlock_window();
        gui->show_window();
        gui->raise_window();
        gui->unlock_window();
-
+       cwindow->gui->lock_window("MWindow::create_objects 1");
+       cwindow->gui->tool_panel->raise_tool();
+       cwindow->gui->unlock_window();
        if(debug) PRINT_TRACE
 
        if(preferences->use_tipwindow)
        if(debug) PRINT_TRACE
 
        if(preferences->use_tipwindow)
@@ -2718,6 +3032,12 @@ void MWindow::create_objects(int want_gui,
        BC_WindowBase::get_resources()->vframe_shm = 1;
 }
 
        BC_WindowBase::get_resources()->vframe_shm = 1;
 }
 
+int MWindow::uses_opengl()
+{
+       if( !playback_3d || !playback_3d->running() ) return 0;
+       PlaybackConfig *playback_config = edl->session->playback_config;
+       return playback_config->vconfig->driver == PLAYBACK_X11_GL ? 1 : 0;
+}
 
 void MWindow::show_splash()
 {
 
 void MWindow::show_splash()
 {
@@ -2773,7 +3093,7 @@ void MWindow::run()
        run_lock->unlock();
 }
 
        run_lock->unlock();
 }
 
-void MWindow::show_vwindow()
+void MWindow::show_vwindow(int raise)
 {
        int total_running = 0;
        session->show_vwindow = 1;
 {
        int total_running = 0;
        session->show_vwindow = 1;
@@ -2783,23 +3103,40 @@ void MWindow::show_vwindow()
        for(int j = 0; j < vwindows.size(); j++) {
                VWindow *vwindow = vwindows[j];
                if( !vwindow->is_running() ) continue;
        for(int j = 0; j < vwindows.size(); j++) {
                VWindow *vwindow = vwindows[j];
                if( !vwindow->is_running() ) continue;
+               total_running++;
+               if( !raise && !vwindow->gui->is_hidden() ) continue;
                vwindow->gui->lock_window("MWindow::show_vwindow");
                vwindow->gui->show_window(0);
                vwindow->gui->raise_window();
                vwindow->gui->flush();
                vwindow->gui->unlock_window();
                vwindow->gui->lock_window("MWindow::show_vwindow");
                vwindow->gui->show_window(0);
                vwindow->gui->raise_window();
                vwindow->gui->flush();
                vwindow->gui->unlock_window();
-               total_running++;
        }
        }
-
 // If no windows visible
 // If no windows visible
-       if(!total_running)
-       {
+       if( !total_running )
                get_viewer(1, DEFAULT_VWINDOW);
                get_viewer(1, DEFAULT_VWINDOW);
-       }
 
        gui->mainmenu->show_vwindow->set_checked(1);
 }
 
 
        gui->mainmenu->show_vwindow->set_checked(1);
 }
 
+
+void MWindow::hide_vwindow(int raise)
+{
+       session->show_vwindow = 0;
+       int total_running = 0;
+
+       for(int j = 0; j < vwindows.size(); j++) {
+               VWindow *vwindow = vwindows[j];
+               if( !vwindow->is_running() ) continue;
+               total_running++;
+               if( !raise && !vwindow->gui->is_hidden() ) continue;
+               vwindow->gui->lock_window("MWindow::show_vwindow");
+               vwindow->gui->hide_window(0);
+               vwindow->gui->unlock_window();
+       }
+       gui->mainmenu->show_vwindow->set_checked(0);
+}
+
+
 void MWindow::show_awindow()
 {
        session->show_awindow = 1;
 void MWindow::show_awindow()
 {
        session->show_awindow = 1;
@@ -2811,6 +3148,17 @@ void MWindow::show_awindow()
        gui->mainmenu->show_awindow->set_checked(1);
 }
 
        gui->mainmenu->show_awindow->set_checked(1);
 }
 
+void MWindow::hide_awindow()
+{
+       session->show_awindow = 0;
+
+       awindow->gui->lock_window("MWindow::show_awindow");
+       awindow->gui->hide_window();
+       awindow->gui->unlock_window();
+       gui->mainmenu->show_awindow->set_checked(0);
+}
+
+
 char *MWindow::get_cwindow_display()
 {
        char *x11_host = screens < 2 || session->window_config == 0 ?
 char *MWindow::get_cwindow_display()
 {
        char *x11_host = screens < 2 || session->window_config == 0 ?
@@ -2822,9 +3170,22 @@ void MWindow::show_cwindow()
 {
        session->show_cwindow = 1;
        cwindow->show_window();
 {
        session->show_cwindow = 1;
        cwindow->show_window();
+       cwindow->gui->tool_panel->raise_tool();
        gui->mainmenu->show_cwindow->set_checked(1);
 }
 
        gui->mainmenu->show_cwindow->set_checked(1);
 }
 
+
+void MWindow::hide_cwindow()
+{
+       session->show_cwindow = 0;
+
+       cwindow->gui->lock_window("MWindow::show_cwindow");
+       cwindow->gui->hide_window();
+       cwindow->gui->unlock_window();
+       gui->mainmenu->show_cwindow->set_checked(0);
+}
+
+
 void MWindow::show_gwindow()
 {
        session->show_gwindow = 1;
 void MWindow::show_gwindow()
 {
        session->show_gwindow = 1;
@@ -2837,6 +3198,7 @@ void MWindow::show_gwindow()
 
        gui->mainmenu->show_gwindow->set_checked(1);
 }
 
        gui->mainmenu->show_gwindow->set_checked(1);
 }
+
 void MWindow::hide_gwindow()
 {
        session->show_gwindow = 0;
 void MWindow::hide_gwindow()
 {
        session->show_gwindow = 0;
@@ -2844,6 +3206,7 @@ void MWindow::hide_gwindow()
        gwindow->gui->lock_window("MWindow::show_gwindow");
        gwindow->gui->hide_window();
        gwindow->gui->unlock_window();
        gwindow->gui->lock_window("MWindow::show_gwindow");
        gwindow->gui->hide_window();
        gwindow->gui->unlock_window();
+       gui->mainmenu->show_gwindow->set_checked(0);
 }
 
 void MWindow::show_lwindow()
 }
 
 void MWindow::show_lwindow()
@@ -2857,8 +3220,20 @@ void MWindow::show_lwindow()
        gui->mainmenu->show_lwindow->set_checked(1);
 }
 
        gui->mainmenu->show_lwindow->set_checked(1);
 }
 
+void MWindow::hide_lwindow()
+{
+       session->show_lwindow = 0;
+
+       lwindow->gui->lock_window("MWindow::show_lwindow");
+       lwindow->gui->hide_window();
+       lwindow->gui->unlock_window();
+       gui->mainmenu->show_lwindow->set_checked(0);
+}
+
+
 void MWindow::restore_windows()
 {
 void MWindow::restore_windows()
 {
+       gui->unlock_window();
        if( !session->show_vwindow ) {
                for( int i=0, n=vwindows.size(); i<n; ++i ) {
                        VWindow *vwindow = vwindows[i];
        if( !session->show_vwindow ) {
                for( int i=0, n=vwindows.size(); i<n; ++i ) {
                        VWindow *vwindow = vwindows[i];
@@ -2868,8 +3243,8 @@ void MWindow::restore_windows()
                        vwindow->gui->unlock_window();
                }
        }
                        vwindow->gui->unlock_window();
                }
        }
-       else
-               show_vwindow();
+       else 
+               show_vwindow(0);
 
        if( !session->show_awindow && !awindow->gui->is_hidden() ) {
                awindow->gui->lock_window("MWindow::restore_windows");
 
        if( !session->show_awindow && !awindow->gui->is_hidden() ) {
                awindow->gui->lock_window("MWindow::restore_windows");
@@ -2885,7 +3260,7 @@ void MWindow::restore_windows()
                cwindow->gui->unlock_window();
        }
        else if( session->show_cwindow && cwindow->gui->is_hidden() )
                cwindow->gui->unlock_window();
        }
        else if( session->show_cwindow && cwindow->gui->is_hidden() )
-               cwindow->show_window();
+               show_cwindow();
 
        if( !session->show_gwindow && !gwindow->gui->is_hidden() ) {
                gwindow->gui->lock_window("MWindow::restore_windows");
 
        if( !session->show_gwindow && !gwindow->gui->is_hidden() ) {
                gwindow->gui->lock_window("MWindow::restore_windows");
@@ -2903,28 +3278,35 @@ void MWindow::restore_windows()
        else if( session->show_lwindow && lwindow->gui->is_hidden() )
                show_lwindow();
 
        else if( session->show_lwindow && lwindow->gui->is_hidden() )
                show_lwindow();
 
+       tile_mixers();
+       gui->lock_window("MWindow::restore_windows");
        gui->focus();
 }
 
        gui->focus();
 }
 
-void MWindow::save_layout(int no)
-{
-       char layout_path[BCTEXTLEN];
-       snprintf(layout_path, sizeof(layout_path), "%s/" LAYOUT_FILE,
-               File::get_config_path(), no);
-       session->save_file(layout_path);
-}
-
-void MWindow::load_layout(int no)
+void MWindow::load_layout(const char *layout)
 {
 {
-       char layout_path[BCTEXTLEN];
-       snprintf(layout_path, sizeof(layout_path), "%s/" LAYOUT_FILE,
-               File::get_config_path(), no);
-       session->load_file(layout_path);
+       char path[BCTEXTLEN];
+       snprintf(path, sizeof(path), "%s/%s", File::get_config_path(), layout);
+       session->load_file(path);
        restore_windows();
        gui->default_positions();
        save_defaults();
 }
 
        restore_windows();
        gui->default_positions();
        save_defaults();
 }
 
+void MWindow::save_layout(const char *layout)
+{
+       char path[BCTEXTLEN];
+       snprintf(path, sizeof(path), "%s/%s", File::get_config_path(), layout);
+       session->save_file(path);
+}
+
+void MWindow::delete_layout(const char *layout)
+{
+       char path[BCTEXTLEN];
+       snprintf(path, sizeof(path), "%s/%s", File::get_config_path(), layout);
+       unlink(path);
+}
+
 int MWindow::tile_windows(int window_config)
 {
        int need_reload = 0;
 int MWindow::tile_windows(int window_config)
 {
        int need_reload = 0;
@@ -2955,18 +3337,24 @@ void MWindow::set_screens(int value)
        screens = value;
 }
 
        screens = value;
 }
 
-void MWindow::set_auto_keyframes(int value, int lock_mwindow, int lock_cwindow)
+void MWindow::set_auto_keyframes(int value)
 {
 {
-       if(lock_mwindow) gui->lock_window("MWindow::set_auto_keyframes");
        edl->session->auto_keyframes = value;
        gui->mbuttons->edit_panel->keyframe->update(value);
        gui->flush();
        edl->session->auto_keyframes = value;
        gui->mbuttons->edit_panel->keyframe->update(value);
        gui->flush();
-       if(lock_mwindow) gui->unlock_window();
+       cwindow->gui->lock_window("MWindow::set_auto_keyframes");
+       cwindow->gui->flush();
+       cwindow->gui->unlock_window();
+}
 
 
-       if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_auto_keyframes");
-       cwindow->gui->edit_panel->keyframe->update(value);
+void MWindow::set_span_keyframes(int value)
+{
+       edl->session->span_keyframes = value;
+       gui->mbuttons->edit_panel->span_keyframe->update(value);
+       gui->flush();
+       cwindow->gui->lock_window("MWindow::set_span_keyframes");
        cwindow->gui->flush();
        cwindow->gui->flush();
-       if(lock_cwindow) cwindow->gui->unlock_window();
+       cwindow->gui->unlock_window();
 }
 
 void MWindow::set_auto_visibility(Autos *autos, int value)
 }
 
 void MWindow::set_auto_visibility(Autos *autos, int value)
@@ -2993,20 +3381,17 @@ void MWindow::set_keyframe_type(int mode)
        gui->unlock_window();
 }
 
        gui->unlock_window();
 }
 
-int MWindow::set_editing_mode(int new_editing_mode, int lock_mwindow, int lock_cwindow)
+int MWindow::set_editing_mode(int new_editing_mode)
 {
 {
-       if(lock_mwindow) gui->lock_window("MWindow::set_editing_mode");
        edl->session->editing_mode = new_editing_mode;
        gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
        gui->mbuttons->edit_panel->update();
        gui->set_editing_mode(1);
        edl->session->editing_mode = new_editing_mode;
        gui->mbuttons->edit_panel->editing_mode = edl->session->editing_mode;
        gui->mbuttons->edit_panel->update();
        gui->set_editing_mode(1);
-       if(lock_mwindow) gui->unlock_window();
-
 
 
-       if(lock_cwindow) cwindow->gui->lock_window("MWindow::set_editing_mode");
+       cwindow->gui->lock_window("MWindow::set_editing_mode");
        cwindow->gui->edit_panel->update();
        cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
        cwindow->gui->edit_panel->update();
        cwindow->gui->edit_panel->editing_mode = edl->session->editing_mode;
-       if(lock_cwindow) cwindow->gui->unlock_window();
+       cwindow->gui->unlock_window();
        return 0;
 }
 
        return 0;
 }
 
@@ -3014,9 +3399,9 @@ void MWindow::toggle_editing_mode()
 {
        int mode = edl->session->editing_mode;
        if( mode == EDITING_ARROW )
 {
        int mode = edl->session->editing_mode;
        if( mode == EDITING_ARROW )
-               set_editing_mode(EDITING_IBEAM, 0, 1);
+               set_editing_mode(EDITING_IBEAM);
        else
        else
-               set_editing_mode(EDITING_ARROW, 0, 1);
+               set_editing_mode(EDITING_ARROW);
 }
 
 void MWindow::toggle_camera_xyz()
 }
 
 void MWindow::toggle_camera_xyz()
@@ -3035,12 +3420,10 @@ void MWindow::toggle_projector_xyz()
 
 void MWindow::set_labels_follow_edits(int value)
 {
 
 void MWindow::set_labels_follow_edits(int value)
 {
-       gui->lock_window("MWindow::set_labels_follow_edits");
        edl->session->labels_follow_edits = value;
        gui->mbuttons->edit_panel->locklabels->update(value);
        gui->mainmenu->labels_follow_edits->set_checked(value);
        gui->flush();
        edl->session->labels_follow_edits = value;
        gui->mbuttons->edit_panel->locklabels->update(value);
        gui->mainmenu->labels_follow_edits->set_checked(value);
        gui->flush();
-       gui->unlock_window();
 }
 
 void MWindow::sync_parameters(int change_type)
 }
 
 void MWindow::sync_parameters(int change_type)
@@ -3048,35 +3431,24 @@ void MWindow::sync_parameters(int change_type)
        if( in_destructor ) return;
 
 // Sync engines which are playing back
        if( in_destructor ) return;
 
 // Sync engines which are playing back
-       if(cwindow->playback_engine->is_playing_back)
-       {
-               if(change_type == CHANGE_PARAMS)
-               {
+       if( cwindow->playback_engine->is_playing_back ) {
+               if( change_type == CHANGE_PARAMS ) {
 // TODO: block keyframes until synchronization is done
                        cwindow->playback_engine->sync_parameters(edl);
                }
 // TODO: block keyframes until synchronization is done
                        cwindow->playback_engine->sync_parameters(edl);
                }
-               else
+               else {
 // Stop and restart
 // Stop and restart
-               {
                        int command = cwindow->playback_engine->command->command;
                        int command = cwindow->playback_engine->command->command;
-                       cwindow->playback_engine->que->send_command(STOP,
-                               CHANGE_NONE,
-                               0,
-                               0);
 // Waiting for tracking to finish would make the restart position more
 // accurate but it can't lock the window to stop tracking for some reason.
 // Not waiting for tracking gives a faster response but restart position is
 // only as accurate as the last tracking update.
 // Waiting for tracking to finish would make the restart position more
 // accurate but it can't lock the window to stop tracking for some reason.
 // Not waiting for tracking gives a faster response but restart position is
 // only as accurate as the last tracking update.
-                       cwindow->playback_engine->interrupt_playback(0);
-                       cwindow->playback_engine->que->send_command(command,
-                                       change_type,
-                                       edl,
-                                       1,
-                                       0);
+                       cwindow->playback_engine->transport_stop(0);
+                       cwindow->playback_engine->next_command->realtime = 1;
+                       cwindow->playback_engine->transport_command(command, change_type, edl, 0);
                }
        }
                }
        }
-       else
-       {
+       else {
                cwindow->refresh_frame(change_type);
        }
 }
                cwindow->refresh_frame(change_type);
        }
 }
@@ -3126,10 +3498,9 @@ void MWindow::show_keyframe_gui(Plugin *plugin)
 {
        keyframe_gui_lock->lock("MWindow::show_keyframe_gui");
 // Find existing thread
 {
        keyframe_gui_lock->lock("MWindow::show_keyframe_gui");
 // Find existing thread
-       for(int i = 0; i < keyframe_threads->size(); i++)
-       {
-               if(keyframe_threads->get(i)->plugin == plugin)
-               {
+       for( int i=0; i<keyframe_threads->size(); ++i ) {
+               int plugin_id = keyframe_threads->get(i)->plugin_id;
+               if( plugin_id == plugin->orig_id ) {
                        keyframe_threads->get(i)->start_window(plugin, 0);
                        keyframe_gui_lock->unlock();
                        return;
                        keyframe_threads->get(i)->start_window(plugin, 0);
                        keyframe_gui_lock->unlock();
                        return;
@@ -3137,10 +3508,8 @@ void MWindow::show_keyframe_gui(Plugin *plugin)
        }
 
 // Find unused thread
        }
 
 // Find unused thread
-       for(int i = 0; i < keyframe_threads->size(); i++)
-       {
-               if(!keyframe_threads->get(i)->plugin)
-               {
+       for( int i=0; i<keyframe_threads->size(); ++i ) {
+               if( keyframe_threads->get(i)->plugin_id < 0 ) {
                        keyframe_threads->get(i)->start_window(plugin, 0);
                        keyframe_gui_lock->unlock();
                        return;
                        keyframe_threads->get(i)->start_window(plugin, 0);
                        keyframe_gui_lock->unlock();
                        return;
@@ -3151,14 +3520,10 @@ void MWindow::show_keyframe_gui(Plugin *plugin)
        KeyFrameThread *thread = new KeyFrameThread(this);
        keyframe_threads->append(thread);
        thread->start_window(plugin, 0);
        KeyFrameThread *thread = new KeyFrameThread(this);
        keyframe_threads->append(thread);
        thread->start_window(plugin, 0);
-
        keyframe_gui_lock->unlock();
 }
 
 
        keyframe_gui_lock->unlock();
 }
 
 
-
-
-
 void MWindow::show_plugin(Plugin *plugin)
 {
        int done = 0;
 void MWindow::show_plugin(Plugin *plugin)
 {
        int done = 0;
@@ -3172,14 +3537,12 @@ SET_TRACE
 //printf("MWindow::show_plugin %d\n", __LINE__);
 SET_TRACE
 
 //printf("MWindow::show_plugin %d\n", __LINE__);
 SET_TRACE
 
-
        plugin_gui_lock->lock("MWindow::show_plugin");
        plugin_gui_lock->lock("MWindow::show_plugin");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-// Pointer comparison
-               if(plugin_guis->get(i)->plugin == plugin)
-               {
-                       plugin_guis->get(i)->raise_window();
+       for( int i=0; i<plugin_guis->total; ++i ) {
+// Pointer/id comparison
+               PluginServer *plugin_gui = plugin_guis->get(i);
+               if( plugin_gui->plugin_id == plugin->orig_id ) {
+                       plugin_gui->raise_window();
                        done = 1;
                        break;
                }
                        done = 1;
                        break;
                }
@@ -3208,6 +3571,8 @@ SET_TRACE
                }
        }
        plugin_gui_lock->unlock();
                }
        }
        plugin_gui_lock->unlock();
+// update show/gui_id
+       sync_parameters(CHANGE_PARAMS);
 //printf("MWindow::show_plugin %d\n", __LINE__);
 SET_TRACE
 //sleep(1);
 //printf("MWindow::show_plugin %d\n", __LINE__);
 SET_TRACE
 //sleep(1);
@@ -3217,28 +3582,33 @@ SET_TRACE
 void MWindow::hide_plugin(Plugin *plugin, int lock)
 {
        plugin->show = 0;
 void MWindow::hide_plugin(Plugin *plugin, int lock)
 {
        plugin->show = 0;
+       return hide_plugin(plugin->orig_id, lock);
+}
+
+void MWindow::hide_plugin(int plugin_id, int lock)
+{
 // Update the toggle
        gui->lock_window("MWindow::hide_plugin");
        gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
        gui->unlock_window();
 
        if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
 // Update the toggle
        gui->lock_window("MWindow::hide_plugin");
        gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
        gui->unlock_window();
 
        if(lock) plugin_gui_lock->lock("MWindow::hide_plugin");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-               if(plugin_guis->get(i)->plugin == plugin)
-               {
-                       PluginServer *ptr = plugin_guis->get(i);
-                       plugin_guis->remove(ptr);
+       for( int i=0; i<plugin_guis->total; ++i ) {
+               PluginServer *plugin_gui = plugin_guis->get(i);
+               if( plugin_gui->plugin_id == plugin_id ) {
+                       plugin_guis->remove(plugin_gui);
                        if(lock) plugin_gui_lock->unlock();
 // Last command executed in client side close
 // Schedule for deletion
                        if(lock) plugin_gui_lock->unlock();
 // Last command executed in client side close
 // Schedule for deletion
-                       ptr->hide_gui();
-                       delete_plugin(ptr);
+                       plugin_gui->hide_gui();
+                       delete_plugin(plugin_gui);
 //sleep(1);
                        return;
                }
        }
        if(lock) plugin_gui_lock->unlock();
 //sleep(1);
                        return;
                }
        }
        if(lock) plugin_gui_lock->unlock();
+// update show/gui_id
+       sync_parameters(CHANGE_PARAMS);
 }
 
 void MWindow::delete_plugin(PluginServer *plugin)
 }
 
 void MWindow::delete_plugin(PluginServer *plugin)
@@ -3280,30 +3650,91 @@ void MWindow::hide_keyframe_guis()
 void MWindow::hide_keyframe_gui(Plugin *plugin)
 {
        keyframe_gui_lock->lock("MWindow::hide_keyframe_gui");
 void MWindow::hide_keyframe_gui(Plugin *plugin)
 {
        keyframe_gui_lock->lock("MWindow::hide_keyframe_gui");
-       for(int i = 0; i < keyframe_threads->size(); i++)
-       {
-               if(keyframe_threads->get(i)->plugin == plugin)
-               {
-                       keyframe_threads->get(i)->close_window();
+       for( int i = 0; i < keyframe_threads->size(); i++) {
+               KeyFrameThread *keyframe_gui = keyframe_threads->get(i);
+               if( keyframe_gui->plugin_id == plugin->orig_id ) {
+                       keyframe_gui->close_window();
                        break;
                }
        }
        keyframe_gui_lock->unlock();
 }
 
                        break;
                }
        }
        keyframe_gui_lock->unlock();
 }
 
+int MWindow::get_hash_color(Edit *edit)
+{
+       Indexable *idxbl = edit->asset ?
+               (Indexable*)edit->asset : (Indexable*)edit->nested_edl;
+       if( !idxbl ) return 0;
+       char path[BCTEXTLEN];
+// map proxy colors to unproxy colors
+       if( edl->session->proxy_state != PROXY_ACTIVE ||
+           !edit->asset || edit->track->data_type != TRACK_VIDEO ||
+           ProxyRender::from_proxy_path(path, (Asset*)idxbl, edl->session->proxy_scale) )
+               strcpy(path, idxbl->path);
+       char *cp = strrchr(path, '/');
+       cp = !cp ? path : cp+1;
+       uint8_t *bp = (uint8_t*)cp;
+       int v = 0;
+       while( *bp ) v += *bp++;
+       return get_hash_color(v);
+}
+
+int MWindow::get_hash_color(int v)
+{
+       int hash = 0x303030;
+       if( v & 0x01 ) hash ^= 0x000040;
+       if( v & 0x02 ) hash ^= 0x004000;
+       if( v & 0x04 ) hash ^= 0x400000;
+       if( v & 0x08 ) hash ^= 0x080000;
+       if( v & 0x10 ) hash ^= 0x000800;
+       if( v & 0x20 ) hash ^= 0x000008;
+       if( v & 0x40 ) hash ^= 0x404040;
+       if( v & 0x80 ) hash ^= 0x080808;
+       return hash;
+}
+
+int MWindow::get_group_color(int v)
+{
+       int color = 0x606060;
+       if( v & 0x01 ) color ^= 0x000080;
+       if( v & 0x02 ) color ^= 0x008000;
+       if( v & 0x04 ) color ^= 0x800000;
+       if( v & 0x08 ) color ^= 0x100000;
+       if( v & 0x10 ) color ^= 0x001000;
+       if( v & 0x20 ) color ^= 0x000010;
+       if( v & 0x40 ) color ^= 0x080808;
+       if( v & 0x80 ) color ^= 0x909090;
+       return color;
+}
+
+int MWindow::get_title_color(Edit *edit)
+{
+        unsigned color = edit->color & 0xffffff;
+       unsigned alpha = (~edit->color>>24) & 0xff;
+       if( !color ) {
+               if( edit->group_id )
+                       color = get_group_color(edit->group_id);
+               else if( preferences->autocolor_assets )
+                       color = get_hash_color(edit);
+               else
+                       return 0;
+       }
+       if( alpha == 0xff )
+               alpha = session->title_bar_alpha*255;
+       return color | (~alpha<<24);
+}
+
 void MWindow::update_keyframe_guis()
 {
 // Send new configuration to keyframe GUI's
        keyframe_gui_lock->lock("MWindow::update_keyframe_guis");
 void MWindow::update_keyframe_guis()
 {
 // Send new configuration to keyframe GUI's
        keyframe_gui_lock->lock("MWindow::update_keyframe_guis");
-       for(int i = 0; i < keyframe_threads->size(); i++)
-       {
-               KeyFrameThread *ptr = keyframe_threads->get(i);
-               if(edl->tracks->plugin_exists(ptr->plugin))
-                       ptr->update_gui(1);
+       for( int i=0; i<keyframe_threads->size(); ++i ) {
+               KeyFrameThread *keyframe_gui = keyframe_threads->get(i);
+               Plugin *plugin = edl->tracks->plugin_exists(keyframe_gui->plugin_id);
+               if( plugin )
+                       keyframe_gui->update_gui(1);
                else
                else
-               {
-                       ptr->close_window();
-               }
+                       keyframe_gui->close_window();
        }
        keyframe_gui_lock->unlock();
 }
        }
        keyframe_gui_lock->unlock();
 }
@@ -3313,139 +3744,134 @@ void MWindow::update_plugin_guis(int do_keyframe_guis)
 // Send new configuration to plugin GUI's
        plugin_gui_lock->lock("MWindow::update_plugin_guis");
 
 // Send new configuration to plugin GUI's
        plugin_gui_lock->lock("MWindow::update_plugin_guis");
 
-       for(int i = 0; i < plugin_guis->size(); i++)
-       {
-               PluginServer *ptr = plugin_guis->get(i);
-               if(edl->tracks->plugin_exists(ptr->plugin))
-                       ptr->update_gui();
-               else
-               {
-// Schedule for deletion if no plugin
-                       plugin_guis->remove_number(i);
-                       i--;
-
-                       ptr->hide_gui();
-                       delete_plugin(ptr);
+       for( int i=0; i<plugin_guis->size(); ++i ) {
+               PluginServer *plugin_gui = plugin_guis->get(i);
+               Plugin *plugin = edl->tracks->plugin_exists(plugin_gui->plugin_id);
+               if( plugin && plugin->show )
+                       plugin_gui->update_gui();
+               else {
+// Schedule for deletion if no plugin or not shown
+                       plugin_guis->remove_number(i--);
+                       plugin_gui->hide_gui();
+                       delete_plugin(plugin_gui);
                }
        }
 
 
 // Change plugin variable if not visible
        Track *track = edl->tracks->first;
                }
        }
 
 
 // Change plugin variable if not visible
        Track *track = edl->tracks->first;
-       while(track)
-       {
-               for(int i = 0; i < track->plugin_set.size(); i++)
-               {
+       for( ; track; track=track->next ) {
+               for( int i=0; i < track->plugin_set.size(); ++i ) {
                        Plugin *plugin = (Plugin*)track->plugin_set[i]->first;
                        Plugin *plugin = (Plugin*)track->plugin_set[i]->first;
-                       while(plugin)
-                       {
+                       for( ; plugin; plugin = (Plugin*)plugin->next ) {
                                int got_it = 0;
                                int got_it = 0;
-                               for(int i = 0; i < plugin_guis->size(); i++)
-                               {
+                               for( int i=0; i<plugin_guis->size(); ++i ) {
                                        PluginServer *server = plugin_guis->get(i);
                                        PluginServer *server = plugin_guis->get(i);
-                                       if(server->plugin == plugin)
-                                       {
+                                       if( server->plugin_id == plugin->orig_id ) {
                                                got_it = 1;
                                                break;
                                        }
                                }
                                                got_it = 1;
                                                break;
                                        }
                                }
-
-                               if(!got_it) plugin->show = 0;
-
-                               plugin = (Plugin*)plugin->next;
+                               if( !got_it ) plugin->show = 0;
                        }
                }
                        }
                }
-
-               track = track->next;
        }
 
        plugin_gui_lock->unlock();
        }
 
        plugin_gui_lock->unlock();
+       if( do_keyframe_guis )
+               update_keyframe_guis();
+}
 
 
+void MWindow::stop_plugin_guis()
+{
+// Send new configuration to plugin GUI's
+       plugin_gui_lock->lock("MWindow::stop_plugin_guis");
 
 
-       if(do_keyframe_guis) update_keyframe_guis();
+       for( int i=0; i<plugin_guis->size(); ++i ) {
+               PluginServer *plugin_gui = plugin_guis->get(i);
+               plugin_gui->render_stop();
+       }
+       plugin_gui_lock->unlock(); 
 }
 
 int MWindow::plugin_gui_open(Plugin *plugin)
 {
 }
 
 int MWindow::plugin_gui_open(Plugin *plugin)
 {
-       int result = 0;
+       int gui_id = plugin->gui_id;
+       if( gui_id < 0 ) return 0;
        plugin_gui_lock->lock("MWindow::plugin_gui_open");
        plugin_gui_lock->lock("MWindow::plugin_gui_open");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-               if(plugin_guis->get(i)->plugin->identical_location(plugin))
-               {
-                       result = 1;
-                       break;
-               }
-       }
+       PluginServer *plugin_server = plugin_guis->gui_server(gui_id);
+       int result = plugin_server ? 1 : 0;
        plugin_gui_lock->unlock();
        return result;
 }
 
        plugin_gui_lock->unlock();
        return result;
 }
 
+
 void MWindow::render_plugin_gui(void *data, Plugin *plugin)
 {
 void MWindow::render_plugin_gui(void *data, Plugin *plugin)
 {
-       plugin_gui_lock->lock("MWindow::render_plugin_gui");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-               if(plugin_guis->get(i)->plugin->identical_location(plugin))
-               {
-                       plugin_guis->get(i)->render_gui(data);
-                       break;
-               }
-       }
+       int gui_id = plugin->gui_id;
+       if( gui_id < 0 ) return;
+       plugin_gui_lock->lock("MWindow::render_plugin_gui 0");
+       PluginServer *plugin_server = plugin_guis->gui_server(gui_id);
+       if( plugin_server )
+               plugin_server->render_gui(data);
        plugin_gui_lock->unlock();
 }
 
 void MWindow::render_plugin_gui(void *data, int size, Plugin *plugin)
 {
        plugin_gui_lock->unlock();
 }
 
 void MWindow::render_plugin_gui(void *data, int size, Plugin *plugin)
 {
-       plugin_gui_lock->lock("MWindow::render_plugin_gui");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-               if(plugin_guis->get(i)->plugin->identical_location(plugin))
-               {
-                       plugin_guis->get(i)->render_gui(data, size);
-                       break;
-               }
-       }
+       int gui_id = plugin->gui_id;
+       if( gui_id < 0 ) return;
+       plugin_gui_lock->lock("MWindow::render_plugin_gui 1");
+       PluginServer *plugin_server = plugin_guis->gui_server(gui_id);
+       if( plugin_server )
+               plugin_server->render_gui(data, size);
+       plugin_gui_lock->unlock();
+}
+
+void MWindow::reset_plugin_gui_frames(Plugin *plugin)
+{
+       int gui_id = plugin->gui_id;
+       if( gui_id < 0 ) return;
+       plugin_gui_lock->lock("MWindow::reset_plugin_gui_frames");
+       PluginServer *plugin_server = plugin_guis->gui_server(gui_id);
+       if( plugin_server )
+               plugin_server->reset_plugin_gui_frames();
+       plugin_gui_lock->unlock();
+}
+
+void MWindow::render_plugin_gui_frames(PluginClientFrames *frames, Plugin *plugin)
+{
+       int gui_id = plugin->gui_id;
+       if( gui_id < 0 ) return;
+       plugin_gui_lock->lock("MWindow::render_plugin_gui_frames");
+       PluginServer *plugin_server = plugin_guis->gui_server(gui_id);
+       if( plugin_server )
+               plugin_server->render_plugin_gui_frames(frames);
        plugin_gui_lock->unlock();
 }
 
        plugin_gui_lock->unlock();
 }
 
+double MWindow::get_tracking_position()
+{
+       return edl->local_session->get_selectionstart(1);
+}
+
+int MWindow::get_tracking_direction()
+{
+       return cwindow->playback_engine->get_direction();
+}
 
 void MWindow::update_plugin_states()
 {
        plugin_gui_lock->lock("MWindow::update_plugin_states");
 
 void MWindow::update_plugin_states()
 {
        plugin_gui_lock->lock("MWindow::update_plugin_states");
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
-               int result = 0;
+       for( int i=0; i<plugin_guis->total; ++i ) {
 // Get a plugin GUI
 // Get a plugin GUI
-               Plugin *src_plugin = plugin_guis->get(i)->plugin;
                PluginServer *src_plugingui = plugin_guis->get(i);
                PluginServer *src_plugingui = plugin_guis->get(i);
-
-// Search for plugin in EDL.  Only the master EDL shows plugin GUIs.
-               for(Track *track = edl->tracks->first;
-                       track && !result;
-                       track = track->next)
-               {
-                       for(int j = 0;
-                               j < track->plugin_set.total && !result;
-                               j++)
-                       {
-                               PluginSet *plugin_set = track->plugin_set[j];
-                               for(Plugin *plugin = (Plugin*)plugin_set->first;
-                                       plugin && !result;
-                                       plugin = (Plugin*)plugin->next)
-                               {
-                                       if(plugin == src_plugin &&
-                                               !strcmp(plugin->title, src_plugingui->title)) result = 1;
-                               }
-                       }
-               }
-
-
+               int plugin_id = src_plugingui->plugin_id;
+               Plugin *src_plugin = edl->tracks->plugin_exists(plugin_id);
 // Doesn't exist anymore
 // Doesn't exist anymore
-               if(!result)
-               {
-                       hide_plugin(src_plugin, 0);
-                       i--;
+               if( !src_plugin ) {
+                       hide_plugin(plugin_id, 0);
+                       --i;
                }
        }
        plugin_gui_lock->unlock();
                }
        }
        plugin_gui_lock->unlock();
@@ -3454,10 +3880,8 @@ void MWindow::update_plugin_states()
 
 void MWindow::update_plugin_titles()
 {
 
 void MWindow::update_plugin_titles()
 {
-       for(int i = 0; i < plugin_guis->total; i++)
-       {
+       for( int i=0; i<plugin_guis->total; ++i )
                plugin_guis->get(i)->update_title();
                plugin_guis->get(i)->update_title();
-       }
 }
 
 int MWindow::asset_to_edl(EDL *new_edl,
 }
 
 int MWindow::asset_to_edl(EDL *new_edl,
@@ -3528,6 +3952,7 @@ if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
 //printf("MWindow::asset_to_edl 4 %s\n", string);
 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
 
 //printf("MWindow::asset_to_edl 4 %s\n", string);
 if(debug) printf("MWindow::asset_to_edl %d\n", __LINE__);
 
+       new_edl->local_session->asset2edl = 1;
        return 0;
 }
 
        return 0;
 }
 
@@ -3545,8 +3970,8 @@ void MWindow::update_project(int load_mode)
 
        if( load_mode == LOADMODE_REPLACE ||
            load_mode == LOADMODE_REPLACE_CONCATENATE ) {
 
        if( load_mode == LOADMODE_REPLACE ||
            load_mode == LOADMODE_REPLACE_CONCATENATE ) {
-               delete gui->keyvalue_popup;
-               gui->keyvalue_popup = 0;
+               edl->session->timecode_offset = 0;
+               gui->close_keyvalue_popup();
                gui->load_panes();
        }
 
                gui->load_panes();
        }
 
@@ -3555,9 +3980,7 @@ void MWindow::update_project(int load_mode)
        gui->unlock_window();
        init_brender();
 
        gui->unlock_window();
        init_brender();
 
-       cwindow->gui->lock_window("MWindow::update_project 1");
        cwindow->update(0, 0, 1, 1, 1);
        cwindow->update(0, 0, 1, 1, 1);
-       cwindow->gui->unlock_window();
 
        if(debug) PRINT_TRACE
 
 
        if(debug) PRINT_TRACE
 
@@ -3590,16 +4013,22 @@ void MWindow::update_project(int load_mode)
 
                for( int i=0; i<edl->mixers.size(); ++i ) {
                        Mixer *mixer = edl->mixers[i];
 
                for( int i=0; i<edl->mixers.size(); ++i ) {
                        Mixer *mixer = edl->mixers[i];
+                       if( !mixer->show ) continue;
                        ZWindow *zwindow = get_mixer(mixer);
                        zwindow->set_title(mixer->title);
                        zwindow->start();
                }
                        ZWindow *zwindow = get_mixer(mixer);
                        zwindow->set_title(mixer->title);
                        zwindow->start();
                }
+               cwindow->gui->canvas->set_zoom(edl, 0);
        }
        update_vwindow();
 
        if(debug) PRINT_TRACE
        cwindow->gui->lock_window("MWindow::update_project 2");
        cwindow->gui->timebar->update(0);
        }
        update_vwindow();
 
        if(debug) PRINT_TRACE
        cwindow->gui->lock_window("MWindow::update_project 2");
        cwindow->gui->timebar->update(0);
+       Track *track = cwindow->calculate_affected_track();
+       cwindow->mask_track_id = track ? track->get_id() : -1;
+       cwindow->gui->tool_panel->raise_tool();
+       cwindow->gui->update_canvas(0);
        cwindow->gui->unlock_window();
 
        if(debug) PRINT_TRACE
        cwindow->gui->unlock_window();
 
        if(debug) PRINT_TRACE
@@ -3614,6 +4043,296 @@ void MWindow::update_project(int load_mode)
        if(debug) PRINT_TRACE
 }
 
        if(debug) PRINT_TRACE
 }
 
+void MWindow::stack_push(EDL *new_edl, Indexable *idxbl)
+{
+       int got_indexes = 0;
+       for( int i=0; i<new_edl->nested_edls.size(); ++i ) {
+               EDL *nested_edl = new_edl->nested_edls[i];
+               mainindexes->add_indexable(nested_edl);
+               edl->nested_edls.update_index(nested_edl);
+               got_indexes = 1;
+       }
+       for( Asset *asset=new_edl->assets->first; asset; asset=asset->next ) {
+               mainindexes->add_indexable(asset);
+               edl->assets->update(asset);
+               got_indexes = 1;
+       }
+// Start examining next batch of index files
+       if( got_indexes )
+               mainindexes->start_build();
+
+// needs gui lock
+       gui->lock_window("MWindow::stack_push");
+       if( stack.size() < 9 ) {
+               save_backup();
+               hide_plugins();
+               undo_before();
+               StackItem &item = stack.append();
+               item.edl = edl;
+               item.new_edl = new_edl;
+               item.duration = new_edl->tracks->total_length();
+               item.undo = undo;
+               item.idxbl = idxbl;
+               item.mtime = 0;
+               if( idxbl && idxbl->is_asset ) {
+                       struct stat st;
+                       Asset *asset = (Asset *)idxbl;
+                       if( asset->format == FILE_REF &&
+                           !stat(asset->path, &st) )
+                               item.mtime = st.st_mtime;
+               }
+               edl = new_edl;
+               edl->add_user();
+               strcpy(session->filename, edl->path);
+               undo = new MainUndo(this);
+               gui->stack_button->update();
+               update_project(LOADMODE_REPLACE);
+       }
+       gui->unlock_window();
+       show_plugins();
+}
+
+void MWindow::stack_pop()
+{
+       if( !stack.size() ) return;
+// writes on config_path/backup%d.xml
+       save_backup();
+       hide_plugins();
+// already have gui lock
+       StackItem &item = stack.last();
+// session edl replaced, overwrite and save clip data
+       if( item.new_edl != edl )
+               item.new_edl->overwrite_clip(edl);
+       Indexable *idxbl = item.idxbl;
+       if( idxbl && idxbl->is_asset && item.mtime ) {
+               Asset *asset = (Asset *)idxbl;
+               if( asset->format == FILE_REF ) {
+                       char *path = asset->path;
+                       struct stat st;
+                       if( stat(path, &st) || item.mtime == st.st_mtime ) {
+                               int cw = xS(250), ch = yS(150), px, py;
+                               gui->get_pop_cursor(px, py);
+                               px -= cw/2;  py -= ch/2;
+                               ConfirmRefWindow confirm(this, path, px, py, cw, ch);
+                               confirm.create_objects();
+                               int result = confirm.run_window();
+                               if( !result ) {
+                                       FileXML file;
+                                       item.new_edl->save_xml(&file, path);
+                                       file.terminate_string();
+                                       if(file.write_to_file(path))
+                                               eprintf(_("Cant write FileREF: %s"), path);
+                               }
+                       }
+               }
+       }
+       edl->remove_user();
+       edl = item.edl;
+       delete undo;
+       undo = item.undo;
+       stack.remove();
+       if( idxbl ) {
+// resize the indexable edits if the new_edl duration changed
+               double duration = item.new_edl->tracks->total_length();
+               double dt = duration - item.duration;
+               if( fabs(dt) > 1e-4 )
+                       edl->tracks->update_idxbl_length(idxbl->id, dt);
+               gui->unlock_window();
+               gui->resource_thread->close_indexable(idxbl);
+               remove_from_caches(idxbl);
+               IndexFile::delete_index_files(preferences, idxbl);
+               mainindexes->add_indexable(idxbl);
+               mainindexes->start_build();
+               awindow->gui->async_update_assets();
+               gui->lock_window("MWindow::stack_pop");
+       }
+       strcpy(session->filename, edl->path);
+       update_project(LOADMODE_REPLACE);
+       undo_after(_("open edl"), LOAD_ALL);
+       show_plugins();
+       gui->stack_button->update();
+}
+
+int MWindow::save(EDL *edl, char *filename, int stat)
+{
+       FileXML file;
+       edl->save_xml(&file, filename);
+       file.terminate_string();
+       if( file.write_to_file(filename) ) {
+               eprintf(_("Couldn't open %s"), filename);
+               return 1;
+       }
+       if( stat ) {
+               char string[BCTEXTLEN];
+               char *filename = stack.size() ?
+                       stack[0].edl->path : session->filename;
+               sprintf(string, _("\"%s\" %jdC written"),
+                        filename, file.length());
+               gui->lock_window("SaveAs::run");
+               gui->show_message(string);
+               gui->unlock_window();
+       }
+       return 0;
+}
+
+int MWindow::save(int save_as)
+{
+       char new_path[BCTEXTLEN];  new_path[0] = 0;
+       char *path = stack.size() ? stack[0].edl->path : session->filename;
+       if( save_as || !path[0] ) {
+               if( ConfirmSave::get_save_path(this, new_path) )
+                       return 1;
+               if( stack.size() ) {
+                       strcpy(path, new_path);
+                       set_titlebar(new_path);
+               }
+               else
+                       set_filename(new_path);
+               gui->mainmenu->add_load(new_path);
+               path = new_path;
+       }
+       for( int i=stack.size(); --i>=0;  ) {
+               StackItem &item = stack[i];
+               Indexable *idxbl = item.idxbl;
+               if( !idxbl ) continue;
+               if( idxbl->is_asset ) {
+                       Asset *asset = (Asset *)idxbl;
+                       if( asset->format == FILE_REF ) {
+                               if( save(item.new_edl, asset->path, 0) )
+                                       return 1;
+                       }
+               }
+               else if( item.new_edl != idxbl )
+                       item.new_edl->overwrite_clip((EDL*)idxbl);
+       }
+       EDL *new_edl = stack.size() ? stack[0].edl : edl;
+       save(new_edl, path, 1);
+       return 0;
+}
+
+void MWindow::show_plugins()
+{
+       for( Track *track=edl->tracks->first; track; track=track->next ) {
+               for( int i=0; i<track->plugin_set.size(); ++i ) {
+                       PluginSet *plugins = track->plugin_set[i];
+                       Plugin *plugin = plugins->get_first_plugin();
+                       for( ; plugin; plugin=(Plugin*)plugin->next ) {
+                               if( plugin->plugin_type == PLUGIN_STANDALONE &&
+                                   plugin->show )
+                                       show_plugin(plugin);
+                       }
+               }
+       }
+}
+
+void MWindow::clip_to_media()
+{
+       if( edl->session->proxy_scale != 1 ) {
+               eprintf("Nesting not allowed when proxy scale != 1");
+               return;
+       }
+       undo_before();
+       awindow->gui->stop_vicon_drawing();
+       int clips_total = session->drag_clips->total;
+       for( int i=0; i<clips_total; ++i ) {
+               EDL *clip = session->drag_clips->values[i];
+               time_t dt;      time(&dt);
+               struct tm dtm;  localtime_r(&dt, &dtm);
+               char path[BCTEXTLEN], *cp = path, *ep = cp+sizeof(path)-1;
+// path_basename = "Nested_<date>-<time>_<basename>"
+               cp += snprintf(cp, ep-cp, _("Nested_%02d%02d%02d-%02d%02d%02d_"),
+                       dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
+                       dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
+               char *bp = strrchr(clip->local_session->clip_title, '/');
+               bp = bp ? bp+1 : clip->local_session->clip_title;
+               cp += snprintf(cp, ep-cp, "%s", bp);
+               EDL *nested = edl->new_nested_clip(clip, path);
+               edl->clips.remove(clip);
+               clip->remove_user();
+               mainindexes->add_indexable(nested);
+       }
+       undo_after(_("clip2media"), LOAD_ALL);
+       mainindexes->start_build();
+       awindow->gui->async_update_assets();
+}
+
+void MWindow::media_to_clip()
+{
+       undo_before();
+       int assets_total = session->drag_assets->total;
+       for( int i=0; i<assets_total; ++i ) {
+               Indexable *idxbl = session->drag_assets->values[i];
+               if( idxbl->is_asset ) {
+                       eprintf(_("media is not EDL:\n%s"), idxbl->path);
+                       continue;
+               }
+               char clip_title[BCSTRLEN];
+               int name_ok = 0;
+               while( !name_ok ) {
+                       name_ok = 1;
+                       sprintf(clip_title, _("Clip %d"), session->clip_number++);
+                       for( int i=0; name_ok && i<edl->clips.size(); ++i ) {
+                               char *title = edl->clips[i]->local_session->clip_title;
+                               if( !strcasecmp(clip_title, title) ) name_ok = 0;
+                       }
+               }
+               EDL *nested = (EDL *)idxbl;
+               EDL *clip = edl->add_clip(nested);
+               strcpy(clip->local_session->clip_title, clip_title);
+               snprintf(clip->local_session->clip_notes,
+                       sizeof(clip->local_session->clip_notes),
+                       _("From: %s"), nested->path);
+       }
+       undo_after(_("media2clip"), LOAD_ALL);
+       awindow->gui->async_update_assets();
+}
+
+int MWindow::create_ref(Asset *asset, EDL *ref)
+{
+       asset->format = FILE_REF;
+       double secs = ref->tracks->total_length();
+       int audio_channels = ref->session->audio_channels;
+       asset->audio_data = audio_channels > 0 ? 1 : 0;
+       asset->channels = audio_channels;
+       asset->sample_rate = ref->session->sample_rate;
+       asset->audio_length = audio_channels > 0 && secs > 0 ?
+                secs * asset->sample_rate : 0;
+       strcpy(asset->acodec, _("reference"));
+
+       int video_layers = ref->session->video_channels;
+       asset->video_data = video_layers > 0 ? 1 : 0;
+       asset->layers = video_layers > 0 ? 1 : 0;
+       asset->actual_width = ref->session->output_w;
+       asset->actual_height = ref->session->output_h;
+       asset->width = asset->actual_width;
+       asset->height = asset->actual_height;
+       asset->frame_rate = ref->session->frame_rate;
+       asset->video_length = video_layers > 0 && secs > 0 ?
+               secs * asset->frame_rate : 0;
+       strcpy(asset->vcodec, _("reference"));
+       return 0;
+}
+
+void MWindow::update_preferences(Preferences *prefs)
+{
+       if( prefs != preferences )
+               preferences->copy_from(prefs);
+       if( cwindow->playback_engine )
+               cwindow->playback_engine->update_preferences(prefs);
+       for(int i = 0; i < vwindows.size(); i++) {
+               VWindow *vwindow = vwindows[i];
+               if( !vwindow->is_running() ) continue;
+               if( vwindow->playback_engine )
+                       vwindow->playback_engine->update_preferences(prefs);
+       }
+       for(int i = 0; i < zwindows.size(); i++) {
+               ZWindow *zwindow = zwindows[i];
+               if( !zwindow->is_running() ) continue;
+               if( zwindow->zgui->playback_engine )
+                       zwindow->zgui->playback_engine->update_preferences(prefs);
+       }
+}
+
 void MWindow::update_vwindow()
 {
        for( int i=0; i<vwindows.size(); ++i ) {
 void MWindow::update_vwindow()
 {
        for( int i=0; i<vwindows.size(); ++i ) {
@@ -3629,11 +4348,8 @@ void MWindow::update_vwindow()
 void MWindow::remove_indexfile(Indexable *indexable)
 {
        if( !indexable->is_asset ) return;
 void MWindow::remove_indexfile(Indexable *indexable)
 {
        if( !indexable->is_asset ) return;
-       Asset *asset = (Asset *)indexable;
 // Erase file
 // Erase file
-       IndexFile::delete_index(preferences, asset, ".toc");
-       IndexFile::delete_index(preferences, asset, ".idx");
-       IndexFile::delete_index(preferences, asset, ".mkr");
+       IndexFile::delete_index_files(preferences, indexable);
 }
 
 void MWindow::rebuild_indices()
 }
 
 void MWindow::rebuild_indices()
@@ -3644,8 +4360,9 @@ void MWindow::rebuild_indices()
 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
                remove_indexfile(indexable);
 // Schedule index build
 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
                remove_indexfile(indexable);
 // Schedule index build
-               IndexState *index_state = indexable->index_state;
-               index_state->index_status = INDEX_NOTTESTED;
+               indexable->index_state->remove_user();
+               indexable->index_state = new IndexState;
+               IndexFile::delete_index_files(preferences, indexable);
                if( indexable->is_asset ) {
                        Asset *asset = (Asset *)indexable;
                        if( asset->format != FILE_PCM ) {
                if( indexable->is_asset ) {
                        Asset *asset = (Asset *)indexable;
                        if( asset->format != FILE_PCM ) {
@@ -3653,38 +4370,70 @@ void MWindow::rebuild_indices()
                                asset->reset_audio();
                        }
                        asset->reset_video();
                                asset->reset_audio();
                        }
                        asset->reset_video();
-                       remove_asset_from_caches(asset);
+                       remove_from_caches(asset);
 //                     File file; // re-probe the asset
 //                     file.open_file(preferences, asset, 1, 0);
                }
 //                     File file; // re-probe the asset
 //                     file.open_file(preferences, asset, 1, 0);
                }
-               mainindexes->add_next_asset(0, indexable);
+               mainindexes->add_indexable(indexable);
        }
        }
+// still in render engine
+       sync_parameters(CHANGE_ALL);
+       awindow->gui->async_update_assets();
        mainindexes->start_build();
 }
 
 
        mainindexes->start_build();
 }
 
 
+void MWindow::get_backup_path(char *path, int len)
+{
+       char *cp = path, *ep = cp + len-1;
+       cp += snprintf(cp, ep-cp, "%s/", File::get_config_path());
+       int idx = stack.size();
+       cp += snprintf(cp, ep-cp, idx ? BACKUPn_FILE : BACKUP_FILE, idx);
+}
+
+void MWindow::create_timestamped_copy_from_previous_backup(char *previouspath)
+{
+  if (previouspath == NULL) return;
+  char backup_path[BCTEXTLEN];
+  backup_path[0] = 0;
+  time_t now = time(NULL);
+  struct tm* currenttime = localtime(&now);
+  snprintf(backup_path, sizeof(backup_path), 
+      "%s/%s_%d%.2d%.2d_%.2d%.2d%.2d",
+      File::get_config_path(), BACKUP_FILE1, 
+      currenttime->tm_year + 1900,
+      currenttime->tm_mon + 1,
+      currenttime->tm_mday,
+      currenttime->tm_hour,
+      currenttime->tm_min,
+      currenttime->tm_sec);
+       rename(previouspath, backup_path);
+}
+
 void MWindow::save_backup()
 {
        FileXML file;
        edl->optimize();
        edl->set_path(session->filename);
 void MWindow::save_backup()
 {
        FileXML file;
        edl->optimize();
        edl->set_path(session->filename);
+
        char backup_path[BCTEXTLEN], backup_path1[BCTEXTLEN];
        char backup_path[BCTEXTLEN], backup_path1[BCTEXTLEN];
-       snprintf(backup_path, sizeof(backup_path), "%s/%s",
-               File::get_config_path(), BACKUP_FILE);
        snprintf(backup_path1, sizeof(backup_path1), "%s/%s",
                File::get_config_path(), BACKUP_FILE1);
        snprintf(backup_path1, sizeof(backup_path1), "%s/%s",
                File::get_config_path(), BACKUP_FILE1);
+       get_backup_path(backup_path, sizeof(backup_path));
+       if( preferences->ongoing_backups )
+               create_timestamped_copy_from_previous_backup(backup_path1);
        rename(backup_path, backup_path1);
        edl->save_xml(&file, backup_path);
        file.terminate_string();
        FileSystem fs;
        fs.complete_path(backup_path);
 
        rename(backup_path, backup_path1);
        edl->save_xml(&file, backup_path);
        file.terminate_string();
        FileSystem fs;
        fs.complete_path(backup_path);
 
-       if(file.write_to_file(backup_path))
-       {
+       if(file.write_to_file(backup_path)) {
                char string2[256];
                sprintf(string2, _("Couldn't open %s for writing."), backup_path);
                gui->show_message(string2);
        }
                char string2[256];
                sprintf(string2, _("Couldn't open %s for writing."), backup_path);
                gui->show_message(string2);
        }
+       save_undo_data();
 }
 
 void MWindow::load_backup()
 }
 
 void MWindow::load_backup()
@@ -3693,15 +4442,14 @@ void MWindow::load_backup()
        path_list.set_array_delete();
        char *out_path;
        char backup_path[BCTEXTLEN];
        path_list.set_array_delete();
        char *out_path;
        char backup_path[BCTEXTLEN];
-       snprintf(backup_path, sizeof(backup_path), "%s/%s",
-               File::get_config_path(), BACKUP_FILE);
+       get_backup_path(backup_path, sizeof(backup_path));
        FileSystem fs;
        fs.complete_path(backup_path);
 
        path_list.append(out_path = new char[strlen(backup_path) + 1]);
        strcpy(out_path, backup_path);
 
        FileSystem fs;
        fs.complete_path(backup_path);
 
        path_list.append(out_path = new char[strlen(backup_path) + 1]);
        strcpy(out_path, backup_path);
 
-       load_filenames(&path_list, LOADMODE_REPLACE, 0);
+       load_filenames(&path_list, LOADMODE_REPLACE, LOADMODE_EDL_CLIP, 0);
        edl->local_session->clip_title[0] = 0;
 // This is unique to backups since the path of the backup is different than the
 // path of the project.
        edl->local_session->clip_title[0] = 0;
 // This is unique to backups since the path of the backup is different than the
 // path of the project.
@@ -3713,8 +4461,8 @@ void MWindow::load_backup()
 
 void MWindow::save_undo_data()
 {
 
 void MWindow::save_undo_data()
 {
-       undo->update_undo_before();
-       undo->update_undo_after(_("perpetual session"), LOAD_ALL);
+       if( stack.size() > 0 ) return;
+       if( !preferences->perpetual_session ) return;
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
@@ -3726,15 +4474,27 @@ void MWindow::save_undo_data()
 
 void MWindow::load_undo_data()
 {
 
 void MWindow::load_undo_data()
 {
+       if( stack.size() > 0 ) return;
+       if( !preferences->perpetual_session ) return;
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
        FILE *fp = fopen(perpetual_path,"r");
        if( !fp ) return;
        undo->load(fp);
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
        FILE *fp = fopen(perpetual_path,"r");
        if( !fp ) return;
        undo->load(fp);
+       undo_before();
+       undo_after(_("perpetual load"), LOAD_ALL);
        fclose(fp);
 }
 
        fclose(fp);
 }
 
+void MWindow::remove_undo_data()
+{
+       if( stack.size() > 0 ) return;
+       char perpetual_path[BCTEXTLEN];
+       snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
+               File::get_config_path(), PERPETUAL_FILE);
+       ::remove(perpetual_path);
+}
 
 int MWindow::copy_target(const char *path, const char *target)
 {
 
 int MWindow::copy_target(const char *path, const char *target)
 {
@@ -3838,7 +4598,10 @@ void MWindow::save_project(const char *dir, int save_mode, int overwrite, int re
        char progress_title[BCTEXTLEN];
        sprintf(progress_title, _("Saving to %s:\n"), dir);
        int total_assets = save_edl->assets->total();
        char progress_title[BCTEXTLEN];
        sprintf(progress_title, _("Saving to %s:\n"), dir);
        int total_assets = save_edl->assets->total();
+       gui->lock_window("MWindow::save_project");
        MainProgressBar *progress = mainprogress->start_progress(progress_title, total_assets);
        MainProgressBar *progress = mainprogress->start_progress(progress_title, total_assets);
+       gui->unlock_window();
+
        int ret = 0;
        Asset *current = save_edl->assets->first;
        for( int i=0; !ret && current; ++i, current=NEXT ) {
        int ret = 0;
        Asset *current = save_edl->assets->first;
        for( int i=0; !ret && current; ++i, current=NEXT ) {
@@ -3910,7 +4673,7 @@ void MWindow::save_project(const char *dir, int save_mode, int overwrite, int re
                gui->lock_window("MWindow::save_project");
                ArrayList<char*> filenames;
                filenames.append(filename);
                gui->lock_window("MWindow::save_project");
                ArrayList<char*> filenames;
                filenames.append(filename);
-               load_filenames(&filenames, LOADMODE_REPLACE);
+               load_filenames(&filenames);
                gui->unlock_window();
        }
 }
                gui->unlock_window();
        }
 }
@@ -3927,11 +4690,14 @@ static inline int gcd(int m, int n)
 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
 {
        w = 1;  h = 1;
 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
 {
        w = 1;  h = 1;
+       double ar;
+       
        if(!width || !height) return 1;
        if( width == 720 && (height == 480 || height == 576) ) {
                w = 4;  h = 3;  return 0; // for NTSC and PAL
        }
        if(!width || !height) return 1;
        if( width == 720 && (height == 480 || height == 576) ) {
                w = 4;  h = 3;  return 0; // for NTSC and PAL
        }
-       double ar = (double)width / height;
+       
+       ar = (double)width / height;
 // square-ish pixels
        if( EQUIV(ar, 1.0000) ) return 0;
        if( EQUIV(ar, 1.3333) ) { w = 4;  h = 3;  return 0; }
 // square-ish pixels
        if( EQUIV(ar, 1.0000) ) return 0;
        if( EQUIV(ar, 1.3333) ) { w = 4;  h = 3;  return 0; }
@@ -3939,6 +4705,8 @@ int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
        if( EQUIV(ar, 2.1111) ) { w = 19; h = 9;  return 0; }
        if( EQUIV(ar, 2.2222) ) { w = 20; h = 9;  return 0; }
        if( EQUIV(ar, 2.3333) ) { w = 21; h = 9;  return 0; }
        if( EQUIV(ar, 2.1111) ) { w = 19; h = 9;  return 0; }
        if( EQUIV(ar, 2.2222) ) { w = 20; h = 9;  return 0; }
        if( EQUIV(ar, 2.3333) ) { w = 21; h = 9;  return 0; }
+       if( EQUIV(ar, 2.37037) ) { w = 64; h = 27;  return 0; }
+
        int ww = width, hh = height;
        // numerator, denominator must be under mx
        int mx = 255, n = gcd(ww, hh);
        int ww = width, hh = height;
        // numerator, denominator must be under mx
        int mx = 255, n = gcd(ww, hh);
@@ -3962,33 +4730,39 @@ int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
        return 0;
 }
 
        return 0;
 }
 
-void MWindow::reset_caches()
+void MWindow::reset_caches(int locked)
 {
 {
-       frame_cache->remove_all();
-       wave_cache->remove_all();
-       audio_cache->remove_all();
-       video_cache->remove_all();
-       if( cwindow->playback_engine ) {
-               if( cwindow->playback_engine->audio_cache )
-                       cwindow->playback_engine->audio_cache->remove_all();
-               if( cwindow->playback_engine->video_cache )
-                       cwindow->playback_engine->video_cache->remove_all();
-       }
+       if( locked ) gui->unlock_window();
+       awindow->gui->stop_vicon_drawing(1);
+       if( cwindow->playback_engine )
+               cwindow->playback_engine->create_cache();
        for(int i = 0; i < vwindows.size(); i++) {
                VWindow *vwindow = vwindows[i];
                if( !vwindow->is_running() ) continue;
                if( !vwindow->playback_engine ) continue;
        for(int i = 0; i < vwindows.size(); i++) {
                VWindow *vwindow = vwindows[i];
                if( !vwindow->is_running() ) continue;
                if( !vwindow->playback_engine ) continue;
-               if( vwindow->playback_engine->audio_cache )
-                       vwindow->playback_engine->audio_cache->remove_all();
-               if( vwindow->playback_engine->video_cache )
-                       vwindow->playback_engine->video_cache->remove_all();
+               vwindow->playback_engine->create_cache();
        }
        }
+       gui->lock_window("MWindow::reset_caches");
+       frame_cache->remove_all();
+       wave_cache->remove_all();
+       audio_cache->remove_all();
+       video_cache->remove_all();
+       if( !locked ) gui->unlock_window();
 }
 
 }
 
-void MWindow::remove_asset_from_caches(Asset *asset)
+void MWindow::remove_from_caches(Indexable *idxbl)
 {
 {
-       frame_cache->remove_asset(asset);
-       wave_cache->remove_asset(asset);
+       awindow->gui->stop_vicon_drawing(1);
+       frame_cache->remove_item(idxbl);
+       wave_cache->remove_item(idxbl);
+       if( gui->render_engine &&
+           gui->render_engine_id == idxbl->id ) {
+               delete gui->render_engine;
+               gui->render_engine = 0;
+       }
+       gui->resource_thread->close_indexable(idxbl);
+       if( !idxbl->is_asset ) return;
+       Asset *asset = (Asset *)idxbl;
        audio_cache->delete_entry(asset);
        video_cache->delete_entry(asset);
        if( cwindow->playback_engine && cwindow->playback_engine->audio_cache )
        audio_cache->delete_entry(asset);
        video_cache->delete_entry(asset);
        if( cwindow->playback_engine && cwindow->playback_engine->audio_cache )
@@ -4004,53 +4778,69 @@ void MWindow::remove_asset_from_caches(Asset *asset)
                if( vwindow->playback_engine->video_cache )
                        vwindow->playback_engine->video_cache->delete_entry(asset);
        }
                if( vwindow->playback_engine->video_cache )
                        vwindow->playback_engine->video_cache->delete_entry(asset);
        }
+       for(int i = 0; i < zwindows.size(); i++) {
+               ZWindow *zwindow = zwindows[i];
+               if( !zwindow->is_running() ) continue;
+               if( zwindow->zgui->playback_engine->audio_cache )
+                       zwindow->zgui->playback_engine->audio_cache->delete_entry(asset);
+               if( zwindow->zgui->playback_engine->video_cache )
+                       zwindow->zgui->playback_engine->video_cache->delete_entry(asset);
+       }
+       awindow->gui->start_vicon_drawing();
 }
 
 }
 
-
-void MWindow::remove_assets_from_project(int push_undo, int redraw,
+void MWindow::remove_assets_from_project(int push_undo, int redraw, int delete_indexes,
                ArrayList<Indexable*> *drag_assets, ArrayList<EDL*> *drag_clips)
 {
                ArrayList<Indexable*> *drag_assets, ArrayList<EDL*> *drag_clips)
 {
-       for(int i = 0; i < drag_assets->total; i++) {
-               Indexable *indexable = drag_assets->get(i);
-               if(indexable->is_asset) remove_asset_from_caches((Asset*)indexable);
-       }
+       awindow->gui->stop_vicon_drawing(1);
 
 // Remove from VWindow.
 
 // Remove from VWindow.
-       for(int i = 0; i < session->drag_clips->total; i++) {
-               for(int j = 0; j < vwindows.size(); j++) {
-                       VWindow *vwindow = vwindows[j];
-                       if( !vwindow->is_running() ) continue;
-                       if(session->drag_clips->get(i) == vwindow->get_edl()) {
-                               vwindow->gui->lock_window("MWindow::remove_assets_from_project 1");
-                               vwindow->delete_source(1, 1);
-                               vwindow->gui->unlock_window();
+       if( drag_clips ) {
+               for(int i = 0; i < drag_clips->total; i++) {
+                       for(int j = 0; j < vwindows.size(); j++) {
+                               VWindow *vwindow = vwindows[j];
+                               if( !vwindow->is_running() ) continue;
+                               if(drag_clips->get(i) == vwindow->get_edl()) {
+                                       vwindow->gui->lock_window("MWindow::remove_assets_from_project 1");
+                                       vwindow->delete_source(1, 1);
+                                       vwindow->gui->unlock_window();
+                               }
                        }
                }
        }
 
                        }
                }
        }
 
-       for(int i = 0; i < drag_assets->size(); i++) {
-               for(int j = 0; j < vwindows.size(); j++) {
-                       VWindow *vwindow = vwindows[j];
-                       if( !vwindow->is_running() ) continue;
-                       if(drag_assets->get(i) == vwindow->get_source()) {
-                               vwindow->gui->lock_window("MWindow::remove_assets_from_project 2");
-                               vwindow->delete_source(1, 1);
-                               vwindow->gui->unlock_window();
+       if( drag_assets ) {
+               for(int i = 0; i < drag_assets->size(); i++) {
+                       for(int j = 0; j < vwindows.size(); j++) {
+                               VWindow *vwindow = vwindows[j];
+                               if( !vwindow->is_running() ) continue;
+                               if(drag_assets->get(i) == vwindow->get_source()) {
+                                       vwindow->gui->lock_window("MWindow::remove_assets_from_project 2");
+                                       vwindow->delete_source(1, 1);
+                                       vwindow->gui->unlock_window();
+                               }
                        }
                }
                        }
                }
-       }
 
 
-       for(int i = 0; i < drag_assets->size(); i++) {
-               Indexable *indexable = drag_assets->get(i);
-               remove_indexfile(indexable);
+               for(int i = 0; i < drag_assets->total; i++) {
+                       Indexable *indexable = drag_assets->get(i);
+                       if(indexable->is_asset) remove_from_caches(indexable);
+               }
+
+               if( delete_indexes ) {
+                       for(int i = 0; i < drag_assets->size(); i++) {
+                               Indexable *indexable = drag_assets->get(i);
+                               remove_indexfile(indexable);
+                       }
+               }
        }
 
 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
        }
 
 //printf("MWindow::rebuild_indices 1 %s\n", indexable->path);
-       if(push_undo) undo->update_undo_before();
+       if(push_undo) undo_before();
        if(drag_assets) edl->remove_from_project(drag_assets);
        if(drag_clips) edl->remove_from_project(drag_clips);
        if(redraw) save_backup();
        if(drag_assets) edl->remove_from_project(drag_assets);
        if(drag_clips) edl->remove_from_project(drag_clips);
        if(redraw) save_backup();
-       if(push_undo) undo->update_undo_after(_("remove assets"), LOAD_ALL);
+       if(push_undo) undo_after(_("remove assets"), LOAD_ALL);
        if(redraw) {
                restart_brender();
 
        if(redraw) {
                restart_brender();
 
@@ -4067,16 +4857,14 @@ void MWindow::remove_assets_from_project(int push_undo, int redraw,
 
 void MWindow::remove_assets_from_disk()
 {
 
 void MWindow::remove_assets_from_disk()
 {
+       remove_assets_from_project(1, 1, 1,
+               session->drag_assets, session->drag_clips);
+
 // Remove from disk
        for(int i = 0; i < session->drag_assets->total; i++)
        {
                remove(session->drag_assets->get(i)->path);
        }
 // Remove from disk
        for(int i = 0; i < session->drag_assets->total; i++)
        {
                remove(session->drag_assets->get(i)->path);
        }
-
-       remove_assets_from_project(1,
-               1,
-               session->drag_assets,
-               session->drag_clips);
 }
 
 void MWindow::dump_plugins(FILE *fp)
 }
 
 void MWindow::dump_plugins(FILE *fp)
@@ -4159,6 +4947,14 @@ void MWindow::dump_exe(FILE *fp)
        fprintf(fp, "\n");
 }
 
        fprintf(fp, "\n");
 }
 
+void MWindow::dump_caches(FILE *fp)
+{
+       fprintf(fp, "audio cache: ");
+       audio_cache->dump(fp);
+       fprintf(fp, "video cache: ");
+       video_cache->dump(fp);
+}
+
 void MWindow::trap_hook(FILE *fp, void *vp)
 {
        MWindow *mwindow = (MWindow *)vp;
 void MWindow::trap_hook(FILE *fp, void *vp)
 {
        MWindow *mwindow = (MWindow *)vp;
@@ -4170,6 +4966,8 @@ void MWindow::trap_hook(FILE *fp, void *vp)
        mwindow->dump_undo(fp);
        fprintf(fp, "\nEXE: %s\n", AboutPrefs::build_timestamp);
        mwindow->dump_exe(fp);
        mwindow->dump_undo(fp);
        fprintf(fp, "\nEXE: %s\n", AboutPrefs::build_timestamp);
        mwindow->dump_exe(fp);
+       fprintf(fp, "\nCACHES:\n");
+       mwindow->dump_caches(fp);
 }
 
 
 }
 
 
@@ -4240,31 +5038,30 @@ int MWindow::interrupt_indexes()
 
 void MWindow::next_time_format()
 {
 
 void MWindow::next_time_format()
 {
-       switch(edl->session->time_format)
-       {
-               case TIME_HMS: edl->session->time_format = TIME_HMSF; break;
-               case TIME_HMSF: edl->session->time_format = TIME_SAMPLES; break;
-               case TIME_SAMPLES: edl->session->time_format = TIME_SAMPLES_HEX; break;
-               case TIME_SAMPLES_HEX: edl->session->time_format = TIME_FRAMES; break;
-               case TIME_FRAMES: edl->session->time_format = TIME_FEET_FRAMES; break;
-               case TIME_FEET_FRAMES: edl->session->time_format = TIME_SECONDS; break;
-               case TIME_SECONDS: edl->session->time_format = TIME_HMS; break;
+       switch( edl->session->time_format ) {
+       case TIME_HMS:          edl->session->time_format = TIME_HMSF;         break;
+       case TIME_HMSF:         edl->session->time_format = TIME_TIMECODE;     break;
+       case TIME_TIMECODE:     edl->session->time_format = TIME_FRAMES;       break;
+       case TIME_FRAMES:       edl->session->time_format = TIME_SAMPLES;      break;
+       case TIME_SAMPLES:      edl->session->time_format = TIME_SAMPLES_HEX;  break;
+       case TIME_SAMPLES_HEX:  edl->session->time_format = TIME_SECONDS;      break;
+       case TIME_SECONDS:      edl->session->time_format = TIME_FEET_FRAMES;  break;
+       case TIME_FEET_FRAMES:  edl->session->time_format = TIME_HMS;          break;
        }
        }
-
        time_format_common();
 }
 
 void MWindow::prev_time_format()
 {
        time_format_common();
 }
 
 void MWindow::prev_time_format()
 {
-       switch(edl->session->time_format)
-       {
-               case TIME_HMS: edl->session->time_format = TIME_SECONDS; break;
-               case TIME_SECONDS: edl->session->time_format = TIME_FEET_FRAMES; break;
-               case TIME_FEET_FRAMES: edl->session->time_format = TIME_FRAMES; break;
-               case TIME_FRAMES: edl->session->time_format = TIME_SAMPLES_HEX; break;
-               case TIME_SAMPLES_HEX: edl->session->time_format = TIME_SAMPLES; break;
-               case TIME_SAMPLES: edl->session->time_format = TIME_HMSF; break;
-               case TIME_HMSF: edl->session->time_format = TIME_HMS; break;
+       switch( edl->session->time_format ) {
+       case TIME_HMS:          edl->session->time_format = TIME_FEET_FRAMES;  break;
+       case TIME_HMSF:         edl->session->time_format = TIME_HMS;          break;
+       case TIME_TIMECODE:     edl->session->time_format = TIME_HMSF;         break;
+       case TIME_FRAMES:       edl->session->time_format = TIME_TIMECODE;     break;
+       case TIME_SAMPLES:      edl->session->time_format = TIME_FRAMES;       break;
+       case TIME_SAMPLES_HEX:  edl->session->time_format = TIME_SAMPLES;      break;
+       case TIME_SECONDS:      edl->session->time_format = TIME_SAMPLES_HEX;  break;
+       case TIME_FEET_FRAMES:  edl->session->time_format = TIME_SECONDS;      break;
        }
 
        time_format_common();
        }
 
        time_format_common();
@@ -4290,32 +5087,25 @@ int MWindow::set_filename(const char *filename)
                strcpy(session->filename, filename);
        if( filename != edl->path )
                strcpy(edl->path, filename);
                strcpy(session->filename, filename);
        if( filename != edl->path )
                strcpy(edl->path, filename);
+       return set_titlebar(filename);
+}
 
 
-       if(gui)
-       {
-               if(filename[0] == 0)
-               {
-                       gui->set_title(PROGRAM_NAME);
-               }
-               else
-               {
-                       FileSystem dir;
-                       char string[BCTEXTLEN], string2[BCTEXTLEN];
-                       dir.extract_name(string, filename);
-                       sprintf(string2, PROGRAM_NAME ": %s", string);
-                       gui->set_title(string2);
-               }
+int MWindow::set_titlebar(const char *filename)
+{
+       if( !gui ) return 0;
+       if( filename[0] ) {
+               FileSystem dir;
+               char string[BCTEXTLEN], string2[BCTEXTLEN];
+               dir.extract_name(string, filename);
+               sprintf(string2, PROGRAM_NAME ": %s", string);
+               gui->set_title(string2);
        }
        }
+       else
+               gui->set_title(PROGRAM_NAME);
        return 0;
 }
 
 
        return 0;
 }
 
 
-
-
-
-
-
-
 int MWindow::set_loop_boundaries()
 {
        double start = edl->local_session->get_selectionstart();
 int MWindow::set_loop_boundaries()
 {
        double start = edl->local_session->get_selectionstart();
@@ -4407,7 +5197,7 @@ void MWindow::resync_guis()
        lwindow->gui->panel->set_meters(channels, 1);
        lwindow->gui->flush();
        lwindow->gui->unlock_window();
        lwindow->gui->panel->set_meters(channels, 1);
        lwindow->gui->flush();
        lwindow->gui->unlock_window();
-
+#ifdef GLx4
 // Warn user
        if(((edl->session->output_w % 4) ||
                (edl->session->output_h % 4)) &&
 // Warn user
        if(((edl->session->output_w % 4) ||
                (edl->session->output_h % 4)) &&
@@ -4417,7 +5207,7 @@ void MWindow::resync_guis()
                        _("This project's dimensions are not multiples of 4 so\n"
                        "it can't be rendered by OpenGL."));
        }
                        _("This project's dimensions are not multiples of 4 so\n"
                        "it can't be rendered by OpenGL."));
        }
-
+#endif
 
 // Flash frame
        sync_parameters(CHANGE_ALL);
 
 // Flash frame
        sync_parameters(CHANGE_ALL);
@@ -4433,7 +5223,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
        session->auto_keyframes = 0;
        int result = file->open_file(preferences, asset, 1, 0);
        if( !result && delete_tracks > 0 )
        session->auto_keyframes = 0;
        int result = file->open_file(preferences, asset, 1, 0);
        if( !result && delete_tracks > 0 )
-               undo->update_undo_before();
+               undo_before();
        int video_layers = asset->get_video_layers();
        if( !result && asset->video_data && vstream < video_layers ) {
                // try to get asset up to date, may fail
        int video_layers = asset->get_video_layers();
        if( !result && asset->video_data && vstream < video_layers ) {
                // try to get asset up to date, may fail
@@ -4442,8 +5232,10 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                double framerate = asset->get_frame_rate();
                int width = asset->get_w();
                int height = asset->get_h();
                double framerate = asset->get_frame_rate();
                int width = asset->get_w();
                int height = asset->get_h();
+#ifdef GLx4
                // must be multiple of 4 for opengl
                width = (width+3) & ~3;  height = (height+3) & ~3;
                // must be multiple of 4 for opengl
                width = (width+3) & ~3;  height = (height+3) & ~3;
+#endif
                int driver = session->playback_config->vconfig->driver;
                int color_model = file->get_best_colormodel(asset, driver);
 //             color_model = BC_CModels::is_yuv(color_model) ?
                int driver = session->playback_config->vconfig->driver;
                int color_model = file->get_best_colormodel(asset, driver);
 //             color_model = BC_CModels::is_yuv(color_model) ?
@@ -4458,12 +5250,26 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                session->output_w = width;
                session->output_h = height;
                session->frame_rate = framerate;
                session->output_w = width;
                session->output_h = height;
                session->frame_rate = framerate;
+               session->interlace_mode = asset->interlace_mode;
                // not, asset->actual_width/actual_height
                asset->width = session->output_w;
                asset->height = session->output_h;
                asset->frame_rate = session->frame_rate;
                // not, asset->actual_width/actual_height
                asset->width = session->output_w;
                asset->height = session->output_h;
                asset->frame_rate = session->frame_rate;
+               
                create_aspect_ratio(session->aspect_w, session->aspect_h,
                        session->output_w, session->output_h);
                create_aspect_ratio(session->aspect_w, session->aspect_h,
                        session->output_w, session->output_h);
+       float ar = asset->aspect_ratio;
+       if (ar) {
+       //printf ("Aspect ratio from asset: %f \n", ar);
+       if( EQUIV(ar, 1.3333) ) { session->aspect_w = 4;  session->aspect_h = 3;  }
+       if( EQUIV(ar, 1.7777) ) { session->aspect_w = 16; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.1111) ) { session->aspect_w = 19; session->aspect_h = 9;  }
+       if( EQUIV(ar, 2.2222) ) { session->aspect_w = 20; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.3333) ) { session->aspect_w = 21; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.370370) ) { session->aspect_w = 64; session->aspect_h = 27; }
+       }
+                       
+                       
                Track *track = edl->tracks->first;
                for( Track *next_track=0; track; track=next_track ) {
                        next_track = track->next;
                Track *track = edl->tracks->first;
                for( Track *next_track=0; track; track=next_track ) {
                        next_track = track->next;
@@ -4483,7 +5289,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                                track->track_h = edl->session->output_h;
                        }
                        else if( delete_tracks )
                                track->track_h = edl->session->output_h;
                        }
                        else if( delete_tracks )
-                               edl->tracks->delete_track(track);
+                               edl->tracks->delete_track(track, 0);
                }
                edl->retrack();
                edl->resample(old_framerate, session->frame_rate, TRACK_VIDEO);
                }
                edl->retrack();
                edl->resample(old_framerate, session->frame_rate, TRACK_VIDEO);
@@ -4498,7 +5304,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                int channels = 0;
                for( uint64_t mask=channel_mask; mask!=0; mask>>=1 ) channels += mask & 1;
                if( channels < 1 ) channels = 1;
                int channels = 0;
                for( uint64_t mask=channel_mask; mask!=0; mask>>=1 ) channels += mask & 1;
                if( channels < 1 ) channels = 1;
-               if( channels > 6 ) channels = 6;
+               if( channels > MAXCHANNELS ) channels = MAXCHANNELS;
                session->audio_tracks = session->audio_channels = channels;
 
                int *achannel_positions = preferences->channel_positions[session->audio_channels-1];
                session->audio_tracks = session->audio_channels = channels;
 
                int *achannel_positions = preferences->channel_positions[session->audio_channels-1];
@@ -4519,7 +5325,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                                                delete edit;
                                }
                                if( !track->edits->first )
                                                delete edit;
                                }
                                if( !track->edits->first )
-                                       edl->tracks->delete_track(track);
+                                       edl->tracks->delete_track(track, 0);
                        }
                }
                edl->rechannel();
                        }
                }
                edl->rechannel();
@@ -4529,7 +5335,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
        session->auto_keyframes = old_auto_keyframes;
        if( !result && delete_tracks > 0 ) {
                save_backup();
        session->auto_keyframes = old_auto_keyframes;
        if( !result && delete_tracks > 0 ) {
                save_backup();
-               undo->update_undo_after(_("select asset"), LOAD_ALL);
+               undo_after(_("select asset"), LOAD_ALL);
        }
        resync_guis();
        return result;
        }
        resync_guis();
        return result;
@@ -4601,3 +5407,74 @@ PatchGUI *MWindow::get_patchgui(Track *track)
         return patchgui;
 }
 
         return patchgui;
 }
 
+int MWindow::get_cpus(int out_w, int out_h)
+{
+       if( !out_w ) out_w = edl->session->output_w;
+       if( !out_h ) out_h = edl->session->output_h;
+       int cpus = out_w*out_h/0x80000 + 1;
+       if( cpus > preferences->processors )
+               cpus = preferences->processors;
+       return cpus;
+}
+int MWindow::get_cpus()
+{
+       return get_cpus(edl->session->output_w, edl->session->output_h);
+}
+
+void MWindow::draw_trackmovement()
+{
+       if( !redraw_tracks )
+               redraw_tracks = new DrawTrackMovement(this);
+       redraw_tracks->start();
+}
+
+DrawTrackMovement::DrawTrackMovement(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+       this->mwindow = mwindow;
+}
+DrawTrackMovement::~DrawTrackMovement()
+{
+       join();
+}
+
+void DrawTrackMovement::run()
+{
+       mwindow->gui->lock_window("DrawTrackMovement::run");
+       mwindow->edl->tracks->update_y_pixels(mwindow->theme);
+       mwindow->gui->draw_trackmovement();
+       mwindow->gui->unlock_window();
+}
+
+
+ConfirmRefWindow::ConfirmRefWindow(MWindow *mwindow, char *path,
+               int px, int py, int cw, int ch)
+ : BC_Window(_(PROGRAM_NAME ": Confirm update"), px, py, cw, ch, cw, ch)
+{
+       this->mwindow = mwindow;
+       this->path = path;
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("File by Reference");
+}
+
+ConfirmRefWindow::~ConfirmRefWindow()
+{
+}
+
+void ConfirmRefWindow::create_objects()
+{
+       lock_window("ConfirmRefWindow::create_objects()");
+       int x = xS(10), y = yS(10), pad = yS(5);
+       BC_Title *title;
+       add_subwindow(title = new BC_Title(x, y, _("FileREF not updated:")));
+       y += title->get_h() + pad;
+       BC_TextBox *text_box;
+       add_subwindow(text_box = new BC_TextBox(x,y, get_w()-2*x, 1, path));
+       y += text_box->get_h() + 2*pad;
+       add_subwindow(title = new BC_Title(x, y, _("Save file ref changes?")));
+       add_subwindow(new BC_OKButton(this));
+       add_subwindow(new BC_CancelButton(this));
+       show_window();
+       unlock_window();
+}
+