-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
consolekey.cpp
236 lines (184 loc) · 5.05 KB
/
consolekey.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// 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/>.
//
#include "consolekey.hpp"
#include <sys/select.h>
#include <termios.h>
#include <sys/ioctl.h>
using namespace p44;
// MARK: - console key
ConsoleKey::ConsoleKey(char aKeyCode, const char *aDescription, bool aInitialState)
{
// check type of key
initialState = aInitialState;
canToggle = false;
char c = tolower(aKeyCode);
if (c>='a' && c<='z') {
aKeyCode = c; // use lowercase only
canToggle = true;
}
// store params
keyCode = aKeyCode;
description = aDescription;
// display usage
if (canToggle)
printf("- Console input '%s' - Press '%c' to pulse, '%c' to toggle state\n", description.c_str(), keyCode, toupper(keyCode));
else
printf("- Console input '%s' - Press '%c' to pulse\n", description.c_str(), keyCode);
// initial state
state = aInitialState;
if (state) {
printf(" Initial state is active: 1\n");
}
}
ConsoleKey::~ConsoleKey()
{
keyHandlerTicket.cancel();
}
bool ConsoleKey::isSet()
{
return state;
}
void ConsoleKey::setConsoleKeyHandler(ConsoleKeyHandlerCB aHandler)
{
keyHandler = aHandler;
}
void ConsoleKey::setState(bool aState)
{
keyHandlerTicket.cancel();
state = aState;
printf("- Console input '%s' - changed to %d\n", description.c_str(), state);
reportState();
}
void ConsoleKey::toggle()
{
keyHandlerTicket.cancel();
state = !state;
printf("- Console input '%s' - toggled to %d\n", description.c_str(), state);
reportState();
}
void ConsoleKey::pulse()
{
keyHandlerTicket.executeOnce(boost::bind(&ConsoleKey::pulseEnd, this), 200*MilliSecond);
if (state==initialState) {
state = !initialState;
reportState();
}
printf("- Console input '%s' - pulsed to %d for 200mS\n", description.c_str(), state);
}
void ConsoleKey::pulseEnd()
{
if (state!=initialState) {
state = initialState;
reportState();
}
}
void ConsoleKey::reportState()
{
if (keyHandler) {
keyHandler(state, MainLoop::now());
}
}
// MARK: - console key manager singleton
static ConsoleKeyManager *consoleKeyManager = NULL;
ConsoleKeyManager *ConsoleKeyManager::sharedKeyManager()
{
if (consoleKeyManager==NULL) {
consoleKeyManager = new ConsoleKeyManager();
}
return consoleKeyManager;
}
ConsoleKeyManager::ConsoleKeyManager() :
termInitialized(false)
{
// install polling
keyPollTicket.executeOnce(boost::bind(&ConsoleKeyManager::consoleKeyPoll, this, _1));
}
ConsoleKeyManager::~ConsoleKeyManager()
{
}
ConsoleKeyPtr ConsoleKeyManager::newConsoleKey(char aKeyCode, const char *aDescription, bool aInitialState)
{
ConsoleKeyPtr newKey = ConsoleKeyPtr(new ConsoleKey(aKeyCode, aDescription, aInitialState));
// store in map
keyMap[newKey->keyCode] = newKey;
// return
return newKey;
}
void ConsoleKeyManager::setKeyPressHandler(ConsoleKeyPressCB aHandler)
{
keyPressHandler = aHandler;
}
int ConsoleKeyManager::kbHit()
{
static const int STDIN = 0;
if (!termInitialized) {
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
termInitialized = true;
}
// return number of bytes waiting
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
#define KEY_POLL_INTERVAL (50*MilliSecond)
#define KEY_POLL_TOLERANCE (20*MilliSecond)
void ConsoleKeyManager::consoleKeyPoll(MLTimer &aTimer)
{
// process all pending console input
while (kbHit()>0) {
char c = getchar();
bool handled = false;
if (keyPressHandler) {
// call custom keypress handler
handled = keyPressHandler(c);
}
if (!handled) {
bool toggle = false;
// A..Z are special "sticky" toggle variants of a..z
if (c>='A' && c<='Z') {
// make lowercase
c = tolower(c);
// toggle
toggle = true;
}
// search key in map
ConsoleKeyMap::iterator keypos = keyMap.find(c);
if (keypos!=keyMap.end()) {
// key is mapped
if (toggle) {
// toggle state
keypos->second->toggle();
}
else {
// pulse
keypos->second->pulse();
}
}
}
}
MainLoop::currentMainLoop().retriggerTimer(aTimer, KEY_POLL_INTERVAL, KEY_POLL_TOLERANCE);
}