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 "core/input.h"
9#include "core/thread.h"
10#include "debugger/debugger.h"
11#include "gba/input.h"
12#include "gba/io.h"
13#include "gba/rr/rr.h"
14#include "gba/serialize.h"
15#include "gba/supervisor/thread.h"
16#include "gba/video.h"
17#include "gba/renderers/video-software.h"
18#include "util/configuration.h"
19#include "util/formatting.h"
20#include "util/vfs.h"
21
22#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
23#define GUI_MOD KMOD_GUI
24#else
25#define GUI_MOD KMOD_CTRL
26#endif
27
28#define GYRO_STEPS 100
29#define RUMBLE_PWM 20
30
31DEFINE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo);
32
33#if SDL_VERSION_ATLEAST(2, 0, 0)
34static void _mSDLSetRumble(struct mRumble* rumble, int enable);
35#endif
36static int32_t _mSDLReadTiltX(struct mRotationSource* rumble);
37static int32_t _mSDLReadTiltY(struct mRotationSource* rumble);
38static int32_t _mSDLReadGyroZ(struct mRotationSource* rumble);
39static void _mSDLRotationSample(struct mRotationSource* source);
40
41bool mSDLInitEvents(struct mSDLEvents* context) {
42#if SDL_VERSION_ATLEAST(2, 0, 4)
43 SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
44#endif
45 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
46 GBALog(0, GBA_LOG_ERROR, "SDL joystick initialization failed: %s", SDL_GetError());
47 }
48
49#if SDL_VERSION_ATLEAST(2, 0, 0)
50 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
51 if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
52 GBALog(0, GBA_LOG_ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
53 }
54 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
55 GBALog(0, GBA_LOG_ERROR, "SDL video initialization failed: %s", SDL_GetError());
56 }
57#endif
58
59 SDL_JoystickEventState(SDL_ENABLE);
60 int nJoysticks = SDL_NumJoysticks();
61 SDL_JoystickListInit(&context->joysticks, nJoysticks);
62 if (nJoysticks > 0) {
63 mSDLUpdateJoysticks(context);
64 }
65
66 context->playersAttached = 0;
67
68 size_t i;
69 for (i = 0; i < MAX_PLAYERS; ++i) {
70 context->preferredJoysticks[i] = 0;
71 }
72
73#if !SDL_VERSION_ATLEAST(2, 0, 0)
74 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
75#else
76 context->screensaverSuspendDepth = 0;
77#endif
78 return true;
79}
80
81void mSDLDeinitEvents(struct mSDLEvents* context) {
82 size_t i;
83 for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) {
84 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
85#if SDL_VERSION_ATLEAST(2, 0, 0)
86 SDL_HapticClose(joystick->haptic);
87#endif
88 SDL_JoystickClose(joystick->joystick);
89 }
90 SDL_JoystickListDeinit(&context->joysticks);
91 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
92}
93
94void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration* config) {
95 context->preferredJoysticks[0] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 0);
96 context->preferredJoysticks[1] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 1);
97 context->preferredJoysticks[2] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 2);
98 context->preferredJoysticks[3] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 3);
99}
100
101void mSDLInitBindingsGBA(struct mInputMap* inputMap) {
102#ifdef BUILD_PANDORA
103 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
104 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
105 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
106 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
107 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
108 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
109 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
110 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
111 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
112 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
113#elif SDL_VERSION_ATLEAST(2, 0, 0)
114 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
115 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
116 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
117 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
118 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
119 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
120 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
121 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
122 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
123 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
124#else
125 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
126 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
127 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
128 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
129 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
130 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
131 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
132 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
133 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
134 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
135#endif
136
137 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
138 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
139 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
140 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
141 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
142 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
143 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
144 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
145 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
146 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
147
148 struct mInputAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
149 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
150 description = (struct mInputAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
151 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
152}
153
154bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
155 player->joystick = 0;
156
157 if (events->playersAttached >= MAX_PLAYERS) {
158 return false;
159 }
160
161#if SDL_VERSION_ATLEAST(2, 0, 0)
162 player->rumble.d.setRumble = _mSDLSetRumble;
163 CircleBufferInit(&player->rumble.history, RUMBLE_PWM);
164 player->rumble.level = 0;
165 player->rumble.p = player;
166#endif
167
168 player->rotation.d.readTiltX = _mSDLReadTiltX;
169 player->rotation.d.readTiltY = _mSDLReadTiltY;
170 player->rotation.d.readGyroZ = _mSDLReadGyroZ;
171 player->rotation.d.sample = _mSDLRotationSample;
172 player->rotation.axisX = 2;
173 player->rotation.axisY = 3;
174 player->rotation.gyroSensitivity = 2.2e9f;
175 player->rotation.gyroX = 0;
176 player->rotation.gyroY = 1;
177 player->rotation.zDelta = 0;
178 CircleBufferInit(&player->rotation.zHistory, sizeof(float) * GYRO_STEPS);
179 player->rotation.p = player;
180
181 player->playerId = events->playersAttached;
182 events->players[player->playerId] = player;
183 size_t firstUnclaimed = SIZE_MAX;
184 size_t index = SIZE_MAX;
185
186 size_t i;
187 for (i = 0; i < SDL_JoystickListSize(&events->joysticks); ++i) {
188 bool claimed = false;
189
190 int p;
191 for (p = 0; p < events->playersAttached; ++p) {
192 if (events->players[p]->joystick == SDL_JoystickListGetPointer(&events->joysticks, i)) {
193 claimed = true;
194 break;
195 }
196 }
197 if (claimed) {
198 continue;
199 }
200
201 if (firstUnclaimed == SIZE_MAX) {
202 firstUnclaimed = i;
203 }
204
205 const char* joystickName;
206#if SDL_VERSION_ATLEAST(2, 0, 0)
207 joystickName = SDL_JoystickName(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick);
208#else
209 joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick));
210#endif
211 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
212 index = i;
213 break;
214 }
215 }
216
217 if (index == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
218 index = firstUnclaimed;
219 }
220
221 if (index != SIZE_MAX) {
222 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
223
224#if SDL_VERSION_ATLEAST(2, 0, 0)
225 if (player->joystick->haptic) {
226 SDL_HapticRumbleInit(player->joystick->haptic);
227 }
228#endif
229 }
230
231 ++events->playersAttached;
232 return true;
233}
234
235void mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
236 if (player != events->players[player->playerId]) {
237 return;
238 }
239 int i;
240 for (i = player->playerId; i < events->playersAttached; ++i) {
241 if (i + 1 < MAX_PLAYERS) {
242 events->players[i] = events->players[i + 1];
243 }
244 if (i < events->playersAttached - 1) {
245 events->players[i]->playerId = i;
246 }
247 }
248 --events->playersAttached;
249 CircleBufferDeinit(&player->rotation.zHistory);
250}
251
252void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) {
253 mInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
254 if (context->joystick) {
255 mInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
256#if SDL_VERSION_ATLEAST(2, 0, 0)
257 const char* name = SDL_JoystickName(context->joystick->joystick);
258#else
259 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
260#endif
261 mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
262
263 const char* value;
264 char* end;
265 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
266 int axis;
267 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", name);
268 if (value) {
269 axis = strtol(value, &end, 0);
270 if (axis >= 0 && axis < numAxes && end && !*end) {
271 context->rotation.axisX = axis;
272 }
273 }
274 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", name);
275 if (value) {
276 axis = strtol(value, &end, 0);
277 if (axis >= 0 && axis < numAxes && end && !*end) {
278 context->rotation.axisY = axis;
279 }
280 }
281 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", name);
282 if (value) {
283 axis = strtol(value, &end, 0);
284 if (axis >= 0 && axis < numAxes && end && !*end) {
285 context->rotation.gyroX = axis;
286 }
287 }
288 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", name);
289 if (value) {
290 axis = strtol(value, &end, 0);
291 if (axis >= 0 && axis < numAxes && end && !*end) {
292 context->rotation.gyroY = axis;
293 }
294 }
295 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
296 if (value) {
297 float sensitivity = strtof_u(value, &end);
298 if (end && !*end) {
299 context->rotation.gyroSensitivity = sensitivity;
300 }
301 }
302 }
303}
304
305void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) {
306 if (context->joystick) {
307#if SDL_VERSION_ATLEAST(2, 0, 0)
308 const char* name = SDL_JoystickName(context->joystick->joystick);
309#else
310 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
311#endif
312 char value[12];
313 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
314 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
315 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
316 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
317 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
318 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
319 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
320 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
321 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
322 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
323 }
324}
325
326void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) {
327 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
328 return;
329 }
330 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
331}
332
333void mSDLUpdateJoysticks(struct mSDLEvents* events) {
334 // Pump SDL joystick events without eating the rest of the events
335 SDL_JoystickUpdate();
336#if SDL_VERSION_ATLEAST(2, 0, 0)
337 SDL_Event event;
338 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
339 if (event.type == SDL_JOYDEVICEADDED) {
340 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
341 joystick->joystick = SDL_JoystickOpen(event.jdevice.which);
342 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
343 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
344#if SDL_VERSION_ATLEAST(2, 0, 0)
345 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
346#endif
347 } else if (event.type == SDL_JOYDEVICEREMOVED) {
348 SDL_JoystickID ids[MAX_PLAYERS];
349 size_t i;
350 for (i = 0; (int) i < events->playersAttached; ++i) {
351 if (events->players[i]->joystick) {
352 ids[i] = events->players[i]->joystick->id;
353 events->players[i]->joystick = 0;
354 } else {
355 ids[i] = -1;
356 }
357 }
358 for (i = 0; i < SDL_JoystickListSize(&events->joysticks);) {
359 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
360 if (joystick->id == event.jdevice.which) {
361 SDL_JoystickListShift(&events->joysticks, i, 1);
362 continue;
363 }
364 SDL_JoystickListGetPointer(&events->joysticks, i)->index = i;
365 int p;
366 for (p = 0; p < events->playersAttached; ++p) {
367 if (joystick->id == ids[p]) {
368 events->players[p]->joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
369 }
370 }
371 ++i;
372 }
373 }
374 }
375#endif
376}
377
378static void _pauseAfterFrame(struct GBAThread* context) {
379 context->frameCallback = 0;
380 GBAThreadPauseFromThread(context);
381}
382
383static void _mSDLHandleKeypressGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
384 enum GBAKey key = GBA_KEY_NONE;
385 if (!event->keysym.mod) {
386#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
387 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
388#else
389 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
390#endif
391 }
392 if (key != GBA_KEY_NONE) {
393 if (event->type == SDL_KEYDOWN) {
394 context->activeKeys |= 1 << key;
395 } else {
396 context->activeKeys &= ~(1 << key);
397 }
398 return;
399 }
400 if (event->keysym.sym == SDLK_TAB) {
401 context->sync.audioWait = event->type != SDL_KEYDOWN;
402 return;
403 }
404 if (event->type == SDL_KEYDOWN) {
405 switch (event->keysym.sym) {
406 case SDLK_F11:
407 if (context->debugger) {
408 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
409 }
410 return;
411#ifdef USE_PNG
412 case SDLK_F12:
413 GBAThreadInterrupt(context);
414 GBAThreadTakeScreenshot(context);
415 GBAThreadContinue(context);
416 return;
417#endif
418 case SDLK_BACKSLASH:
419 GBAThreadPause(context);
420 context->frameCallback = _pauseAfterFrame;
421 GBAThreadUnpause(context);
422 return;
423 case SDLK_BACKQUOTE:
424 GBAThreadInterrupt(context);
425 GBARewind(context, 10);
426 GBAThreadContinue(context);
427 return;
428#ifdef BUILD_PANDORA
429 case SDLK_ESCAPE:
430 GBAThreadEnd(context);
431 return;
432#endif
433 default:
434 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
435 switch (event->keysym.sym) {
436#if SDL_VERSION_ATLEAST(2, 0, 0)
437 case SDLK_f:
438 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
439 sdlContext->fullscreen = !sdlContext->fullscreen;
440 sdlContext->windowUpdated = 1;
441 break;
442#endif
443 case SDLK_p:
444 GBAThreadTogglePause(context);
445 break;
446 case SDLK_n:
447 GBAThreadPause(context);
448 context->frameCallback = _pauseAfterFrame;
449 GBAThreadUnpause(context);
450 break;
451 case SDLK_r:
452 GBAThreadReset(context);
453 break;
454 default:
455 break;
456 }
457 }
458 if (event->keysym.mod & KMOD_SHIFT) {
459 switch (event->keysym.sym) {
460 case SDLK_F1:
461 case SDLK_F2:
462 case SDLK_F3:
463 case SDLK_F4:
464 case SDLK_F5:
465 case SDLK_F6:
466 case SDLK_F7:
467 case SDLK_F8:
468 case SDLK_F9:
469 GBAThreadInterrupt(context);
470 GBASaveState(context, context->dirs.state, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
471 GBAThreadContinue(context);
472 break;
473 default:
474 break;
475 }
476 } else {
477 switch (event->keysym.sym) {
478 case SDLK_F1:
479 case SDLK_F2:
480 case SDLK_F3:
481 case SDLK_F4:
482 case SDLK_F5:
483 case SDLK_F6:
484 case SDLK_F7:
485 case SDLK_F8:
486 case SDLK_F9:
487 GBAThreadInterrupt(context);
488 GBALoadState(context, context->dirs.state, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
489 GBAThreadContinue(context);
490 break;
491 default:
492 break;
493 }
494 }
495 return;
496 }
497 }
498}
499
500static void _mSDLHandleJoyButtonGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
501 enum GBAKey key = 0;
502 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
503 if (key == GBA_KEY_NONE) {
504 return;
505 }
506
507 if (event->type == SDL_JOYBUTTONDOWN) {
508 context->activeKeys |= 1 << key;
509 } else {
510 context->activeKeys &= ~(1 << key);
511 }
512}
513
514static void _mSDLHandleJoyHatGBA(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
515 enum GBAKey key = 0;
516
517 if (event->value & SDL_HAT_UP) {
518 key |= 1 << GBA_KEY_UP;
519 }
520 if (event->value & SDL_HAT_LEFT) {
521 key |= 1 << GBA_KEY_LEFT;
522 }
523 if (event->value & SDL_HAT_DOWN) {
524 key |= 1 << GBA_KEY_DOWN;
525 }
526 if (event->value & SDL_HAT_RIGHT) {
527 key |= 1 << GBA_KEY_RIGHT;
528 }
529
530 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
531 context->activeKeys |= key;
532}
533
534static void _mSDLHandleJoyAxisGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
535 int keys = context->activeKeys;
536
537 keys = mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
538 enum GBAKey key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
539 if (key != -1) {
540 keys |= 1 << key;
541 }
542
543 context->activeKeys = keys;
544}
545
546static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
547 int key = -1;
548 if (!event->keysym.mod) {
549#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
550 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
551#else
552 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
553#endif
554 }
555 if (key != -1) {
556 if (event->type == SDL_KEYDOWN) {
557 context->core->addKeys(context->core, 1 << key);
558 } else {
559 context->core->clearKeys(context->core, 1 << key);
560 }
561 return;
562 }
563 if (event->keysym.sym == SDLK_TAB) {
564 context->sync.audioWait = event->type != SDL_KEYDOWN;
565 return;
566 }
567 // TODO: Put back events
568}
569
570static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
571 int key = 0;
572 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
573 if (key == -1) {
574 return;
575 }
576
577 if (event->type == SDL_JOYBUTTONDOWN) {
578 core->addKeys(core, 1 << key);
579 } else {
580 core->clearKeys(core, 1 << key);
581 }
582}
583
584static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
585 int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
586 int newKeys = 0;
587 int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
588 if (key != -1) {
589 newKeys |= 1 << key;
590 }
591 clearKeys &= ~newKeys;
592 core->clearKeys(core, clearKeys);
593 core->addKeys(core, newKeys);
594
595}
596
597#if SDL_VERSION_ATLEAST(2, 0, 0)
598static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
599 switch (event->event) {
600 case SDL_WINDOWEVENT_SIZE_CHANGED:
601 sdlContext->windowUpdated = 1;
602 break;
603 }
604}
605#endif
606
607void mSDLHandleEventGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
608 switch (event->type) {
609 case SDL_QUIT:
610 GBAThreadEnd(context);
611 break;
612#if SDL_VERSION_ATLEAST(2, 0, 0)
613 case SDL_WINDOWEVENT:
614 _mSDLHandleWindowEvent(sdlContext, &event->window);
615 break;
616#endif
617 case SDL_KEYDOWN:
618 case SDL_KEYUP:
619 _mSDLHandleKeypressGBA(context, sdlContext, &event->key);
620 break;
621 case SDL_JOYBUTTONDOWN:
622 case SDL_JOYBUTTONUP:
623 _mSDLHandleJoyButtonGBA(context, sdlContext, &event->jbutton);
624 break;
625 case SDL_JOYHATMOTION:
626 _mSDLHandleJoyHatGBA(context, &event->jhat);
627 break;
628 case SDL_JOYAXISMOTION:
629 _mSDLHandleJoyAxisGBA(context, sdlContext, &event->jaxis);
630 break;
631 }
632}
633
634void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
635 switch (event->type) {
636 case SDL_QUIT:
637 mCoreThreadEnd(context);
638 break;
639#if SDL_VERSION_ATLEAST(2, 0, 0)
640 case SDL_WINDOWEVENT:
641 _mSDLHandleWindowEvent(sdlContext, &event->window);
642 break;
643#endif
644 case SDL_KEYDOWN:
645 case SDL_KEYUP:
646 _mSDLHandleKeypress(context, sdlContext, &event->key);
647 break;
648 case SDL_JOYBUTTONDOWN:
649 case SDL_JOYBUTTONUP:
650 _mSDLHandleJoyButton(context->core, sdlContext, &event->jbutton);
651 break;
652 case SDL_JOYHATMOTION:
653 // TODO
654 break;
655 case SDL_JOYAXISMOTION:
656 _mSDLHandleJoyAxis(context->core, sdlContext, &event->jaxis);
657 break;
658 }
659}
660
661#if SDL_VERSION_ATLEAST(2, 0, 0)
662static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
663 struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
664 if (!sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
665 return;
666 }
667 sdlRumble->level += enable;
668 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
669 int8_t oldLevel;
670 CircleBufferRead8(&sdlRumble->history, &oldLevel);
671 sdlRumble->level -= oldLevel;
672 }
673 CircleBufferWrite8(&sdlRumble->history, enable);
674 if (sdlRumble->level) {
675 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, sdlRumble->level / (float) RUMBLE_PWM, 20);
676 } else {
677 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
678 }
679}
680#endif
681
682static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
683 if (!player->joystick) {
684 return 0;
685 }
686 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
687}
688
689static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
690 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
691 return _readTilt(rotation->p, rotation->axisX);
692}
693
694static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
695 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
696 return _readTilt(rotation->p, rotation->axisY);
697}
698
699static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
700 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
701 float z = rotation->zDelta;
702 return z * rotation->gyroSensitivity;
703}
704
705static void _mSDLRotationSample(struct mRotationSource* source) {
706 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
707 SDL_JoystickUpdate();
708 if (!rotation->p->joystick) {
709 return;
710 }
711
712 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
713 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
714 union {
715 float f;
716 int32_t i;
717 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
718 if (isnan(theta.f)) {
719 theta.f = 0.0f;
720 } else if (theta.f > M_PI) {
721 theta.f -= 2.0f * M_PI;
722 } else if (theta.f < -M_PI) {
723 theta.f += 2.0f * M_PI;
724 }
725 rotation->oldX = x;
726 rotation->oldY = y;
727
728 float oldZ = 0;
729 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
730 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
731 }
732 CircleBufferWrite32(&rotation->zHistory, theta.i);
733 rotation->zDelta += theta.f - oldZ;
734}
735
736#if SDL_VERSION_ATLEAST(2, 0, 0)
737void mSDLSuspendScreensaver(struct mSDLEvents* events) {
738 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
739 SDL_DisableScreenSaver();
740 }
741 ++events->screensaverSuspendDepth;
742}
743
744void mSDLResumeScreensaver(struct mSDLEvents* events) {
745 --events->screensaverSuspendDepth;
746 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
747 SDL_EnableScreenSaver();
748 }
749}
750
751void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
752 bool wasSuspendable = events->screensaverSuspendable;
753 events->screensaverSuspendable = suspendable;
754 if (events->screensaverSuspendDepth > 0) {
755 if (suspendable && !wasSuspendable) {
756 SDL_DisableScreenSaver();
757 } else if (!suspendable && wasSuspendable) {
758 SDL_EnableScreenSaver();
759 }
760 } else {
761 SDL_EnableScreenSaver();
762 }
763}
764#endif