From a06a005124227fe3f2a4acf996cf85f4bb85772d Mon Sep 17 00:00:00 2001 From: nmzik Date: Wed, 2 Sep 2026 01:14:02 +0400 Subject: [PATCH] controller: simplify input facade API --- src/graphics/presentation/imeOverlay.cpp | 4 +-- .../presentation/window/hostInput.cpp | 20 +++++------ src/graphics/presentation/window/window.cpp | 16 ++++----- src/libs/controller.cpp | 36 ++++--------------- src/libs/controller.h | 14 ++++---- 5 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/graphics/presentation/imeOverlay.cpp b/src/graphics/presentation/imeOverlay.cpp index 374e8187f..afb9903aa 100644 --- a/src/graphics/presentation/imeOverlay.cpp +++ b/src/graphics/presentation/imeOverlay.cpp @@ -209,7 +209,7 @@ void RefreshVisibility() { } const bool was_controller = std::exchange(g_controller_captured, capture_controller); if (capture_controller || was_controller) { - Controller::ControllerResetInputState(); + Controller::ResetInputState(); } g_active = visible; @@ -404,7 +404,7 @@ void ShutdownImeInput() { g_input_lifecycle_active = false; g_active = false; if (std::exchange(g_controller_captured, false)) { - Controller::ControllerResetInputState(); + Controller::ResetInputState(); } g_visibility_event.store(static_cast(-1), std::memory_order_release); } diff --git a/src/graphics/presentation/window/hostInput.cpp b/src/graphics/presentation/window/hostInput.cpp index 8e6378094..22ba8ae37 100644 --- a/src/graphics/presentation/window/hostInput.cpp +++ b/src/graphics/presentation/window/hostInput.cpp @@ -194,18 +194,18 @@ const InputMap& GetInputMap() { void SetButton(uint32_t button, bool down) { if (button == Controller::PAD_BUTTON_L2) { - Controller::ControllerAxis(Controller::HOST_INPUT_CONTROLLER_ID, - Controller::Axis::TriggerLeft, down ? 255 : 0); + Controller::SetAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::Axis::TriggerLeft, + down ? 255 : 0); } else if (button == Controller::PAD_BUTTON_R2) { - Controller::ControllerAxis(Controller::HOST_INPUT_CONTROLLER_ID, - Controller::Axis::TriggerRight, down ? 255 : 0); + Controller::SetAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::Axis::TriggerRight, + down ? 255 : 0); } else if (button != 0) { - Controller::ControllerButton(Controller::HOST_INPUT_CONTROLLER_ID, button, down); + Controller::SetButton(Controller::HOST_INPUT_CONTROLLER_ID, button, down); } } void SetTouchPad(float x, bool down) { - Controller::ControllerTouchPad(Controller::HOST_INPUT_CONTROLLER_ID, 0, down, x, 0.5f); + Controller::SetTouchPad(Controller::HOST_INPUT_CONTROLLER_ID, 0, down, x, 0.5f); } uint32_t DefaultKeyboardButton(int key_code) { @@ -242,7 +242,7 @@ void SetStickAxis(Controller::Axis axis, bool negative, bool positive) { } else if (positive && !negative) { value = 255; } - Controller::ControllerAxis(Controller::HOST_INPUT_CONTROLLER_ID, axis, value); + Controller::SetAxis(Controller::HOST_INPUT_CONTROLLER_ID, axis, value); } void SetControl(std::size_t control, bool down) { @@ -326,15 +326,15 @@ void MouseToJoystick(int delta_x, int delta_y) { const auto map_axis = [scale](int delta) { return std::clamp(128 + static_cast(std::lround(delta * scale)), 0, 255); }; - Controller::ControllerRightStick(Controller::HOST_INPUT_CONTROLLER_ID, map_axis(delta_x), - map_axis(delta_y)); + Controller::SetRightStick(Controller::HOST_INPUT_CONTROLLER_ID, map_axis(delta_x), + map_axis(delta_y)); } void CenterMouseStick() { if (!g_mouse.output) { return; } - Controller::ControllerRightStick(Controller::HOST_INPUT_CONTROLLER_ID, 128, 128); + Controller::SetRightStick(Controller::HOST_INPUT_CONTROLLER_ID, 128, 128); g_mouse.output = false; } diff --git a/src/graphics/presentation/window/window.cpp b/src/graphics/presentation/window/window.cpp index ca40fefa0..dc2eb9a5a 100644 --- a/src/graphics/presentation/window/window.cpp +++ b/src/graphics/presentation/window/window.cpp @@ -351,26 +351,26 @@ static void GameEventController([[maybe_unused]] const EventController& f) { auto* pad = SDL_GameControllerOpen(f.id); EXIT_NOT_IMPLEMENTED(pad == nullptr); int id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad)); - Controller::ControllerConnect(id); + Controller::Connect(id); } if (f.removed) { - Controller::ControllerDisconnect(f.id); + Controller::Disconnect(f.id); SDL_GameControllerClose(SDL_GameControllerFromInstanceID(f.id)); } if (f.down || f.up) { const auto button = ControllerButtonToPadButton(f.button); if (button != 0) { - Controller::ControllerButton(f.id, button, f.down); + Controller::SetButton(f.id, button, f.down); } } if (f.axis) { const auto axis = ControllerAxisFromSdl(f.axis_id); if (axis != Controller::Axis::AxisMax) { - Controller::ControllerAxis(f.id, axis, - ControllerAxisValueFromSdl(f.axis_id, f.axis_value)); + Controller::SetAxis(f.id, axis, + ControllerAxisValueFromSdl(f.axis_id, f.axis_value)); } } } @@ -708,9 +708,9 @@ void WindowContext::ProcessEvent(double time_s) { case SDL_CONTROLLERTOUCHPADMOTION: case SDL_CONTROLLERTOUCHPADUP: if (event->ctouchpad.touchpad == 0) { - Controller::ControllerTouchPad(event->ctouchpad.which, event->ctouchpad.finger, - event->ctouchpad.type != SDL_CONTROLLERTOUCHPADUP, - event->ctouchpad.x, event->ctouchpad.y); + Controller::SetTouchPad(event->ctouchpad.which, event->ctouchpad.finger, + event->ctouchpad.type != SDL_CONTROLLERTOUCHPADUP, + event->ctouchpad.x, event->ctouchpad.y); } break; diff --git a/src/libs/controller.cpp b/src/libs/controller.cpp index abb9fe79b..fb1ec6263 100644 --- a/src/libs/controller.cpp +++ b/src/libs/controller.cpp @@ -515,44 +515,31 @@ int GameController::ReadStates(ControllerState* states, int states_num, bool* fl return ret_num; } -void ControllerConnect(int id) { - EXIT_IF(g_controller == nullptr); - +void Connect(int id) { g_controller->Connect(id); } -void ControllerDisconnect(int id) { - EXIT_IF(g_controller == nullptr); - +void Disconnect(int id) { g_controller->Disconnect(id); } -void ControllerButton(int id, uint32_t button, bool down) { - EXIT_IF(g_controller == nullptr); - +void SetButton(int id, uint32_t button, bool down) { g_controller->Button(id, button, down); } -void ControllerAxis(int id, Axis axis, int value) { - EXIT_IF(g_controller == nullptr); - +void SetAxis(int id, Axis axis, int value) { g_controller->Axis(id, axis, value); } -void ControllerRightStick(int id, int x, int y) { - EXIT_IF(g_controller == nullptr); - +void SetRightStick(int id, int x, int y) { g_controller->RightStick(id, x, y); } -void ControllerTouchPad(int id, int finger, bool down, float x, float y) { - EXIT_IF(g_controller == nullptr); - +void SetTouchPad(int id, int finger, bool down, float x, float y) { g_controller->TouchPad(id, finger, down, x, y); } -void ControllerResetInputState() { - EXIT_IF(g_controller == nullptr); +void ResetInputState() { g_controller->ResetInputState(); } @@ -647,8 +634,6 @@ int KYTY_SYSV_ABI PadResetOrientation(int handle) { int KYTY_SYSV_ABI PadGetControllerInformation(int handle, PadControllerInformation* info) { PRINT_NAME(); - EXIT_IF(g_controller == nullptr); - int connected_count = 0; bool connected = false; @@ -686,8 +671,6 @@ int KYTY_SYSV_ABI PadReadState(int handle, PadData* data) { return PAD_ERROR_INVALID_ARG; } - EXIT_IF(g_controller == nullptr); - int connected_count = 0; bool connected = false; ControllerState state; @@ -712,8 +695,6 @@ int KYTY_SYSV_ABI PadRead(int handle, PadData* data, int num) { std::memset(data, 0, sizeof(PadData) * static_cast(num)); - EXIT_IF(g_controller == nullptr); - int connected_count = 0; bool connected = false; ControllerState states[64] = {}; @@ -748,7 +729,6 @@ int KYTY_SYSV_ABI PadSetVibration(int handle, const PadVibrationParam* param) { "\t small_motor = %d\n", static_cast(param->large_motor), static_cast(param->small_motor)); - EXIT_IF(g_controller == nullptr); g_controller->SetVibration(param->large_motor, param->small_motor); return OK; @@ -774,7 +754,6 @@ int KYTY_SYSV_ABI PadSetLightBar(int handle, const PadLightBarParam* param) { return PAD_ERROR_INVALID_ARG; } - EXIT_IF(g_controller == nullptr); g_controller->SetLightBar(param->r, param->g, param->b); return OK; @@ -790,7 +769,6 @@ int KYTY_SYSV_ABI PadSetTriggerEffect(int handle, const PadTriggerEffectParam* p return PAD_ERROR_INVALID_ARG; } - EXIT_IF(g_controller == nullptr); return g_controller->SetTriggerEffect(*param) ? OK : PAD_ERROR_INVALID_ARG; } diff --git a/src/libs/controller.h b/src/libs/controller.h index 74a09fa8a..8a2edef2a 100644 --- a/src/libs/controller.h +++ b/src/libs/controller.h @@ -61,13 +61,13 @@ inline int controller_get_axis(int min, int max, int value) { return v; } -void ControllerConnect(int id); -void ControllerDisconnect(int id); -void ControllerButton(int id, uint32_t button, bool down); -void ControllerAxis(int id, Axis axis, int value); -void ControllerRightStick(int id, int x, int y); -void ControllerTouchPad(int id, int finger, bool down, float x, float y); -void ControllerResetInputState(); +void Connect(int id); +void Disconnect(int id); +void SetButton(int id, uint32_t button, bool down); +void SetAxis(int id, Axis axis, int value); +void SetRightStick(int id, int x, int y); +void SetTouchPad(int id, int finger, bool down, float x, float y); +void ResetInputState(); int KYTY_SYSV_ABI PadInit(); int KYTY_SYSV_ABI PadOpen(int user_id, int type, int index, const void* param);