Last active
August 29, 2015 13:56
-
-
Save jvcleave/9141321 to your computer and use it in GitHub Desktop.
PipeReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "developApp.h" | |
void developApp::onCharacterReceived(ofxPipeListenerEventData& e) | |
{ | |
keyPressed((int)e.character); | |
} | |
//-------------------------------------------------------------- | |
void developApp::setup() | |
{ | |
pipeReader.start(this); | |
} | |
//-------------------------------------------------------------- | |
void developApp::update() | |
{ | |
} | |
//-------------------------------------------------------------- | |
void developApp::draw(){ | |
} | |
//-------------------------------------------------------------- | |
void developApp::keyPressed (int key){ | |
switch (key) | |
{ | |
default: | |
{ | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "PipeReader.h" | |
class developApp : public ofBaseApp, public ofxPipeListener{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed(int key); | |
//allows key commands via Shell via ofxPipeListener | |
PipeReader pipeReader; | |
void onCharacterReceived(ofxPipeListenerEventData& e); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
extern "C" | |
{ | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
}; | |
#define PIPE_BUFFER_SIZE 32 | |
class ofxPipeListenerEventData | |
{ | |
public: | |
ofxPipeListenerEventData(char character_) | |
{ | |
character = character_; | |
} | |
char character; | |
}; | |
class ofxPipeListener | |
{ | |
public: | |
virtual void onCharacterReceived(ofxPipeListenerEventData& e) = 0; | |
}; | |
class PipeReader : public ofThread{ | |
public: | |
string namedPipe; | |
int fd; | |
ofxPipeListener* listener; | |
PipeReader() | |
{ | |
listener = NULL; | |
} | |
void start(ofxPipeListener *listener_) | |
{ | |
listener = listener_; | |
namedPipe = ofToDataPath("ofpipe", true); | |
ofFile file(namedPipe, ofFile::ReadWrite, false); | |
if (!file.exists()) | |
{ | |
bool didCreate = file.create(); | |
if (didCreate) | |
{ | |
ofLogVerbose() << "file creation PASS " << namedPipe; | |
}else | |
{ | |
ofLogError() << "file creation FAIL " << namedPipe; | |
} | |
}else | |
{ | |
ofLogVerbose() << namedPipe << " EXISTS"; | |
} | |
ofLogVerbose() << "use command echo SOMEKEY > " << namedPipe << " in a second terminal to send key commands"; | |
ofLogVerbose() << "EXAMPLE: " << endl << "$echo p > " << namedPipe << endl << " will pause the player"; | |
mkfifo(namedPipe.c_str(), 0777); | |
startThread(true, false); | |
} | |
void stop() | |
{ | |
stopThread(); | |
} | |
void threadedFunction() | |
{ | |
while( isThreadRunning()) | |
{ | |
ofBuffer pipeContents = ofBufferFromFile(namedPipe, false); | |
char * content = pipeContents.getBinaryBuffer(); | |
if (content[0] != 0) | |
{ | |
ofxPipeListenerEventData eventData(content[0]); | |
listener->onCharacterReceived(eventData); | |
pipeContents.clear(); | |
ofBufferToFile(namedPipe, pipeContents, false); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment