loveplus: improve window searching for touch hook (#321)

## Link to GitHub Issue, if one exists
Fixes #320 

## Description of change
Replacing the existing logic that searches for game window to hook touch
on.

Eventually, we should fix all the other games that hook touch by
matching on window title with a call to `FindProcessWindowBeginsWith`.

## Testing
Tested with notepad open that was also titled `LovePlusAC`. Game hooked
correctly when the game was in focus during boot, and also when notepad
was in focus.
This commit is contained in:
bicarus-dev
2025-05-07 21:41:48 -07:00
committed by GitHub
parent 621b702ed4
commit 54028d8cbb
2 changed files with 29 additions and 7 deletions
+6 -7
View File
@@ -30,16 +30,15 @@ namespace games::loveplus {
if (!TOUCH_ATTACHED) { if (!TOUCH_ATTACHED) {
// Find the game window. // Find the game window.
// We check the foreground window first, then fall back to searching for the window title HWND wnd = FindProcessWindowBeginsWith("LovePlusAC");
// All game versions seem to have their model first in the window title
HWND wnd = GetForegroundWindow();
if (!string_begins_with(GetActiveWindowTitle(), "LovePlus")) {
wnd = FindWindowBeginsWith(avs::game::MODEL);
}
// attach touch hook // attach touch hook
if (wnd) { if (wnd) {
log_info("loveplus", "using window handle for touch: {}", fmt::ptr(wnd)); log_info(
"loveplus",
"using window handle for touch: {} ({})",
fmt::ptr(wnd),
get_window_title(wnd));
touch_create_wnd(wnd); touch_create_wnd(wnd);
} else { } else {
log_info("loveplus", "falling back to the DirectX window handle for touch"); log_info("loveplus", "falling back to the DirectX window handle for touch");
+23
View File
@@ -227,6 +227,7 @@ static inline std::vector<HWND> find_windows_beginning_with(const std::string &t
return windows; return windows;
} }
// exists for compat only; prefer to use FindProcessWindowBeginsWith instead
static inline HWND FindWindowBeginsWith(std::string title) { static inline HWND FindWindowBeginsWith(std::string title) {
// get all windows // get all windows
@@ -248,6 +249,28 @@ static inline HWND FindWindowBeginsWith(std::string title) {
return nullptr; return nullptr;
} }
static inline HWND FindProcessWindowBeginsWith(const std::string &title) {
// try foreground window first
HWND fg_win = GetForegroundWindow();
if (string_begins_with(get_window_title(fg_win), title)) {
DWORD fg_pid;
GetWindowThreadProcessId(fg_win, &fg_pid);
if (fg_pid == GetCurrentProcessId()) {
return fg_win;
}
}
// try different windows
for (const auto window : find_windows_beginning_with(title)) {
DWORD fg_pid;
GetWindowThreadProcessId(window, &fg_pid);
if (fg_pid == GetCurrentProcessId()) {
return window;
}
}
return nullptr;
}
static inline std::string get_last_error_string() { static inline std::string get_last_error_string() {
// get error // get error