From 484618ca4c5f59f62a73018051ca6397472ea35e Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 9 Apr 2017 19:13:13 -0700 Subject: [PATCH 1/5] Core: Restore sleep callback --- CHANGES | 1 + include/mgba/core/interface.h | 5 +---- include/mgba/core/thread.h | 1 + include/mgba/internal/gba/gba.h | 1 - src/core/thread.c | 11 +++++++++++ src/gba/gba.c | 11 ++++++----- src/platform/qt/GameController.cpp | 10 ++++------ 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 4607a27b8..25720a77c 100644 --- a/CHANGES +++ b/CHANGES @@ -85,6 +85,7 @@ Misc: - FFmpeg: Return false if a file fails to open - FFmpeg: Force MP4 files to YUV420P - Qt: Make "Mute" able to be bound to a key + - Core: Restore sleep callback 0.5.2: (2016-12-31) Bugfixes: diff --git a/include/mgba/core/interface.h b/include/mgba/core/interface.h index 933e8d2d5..3a702c1f1 100644 --- a/include/mgba/core/interface.h +++ b/include/mgba/core/interface.h @@ -38,6 +38,7 @@ struct mCoreCallbacks { void (*videoFrameStarted)(void* context); void (*videoFrameEnded)(void* context); void (*coreCrashed)(void* context); + void (*sleep)(void* context); }; DECLARE_VECTOR(mCoreCallbacksList, struct mCoreCallbacks); @@ -53,10 +54,6 @@ struct mKeyCallback { uint16_t (*readKeys)(struct mKeyCallback*); }; -struct mStopCallback { - void (*stop)(struct mStopCallback*); -}; - struct mRotationSource { void (*sample)(struct mRotationSource*); diff --git a/include/mgba/core/thread.h b/include/mgba/core/thread.h index f586bfd3b..3c7a168e9 100644 --- a/include/mgba/core/thread.h +++ b/include/mgba/core/thread.h @@ -62,6 +62,7 @@ struct mCoreThread { ThreadCallback resetCallback; ThreadCallback cleanCallback; ThreadCallback frameCallback; + ThreadCallback sleepCallback; void* userData; void (*run)(struct mCoreThread*); diff --git a/include/mgba/internal/gba/gba.h b/include/mgba/internal/gba/gba.h index e5b1f8899..732a4aba6 100644 --- a/include/mgba/internal/gba/gba.h +++ b/include/mgba/internal/gba/gba.h @@ -99,7 +99,6 @@ struct GBA { struct mAVStream* stream; struct mKeyCallback* keyCallback; - struct mStopCallback* stopCallback; struct mCoreCallbacksList coreCallbacks; enum GBAIdleLoopOptimization idleOptimization; diff --git a/src/core/thread.c b/src/core/thread.c index 686330615..54f83f39f 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -120,6 +120,16 @@ void _crashed(void* context) { _changeState(thread, THREAD_CRASHED, true); } +void _sleep(void* context) { + struct mCoreThread* thread = context; + if (!thread) { + return; + } + if (thread->sleepCallback) { + thread->sleepCallback(thread); + } +} + static THREAD_ENTRY _mCoreThreadRun(void* context) { struct mCoreThread* threadContext = context; #ifdef USE_PTHREADS @@ -143,6 +153,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { .videoFrameStarted = _frameStarted, .videoFrameEnded = _frameEnded, .coreCrashed = _crashed, + .sleep = _sleep, .context = threadContext }; core->addCoreCallbacks(core, &callbacks); diff --git a/src/gba/gba.c b/src/gba/gba.c index c7059df12..25ff7670a 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -89,8 +89,6 @@ static void GBAInit(void* cpu, struct mCPUComponent* component) { gba->stream = NULL; gba->keyCallback = NULL; - gba->stopCallback = NULL; - gba->stopCallback = NULL; mCoreCallbacksListInit(&gba->coreCallbacks, 0); gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS); @@ -450,11 +448,14 @@ void GBAHalt(struct GBA* gba) { } void GBAStop(struct GBA* gba) { - if (!gba->stopCallback) { - return; + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c); + if (callbacks->sleep) { + callbacks->sleep(callbacks->context); + } } gba->cpu->nextEvent = gba->cpu->cycles; - gba->stopCallback->stop(gba->stopCallback); } void GBADebug(struct GBA* gba, uint16_t flags) { diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index e64243682..72f7b75b1 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -217,18 +217,16 @@ GameController::GameController(QObject* parent) } }; - // TODO: Put back - /*m_threadContext.stopCallback = [](mCoreThread* context) { + m_threadContext.sleepCallback = [](mCoreThread* context) { if (!context) { - return false; + return; } GameController* controller = static_cast(context->userData); if (!mCoreSaveState(context->core, 0, controller->m_saveStateFlags)) { - return false; + return; } QMetaObject::invokeMethod(controller, "closeGame"); - return true; - };*/ + }; m_threadContext.logger.d.log = [](mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) { mThreadLogger* logContext = reinterpret_cast(logger); From 5646ba7d606a5da1155be568946f80602a1c2e72 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 9 Apr 2017 19:18:29 -0700 Subject: [PATCH 2/5] Core: Fix Windows build --- src/core/thread.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/thread.c b/src/core/thread.c index 54f83f39f..652561b8c 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -120,7 +120,7 @@ void _crashed(void* context) { _changeState(thread, THREAD_CRASHED, true); } -void _sleep(void* context) { +void _coreSleep(void* context) { struct mCoreThread* thread = context; if (!thread) { return; @@ -153,7 +153,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) { .videoFrameStarted = _frameStarted, .videoFrameEnded = _frameEnded, .coreCrashed = _crashed, - .sleep = _sleep, + .sleep = _coreSleep, .context = threadContext }; core->addCoreCallbacks(core, &callbacks); From e063e056623dcd1ca1ed516e154a68e306b93595 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 9 Apr 2017 19:37:43 -0700 Subject: [PATCH 3/5] Core: Unify peripheral attachment --- include/mgba/core/core.h | 3 +-- include/mgba/core/interface.h | 6 ++++++ src/gb/core.c | 21 ++++++++++++--------- src/gba/core.c | 21 ++++++++++++--------- src/platform/3ds/main.c | 2 +- src/platform/libretro/libretro.c | 2 +- src/platform/psp2/psp2-context.c | 4 ++-- src/platform/qt/GameController.cpp | 4 ++-- src/platform/sdl/main.c | 2 +- src/platform/wii/main.c | 4 ++-- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/include/mgba/core/core.h b/include/mgba/core/core.h index 82ee3d6d6..e382a7beb 100644 --- a/include/mgba/core/core.h +++ b/include/mgba/core/core.h @@ -110,8 +110,7 @@ struct mCore { void (*getGameTitle)(const struct mCore*, char* title); void (*getGameCode)(const struct mCore*, char* title); - void (*setRotation)(struct mCore*, struct mRotationSource*); - void (*setRumble)(struct mCore*, struct mRumble*); + void (*setPeripheral)(struct mCore*, int type, void*); uint32_t (*busRead8)(struct mCore*, uint32_t address); uint32_t (*busRead16)(struct mCore*, uint32_t address); diff --git a/include/mgba/core/interface.h b/include/mgba/core/interface.h index 3a702c1f1..acaa930cd 100644 --- a/include/mgba/core/interface.h +++ b/include/mgba/core/interface.h @@ -54,6 +54,12 @@ struct mKeyCallback { uint16_t (*readKeys)(struct mKeyCallback*); }; +enum mPeripheral { + mPERIPH_ROTATION = 1, + mPERIPH_RUMBLE, + mPERIPH_CUSTOM = 0x1000 +}; + struct mRotationSource { void (*sample)(struct mRotationSource*); diff --git a/src/gb/core.c b/src/gb/core.c index 8263696f1..c3eb1b22a 100644 --- a/src/gb/core.c +++ b/src/gb/core.c @@ -408,14 +408,18 @@ static void _GBCoreGetGameCode(const struct mCore* core, char* title) { GBGetGameCode(core->board, title); } -static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) { +static void _GBCoreSetPeripheral(struct mCore* core, int type, void* periph) { struct GB* gb = core->board; - gb->memory.rotation = rotation; -} - -static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) { - struct GB* gb = core->board; - gb->memory.rumble = rumble; + switch (type) { + case mPERIPH_ROTATION: + gb->memory.rotation = periph; + break; + case mPERIPH_RUMBLE: + gb->memory.rumble = periph; + break; + default: + return; + } } static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) { @@ -620,8 +624,7 @@ struct mCore* GBCoreCreate(void) { core->frequency = _GBCoreFrequency; core->getGameTitle = _GBCoreGetGameTitle; core->getGameCode = _GBCoreGetGameCode; - core->setRotation = _GBCoreSetRotation; - core->setRumble = _GBCoreSetRumble; + core->setPeripheral = _GBCoreSetPeripheral; core->busRead8 = _GBCoreBusRead8; core->busRead16 = _GBCoreBusRead16; core->busRead32 = _GBCoreBusRead32; diff --git a/src/gba/core.c b/src/gba/core.c index 2ed68e435..2d653deca 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -405,14 +405,18 @@ static void _GBACoreGetGameCode(const struct mCore* core, char* title) { GBAGetGameCode(core->board, title); } -static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) { +static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) { struct GBA* gba = core->board; - gba->rotationSource = rotation; -} - -static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) { - struct GBA* gba = core->board; - gba->rumble = rumble; + switch (type) { + case mPERIPH_ROTATION: + gba->rotationSource = periph; + break; + case mPERIPH_RUMBLE: + gba->rumble = periph; + break; + default: + return; + } } static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) { @@ -619,8 +623,7 @@ struct mCore* GBACoreCreate(void) { core->frequency = _GBACoreFrequency; core->getGameTitle = _GBACoreGetGameTitle; core->getGameCode = _GBACoreGetGameCode; - core->setRotation = _GBACoreSetRotation; - core->setRumble = _GBACoreSetRumble; + core->setPeripheral = _GBACoreSetPeripheral; core->busRead8 = _GBACoreBusRead8; core->busRead16 = _GBACoreBusRead16; core->busRead32 = _GBACoreBusRead32; diff --git a/src/platform/3ds/main.c b/src/platform/3ds/main.c index 0d17ba9fa..da0954a21 100644 --- a/src/platform/3ds/main.c +++ b/src/platform/3ds/main.c @@ -242,7 +242,7 @@ static void _setup(struct mGUIRunner* runner) { mCoreLoadForeignConfig(runner->core, &runner->config); } - runner->core->setRotation(runner->core, &rotation.d); + runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation.d); if (hasSound != NO_SOUND) { runner->core->setAVStream(runner->core, &stream); } diff --git a/src/platform/libretro/libretro.c b/src/platform/libretro/libretro.c index 95f3f8e24..e640a91f0 100644 --- a/src/platform/libretro/libretro.c +++ b/src/platform/libretro/libretro.c @@ -398,7 +398,7 @@ bool retro_load_game(const struct retro_game_info* game) { blip_set_rates(core->getAudioChannel(core, 0), core->frequency(core), 32768); blip_set_rates(core->getAudioChannel(core, 1), core->frequency(core), 32768); - core->setRumble(core, &rumble); + core->setPeripheral(core, mPERIPH_RUMBLE, &rumble); savedata = anonymousMemoryMap(SIZE_CART_FLASH1M); struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M); diff --git a/src/platform/psp2/psp2-context.c b/src/platform/psp2/psp2-context.c index f58ee6a1d..b3aba3593 100644 --- a/src/platform/psp2/psp2-context.c +++ b/src/platform/psp2/psp2-context.c @@ -203,11 +203,11 @@ void mPSP2Setup(struct mGUIRunner* runner) { rotation.d.readTiltX = _readTiltX; rotation.d.readTiltY = _readTiltY; rotation.d.readGyroZ = _readGyroZ; - runner->core->setRotation(runner->core, &rotation.d); + runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation.d); rumble.d.setRumble = _setRumble; CircleBufferInit(&rumble.history, RUMBLE_PWM); - runner->core->setRumble(runner->core, &rumble.d); + runner->core->setPeripheral(runner->core, mPERIPH_RUMBLE, &rumble.d); frameLimiter = true; backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start); diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index 72f7b75b1..7d8484b84 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -87,8 +87,8 @@ GameController::GameController(QObject* parent) m_threadContext.startCallback = [](mCoreThread* context) { GameController* controller = static_cast(context->userData); - context->core->setRotation(context->core, controller->m_inputController->rotationSource()); - context->core->setRumble(context->core, controller->m_inputController->rumble()); + context->core->setPeripheral(context->core, mPERIPH_ROTATION, controller->m_inputController->rotationSource()); + context->core->setPeripheral(context->core, mPERIPH_RUMBLE, controller->m_inputController->rumble()); #ifdef M_CORE_GBA GBA* gba = static_cast(context->core->board); diff --git a/src/platform/sdl/main.c b/src/platform/sdl/main.c index 33aaf0b01..f5ec5fe53 100644 --- a/src/platform/sdl/main.c +++ b/src/platform/sdl/main.c @@ -130,7 +130,7 @@ int main(int argc, char** argv) { mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config)); #if SDL_VERSION_ATLEAST(2, 0, 0) - renderer.core->setRumble(renderer.core, &renderer.player.rumble.d); + renderer.core->setPeripheral(renderer.core, mPERIPH_RUMBLE, &renderer.player.rumble.d); #endif int ret; diff --git a/src/platform/wii/main.c b/src/platform/wii/main.c index 29096042e..f7561f018 100644 --- a/src/platform/wii/main.c +++ b/src/platform/wii/main.c @@ -641,8 +641,8 @@ void _guiPrepare(void) { } void _setup(struct mGUIRunner* runner) { - runner->core->setRotation(runner->core, &rotation); - runner->core->setRumble(runner->core, &rumble); + runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation); + runner->core->setPeripheral(runner->core, mPERIPH_RUMBLE, &rumble); _mapKey(&runner->core->inputMap, GCN1_INPUT, PAD_BUTTON_A, GBA_KEY_A); _mapKey(&runner->core->inputMap, GCN1_INPUT, PAD_BUTTON_B, GBA_KEY_B); From 473ae29d821bd7b24c78d08e74ea4673eeb64a1b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 9 Apr 2017 19:47:19 -0700 Subject: [PATCH 4/5] GBA: Add GBA luminance peripheral --- include/mgba/gba/interface.h | 4 ++++ src/feature/gui/gui-runner.c | 2 +- src/gba/core.c | 3 +++ src/platform/libretro/libretro.c | 3 +-- src/platform/qt/GameController.cpp | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/mgba/gba/interface.h b/include/mgba/gba/interface.h index 3a5412645..90f5e0697 100644 --- a/include/mgba/gba/interface.h +++ b/include/mgba/gba/interface.h @@ -28,6 +28,10 @@ struct GBAVideoRenderer; extern const int GBA_LUX_LEVELS[10]; +enum { + mPERIPH_GBA_LUMINANCE = 0x1000 +}; + struct GBALuminanceSource { void (*sample)(struct GBALuminanceSource*); diff --git a/src/feature/gui/gui-runner.c b/src/feature/gui/gui-runner.c index c26cbe662..ad59ae893 100644 --- a/src/feature/gui/gui-runner.c +++ b/src/feature/gui/gui-runner.c @@ -298,7 +298,7 @@ void mGUIRun(struct mGUIRunner* runner, const char* path) { return; } if (runner->core->platform(runner->core) == PLATFORM_GBA) { - ((struct GBA*) runner->core->board)->luminanceSource = &runner->luminanceSource.d; + runner->core->setPeripheral(runner->core, mPERIPH_GBA_LUMINANCE, &runner->luminanceSource.d); } mLOG(GUI_RUNNER, DEBUG, "Loading config..."); mCoreLoadForeignConfig(runner->core, &runner->config); diff --git a/src/gba/core.c b/src/gba/core.c index 2d653deca..4f7963cea 100644 --- a/src/gba/core.c +++ b/src/gba/core.c @@ -414,6 +414,9 @@ static void _GBACoreSetPeripheral(struct mCore* core, int type, void* periph) { case mPERIPH_RUMBLE: gba->rumble = periph; break; + case mPERIPH_GBA_LUMINANCE: + gba->luminanceSource = periph; + break; default: return; } diff --git a/src/platform/libretro/libretro.c b/src/platform/libretro/libretro.c index e640a91f0..d19c5006f 100644 --- a/src/platform/libretro/libretro.c +++ b/src/platform/libretro/libretro.c @@ -409,8 +409,7 @@ bool retro_load_game(const struct retro_game_info* game) { #ifdef M_CORE_GBA if (core->platform(core) == PLATFORM_GBA) { - struct GBA* gba = core->board; - gba->luminanceSource = &lux; + core->setPeripheral(core, mPERIPH_GBA_LUMINANCE, &lux); const char* sysDir = 0; if (core->opts.useBios && environCallback(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &sysDir)) { diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index 7d8484b84..486c22d6e 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -99,7 +99,7 @@ GameController::GameController(QObject* parent) switch (context->core->platform(context->core)) { #ifdef M_CORE_GBA case PLATFORM_GBA: - gba->luminanceSource = &controller->m_lux; + context->core->setPeripheral(context->core, mPERIPH_GBA_LUMINANCE, &controller->m_lux); gba->audio.psg.forceDisableCh[0] = !controller->m_audioChannels[0]; gba->audio.psg.forceDisableCh[1] = !controller->m_audioChannels[1]; gba->audio.psg.forceDisableCh[2] = !controller->m_audioChannels[2]; From 7bc6c579ce332efe0097c14ba4e40ccb26872f86 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 9 Apr 2017 23:34:23 -0700 Subject: [PATCH 5/5] Qt: Ensure audio processor is paused before closing thread --- src/platform/qt/GameController.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index 486c22d6e..28efaa8ea 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -641,6 +641,8 @@ void GameController::cleanGame() { if (!m_gameOpen || mCoreThreadIsActive(&m_threadContext)) { return; } + + m_audioProcessor->pause(); mCoreThreadJoin(&m_threadContext); if (m_tileCache) {