Skip to content

Commit 62a52f6

Browse files
committed
make bash script object non blocking with ofThread
1 parent 1aae195 commit 62a52f6

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/objects/scripting/BashScript.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
//--------------------------------------------------------------
8182
void 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
//--------------------------------------------------------------
88104
void 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

src/objects/scripting/BashScript.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@
4040
#include "ImGuiFileBrowser.h"
4141
#include "IconsFontAwesome5.h"
4242

43+
#include <atomic>
4344

44-
class BashScript : public PatchObject{
45+
class BashScript : public ofThread, public PatchObject{
4546

4647
public:
4748

4849
BashScript();
4950

51+
void threadedFunction() override;
52+
5053
void autoloadFile(string _fp) override;
5154
void newObject() override;
5255
void setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow) override;
@@ -81,8 +84,9 @@ class BashScript : public PatchObject{
8184
float canvasZoom;
8285

8386
protected:
84-
87+
std::condition_variable condition;
8588
bool needToLoadScript;
89+
bool threadLoaded;
8690
bool loadScriptFlag;
8791
bool saveScriptFlag;
8892

0 commit comments

Comments
 (0)