Skip to content

Commit

Permalink
Merge pull request hrydgard#19008 from hrydgard/more-ra-fixes
Browse files Browse the repository at this point in the history
RAIntegration: Run window-modifying code on the correct thread
  • Loading branch information
hrydgard authored Apr 6, 2024
2 parents 7da7289 + 80a12ca commit 764e84c
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 24 deletions.
11 changes: 8 additions & 3 deletions Common/System/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const char *RequestTypeAsString(SystemRequestType type) {
}
}

bool RequestManager::MakeSystemRequest(SystemRequestType type, RequesterToken token, RequestCallback callback, RequestFailedCallback failedCallback, std::string_view param1, std::string_view param2, int param3) {
bool RequestManager::MakeSystemRequest(SystemRequestType type, RequesterToken token, RequestCallback callback, RequestFailedCallback failedCallback, std::string_view param1, std::string_view param2, int64_t param3, int64_t param4) {
if (token == NO_REQUESTER_TOKEN) {
_dbg_assert_(!callback);
_dbg_assert_(!failedCallback);
Expand All @@ -52,14 +52,13 @@ bool RequestManager::MakeSystemRequest(SystemRequestType type, RequesterToken to
std::string p1(param1);
std::string p2(param2);
// TODO: Convert to string_view
if (!System_MakeRequest(type, requestId, p1, p2, param3)) {
if (!System_MakeRequest(type, requestId, p1, p2, param3, param4)) {
if (callback || failedCallback) {
std::lock_guard<std::mutex> guard(callbackMutex_);
callbackMap_.erase(requestId);
}
return false;
}

return true;
}

Expand Down Expand Up @@ -145,3 +144,9 @@ void System_ShowFileInFolder(const Path &path) {
void System_BrowseForFolder(RequesterToken token, std::string_view title, const Path &initialPath, RequestCallback callback, RequestFailedCallback failedCallback) {
g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FOLDER, token, callback, failedCallback, title, initialPath.ToCString(), 0);
}

void System_RunCallbackInWndProc(void (*callback)(void *, void *), void *userdata) {
int64_t castPtr = (int64_t)callback;
int64_t castUserData = (int64_t)userdata;
g_requestManager.MakeSystemRequest(SystemRequestType::RUN_CALLBACK_IN_WNDPROC, NO_REQUESTER_TOKEN, nullptr, nullptr, "", "", castPtr, castUserData);
}
5 changes: 4 additions & 1 deletion Common/System/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class RequestManager {
// The callback you pass in will be called on the main thread later.
// Params are at the end since it's the part most likely to recieve additions in the future,
// now that we have both callbacks.
bool MakeSystemRequest(SystemRequestType type, RequesterToken token, RequestCallback callback, RequestFailedCallback failedCallback, std::string_view param1, std::string_view param2, int param3);
// Pointers can be passed through param3 and param4 if needed, by casting.
bool MakeSystemRequest(SystemRequestType type, RequesterToken token, RequestCallback callback, RequestFailedCallback failedCallback, std::string_view param1, std::string_view param2, int64_t param3, int64_t param4 = 0);

// Called by the platform implementation, when it's finished with a request.
void PostSystemSuccess(int requestId, const char *responseString, int responseValue = 0);
Expand Down Expand Up @@ -175,6 +176,8 @@ inline void System_SendDebugScreenshot(std::string_view data, int height) {
g_requestManager.MakeSystemRequest(SystemRequestType::SEND_DEBUG_SCREENSHOT, NO_REQUESTER_TOKEN, nullptr, nullptr, data, "", height);
}

void System_RunCallbackInWndProc(void (*callback)(void *, void *), void *userdata);

// Non-inline to avoid including Path.h
void System_CreateGameShortcut(const Path &path, std::string_view title);
void System_ShowFileInFolder(const Path &path);
4 changes: 3 additions & 1 deletion Common/System/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ enum class SystemRequestType {
GPS_COMMAND,
INFRARED_COMMAND,
MICROPHONE_COMMAND,

RUN_CALLBACK_IN_WNDPROC,
};

// Implementations are supposed to process the request, and post the response to the g_RequestManager (see Message.h).
// This is not to be used directly by applications, instead use the g_RequestManager to make the requests.
// This can return false if it's known that the platform doesn't support the request, the app is supposed to handle
// or ignore that cleanly.
// Some requests don't use responses.
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3);
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4);

PermissionStatus System_GetPermissionStatus(SystemPermission permission);
void System_AskForPermission(SystemPermission permission);
Expand Down
4 changes: 2 additions & 2 deletions Common/VR/VRRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ bool VR_InitFrame( engine_t* engine ) {
}

// Update passthrough
if (passthroughRunning != VR_GetConfig(VR_CONFIG_PASSTHROUGH)) {
if (passthroughRunning != (VR_GetConfig(VR_CONFIG_PASSTHROUGH) != 0)) {
if (VR_GetConfig(VR_CONFIG_PASSTHROUGH)) {
OXR(xrPassthroughLayerResumeFB(passthroughLayer));
} else {
OXR(xrPassthroughLayerPauseFB(passthroughLayer));
}
passthroughRunning = VR_GetConfig(VR_CONFIG_PASSTHROUGH);
passthroughRunning = (VR_GetConfig(VR_CONFIG_PASSTHROUGH) != 0);
}

// NOTE: OpenXR does not use the concept of frame indices. Instead,
Expand Down
13 changes: 9 additions & 4 deletions Core/RetroAchievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,10 @@ static void raintegration_event_handler(const rc_client_raintegration_event_t *e
case RC_CLIENT_RAINTEGRATION_EVENT_MENUITEM_CHECKED_CHANGED:
// The checked state of one of the menu items has changed and should be reflected in the UI.
// Call the handy helper function if the menu was created by rc_client_raintegration_rebuild_submenu.
rc_client_raintegration_update_menu_item(client, event->menu_item);
System_RunCallbackInWndProc([](void *vhWnd, void *userdata) {
auto menuItem = reinterpret_cast<const rc_client_raintegration_menu_item_t *>(userdata);
rc_client_raintegration_update_menu_item(g_rcClient, menuItem);
}, reinterpret_cast<void *>(reinterpret_cast<int64_t>(event->menu_item)));
break;
case RC_CLIENT_RAINTEGRATION_EVENT_PAUSE:
// The toolkit has hit a breakpoint and wants to pause the emulator. Do so.
Expand Down Expand Up @@ -530,9 +533,11 @@ static void load_integration_callback(int result, const char *error_message, rc_
rc_client_raintegration_set_event_handler(g_rcClient, &raintegration_event_handler);
rc_client_raintegration_set_write_memory_function(g_rcClient, &raintegration_write_memory_handler);
rc_client_raintegration_set_get_game_name_function(g_rcClient, &raintegration_get_game_name_handler);
HWND hWnd = (HWND)userdata;
rc_client_raintegration_rebuild_submenu(g_rcClient, GetMenu(hWnd));
DrawMenuBar(hWnd);

System_RunCallbackInWndProc([](void *vhWnd, void *userdata) {
HWND hWnd = reinterpret_cast<HWND>(vhWnd);
rc_client_raintegration_rebuild_submenu(g_rcClient, GetMenu(hWnd));
}, nullptr);
break;
}
case RC_MISSING_VALUE:
Expand Down
4 changes: 2 additions & 2 deletions Qt/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool MainUI::HandleCustomEvent(QEvent *e) {
return true;
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::EXIT_APP:
qApp->exit(0);
Expand Down Expand Up @@ -388,7 +388,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
}
case SystemRequestType::BROWSE_FOR_IMAGE:
// Fall back to file browser.
return System_MakeRequest(SystemRequestType::BROWSE_FOR_FILE, requestId, param1, param2, (int)BrowseFileType::IMAGE);
return System_MakeRequest(SystemRequestType::BROWSE_FOR_FILE, requestId, param1, param2, (int)BrowseFileType::IMAGE, 0);
case SystemRequestType::BROWSE_FOR_FILE:
g_requestId = requestId;
g_param1 = param1;
Expand Down
2 changes: 1 addition & 1 deletion SDL/CocoaBarItems.mm
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ -(void)toggle##name: (NSMenuItem *)item { \
TOGGLE_METHOD(IgnoreIllegalRWs, g_Config.bIgnoreBadMemAccess)
TOGGLE_METHOD(AutoFrameSkip, g_Config.bAutoFrameSkip, g_Config.UpdateAfterSettingAutoFrameSkip())
TOGGLE_METHOD(SoftwareRendering, g_Config.bSoftwareRendering)
TOGGLE_METHOD(FullScreen, g_Config.bFullScreen, System_MakeRequest(SystemRequestType::TOGGLE_FULLSCREEN_STATE, 0, g_Config.UseFullScreen() ? "1" : "0", "", 3))
TOGGLE_METHOD(FullScreen, g_Config.bFullScreen, System_MakeRequest(SystemRequestType::TOGGLE_FULLSCREEN_STATE, 0, g_Config.UseFullScreen() ? "1" : "0", "", 3, 0))
// TOGGLE_METHOD(VSync, g_Config.bVSync)
#undef TOGGLE_METHOD

Expand Down
2 changes: 1 addition & 1 deletion SDL/SDLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void System_Vibrate(int length_ms) {
// Ignore on PC
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::RESTART_APP:
g_RestartRequested = true;
Expand Down
4 changes: 2 additions & 2 deletions UWP/PPSSPP_UWPMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void System_Notify(SystemNotification notification) {
}
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {

case SystemRequestType::EXIT_APP:
Expand All @@ -478,7 +478,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
ExecuteTask(error, Windows::ApplicationModel::Core::CoreApplication::RequestRestartAsync(nullptr));
if (error != Windows::ApplicationModel::Core::AppRestartFailureReason::RestartPending) {
// Shutdown
System_MakeRequest(SystemRequestType::EXIT_APP, requestId, param1, param2, param3);
System_MakeRequest(SystemRequestType::EXIT_APP, requestId, param1, param2, param3, param4);
}
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions Windows/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,13 @@ namespace MainWindow
}
break;

case WM_USER_RUN_CALLBACK:
{
auto callback = reinterpret_cast<void (*)(void *window, void *userdata)>(wParam);
void *userdata = reinterpret_cast<void *>(lParam);
callback(hWnd, userdata);
break;
}
case WM_USER_GET_BASE_POINTER:
Reporting::NotifyDebugger();
switch (lParam) {
Expand Down Expand Up @@ -1135,4 +1142,8 @@ namespace MainWindow
return g_isFullscreen;
}

void RunCallbackInWndProc(void (*callback)(void *, void *), void *userdata) {
PostMessage(hwndMain, WM_USER_RUN_CALLBACK, reinterpret_cast<WPARAM>(callback), reinterpret_cast<LPARAM>(userdata));
}

} // namespace
4 changes: 3 additions & 1 deletion Windows/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace MainWindow
WM_USER_WINDOW_TITLE_CHANGED = WM_USER + 103,
WM_USER_TOGGLE_FULLSCREEN = WM_USER + 105,
WM_USER_RESTART_EMUTHREAD = WM_USER + 106,
WM_USER_SWITCHUMD_UPDATED = WM_USER + 107
WM_USER_SWITCHUMD_UPDATED = WM_USER + 107,
WM_USER_RUN_CALLBACK = WM_USER + 108,
};

enum {
Expand Down Expand Up @@ -79,6 +80,7 @@ namespace MainWindow
void ToggleDebugConsoleVisibility();
void SetInternalResolution(int res = -1);
void SetWindowSize(int zoom);
void RunCallbackInWndProc(void (*callback)(void *window, void *userdata), void *userdata);
}

#endif
9 changes: 8 additions & 1 deletion Windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ std::wstring MakeFilter(std::wstring filter) {
return filter;
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::EXIT_APP:
if (!NativeIsRestarting()) {
Expand Down Expand Up @@ -636,6 +636,13 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
}
case SystemRequestType::CREATE_GAME_SHORTCUT:
return W32Util::CreateDesktopShortcut(param1, param2);
case SystemRequestType::RUN_CALLBACK_IN_WNDPROC:
{
auto func = reinterpret_cast<void (*)(void *window, void *userdata)>(param3);
void *userdata = reinterpret_cast<void *>(param4);
MainWindow::RunCallbackInWndProc(func, userdata);
return true;
}
default:
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion android/jni/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ void System_Notify(SystemNotification notification) {
}
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::EXIT_APP:
PushCommand("finish", "");
Expand Down
2 changes: 1 addition & 1 deletion headless/Headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool System_GetPropertyBool(SystemProperty prop) {
}
void System_Notify(SystemNotification notification) {}
void System_PostUIMessage(UIMessage message, const std::string &param) {}
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::SEND_DEBUG_OUTPUT:
if (g_headlessHost) {
Expand Down
2 changes: 1 addition & 1 deletion ios/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void System_Notify(SystemNotification notification) {
}
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) {
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) {
switch (type) {
case SystemRequestType::EXIT_APP:
exit(0);
Expand Down
2 changes: 1 addition & 1 deletion libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ void System_Notify(SystemNotification notification) {
break;
}
}
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) { return false; }
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) { return false; }
void System_PostUIMessage(UIMessage message, const std::string &param) {}
void NativeFrame(GraphicsContext *graphicsContext) {}
void NativeResized() {}
Expand Down
2 changes: 1 addition & 1 deletion unittest/JitHarness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
void NativeFrame(GraphicsContext *graphicsContext) { }
void NativeResized() { }

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) { return false; }
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int64_t param3, int64_t param4) { return false; }
void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function<void(bool, const std::string &)> cb) { cb(false, ""); }
void System_AskForPermission(SystemPermission permission) {}
PermissionStatus System_GetPermissionStatus(SystemPermission permission) { return PERMISSION_STATUS_GRANTED; }
Expand Down

0 comments on commit 764e84c

Please sign in to comment.