From 72f59dcffe5f741facb30827c798fb1999ae9b9b Mon Sep 17 00:00:00 2001
From: Andrew Randrianasulu <randrianasulu@gmail.com>
Date: Mon, 11 Oct 2021 12:04:24 +0300
Subject: [PATCH 5/6] WIP: reconnect/fix local plugin dir setting

---
 cinelerra-5.1/cinelerra/Makefile            |  1 +
 cinelerra-5.1/cinelerra/mwindow.C           | 51 +++++++++++++++++++++
 cinelerra-5.1/cinelerra/mwindow.inc         |  1 +
 cinelerra-5.1/cinelerra/pluginprefs.C       | 19 ++++----
 cinelerra-5.1/cinelerra/pluginprefs.h       |  1 +
 cinelerra-5.1/cinelerra/preferences.C       |  5 ++
 cinelerra-5.1/cinelerra/preferences.h       |  1 +
 cinelerra-5.1/cinelerra/preferences.inc     |  1 +
 cinelerra-5.1/cinelerra/preferencesthread.C |  7 +++
 cinelerra-5.1/cinelerra/preferencesthread.h |  3 +-
 10 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/cinelerra-5.1/cinelerra/Makefile b/cinelerra-5.1/cinelerra/Makefile
index c660ee37..cd4df41b 100644
--- a/cinelerra-5.1/cinelerra/Makefile
+++ b/cinelerra-5.1/cinelerra/Makefile
@@ -252,6 +252,7 @@ OBJS := $(OVERLAYS) \
 	$(OBJDIR)/pluginlv2client.o \
 	$(OBJDIR)/pluginlv2gui.o \
 	$(OBJDIR)/plugin.o \
+	$(OBJDIR)/pluginprefs.o \
 	$(OBJDIR)/pluginpopup.o \
 	$(OBJDIR)/pluginserver.o \
 	$(OBJDIR)/pluginset.o \
diff --git a/cinelerra-5.1/cinelerra/mwindow.C b/cinelerra-5.1/cinelerra/mwindow.C
index b0e064ed..1bcab625 100644
--- a/cinelerra-5.1/cinelerra/mwindow.C
+++ b/cinelerra-5.1/cinelerra/mwindow.C
@@ -653,15 +653,29 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
 		plugindb = new ArrayList<PluginServer*>;
 	init_ffmpeg();
 	char index_path[BCTEXTLEN], plugin_path[BCTEXTLEN];
+	char index_path1[BCTEXTLEN], local_plugin_path[BCTEXTLEN];
 	create_defaults_path(index_path, PLUGIN_FILE);
+	create_defaults_path(index_path1, LOCAL_PLUGIN_FILE);
 	char *plugin_dir = FileSystem::basepath(preferences->plugin_dir);
 	strcpy(plugin_path, plugin_dir);  delete [] plugin_dir;
+	char *local_plugin_dir = FileSystem::basepath(preferences->local_plugin_dir);
+	strcpy(local_plugin_path, local_plugin_dir);  delete [] local_plugin_dir;
+
 	FILE *fp = fopen(index_path,"a+");
 	if( !fp ) {
 		fprintf(stderr,_("MWindow::init_plugins: "
 			"can't open plugin index: %s\n"), index_path);
 		return -1;
 	}
+	
+	FILE *fp1 = fopen(index_path1,"a+");
+	if( !fp1 ) {
+		fprintf(stderr,_("MWindow::init_plugins: "
+			"can't open plugin index: %s\n"), index_path1);
+		return -1;
+	}
+	
+	
 	int fd = fileno(fp), ret = -1;
 	if( !flock(fd, LOCK_EX) ) {
 		fseek(fp, 0, SEEK_SET);
@@ -670,12 +684,16 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
 	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);
 	}
@@ -684,6 +702,36 @@ int MWindow::init_plugins(MWindow *mwindow, Preferences *preferences)
 			"can't %s plugin index: %s\n"),
 			ret>0 ? _("create") : _("lock"), index_path);
 	}
+	
+	
+	int fd1 = fileno(fp1), ret1 = -1;
+	if( !flock(fd1, LOCK_EX) ) {
+		fseek(fp1, 0, SEEK_SET);
+		ret1 = load_plugin_index(mwindow, fp1, plugin_path);
+	}
+	if( ret1 > 0 ) {
+		ftruncate(fd1, 0);
+		fseek(fp1, 0, SEEK_SET);
+		
+		
+		printf("init plugin index: %s\n", local_plugin_path);
+		fprintf(fp1, "%d\n", PLUGIN_FILE_VERSION);
+		fprintf(fp1, "%s\n", local_plugin_path);
+		init_plugin_index(mwindow, preferences, fp1, local_plugin_path);
+		
+		fseek(fp1, 0, SEEK_SET);
+		ret1 = load_plugin_index(mwindow, fp1, local_plugin_path);
+	}
+	if( ret1 ) {
+		fprintf(stderr,_("MWindow::init_plugins: "
+			"can't %s plugin index: %s\n"),
+			ret>0 ? _("create") : _("lock"), index_path1);
+	}
+	
+	
+	
+	
+	fclose(fp1);
 	fclose(fp);
 	return ret;
 }
@@ -961,6 +1009,9 @@ void MWindow::remove_plugin_index()
 	char index_path[BCTEXTLEN];
 	MWindow::create_defaults_path(index_path, PLUGIN_FILE);
 	::remove(index_path);
+	MWindow::create_defaults_path(index_path, LOCAL_PLUGIN_FILE);
+	::remove(index_path);
+	
 }
 
 void MWindow::init_preferences()
diff --git a/cinelerra-5.1/cinelerra/mwindow.inc b/cinelerra-5.1/cinelerra/mwindow.inc
index f1847175..409f4d00 100644
--- a/cinelerra-5.1/cinelerra/mwindow.inc
+++ b/cinelerra-5.1/cinelerra/mwindow.inc
@@ -35,6 +35,7 @@
 #define FACTORY_FILE "Cinelerra_factory"
 #define PICTURE_FILE "Cinelerra_picture"
 #define PLUGIN_FILE "Cinelerra_plugins"
+#define LOCAL_PLUGIN_FILE "Cinelerra_local_plugins"
 #define LADSPA_FILE "ladspa_plugins."
 #define PLUGIN_FILE_VERSION 4
 
diff --git a/cinelerra-5.1/cinelerra/pluginprefs.C b/cinelerra-5.1/cinelerra/pluginprefs.C
index 2a3f7878..9bb52423 100644
--- a/cinelerra-5.1/cinelerra/pluginprefs.C
+++ b/cinelerra-5.1/cinelerra/pluginprefs.C
@@ -19,6 +19,7 @@
  *
  */
 
+#include "mwindow.h"
 #include "pluginprefs.h"
 #include "preferences.h"
 #include <string.h>
@@ -45,7 +46,7 @@ PluginPrefs::~PluginPrefs()
 
 void PluginPrefs::create_objects()
 {
-	char string[1024];
+	char string[BCTEXTLEN];
 	int xs5 = xS(5), xs10 = xS(10), xs20 = xS(20), xs30 = xS(30);
 	int ys5 = yS(5), ys10 = yS(10), ys20 = yS(20), ys30 = yS(30);
 	int x = xs5, y = ys5;
@@ -57,13 +58,13 @@ void PluginPrefs::create_objects()
 // 		get_resources()->get_bg_light1());
 
 	add_subwindow(new BC_Title(x, y, _("Plugin Set"), LARGEFONT, BLACK));
-	y += ys35;
+	y += ys30;
 	add_subwindow(new BC_Title(x, y, _("Look for global plugins here"), MEDIUMFONT, BLACK));
 	y += ys20;
 	add_subwindow(ipathtext = new PluginGlobalPathText(x, y,
-			pwindow, pwindow->thread->preferences->global_plugin_dir));
-	add_subwindow(ipath = new BrowseButton(mwindow, this, ipathtext,
-		xS(215), y, pwindow->thread->preferences->global_plugin_dir,
+			pwindow, pwindow->thread->preferences->plugin_dir));
+	add_subwindow(ipath = new BrowseButton(mwindow->theme, this, ipathtext,
+		xS(215), y, pwindow->thread->preferences->plugin_dir,
 		_("Global Plugin Path"), _("Select the directory for plugins"),
 		1));
 
@@ -72,7 +73,7 @@ void PluginPrefs::create_objects()
 	y += 20;
 	add_subwindow(lpathtext = new PluginLocalPathText(x, y,
 			pwindow, pwindow->thread->preferences->local_plugin_dir));
-	add_subwindow(lpath = new BrowseButton(mwindow, this, lpathtext,
+	add_subwindow(lpath = new BrowseButton(mwindow->theme, this, lpathtext,
 		xS(215), y, pwindow->thread->preferences->local_plugin_dir,
 		_("Personal Plugin Path"), _("Select the directory for plugins"),
 		1));
@@ -92,7 +93,8 @@ PluginGlobalPathText::~PluginGlobalPathText() {}
 
 int PluginGlobalPathText::handle_event()
 {
-	strcpy(pwindow->thread->preferences->global_plugin_dir, get_text());
+	strcpy(pwindow->thread->preferences->plugin_dir, get_text());
+	return 1;
 }
 
 
@@ -100,7 +102,7 @@ int PluginGlobalPathText::handle_event()
 
 
 PluginLocalPathText::PluginLocalPathText(int x, int y, PreferencesWindow *pwindow, char *text)
- : BC_TextBox(x, y, xs(200), 1, text)
+ : BC_TextBox(x, y, xS(200), 1, text)
 {
 	this->pwindow = pwindow;
 }
@@ -110,4 +112,5 @@ PluginLocalPathText::~PluginLocalPathText() {}
 int PluginLocalPathText::handle_event()
 {
 	strcpy(pwindow->thread->preferences->local_plugin_dir, get_text());
+	return 1;
 }
diff --git a/cinelerra-5.1/cinelerra/pluginprefs.h b/cinelerra-5.1/cinelerra/pluginprefs.h
index a6f46032..6cb96787 100644
--- a/cinelerra-5.1/cinelerra/pluginprefs.h
+++ b/cinelerra-5.1/cinelerra/pluginprefs.h
@@ -28,6 +28,7 @@ class PluginLocalPathText;
 #include "browsebutton.h"
 #include "preferencesthread.h"
 
+
 class PluginPrefs : public PreferencesDialog
 {
 public:
diff --git a/cinelerra-5.1/cinelerra/preferences.C b/cinelerra-5.1/cinelerra/preferences.C
index 2b8d84bd..d2abdcc1 100644
--- a/cinelerra-5.1/cinelerra/preferences.C
+++ b/cinelerra-5.1/cinelerra/preferences.C
@@ -66,6 +66,7 @@ Preferences::Preferences()
 	keyframe_reticle = HAIRLINE_DRAGGING;
 	perpetual_session = 0;
 	strcpy(lv2_path, DEFAULT_LV2_PATH);
+	strcpy(local_plugin_dir, DEFAULT_LOCAL_PLUG_PATH);
 	strcpy(nested_proxy_path, DEFAULT_NESTED_PROXY_PATH);
 	autostart_lv2ui = 0;
 	trap_sigsegv = 1;
@@ -219,6 +220,7 @@ void Preferences::copy_from(Preferences *that)
 	project_smp = that->project_smp;
 	force_uniprocessor = that->force_uniprocessor;
 	strcpy(lv2_path, that->lv2_path);
+	strcpy(local_plugin_dir, that->local_plugin_dir);
 	autostart_lv2ui = that->autostart_lv2ui;
 	strcpy(nested_proxy_path, that->nested_proxy_path);
 	trap_sigsegv = that->trap_sigsegv;
@@ -350,6 +352,8 @@ int Preferences::load_defaults(BC_Hash *defaults)
 	perpetual_session = defaults->get("PERPETUAL_SESSION", perpetual_session);
 	strcpy(lv2_path, DEFAULT_LV2_PATH);
 	defaults->get("LV2_PATH", lv2_path);
+	strcpy(local_plugin_dir, DEFAULT_LOCAL_PLUG_PATH);
+	defaults->get("LOCAL_PLUGIN_DIR", local_plugin_dir);
 	autostart_lv2ui = defaults->get("AUTOSTART_LV2UI", autostart_lv2ui);
 	strcpy(nested_proxy_path, DEFAULT_NESTED_PROXY_PATH);
 	defaults->get("NESTED_PROXY_PATH", nested_proxy_path);
@@ -515,6 +519,7 @@ int Preferences::save_defaults(BC_Hash *defaults)
 	defaults->update("KEYFRAME_RETICLE", keyframe_reticle);
 	defaults->update("PERPETUAL_SESSION", perpetual_session);
 	defaults->update("LV2_PATH", lv2_path);
+	defaults->update("LOCAL_PLUGIN_DIR", local_plugin_dir);
 	defaults->update("AUTOSTART_LV2UI", autostart_lv2ui);
 	defaults->update("NESTED_PROXY_PATH", nested_proxy_path);
 	defaults->update("TRAP_SIGSEGV", trap_sigsegv);
diff --git a/cinelerra-5.1/cinelerra/preferences.h b/cinelerra-5.1/cinelerra/preferences.h
index 4f743975..d41865cb 100644
--- a/cinelerra-5.1/cinelerra/preferences.h
+++ b/cinelerra-5.1/cinelerra/preferences.h
@@ -199,6 +199,7 @@ public:
 
 // ====================================== Plugin Set ==============================
 	char plugin_dir[BCTEXTLEN];
+	char local_plugin_dir[BCTEXTLEN];
 	char lv2_path[BCTEXTLEN];
 	int autostart_lv2ui;
 	char nested_proxy_path[BCTEXTLEN];
diff --git a/cinelerra-5.1/cinelerra/preferences.inc b/cinelerra-5.1/cinelerra/preferences.inc
index f941c1b8..9c6cd982 100644
--- a/cinelerra-5.1/cinelerra/preferences.inc
+++ b/cinelerra-5.1/cinelerra/preferences.inc
@@ -33,6 +33,7 @@
 #define DEFAULT_LOCALE "sys"
 #define LOCALE_LIST DEFAULT_LOCALE, "en", "fr", "de", "ru", "es", "it", "nb"
 #define DEFAULT_LV2_PATH CINDAT_DIR "/lv2"
+#define DEFAULT_LOCAL_PLUG_PATH CINDAT_DIR "cingg"
 #define DEFAULT_NESTED_PROXY_PATH NESTED_DIR
 #define DEFAULT_SNAPSHOT_PATH SNAP_DIR
 #define FFMPEG_EARLY_TIP _("Currently: Try FFMpeg first\n   Click to: Try FFMpeg last")
diff --git a/cinelerra-5.1/cinelerra/preferencesthread.C b/cinelerra-5.1/cinelerra/preferencesthread.C
index 3ca281e5..95691aa7 100644
--- a/cinelerra-5.1/cinelerra/preferencesthread.C
+++ b/cinelerra-5.1/cinelerra/preferencesthread.C
@@ -51,6 +51,7 @@
 #include "performanceprefs.h"
 #include "playbackengine.h"
 #include "playbackprefs.h"
+#include "pluginprefs.h"
 #include "preferences.h"
 #include "record.h"
 #include "recordprefs.h"
@@ -407,6 +408,8 @@ const char* PreferencesThread::category_to_text(int category)
 			return _("Interface");
 		case APPEARANCE:
 			return _("Appearance");
+		case PLUGINPREF:
+			return _("Plugin set");
 		case ABOUT:
 			return _("About");
 	}
@@ -585,6 +588,10 @@ int PreferencesWindow::set_current_dialog(int number)
 		case PreferencesThread::APPEARANCE:
 			add_subwindow(dialog = new AppearancePrefs(mwindow, this));
 			break;
+			
+		case PreferencesThread::PLUGINPREF:
+			add_subwindow(dialog = new PluginPrefs(mwindow, this));
+			break;
 
 		case PreferencesThread::ABOUT:
 			add_subwindow(dialog = new AboutPrefs(mwindow, this));
diff --git a/cinelerra-5.1/cinelerra/preferencesthread.h b/cinelerra-5.1/cinelerra/preferencesthread.h
index 653199b6..4070b8df 100644
--- a/cinelerra-5.1/cinelerra/preferencesthread.h
+++ b/cinelerra-5.1/cinelerra/preferencesthread.h
@@ -79,7 +79,7 @@ public:
 	EDL *edl;
 
 // Categories
-#define CATEGORIES 7
+#define CATEGORIES 8
 	enum
 	{
 		PLAYBACK_A,
@@ -88,6 +88,7 @@ public:
 		PERFORMANCE,
 		INTERFACE,
 		APPEARANCE,
+		PLUGINPREF,
 		ABOUT
 	};
 };
-- 
2.33.0

