/
GUIConfigurationWizard.cpp
356 lines (288 loc) · 8.35 KB
/
GUIConfigurationWizard.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2014-2016 Team Kodi
* http://kodi.tv
*
* This Program 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 2, or (at your option)
* any later version.
*
* This Program 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 this Program; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "GUIConfigurationWizard.h"
#include "games/controllers/guicontrols/GUIFeatureButton.h"
#include "games/controllers/Controller.h"
#include "games/controllers/ControllerFeature.h"
#include "input/joysticks/IButtonMap.h"
#include "input/joysticks/IButtonMapCallback.h"
#include "input/keyboard/KeymapActionMap.h"
#include "input/InputManager.h"
#include "peripherals/Peripherals.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
using namespace KODI;
using namespace GAME;
#define ESC_KEY_CODE 27
#define SKIPPING_DETECTION_MS 200
// Duration to wait for axes to neutralize after mapping is finished
#define POST_MAPPING_WAIT_TIME_MS (5 * 1000)
CGUIConfigurationWizard::CGUIConfigurationWizard(bool bEmulation, unsigned int controllerNumber /* = 0 */) :
CThread("GUIConfigurationWizard"),
m_bEmulation(bEmulation),
m_controllerNumber(controllerNumber),
m_actionMap(new KEYBOARD::CKeymapActionMap)
{
InitializeState();
}
CGUIConfigurationWizard::~CGUIConfigurationWizard(void)
{
}
void CGUIConfigurationWizard::InitializeState(void)
{
m_currentButton = nullptr;
m_currentDirection = JOYSTICK::ANALOG_STICK_DIRECTION::UNKNOWN;
m_history.clear();
}
void CGUIConfigurationWizard::Run(const std::string& strControllerId, const std::vector<IFeatureButton*>& buttons)
{
Abort();
{
CSingleLock lock(m_stateMutex);
// Set Run() parameters
m_strControllerId = strControllerId;
m_buttons = buttons;
// Reset synchronization variables
m_inputEvent.Reset();
m_motionlessEvent.Reset();
m_bInMotion.clear();
// Initialize state variables
InitializeState();
}
Create();
}
void CGUIConfigurationWizard::OnUnfocus(IFeatureButton* button)
{
CSingleLock lock(m_stateMutex);
if (button == m_currentButton)
Abort(false);
}
bool CGUIConfigurationWizard::Abort(bool bWait /* = true */)
{
if (IsRunning())
{
StopThread(false);
m_inputEvent.Set();
m_motionlessEvent.Set();
if (bWait)
StopThread(true);
return true;
}
return false;
}
void CGUIConfigurationWizard::Process(void)
{
CLog::Log(LOGDEBUG, "Starting configuration wizard");
InstallHooks();
{
CSingleLock lock(m_stateMutex);
for (IFeatureButton* button : m_buttons)
{
// Allow other threads to access the button we're using
m_currentButton = button;
while (!button->IsFinished())
{
// Allow other threads to access which direction the analog stick is on
m_currentDirection = button->GetDirection();
// Wait for input
{
CSingleExit exit(m_stateMutex);
CLog::Log(LOGDEBUG, "%s: Waiting for input for feature \"%s\"", m_strControllerId.c_str(), button->Feature().Name().c_str());
if (!button->PromptForInput(m_inputEvent))
Abort(false);
}
if (m_bStop)
break;
}
button->Reset();
if (m_bStop)
break;
}
// Finished mapping
InitializeState();
}
for (auto callback : ButtonMapCallbacks())
callback.second->SaveButtonMap();
bool bInMotion;
{
CSingleLock lock(m_motionMutex);
bInMotion = !m_bInMotion.empty();
}
if (bInMotion)
{
CLog::Log(LOGDEBUG, "Configuration wizard: waiting %ums for axes to neutralize", POST_MAPPING_WAIT_TIME_MS);
m_motionlessEvent.WaitMSec(POST_MAPPING_WAIT_TIME_MS);
}
RemoveHooks();
CLog::Log(LOGDEBUG, "Configuration wizard ended");
}
bool CGUIConfigurationWizard::MapPrimitive(JOYSTICK::IButtonMap* buttonMap,
JOYSTICK::IActionMap* actionMap,
const JOYSTICK::CDriverPrimitive& primitive)
{
using namespace JOYSTICK;
bool bHandled = false;
// Handle esc key separately
if (primitive.Type() == PRIMITIVE_TYPE::BUTTON &&
primitive.Index() == ESC_KEY_CODE)
{
bHandled = Abort(false);
}
else if (m_history.find(primitive) != m_history.end())
{
// Primitive has already been mapped this round, ignore it
bHandled = true;
}
else if (buttonMap->IsIgnored(primitive))
{
bHandled = true;
}
else
{
// Get the current state of the thread
IFeatureButton* currentButton;
ANALOG_STICK_DIRECTION currentDirection;
{
CSingleLock lock(m_stateMutex);
currentButton = m_currentButton;
currentDirection = m_currentDirection;
}
if (currentButton)
{
const CControllerFeature& feature = currentButton->Feature();
CLog::Log(LOGDEBUG, "%s: mapping feature \"%s\" for device %s",
m_strControllerId.c_str(), feature.Name().c_str(), buttonMap->DeviceName().c_str());
switch (feature.Type())
{
case FEATURE_TYPE::SCALAR:
{
buttonMap->AddScalar(feature.Name(), primitive);
bHandled = true;
break;
}
case FEATURE_TYPE::ANALOG_STICK:
{
buttonMap->AddAnalogStick(feature.Name(), currentDirection, primitive);
bHandled = true;
break;
}
default:
break;
}
if (bHandled)
{
m_history.insert(primitive);
OnMotion(buttonMap);
m_inputEvent.Set();
}
}
}
return bHandled;
}
void CGUIConfigurationWizard::OnEventFrame(const JOYSTICK::IButtonMap* buttonMap, bool bMotion)
{
CSingleLock lock(m_motionMutex);
if (m_bInMotion.find(buttonMap) != m_bInMotion.end() && !bMotion)
OnMotionless(buttonMap);
}
void CGUIConfigurationWizard::OnLateAxis(const JOYSTICK::IButtonMap* buttonMap, unsigned int axisIndex)
{
//! @todo
}
void CGUIConfigurationWizard::OnMotion(const JOYSTICK::IButtonMap* buttonMap)
{
CSingleLock lock(m_motionMutex);
m_motionlessEvent.Reset();
m_bInMotion.insert(buttonMap);
}
void CGUIConfigurationWizard::OnMotionless(const JOYSTICK::IButtonMap* buttonMap)
{
m_bInMotion.erase(buttonMap);
if (m_bInMotion.empty())
m_motionlessEvent.Set();
}
bool CGUIConfigurationWizard::OnKeyPress(const CKey& key)
{
using namespace KEYBOARD;
bool bHandled = false;
switch (m_actionMap->GetActionID(key))
{
case ACTION_MOVE_LEFT:
case ACTION_MOVE_RIGHT:
case ACTION_MOVE_UP:
case ACTION_MOVE_DOWN:
case ACTION_PAGE_UP:
case ACTION_PAGE_DOWN:
// Abort and allow motion
Abort(false);
bHandled = false;
break;
case ACTION_PARENT_DIR:
case ACTION_PREVIOUS_MENU:
case ACTION_STOP:
// Abort and prevent action
Abort(false);
bHandled = true;
break;
default:
// Absorb keypress
bHandled = true;
break;
}
return bHandled;
}
bool CGUIConfigurationWizard::OnButtonPress(const std::string& button)
{
return Abort(false);
}
void CGUIConfigurationWizard::InstallHooks(void)
{
using namespace PERIPHERALS;
g_peripherals.RegisterJoystickButtonMapper(this);
g_peripherals.RegisterObserver(this);
// If we're not using emulation, allow keyboard input to abort prompt
if (!m_bEmulation)
CInputManager::GetInstance().RegisterKeyboardHandler(this);
CInputManager::GetInstance().RegisterMouseHandler(this);
}
void CGUIConfigurationWizard::RemoveHooks(void)
{
using namespace PERIPHERALS;
CInputManager::GetInstance().UnregisterMouseHandler(this);
if (!m_bEmulation)
CInputManager::GetInstance().UnregisterKeyboardHandler(this);
g_peripherals.UnregisterObserver(this);
g_peripherals.UnregisterJoystickButtonMapper(this);
}
void CGUIConfigurationWizard::Notify(const Observable& obs, const ObservableMessage msg)
{
using namespace PERIPHERALS;
switch (msg)
{
case ObservableMessagePeripheralsChanged:
{
g_peripherals.UnregisterJoystickButtonMapper(this);
g_peripherals.RegisterJoystickButtonMapper(this);
break;
}
default:
break;
}
}