Skip to content

Commit

Permalink
[HID] Cleanup Keyboard Input (#15)
Browse files Browse the repository at this point in the history
Removed all mousehook specific code.
  • Loading branch information
AdrianCassar authored and marinesciencedude committed Nov 6, 2024
1 parent 6010695 commit e6d6586
Show file tree
Hide file tree
Showing 32 changed files with 480 additions and 2,457 deletions.
6 changes: 3 additions & 3 deletions src/xenia/app/emulator_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ void EmulatorWindow::EmulatorWindowListener::OnKeyDown(ui::KeyEvent& e) {
}

void EmulatorWindow::EmulatorWindowListener::OnMouseDown(ui::MouseEvent& e) {
//emulator_window_.OnMouseDown(e);
emulator_window_.OnMouseDown(e);
}

void EmulatorWindow::EmulatorWindowListener::OnMouseUp(ui::MouseEvent& e) {
//emulator_window_.OnMouseUp(e);
emulator_window_.OnMouseUp(e);
}

void EmulatorWindow::DisplayConfigGameConfigLoadCallback::PostGameConfigLoad() {
Expand Down Expand Up @@ -1443,7 +1443,7 @@ void EmulatorWindow::SetFullscreen(bool fullscreen) {
}
window_->SetFullscreen(fullscreen);
window_->SetCursorVisibility(fullscreen
? ui::Window::CursorVisibility::kHidden
? ui::Window::CursorVisibility::kAutoHidden
: ui::Window::CursorVisibility::kVisible);
}

Expand Down
8 changes: 4 additions & 4 deletions src/xenia/app/xenia_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,16 @@ std::vector<std::unique_ptr<hid::InputDriver>> EmulatorApp::CreateInputDrivers(
xe::hid::nop::Create(window, EmulatorWindow::kZOrderHidInput));
} else {
Factory<hid::InputDriver, ui::Window*, size_t> factory;
#if XE_PLATFORM_WIN32
// WinKey input driver should always be the last input driver added!
factory.Add("winkey", xe::hid::winkey::Create);
#endif // XE_PLATFORM_WIN32
#if XE_PLATFORM_WIN32
factory.Add("xinput", xe::hid::xinput::Create);
#endif // XE_PLATFORM_WIN32
#if !XE_PLATFORM_ANDROID
factory.Add("sdl", xe::hid::sdl::Create);
#endif // !XE_PLATFORM_ANDROID
#if XE_PLATFORM_WIN32
// WinKey input driver should always be the last input driver added!
factory.Add("winkey", xe::hid::winkey::Create);
#endif // XE_PLATFORM_WIN32
for (auto& driver : factory.CreateAll(cvars::hid, window,
EmulatorWindow::kZOrderHidInput)) {
if (XSUCCEEDED(driver->Setup())) {
Expand Down
40 changes: 0 additions & 40 deletions src/xenia/base/profiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,46 +271,6 @@ void Profiler::SetUserIO(size_t z_order, ui::Window* window,
}
#endif // XE_OPTION_PROFILING_UI
}

// Pass through mouse events.
window_->on_mouse_down.AddListener([](ui::MouseEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnMouseDown(e);
e.set_handled(true);
}
});
window_->on_mouse_up.AddListener([](ui::MouseEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnMouseUp(e);
e.set_handled(true);
}
});
window_->on_mouse_move.AddListener([](ui::MouseEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnMouseMove(e);
e.set_handled(true);
}
});
window_->on_mouse_wheel.AddListener([](ui::MouseEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnMouseWheel(e);
e.set_handled(true);
}
});

// Watch for toggle/mode keys and such.
window_->on_key_down.AddListener([](ui::KeyEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnKeyDown(e);
e.set_handled(true);
}
});
window_->on_key_up.AddListener([](ui::KeyEvent& e) {
if (Profiler::is_visible()) {
Profiler::ProfilerWindowInputListener::OnKeyUp(e);
e.set_handled(true);
}
});
}

void Profiler::Flip() {
Expand Down
14 changes: 7 additions & 7 deletions src/xenia/base/profiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "xenia/ui/virtual_key.h"
#include "xenia/ui/window_listener.h"

#if XE_PLATFORM_WIN32
#if XE_PLATFORM_WIN32 && 0
#define XE_OPTION_PROFILING 1
#define XE_OPTION_PROFILING_UI 1
#else
Expand Down Expand Up @@ -204,13 +204,13 @@ class Profiler {
#if XE_OPTION_PROFILING
class ProfilerWindowInputListener final : public ui::WindowInputListener {
public:
static void OnKeyDown(ui::KeyEvent& e);
static void OnKeyUp(ui::KeyEvent& e);
void OnKeyDown(ui::KeyEvent& e) override;
void OnKeyUp(ui::KeyEvent& e) override;
#if XE_OPTION_PROFILING_UI
static void OnMouseDown(ui::MouseEvent& e);
static void OnMouseMove(ui::MouseEvent& e);
static void OnMouseUp(ui::MouseEvent& e);
static void OnMouseWheel(ui::MouseEvent& e);
void OnMouseDown(ui::MouseEvent& e) override;
void OnMouseMove(ui::MouseEvent& e) override;
void OnMouseUp(ui::MouseEvent& e) override;
void OnMouseWheel(ui::MouseEvent& e) override;
#endif // XE_OPTION_PROFILING_UI
};
// For now, no need for OnDpiChanged in a WindowListener because redrawing is
Expand Down
144 changes: 2 additions & 142 deletions src/xenia/emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ DEFINE_string(
"or the module specified by the game. Leave blank to launch the default "
"module.",
"General");

DEFINE_bool(ge_remove_blur, false,
"(GoldenEye) Removes low-res blur when in classic-graphics mode", "MouseHook");
DEFINE_bool(ge_debug_menu, false,
"(GoldenEye) Enables the debug menu, accessible with LB/1", "MouseHook");

DEFINE_bool(allow_game_relative_writes, false,
"Not useful to non-developers. Allows code to write to paths "
Expand Down Expand Up @@ -1402,7 +1397,6 @@ X_STATUS Emulator::CompleteLaunch(const std::filesystem::path& path,
title_id_ = std::nullopt;
title_name_ = "";
title_version_ = "";
executable_path_.clear();
display_window_->SetIcon(nullptr, 0);

// Allow xam to request module loads.
Expand All @@ -1427,9 +1421,6 @@ X_STATUS Emulator::CompleteLaunch(const std::filesystem::path& path,
XELOGE("Failed to initialize user module {}", xe::path_to_utf8(path));
return result;
}

executable_path_ = path;

// Grab the current title ID.
xex2_opt_execution_info* info = nullptr;
uint32_t workspace_address = 0;
Expand Down Expand Up @@ -1533,139 +1524,6 @@ X_STATUS Emulator::CompleteLaunch(const std::filesystem::path& path,
}
}
}

auto patch_addr = [module](uint32_t addr, uint32_t value) {
auto* patch_ptr =
(xe::be<uint32_t>*)module->memory()->TranslateVirtual(addr);
auto heap = module->memory()->LookupHeap(addr);

uint32_t old_protect = 0;
heap->Protect(addr, 4, kMemoryProtectRead | kMemoryProtectWrite,
&old_protect);
*patch_ptr = value;
heap->Protect(addr, 4, old_protect);
};

if (module->title_id() == 0x584109C2) {
// Prevent game from writing RS thumbstick to crosshair/gun position
// Multiple PD revisions so we'll need to search the code...

std::vector<uint32_t> search_insns = {
0xD17F16A8, // stfs f11, 0x16A8(r31)
0xD19F16A4, // stfs f12, 0x16A4(r31)
0xD19F1690, // stfs f12, 0x1690(r31)
0xD15F1694, // stfs f10, 0x1694(r31)
0xD0FF0CFC, // stfs f7, 0xCFC(r31)
0xD0BF0D00 // stfs f5, 0xD00(r31)
};

int patched = 0;

auto* xex = module->xex_module();
auto* check_addr =
(xe::be<uint32_t>*)module->memory()->TranslateVirtual(
xex->base_address());
auto* end_addr = (xe::be<uint32_t>*)module->memory()->TranslateVirtual(
xex->base_address() + xex->image_size());

while (end_addr > check_addr) {
auto value = *check_addr;

for (auto test : search_insns) {
if (test == value) {
uint32_t addr = module->memory()->HostToGuestVirtual(check_addr);
patch_addr(addr, 0x60000000);
patched++;
break;
}
}

check_addr++;
}
}

if (module->title_id() == 0x584108A9) {
struct GEPatchOffsets {
uint32_t check_addr;
uint32_t check_value;

uint32_t crosshair_addr1;
uint32_t crosshair_patch1;
uint32_t crosshair_addr2;
uint32_t crosshair_patch2;

uint32_t returnarcade_addr1;
uint32_t returnarcade_patch1;
uint32_t returnarcade_addr2;
uint32_t returnarcade_patch2;
uint32_t returnarcade_addr3;
uint32_t returnarcade_patch3;

uint32_t blur_addr;
uint32_t debug_addr;
};

std::vector<GEPatchOffsets> supported_builds = {
// Nov 2007 Release build
{0x8200336C, 0x676f6c64, 0x820A45D0, 0x4800003C, 0x820A46D4,
0x4800003C, 0x820F7750, 0x2F1E0007, 0x820F7D04, 0x2F1A0007,
0x820F7780, 0x2B0A0003, 0x82188E70, 0x82189F28},

// Nov 2007 Team build
{0x82003398, 0x676f6c64, 0x820C85B0, 0x480000B0, 0x820C88B8,
0x480000B0, 0x8213ABE8, 0x2F0B0007, 0x8213AF0C, 0x2F0B0007,
0x8213ACB4, 0x2B0B0004, 0x8221DF34, 0},

// Nov 2007 Debug build
{0x82005540, 0x676f6c64, 0x822A2BFC, 0x480000B0, 0x822A2F04,
0x480000B0, 0x82344D04, 0x2F0B0007, 0x82345030, 0x2F0B0007,
0x82344DD0, 0x2B0B0004, 0x824AB510, 0},
};

for (auto& build : supported_builds) {
auto* test_addr =
(xe::be<uint32_t>*)module->memory()->TranslateVirtual(
build.check_addr);
if (*test_addr != build.check_value) {
continue;
}

// Prevent game from overwriting crosshair/gun positions
if (build.crosshair_addr1) {
patch_addr(build.crosshair_addr1, build.crosshair_patch1);
}
if (build.crosshair_addr2) {
patch_addr(build.crosshair_addr2, build.crosshair_patch2);
}

// Hide "return to arcade" menu option
if (build.returnarcade_addr1) {
patch_addr(build.returnarcade_addr1, build.returnarcade_patch1);
}
if (build.returnarcade_addr2) {
patch_addr(build.returnarcade_addr2, build.returnarcade_patch2);
}
// Prevent "return to arcade" code from being executed
if (build.returnarcade_addr3) {
patch_addr(build.returnarcade_addr3, build.returnarcade_patch3);
}

if (cvars::ge_remove_blur && build.blur_addr) {
// Patch out N64 blur
// Source:
// https://github.com/xenia-canary/game-patches/blob/main/patches/584108A9.patch

patch_addr(build.blur_addr, 0x60000000);
}

if (cvars::ge_debug_menu && build.debug_addr) {
// Enable debug menu
patch_addr(build.debug_addr, 0x2B0B0000);
}

break;
}
}

// Initializing the shader storage in a blocking way so the user doesn't
// miss the initial seconds - for instance, sound from an intro video may
Expand All @@ -1683,6 +1541,8 @@ X_STATUS Emulator::CompleteLaunch(const std::filesystem::path& path,
main_thread_ = main_thread;
on_launch(title_id_.value(), title_name_);

input_system()->UpdateTitleId(title_id_.value());

// Plugins must be loaded after calling LaunchModule() and
// FinishLoadingUserModule() which will apply TUs and patching to the main
// xex.
Expand Down
7 changes: 0 additions & 7 deletions src/xenia/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ class Emulator {

// Version of the title as a string.
const std::string& title_version() const { return title_version_; }

// Host path of the executable being ran
const std::filesystem::path& executable_path() const {
return executable_path_;
}

// Currently running title ID
uint32_t title_id() const {
Expand Down Expand Up @@ -307,8 +302,6 @@ class Emulator {
std::filesystem::path storage_root_;
std::filesystem::path content_root_;
std::filesystem::path cache_root_;

std::filesystem::path executable_path_;

std::string title_name_;
std::string title_version_;
Expand Down
2 changes: 2 additions & 0 deletions src/xenia/hid/input_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class InputDriver {
is_active_callback_ = is_active_callback;
}

uint32_t title_id = 0;

protected:
explicit InputDriver(xe::ui::Window* window, size_t window_z_order)
: window_(window), window_z_order_(window_z_order) {}
Expand Down
7 changes: 7 additions & 0 deletions src/xenia/hid/input_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ X_INPUT_VIBRATION InputSystem::ModifyVibrationLevel(
modified_vibration.right_motor_speed = 0;
return modified_vibration;
}

void InputSystem::UpdateTitleId(uint32_t title_id) {
for (auto& driver : drivers_) {
driver->title_id = title_id;
}
}

std::unique_lock<xe_unlikely_mutex> InputSystem::lock() {
return std::unique_lock<xe_unlikely_mutex>{lock_};
}
Expand Down
2 changes: 2 additions & 0 deletions src/xenia/hid/input_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class InputSystem {

uint32_t GetLastUsedSlot() const { return last_used_slot; }

void UpdateTitleId(uint32_t title_id);

std::unique_lock<xe_unlikely_mutex> lock();

private:
Expand Down
6 changes: 3 additions & 3 deletions src/xenia/hid/sdl/sdl_input_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ X_RESULT SDLInputDriver::GetKeystroke(uint32_t users, uint32_t flags,
if (!(butts_changed & fbutton)) {
continue;
}
ui::VirtualKey vk = kVkLookup.at(last.repeat_butt_idx);
ui::VirtualKey vk = kVkLookup.at(i);
if (vk == ui::VirtualKey::kNone) {
continue;
}
Expand Down Expand Up @@ -635,9 +635,9 @@ void SDLInputDriver::OnControllerDeviceButtonChanged(const SDL_Event& event) {
if (xbutton == X_INPUT_GAMEPAD_GUIDE && !cvars::guide_button) {
return;
}
xbuttons |= (uint16_t)xbutton;
xbuttons |= xbutton;
} else {
xbuttons &= ~(uint16_t)xbutton;
xbuttons &= ~xbutton;
}
controller.state.gamepad.buttons = xbuttons;
controller.state_changed = true;
Expand Down
Loading

0 comments on commit e6d6586

Please sign in to comment.