SDL: Refactor GBASDLEvents and GBASDLPlayer into mSDLEvents and mSDLPlayer
jump to
@@ -42,6 +42,8 @@ void (*runLoop)(struct mCore*);
void (*step)(struct mCore*); void (*setKeys)(struct mCore*, uint32_t keys); + void (*addKeys)(struct mCore*, uint32_t keys); + void (*clearKeys)(struct mCore*, uint32_t keys); int32_t (*frameCounter)(struct mCore*); int32_t (*frameCycles)(struct mCore*);
@@ -46,4 +46,8 @@ };
void mRTCGenericSourceInit(struct mRTCGenericSource* rtc, struct mCore* core); +struct mRumble { + void (*setRumble)(struct mRumble*, int enable); +}; + #endif
@@ -98,6 +98,16 @@ struct GBCore* gbcore = (struct GBCore*) core;
gbcore->keys = keys; } +static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) { + struct GBCore* gbcore = (struct GBCore*) core; + gbcore->keys |= keys; +} + +static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) { + struct GBCore* gbcore = (struct GBCore*) core; + gbcore->keys &= ~keys; +} + static int32_t _GBCoreFrameCounter(struct mCore* core) { struct GB* gb = core->board; return gb->video.frameCounter;@@ -136,6 +146,8 @@ core->runFrame = _GBCoreRunFrame;
core->runLoop = _GBCoreRunLoop; core->step = _GBCoreStep; core->setKeys = _GBCoreSetKeys; + core->addKeys = _GBCoreAddKeys; + core->clearKeys = _GBCoreClearKeys; core->frameCounter = _GBCoreFrameCounter; core->frameCycles = _GBCoreFrameCycles; core->frequency = _GBCoreFrequency;
@@ -99,7 +99,7 @@ int* keySource;
struct mRotationSource* rotationSource; struct GBALuminanceSource* luminanceSource; struct mRTCSource* rtcSource; - struct GBARumble* rumble; + struct mRumble* rumble; struct GBARRContext* rr; void* pristineRom;
@@ -364,7 +364,7 @@ hw->devices |= HW_RUMBLE;
} void _rumbleReadPins(struct GBACartridgeHardware* hw) { - struct GBARumble* rumble = hw->p->rumble; + struct mRumble* rumble = hw->p->rumble; if (!rumble) { return; }
@@ -76,10 +76,6 @@ uint8_t time[7];
}; #pragma pack(pop) -struct GBARumble { - void (*setRumble)(struct GBARumble*, int enable); -}; - struct GBAGBPKeyCallback { struct mKeyCallback d; struct GBACartridgeHardware* p;
@@ -29,7 +29,7 @@
static void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args); static void _postAudioBuffer(struct GBAAVStream*, struct GBAAudio* audio); -static void _setRumble(struct GBARumble* rumble, int enable); +static void _setRumble(struct mRumble* rumble, int enable); static uint8_t _readLux(struct GBALuminanceSource* lux); static void _updateLux(struct GBALuminanceSource* lux);@@ -41,7 +41,7 @@ static void* savedata;
static struct GBAAVStream stream; static int rumbleLevel; static struct CircleBuffer rumbleHistory; -static struct GBARumble rumble; +static struct mRumble rumble; static struct GBALuminanceSource lux; static int luxLevel; static struct GBACheatDevice cheats;@@ -470,7 +470,7 @@ blip_read_samples(audio->psg.right, samples + 1, SAMPLES, true);
audioCallback(samples, SAMPLES); } -static void _setRumble(struct GBARumble* rumble, int enable) { +static void _setRumble(struct mRumble* rumble, int enable) { UNUSED(rumble); if (!rumbleCallback) { return;
@@ -22,7 +22,7 @@ using namespace QGBA;
#ifdef BUILD_SDL int InputController::s_sdlInited = 0; -GBASDLEvents InputController::s_sdlEvents; +mSDLEvents InputController::s_sdlEvents; #endif InputController::InputController(int playerId, QWidget* topLevel, QObject* parent)@@ -41,11 +41,11 @@ mInputMapInit(&m_inputMap, &GBAInputInfo);
#ifdef BUILD_SDL if (s_sdlInited == 0) { - GBASDLInitEvents(&s_sdlEvents); + mSDLInitEvents(&s_sdlEvents); } ++s_sdlInited; m_sdlPlayer.bindings = &m_inputMap; - GBASDLInitBindings(&m_inputMap); + mSDLInitBindingsGBA(&m_inputMap); updateJoysticks(); #endif@@ -75,12 +75,12 @@ mInputMapDeinit(&m_inputMap);
#ifdef BUILD_SDL if (m_playerAttached) { - GBASDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer); + mSDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer); } --s_sdlInited; if (s_sdlInited == 0) { - GBASDLDeinitEvents(&s_sdlEvents); + mSDLDeinitEvents(&s_sdlEvents); } #endif }@@ -90,9 +90,9 @@ m_config = config;
setAllowOpposing(config->getOption("allowOpposingDirections").toInt()); loadConfiguration(KEYBOARD); #ifdef BUILD_SDL - GBASDLEventsLoadConfig(&s_sdlEvents, config->input()); + mSDLEventsLoadConfig(&s_sdlEvents, config->input()); if (!m_playerAttached) { - m_playerAttached = GBASDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer); + m_playerAttached = mSDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer); } loadConfiguration(SDL_BINDING_BUTTON); loadProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));@@ -103,7 +103,7 @@ void InputController::loadConfiguration(uint32_t type) {
mInputMapLoad(&m_inputMap, type, m_config->input()); #ifdef BUILD_SDL if (m_playerAttached) { - GBASDLPlayerLoadConfig(&m_sdlPlayer, m_config->input()); + mSDLPlayerLoadConfig(&m_sdlPlayer, m_config->input()); } #endif }@@ -126,7 +126,7 @@ #ifdef BUILD_SDL
saveConfiguration(SDL_BINDING_BUTTON); saveProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON)); if (m_playerAttached) { - GBASDLPlayerSaveConfig(&m_sdlPlayer, m_config->input()); + mSDLPlayerSaveConfig(&m_sdlPlayer, m_config->input()); } m_config->write(); #endif@@ -194,7 +194,7 @@
void InputController::setGamepad(uint32_t type, int index) { #ifdef BUILD_SDL if (type == SDL_BINDING_BUTTON) { - GBASDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index); + mSDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index); } #endif }@@ -206,7 +206,7 @@ }
mInputSetPreferredDevice(m_config->input(), "gba", type, m_playerId, device.toUtf8().constData()); } -GBARumble* InputController::rumble() { +mRumble* InputController::rumble() { #ifdef BUILD_SDL #if SDL_VERSION_ATLEAST(2, 0, 0) if (m_playerAttached) {@@ -285,7 +285,7 @@ }
void InputController::updateJoysticks() { #ifdef BUILD_SDL - GBASDLUpdateJoysticks(&s_sdlEvents); + mSDLUpdateJoysticks(&s_sdlEvents); #endif }@@ -513,7 +513,7 @@
void InputController::suspendScreensaver() { #ifdef BUILD_SDL #if SDL_VERSION_ATLEAST(2, 0, 0) - GBASDLSuspendScreensaver(&s_sdlEvents); + mSDLSuspendScreensaver(&s_sdlEvents); #endif #endif }@@ -521,7 +521,7 @@
void InputController::resumeScreensaver() { #ifdef BUILD_SDL #if SDL_VERSION_ATLEAST(2, 0, 0) - GBASDLResumeScreensaver(&s_sdlEvents); + mSDLResumeScreensaver(&s_sdlEvents); #endif #endif }@@ -529,7 +529,7 @@
void InputController::setScreensaverSuspendable(bool suspendable) { #ifdef BUILD_SDL #if SDL_VERSION_ATLEAST(2, 0, 0) - GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable); + mSDLSetScreensaverSuspendable(&s_sdlEvents, suspendable); #endif #endif }
@@ -79,7 +79,7 @@
void stealFocus(QWidget* focus); void releaseFocus(QWidget* focus); - GBARumble* rumble(); + mRumble* rumble(); mRotationSource* rotationSource(); signals:@@ -108,8 +108,8 @@ QWidget* m_focusParent;
#ifdef BUILD_SDL static int s_sdlInited; - static GBASDLEvents s_sdlEvents; - GBASDLPlayer m_sdlPlayer; + static mSDLEvents s_sdlEvents; + mSDLPlayer m_sdlPlayer; bool m_playerAttached; #endif
@@ -69,7 +69,7 @@ renderer->audio.psg = &context->gba->audio.psg;
while (context->state < THREAD_EXITING) { while (SDL_PollEvent(&event)) { - GBASDLHandleEvent(context, &renderer->player, &event); + mSDLHandleEventGBA(context, &renderer->player, &event); #if SDL_VERSION_ATLEAST(2, 0, 0) // Event handling can change the size of the screen if (renderer->player.windowUpdated) {@@ -132,32 +132,15 @@ void mSDLGLRunloopGB(struct mSDLRenderer* renderer, void* user) {
UNUSED(user); SDL_Event event; struct VideoBackend* v = &renderer->gl.d; - int activeKeys = 0; renderer->audio.psg = &((struct GB*) renderer->core->board)->audio; while (true) { renderer->core->runFrame(renderer->core); while (SDL_PollEvent(&event)) { - // TODO: Refactor out - if (event.type == SDL_KEYUP || event.type == SDL_KEYDOWN) { - int key; -#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0) - key = mInputMapKey(renderer->player.bindings, SDL_BINDING_KEY, event.key.keysym.scancode); -#else - key = mInputMapKey(renderer->player.bindings, SDL_BINDING_KEY, event.key.keysym.sym); -#endif - if (key != GBA_KEY_NONE) { - if (event.type == SDL_KEYDOWN) { - activeKeys |= 1 << key; - } else { - activeKeys &= ~(1 << key); - } - } - } + mSDLHandleEvent(renderer->core, &renderer->player, &event); if (event.type == SDL_QUIT) { return; } - #if SDL_VERSION_ATLEAST(2, 0, 0) // Event handling can change the size of the screen if (renderer->player.windowUpdated) {@@ -167,7 +150,6 @@ renderer->player.windowUpdated = 0;
} #endif } - renderer->core->setKeys(renderer->core, activeKeys); v->postFrame(v, renderer->outputBuffer); v->drawFrame(v);
@@ -112,7 +112,7 @@ struct VideoBackend* v = &renderer->gl2.d;
while (context->state < THREAD_EXITING) { while (SDL_PollEvent(&event)) { - GBASDLHandleEvent(context, &renderer->player, &event); + mSDLHandleEventGBA(context, &renderer->player, &event); } if (mCoreSyncWaitFrameStart(&context->sync)) {
@@ -188,11 +188,11 @@ renderer.core->init(renderer.core);
} renderer.player.bindings = &inputMap; - GBASDLInitBindings(&inputMap); - GBASDLInitEvents(&renderer.events); - GBASDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&config)); - GBASDLAttachPlayer(&renderer.events, &renderer.player); - GBASDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&config)); + mSDLInitBindingsGBA(&inputMap); + mSDLInitEvents(&renderer.events); + mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&config)); + mSDLAttachPlayer(&renderer.events, &renderer.player); + mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&config)); int ret;@@ -207,7 +207,7 @@ default:
ret = 1; break; } - GBASDLDetachPlayer(&renderer.events, &renderer.player); + mSDLDetachPlayer(&renderer.events, &renderer.player); mInputMapDeinit(&inputMap); mSDLDeinit(&renderer);@@ -245,8 +245,8 @@ }
if (!didFail) { #if SDL_VERSION_ATLEAST(2, 0, 0) - GBASDLSetScreensaverSuspendable(&renderer->events, opts->suspendScreensaver); - GBASDLSuspendScreensaver(&renderer->events); + mSDLSetScreensaverSuspendable(&renderer->events, opts->suspendScreensaver); + mSDLSuspendScreensaver(&renderer->events); #endif if (GBAThreadStart(&context)) { renderer->audio.psg = &context.gba->audio.psg;@@ -259,8 +259,8 @@ printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
} #if SDL_VERSION_ATLEAST(2, 0, 0) - GBASDLResumeScreensaver(&renderer->events); - GBASDLSetScreensaverSuspendable(&renderer->events, false); + mSDLResumeScreensaver(&renderer->events); + mSDLSetScreensaverSuspendable(&renderer->events, false); #endif if (GBAThreadHasCrashed(&context)) {@@ -315,7 +315,7 @@ return renderer->init(renderer);
} static void mSDLDeinit(struct mSDLRenderer* renderer) { - GBASDLDeinitEvents(&renderer->events); + mSDLDeinitEvents(&renderer->events); GBSDLDeinitAudio(&renderer->audio); #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_DestroyWindow(renderer->window);
@@ -49,8 +49,8 @@ // TODO: Remove
struct GBAVideoSoftwareRenderer d; #endif struct GBSDLAudio audio; - struct GBASDLEvents events; - struct GBASDLPlayer player; + struct mSDLEvents events; + struct mSDLPlayer player; bool (*init)(struct mSDLRenderer* renderer); void (*runloop)(struct mSDLRenderer* renderer, void* user);
@@ -16,23 +16,23 @@ #ifndef FBIO_WAITFORVSYNC
#define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32) #endif -static bool GBASDLInit(struct SDLSoftwareRenderer* renderer); -static void GBASDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer); -static void GBASDLDeinit(struct SDLSoftwareRenderer* renderer); +static bool mSDLInit(struct SDLSoftwareRenderer* renderer); +static void mSDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer); +static void mSDLDeinit(struct SDLSoftwareRenderer* renderer); -void GBASDLGLCreate(struct SDLSoftwareRenderer* renderer) { - renderer->init = GBASDLInit; - renderer->deinit = GBASDLDeinit; - renderer->runloop = GBASDLRunloop; +void mSDLGLCreate(struct SDLSoftwareRenderer* renderer) { + renderer->init = mSDLInit; + renderer->deinit = mSDLDeinit; + renderer->runloop = mSDLRunloop; } -void GBASDLSWCreate(struct SDLSoftwareRenderer* renderer) { - renderer->init = GBASDLInit; - renderer->deinit = GBASDLDeinit; - renderer->runloop = GBASDLRunloop; +void mSDLSWCreate(struct SDLSoftwareRenderer* renderer) { + renderer->init = mSDLInit; + renderer->deinit = mSDLDeinit; + renderer->runloop = mSDLRunloop; } -bool GBASDLInit(struct SDLSoftwareRenderer* renderer) { +bool mSDLInit(struct SDLSoftwareRenderer* renderer) { SDL_SetVideoMode(800, 480, 16, SDL_FULLSCREEN); renderer->odd = 0;@@ -83,12 +83,12 @@ renderer->d.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
return true; } -void GBASDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) { +void mSDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) { SDL_Event event; while (context->state < THREAD_EXITING) { while (SDL_PollEvent(&event)) { - GBASDLHandleEvent(context, &renderer->player, &event); + mSDLHandleEventGBA(context, &renderer->player, &event); } if (mCoreSyncWaitFrameStart(&context->sync)) {@@ -107,7 +107,7 @@ mCoreSyncWaitFrameEnd(&context->sync);
} } -void GBASDLDeinit(struct SDLSoftwareRenderer* renderer) { +void mSDLDeinit(struct SDLSoftwareRenderer* renderer) { munmap(renderer->base[0], VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4); struct omapfb_plane_info plane;
@@ -5,10 +5,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "sdl-events.h" +#include "core/input.h" #include "debugger/debugger.h" +#include "gba/input.h" #include "gba/io.h" #include "gba/rr/rr.h" #include "gba/serialize.h" +#include "gba/supervisor/thread.h" #include "gba/video.h" #include "gba/renderers/video-software.h" #include "util/configuration.h"@@ -27,14 +30,14 @@
DEFINE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo); #if SDL_VERSION_ATLEAST(2, 0, 0) -static void _GBASDLSetRumble(struct GBARumble* rumble, int enable); +static void _mSDLSetRumble(struct mRumble* rumble, int enable); #endif -static int32_t _GBASDLReadTiltX(struct mRotationSource* rumble); -static int32_t _GBASDLReadTiltY(struct mRotationSource* rumble); -static int32_t _GBASDLReadGyroZ(struct mRotationSource* rumble); -static void _GBASDLRotationSample(struct mRotationSource* source); +static int32_t _mSDLReadTiltX(struct mRotationSource* rumble); +static int32_t _mSDLReadTiltY(struct mRotationSource* rumble); +static int32_t _mSDLReadGyroZ(struct mRotationSource* rumble); +static void _mSDLRotationSample(struct mRotationSource* source); -bool GBASDLInitEvents(struct GBASDLEvents* context) { +bool mSDLInitEvents(struct mSDLEvents* context) { #if SDL_VERSION_ATLEAST(2, 0, 4) SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); #endif@@ -56,7 +59,7 @@ SDL_JoystickEventState(SDL_ENABLE);
int nJoysticks = SDL_NumJoysticks(); SDL_JoystickListInit(&context->joysticks, nJoysticks); if (nJoysticks > 0) { - GBASDLUpdateJoysticks(context); + mSDLUpdateJoysticks(context); } context->playersAttached = 0;@@ -74,7 +77,7 @@ #endif
return true; } -void GBASDLDeinitEvents(struct GBASDLEvents* context) { +void mSDLDeinitEvents(struct mSDLEvents* context) { size_t i; for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) { struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);@@ -87,14 +90,14 @@ SDL_JoystickListDeinit(&context->joysticks);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } -void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) { +void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration* config) { context->preferredJoysticks[0] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 0); context->preferredJoysticks[1] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 1); context->preferredJoysticks[2] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 2); context->preferredJoysticks[3] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 3); } -void GBASDLInitBindings(struct mInputMap* inputMap) { +void mSDLInitBindingsGBA(struct mInputMap* inputMap) { #ifdef BUILD_PANDORA mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A); mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);@@ -147,7 +150,7 @@ description = (struct mInputAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description); } -bool GBASDLAttachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) { +bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) { player->joystick = 0; if (events->playersAttached >= MAX_PLAYERS) {@@ -155,16 +158,16 @@ return false;
} #if SDL_VERSION_ATLEAST(2, 0, 0) - player->rumble.d.setRumble = _GBASDLSetRumble; + player->rumble.d.setRumble = _mSDLSetRumble; CircleBufferInit(&player->rumble.history, RUMBLE_PWM); player->rumble.level = 0; player->rumble.p = player; #endif - player->rotation.d.readTiltX = _GBASDLReadTiltX; - player->rotation.d.readTiltY = _GBASDLReadTiltY; - player->rotation.d.readGyroZ = _GBASDLReadGyroZ; - player->rotation.d.sample = _GBASDLRotationSample; + player->rotation.d.readTiltX = _mSDLReadTiltX; + player->rotation.d.readTiltY = _mSDLReadTiltY; + player->rotation.d.readGyroZ = _mSDLReadGyroZ; + player->rotation.d.sample = _mSDLRotationSample; player->rotation.axisX = 2; player->rotation.axisY = 3; player->rotation.gyroSensitivity = 2.2e9f;@@ -228,7 +231,7 @@ ++events->playersAttached;
return true; } -void GBASDLDetachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) { +void mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) { if (player != events->players[player->playerId]) { return; }@@ -245,7 +248,7 @@ --events->playersAttached;
CircleBufferDeinit(&player->rotation.zHistory); } -void GBASDLPlayerLoadConfig(struct GBASDLPlayer* context, const struct Configuration* config) { +void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) { mInputMapLoad(context->bindings, SDL_BINDING_KEY, config); if (context->joystick) { mInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);@@ -298,7 +301,7 @@ }
} } -void GBASDLPlayerSaveConfig(const struct GBASDLPlayer* context, struct Configuration* config) { +void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) { if (context->joystick) { #if SDL_VERSION_ATLEAST(2, 0, 0) const char* name = SDL_JoystickName(context->joystick->joystick);@@ -319,14 +322,14 @@ mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
} } -void GBASDLPlayerChangeJoystick(struct GBASDLEvents* events, struct GBASDLPlayer* player, size_t index) { +void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) { if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) { return; } player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index); } -void GBASDLUpdateJoysticks(struct GBASDLEvents* events) { +void mSDLUpdateJoysticks(struct mSDLEvents* events) { // Pump SDL joystick events without eating the rest of the events SDL_JoystickUpdate(); #if SDL_VERSION_ATLEAST(2, 0, 0)@@ -376,7 +379,7 @@ context->frameCallback = 0;
GBAThreadPauseFromThread(context); } -static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) { +static void _mSDLHandleKeypressGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) { enum GBAKey key = GBA_KEY_NONE; if (!event->keysym.mod) { #if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)@@ -493,7 +496,7 @@ }
} } -static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) { +static void _mSDLHandleJoyButtonGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) { enum GBAKey key = 0; key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button); if (key == GBA_KEY_NONE) {@@ -507,7 +510,7 @@ context->activeKeys &= ~(1 << key);
} } -static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) { +static void _mSDLHandleJoyHatGBA(struct GBAThread* context, const struct SDL_JoyHatEvent* event) { enum GBAKey key = 0; if (event->value & SDL_HAT_UP) {@@ -527,21 +530,67 @@ context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
context->activeKeys |= key; } -static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) { +static void _mSDLHandleJoyAxisGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) { int keys = context->activeKeys; keys = mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys); enum GBAKey key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value); - if (key != GBA_KEY_NONE) { + if (key != -1) { keys |= 1 << key; } context->activeKeys = keys; } +static void _mSDLHandleKeypress(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) { + int key = -1; + if (!event->keysym.mod) { +#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0) + key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode); +#else + key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym); +#endif + } + if (key != -1) { + if (event->type == SDL_KEYDOWN) { + core->addKeys(core, 1 << key); + } else { + core->clearKeys(core, 1 << key); + } + return; + } + // TODO: Put back events +} + +static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) { + int key = 0; + key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button); + if (key == -1) { + return; + } + + if (event->type == SDL_JOYBUTTONDOWN) { + core->addKeys(core, 1 << key); + } else { + core->clearKeys(core, 1 << key); + } +} + +static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) { + int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1); + int newKeys = 0; + int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value); + if (key != -1) { + newKeys |= 1 << key; + } + clearKeys &= ~newKeys; + core->clearKeys(core, clearKeys); + core->addKeys(core, newKeys); + +} + #if SDL_VERSION_ATLEAST(2, 0, 0) -static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_WindowEvent* event) { - UNUSED(context); +static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) { switch (event->event) { case SDL_WINDOWEVENT_SIZE_CHANGED: sdlContext->windowUpdated = 1;@@ -550,36 +599,63 @@ }
} #endif -void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const union SDL_Event* event) { +void mSDLHandleEventGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) { switch (event->type) { case SDL_QUIT: GBAThreadEnd(context); break; #if SDL_VERSION_ATLEAST(2, 0, 0) case SDL_WINDOWEVENT: - _GBASDLHandleWindowEvent(context, sdlContext, &event->window); + _mSDLHandleWindowEvent(sdlContext, &event->window); break; #endif case SDL_KEYDOWN: case SDL_KEYUP: - _GBASDLHandleKeypress(context, sdlContext, &event->key); + _mSDLHandleKeypressGBA(context, sdlContext, &event->key); break; case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: - _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton); + _mSDLHandleJoyButtonGBA(context, sdlContext, &event->jbutton); break; case SDL_JOYHATMOTION: - _GBASDLHandleJoyHat(context, &event->jhat); + _mSDLHandleJoyHatGBA(context, &event->jhat); break; case SDL_JOYAXISMOTION: - _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis); + _mSDLHandleJoyAxisGBA(context, sdlContext, &event->jaxis); break; } } +void mSDLHandleEvent(struct mCore* core, struct mSDLPlayer* sdlContext, const union SDL_Event* event) { + switch (event->type) { + case SDL_QUIT: + // TODO + break; #if SDL_VERSION_ATLEAST(2, 0, 0) -static void _GBASDLSetRumble(struct GBARumble* rumble, int enable) { - struct GBASDLRumble* sdlRumble = (struct GBASDLRumble*) rumble; + case SDL_WINDOWEVENT: + _mSDLHandleWindowEvent(sdlContext, &event->window); + break; +#endif + case SDL_KEYDOWN: + case SDL_KEYUP: + _mSDLHandleKeypress(core, sdlContext, &event->key); + break; + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + _mSDLHandleJoyButton(core, sdlContext, &event->jbutton); + break; + case SDL_JOYHATMOTION: + // TODO + break; + case SDL_JOYAXISMOTION: + _mSDLHandleJoyAxis(core, sdlContext, &event->jaxis); + break; + } +} + +#if SDL_VERSION_ATLEAST(2, 0, 0) +static void _mSDLSetRumble(struct mRumble* rumble, int enable) { + struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble; if (!sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) { return; }@@ -598,31 +674,31 @@ }
} #endif -static int32_t _readTilt(struct GBASDLPlayer* player, int axis) { +static int32_t _readTilt(struct mSDLPlayer* player, int axis) { if (!player->joystick) { return 0; } return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800; } -static int32_t _GBASDLReadTiltX(struct mRotationSource* source) { - struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; +static int32_t _mSDLReadTiltX(struct mRotationSource* source) { + struct mSDLRotation* rotation = (struct mSDLRotation*) source; return _readTilt(rotation->p, rotation->axisX); } -static int32_t _GBASDLReadTiltY(struct mRotationSource* source) { - struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; +static int32_t _mSDLReadTiltY(struct mRotationSource* source) { + struct mSDLRotation* rotation = (struct mSDLRotation*) source; return _readTilt(rotation->p, rotation->axisY); } -static int32_t _GBASDLReadGyroZ(struct mRotationSource* source) { - struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; +static int32_t _mSDLReadGyroZ(struct mRotationSource* source) { + struct mSDLRotation* rotation = (struct mSDLRotation*) source; float z = rotation->zDelta; return z * rotation->gyroSensitivity; } -static void _GBASDLRotationSample(struct mRotationSource* source) { - struct GBASDLRotation* rotation = (struct GBASDLRotation*) source; +static void _mSDLRotationSample(struct mRotationSource* source) { + struct mSDLRotation* rotation = (struct mSDLRotation*) source; SDL_JoystickUpdate(); if (!rotation->p->joystick) { return;@@ -653,21 +729,21 @@ rotation->zDelta += theta.f - oldZ;
} #if SDL_VERSION_ATLEAST(2, 0, 0) -void GBASDLSuspendScreensaver(struct GBASDLEvents* events) { +void mSDLSuspendScreensaver(struct mSDLEvents* events) { if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) { SDL_DisableScreenSaver(); } ++events->screensaverSuspendDepth; } -void GBASDLResumeScreensaver(struct GBASDLEvents* events) { +void mSDLResumeScreensaver(struct mSDLEvents* events) { --events->screensaverSuspendDepth; if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) { SDL_EnableScreenSaver(); } } -void GBASDLSetScreensaverSuspendable(struct GBASDLEvents* events, bool suspendable) { +void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) { bool wasSuspendable = events->screensaverSuspendable; events->screensaverSuspendable = suspendable; if (events->screensaverSuspendDepth > 0) {
@@ -7,10 +7,10 @@ #ifndef SDL_EVENTS_H
#define SDL_EVENTS_H #include "util/common.h" + +#include "core/interface.h" #include "util/circle-buffer.h" #include "util/vector.h" - -#include "gba/supervisor/thread.h" #include <SDL.h>@@ -19,7 +19,6 @@ #define SDL_BINDING_BUTTON 0x53444C42U
#define MAX_PLAYERS 4 -struct GBAVideoSoftwareRenderer; struct Configuration; struct SDL_JoystickCombo {@@ -33,20 +32,19 @@ };
DECLARE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo); -struct GBASDLPlayer; - -struct GBASDLEvents { +struct mSDLPlayer; +struct mSDLEvents { struct SDL_JoystickList joysticks; const char* preferredJoysticks[MAX_PLAYERS]; int playersAttached; - struct GBASDLPlayer* players[MAX_PLAYERS]; + struct mSDLPlayer* players[MAX_PLAYERS]; #if SDL_VERSION_ATLEAST(2, 0, 0) int screensaverSuspendDepth; bool screensaverSuspendable; #endif }; -struct GBASDLPlayer { +struct mSDLPlayer { size_t playerId; struct mInputMap* bindings; struct SDL_JoystickCombo* joystick;@@ -55,18 +53,18 @@ SDL_Window* window;
int fullscreen; int windowUpdated; - struct GBASDLRumble { - struct GBARumble d; - struct GBASDLPlayer* p; + struct mSDLRumble { + struct mRumble d; + struct mSDLPlayer* p; int level; struct CircleBuffer history; } rumble; #endif - struct GBASDLRotation { + struct mSDLRotation { struct mRotationSource d; - struct GBASDLPlayer* p; + struct mSDLPlayer* p; // Tilt int axisX;@@ -83,25 +81,29 @@ float zDelta;
} rotation; }; -bool GBASDLInitEvents(struct GBASDLEvents*); -void GBASDLDeinitEvents(struct GBASDLEvents*); +bool mSDLInitEvents(struct mSDLEvents*); +void mSDLDeinitEvents(struct mSDLEvents*); -bool GBASDLAttachPlayer(struct GBASDLEvents*, struct GBASDLPlayer*); -void GBASDLDetachPlayer(struct GBASDLEvents*, struct GBASDLPlayer*); -void GBASDLEventsLoadConfig(struct GBASDLEvents*, const struct Configuration*); -void GBASDLPlayerChangeJoystick(struct GBASDLEvents*, struct GBASDLPlayer*, size_t index); -void GBASDLUpdateJoysticks(struct GBASDLEvents* events); +bool mSDLAttachPlayer(struct mSDLEvents*, struct mSDLPlayer*); +void mSDLDetachPlayer(struct mSDLEvents*, struct mSDLPlayer*); +void mSDLEventsLoadConfig(struct mSDLEvents*, const struct Configuration*); +void mSDLPlayerChangeJoystick(struct mSDLEvents*, struct mSDLPlayer*, size_t index); +void mSDLUpdateJoysticks(struct mSDLEvents* events); -void GBASDLInitBindings(struct mInputMap* inputMap); -void GBASDLPlayerLoadConfig(struct GBASDLPlayer*, const struct Configuration*); -void GBASDLPlayerSaveConfig(const struct GBASDLPlayer*, struct Configuration*); +void mSDLPlayerLoadConfig(struct mSDLPlayer*, const struct Configuration*); +void mSDLPlayerSaveConfig(const struct mSDLPlayer*, struct Configuration*); -void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const union SDL_Event* event); +struct GBAThread; +void mSDLInitBindingsGBA(struct mInputMap* inputMap); +void mSDLHandleEventGBA(struct GBAThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event); + +struct mCore; +void mSDLHandleEvent(struct mCore* core, struct mSDLPlayer* sdlContext, const union SDL_Event* event); #if SDL_VERSION_ATLEAST(2, 0, 0) -void GBASDLSuspendScreensaver(struct GBASDLEvents*); -void GBASDLResumeScreensaver(struct GBASDLEvents*); -void GBASDLSetScreensaverSuspendable(struct GBASDLEvents*, bool suspendable); +void mSDLSuspendScreensaver(struct mSDLEvents*); +void mSDLResumeScreensaver(struct mSDLEvents*); +void mSDLSetScreensaverSuspendable(struct mSDLEvents*, bool suspendable); #endif #endif
@@ -91,7 +91,7 @@ #endif
while (context->state < THREAD_EXITING) { while (SDL_PollEvent(&event)) { - GBASDLHandleEvent(context, &renderer->player, &event); + mSDLHandleEventGBA(context, &renderer->player, &event); } if (mCoreSyncWaitFrameStart(&context->sync)) {
@@ -50,7 +50,7 @@
static void _retraceCallback(u32 count); static void _audioDMA(void); -static void _setRumble(struct GBARumble* rumble, int enable); +static void _setRumble(struct mRumble* rumble, int enable); static void _sampleRotation(struct mRotationSource* source); static int32_t _readTiltX(struct mRotationSource* source); static int32_t _readTiltY(struct mRotationSource* source);@@ -74,7 +74,7 @@ static s8 WPAD_StickX(u8 chan, u8 right);
static s8 WPAD_StickY(u8 chan, u8 right); static struct GBAVideoSoftwareRenderer renderer; -static struct GBARumble rumble; +static struct mRumble rumble; static struct mRotationSource rotation; static GXRModeObj* vmode; static Mtx model, view, modelview;@@ -717,7 +717,7 @@
return keys; } -void _setRumble(struct GBARumble* rumble, int enable) { +void _setRumble(struct mRumble* rumble, int enable) { UNUSED(rumble); WPAD_Rumble(0, enable); if (enable) {