Skip to content

Commit

Permalink
replaced ConsoleListener with new TerminalListener
Browse files Browse the repository at this point in the history
  • Loading branch information
jvcleave committed Jan 9, 2015
1 parent 54a16d8 commit 2d71f91
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example-pixels/src/pixelsApp.cpp
Expand Up @@ -69,7 +69,7 @@ void pixelsApp::keyPressed (int key)
}
}

void pixelsApp::onCharacterReceived(SSHKeyListenerEventData& e)
void pixelsApp::onCharacterReceived(KeyListenerEventData& e)
{
keyPressed((int)e.character);
}
8 changes: 4 additions & 4 deletions example-pixels/src/pixelsApp.h
Expand Up @@ -3,8 +3,8 @@
#include "ofMain.h"
#include "ofxOMXPlayer.h"

#include "ConsoleListener.h"
class pixelsApp : public ofBaseApp, public SSHKeyListener{
#include "TerminalListener.h"
class pixelsApp : public ofBaseApp, public KeyListener{

public:

Expand All @@ -17,8 +17,8 @@ class pixelsApp : public ofBaseApp, public SSHKeyListener{
ofxOMXPlayer omxPlayer;
bool doSaveImage;

ConsoleListener consoleListener;
void onCharacterReceived(SSHKeyListenerEventData& e);
TerminalListener consoleListener;
void onCharacterReceived(KeyListenerEventData& e);

ofTexture pixelOutput;
bool doUpdatePixels;
Expand Down
2 changes: 1 addition & 1 deletion example-playlist/src/playlistApp.cpp
Expand Up @@ -20,7 +20,7 @@ void playlistApp::onVideoEnd(ofxOMXPlayerListenerEventData& e)
}


void playlistApp::onCharacterReceived(SSHKeyListenerEventData& e)
void playlistApp::onCharacterReceived(KeyListenerEventData& e)
{
keyPressed((int)e.character);
}
Expand Down
8 changes: 4 additions & 4 deletions example-playlist/src/playlistApp.h
Expand Up @@ -2,10 +2,10 @@

#include "ofMain.h"
#include "ofxOMXPlayer.h"
#include "ConsoleListener.h"
#include "TerminalListener.h"


class playlistApp : public ofBaseApp, public ofxOMXPlayerListener, public SSHKeyListener{
class playlistApp : public ofBaseApp, public ofxOMXPlayerListener, public KeyListener{

public:

Expand All @@ -24,8 +24,8 @@ class playlistApp : public ofBaseApp, public ofxOMXPlayerListener, public SSHKey
vector<ofFile> files;
int videoCounter;

void onCharacterReceived(SSHKeyListenerEventData& e);
ConsoleListener consoleListener;
void onCharacterReceived(KeyListenerEventData& e);
TerminalListener consoleListener;
ofxOMXPlayerSettings settings;

void loadNextMovie();
Expand Down
2 changes: 1 addition & 1 deletion example-restartMovie/src/testApp.cpp
Expand Up @@ -81,7 +81,7 @@ void testApp::keyPressed(int key)
}
}

void testApp::onCharacterReceived(SSHKeyListenerEventData& e)
void testApp::onCharacterReceived(KeyListenerEventData& e)
{
keyPressed((int)e.character);
}
8 changes: 4 additions & 4 deletions example-restartMovie/src/testApp.h
Expand Up @@ -2,9 +2,9 @@

#include "ofMain.h"
#include "ofxOMXPlayer.h"
#include "ConsoleListener.h"
#include "TerminalListener.h"

class testApp : public ofBaseApp, public SSHKeyListener{
class testApp : public ofBaseApp, public KeyListener{

public:

Expand All @@ -14,7 +14,7 @@ class testApp : public ofBaseApp, public SSHKeyListener{
void keyPressed(int key);
ofxOMXPlayer omxPlayer;

void onCharacterReceived(SSHKeyListenerEventData& e);
ConsoleListener consoleListener;
void onCharacterReceived(KeyListenerEventData& e);
TerminalListener consoleListener;
};

3 changes: 1 addition & 2 deletions example-shader/src/shaderApp.cpp
@@ -1,9 +1,8 @@
#include "shaderApp.h"


void shaderApp::onCharacterReceived(SSHKeyListenerEventData& e)
void shaderApp::onCharacterReceived(KeyListenerEventData& e)
{
//ConsoleListener* thread = (ConsoleListener*) e.listener;
keyPressed((int)e.character);
}

Expand Down
8 changes: 4 additions & 4 deletions example-shader/src/shaderApp.h
Expand Up @@ -3,9 +3,9 @@
#include "ofMain.h"
#include "ofxOMXPlayer.h"

#include "ConsoleListener.h"
#include "TerminalListener.h"

class shaderApp : public ofBaseApp, public SSHKeyListener, public ofxOMXPlayerListener{
class shaderApp : public ofBaseApp, public KeyListener, public ofxOMXPlayerListener{

public:

Expand All @@ -25,8 +25,8 @@ class shaderApp : public ofBaseApp, public SSHKeyListener, public ofxOMXPlayerLi


//allows key commands via Shell
void onCharacterReceived(SSHKeyListenerEventData& e);
ConsoleListener consoleListener;
void onCharacterReceived(KeyListenerEventData& e);
TerminalListener consoleListener;

void onVideoEnd(ofxOMXPlayerListenerEventData& e);
void onVideoLoop(ofxOMXPlayerListenerEventData& e);
Expand Down
103 changes: 103 additions & 0 deletions src/TerminalListener.h
@@ -0,0 +1,103 @@
#pragma once

#include "ofMain.h"
#include <fcntl.h>

class KeyListenerEventData
{
public:
KeyListenerEventData(char character, void* listener)
{
this->character = character;
this->listener = listener;
}
void* listener;

char character;
};

class KeyListener
{
public:
virtual void onCharacterReceived(KeyListenerEventData& e) = 0;
};


class TerminalListener : public Poco::Runnable
{
private:
struct termios orig_termios;
public:
KeyListener* listener;
Poco::Thread thread;
TerminalListener()
{

listener = NULL;

}


void setup(KeyListener* listener_)
{
listener = listener_;
struct termios new_termios;

tcgetattr(STDIN_FILENO, &orig_termios);

new_termios = orig_termios;
new_termios.c_lflag &= ~(ICANON | ECHO | ECHOCTL | ECHONL);
new_termios.c_cflag |= HUPCL;
new_termios.c_cc[VMIN] = 0;

tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
thread.start(*this);
}

void run()
{

while (thread.isRunning())
{
if (listener != NULL)
{
int ch[8];
int chnum = 0;

while ((ch[chnum] = getchar()) != EOF)
{
chnum++;
}

if (chnum > 1)
{
ch[0] = ch[chnum - 1];
}else
{
(ch[chnum - 2] << 8);
}

if (chnum > 0)
{
//ofLog(OF_LOG_VERBOSE, "TerminalListener: character %c", ch[0]);
KeyListenerEventData eventData(ch[0], (void *)this);
listener->onCharacterReceived(eventData);
}
}
}
}

void close()
{
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
thread.tryJoin(50);
listener = NULL;
}

~TerminalListener()
{
close();
//ofLogVerbose(__func__) << " END";
}

};

0 comments on commit 2d71f91

Please sign in to comment.