reload plugin btn in prefs, speed gang fader tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 565f33283a705a29902585f131badf72817a3f0a..7d871c9652126768e7e3d1612aaaaaa23fcde9ec 100644 (file)
@@ -28,6 +28,7 @@
 #include "awindow.h"
 #include "batchrender.h"
 #include "bcdisplayinfo.h"
+#include "bcprogressbox.h"
 #include "bcsignals.h"
 #include "bctimer.h"
 #include "bctrace.h"
@@ -98,6 +99,7 @@
 #include "removefile.h"
 #include "render.h"
 #include "resourcethread.h"
+#include "savefile.inc"
 #include "samplescroll.h"
 #include "sha1.h"
 #include "sighandler.h"
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/mman.h>
+#include <sys/file.h>
 #include <limits.h>
 #include <errno.h>
 
@@ -523,12 +526,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);
@@ -576,7 +578,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, ".");
@@ -623,21 +624,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)
@@ -661,9 +677,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;
 }
@@ -1272,6 +1309,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];
@@ -1281,12 +1319,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();
        }
 }
 
@@ -1519,9 +1557,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);
 }
 
@@ -1805,7 +1841,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                char string[BCTEXTLEN];
 
                new_edl->create_objects();
-               new_edl->copy_session(edl);
+               new_edl->copy_session(edl, -1);
                new_file->set_program(edl->session->program_no);
 
                sprintf(string, _("Loading %s"), new_asset->path);
@@ -1970,7 +2006,8 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                                result = 1;
                                break;
                        }
-                       if( strcmp(cin_version, CINELERRA_VERSION) ) {
+                       if( strcmp(cin_version, CINELERRA_VERSION) &&
+                           strcmp(cin_version, "5.1") ) {
                                char string[BCTEXTLEN];
                                snprintf(string, sizeof(string),
                                         _("Warning: XML from cinelerra version %s\n"
@@ -2141,7 +2178,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                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);
@@ -2256,15 +2293,19 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
 
 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);
-               to_proxy(asset, new_scale);
+               ret = to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
+               if( ret > 0 )
+                       beep(2000., 1.5, 0.5);
        }
        return 1;
 }
@@ -2273,17 +2314,19 @@ 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, 1);
+               to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
        }
        return 1;
 }
 
-int MWindow::to_proxy(Asset *asset, int new_scale)
+int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
 {
        ArrayList<Indexable*> orig_idxbls;
        ArrayList<Indexable*> proxy_assets;
@@ -2296,7 +2339,6 @@ int MWindow::to_proxy(Asset *asset, int new_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;
-       int proxy_use_scaler = edl->session->proxy_use_scaler;
 
        if( proxy_scale > 1 ) {
                Asset *orig_asset = edl->assets->first;
@@ -2401,7 +2443,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale)
                result = proxy_render.create_needed_proxies(new_scale);
 
        if( !result ) // resize project
-               edl->set_proxy(new_scale, proxy_use_scaler,
+               edl->set_proxy(new_scale, new_use_scaler,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
 
        undo->update_undo_after(_("proxy"), LOAD_ALL);
@@ -2512,14 +2554,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();
 
@@ -2527,7 +2569,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);
@@ -2839,12 +2881,6 @@ void MWindow::toggle_loop_playback()
        sync_parameters(CHANGE_PARAMS);
 }
 
-//void MWindow::set_titles(int value)
-//{
-//     edl->session->show_titles = value;
-//     trackmovement(edl->local_session->track_start);
-//}
-
 void MWindow::set_screens(int value)
 {
        screens = value;
@@ -2914,6 +2950,19 @@ void MWindow::toggle_editing_mode()
                set_editing_mode(EDITING_ARROW, 0, 1);
 }
 
+void MWindow::toggle_camera_xyz()
+{
+       gwindow->gui->lock_window("MWindow::toggle_camera_xyz");
+       gwindow->gui->toggle_camera_xyz();
+       gwindow->gui->unlock_window();
+}
+
+void MWindow::toggle_projector_xyz()
+{
+       gwindow->gui->lock_window("MWindow::toggle_projector_xyz");
+       gwindow->gui->toggle_projector_xyz();
+       gwindow->gui->unlock_window();
+}
 
 void MWindow::set_labels_follow_edits(int value)
 {
@@ -3424,9 +3473,10 @@ void MWindow::update_project(int load_mode)
 
        if(debug) PRINT_TRACE
 
-       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;
                gui->load_panes();
        }
 
@@ -3590,6 +3640,8 @@ void MWindow::load_backup()
 
 void MWindow::save_undo_data()
 {
+       undo->update_undo_before();
+       undo->update_undo_after(_("perpetual session"), LOAD_ALL);
        char perpetual_path[BCTEXTLEN];
        snprintf(perpetual_path, sizeof(perpetual_path), "%s/%s",
                File::get_config_path(), PERPETUAL_FILE);
@@ -3610,6 +3662,187 @@ void MWindow::load_undo_data()
        fclose(fp);
 }
 
+
+int MWindow::copy_target(const char *path, const char *target)
+{
+       int ifd = ::open(path, O_RDONLY);
+       if( ifd < 0 ) {
+               eprintf("Cannot open asset: %s", path);
+               return 1;
+       }
+       int ret = 0;
+       int ofd = ::open(target, O_CREAT+O_TRUNC+O_WRONLY, 0777);
+       if( ofd >= 0 ) {
+               struct stat st;
+               int64_t total_bytes = !fstat(ifd, &st) ? st.st_size : 0;
+               char progress_title[BCTEXTLEN];
+               sprintf(progress_title, _("Copying: %s\n"), target);
+               BC_ProgressBox progress(-1, -1, progress_title, total_bytes);
+
+               int64_t count = 0, len = -1;
+               int bfrsz = 0x100000;
+               uint8_t *bfr = new uint8_t[bfrsz];
+               while( (len=::read(ifd, bfr, bfrsz)) > 0 ) {
+                       if( len != ::write(ofd, bfr, len) ) {
+                               eprintf("Error writing: %s", target);
+                               break;
+                       }
+                       if( progress.is_cancelled() ) {
+                               ret = 1;
+                               break;
+                       }
+                       progress.update(count += len, 1);
+               }
+               delete [] bfr;
+               ::close(ofd);
+
+               progress.stop_progress();
+               if( len < 0 ) {
+                       eprintf("Error reading: %s", path);
+                       ret = 1;
+               }
+       }
+       else
+               eprintf("Cannot create asset target: %s", target);
+       ::close(ifd);
+       return ret;
+}
+
+int MWindow::link_target(const char *real_path, const char *link_path, int relative)
+{
+       char target[BCTEXTLEN];
+       if( relative ) {
+               const char *bp = real_path, *cp = bp;
+               const char *lp = link_path, *np = lp;
+               char *tp = target, *ep = tp+sizeof(target)-1, lch;
+               while( *lp && *bp && (lch=*lp++) == *bp++ ) {
+                       if( lch == '/' ) { np = lp;  cp = bp; }
+               }
+               while( tp<ep && *np ) {
+                       if( *np++ != '/' ) continue;
+                       *tp++ = '.';  *tp++ = '.';  *tp++ = '/';
+               }
+               while( tp<ep && *cp ) *tp++ = *cp++;
+               *tp = 0;
+       }
+       else
+               strcpy(target, real_path);
+       if( symlink(target, link_path) ) {
+               eprintf("Cannot create symlink: %s", link_path);
+               return 1;
+       }
+       return 0;
+}
+
+void MWindow::save_project(const char *dir, int save_mode, int overwrite, int reload)
+{
+       char dir_path[BCTEXTLEN];
+       strcpy(dir_path, dir);
+       FileSystem fs;
+       fs.complete_path(dir_path);
+
+       struct stat st;
+       if( !stat(dir_path, &st) ) {
+               if( !S_ISDIR(st.st_mode) ) {
+                       eprintf("Path exists and is not a directory\n%s", dir_path);
+                       return;
+               }
+       }
+       else {
+               if( mkdir(dir_path, S_IRWXU | S_IRWXG | S_IRWXO) ) {
+                       eprintf("Cannot create directory\n%s", dir_path);
+                       return;
+               }
+       }
+       char *real_dir = realpath(dir_path, 0);
+       strcpy(dir_path, real_dir);
+       free(real_dir);
+
+       EDL *save_edl = new EDL;
+       save_edl->create_objects();
+       save_edl->copy_all(edl);
+
+       char progress_title[BCTEXTLEN];
+       sprintf(progress_title, _("Saving to %s:\n"), dir);
+       int total_assets = save_edl->assets->total();
+       MainProgressBar *progress = mainprogress->start_progress(progress_title, total_assets);
+       int ret = 0;
+       Asset *current = save_edl->assets->first;
+       for( int i=0; !ret && current; ++i, current=NEXT ) {
+               char *path = current->path;
+               if( ::stat(path, &st) ) {
+                       eprintf("Asset not found: %s", path);
+                       continue;
+               }
+               char *real_path = realpath(path, 0);
+               const char *cp = strrchr(path, '/'), *bp = !cp ? path : cp+1;
+               char link_path[BCTEXTLEN];
+               snprintf(link_path, sizeof(link_path), "%s/%s", dir_path, bp);
+               int skip = 0;
+               if( strcmp(real_path, link_path) ) {
+                       if( !::lstat(link_path, &st) ) {
+                               if( overwrite )
+                                       ::remove(link_path);
+                               else
+                                       skip = 1;
+                       }
+               }
+               else {
+                       eprintf("copy/link to self, skippped: %s", path);
+                       skip = 1;
+               }
+               if( !skip ) {
+                       if( save_mode == SAVE_PROJECT_COPY ) {
+                               if( copy_target(real_path, link_path) )
+                                       ret = 1;
+                       }
+                       else {
+                               link_target(real_path, link_path,
+                                       save_mode == SAVE_PROJECT_RELLINK ? 1 : 0);
+                       }
+               }
+               free(real_path);
+               strcpy(path, link_path);
+
+               if( progress->is_cancelled() ) break;
+               progress->update(i);
+       }
+
+       progress->stop_progress();
+       delete progress;
+
+       char *cp = strrchr(dir_path,'/');
+       char *bp = cp ? cp+1 : dir_path;
+       char filename[BCTEXTLEN];
+       snprintf(filename, sizeof(filename), "%s/%s.xml", dir_path, bp);
+       save_edl->set_path(filename);
+       FileXML file;
+       save_edl->save_xml(&file, filename);
+       file.terminate_string();
+
+       if( !file.write_to_file(filename) ) {
+               char string[BCTEXTLEN];
+               sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
+               gui->lock_window("SaveProject::run 2");
+               gui->show_message(string);
+               gui->unlock_window();
+               gui->mainmenu->add_load(filename);
+       }
+       else
+               eprintf(_("Couldn't open %s."), filename);
+
+       save_edl->remove_user();
+
+       if( reload ) {
+               gui->lock_window("MWindow::save_project");
+               ArrayList<char*> filenames;
+               filenames.append(filename);
+               load_filenames(&filenames, LOADMODE_REPLACE);
+               gui->unlock_window();
+       }
+}
+
+
 static inline int gcd(int m, int n)
 {
        int r;