Thread Control optimize

This commit is contained in:
PlayXboxtion963 2025-12-15 20:19:54 +08:00
parent 3ffa9f3ba7
commit fbb0f9b578
4 changed files with 28 additions and 19 deletions

View File

@ -1423,22 +1423,11 @@ class SettingsFragmentPresenter(
}
}, R.string.real_balance_board, 0))
sl.add(SwitchSetting(context, object : AbstractBooleanSetting {
override val isOverridden: Boolean = BooleanSetting.SERVERS_ENABLED.isOverridden
override val isRuntimeEditable: Boolean =
BooleanSetting.SERVERS_ENABLED.isRuntimeEditable
override fun delete(settings: Settings): Boolean {
return BooleanSetting.SERVERS_ENABLED.delete(settings)
}
override val boolean: Boolean get() = BooleanSetting.SERVERS_ENABLED.boolean;
override fun setBoolean(settings: Settings, newValue: Boolean) {
BooleanSetting.SERVERS_ENABLED.setBoolean(settings, if (newValue) true else false);
}
}, R.string.dualshockudp_client, R.string.dualshockudp_client_description))
sl.add(SwitchSetting(context,
BooleanSetting.SERVERS_ENABLED,
R.string.dualshockudp_client,
R.string.dualshockudp_client_description)
)
sl.add(
InputStringSetting(

View File

@ -24,7 +24,7 @@
<string name="wiimote_extension_3">Wii Remote Extension 4</string>
<string name="real_balance_board">Real Balance Board</string>
<string name="dualshockudp_client">DualshockUDP Client (Background Input)</string>
<string name="dualshockudp_client">DSU Input</string>
<string name="dualshockudp_entries">DualshockUDP Server Info (Alternate Input Source)</string>
<string name="dualshockudp_entries_description">Description:IP:Port;</string>

View File

@ -6,6 +6,9 @@
#include "Common/Logging/Log.h"
#include "Core/AchievementManager.h"
// Minimal bridge to notify native code to pause/resume background input updates.
extern "C" void SetBackgroundInputExecutionAllowed(bool allowed);
extern "C" {
JNIEXPORT void JNICALL
@ -17,5 +20,6 @@ Java_org_dolphinemu_dolphinemu_utils_ActivityTracker_setBackgroundExecutionAllow
INFO_LOG_FMT(CORE, "SetBackgroundExecutionAllowed {}", allowed);
AchievementManager::GetInstance().SetBackgroundExecutionAllowed(allowed);
SetBackgroundInputExecutionAllowed(static_cast<bool>(allowed));
}
}

View File

@ -9,6 +9,7 @@
#include <optional>
#include <string>
#include <thread>
#include <atomic>
#include <utility>
#include <vector>
@ -59,7 +60,6 @@
#include "InputCommon/GCAdapter.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "UICommon/GameFile.h"
#include "UICommon/UICommon.h"
@ -88,6 +88,8 @@ bool s_need_nonblocking_alert_msg;
Common::Flag s_is_booting;
bool s_game_metadata_is_valid = false;
// When false, the controller-update thread will pause updates to save battery.
std::atomic<bool> s_background_execution_allowed{true};
} // Anonymous namespace
void UpdatePointer()
@ -566,10 +568,17 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Initialize(J
AchievementManager::GetInstance().Init(nullptr);
// Start a background thread to periodically update controller input (every 10ms).
// This ensures Android keeps controller state updated even without a dedicated input loop.
// The thread checks `s_background_execution_allowed` and pauses updates when
// background execution is disallowed (to save battery).
std::thread([]() {
while (true)
{
if (!s_background_execution_allowed.load())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
g_controller_interface.SetCurrentInputChannel(ciface::InputChannel::Host);
g_controller_interface.UpdateInput();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
@ -589,6 +598,13 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GenerateNewS
DolphinAnalytics::Instance().GenerateNewIdentity();
}
// Called from Java-side ActivityTracker native shim to allow/disable background
// controller updates. Kept minimal: just flip the flag the thread checks.
extern "C" void SetBackgroundInputExecutionAllowed(bool allowed)
{
s_background_execution_allowed.store(allowed);
}
// Returns the scale factor for imgui rendering.
// Based on the scaledDensity of the device's display metrics.
static float GetRenderSurfaceScale(JNIEnv* env)