summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/drivers/joycon.cpp49
-rw-r--r--src/input_common/drivers/joycon.h1
-rw-r--r--src/input_common/drivers/sdl_driver.cpp18
-rw-r--r--src/input_common/helpers/joycon_driver.cpp3
-rw-r--r--src/input_common/helpers/stick_from_buttons.cpp45
-rw-r--r--src/input_common/input_poller.cpp30
6 files changed, 98 insertions, 48 deletions
diff --git a/src/input_common/drivers/joycon.cpp b/src/input_common/drivers/joycon.cpp
index 4fcfb4510..afc33db57 100644
--- a/src/input_common/drivers/joycon.cpp
+++ b/src/input_common/drivers/joycon.cpp
@@ -16,7 +16,7 @@ namespace InputCommon {
Joycons::Joycons(const std::string& input_engine_) : InputEngine(input_engine_) {
// Avoid conflicting with SDL driver
- if (!Settings::values.enable_joycon_driver) {
+ if (!Settings::values.enable_joycon_driver && !Settings::values.enable_procon_driver) {
return;
}
LOG_INFO(Input, "Joycon driver Initialization started");
@@ -46,6 +46,12 @@ void Joycons::Reset() {
}
device->Stop();
}
+ for (const auto& device : pro_controller) {
+ if (!device) {
+ continue;
+ }
+ device->Stop();
+ }
SDL_hid_exit();
}
@@ -61,6 +67,11 @@ void Joycons::Setup() {
PreSetController(GetIdentifier(port, Joycon::ControllerType::Right));
device = std::make_shared<Joycon::JoyconDriver>(port++);
}
+ port = 0;
+ for (auto& device : pro_controller) {
+ PreSetController(GetIdentifier(port, Joycon::ControllerType::Pro));
+ device = std::make_shared<Joycon::JoyconDriver>(port++);
+ }
scan_thread = std::jthread([this](std::stop_token stop_token) { ScanThread(stop_token); });
}
@@ -116,6 +127,9 @@ bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const {
// Check if device already exist
switch (type) {
case Joycon::ControllerType::Left:
+ if (!Settings::values.enable_joycon_driver) {
+ return false;
+ }
for (const auto& device : left_joycons) {
if (is_handle_identical(device)) {
return false;
@@ -123,12 +137,25 @@ bool Joycons::IsDeviceNew(SDL_hid_device_info* device_info) const {
}
break;
case Joycon::ControllerType::Right:
+ if (!Settings::values.enable_joycon_driver) {
+ return false;
+ }
for (const auto& device : right_joycons) {
if (is_handle_identical(device)) {
return false;
}
}
break;
+ case Joycon::ControllerType::Pro:
+ if (!Settings::values.enable_procon_driver) {
+ return false;
+ }
+ for (const auto& device : pro_controller) {
+ if (is_handle_identical(device)) {
+ return false;
+ }
+ }
+ break;
default:
return false;
}
@@ -199,6 +226,14 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetNextFreeHandle(
return *unconnected_device;
}
}
+ if (type == Joycon::ControllerType::Pro) {
+ const auto unconnected_device = std::ranges::find_if(
+ pro_controller, [](auto& device) { return !device->IsConnected(); });
+
+ if (unconnected_device != pro_controller.end()) {
+ return *unconnected_device;
+ }
+ }
return nullptr;
}
@@ -409,6 +444,15 @@ std::shared_ptr<Joycon::JoyconDriver> Joycons::GetHandle(PadIdentifier identifie
}
}
+ if (type == Joycon::ControllerType::Pro) {
+ const auto matching_device = std::ranges::find_if(
+ pro_controller, [is_handle_active](auto& device) { return is_handle_active(device); });
+
+ if (matching_device != pro_controller.end()) {
+ return *matching_device;
+ }
+ }
+
return nullptr;
}
@@ -455,6 +499,9 @@ std::vector<Common::ParamPackage> Joycons::GetInputDevices() const {
for (const auto& controller : right_joycons) {
add_entry(controller);
}
+ for (const auto& controller : pro_controller) {
+ add_entry(controller);
+ }
// List dual joycon pairs
for (std::size_t i = 0; i < MaxSupportedControllers; i++) {
diff --git a/src/input_common/drivers/joycon.h b/src/input_common/drivers/joycon.h
index 2149ab7fd..473ba1b9e 100644
--- a/src/input_common/drivers/joycon.h
+++ b/src/input_common/drivers/joycon.h
@@ -106,6 +106,7 @@ private:
// Joycon types are split by type to ease supporting dualjoycon configurations
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> left_joycons{};
std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> right_joycons{};
+ std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> pro_controller{};
};
} // namespace InputCommon
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp
index d975eb815..88cacd615 100644
--- a/src/input_common/drivers/sdl_driver.cpp
+++ b/src/input_common/drivers/sdl_driver.cpp
@@ -343,6 +343,14 @@ void SDLDriver::InitJoystick(int joystick_index) {
}
}
+ if (Settings::values.enable_procon_driver) {
+ if (guid.uuid[5] == 0x05 && guid.uuid[4] == 0x7e && guid.uuid[8] == 0x09) {
+ LOG_WARNING(Input, "Preferring joycon driver for device index {}", joystick_index);
+ SDL_JoystickClose(sdl_joystick);
+ return;
+ }
+ }
+
std::scoped_lock lock{joystick_map_mutex};
if (joystick_map.find(guid) == joystick_map.end()) {
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller);
@@ -465,13 +473,19 @@ SDLDriver::SDLDriver(std::string input_engine_) : InputEngine(std::move(input_en
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
- // Disable hidapi drivers for switch controllers when the custom joycon driver is enabled
+ // Disable hidapi drivers for joycon controllers when the custom joycon driver is enabled
if (Settings::values.enable_joycon_driver) {
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, "0");
} else {
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, "1");
}
- SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, "1");
+
+ // Disable hidapi drivers for pro controllers when the custom joycon driver is enabled
+ if (Settings::values.enable_procon_driver) {
+ SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, "0");
+ } else {
+ SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_SWITCH, "1");
+ }
// Disable hidapi driver for xbox. Already default on Windows, this causes conflict with native
// driver on Linux.
diff --git a/src/input_common/helpers/joycon_driver.cpp b/src/input_common/helpers/joycon_driver.cpp
index 8f94c9f45..e65b6b845 100644
--- a/src/input_common/helpers/joycon_driver.cpp
+++ b/src/input_common/helpers/joycon_driver.cpp
@@ -543,9 +543,10 @@ void JoyconDriver::SetCallbacks(const JoyconCallbacks& callbacks) {
DriverResult JoyconDriver::GetDeviceType(SDL_hid_device_info* device_info,
ControllerType& controller_type) {
- static constexpr std::array<std::pair<u32, ControllerType>, 2> supported_devices{
+ static constexpr std::array<std::pair<u32, ControllerType>, 6> supported_devices{
std::pair<u32, ControllerType>{0x2006, ControllerType::Left},
{0x2007, ControllerType::Right},
+ {0x2009, ControllerType::Pro},
};
constexpr u16 nintendo_vendor_id = 0x057e;
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp
index 096c23b07..a6be6dac1 100644
--- a/src/input_common/helpers/stick_from_buttons.cpp
+++ b/src/input_common/helpers/stick_from_buttons.cpp
@@ -15,6 +15,9 @@ public:
// do not play nicely with the theoretical maximum range.
// Using a value one lower from the maximum emulates real stick behavior.
static constexpr float MAX_RANGE = 32766.0f / 32767.0f;
+ static constexpr float TAU = Common::PI * 2.0f;
+ // Use wider angle to ease the transition.
+ static constexpr float APERTURE = TAU * 0.15f;
using Button = std::unique_ptr<Common::Input::InputDevice>;
@@ -61,30 +64,23 @@ public:
}
bool IsAngleGreater(float old_angle, float new_angle) const {
- constexpr float TAU = Common::PI * 2.0f;
- // Use wider angle to ease the transition.
- constexpr float aperture = TAU * 0.15f;
- const float top_limit = new_angle + aperture;
+ const float top_limit = new_angle + APERTURE;
return (old_angle > new_angle && old_angle <= top_limit) ||
(old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
}
bool IsAngleSmaller(float old_angle, float new_angle) const {
- constexpr float TAU = Common::PI * 2.0f;
- // Use wider angle to ease the transition.
- constexpr float aperture = TAU * 0.15f;
- const float bottom_limit = new_angle - aperture;
+ const float bottom_limit = new_angle - APERTURE;
return (old_angle >= bottom_limit && old_angle < new_angle) ||
(old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
}
float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
- constexpr float TAU = Common::PI * 2.0f;
float new_angle = angle;
auto time_difference = static_cast<float>(
- std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
- time_difference /= 1000.0f * 1000.0f;
+ std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
+ time_difference /= 1000.0f;
if (time_difference > 0.5f) {
time_difference = 0.5f;
}
@@ -201,8 +197,6 @@ public:
}
void UpdateStatus() {
- const float coef = modifier_status.value ? modifier_scale : MAX_RANGE;
-
bool r = right_status;
bool l = left_status;
bool u = up_status;
@@ -220,7 +214,7 @@ public:
// Move if a key is pressed
if (r || l || u || d) {
- amplitude = coef;
+ amplitude = modifier_status.value ? modifier_scale : MAX_RANGE;
} else {
amplitude = 0;
}
@@ -274,30 +268,17 @@ public:
Common::Input::StickStatus status{};
status.x.properties = properties;
status.y.properties = properties;
+
if (Settings::values.emulate_analog_keyboard) {
const auto now = std::chrono::steady_clock::now();
- float angle_ = GetAngle(now);
+ const float angle_ = GetAngle(now);
status.x.raw_value = std::cos(angle_) * amplitude;
status.y.raw_value = std::sin(angle_) * amplitude;
return status;
}
- constexpr float SQRT_HALF = 0.707106781f;
- int x = 0, y = 0;
- if (right_status) {
- ++x;
- }
- if (left_status) {
- --x;
- }
- if (up_status) {
- ++y;
- }
- if (down_status) {
- --y;
- }
- const float coef = modifier_status.value ? modifier_scale : MAX_RANGE;
- status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF);
- status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF);
+
+ status.x.raw_value = std::cos(goal_angle) * amplitude;
+ status.y.raw_value = std::sin(goal_angle) * amplitude;
return status;
}
diff --git a/src/input_common/input_poller.cpp b/src/input_common/input_poller.cpp
index 15cbf7e5f..8c6a6521a 100644
--- a/src/input_common/input_poller.cpp
+++ b/src/input_common/input_poller.cpp
@@ -16,10 +16,10 @@ public:
class InputFromButton final : public Common::Input::InputDevice {
public:
- explicit InputFromButton(PadIdentifier identifier_, int button_, bool toggle_, bool inverted_,
- InputEngine* input_engine_)
- : identifier(identifier_), button(button_), toggle(toggle_), inverted(inverted_),
- input_engine(input_engine_) {
+ explicit InputFromButton(PadIdentifier identifier_, int button_, bool turbo_, bool toggle_,
+ bool inverted_, InputEngine* input_engine_)
+ : identifier(identifier_), button(button_), turbo(turbo_), toggle(toggle_),
+ inverted(inverted_), input_engine(input_engine_) {
UpdateCallback engine_callback{[this]() { OnChange(); }};
const InputIdentifier input_identifier{
.identifier = identifier,
@@ -40,6 +40,7 @@ public:
.value = input_engine->GetButton(identifier, button),
.inverted = inverted,
.toggle = toggle,
+ .turbo = turbo,
};
}
@@ -68,6 +69,7 @@ public:
private:
const PadIdentifier identifier;
const int button;
+ const bool turbo;
const bool toggle;
const bool inverted;
int callback_key;
@@ -77,10 +79,10 @@ private:
class InputFromHatButton final : public Common::Input::InputDevice {
public:
- explicit InputFromHatButton(PadIdentifier identifier_, int button_, u8 direction_, bool toggle_,
- bool inverted_, InputEngine* input_engine_)
- : identifier(identifier_), button(button_), direction(direction_), toggle(toggle_),
- inverted(inverted_), input_engine(input_engine_) {
+ explicit InputFromHatButton(PadIdentifier identifier_, int button_, u8 direction_, bool turbo_,
+ bool toggle_, bool inverted_, InputEngine* input_engine_)
+ : identifier(identifier_), button(button_), direction(direction_), turbo(turbo_),
+ toggle(toggle_), inverted(inverted_), input_engine(input_engine_) {
UpdateCallback engine_callback{[this]() { OnChange(); }};
const InputIdentifier input_identifier{
.identifier = identifier,
@@ -101,6 +103,7 @@ public:
.value = input_engine->GetHatButton(identifier, button, direction),
.inverted = inverted,
.toggle = toggle,
+ .turbo = turbo,
};
}
@@ -130,6 +133,7 @@ private:
const PadIdentifier identifier;
const int button;
const u8 direction;
+ const bool turbo;
const bool toggle;
const bool inverted;
int callback_key;
@@ -853,14 +857,15 @@ std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateButtonDevice(
const auto keyboard_key = params.Get("code", 0);
const auto toggle = params.Get("toggle", false) != 0;
const auto inverted = params.Get("inverted", false) != 0;
+ const auto turbo = params.Get("turbo", false) != 0;
input_engine->PreSetController(identifier);
input_engine->PreSetButton(identifier, button_id);
input_engine->PreSetButton(identifier, keyboard_key);
if (keyboard_key != 0) {
- return std::make_unique<InputFromButton>(identifier, keyboard_key, toggle, inverted,
+ return std::make_unique<InputFromButton>(identifier, keyboard_key, turbo, toggle, inverted,
input_engine.get());
}
- return std::make_unique<InputFromButton>(identifier, button_id, toggle, inverted,
+ return std::make_unique<InputFromButton>(identifier, button_id, turbo, toggle, inverted,
input_engine.get());
}
@@ -876,11 +881,12 @@ std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateHatButtonDevice(
const auto direction = input_engine->GetHatButtonId(params.Get("direction", ""));
const auto toggle = params.Get("toggle", false) != 0;
const auto inverted = params.Get("inverted", false) != 0;
+ const auto turbo = params.Get("turbo", false) != 0;
input_engine->PreSetController(identifier);
input_engine->PreSetHatButton(identifier, button_id);
- return std::make_unique<InputFromHatButton>(identifier, button_id, direction, toggle, inverted,
- input_engine.get());
+ return std::make_unique<InputFromHatButton>(identifier, button_id, direction, turbo, toggle,
+ inverted, input_engine.get());
}
std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateStickDevice(