This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
InputComponent.cpp
291 lines (250 loc) · 8.66 KB
/
InputComponent.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "QsLog.h"
#include "InputComponent.h"
#include "settings/SettingsComponent.h"
#include "system/SystemComponent.h"
#include "power/PowerComponent.h"
#include "InputKeyboard.h"
#include "InputSocket.h"
#include "InputRoku.h"
#ifdef Q_OS_MAC
#include "apple/InputAppleRemote.h"
#include "apple/InputAppleMediaKeys.h"
#endif
#ifdef HAVE_SDL
#include "InputSDL.h"
#endif
#ifdef HAVE_LIRC
#include "InputLIRC.h"
#endif
#ifdef HAVE_CEC
#include "InputCEC.h"
#endif
#define LONG_HOLD_MSEC 500
#define INITAL_AUTOREPEAT_MSEC 650
// Synthetic key repeat events are emitted at 60ms intervals. The interval is
// half of the fastest press-and-release time. Empirical key repeat intervals:
// * Key hold on macOS wireless USB keyboard with fastest key repeat preference: ~35s
// * Press and release on macOS wireless USB keyboard (like a meth monkey): ~120ms
// * Key hold on macOS Apple TV IR remote + FLiRC (3.8 firmware) with fastest key repeat preference: ~35s
// * Press and release on macOS Apple TV IR remote + FLiRC (3.8 firmware): ~150ms
//
#define AUTOREPEAT_MSEC 60
///////////////////////////////////////////////////////////////////////////////////////////////////
InputComponent::InputComponent(QObject* parent) : ComponentBase(parent)
{
m_mappings = new InputMapping(this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
bool InputComponent::addInput(InputBase* base)
{
if (!base->initInput())
{
QLOG_WARN() << "Failed to init input:" << base->inputName();
return false;
}
QLOG_INFO() << "Successfully inited input:" << base->inputName();
m_inputs.push_back(base);
// we connect to the provider receivedInput signal, then we check if the name
// needs to be remaped in remapInput and then finally send it out to JS land.
//
connect(base, &InputBase::receivedInput, this, &InputComponent::remapInput);
// for auto-repeating inputs
//
m_autoRepeatTimer = new QTimer(this);
connect(m_autoRepeatTimer, &QTimer::timeout, [=]()
{
if (!m_autoRepeatActions.isEmpty())
{
QLOG_DEBUG() << "Emit input action (autorepeat):" << m_autoRepeatActions;
emit hostInput(m_autoRepeatActions);
}
m_autoRepeatTimer->setInterval(AUTOREPEAT_MSEC);
});
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
bool InputComponent::componentInitialize()
{
// load our input mappings
m_mappings->loadMappings();
addInput(&InputKeyboard::Get());
addInput(new InputSocket(this));
addInput(new InputRoku(this));
#ifdef Q_OS_MAC
addInput(new InputAppleRemote(this));
addInput(new InputAppleMediaKeys(this));
#endif
#ifdef HAVE_SDL
if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "sdlEnabled").toBool())
addInput(new InputSDL(this));
#endif
#ifdef HAVE_LIRC
addInput(new InputLIRC(this));
#endif
#ifdef HAVE_CEC
if (SettingsComponent::Get().value(SETTINGS_SECTION_CEC, "enable").toBool())
addInput(new InputCEC(this));
#endif
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::handleAction(const QString& action)
{
if (action.startsWith("host:"))
{
QStringList argList = action.mid(5).split(" ");
QString hostCommand = argList.value(0);
QString hostArguments;
if (argList.size() > 1)
{
argList.pop_front();
hostArguments = argList.join(" ");
}
QLOG_DEBUG() << "Got host command:" << hostCommand << "arguments:" << hostArguments;
if (m_hostCommands.contains(hostCommand))
{
ReceiverSlot* recvSlot = m_hostCommands.value(hostCommand);
if (recvSlot)
{
if (recvSlot->m_function)
{
QLOG_DEBUG() << "Invoking anonymous function";
recvSlot->m_function();
}
else
{
QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->m_slot.data());
QGenericArgument arg0 = QGenericArgument();
if (recvSlot->m_hasArguments)
arg0 = Q_ARG(const QString&, hostArguments);
if (!QMetaObject::invokeMethod(recvSlot->m_receiver, recvSlot->m_slot.data(),
Qt::AutoConnection, arg0))
{
QLOG_ERROR() << "Invoking slot" << qPrintable(recvSlot->m_slot.data()) << "failed!";
}
}
}
}
else
{
QLOG_WARN() << "No such host command:" << hostCommand;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::remapInput(const QString &source, const QString &keycode, InputBase::InputkeyState keyState)
{
QLOG_DEBUG() << "Input received: source:" << source << "keycode:" << keycode << ":" << keyState;
emit receivedInput();
if (keyState == InputBase::KeyUp)
{
cancelAutoRepeat();
if (!m_currentLongPressAction.isEmpty())
{
QString type;
if (m_longHoldTimer.elapsed() > LONG_HOLD_MSEC)
type = "long";
else
type = "short";
QString action = m_currentLongPressAction.value(type).toString();
m_currentLongPressAction.clear();
QLOG_DEBUG() << "Emit input action (" + type + "):" << action;
emit hostInput(QStringList{action});
}
return;
}
QStringList queuedActions;
m_autoRepeatActions.clear();
auto actions = m_mappings->mapToAction(source, keycode);
for (auto action : actions)
{
if (action.type() == QVariant::String)
{
queuedActions.append(action.toString());
m_autoRepeatActions.append(action.toString());
}
else if (action.type() == QVariant::Map)
{
QVariantMap map = action.toMap();
if (map.contains("long"))
{
// Don't overwrite long actions if there was no key up event yet.
// (It could be a key autorepeated by Qt.)
if (m_currentLongPressAction.isEmpty())
{
m_longHoldTimer.start();
m_currentLongPressAction = map;
}
}
else if (map.contains("short"))
{
queuedActions.append(map.value("short").toString());
}
}
else if (action.type() == QVariant::List)
{
queuedActions.append(action.toStringList());
}
}
if (!m_autoRepeatActions.isEmpty() && keyState != InputBase::KeyPressed)
m_autoRepeatTimer->start(INITAL_AUTOREPEAT_MSEC);
if (!queuedActions.isEmpty())
{
if (SystemComponent::Get().isWebClientConnected())
{
QLOG_DEBUG() << "Emit input action:" << queuedActions;
emit hostInput(queuedActions);
}
else
{
QLOG_DEBUG() << "Web Client has not connected, handling input in host instead.";
executeActions(queuedActions);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::executeActions(const QStringList& actions)
{
for (auto action : actions)
handleAction(action);
}
/////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::registerHostCommand(const QString& command, QObject* receiver, const char* slot)
{
auto recvSlot = new ReceiverSlot;
recvSlot->m_receiver = receiver;
recvSlot->m_slot = QMetaObject::normalizedSignature(slot);
recvSlot->m_hasArguments = false;
QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to"
<< qPrintable(QString(receiver->metaObject()->className()) + "::" + recvSlot->m_slot);
m_hostCommands.insert(command, recvSlot);
auto slotWithArgs = QString("%1(QString)").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
auto slotWithoutArgs = QString("%1()").arg(QString::fromLatin1(recvSlot->m_slot)).toLatin1();
if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithArgs.data()) != -1)
{
QLOG_DEBUG() << "Host command maps to method with an argument.";
recvSlot->m_hasArguments = true;
}
else if (recvSlot->m_receiver->metaObject()->indexOfMethod(slotWithoutArgs.data()) != -1)
{
QLOG_DEBUG() << "Host command maps to method without arguments.";
}
else
{
QLOG_ERROR() << "Slot for host command missing, or has incorrect signature!";
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::registerHostCommand(const QString& command, std::function<void(void)> function)
{
auto recvSlot = new ReceiverSlot;
recvSlot->m_function = function;
QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to anonymous function";
m_hostCommands.insert(command, recvSlot);
}
/////////////////////////////////////////////////////////////////////////////////////////
void InputComponent::cancelAutoRepeat()
{
m_autoRepeatTimer->stop();
m_autoRepeatActions.clear();
}