controller: simplify input facade API

This commit is contained in:
nmzik
2026-09-02 01:14:02 +04:00
parent e9425e101d
commit a06a005124
5 changed files with 34 additions and 56 deletions
+2 -2
View File
@@ -209,7 +209,7 @@ void RefreshVisibility() {
} }
const bool was_controller = std::exchange(g_controller_captured, capture_controller); const bool was_controller = std::exchange(g_controller_captured, capture_controller);
if (capture_controller || was_controller) { if (capture_controller || was_controller) {
Controller::ControllerResetInputState(); Controller::ResetInputState();
} }
g_active = visible; g_active = visible;
@@ -404,7 +404,7 @@ void ShutdownImeInput() {
g_input_lifecycle_active = false; g_input_lifecycle_active = false;
g_active = false; g_active = false;
if (std::exchange(g_controller_captured, false)) { if (std::exchange(g_controller_captured, false)) {
Controller::ControllerResetInputState(); Controller::ResetInputState();
} }
g_visibility_event.store(static_cast<Uint32>(-1), std::memory_order_release); g_visibility_event.store(static_cast<Uint32>(-1), std::memory_order_release);
} }
+10 -10
View File
@@ -194,18 +194,18 @@ const InputMap& GetInputMap() {
void SetButton(uint32_t button, bool down) { void SetButton(uint32_t button, bool down) {
if (button == Controller::PAD_BUTTON_L2) { if (button == Controller::PAD_BUTTON_L2) {
Controller::ControllerAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::SetAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::Axis::TriggerLeft,
Controller::Axis::TriggerLeft, down ? 255 : 0); down ? 255 : 0);
} else if (button == Controller::PAD_BUTTON_R2) { } else if (button == Controller::PAD_BUTTON_R2) {
Controller::ControllerAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::SetAxis(Controller::HOST_INPUT_CONTROLLER_ID, Controller::Axis::TriggerRight,
Controller::Axis::TriggerRight, down ? 255 : 0); down ? 255 : 0);
} else if (button != 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) { 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) { uint32_t DefaultKeyboardButton(int key_code) {
@@ -242,7 +242,7 @@ void SetStickAxis(Controller::Axis axis, bool negative, bool positive) {
} else if (positive && !negative) { } else if (positive && !negative) {
value = 255; 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) { 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) { const auto map_axis = [scale](int delta) {
return std::clamp(128 + static_cast<int>(std::lround(delta * scale)), 0, 255); return std::clamp(128 + static_cast<int>(std::lround(delta * scale)), 0, 255);
}; };
Controller::ControllerRightStick(Controller::HOST_INPUT_CONTROLLER_ID, map_axis(delta_x), Controller::SetRightStick(Controller::HOST_INPUT_CONTROLLER_ID, map_axis(delta_x),
map_axis(delta_y)); map_axis(delta_y));
} }
void CenterMouseStick() { void CenterMouseStick() {
if (!g_mouse.output) { if (!g_mouse.output) {
return; return;
} }
Controller::ControllerRightStick(Controller::HOST_INPUT_CONTROLLER_ID, 128, 128); Controller::SetRightStick(Controller::HOST_INPUT_CONTROLLER_ID, 128, 128);
g_mouse.output = false; g_mouse.output = false;
} }
+8 -8
View File
@@ -351,26 +351,26 @@ static void GameEventController([[maybe_unused]] const EventController& f) {
auto* pad = SDL_GameControllerOpen(f.id); auto* pad = SDL_GameControllerOpen(f.id);
EXIT_NOT_IMPLEMENTED(pad == nullptr); EXIT_NOT_IMPLEMENTED(pad == nullptr);
int id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad)); int id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad));
Controller::ControllerConnect(id); Controller::Connect(id);
} }
if (f.removed) { if (f.removed) {
Controller::ControllerDisconnect(f.id); Controller::Disconnect(f.id);
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(f.id)); SDL_GameControllerClose(SDL_GameControllerFromInstanceID(f.id));
} }
if (f.down || f.up) { if (f.down || f.up) {
const auto button = ControllerButtonToPadButton(f.button); const auto button = ControllerButtonToPadButton(f.button);
if (button != 0) { if (button != 0) {
Controller::ControllerButton(f.id, button, f.down); Controller::SetButton(f.id, button, f.down);
} }
} }
if (f.axis) { if (f.axis) {
const auto axis = ControllerAxisFromSdl(f.axis_id); const auto axis = ControllerAxisFromSdl(f.axis_id);
if (axis != Controller::Axis::AxisMax) { if (axis != Controller::Axis::AxisMax) {
Controller::ControllerAxis(f.id, axis, Controller::SetAxis(f.id, axis,
ControllerAxisValueFromSdl(f.axis_id, f.axis_value)); ControllerAxisValueFromSdl(f.axis_id, f.axis_value));
} }
} }
} }
@@ -708,9 +708,9 @@ void WindowContext::ProcessEvent(double time_s) {
case SDL_CONTROLLERTOUCHPADMOTION: case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP: case SDL_CONTROLLERTOUCHPADUP:
if (event->ctouchpad.touchpad == 0) { if (event->ctouchpad.touchpad == 0) {
Controller::ControllerTouchPad(event->ctouchpad.which, event->ctouchpad.finger, Controller::SetTouchPad(event->ctouchpad.which, event->ctouchpad.finger,
event->ctouchpad.type != SDL_CONTROLLERTOUCHPADUP, event->ctouchpad.type != SDL_CONTROLLERTOUCHPADUP,
event->ctouchpad.x, event->ctouchpad.y); event->ctouchpad.x, event->ctouchpad.y);
} }
break; break;
+7 -29
View File
@@ -515,44 +515,31 @@ int GameController::ReadStates(ControllerState* states, int states_num, bool* fl
return ret_num; return ret_num;
} }
void ControllerConnect(int id) { void Connect(int id) {
EXIT_IF(g_controller == nullptr);
g_controller->Connect(id); g_controller->Connect(id);
} }
void ControllerDisconnect(int id) { void Disconnect(int id) {
EXIT_IF(g_controller == nullptr);
g_controller->Disconnect(id); g_controller->Disconnect(id);
} }
void ControllerButton(int id, uint32_t button, bool down) { void SetButton(int id, uint32_t button, bool down) {
EXIT_IF(g_controller == nullptr);
g_controller->Button(id, button, down); g_controller->Button(id, button, down);
} }
void ControllerAxis(int id, Axis axis, int value) { void SetAxis(int id, Axis axis, int value) {
EXIT_IF(g_controller == nullptr);
g_controller->Axis(id, axis, value); g_controller->Axis(id, axis, value);
} }
void ControllerRightStick(int id, int x, int y) { void SetRightStick(int id, int x, int y) {
EXIT_IF(g_controller == nullptr);
g_controller->RightStick(id, x, y); g_controller->RightStick(id, x, y);
} }
void ControllerTouchPad(int id, int finger, bool down, float x, float y) { void SetTouchPad(int id, int finger, bool down, float x, float y) {
EXIT_IF(g_controller == nullptr);
g_controller->TouchPad(id, finger, down, x, y); g_controller->TouchPad(id, finger, down, x, y);
} }
void ControllerResetInputState() { void ResetInputState() {
EXIT_IF(g_controller == nullptr);
g_controller->ResetInputState(); g_controller->ResetInputState();
} }
@@ -647,8 +634,6 @@ int KYTY_SYSV_ABI PadResetOrientation(int handle) {
int KYTY_SYSV_ABI PadGetControllerInformation(int handle, PadControllerInformation* info) { int KYTY_SYSV_ABI PadGetControllerInformation(int handle, PadControllerInformation* info) {
PRINT_NAME(); PRINT_NAME();
EXIT_IF(g_controller == nullptr);
int connected_count = 0; int connected_count = 0;
bool connected = false; bool connected = false;
@@ -686,8 +671,6 @@ int KYTY_SYSV_ABI PadReadState(int handle, PadData* data) {
return PAD_ERROR_INVALID_ARG; return PAD_ERROR_INVALID_ARG;
} }
EXIT_IF(g_controller == nullptr);
int connected_count = 0; int connected_count = 0;
bool connected = false; bool connected = false;
ControllerState state; 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<size_t>(num)); std::memset(data, 0, sizeof(PadData) * static_cast<size_t>(num));
EXIT_IF(g_controller == nullptr);
int connected_count = 0; int connected_count = 0;
bool connected = false; bool connected = false;
ControllerState states[64] = {}; ControllerState states[64] = {};
@@ -748,7 +729,6 @@ int KYTY_SYSV_ABI PadSetVibration(int handle, const PadVibrationParam* param) {
"\t small_motor = %d\n", "\t small_motor = %d\n",
static_cast<int>(param->large_motor), static_cast<int>(param->small_motor)); static_cast<int>(param->large_motor), static_cast<int>(param->small_motor));
EXIT_IF(g_controller == nullptr);
g_controller->SetVibration(param->large_motor, param->small_motor); g_controller->SetVibration(param->large_motor, param->small_motor);
return OK; return OK;
@@ -774,7 +754,6 @@ int KYTY_SYSV_ABI PadSetLightBar(int handle, const PadLightBarParam* param) {
return PAD_ERROR_INVALID_ARG; return PAD_ERROR_INVALID_ARG;
} }
EXIT_IF(g_controller == nullptr);
g_controller->SetLightBar(param->r, param->g, param->b); g_controller->SetLightBar(param->r, param->g, param->b);
return OK; return OK;
@@ -790,7 +769,6 @@ int KYTY_SYSV_ABI PadSetTriggerEffect(int handle, const PadTriggerEffectParam* p
return PAD_ERROR_INVALID_ARG; return PAD_ERROR_INVALID_ARG;
} }
EXIT_IF(g_controller == nullptr);
return g_controller->SetTriggerEffect(*param) ? OK : PAD_ERROR_INVALID_ARG; return g_controller->SetTriggerEffect(*param) ? OK : PAD_ERROR_INVALID_ARG;
} }
+7 -7
View File
@@ -61,13 +61,13 @@ inline int controller_get_axis(int min, int max, int value) {
return v; return v;
} }
void ControllerConnect(int id); void Connect(int id);
void ControllerDisconnect(int id); void Disconnect(int id);
void ControllerButton(int id, uint32_t button, bool down); void SetButton(int id, uint32_t button, bool down);
void ControllerAxis(int id, Axis axis, int value); void SetAxis(int id, Axis axis, int value);
void ControllerRightStick(int id, int x, int y); void SetRightStick(int id, int x, int y);
void ControllerTouchPad(int id, int finger, bool down, float x, float y); void SetTouchPad(int id, int finger, bool down, float x, float y);
void ControllerResetInputState(); void ResetInputState();
int KYTY_SYSV_ABI PadInit(); int KYTY_SYSV_ABI PadInit();
int KYTY_SYSV_ABI PadOpen(int user_id, int type, int index, const void* param); int KYTY_SYSV_ABI PadOpen(int user_id, int type, int index, const void* param);