-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathWindowsKeyManager.cpp
More file actions
240 lines (207 loc) · 6.09 KB
/
WindowsKeyManager.cpp
File metadata and controls
240 lines (207 loc) · 6.09 KB
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
#include "Common.h"
#include "WindowsKeyManager.h"
#include "Shared/KeyDefinitions.h"
WindowsKeyManager::WindowsKeyManager(Emulator* emu, HWND hWnd)
{
_emu = emu;
_hWnd = hWnd;
ResetKeyState();
_keyDefinitions = KeyDefinition::GetSharedKeyDefinitions();
//Init XInput buttons
vector<string> buttonNames = {
"Up", "Down", "Left", "Right",
"Start", "Back",
"L3", "R3", "L1", "R1",
"?", "?",
"A", "B", "X", "Y",
"L2", "R2",
"RT Up", "RT Down", "RT Left", "RT Right",
"LT Up", "LT Down", "LT Left", "LT Right",
"LT Y", "LT X", "RT Y", "RT X"
};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < (int)buttonNames.size(); j++) {
_keyDefinitions.push_back({ "Pad" + std::to_string(i + 1) + " " + buttonNames[j], (uint32_t)(WindowsKeyManager::BaseGamepadIndex + i * 0x100 + j + 1) });
}
}
//Init DirectInput buttons
vector<string> diButtonNames = {
"Y+", "Y-",
"X-", "X+",
"Y2+", "Y2-",
"X2-", "X2+",
"Z+", "Z-",
"Z2+", "Z2-",
"DPad Up", "DPad Down", "DPad Right", "DPad Left"
};
vector<string> axisNames = {
"Y", "X", "Y2", "X2", "Z", "Z2"
};
for(int i = 0; i < 16; i++) {
for(int j = 0; j < (int)diButtonNames.size(); j++) {
_keyDefinitions.push_back({ "Joy" + std::to_string(i + 1) + " " + diButtonNames[j], (uint32_t)(WindowsKeyManager::BaseDirectInputIndex + i * 0x100 + j) });
}
for(int j = 0; j < 128; j++) {
_keyDefinitions.push_back({ "Joy" + std::to_string(i + 1) + " But" + std::to_string(j + 1), (uint32_t)(WindowsKeyManager::BaseDirectInputIndex + i * 0x100 + j + diButtonNames.size()) });
}
for(int j = 0; j < (int)axisNames.size(); j++) {
_keyDefinitions.push_back({ "Joy" + std::to_string(i + 1) + " " + axisNames[j], (uint32_t)(WindowsKeyManager::BaseDirectInputIndex + i * 0x100 + j + diButtonNames.size() + 0x100) });
}
}
for(KeyDefinition& keyDef : _keyDefinitions) {
_keyNames[keyDef.keyCode] = keyDef.name;
_keyCodes[keyDef.name] = keyDef.keyCode;
}
StartUpdateDeviceThread();
}
WindowsKeyManager::~WindowsKeyManager()
{
_stopUpdateDeviceThread = true;
_stopSignal.Signal();
_updateDeviceThread.join();
}
void WindowsKeyManager::StartUpdateDeviceThread()
{
_updateDeviceThread = std::thread([=]() {
_xInput.reset(new XInputManager(_emu));
_directInput.reset(new DirectInputManager(_emu, _hWnd));
while(!_stopUpdateDeviceThread) {
//Check for newly plugged in XInput controllers every 5 secs
//Do not check for DirectInput controllers because this takes more time and sometimes causes issues/freezes
if(_xInput->NeedToUpdate()) {
_xInput->UpdateDeviceList();
}
_stopSignal.Wait(5000);
}
});
}
void WindowsKeyManager::RefreshState()
{
if(!_xInput || !_directInput) {
return;
}
_xInput->RefreshState();
_directInput->RefreshState();
}
bool WindowsKeyManager::IsKeyPressed(uint16_t key)
{
if(_disableAllKeys || key == 0) {
return false;
}
if(key >= WindowsKeyManager::BaseGamepadIndex) {
if(!_xInput || !_directInput) {
return false;
}
if(key >= WindowsKeyManager::BaseDirectInputIndex) {
//Directinput key
uint8_t gamepadPort = (key - WindowsKeyManager::BaseDirectInputIndex) / 0x100;
uint8_t gamepadButton = (key - WindowsKeyManager::BaseDirectInputIndex) % 0x100;
return _directInput->IsPressed(gamepadPort, gamepadButton);
} else {
//XInput key
uint8_t gamepadPort = (key - WindowsKeyManager::BaseGamepadIndex) / 0x100;
uint8_t gamepadButton = (key - WindowsKeyManager::BaseGamepadIndex) % 0x100;
return _xInput->IsPressed(gamepadPort, gamepadButton);
}
} else if(key < 0x205) {
return _keyState[key] != 0;
}
return false;
}
optional<int16_t> WindowsKeyManager::GetAxisPosition(uint16_t key)
{
if(key >= WindowsKeyManager::BaseGamepadIndex) {
if(!_xInput || !_directInput) {
return std::nullopt;
}
if(key >= WindowsKeyManager::BaseDirectInputIndex) {
//Directinput key
uint8_t port = (key - WindowsKeyManager::BaseDirectInputIndex) / 0x100;
uint8_t button = (key - WindowsKeyManager::BaseDirectInputIndex) % 0x100;
return _directInput->GetAxisPosition(port, button);
} else {
//XInput key
uint8_t port = (key - WindowsKeyManager::BaseGamepadIndex) / 0x100;
uint8_t button = (key - WindowsKeyManager::BaseGamepadIndex) % 0x100;
return _xInput->GetAxisPosition(port, button);
}
}
return std::nullopt;
}
bool WindowsKeyManager::IsMouseButtonPressed(MouseButton button)
{
return _keyState[WindowsKeyManager::BaseMouseButtonIndex + (int)button];
}
vector<uint16_t> WindowsKeyManager::GetPressedKeys()
{
vector<uint16_t> result;
if(!_xInput || !_directInput) {
return result;
}
_xInput->RefreshState();
for(int i = 0; i < XUSER_MAX_COUNT; i++) {
for(int j = 1; j <= 26; j++) {
if(_xInput->IsPressed(i, j)) {
result.push_back(WindowsKeyManager::BaseGamepadIndex + i * 0x100 + j);
}
}
}
_directInput->RefreshState();
for(int i = _directInput->GetJoystickCount() - 1; i >= 0; i--) {
for(int j = 0; j < 16+128; j++) {
if(_directInput->IsPressed(i, j)) {
result.push_back(WindowsKeyManager::BaseDirectInputIndex + i * 0x100 + j);
}
}
}
for(int i = 0; i < 0x205; i++) {
if(_keyState[i]) {
result.push_back(i);
}
}
return result;
}
string WindowsKeyManager::GetKeyName(uint16_t keyCode)
{
auto keyDef = _keyNames.find(keyCode);
if(keyDef != _keyNames.end()) {
return keyDef->second;
}
return "";
}
uint16_t WindowsKeyManager::GetKeyCode(string keyName)
{
auto keyDef = _keyCodes.find(keyName);
if(keyDef != _keyCodes.end()) {
return keyDef->second;
}
return 0;
}
void WindowsKeyManager::UpdateDevices()
{
if(!_xInput || !_directInput) {
return;
}
_xInput->UpdateDeviceList();
_directInput->UpdateDeviceList();
}
bool WindowsKeyManager::SetKeyState(uint16_t scanCode, bool state)
{
if(scanCode < 0x205 && _keyState[scanCode] != state) {
_keyState[scanCode] = state;
return true;
}
return false;
}
void WindowsKeyManager::SetDisabled(bool disabled)
{
_disableAllKeys = disabled;
}
void WindowsKeyManager::SetForceFeedback(uint16_t magnitudeRight, uint16_t magnitudeLeft)
{
_xInput->SetForceFeedback(magnitudeRight, magnitudeLeft);
}
void WindowsKeyManager::ResetKeyState()
{
memset(_keyState, 0, sizeof(_keyState));
}