1 |
schoenebeck |
2515 |
/* |
2 |
|
|
* LSCP Shell |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2014 Christian Schoenebeck |
5 |
|
|
* |
6 |
|
|
* This program is part of LinuxSampler and released under the same terms. |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
#ifndef KEYBOARDREADER_H |
10 |
|
|
#define KEYBOARDREADER_H |
11 |
|
|
|
12 |
|
|
#include <list> |
13 |
|
|
#include <string> |
14 |
|
|
|
15 |
|
|
#include "../common/global.h" |
16 |
|
|
#include "../common/Thread.h" |
17 |
|
|
#include "../common/Condition.h" |
18 |
|
|
#include "TerminalCtrl.h" |
19 |
|
|
|
20 |
|
|
#define KBD_BACKSPACE 127 |
21 |
schoenebeck |
2518 |
#define KBD_ESCAPE 27 |
22 |
schoenebeck |
2515 |
|
23 |
|
|
using namespace LinuxSampler; |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* Implements a FIFO object that is constantly reading from stdin on a separate |
27 |
|
|
* thread. The reader thread can call a callback function as soon as new data |
28 |
|
|
* arrived from stdin. The reader thread will react on every single key stroke, |
29 |
|
|
* that is it will already fire before line end (RETURN / ENTER) was received. |
30 |
|
|
*/ |
31 |
|
|
class KeyboardReader : protected Thread { |
32 |
|
|
public: |
33 |
|
|
typedef void(*Callback_t)(KeyboardReader*); |
34 |
|
|
|
35 |
|
|
KeyboardReader(); |
36 |
|
|
virtual ~KeyboardReader(); |
37 |
|
|
void setCallback(Callback_t fn); |
38 |
|
|
void callback(bool b); |
39 |
|
|
bool charAvailable() const; |
40 |
|
|
char popChar(); |
41 |
|
|
void startReading(); |
42 |
|
|
void stopReading(); |
43 |
|
|
std::string popStringToDelimiterSync(char delimiter, bool includeDelimiter = false); |
44 |
|
|
|
45 |
|
|
static KeyboardReader* singleton(); |
46 |
|
|
protected: |
47 |
|
|
int Main() OVERRIDE; |
48 |
|
|
private: |
49 |
|
|
TerminalSetting m_originalTerminalSetting; |
50 |
|
|
Callback_t m_callback; |
51 |
|
|
std::list<char> m_fifo; |
52 |
|
|
Mutex m_fifoMutex; |
53 |
|
|
bool m_doCallback; |
54 |
|
|
Condition m_sync; |
55 |
|
|
char m_syncDelimiter; |
56 |
|
|
}; |
57 |
|
|
|
58 |
|
|
#endif // KEYBOARDREADER_H |