@@ -59,6 +59,7 @@ BashScript::BashScript() : PatchObject("bash script"){
5959
6060 lastMessage = " " ;
6161
62+ threadLoaded = false ;
6263 needToLoadScript = true ;
6364
6465 loadScriptFlag = false ;
@@ -79,11 +80,26 @@ void BashScript::newObject(){
7980
8081// --------------------------------------------------------------
8182void BashScript::autoloadFile (string _fp){
82- // filepath = _fp ;
83+ threadLoaded = false ;
8384 filepath = copyFileToPatchFolder (this ->patchFolderPath ,_fp);
8485 reloadScript ();
8586}
8687
88+ // --------------------------------------------------------------
89+ void BashScript::threadedFunction (){
90+ while (isThreadRunning ()){
91+ std::unique_lock<std::mutex> lock (mutex);
92+ if (needToLoadScript){
93+ needToLoadScript = false ;
94+ loadScript (filepath);
95+ threadLoaded = true ;
96+ }
97+ condition.wait (lock);
98+ sleep (10 );
99+ }
100+
101+ }
102+
87103// --------------------------------------------------------------
88104void BashScript::setupObjectContent (shared_ptr<ofAppGLFWWindow> &mainWindow){
89105 // GUI
@@ -99,6 +115,10 @@ void BashScript::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){
99115 filepath = copyFileToPatchFolder (this ->patchFolderPath ,file.getAbsolutePath ());
100116 }
101117
118+ if (!isThreadRunning ()){
119+ startThread ();
120+ }
121+
102122}
103123
104124// --------------------------------------------------------------
@@ -115,15 +135,18 @@ void BashScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObje
115135 }
116136 }
117137
118- if (needToLoadScript){
138+ /* if(needToLoadScript){
119139 needToLoadScript = false;
120140 loadScript(filepath);
121- }
141+ }*/
122142
123143 // path watcher
124144 while (watcher.waitingEvents ()) {
125145 pathChanged (watcher.nextEvent ());
126146 }
147+
148+ condition.notify_all ();
149+
127150}
128151
129152// --------------------------------------------------------------
@@ -174,15 +197,16 @@ void BashScript::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){
174197 if (ImGuiEx::getFileDialog (fileDialog, saveScriptFlag, " Save new bash script as" , imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, " .sh" , newFileName, scaleFactor)){
175198 ofFile fileToRead (ofToDataPath (" scripts/empty.sh" ));
176199 ofFile newBashFile (fileDialog.selected_path );
177-
178200 ofFile::copyFromTo (fileToRead.getAbsolutePath (),checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ),true ,true );
179201 filepath = copyFileToPatchFolder (this ->patchFolderPath ,checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ));
202+ threadLoaded = false ;
180203 reloadScript ();
181204 }
182205
183206 if (ImGuiEx::getFileDialog (fileDialog, loadScriptFlag, " Select a bash script" , imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, " .sh" , " " , scaleFactor)){
184207 ofFile bashFile (fileDialog.selected_path );
185208 filepath = copyFileToPatchFolder (this ->patchFolderPath ,bashFile.getAbsolutePath ());
209+ threadLoaded = false ;
186210 reloadScript ();
187211 }
188212
@@ -223,6 +247,10 @@ void BashScript::removeObjectContent(bool removeFileFromData){
223247 if (filepath != ofToDataPath (" scripts/empty.sh" ,true ) && removeFileFromData){
224248 removeFile (filepath);
225249 }
250+
251+ std::unique_lock<std::mutex> lck (mutex);
252+ stopThread ();
253+ condition.notify_all ();
226254}
227255
228256
@@ -234,16 +262,9 @@ void BashScript::loadScript(string scriptFile){
234262
235263 string cmd = " " ;
236264 FILE *execFile;
237- #ifdef TARGET_LINUX
238- cmd = " sh " +filepath;
239- execFile = popen (cmd.c_str (), " r" );
240- #elif defined(TARGET_OSX)
265+
241266 cmd = " sh " +filepath;
242267 execFile = popen (cmd.c_str (), " r" );
243- #elif defined(TARGET_WIN32)
244- cmd = filepath;
245- execFile = _popen (cmd.c_str (), " r" );
246- #endif
247268
248269 if (execFile){
249270 scriptLoaded = true ;
@@ -269,13 +290,7 @@ void BashScript::loadScript(string scriptFile){
269290
270291 this ->saveConfig (false );
271292
272- #ifdef TARGET_LINUX
273293 pclose (execFile);
274- #elif defined(TARGET_OSX)
275- pclose (execFile);
276- #elif defined(TARGET_WIN32)
277- _pclose (execFile);
278- #endif
279294
280295 }
281296
0 commit comments