summaryrefslogtreecommitdiffstats
path: root/source/opendingux/od-input.c
blob: a657e77c90b423f64ed2a6e09d75ec7965cdf530 (plain)
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
/* 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 FastForwardTarget = 4; // 6x by default

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[12] = {
	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)
};

// These must be OpenDingux buttons at the bit suitable for the ReGBA_Buttons
// enumeration.
const enum OpenDingux_Buttons DefaultKeypadRemapping[13] = {
	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
	OPENDINGUX_BUTTON_FACE_UP,    // ReGBA Menu
};

// These must be OpenDingux buttons at the bit suitable for the ReGBA_Buttons
// enumeration.
enum OpenDingux_Buttons KeypadRemapping[13] = {
	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
	OPENDINGUX_BUTTON_FACE_UP,    // ReGBA Menu
};

enum OpenDingux_Buttons Hotkeys[1] = {
	0,                            // Fast-forward
};

// 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[6] = {
	OPENDINGUX_BUTTON_FACE_RIGHT,  // Select/Enter button
	OPENDINGUX_BUTTON_FACE_DOWN,   // Cancel/Leave button
	OPENDINGUX_BUTTON_DOWN,        // Menu navigation
	OPENDINGUX_BUTTON_UP,
	OPENDINGUX_BUTTON_RIGHT,
	OPENDINGUX_BUTTON_LEFT,
};

// In the same order as MenuKeys above. Maps the OpenDingux buttons to their
// corresponding GUI action.
enum GUI_Action MenuKeysToGUI[6] = {
	GUI_ACTION_ENTER,
	GUI_ACTION_LEAVE,
	GUI_ACTION_DOWN,
	GUI_ACTION_UP,
	GUI_ACTION_RIGHT,
	GUI_ACTION_LEFT,
};

static enum OpenDingux_Buttons LastButtons = 0;

static void UpdateOpenDinguxButtons()
{
	SDL_Event ev;

	while (SDL_PollEvent(&ev))
	{
		switch (ev.type)
		{
			case SDL_KEYDOWN:
			case SDL_KEYUP:
			{
				uint_fast8_t i;
				for (i = 0; i < sizeof(OpenDinguxKeys) / sizeof(OpenDinguxKeys[0]); i++)
				{
					if (ev.key.keysym.sym == OpenDinguxKeys[i])
					{
						if (ev.type == SDL_KEYDOWN)
							LastButtons |= 1 << (uint_fast16_t) i;
						else
							LastButtons &= ~(1 << (uint_fast16_t) i);
						break;
					}
				}
				break;
			}
			default:
				break;
		}
	}
}

void ProcessSpecialKeys()
{
	FastForwardFrameskip = (Hotkeys[0] != 0 && (Hotkeys[0] & LastButtons) == Hotkeys[0])
		? FastForwardTarget + 1
		: 0;
}

enum ReGBA_Buttons ReGBA_GetPressedButtons()
{
	uint_fast8_t i;
	enum ReGBA_Buttons Result = 0;

	UpdateOpenDinguxButtons();
	ProcessSpecialKeys();
	for (i = 0; i < 13; i++)
	{
		if (LastButtons & KeypadRemapping[i])
		{
			Result |= 1 << (uint_fast16_t) i;
		}
	}
	return Result;
}

enum OpenDingux_Buttons GetPressedOpenDinguxButtons()
{
	UpdateOpenDinguxButtons();
	return LastButtons;
}

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();
	enum OpenDingux_Buttons EffectiveButtons = LastButtons;
	// Incorporate analog nub input. (GCW Zero, implemented as 0 on the
	// Dingoo A320.)
	int16_t X = GetHorizontalAxisValue(), Y = GetVerticalAxisValue();
	     if (X < -32700) EffectiveButtons |= OPENDINGUX_BUTTON_LEFT;
	else if (X >  32700) EffectiveButtons |= OPENDINGUX_BUTTON_RIGHT;
	     if (Y < -32700) EffectiveButtons |= OPENDINGUX_BUTTON_UP;
	else if (Y >  32700) EffectiveButtons |= OPENDINGUX_BUTTON_DOWN;
	// Now get the currently-held button with the highest priority in MenuKeys.
	for (i = 0; i < 6; 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;
}