From 7740634e6a8269d1327fdf3dad4d88de2ac4d330 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Fri, 1 Nov 2024 02:19:29 +0100 Subject: [PATCH] reimplement MP audio mode 2 (active instance only) --- src/frontend/qt_sdl/EmuInstanceAudio.cpp | 15 +++++++++++---- src/frontend/qt_sdl/Screen.cpp | 4 ++++ src/frontend/qt_sdl/Window.cpp | 18 ++++++++++++++++-- src/frontend/qt_sdl/Window.h | 6 ++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/frontend/qt_sdl/EmuInstanceAudio.cpp b/src/frontend/qt_sdl/EmuInstanceAudio.cpp index ac6e7d5b..3b222ba0 100644 --- a/src/frontend/qt_sdl/EmuInstanceAudio.cpp +++ b/src/frontend/qt_sdl/EmuInstanceAudio.cpp @@ -133,6 +133,7 @@ void EmuInstance::micCallback(void* data, Uint8* stream, int len) void EmuInstance::audioMute() { audioMuted = false; + if (numEmuInstances() < 2) return; switch (mpAudioMode) { @@ -141,10 +142,16 @@ void EmuInstance::audioMute() break; case 2: // only currently focused instance - //if (mainWindow != nullptr) - // audioMuted = !mainWindow->isActiveWindow(); - // TODO!! - printf("TODO!! audioMute mode 2\n"); + audioMuted = true; + for (int i = 0; i < kMaxWindows; i++) + { + if (!windowList[i]) continue; + if (windowList[i]->isFocused()) + { + audioMuted = false; + break; + } + } break; } } diff --git a/src/frontend/qt_sdl/Screen.cpp b/src/frontend/qt_sdl/Screen.cpp index a1048018..10abe1ce 100644 --- a/src/frontend/qt_sdl/Screen.cpp +++ b/src/frontend/qt_sdl/Screen.cpp @@ -385,6 +385,10 @@ bool ScreenPanel::event(QEvent* event) touchEvent((QTouchEvent*)event); return true; } + else if (event->type() == QEvent::FocusIn) + mainWindow->onFocusIn(); + else if (event->type() == QEvent::FocusOut) + mainWindow->onFocusOut(); return QWidget::event(event); } diff --git a/src/frontend/qt_sdl/Window.cpp b/src/frontend/qt_sdl/Window.cpp index aed8076a..596a0f5c 100644 --- a/src/frontend/qt_sdl/Window.cpp +++ b/src/frontend/qt_sdl/Window.cpp @@ -235,7 +235,8 @@ MainWindow::MainWindow(int id, EmuInstance* inst, QWidget* parent) : localCfg(inst->localCfg), windowCfg(localCfg.GetTable("Window"+std::to_string(id), "Window0")), emuThread(inst->getEmuThread()), - enabledSaved(false) + enabledSaved(false), + focused(true) { #ifndef _WIN32 if (!parent) @@ -1015,13 +1016,26 @@ void MainWindow::dropEvent(QDropEvent* event) void MainWindow::focusInEvent(QFocusEvent* event) { - emuInstance->audioMute(); + onFocusIn(); } void MainWindow::focusOutEvent(QFocusEvent* event) +{ + onFocusOut(); +} + +void MainWindow::onFocusIn() +{ + focused = true; + if (emuInstance) + emuInstance->audioMute(); +} + +void MainWindow::onFocusOut() { // focusOutEvent is called through the window close event handler // prevent use after free + focused = false; if (emuInstance) emuInstance->audioMute(); } diff --git a/src/frontend/qt_sdl/Window.h b/src/frontend/qt_sdl/Window.h index 121115c0..9f652f54 100644 --- a/src/frontend/qt_sdl/Window.h +++ b/src/frontend/qt_sdl/Window.h @@ -131,6 +131,10 @@ public: void onAppStateChanged(Qt::ApplicationState state); + void onFocusIn(); + void onFocusOut(); + bool isFocused() { return focused; } + void osdAddMessage(unsigned int color, const char* msg); // called when the MP interface is changed @@ -264,6 +268,8 @@ private: int windowID; bool enabledSaved; + bool focused; + EmuInstance* emuInstance; EmuThread* emuThread;