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 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
82}
83
84void GBASDLDeinitEvents(struct GBASDLEvents* context) {
85 SDL_JoystickClose(context->joystick);
86
87 --_openContexts;
88 if (!_openContexts) {
89 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
90 }
91}
92
93static void _pauseAfterFrame(struct GBAThread* context) {
94 context->frameCallback = 0;
95 GBAThreadPauseFromThread(context);
96}
97
98static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
99 enum GBAKey key = GBA_KEY_NONE;
100 if (!event->keysym.mod) {
101#if SDL_VERSION_ATLEAST(2, 0, 0)
102 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
103#else
104 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
105#endif
106 }
107 if (key != GBA_KEY_NONE) {
108 if (event->type == SDL_KEYDOWN) {
109 context->activeKeys |= 1 << key;
110 } else {
111 context->activeKeys &= ~(1 << key);
112 }
113 return;
114 }
115 if (event->keysym.sym == SDLK_TAB) {
116 context->sync.audioWait = event->type != SDL_KEYDOWN;
117 return;
118 }
119 if (event->type == SDL_KEYDOWN) {
120 switch (event->keysym.sym) {
121 case SDLK_F11:
122 if (context->debugger) {
123 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
124 }
125 return;
126#ifdef USE_PNG
127 case SDLK_F12:
128 GBAThreadInterrupt(context);
129 GBAThreadTakeScreenshot(context);
130 GBAThreadContinue(context);
131 return;
132#endif
133 case SDLK_BACKSLASH:
134 GBAThreadPause(context);
135 context->frameCallback = _pauseAfterFrame;
136 GBAThreadUnpause(context);
137 return;
138 case SDLK_BACKQUOTE:
139 GBAThreadInterrupt(context);
140 GBARewind(context, 10);
141 GBAThreadContinue(context);
142 return;
143 default:
144 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
145 switch (event->keysym.sym) {
146#if SDL_VERSION_ATLEAST(2, 0, 0)
147 case SDLK_f:
148 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
149 sdlContext->fullscreen = !sdlContext->fullscreen;
150 sdlContext->windowUpdated = 1;
151 break;
152#endif
153 case SDLK_p:
154 GBAThreadTogglePause(context);
155 break;
156 case SDLK_n:
157 GBAThreadPause(context);
158 context->frameCallback = _pauseAfterFrame;
159 GBAThreadUnpause(context);
160 break;
161 case SDLK_r:
162 GBAThreadReset(context);
163 break;
164 default:
165 break;
166 }
167 }
168 if (event->keysym.mod & KMOD_SHIFT) {
169 switch (event->keysym.sym) {
170 case SDLK_F1:
171 case SDLK_F2:
172 case SDLK_F3:
173 case SDLK_F4:
174 case SDLK_F5:
175 case SDLK_F6:
176 case SDLK_F7:
177 case SDLK_F8:
178 case SDLK_F9:
179 GBAThreadInterrupt(context);
180 GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
181 GBAThreadContinue(context);
182 break;
183 default:
184 break;
185 }
186 } else {
187 switch (event->keysym.sym) {
188 case SDLK_F1:
189 case SDLK_F2:
190 case SDLK_F3:
191 case SDLK_F4:
192 case SDLK_F5:
193 case SDLK_F6:
194 case SDLK_F7:
195 case SDLK_F8:
196 case SDLK_F9:
197 GBAThreadInterrupt(context);
198 GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
199 GBAThreadContinue(context);
200 break;
201 default:
202 break;
203 }
204 }
205 return;
206 }
207 }
208}
209
210static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyButtonEvent* event) {
211 enum GBAKey key = 0;
212 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
213 if (key == GBA_KEY_NONE) {
214 return;
215 }
216
217 if (event->type == SDL_JOYBUTTONDOWN) {
218 context->activeKeys |= 1 << key;
219 } else {
220 context->activeKeys &= ~(1 << key);
221 }
222}
223
224static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
225 enum GBAKey key = 0;
226
227 if (event->value & SDL_HAT_UP) {
228 key |= 1 << GBA_KEY_UP;
229 }
230 if (event->value & SDL_HAT_LEFT) {
231 key |= 1 << GBA_KEY_LEFT;
232 }
233 if (event->value & SDL_HAT_DOWN) {
234 key |= 1 << GBA_KEY_DOWN;
235 }
236 if (event->value & SDL_HAT_RIGHT) {
237 key |= 1 << GBA_KEY_RIGHT;
238 }
239
240 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
241 context->activeKeys |= key;
242}
243
244static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyAxisEvent* event) {
245 int keys = context->activeKeys;
246
247 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
248 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
249 if (key != GBA_KEY_NONE) {
250 keys |= 1 << key;
251 }
252
253 context->activeKeys = keys;
254}
255
256#if SDL_VERSION_ATLEAST(2, 0, 0)
257static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
258 UNUSED(context);
259 switch (event->event) {
260 case SDL_WINDOWEVENT_SIZE_CHANGED:
261 sdlContext->windowUpdated = 1;
262 break;
263 }
264}
265#endif
266
267void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
268 switch (event->type) {
269 case SDL_QUIT:
270 GBAThreadEnd(context);
271 break;
272#if SDL_VERSION_ATLEAST(2, 0, 0)
273 case SDL_WINDOWEVENT:
274 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
275 break;
276#endif
277 case SDL_KEYDOWN:
278 case SDL_KEYUP:
279 _GBASDLHandleKeypress(context, sdlContext, &event->key);
280 break;
281 case SDL_JOYBUTTONDOWN:
282 case SDL_JOYBUTTONUP:
283 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
284 break;
285 case SDL_JOYHATMOTION:
286 _GBASDLHandleJoyHat(context, &event->jhat);
287 break;
288 case SDL_JOYAXISMOTION:
289 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
290 break;
291 }
292}