8 changed files with 1 additions and 192 deletions
@ -1,74 +0,0 @@ |
|||||
#include "TerminalListener.h" |
|
||||
|
|
||||
namespace ofx { |
|
||||
namespace piMapper { |
|
||||
|
|
||||
KeyListenerEventData::KeyListenerEventData(char character, void * listener){ |
|
||||
this->character = character; |
|
||||
this->listener = listener; |
|
||||
} |
|
||||
|
|
||||
TerminalListener::TerminalListener(){ |
|
||||
sleepTime = 100; |
|
||||
listener = 0; |
|
||||
} |
|
||||
|
|
||||
TerminalListener::~TerminalListener(){ |
|
||||
close(); |
|
||||
} |
|
||||
|
|
||||
void TerminalListener::setup(KeyListener * listener_, int sleepTime_){ |
|
||||
listener = listener_; |
|
||||
|
|
||||
if(sleepTime_ > 0){ |
|
||||
sleepTime = sleepTime_; |
|
||||
} |
|
||||
|
|
||||
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 TerminalListener::run(){ |
|
||||
while (thread.isRunning()){ |
|
||||
if (listener != 0){ |
|
||||
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); |
|
||||
} |
|
||||
|
|
||||
thread.sleep(sleepTime); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void TerminalListener::close(){ |
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); |
|
||||
thread.tryJoin(50); |
|
||||
listener = 0; |
|
||||
} |
|
||||
|
|
||||
} // namespace piMapper
|
|
||||
} // namespace ofx
|
|
@ -1,47 +0,0 @@ |
|||||
/*
|
|
||||
* TerminalListener |
|
||||
* Helper for capturing keyboard input when via SSH |
|
||||
* Author: Jason Van Cleave / https://github.com/jvcleave
|
|
||||
* Adaptor: Krisjanis Rijnieks / https://github.com/kr15h
|
|
||||
*/ |
|
||||
|
|
||||
#pragma once |
|
||||
|
|
||||
#include "ofMain.h" |
|
||||
#include <fcntl.h> |
|
||||
|
|
||||
namespace ofx { |
|
||||
namespace piMapper { |
|
||||
|
|
||||
class KeyListenerEventData { |
|
||||
public: |
|
||||
KeyListenerEventData(char character, void * 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: |
|
||||
TerminalListener(); |
|
||||
~TerminalListener(); |
|
||||
|
|
||||
void setup(KeyListener * listener_, int sleepTime_ = 0); |
|
||||
void run(); |
|
||||
void close(); |
|
||||
|
|
||||
KeyListener * listener; |
|
||||
Poco::Thread thread; |
|
||||
int sleepTime; |
|
||||
}; |
|
||||
|
|
||||
} // namespace piMapper
|
|
||||
} // namespace ofx
|
|
Loading…
Reference in new issue