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/configuration.h"
15#include "util/vfs.h"
16
17#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
18#define GUI_MOD KMOD_GUI
19#else
20#define GUI_MOD KMOD_CTRL
21#endif
22
23static void _GBASDLSetRumble(struct GBARumble* rumble, int enable);
24
25bool GBASDLInitEvents(struct GBASDLEvents* context) {
26 int subsystem = SDL_INIT_JOYSTICK;
27#if SDL_VERSION_ATLEAST(2, 0, 0)
28 subsystem |= SDL_INIT_HAPTIC;
29#endif
30 if (SDL_InitSubSystem(subsystem) < 0) {
31 return false;
32 }
33
34 SDL_JoystickEventState(SDL_ENABLE);
35 int nJoysticks = SDL_NumJoysticks();
36 if (nJoysticks > 0) {
37 context->nJoysticks = nJoysticks;
38 context->joysticks = calloc(context->nJoysticks, sizeof(SDL_Joystick*));
39#if SDL_VERSION_ATLEAST(2, 0, 0)
40 context->haptic = calloc(context->nJoysticks, sizeof(SDL_Haptic*));
41#endif
42 size_t i;
43 for (i = 0; i < context->nJoysticks; ++i) {
44 context->joysticks[i] = SDL_JoystickOpen(i);
45#if SDL_VERSION_ATLEAST(2, 0, 0)
46 context->haptic[i] = SDL_HapticOpenFromJoystick(context->joysticks[i]);
47#endif
48 }
49 } else {
50 context->nJoysticks = 0;
51 context->joysticks = 0;
52 }
53
54 context->playersAttached = 0;
55
56 size_t i;
57 for (i = 0; i < MAX_PLAYERS; ++i) {
58 context->preferredJoysticks[i] = 0;
59 context->joysticksClaimed[i] = SIZE_MAX;
60 }
61
62#if !SDL_VERSION_ATLEAST(2, 0, 0)
63 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
64#endif
65 return true;
66}
67
68void GBASDLDeinitEvents(struct GBASDLEvents* context) {
69 size_t i;
70 for (i = 0; i < context->nJoysticks; ++i) {
71#if SDL_VERSION_ATLEAST(2, 0, 0)
72 SDL_HapticClose(context->haptic[i]);
73#endif
74 SDL_JoystickClose(context->joysticks[i]);
75 }
76
77 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
78}
79
80void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
81 context->preferredJoysticks[0] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 0);
82 context->preferredJoysticks[1] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 1);
83 context->preferredJoysticks[2] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 2);
84 context->preferredJoysticks[3] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 3);
85}
86
87void GBASDLInitBindings(struct GBAInputMap* inputMap) {
88#ifdef BUILD_PANDORA
89 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
90 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
91 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
92 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
93 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
94 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
95 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
96 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
97 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
98 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
99#elif SDL_VERSION_ATLEAST(2, 0, 0)
100 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
101 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
102 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
103 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
104 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
105 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
106 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
107 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
108 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
109 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
110#else
111 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
112 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
113 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
114 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
115 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
116 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
117 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
118 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
119 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
120 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
121#endif
122
123 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
124 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
125 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
126 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
127 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
128 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
129 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
130 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
131 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
132 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
133
134 struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
135 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
136 description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
137 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
138}
139
140bool GBASDLAttachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) {
141 player->joystick = 0;
142 player->joystickIndex = SIZE_MAX;
143
144#if SDL_VERSION_ATLEAST(2, 0, 0)
145 player->rumble.d.setRumble = _GBASDLSetRumble;
146 player->rumble.p = player;
147#endif
148
149 if (events->playersAttached >= MAX_PLAYERS) {
150 return false;
151 }
152
153 player->playerId = events->playersAttached;
154 size_t firstUnclaimed = SIZE_MAX;
155
156 size_t i;
157 for (i = 0; i < events->nJoysticks; ++i) {
158 bool claimed = false;
159
160 int p;
161 for (p = 0; p < events->playersAttached; ++p) {
162 if (events->joysticksClaimed[p] == i) {
163 claimed = true;
164 break;
165 }
166 }
167 if (claimed) {
168 continue;
169 }
170
171 if (firstUnclaimed == SIZE_MAX) {
172 firstUnclaimed = i;
173 }
174
175 const char* joystickName;
176#if SDL_VERSION_ATLEAST(2, 0, 0)
177 joystickName = SDL_JoystickName(events->joysticks[i]);
178#else
179 joystickName = SDL_JoystickName(SDL_JoystickIndex(events->joysticks[i]));
180#endif
181 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
182 player->joystickIndex = i;
183 break;
184 }
185 }
186
187 if (player->joystickIndex == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
188 player->joystickIndex = firstUnclaimed;
189 }
190
191 if (player->joystickIndex != SIZE_MAX) {
192 player->joystick = events->joysticks[player->joystickIndex];
193 events->joysticksClaimed[player->playerId] = player->joystickIndex;
194
195#if SDL_VERSION_ATLEAST(2, 0, 0)
196 player->haptic = events->haptic[player->joystickIndex];
197 if (player->haptic) {
198 SDL_HapticRumbleInit(player->haptic);
199 }
200#endif
201 }
202
203 ++events->playersAttached;
204 return true;
205}
206
207void GBASDLPlayerLoadConfig(struct GBASDLPlayer* context, const struct Configuration* config) {
208 GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
209 if (context->joystick) {
210 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
211#if SDL_VERSION_ATLEAST(2, 0, 0)
212 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(context->joystick));
213#else
214 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(SDL_JoystickIndex(context->joystick)));
215#endif
216 }
217}
218
219void GBASDLPlayerChangeJoystick(struct GBASDLEvents* events, struct GBASDLPlayer* player, size_t index) {
220 if (player->playerId > MAX_PLAYERS || index >= events->nJoysticks) {
221 return;
222 }
223 events->joysticksClaimed[player->playerId] = index;
224 player->joystickIndex = index;
225 player->joystick = events->joysticks[index];
226#if SDL_VERSION_ATLEAST(2, 0, 0)
227 player->haptic = events->haptic[index];
228#endif
229}
230
231static void _pauseAfterFrame(struct GBAThread* context) {
232 context->frameCallback = 0;
233 GBAThreadPauseFromThread(context);
234}
235
236static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
237 enum GBAKey key = GBA_KEY_NONE;
238 if (!event->keysym.mod) {
239#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
240 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
241#else
242 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
243#endif
244 }
245 if (key != GBA_KEY_NONE) {
246 if (event->type == SDL_KEYDOWN) {
247 context->activeKeys |= 1 << key;
248 } else {
249 context->activeKeys &= ~(1 << key);
250 }
251 return;
252 }
253 if (event->keysym.sym == SDLK_TAB) {
254 context->sync.audioWait = event->type != SDL_KEYDOWN;
255 return;
256 }
257 if (event->type == SDL_KEYDOWN) {
258 switch (event->keysym.sym) {
259 case SDLK_F11:
260 if (context->debugger) {
261 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
262 }
263 return;
264#ifdef USE_PNG
265 case SDLK_F12:
266 GBAThreadInterrupt(context);
267 GBAThreadTakeScreenshot(context);
268 GBAThreadContinue(context);
269 return;
270#endif
271 case SDLK_BACKSLASH:
272 GBAThreadPause(context);
273 context->frameCallback = _pauseAfterFrame;
274 GBAThreadUnpause(context);
275 return;
276 case SDLK_BACKQUOTE:
277 GBAThreadInterrupt(context);
278 GBARewind(context, 10);
279 GBAThreadContinue(context);
280 return;
281#ifdef BUILD_PANDORA
282 case SDLK_ESCAPE:
283 GBAThreadEnd(context);
284 return;
285#endif
286 default:
287 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
288 switch (event->keysym.sym) {
289#if SDL_VERSION_ATLEAST(2, 0, 0)
290 case SDLK_f:
291 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
292 sdlContext->fullscreen = !sdlContext->fullscreen;
293 sdlContext->windowUpdated = 1;
294 break;
295#endif
296 case SDLK_p:
297 GBAThreadTogglePause(context);
298 break;
299 case SDLK_n:
300 GBAThreadPause(context);
301 context->frameCallback = _pauseAfterFrame;
302 GBAThreadUnpause(context);
303 break;
304 case SDLK_r:
305 GBAThreadReset(context);
306 break;
307 default:
308 break;
309 }
310 }
311 if (event->keysym.mod & KMOD_SHIFT) {
312 switch (event->keysym.sym) {
313 case SDLK_F1:
314 case SDLK_F2:
315 case SDLK_F3:
316 case SDLK_F4:
317 case SDLK_F5:
318 case SDLK_F6:
319 case SDLK_F7:
320 case SDLK_F8:
321 case SDLK_F9:
322 GBAThreadInterrupt(context);
323 GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
324 GBAThreadContinue(context);
325 break;
326 default:
327 break;
328 }
329 } else {
330 switch (event->keysym.sym) {
331 case SDLK_F1:
332 case SDLK_F2:
333 case SDLK_F3:
334 case SDLK_F4:
335 case SDLK_F5:
336 case SDLK_F6:
337 case SDLK_F7:
338 case SDLK_F8:
339 case SDLK_F9:
340 GBAThreadInterrupt(context);
341 GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
342 GBAThreadContinue(context);
343 break;
344 default:
345 break;
346 }
347 }
348 return;
349 }
350 }
351}
352
353static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
354 enum GBAKey key = 0;
355 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
356 if (key == GBA_KEY_NONE) {
357 return;
358 }
359
360 if (event->type == SDL_JOYBUTTONDOWN) {
361 context->activeKeys |= 1 << key;
362 } else {
363 context->activeKeys &= ~(1 << key);
364 }
365}
366
367static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
368 enum GBAKey key = 0;
369
370 if (event->value & SDL_HAT_UP) {
371 key |= 1 << GBA_KEY_UP;
372 }
373 if (event->value & SDL_HAT_LEFT) {
374 key |= 1 << GBA_KEY_LEFT;
375 }
376 if (event->value & SDL_HAT_DOWN) {
377 key |= 1 << GBA_KEY_DOWN;
378 }
379 if (event->value & SDL_HAT_RIGHT) {
380 key |= 1 << GBA_KEY_RIGHT;
381 }
382
383 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
384 context->activeKeys |= key;
385}
386
387static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
388 int keys = context->activeKeys;
389
390 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
391 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
392 if (key != GBA_KEY_NONE) {
393 keys |= 1 << key;
394 }
395
396 context->activeKeys = keys;
397}
398
399#if SDL_VERSION_ATLEAST(2, 0, 0)
400static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
401 UNUSED(context);
402 switch (event->event) {
403 case SDL_WINDOWEVENT_SIZE_CHANGED:
404 sdlContext->windowUpdated = 1;
405 break;
406 }
407}
408#endif
409
410void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const union SDL_Event* event) {
411 switch (event->type) {
412 case SDL_QUIT:
413 GBAThreadEnd(context);
414 break;
415#if SDL_VERSION_ATLEAST(2, 0, 0)
416 case SDL_WINDOWEVENT:
417 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
418 break;
419#endif
420 case SDL_KEYDOWN:
421 case SDL_KEYUP:
422 _GBASDLHandleKeypress(context, sdlContext, &event->key);
423 break;
424 case SDL_JOYBUTTONDOWN:
425 case SDL_JOYBUTTONUP:
426 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
427 break;
428 case SDL_JOYHATMOTION:
429 _GBASDLHandleJoyHat(context, &event->jhat);
430 break;
431 case SDL_JOYAXISMOTION:
432 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
433 break;
434 }
435}
436
437#if SDL_VERSION_ATLEAST(2, 0, 0)
438static void _GBASDLSetRumble(struct GBARumble* rumble, int enable) {
439 struct GBASDLRumble* sdlRumble = (struct GBASDLRumble*) rumble;
440 if (!sdlRumble->p->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->haptic)) {
441 return;
442 }
443 if (enable) {
444 SDL_HapticRumblePlay(sdlRumble->p->haptic, 1.0f, 20);
445 } else {
446 SDL_HapticRumbleStop(sdlRumble->p->haptic);
447 }
448}
449#endif