-
-
Notifications
You must be signed in to change notification settings - Fork 705
Expand file tree
/
Copy pathEmuInstanceInput.cpp
More file actions
470 lines (398 loc) · 11.4 KB
/
EmuInstanceInput.cpp
File metadata and controls
470 lines (398 loc) · 11.4 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright 2016-2026 melonDS team
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include <QKeyEvent>
#include <SDL2/SDL.h>
#include "Platform.h"
#include "SDL_gamecontroller.h"
#include "SDL_sensor.h"
#include "main.h"
#include "Config.h"
using namespace melonDS;
const char* EmuInstance::buttonNames[12] =
{
"A",
"B",
"Select",
"Start",
"Right",
"Left",
"Up",
"Down",
"R",
"L",
"X",
"Y"
};
const char* EmuInstance::hotkeyNames[HK_MAX] =
{
"HK_Lid",
"HK_Mic",
"HK_Pause",
"HK_Reset",
"HK_FastForward",
"HK_FrameLimitToggle",
"HK_FullscreenToggle",
"HK_SwapScreens",
"HK_SwapScreenEmphasis",
"HK_SolarSensorDecrease",
"HK_SolarSensorIncrease",
"HK_FrameStep",
"HK_PowerButton",
"HK_VolumeUp",
"HK_VolumeDown",
"HK_AudioMuteToggle",
"HK_SlowMo",
"HK_FastForwardToggle",
"HK_SlowMoToggle",
"HK_GuitarGripGreen",
"HK_GuitarGripRed",
"HK_GuitarGripYellow",
"HK_GuitarGripBlue",
"HK_SpeedStep"
};
std::shared_ptr<SDL_mutex> EmuInstance::joyMutexGlobal = nullptr;
void EmuInstance::inputInit()
{
if (!joyMutexGlobal)
{
SDL_mutex* mutex = SDL_CreateMutex();
joyMutexGlobal = std::shared_ptr<SDL_mutex>(mutex, SDL_DestroyMutex);
}
joyMutex = joyMutexGlobal;
keyInputMask = 0xFFF;
joyInputMask = 0xFFF;
inputMask = 0xFFF;
keyHotkeyMask = 0;
joyHotkeyMask = 0;
hotkeyMask = 0;
lastHotkeyMask = 0;
isTouching = false;
touchX = 0;
touchY = 0;
joystick = nullptr;
controller = nullptr;
hasRumble = false;
hasAccelerometer = false;
hasGyroscope = false;
isRumbling = false;
inputLoadConfig();
}
void EmuInstance::inputDeInit()
{
SDL_LockMutex(joyMutex.get());
closeJoystick();
SDL_UnlockMutex(joyMutex.get());
}
void EmuInstance::inputLoadConfig()
{
SDL_LockMutex(joyMutex.get());
Config::Table keycfg = localCfg.GetTable("Keyboard");
Config::Table joycfg = localCfg.GetTable("Joystick");
for (int i = 0; i < 12; i++)
{
keyMapping[i] = keycfg.GetInt(buttonNames[i]);
joyMapping[i] = joycfg.GetInt(buttonNames[i]);
}
for (int i = 0; i < HK_MAX; i++)
{
hkKeyMapping[i] = keycfg.GetInt(hotkeyNames[i]);
hkJoyMapping[i] = joycfg.GetInt(hotkeyNames[i]);
}
setJoystick(localCfg.GetInt("JoystickID"));
SDL_UnlockMutex(joyMutex.get());
}
void EmuInstance::inputRumbleStart(melonDS::u32 len_ms)
{
SDL_LockMutex(joyMutex.get());
if (controller && hasRumble && !isRumbling)
{
SDL_GameControllerRumble(controller, 0xFFFF, 0xFFFF, len_ms);
isRumbling = true;
}
SDL_UnlockMutex(joyMutex.get());
}
void EmuInstance::inputRumbleStop()
{
SDL_LockMutex(joyMutex.get());
if (controller && hasRumble && isRumbling)
{
SDL_GameControllerRumble(controller, 0, 0, 0);
isRumbling = false;
}
SDL_UnlockMutex(joyMutex.get());
}
float EmuInstance::inputMotionQuery(melonDS::Platform::MotionQueryType type)
{
float values[3];
SDL_LockMutex(joyMutex.get());
if (type <= melonDS::Platform::MotionAccelerationZ)
{
if (controller && hasAccelerometer)
{
if (SDL_GameControllerGetSensorData(controller, SDL_SENSOR_ACCEL, values, 3) == 0)
{
// Map values from DS console orientation to SDL controller orientation.
SDL_UnlockMutex(joyMutex.get());
switch (type)
{
case melonDS::Platform::MotionAccelerationX:
return values[0];
case melonDS::Platform::MotionAccelerationY:
return -values[2];
case melonDS::Platform::MotionAccelerationZ:
return values[1];
}
}
}
}
else if (type <= melonDS::Platform::MotionRotationZ)
{
if (controller && hasGyroscope)
{
if (SDL_GameControllerGetSensorData(controller, SDL_SENSOR_GYRO, values, 3) == 0)
{
// Map values from DS console orientation to SDL controller orientation.
SDL_UnlockMutex(joyMutex.get());
switch (type)
{
case melonDS::Platform::MotionRotationX:
return values[0];
case melonDS::Platform::MotionRotationY:
return -values[2];
case melonDS::Platform::MotionRotationZ:
return values[1];
}
}
}
}
SDL_UnlockMutex(joyMutex.get());
if (type == melonDS::Platform::MotionAccelerationZ)
return SDL_STANDARD_GRAVITY;
return 0.0f;
}
void EmuInstance::setJoystick(int id)
{
SDL_LockMutex(joyMutex.get());
joystickID = id;
openJoystick();
SDL_UnlockMutex(joyMutex.get());
}
void EmuInstance::openJoystick()
{
if (controller) SDL_GameControllerClose(controller);
if (joystick) SDL_JoystickClose(joystick);
int num = SDL_NumJoysticks();
if (num < 1)
{
controller = nullptr;
joystick = nullptr;
hasRumble = false;
hasAccelerometer = false;
hasGyroscope = false;
return;
}
if (joystickID >= num)
joystickID = 0;
joystick = SDL_JoystickOpen(joystickID);
if (SDL_IsGameController(joystickID))
{
controller = SDL_GameControllerOpen(joystickID);
}
if (controller)
{
if (SDL_GameControllerHasRumble(controller))
{
hasRumble = true;
}
if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL))
{
hasAccelerometer = SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE) == 0;
}
if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO))
{
hasGyroscope = SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE) == 0;
}
}
}
void EmuInstance::closeJoystick()
{
if (controller)
{
SDL_GameControllerClose(controller);
controller = nullptr;
hasRumble = false;
hasAccelerometer = false;
hasGyroscope = false;
}
if (joystick)
{
SDL_JoystickClose(joystick);
joystick = nullptr;
}
}
// distinguish between left and right modifier keys (Ctrl, Alt, Shift)
// Qt provides no real cross-platform way to do this, so here we go
// for Windows and Linux we can distinguish via scancodes (but both
// provide different scancodes)
bool isRightModKey(QKeyEvent* event)
{
#ifdef __WIN32__
quint32 scan = event->nativeScanCode();
return (scan == 0x11D || scan == 0x138 || scan == 0x36);
#elif __APPLE__
quint32 scan = event->nativeVirtualKey();
return (scan == 0x36 || scan == 0x3C || scan == 0x3D || scan == 0x3E);
#else
quint32 scan = event->nativeScanCode();
return (scan == 0x69 || scan == 0x6C || scan == 0x3E);
#endif
}
int getEventKeyVal(QKeyEvent* event)
{
int key = event->key();
int mod = event->modifiers();
bool ismod = (key == Qt::Key_Control ||
key == Qt::Key_Alt ||
key == Qt::Key_AltGr ||
key == Qt::Key_Shift ||
key == Qt::Key_Meta);
if (!ismod)
key |= mod;
else if (isRightModKey(event))
key |= (1<<31);
return key;
}
void EmuInstance::onKeyPress(QKeyEvent* event)
{
int keyHK = getEventKeyVal(event);
int keyKP = keyHK;
if (event->modifiers() != Qt::KeypadModifier)
keyKP &= ~event->modifiers();
for (int i = 0; i < 12; i++)
if (keyKP == keyMapping[i])
keyInputMask &= ~(1<<i);
for (int i = 0; i < HK_MAX; i++)
if (keyHK == hkKeyMapping[i])
keyHotkeyMask |= (1<<i);
}
void EmuInstance::onKeyRelease(QKeyEvent* event)
{
int keyHK = getEventKeyVal(event);
int keyKP = keyHK;
if (event->modifiers() != Qt::KeypadModifier)
keyKP &= ~event->modifiers();
for (int i = 0; i < 12; i++)
if (keyKP == keyMapping[i])
keyInputMask |= (1<<i);
for (int i = 0; i < HK_MAX; i++)
if (keyHK == hkKeyMapping[i])
keyHotkeyMask &= ~(1<<i);
}
void EmuInstance::keyReleaseAll()
{
keyInputMask = 0xFFF;
keyHotkeyMask = 0;
}
bool EmuInstance::joystickButtonDown(int val)
{
if (val == -1) return false;
bool hasbtn = ((val & 0xFFFF) != 0xFFFF);
if (hasbtn)
{
if (val & 0x100)
{
int hatnum = (val >> 4) & 0xF;
int hatdir = val & 0xF;
Uint8 hatval = SDL_JoystickGetHat(joystick, hatnum);
bool pressed = false;
if (hatdir == 0x1) pressed = (hatval & SDL_HAT_UP);
else if (hatdir == 0x4) pressed = (hatval & SDL_HAT_DOWN);
else if (hatdir == 0x2) pressed = (hatval & SDL_HAT_RIGHT);
else if (hatdir == 0x8) pressed = (hatval & SDL_HAT_LEFT);
if (pressed) return true;
}
else
{
int btnnum = val & 0xFFFF;
Uint8 btnval = SDL_JoystickGetButton(joystick, btnnum);
if (btnval) return true;
}
}
if (val & 0x10000)
{
int axisnum = (val >> 24) & 0xF;
int axisdir = (val >> 20) & 0xF;
Sint16 axisval = SDL_JoystickGetAxis(joystick, axisnum);
switch (axisdir)
{
case 0: // positive
if (axisval > 16384) return true;
break;
case 1: // negative
if (axisval < -16384) return true;
break;
case 2: // trigger
if (axisval > 0) return true;
break;
}
}
return false;
}
void EmuInstance::inputProcess()
{
SDL_LockMutex(joyMutex.get());
SDL_JoystickUpdate();
if (joystick)
{
if (!SDL_JoystickGetAttached(joystick))
{
SDL_JoystickClose(joystick);
joystick = nullptr;
}
}
if (!joystick && (SDL_NumJoysticks() > 0))
{
openJoystick();
}
joyInputMask = 0xFFF;
if (joystick)
{
for (int i = 0; i < 12; i++)
if (joystickButtonDown(joyMapping[i]))
joyInputMask &= ~(1 << i);
}
inputMask = keyInputMask & joyInputMask;
joyHotkeyMask = 0;
if (joystick)
{
for (int i = 0; i < HK_MAX; i++)
if (joystickButtonDown(hkJoyMapping[i]))
joyHotkeyMask |= (1 << i);
}
hotkeyMask = keyHotkeyMask | joyHotkeyMask;
hotkeyPress = hotkeyMask & ~lastHotkeyMask;
hotkeyRelease = lastHotkeyMask & ~hotkeyMask;
lastHotkeyMask = hotkeyMask;
SDL_UnlockMutex(joyMutex.get());
}
void EmuInstance::touchScreen(int x, int y)
{
touchX = x;
touchY = y;
isTouching = true;
}
void EmuInstance::releaseScreen()
{
isTouching = false;
}