-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsolekey.hpp
130 lines (98 loc) · 3.53 KB
/
consolekey.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef __p44utils__consolekey__
#define __p44utils__consolekey__
#include "p44utils_main.hpp"
using namespace std;
namespace p44 {
class ConsoleKeyManager;
/// Wrapper to use single keys on the console as simulated buttons
class ConsoleKey : public P44Obj
{
friend class ConsoleKeyManager;
public:
/// button event handler
/// @param aNewState the current state of the key
/// @param aTimestamp the main loop timestamp of the key action
typedef boost::function<void (bool aNewState, MLMicroSeconds aTimestamp)> ConsoleKeyHandlerCB;
private:
char keyCode;
string description;
bool state;
bool initialState;
bool canToggle;
ConsoleKeyHandlerCB keyHandler;
MLTicket keyHandlerTicket;
ConsoleKey(char aKeyCode, const char *aDescription, bool aInitialState=false);
public:
virtual ~ConsoleKey();
/// get key code
char getKeyCode() { return keyCode; };
/// get state of input
/// @return true if key is pressed
bool isSet();
/// set handler for when key state changes
/// @param aHandler to call when input state changes
void setConsoleKeyHandler(ConsoleKeyHandlerCB aHandler);
private:
void setState(bool aState);
void toggle();
void pulse();
void pulseEnd();
void reportState();
};
typedef boost::intrusive_ptr<ConsoleKey> ConsoleKeyPtr;
typedef std::map<char, ConsoleKeyPtr> ConsoleKeyMap;
/// manager of console keys
class ConsoleKeyManager
{
friend class ConsoleKey;
public:
/// button event handler
/// @param aKeyPress key pressed
/// @return true if fully handled already
typedef boost::function<bool (char aKeyPress)> ConsoleKeyPressCB;
private:
MLTicket keyPollTicket;
ConsoleKeyMap keyMap;
bool termInitialized;
ConsoleKeyPressCB keyPressHandler;
ConsoleKeyManager();
virtual ~ConsoleKeyManager();
public:
static ConsoleKeyManager *sharedKeyManager();
/// install a callback for when a key is pressed
/// @param aHandler to call when a key is pressed on the console
void setKeyPressHandler(ConsoleKeyPressCB aHandler);
/// create a new console key
/// @param aKeyCode ASCII-code of the key. A-Z are special, as
/// for these typing the lowercase (a-z) means pulsing the input state,
/// while typing the uppercase (A-Z) means toggling the input state.
/// @param aDescription description shown on console at initialisation and when key
/// is operated.
ConsoleKeyPtr newConsoleKey(char aKeyCode, const char *aDescription, bool aInitialState=false);
private:
int kbHit();
void consoleKeyPoll(MLTimer &aTimer);
};
} // namespace
#endif /* defined(__p44utils__consolekey__) */