refresh frame fix, dblclk proxy viewer fix, vicon refresh fix for awdw resize, fix...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 992e9f342823dff1d3cec31537a08a3269725982..f4a850b9838f4ba743cf8f831044eb851e9a0dd5 100644 (file)
@@ -41,6 +41,7 @@
 #include "clipedls.h"
 #include "bccmodels.h"
 #include "commercials.h"
+#include "confirmsave.h"
 #include "cplayback.h"
 #include "ctimebar.h"
 #include "cwindowgui.h"
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/mman.h>
+#include <sys/file.h>
 #include <limits.h>
 #include <errno.h>
 
@@ -173,6 +175,7 @@ extern "C"
 }
 
 
+extern long cin_timezone;
 
 ArrayList<PluginServer*>* MWindow::plugindb = 0;
 Commercials* MWindow::commercials = 0;
@@ -423,8 +426,8 @@ const char *MWindow::default_std()
                }
        }
 
-//__timezone: Seconds west of UTC.  240sec/deg
-       double tz_deg = -__timezone / 240.;
+// cin_timezone: Seconds west of UTC.  240sec/deg
+       double tz_deg =  -cin_timezone / 240.;
 // from Honolulu = -10, to New York = -5, 15deg/hr   lat -150..-75
        return tz_deg >= -10*15 && tz_deg <= -5*15 ? "NTSC" : "PAL";
 }
@@ -521,12 +524,11 @@ void MWindow::get_plugin_path(char *path, const char *plug_dir, const char *fs_p
        delete [] base_path;
 }
 
-int MWindow::load_plugin_index(MWindow *mwindow, const char *index_path, const char *plugin_dir)
+int MWindow::load_plugin_index(MWindow *mwindow, FILE *fp, const char *plugin_dir)
 {
-// load index
-       FILE *fp = fopen(index_path, "r");
        if( !fp ) return -1;
-
+// load index
+       fseek(fp, 0, SEEK_SET);
        int ret = 0;
        char index_line[BCTEXTLEN];
        int index_version = -1, len = strlen(plugin_dir);
@@ -574,7 +576,6 @@ int MWindow::load_plugin_index(MWindow *mwindow, const char *index_path, const c
                        ret = 1;  continue;
                }
        }
-       fclose(fp);
 
        if( !ret )
                ret = check_plugin_index(plugins, plugin_dir, ".");
@@ -621,21 +622,36 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
        create_defaults_path(index_path, PLUGIN_FILE);
        char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
        strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
-       if( !load_plugin_index(mwindow, index_path, plugin_path) ) return 1;
-       printf("init plugin index: %s\n", plugin_path);
-       FILE *fp = fopen(index_path,"w");
+       FILE *fp = fopen(index_path,"a+");
        if( !fp ) {
                fprintf(stderr,_("MWindow::init_plugins: "
-                       "can't create plugin index: %s\n"), index_path);
-               return 1;
+                       "can't open plugin index: %s\n"), index_path);
+               return -1;
+       }
+       int fd = fileno(fp), ret = -1;
+       if( !flock(fd, LOCK_EX) ) {
+               fseek(fp, 0, SEEK_SET);
+               ret = load_plugin_index(mwindow, fp, plugin_path);
+       }
+       if( ret > 0 ) {
+               ftruncate(fd, 0);
+               fseek(fp, 0, SEEK_SET);
+               printf("init plugin index: %s\n", plugin_path);
+               fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
+               fprintf(fp, "%s\n", plugin_path);
+               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);
+       }
+       if( ret ) {
+               fprintf(stderr,_("MWindow::init_plugins: "
+                       "can't %s plugin index: %s\n"),
+                       ret>0 ? _("create") : _("lock"), index_path);
        }
-       fprintf(fp, "%d\n", PLUGIN_FILE_VERSION);
-       fprintf(fp, "%s\n", plugin_path);
-       init_plugin_index(mwindow, preferences, fp, plugin_path);
-       init_ffmpeg_index(mwindow, preferences, fp);
-       init_lv2_index(mwindow, preferences, fp);
        fclose(fp);
-       return load_plugin_index(mwindow, index_path, plugin_path);
+       return ret;
 }
 
 int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
@@ -659,9 +675,30 @@ int MWindow::init_ladspa_plugins(MWindow *mwindow, Preferences *preferences)
                for( char *bp=plugin_path; *bp!=0; ++bp )
                        *cp++ = *bp=='/' ? '_' : *bp;
                *cp = 0;
-               if( !load_plugin_index(mwindow, index_path, plugin_path) ) continue;
-               if( init_ladspa_index(mwindow, preferences, index_path, plugin_path) ) continue;
-               load_plugin_index(mwindow, index_path, plugin_path);
+               FILE *fp = fopen(index_path,"a+");
+               if( !fp ) {
+                       fprintf(stderr,_("MWindow::init_ladspa_plugins: "
+                               "can't open ladspa plugin index: %s\n"), index_path);
+                       continue;
+               }
+               int fd = fileno(fp), ret = -1;
+               if( !flock(fd, LOCK_EX) ) {
+                       fseek(fp, 0, SEEK_SET);
+                       ret = load_plugin_index(mwindow, fp, plugin_path);
+               }
+               if( ret > 0 ) {
+                       ftruncate(fd, 0);
+                       fseek(fp, 0, SEEK_SET);
+                       init_ladspa_index(mwindow, preferences, fp, plugin_path);
+                       fseek(fp, 0, SEEK_SET);
+                       ret = load_plugin_index(mwindow, fp, plugin_path);
+               }
+               if( ret ) {
+                       fprintf(stderr,_("MWindow::init_ladspa_plugins: "
+                               "can't %s ladspa plugin index: %s\n"),
+                               ret>0 ? _("create") : _("lock"), index_path);
+               }
+               fclose(fp);
        }
        return 1;
 }
@@ -1270,6 +1307,7 @@ void MWindow::stop_mixers()
 
 void MWindow::close_mixers(int destroy)
 {
+       ArrayList<ZWindow*> closed;
        zwindows_lock->lock("MWindow::close_mixers");
        for( int i=zwindows.size(); --i>=0; ) {
                ZWindow *zwindow = zwindows[i];
@@ -1279,12 +1317,12 @@ void MWindow::close_mixers(int destroy)
                zgui->lock_window("MWindow::select_zwindow 0");
                zgui->set_done(0);
                zgui->unlock_window();
+               closed.append(zwindow);
        }
        zwindows_lock->unlock();
-       for( int i=0; i<zwindows.size(); ++i ) {
-               ZWindow *zwindow = zwindows[i];
-               if( zwindow->idx < 0 ) continue;
-               zwindow->close_window();
+       for( int i=0; i<closed.size(); ++i ) {
+               ZWindow *zwindow = closed[i];
+               zwindow->join();
        }
 }
 
@@ -1517,9 +1555,7 @@ void MWindow::init_indexes()
 void MWindow::init_gui()
 {
        gui = new MWindowGUI(this);
-       gui->lock_window("MWindow::init_gui");
        gui->create_objects();
-       gui->unlock_window();
        gui->load_defaults(defaults);
 }
 
@@ -2134,11 +2170,12 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
              load_mode == LOADMODE_REPLACE_CONCATENATE ) ) {
                select_asset(0, 0);
                edl->session->proxy_scale = 1;
+               edl->session->proxy_disabled_scale = 1;
                edl->session->proxy_use_scaler = 0;
                edl->session->proxy_auto_scale = 0;
                edl->session->proxy_beep = 0;
                edl->local_session->preview_start = 0;
-               edl->local_session->preview_end = 0;
+               edl->local_session->preview_end = -1;
                edl->local_session->loop_playback = 0;
                edl->local_session->set_selectionstart(0);
                edl->local_session->set_selectionend(0);
@@ -2251,6 +2288,172 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
        return !result ? proxy_render.needed_proxies.size() : -1;
 }
 
+int MWindow::enable_proxy()
+{
+       int ret = 0;
+       if( edl->session->proxy_scale == 1 &&
+           edl->session->proxy_disabled_scale != 1 ) {
+               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();
+               if( ret > 0 )
+                       beep(2000., 1.5, 0.5);
+       }
+       return 1;
+}
+
+int MWindow::disable_proxy()
+{
+       if( edl->session->proxy_scale != 1 &&
+           edl->session->proxy_disabled_scale == 1 ) {
+               int new_scale = 1;
+               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();
+       }
+       return 1;
+}
+
+int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
+{
+       ArrayList<Indexable*> orig_idxbls;
+       ArrayList<Indexable*> proxy_assets;
+
+       edl->Garbage::add_user();
+       save_backup();
+       undo->update_undo_before(_("proxy"), this);
+       ProxyRender proxy_render(this, asset);
+
+// 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 ) {
+               Asset *orig_asset = edl->assets->first;
+               for( ; orig_asset; orig_asset=orig_asset->next ) {
+                       char new_path[BCTEXTLEN];
+                       proxy_render.to_proxy_path(new_path, orig_asset, proxy_scale);
+// test if proxy asset was already added to proxy_assets
+                       int got_it = 0;
+                       for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
+                               got_it = !strcmp(proxy_assets[i]->path, new_path);
+                       if( got_it ) continue;
+                       Asset *proxy_asset = edl->assets->get_asset(new_path);
+                       if( !proxy_asset ) continue;
+// add pointer to existing EDL asset if it exists
+// EDL won't delete it unless it's the same pointer.
+                       proxy_assets.append(proxy_asset);
+                       proxy_asset->add_user();
+                       orig_idxbls.append(orig_asset);
+                       orig_asset->add_user();
+               }
+               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;
+                       for( int i = 0; !got_it && i<proxy_assets.size(); ++i )
+                               got_it = !strcmp(proxy_assets[i]->path, new_path);
+                       if( got_it ) continue;
+                       Asset *proxy_nested = edl->assets->get_asset(new_path);
+                       if( !proxy_nested ) continue;
+// add pointer to existing EDL asset if it exists
+// EDL won't delete it unless it's the same pointer.
+                       proxy_assets.append(proxy_nested);
+                       proxy_nested->add_user();
+                       orig_idxbls.append(orig_nested);
+                       orig_nested->add_user();
+               }
+
+// convert from the proxy assets to the original assets
+               edl->set_proxy(1, 0, &proxy_assets, &orig_idxbls);
+
+// remove the references
+               for( int i=0; i<proxy_assets.size(); ++i ) {
+                       Asset *proxy = (Asset *) proxy_assets[i];
+                       proxy->width = proxy->actual_width;
+                       proxy->height = proxy->actual_height;
+                       proxy->remove_user();
+                       edl->assets->remove_pointer(proxy);
+                       proxy->remove_user();
+               }
+               proxy_assets.remove_all();
+               for( int i = 0; i < orig_idxbls.size(); i++ )
+                       orig_idxbls[i]->remove_user();
+               orig_idxbls.remove_all();
+       }
+
+       ArrayList<char *> confirm_paths;    // test for new files
+       confirm_paths.set_array_delete();
+
+// convert to new size if not original size
+       if( new_scale != 1 ) {
+               FileSystem fs;
+               Asset *orig = edl->assets->first;
+               for( ; orig; orig=orig->next ) {
+                       Asset *proxy = proxy_render.add_original(orig, new_scale);
+                       if( !proxy ) continue;
+                       int exists = fs.get_size(proxy->path) > 0 ? 1 : 0;
+                       int got_it = exists && // if proxy exists, and is newer than orig
+                           fs.get_date(proxy->path) > fs.get_date(orig->path) ? 1 : 0;
+                       if( !got_it ) {
+                               if( exists ) // prompt user to overwrite
+                                       confirm_paths.append(cstrdup(proxy->path));
+                               proxy_render.add_needed(orig, proxy);
+                       }
+               }
+               for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
+                       EDL *orig_nested = edl->nested_edls[i];
+                       Asset *proxy = proxy_render.add_original(orig_nested, new_scale);
+                       if( !proxy ) continue;
+                       int exists = fs.get_size(proxy->path) > 0 ? 1 : 0;
+                       int got_it = exists && // if proxy exists, and is newer than orig_nested
+                           fs.get_date(proxy->path) > fs.get_date(orig_nested->path) ? 1 : 0;
+                       if( !got_it ) {
+                               if( exists ) // prompt user to overwrite
+                                       confirm_paths.append(cstrdup(proxy->path));
+                               proxy_render.add_needed(orig_nested, proxy);
+                       }
+               }
+       }
+
+       int result = 0;
+// test for existing files
+       if( confirm_paths.size() ) {
+               result = ConfirmSave::test_files(this, &confirm_paths);
+               confirm_paths.remove_all_objects();
+       }
+
+       if( !result )
+               result = proxy_render.create_needed_proxies(new_scale);
+
+       if( !result ) // resize project
+               edl->set_proxy(new_scale, new_use_scaler,
+                       &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
+
+       undo->update_undo_after(_("proxy"), LOAD_ALL);
+       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;
+}
+
 void MWindow::test_plugins(EDL *new_edl, char *path)
 {
        char string[BCTEXTLEN];
@@ -2348,14 +2551,14 @@ void MWindow::create_objects(int want_gui,
        check_language();
        init_preferences();
        if(splash_window)
-               splash_window->operation->update(_("Initializing Plugins"));
+               splash_window->update_status(_("Initializing Plugins"));
        init_plugins(this, preferences);
        if(debug) PRINT_TRACE
        init_ladspa_plugins(this, preferences);
        if(debug) PRINT_TRACE
        init_plugin_tips(*plugindb, cin_lang);
        if(splash_window)
-               splash_window->operation->update(_("Initializing GUI"));
+               splash_window->update_status(_("Initializing GUI"));
        if(debug) PRINT_TRACE
        init_theme();
 
@@ -2363,7 +2566,7 @@ void MWindow::create_objects(int want_gui,
        init_error();
 
        if(splash_window)
-               splash_window->operation->update(_("Initializing Fonts"));
+               splash_window->update_status(_("Initializing Fonts"));
        char string[BCTEXTLEN];
        strcpy(string, preferences->plugin_dir);
        strcat(string, "/" FONT_SEARCHPATH);