src/platform/sdl/sdl-events.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "sdl-events.h"
7
8#include "debugger/debugger.h"
9#include "gba/io.h"
10#include "gba/supervisor/rr.h"
11#include "gba/serialize.h"
12#include "gba/video.h"
13#include "gba/renderers/video-software.h"
14#include "util/vfs.h"
15
16#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
17#define GUI_MOD KMOD_GUI
18#else
19#define GUI_MOD KMOD_CTRL
20#endif
21
22static int _openContexts = 0;
23
24bool GBASDLInitEvents(struct GBASDLEvents* context) {
25 if (!_openContexts && SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
26 return false;
27 }
28 ++_openContexts;
29 SDL_JoystickEventState(SDL_ENABLE);
30 context->joystick = SDL_JoystickOpen(0);
31#if !SDL_VERSION_ATLEAST(2, 0, 0)
32 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
33#endif
34 return true;
35}
36
37void GBASDLInitBindings(struct GBAInputMap* inputMap) {
38#ifdef BUILD_PANDORA
39 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
40 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
41 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
42 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
43 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
44 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
45 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
46 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
47 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
48 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
49#elif SDL_VERSION_ATLEAST(2, 0, 0)
50 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
51 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
52 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
53 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
54 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
55 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
56 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
57 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
58 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
59 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
60#else
61 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
62 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
63 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
64 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
65 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
66 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
67 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
68 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
69 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
70 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
71#endif
72
73 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
74 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
75 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
76 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
77 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
78 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
79 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
80 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
81 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
82 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
83
84 struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
85 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
86 description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
87 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
88}
89
90void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
91 GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
92 if (context->joystick) {
93 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
94#if SDL_VERSION_ATLEAST(2, 0, 0)
95 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(context->joystick));
96#else
97 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(SDL_JoystickIndex(context->joystick)));
98#endif
99 }
100}
101
102void GBASDLDeinitEvents(struct GBASDLEvents* context) {
103 SDL_JoystickClose(context->joystick);
104
105 --_openContexts;
106 if (!_openContexts) {
107 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
108 }
109}
110
111static void _pauseAfterFrame(struct GBAThread* context) {
112 context->frameCallback = 0;
113 GBAThreadPauseFromThread(context);
114}
115
116static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
117 enum GBAKey key = GBA_KEY_NONE;
118 if (!event->keysym.mod) {
119#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
120 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
121#else
122 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
123#endif
124 }
125 if (key != GBA_KEY_NONE) {
126 if (event->type == SDL_KEYDOWN) {
127 context->activeKeys |= 1 << key;
128 } else {
129 context->activeKeys &= ~(1 << key);
130 }
131 return;
132 }
133 if (event->keysym.sym == SDLK_TAB) {
134 context->sync.audioWait = event->type != SDL_KEYDOWN;
135 return;
136 }
137 if (event->type == SDL_KEYDOWN) {
138 switch (event->keysym.sym) {
139 case SDLK_F11:
140 if (context->debugger) {
141 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
142 }
143 return;
144#ifdef USE_PNG
145 case SDLK_F12:
146 GBAThreadInterrupt(context);
147 GBAThreadTakeScreenshot(context);
148 GBAThreadContinue(context);
149 return;
150#endif
151 case SDLK_BACKSLASH:
152 GBAThreadPause(context);
153 context->frameCallback = _pauseAfterFrame;
154 GBAThreadUnpause(context);
155 return;
156 case SDLK_BACKQUOTE:
157 GBAThreadInterrupt(context);
158 GBARewind(context, 10);
159 GBAThreadContinue(context);
160 return;
161#ifdef BUILD_PANDORA
162 case SDLK_ESCAPE:
163 GBAThreadEnd(context);
164 return;
165#endif
166 default:
167 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
168 switch (event->keysym.sym) {
169#if SDL_VERSION_ATLEAST(2, 0, 0)
170 case SDLK_f:
171 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
172 sdlContext->fullscreen = !sdlContext->fullscreen;
173 sdlContext->windowUpdated = 1;
174 break;
175#endif
176 case SDLK_p:
177 GBAThreadTogglePause(context);
178 break;
179 case SDLK_n:
180 GBAThreadPause(context);
181 context->frameCallback = _pauseAfterFrame;
182 GBAThreadUnpause(context);
183 break;
184 case SDLK_r:
185 GBAThreadReset(context);
186 break;
187 default:
188 break;
189 }
190 }
191 if (event->keysym.mod & KMOD_SHIFT) {
192 switch (event->keysym.sym) {
193 case SDLK_F1:
194 case SDLK_F2:
195 case SDLK_F3:
196 case SDLK_F4:
197 case SDLK_F5:
198 case SDLK_F6:
199 case SDLK_F7:
200 case SDLK_F8:
201 case SDLK_F9:
202 GBAThreadInterrupt(context);
203 GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
204 GBAThreadContinue(context);
205 break;
206 default:
207 break;
208 }
209 } else {
210 switch (event->keysym.sym) {
211 case SDLK_F1:
212 case SDLK_F2:
213 case SDLK_F3:
214 case SDLK_F4:
215 case SDLK_F5:
216 case SDLK_F6:
217 case SDLK_F7:
218 case SDLK_F8:
219 case SDLK_F9:
220 GBAThreadInterrupt(context);
221 GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
222 GBAThreadContinue(context);
223 break;
224 default:
225 break;
226 }
227 }
228 return;
229 }
230 }
231}
232
233static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyButtonEvent* event) {
234 enum GBAKey key = 0;
235 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
236 if (key == GBA_KEY_NONE) {
237 return;
238 }
239
240 if (event->type == SDL_JOYBUTTONDOWN) {
241 context->activeKeys |= 1 << key;
242 } else {
243 context->activeKeys &= ~(1 << key);
244 }
245}
246
247static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
248 enum GBAKey key = 0;
249
250 if (event->value & SDL_HAT_UP) {
251 key |= 1 << GBA_KEY_UP;
252 }
253 if (event->value & SDL_HAT_LEFT) {
254 key |= 1 << GBA_KEY_LEFT;
255 }
256 if (event->value & SDL_HAT_DOWN) {
257 key |= 1 << GBA_KEY_DOWN;
258 }
259 if (event->value & SDL_HAT_RIGHT) {
260 key |= 1 << GBA_KEY_RIGHT;
261 }
262
263 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
264 context->activeKeys |= key;
265}
266
267static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyAxisEvent* event) {
268 int keys = context->activeKeys;
269
270 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
271 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
272 if (key != GBA_KEY_NONE) {
273 keys |= 1 << key;
274 }
275
276 context->activeKeys = keys;
277}
278
279#if SDL_VERSION_ATLEAST(2, 0, 0)
280static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
281 UNUSED(context);
282 switch (event->event) {
283 case SDL_WINDOWEVENT_SIZE_CHANGED:
284 sdlContext->windowUpdated = 1;
285 break;
286 }
287}
288#endif
289
290void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
291 switch (event->type) {
292 case SDL_QUIT:
293 GBAThreadEnd(context);
294 break;
295#if SDL_VERSION_ATLEAST(2, 0, 0)
296 case SDL_WINDOWEVENT:
297 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
298 break;
299#endif
300 case SDL_KEYDOWN:
301 case SDL_KEYUP:
302 _GBASDLHandleKeypress(context, sdlContext, &event->key);
303 break;
304 case SDL_JOYBUTTONDOWN:
305 case SDL_JOYBUTTONUP:
306 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
307 break;
308 case SDL_JOYHATMOTION:
309 _GBASDLHandleJoyHat(context, &event->jhat);
310 break;
311 case SDL_JOYAXISMOTION:
312 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
313 break;
314 }
315}