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
|
/* Per-platform code - ReGBA on OpenDingux
*
* Copyright (C) 2013 Paul Cercueil and Dingoonity user Nebuleon
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public Licens e as
* published by the Free Software Foundation; either version 2 of
* the License, 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; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common.h"
uint_fast8_t FastForwardFrameskip = 0;
uint32_t PerGameFastForwardTarget = 0;
uint32_t FastForwardTarget = 4; // 6x by default
uint32_t PerGameAnalogSensitivity = 0;
uint32_t AnalogSensitivity = 0; // require 32256/32767 of the axis by default
uint32_t PerGameAnalogAction = 0;
uint32_t AnalogAction = 0;
uint_fast8_t FastForwardFrameskipControl = 0;
static SDL_Joystick* Joystick;
static bool JoystickInitialised = false;
// Mandatory remapping for OpenmDingux keys. Each OpenmDingux key maps to a
// key on the keyboard, but not all keys on the keyboard map to these.
// They are not in GBA bitfield order in this array.
uint32_t OpenDinguxKeys[OPENDINGUX_BUTTON_COUNT] = {
SDLK_TAB, // L
SDLK_BACKSPACE, // R
SDLK_DOWN, // Down
SDLK_UP, // Up
SDLK_LEFT, // Left
SDLK_RIGHT, // Right
SDLK_RETURN, // Start
SDLK_ESCAPE, // Select
SDLK_LALT, // Lower face button (B)
SDLK_LCTRL, // Right face button (A)
SDLK_LSHIFT, // Left face button (GCW X, A320 Y)
SDLK_SPACE, // Upper face button (GCW Y, A320 X)
0,
0,
0,
0,
SDLK_HOME, // GCW: Quick flick of Power
};
// These must be OpenDingux buttons at the bit suitable for the ReGBA_Buttons
// enumeration.
const enum OpenDingux_Buttons DefaultKeypadRemapping[12] = {
OPENDINGUX_BUTTON_FACE_RIGHT, // GBA A
OPENDINGUX_BUTTON_FACE_DOWN, // GBA B
OPENDINGUX_BUTTON_SELECT, // GBA Select
OPENDINGUX_BUTTON_START, // GBA Start
OPENDINGUX_BUTTON_RIGHT, // GBA D-pad Right
OPENDINGUX_BUTTON_LEFT, // GBA D-pad Left
OPENDINGUX_BUTTON_UP, // GBA D-pad Up
OPENDINGUX_BUTTON_DOWN, // GBA D-pad Down
OPENDINGUX_BUTTON_R, // GBA R trigger
OPENDINGUX_BUTTON_L, // GBA L trigger
0, // ReGBA rapid-fire A
0, // ReGBA rapid-fire B
};
// These must be OpenDingux buttons at the bit suitable for the ReGBA_Buttons
// enumeration.
enum OpenDingux_Buttons PerGameKeypadRemapping[12] = {
0, // GBA A
0, // GBA B
0, // GBA Select
0, // GBA Start
0, // GBA D-pad Right
0, // GBA D-pad Left
0, // GBA D-pad Up
0, // GBA D-pad Down
0, // GBA R trigger
0, // GBA L trigger
0, // ReGBA rapid-fire A
0, // ReGBA rapid-fire B
};
enum OpenDingux_Buttons KeypadRemapping[12] = {
OPENDINGUX_BUTTON_FACE_RIGHT, // GBA A
OPENDINGUX_BUTTON_FACE_DOWN, // GBA B
OPENDINGUX_BUTTON_SELECT, // GBA Select
OPENDINGUX_BUTTON_START, // GBA Start
OPENDINGUX_BUTTON_RIGHT, // GBA D-pad Right
OPENDINGUX_BUTTON_LEFT, // GBA D-pad Left
OPENDINGUX_BUTTON_UP, // GBA D-pad Up
OPENDINGUX_BUTTON_DOWN, // GBA D-pad Down
OPENDINGUX_BUTTON_R, // GBA R trigger
OPENDINGUX_BUTTON_L, // GBA L trigger
0, // ReGBA rapid-fire A
0, // ReGBA rapid-fire B
};
enum OpenDingux_Buttons PerGameHotkeys[5] = {
0, // Fast-forward while held
0, // Menu
0, // Fast-forward toggle
0, // Quick load state #1
0, // Quick save state #1
};
enum OpenDingux_Buttons Hotkeys[5] = {
0, // Fast-forward while held
OPENDINGUX_BUTTON_FACE_UP, // Menu
0, // Fast-forward toggle
0, // Quick load state #1
0, // Quick save state #1
};
// The menu keys, in decreasing order of priority when two or more are
// pressed. For example, when the user keeps a direction pressed but also
// presses A, start ignoring the direction.
enum OpenDingux_Buttons MenuKeys[7] = {
OPENDINGUX_BUTTON_FACE_RIGHT, // Select/Enter button
OPENDINGUX_BUTTON_FACE_DOWN, // Cancel/Leave button
OPENDINGUX_BUTTON_DOWN | OPENDINGUX_ANALOG_DOWN, // Menu navigation
OPENDINGUX_BUTTON_UP | OPENDINGUX_ANALOG_UP,
OPENDINGUX_BUTTON_RIGHT | OPENDINGUX_ANALOG_RIGHT,
OPENDINGUX_BUTTON_LEFT | OPENDINGUX_ANALOG_LEFT,
OPENDINGUX_BUTTON_SELECT,
};
// In the same order as MenuKeys above. Maps the OpenDingux buttons to their
// corresponding GUI action.
enum GUI_Action MenuKeysToGUI[7] = {
GUI_ACTION_ENTER,
GUI_ACTION_LEAVE,
GUI_ACTION_DOWN,
GUI_ACTION_UP,
GUI_ACTION_RIGHT,
GUI_ACTION_LEFT,
GUI_ACTION_ALTERNATE,
};
// CurButtons allows only one state change per button relative to LastButtons
// during a frame.
// FutureButtons alows any number of state changes per button.
static enum OpenDingux_Buttons LastButtons = 0, CurButtons = 0, FutureButtons = 0;
static void UpdateOpenDinguxButtons()
{
SDL_Event ev;
uint_fast8_t i;
while (SDL_PollEvent(&ev))
{
switch (ev.type)
{
case SDL_KEYDOWN:
for (i = 0; i < sizeof(OpenDinguxKeys) / sizeof(OpenDinguxKeys[0]); i++)
if (ev.key.keysym.sym == OpenDinguxKeys[i])
{
FutureButtons |= 1 << i;
if ((LastButtons & (1 << i)) == (CurButtons & (1 << i)))
CurButtons |= 1 << i;
break;
}
break;
case SDL_KEYUP:
for (i = 0; i < sizeof(OpenDinguxKeys) / sizeof(OpenDinguxKeys[0]); i++)
if (ev.key.keysym.sym == OpenDinguxKeys[i])
{
FutureButtons &= ~(1 << i);
if ((LastButtons & (1 << i)) == (CurButtons & (1 << i)))
CurButtons &= ~(1 << i);
break;
}
break;
default:
break;
}
}
CurButtons &= ~(OPENDINGUX_ANALOG_LEFT | OPENDINGUX_ANALOG_RIGHT
| OPENDINGUX_ANALOG_UP | OPENDINGUX_ANALOG_DOWN);
int16_t X = GetHorizontalAxisValue(), Y = GetVerticalAxisValue(),
Threshold = (4 - ResolveSetting(AnalogSensitivity, PerGameAnalogSensitivity)) * 7808 + 1024;
if (X > Threshold) CurButtons |= OPENDINGUX_ANALOG_RIGHT;
else if (X < -Threshold) CurButtons |= OPENDINGUX_ANALOG_LEFT;
if (Y > Threshold) CurButtons |= OPENDINGUX_ANALOG_DOWN;
else if (Y < -Threshold) CurButtons |= OPENDINGUX_ANALOG_UP;
}
static bool IsFastForwardToggled = false;
static bool WasFastForwardToggleHeld = false;
static bool WasLoadStateHeld = false;
static bool WasSaveStateHeld = false;
void ProcessSpecialKeys()
{
enum OpenDingux_Buttons ButtonCopy = LastButtons;
enum OpenDingux_Buttons FastForwardToggle = ResolveButtons(Hotkeys[2], PerGameHotkeys[2]);
bool IsFastForwardToggleHeld = FastForwardToggle != 0 && (FastForwardToggle & LastButtons) == FastForwardToggle;
if (!WasFastForwardToggleHeld && IsFastForwardToggleHeld)
IsFastForwardToggled = !IsFastForwardToggled;
WasFastForwardToggleHeld = IsFastForwardToggleHeld;
// Resolve fast-forwarding. It is activated if it's either toggled by the
// Toggle hotkey, or the While Held key is held down.
enum OpenDingux_Buttons FastForwardWhileHeld = ResolveButtons(Hotkeys[0], PerGameHotkeys[0]);
bool IsFastForwardWhileHeld = FastForwardWhileHeld != 0 && (FastForwardWhileHeld & ButtonCopy) == FastForwardWhileHeld;
FastForwardFrameskip = (IsFastForwardToggled || IsFastForwardWhileHeld)
? ResolveSetting(FastForwardTarget, PerGameFastForwardTarget) + 1
: 0;
enum OpenDingux_Buttons LoadState = ResolveButtons(Hotkeys[3], PerGameHotkeys[3]);
bool IsLoadStateHeld = LoadState != 0 && (LoadState & ButtonCopy) == LoadState;
if (!WasLoadStateHeld && IsLoadStateHeld)
{
SetMenuResolution();
switch (load_state(0))
{
case 1:
if (errno != 0)
ShowErrorScreen("Reading saved state #1 failed:\n%s", strerror(errno));
else
ShowErrorScreen("Reading saved state #1 failed:\nIncomplete file");
break;
case 2:
ShowErrorScreen("Reading saved state #1 failed:\nFile format invalid");
break;
}
SetGameResolution();
}
WasLoadStateHeld = IsLoadStateHeld;
enum OpenDingux_Buttons SaveState = ResolveButtons(Hotkeys[4], PerGameHotkeys[4]);
bool IsSaveStateHeld = SaveState != 0 && (SaveState & ButtonCopy) == SaveState;
if (!WasSaveStateHeld && IsSaveStateHeld)
{
void* Screenshot = copy_screen();
SetMenuResolution();
if (Screenshot == NULL)
ShowErrorScreen("Gathering the screenshot for saved state #1 failed: Memory allocation error");
else
{
uint32_t ret = save_state(0, Screenshot /* preserved screenshot */);
free(Screenshot);
if (ret != 1)
{
if (errno != 0)
ShowErrorScreen("Writing saved state #1 failed:\n%s", strerror(errno));
else
ShowErrorScreen("Writing saved state #1 failed:\nUnknown error");
}
}
SetGameResolution();
}
WasSaveStateHeld = IsSaveStateHeld;
}
enum ReGBA_Buttons ReGBA_GetPressedButtons()
{
uint_fast8_t i;
enum ReGBA_Buttons Result = 0;
UpdateOpenDinguxButtons();
LastButtons = CurButtons;
CurButtons = FutureButtons;
ProcessSpecialKeys();
for (i = 0; i < 12; i++)
{
if (LastButtons & ResolveButtons(KeypadRemapping[i], PerGameKeypadRemapping[i]))
{
Result |= 1 << (uint_fast16_t) i;
}
}
if (ResolveSetting(AnalogAction, PerGameAnalogAction) == 1)
{
if (LastButtons & OPENDINGUX_ANALOG_LEFT) Result |= REGBA_BUTTON_LEFT;
if (LastButtons & OPENDINGUX_ANALOG_RIGHT) Result |= REGBA_BUTTON_RIGHT;
if (LastButtons & OPENDINGUX_ANALOG_UP) Result |= REGBA_BUTTON_UP;
if (LastButtons & OPENDINGUX_ANALOG_DOWN) Result |= REGBA_BUTTON_DOWN;
}
if ((Result & REGBA_BUTTON_LEFT) && (Result & REGBA_BUTTON_RIGHT))
Result &= ~REGBA_BUTTON_LEFT;
if ((Result & REGBA_BUTTON_UP) && (Result & REGBA_BUTTON_DOWN))
Result &= ~REGBA_BUTTON_UP;
if (
#if defined GCW_ZERO
// Unified emulator menu buttons: Start+Select
((LastButtons & (OPENDINGUX_BUTTON_START | OPENDINGUX_BUTTON_SELECT)) == (OPENDINGUX_BUTTON_START | OPENDINGUX_BUTTON_SELECT))
#else
// The ReGBA Menu key should be pressed if ONLY the hotkey bound to it
// is pressed on the native device.
// This is not in ProcessSpecialKeys because REGBA_BUTTON_MENU needs to
// be returned by ReGBA_GetPressedButtons.
LastButtons == Hotkeys[1]
#endif
|| (LastButtons & OPENDINGUX_BUTTON_MENU))
Result |= REGBA_BUTTON_MENU;
return Result;
}
bool IsImpossibleHotkey(enum OpenDingux_Buttons Hotkey)
{
if ((Hotkey & (OPENDINGUX_BUTTON_LEFT | OPENDINGUX_BUTTON_RIGHT)) == (OPENDINGUX_BUTTON_LEFT | OPENDINGUX_BUTTON_RIGHT))
return true;
if ((Hotkey & (OPENDINGUX_BUTTON_UP | OPENDINGUX_BUTTON_DOWN)) == (OPENDINGUX_BUTTON_UP | OPENDINGUX_BUTTON_DOWN))
return true;
#if defined DINGOO_A320
if (Hotkey & (OPENDINGUX_ANALOG_LEFT | OPENDINGUX_ANALOG_RIGHT | OPENDINGUX_ANALOG_UP | OPENDINGUX_ANALOG_DOWN))
return true;
#elif defined GCW_ZERO
if ((Hotkey & (OPENDINGUX_ANALOG_LEFT | OPENDINGUX_ANALOG_RIGHT)) == (OPENDINGUX_ANALOG_LEFT | OPENDINGUX_ANALOG_RIGHT))
return true;
if ((Hotkey & (OPENDINGUX_ANALOG_UP | OPENDINGUX_ANALOG_DOWN)) == (OPENDINGUX_ANALOG_UP | OPENDINGUX_ANALOG_DOWN))
return true;
#endif
return false;
}
enum OpenDingux_Buttons GetPressedOpenDinguxButtons()
{
UpdateOpenDinguxButtons();
LastButtons = CurButtons;
CurButtons = FutureButtons;
return LastButtons & ~OPENDINGUX_BUTTON_MENU;
}
static void EnsureJoystick()
{
if (!JoystickInitialised)
{
JoystickInitialised = true;
Joystick = SDL_JoystickOpen(0);
if (Joystick == NULL)
{
ReGBA_Trace("I: Joystick #0 could not be opened");
}
}
}
int16_t GetHorizontalAxisValue()
{
EnsureJoystick();
if (Joystick != NULL)
return SDL_JoystickGetAxis(Joystick, 0);
else
return 0;
}
int16_t GetVerticalAxisValue()
{
EnsureJoystick();
if (Joystick != NULL)
return SDL_JoystickGetAxis(Joystick, 1);
else
return 0;
}
enum GUI_ActionRepeatState
{
BUTTON_NOT_HELD,
BUTTON_HELD_INITIAL,
BUTTON_HELD_REPEAT
};
// Nanoseconds to wait before repeating GUI actions the first time.
static const uint64_t BUTTON_REPEAT_START = 500000000;
// Nanoseconds to wait before repeating GUI actions subsequent times.
static const uint64_t BUTTON_REPEAT_CONTINUE = 100000000;
static enum GUI_ActionRepeatState ActionRepeatState = BUTTON_NOT_HELD;
static struct timespec LastActionRepeat;
static enum GUI_Action LastAction = GUI_ACTION_NONE;
enum GUI_Action GetGUIAction()
{
uint_fast8_t i;
enum GUI_Action Result = GUI_ACTION_NONE;
UpdateOpenDinguxButtons();
LastButtons = CurButtons;
CurButtons = FutureButtons;
enum OpenDingux_Buttons EffectiveButtons = LastButtons;
// Now get the currently-held button with the highest priority in MenuKeys.
for (i = 0; i < sizeof(MenuKeys) / sizeof(MenuKeys[0]); i++)
{
if ((EffectiveButtons & MenuKeys[i]) != 0)
{
Result = MenuKeysToGUI[i];
break;
}
}
struct timespec Now;
clock_gettime(CLOCK_MONOTONIC, &Now);
if (Result == GUI_ACTION_NONE || LastAction != Result || ActionRepeatState == BUTTON_NOT_HELD)
{
LastAction = Result;
LastActionRepeat = Now;
if (Result != GUI_ACTION_NONE)
ActionRepeatState = BUTTON_HELD_INITIAL;
else
ActionRepeatState = BUTTON_NOT_HELD;
return Result;
}
// We are certain of the following here:
// Result != GUI_ACTION_NONE && LastAction == Result
// We need to check how much time has passed since the last repeat.
struct timespec Difference = TimeDifference(LastActionRepeat, Now);
uint64_t DiffNanos = (uint64_t) Difference.tv_sec * 1000000000 + Difference.tv_nsec;
uint64_t RequiredNanos = (ActionRepeatState == BUTTON_HELD_INITIAL)
? BUTTON_REPEAT_START
: BUTTON_REPEAT_CONTINUE;
if (DiffNanos < RequiredNanos)
return GUI_ACTION_NONE;
// Here we repeat the action.
LastActionRepeat = Now;
ActionRepeatState = BUTTON_HELD_REPEAT;
return Result;
}
|