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#if SDL_VERSION_ATLEAST(2, 0, 0)
39 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
40 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
41 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
42 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
43 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
44 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
45 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
46 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
47 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
48 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
49#else
50 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
51 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
52 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
53 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
54 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
55 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
56 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
57 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
58 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
59 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
60#endif
61
62 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
63 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
64 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
65 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
66 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
67 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
68 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
69 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
70 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
71 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
72
73 struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
74 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
75 description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
76 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
77}
78
79void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
80 GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
81 if (context->joystick) {
82 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
83#if SDL_VERSION_ATLEAST(2, 0, 0)
84 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(context->joystick));
85#else
86 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(SDL_JoystickIndex(context->joystick)));
87#endif
88 }
89}
90
91void GBASDLDeinitEvents(struct GBASDLEvents* context) {
92 SDL_JoystickClose(context->joystick);
93
94 --_openContexts;
95 if (!_openContexts) {
96 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
97 }
98}
99
100static void _pauseAfterFrame(struct GBAThread* context) {
101 context->frameCallback = 0;
102 GBAThreadPauseFromThread(context);
103}
104
105static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
106 enum GBAKey key = GBA_KEY_NONE;
107 if (!event->keysym.mod) {
108#if SDL_VERSION_ATLEAST(2, 0, 0)
109 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
110#else
111 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
112#endif
113 }
114 if (key != GBA_KEY_NONE) {
115 if (event->type == SDL_KEYDOWN) {
116 context->activeKeys |= 1 << key;
117 } else {
118 context->activeKeys &= ~(1 << key);
119 }
120 return;
121 }
122 if (event->keysym.sym == SDLK_TAB) {
123 context->sync.audioWait = event->type != SDL_KEYDOWN;
124 return;
125 }
126 if (event->type == SDL_KEYDOWN) {
127 switch (event->keysym.sym) {
128 case SDLK_F11:
129 if (context->debugger) {
130 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
131 }
132 return;
133#ifdef USE_PNG
134 case SDLK_F12:
135 GBAThreadInterrupt(context);
136 GBAThreadTakeScreenshot(context);
137 GBAThreadContinue(context);
138 return;
139#endif
140 case SDLK_BACKSLASH:
141 GBAThreadPause(context);
142 context->frameCallback = _pauseAfterFrame;
143 GBAThreadUnpause(context);
144 return;
145 case SDLK_BACKQUOTE:
146 GBAThreadInterrupt(context);
147 GBARewind(context, 10);
148 GBAThreadContinue(context);
149 return;
150 default:
151 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
152 switch (event->keysym.sym) {
153#if SDL_VERSION_ATLEAST(2, 0, 0)
154 case SDLK_f:
155 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
156 sdlContext->fullscreen = !sdlContext->fullscreen;
157 sdlContext->windowUpdated = 1;
158 break;
159#endif
160 case SDLK_p:
161 GBAThreadTogglePause(context);
162 break;
163 case SDLK_n:
164 GBAThreadPause(context);
165 context->frameCallback = _pauseAfterFrame;
166 GBAThreadUnpause(context);
167 break;
168 case SDLK_r:
169 GBAThreadReset(context);
170 break;
171 default:
172 break;
173 }
174 }
175 if (event->keysym.mod & KMOD_SHIFT) {
176 switch (event->keysym.sym) {
177 case SDLK_F1:
178 case SDLK_F2:
179 case SDLK_F3:
180 case SDLK_F4:
181 case SDLK_F5:
182 case SDLK_F6:
183 case SDLK_F7:
184 case SDLK_F8:
185 case SDLK_F9:
186 GBAThreadInterrupt(context);
187 GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
188 GBAThreadContinue(context);
189 break;
190 default:
191 break;
192 }
193 } else {
194 switch (event->keysym.sym) {
195 case SDLK_F1:
196 case SDLK_F2:
197 case SDLK_F3:
198 case SDLK_F4:
199 case SDLK_F5:
200 case SDLK_F6:
201 case SDLK_F7:
202 case SDLK_F8:
203 case SDLK_F9:
204 GBAThreadInterrupt(context);
205 GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
206 GBAThreadContinue(context);
207 break;
208 default:
209 break;
210 }
211 }
212 return;
213 }
214 }
215}
216
217static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyButtonEvent* event) {
218 enum GBAKey key = 0;
219 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
220 if (key == GBA_KEY_NONE) {
221 return;
222 }
223
224 if (event->type == SDL_JOYBUTTONDOWN) {
225 context->activeKeys |= 1 << key;
226 } else {
227 context->activeKeys &= ~(1 << key);
228 }
229}
230
231static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
232 enum GBAKey key = 0;
233
234 if (event->value & SDL_HAT_UP) {
235 key |= 1 << GBA_KEY_UP;
236 }
237 if (event->value & SDL_HAT_LEFT) {
238 key |= 1 << GBA_KEY_LEFT;
239 }
240 if (event->value & SDL_HAT_DOWN) {
241 key |= 1 << GBA_KEY_DOWN;
242 }
243 if (event->value & SDL_HAT_RIGHT) {
244 key |= 1 << GBA_KEY_RIGHT;
245 }
246
247 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
248 context->activeKeys |= key;
249}
250
251static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyAxisEvent* event) {
252 int keys = context->activeKeys;
253
254 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
255 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
256 if (key != GBA_KEY_NONE) {
257 keys |= 1 << key;
258 }
259
260 context->activeKeys = keys;
261}
262
263#if SDL_VERSION_ATLEAST(2, 0, 0)
264static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
265 UNUSED(context);
266 switch (event->event) {
267 case SDL_WINDOWEVENT_SIZE_CHANGED:
268 sdlContext->windowUpdated = 1;
269 break;
270 }
271}
272#endif
273
274void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
275 switch (event->type) {
276 case SDL_QUIT:
277 GBAThreadEnd(context);
278 break;
279#if SDL_VERSION_ATLEAST(2, 0, 0)
280 case SDL_WINDOWEVENT:
281 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
282 break;
283#endif
284 case SDL_KEYDOWN:
285 case SDL_KEYUP:
286 _GBASDLHandleKeypress(context, sdlContext, &event->key);
287 break;
288 case SDL_JOYBUTTONDOWN:
289 case SDL_JOYBUTTONUP:
290 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
291 break;
292 case SDL_JOYHATMOTION:
293 _GBASDLHandleJoyHat(context, &event->jhat);
294 break;
295 case SDL_JOYAXISMOTION:
296 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
297 break;
298 }
299}