2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

settings: Fix mouse and keyboard mappings

This commit is contained in:
german77 2021-10-24 11:22:20 -05:00 committed by Narr the Reg
parent cc651c7c99
commit 464c4d26ac
10 changed files with 100 additions and 103 deletions

View file

@ -246,7 +246,7 @@ std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
devices.begin(), devices.end(), [param](const Common::ParamPackage param_) {
return param.Get("engine", "") == param_.Get("engine", "") &&
param.Get("guid", "") == param_.Get("guid", "") &&
param.Get("port", "") == param_.Get("port", "");
param.Get("port", 0) == param_.Get("port", 0);
});
if (devices_it != devices.end()) {
continue;
@ -254,7 +254,7 @@ std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
Common::ParamPackage device{};
device.Set("engine", param.Get("engine", ""));
device.Set("guid", param.Get("guid", ""));
device.Set("port", param.Get("port", ""));
device.Set("port", param.Get("port", 0));
devices.push_back(device);
}
@ -269,7 +269,7 @@ std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
devices.begin(), devices.end(), [param](const Common::ParamPackage param_) {
return param.Get("engine", "") == param_.Get("engine", "") &&
param.Get("guid", "") == param_.Get("guid", "") &&
param.Get("port", "") == param_.Get("port", "");
param.Get("port", 0) == param_.Get("port", 0);
});
if (devices_it != devices.end()) {
continue;
@ -277,7 +277,7 @@ std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
Common::ParamPackage device{};
device.Set("engine", param.Get("engine", ""));
device.Set("guid", param.Get("guid", ""));
device.Set("port", param.Get("port", ""));
device.Set("port", param.Get("port", 0));
devices.push_back(device);
}
return devices;

View file

@ -162,17 +162,22 @@ void EmulatedDevices::SetKeyboardButton(Input::CallbackStatus callback, std::siz
return;
}
// TODO(german77): Do this properly
// switch (index) {
// case Settings::NativeKeyboard::A:
// interface_status.keyboard_state.a.Assign(current_status.value);
// break;
// ....
// }
UpdateKey(index, current_status.value);
TriggerOnChange(DeviceTriggerType::Keyboard);
}
void EmulatedDevices::UpdateKey(std::size_t key_index, bool status) {
constexpr u8 KEYS_PER_BYTE = 8;
auto& entry = device_status.keyboard_state.key[key_index / KEYS_PER_BYTE];
const u8 mask = 1 << (key_index % KEYS_PER_BYTE);
if (status) {
entry = entry | mask;
} else {
entry = entry & ~mask;
}
}
void EmulatedDevices::SetKeyboardModifier(Input::CallbackStatus callback, std::size_t index) {
if (index >= device_status.keyboard_moddifier_values.size()) {
return;

View file

@ -143,6 +143,9 @@ public:
void DeleteCallback(int key);
private:
/// Helps assigning a value to keyboard_state
void UpdateKey(std::size_t key_index, bool status);
/**
* Updates the touch status of the console
* @param callback: A CallbackStatus containing the key status

View file

@ -121,12 +121,27 @@ void Mouse::StopPanning() {
std::vector<Common::ParamPackage> Mouse::GetInputDevices() const {
std::vector<Common::ParamPackage> devices;
devices.emplace_back(Common::ParamPackage{
{"engine", "keyboard"},
{"engine", GetEngineName()},
{"display", "Keyboard/Mouse"},
});
return devices;
}
AnalogMapping Mouse::GetAnalogMappingForDevice(
[[maybe_unused]] const Common::ParamPackage& params) {
// Only overwrite different buttons from default
AnalogMapping mapping = {};
Common::ParamPackage right_analog_params;
right_analog_params.Set("engine", GetEngineName());
right_analog_params.Set("axis_x", 0);
right_analog_params.Set("axis_y", 1);
right_analog_params.Set("threshold", 0.5f);
right_analog_params.Set("range", 1.0f);
right_analog_params.Set("deadzone", 0.0f);
mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
return mapping;
}
std::string Mouse::GetUIName(const Common::ParamPackage& params) const {
if (params.Has("button")) {
return fmt::format("Mouse {}", params.Get("button", 0));

View file

@ -55,6 +55,7 @@ public:
void ReleaseAllButtons();
std::vector<Common::ParamPackage> GetInputDevices() const override;
AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
std::string GetUIName(const Common::ParamPackage& params) const override;
private:

View file

@ -202,6 +202,8 @@ void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int but
if (!configuring || !mapping_callback.on_data) {
return;
}
PreSetButton(identifier, button);
if (value == GetButton(identifier, button)) {
return;
}

View file

@ -143,6 +143,9 @@ struct InputSubsystem::Impl {
return {};
}
const std::string engine = params.Get("engine", "");
if (engine == mouse->GetEngineName()) {
return mouse->GetAnalogMappingForDevice(params);
}
if (engine == gcadapter->GetEngineName()) {
return gcadapter->GetAnalogMappingForDevice(params);
}

View file

@ -478,12 +478,14 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
UpdateControllerEnabledButtons();
UpdateControllerButtonNames();
UpdateMotionButtons();
connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged), [this, player_index](int) {
connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged),
[this, player_index](int) {
UpdateControllerAvailableButtons();
UpdateControllerEnabledButtons();
UpdateControllerButtonNames();
UpdateMotionButtons();
const Core::HID::NpadType type = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
const Core::HID::NpadType type =
GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
if (player_index == 0) {
auto* emulated_controller_p1 =
@ -555,7 +557,7 @@ ConfigureInputPlayer::~ConfigureInputPlayer() {
} else {
emulated_controller->DisableConfiguration();
}
};
}
void ConfigureInputPlayer::ApplyConfiguration() {
if (player_index == 0) {
@ -642,7 +644,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
const auto first_engine = devices[0].Get("engine", "");
const auto first_guid = devices[0].Get("guid", "");
const auto first_port = devices[0].Get("port", "");
const auto first_port = devices[0].Get("port", 0);
if (devices.size() == 1) {
const auto devices_it =
@ -650,7 +652,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
[first_engine, first_guid, first_port](const Common::ParamPackage param) {
return param.Get("engine", "") == first_engine &&
param.Get("guid", "") == first_guid &&
param.Get("port", "") == first_port;
param.Get("port", 0) == first_port;
});
const int device_index =
devices_it != input_devices.end()
@ -662,7 +664,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
const auto second_engine = devices[1].Get("engine", "");
const auto second_guid = devices[1].Get("guid", "");
const auto second_port = devices[1].Get("port", "");
const auto second_port = devices[1].Get("port", 0);
const bool is_keyboard_mouse = (first_engine == "keyboard" || first_engine == "mouse") &&
(second_engine == "keyboard" || second_engine == "mouse");
@ -684,7 +686,7 @@ void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
param.Get("guid2", "") == second_guid) ||
(param.Get("guid", "") == second_guid && param.Get("guid2", "") == first_guid);
return param.Get("engine", "") == first_engine && is_guid_valid &&
param.Get("port", "") == first_port;
param.Get("port", 0) == first_port;
});
const int device_index =
devices_it != input_devices.end()
@ -1096,8 +1098,8 @@ void ConfigureInputPlayer::UpdateMappingWithDefaults() {
emulated_controller->SetMotionParam(motion_id, {});
}
// Reset keyboard bindings
if (ui->comboDevices->currentIndex() == 1) {
// Reset keyboard or mouse bindings
if (ui->comboDevices->currentIndex() == 1 || ui->comboDevices->currentIndex() == 2) {
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
emulated_controller->SetButtonParam(
button_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
@ -1122,63 +1124,30 @@ void ConfigureInputPlayer::UpdateMappingWithDefaults() {
Config::default_motions[motion_id])});
}
// If mouse is selected we want to override with mappings from the driver
if (ui->comboDevices->currentIndex() == 1) {
UpdateUI();
return;
}
// Reset keyboard with mouse bindings
if (ui->comboDevices->currentIndex() == 2) {
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
emulated_controller->SetButtonParam(
button_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
Config::default_buttons[button_id])});
}
Common::ParamPackage left_analog_param{};
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
Config::default_analogs[Settings::NativeAnalog::LStick][sub_button_id])};
SetAnalogParam(params, left_analog_param, analog_sub_buttons[sub_button_id]);
}
left_analog_param.Set("modifier",
InputCommon::GenerateKeyboardParam(
Config::default_stick_mod[Settings::NativeAnalog::LStick]));
emulated_controller->SetStickParam(Settings::NativeAnalog::LStick, left_analog_param);
Common::ParamPackage right_analog_param{};
right_analog_param.Set("engine", "mouse");
right_analog_param.Set("port", 0);
right_analog_param.Set("axis_x", 0);
right_analog_param.Set("axis_y", 1);
emulated_controller->SetStickParam(Settings::NativeAnalog::RStick,
std::move(right_analog_param));
for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
emulated_controller->SetMotionParam(
motion_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
Config::default_motions[motion_id])});
}
UpdateUI();
return;
}
// Reset controller bindings
const auto& device = input_devices[ui->comboDevices->currentIndex()];
auto button_mapping = input_subsystem->GetButtonMappingForDevice(device);
auto analog_mapping = input_subsystem->GetAnalogMappingForDevice(device);
auto motion_mapping = input_subsystem->GetMotionMappingForDevice(device);
for (std::size_t i = 0; i < button_mapping.size(); ++i) {
emulated_controller->SetButtonParam(
i, button_mapping[static_cast<Settings::NativeButton::Values>(i)]);
auto button_mappings = input_subsystem->GetButtonMappingForDevice(device);
auto analog_mappings = input_subsystem->GetAnalogMappingForDevice(device);
auto motion_mappings = input_subsystem->GetMotionMappingForDevice(device);
for (const auto& button_mapping : button_mappings) {
const std::size_t index = button_mapping.first;
emulated_controller->SetButtonParam(index, button_mapping.second);
}
for (std::size_t i = 0; i < analog_mapping.size(); ++i) {
emulated_controller->SetStickParam(
i, analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)]);
for (const auto& analog_mapping : analog_mappings) {
const std::size_t index = analog_mapping.first;
emulated_controller->SetStickParam(index, analog_mapping.second);
}
for (std::size_t i = 0; i < motion_mapping.size(); ++i) {
emulated_controller->SetMotionParam(
i, motion_mapping[static_cast<Settings::NativeMotion::Values>(i)]);
for (const auto& motion_mapping : motion_mappings) {
const std::size_t index = motion_mapping.first;
emulated_controller->SetMotionParam(index, motion_mapping.second);
}
UpdateUI();
@ -1237,7 +1206,7 @@ bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params)
}
// Keyboard/Mouse
if (ui->comboDevices->currentIndex() == 2) {
if (ui->comboDevices->currentIndex() == 1 || ui->comboDevices->currentIndex() == 2) {
return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse";
}
@ -1245,7 +1214,7 @@ bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params)
return params.Get("engine", "") == current_input_device.Get("engine", "") &&
(params.Get("guid", "") == current_input_device.Get("guid", "") ||
params.Get("guid", "") == current_input_device.Get("guid2", "")) &&
params.Get("port", "") == current_input_device.Get("port", "");
params.Get("port", 0) == current_input_device.Get("port", 0);
}
void ConfigureInputPlayer::mousePressEvent(QMouseEvent* event) {

View file

@ -97,7 +97,7 @@ void ConfigureVibration::SetVibrationDevices(std::size_t player_index) {
const auto engine = param.Get("engine", "");
const auto guid = param.Get("guid", "");
const auto port = param.Get("port", "");
const auto port = param.Get("port", 0);
if (engine.empty() || engine == "keyboard" || engine == "mouse" || engine == "tas") {
continue;
@ -105,7 +105,7 @@ void ConfigureVibration::SetVibrationDevices(std::size_t player_index) {
vibration_param_str += fmt::format("engine:{}", engine);
if (!port.empty()) {
if (port != 0) {
vibration_param_str += fmt::format(",port:{}", port);
}
if (!guid.empty()) {

View file

@ -21,8 +21,7 @@ ControllerDialog::ControllerDialog(Core::System& system, QWidget* parent)
Qt::WindowMaximizeButtonHint);
widget = new PlayerControlPreview(this);
widget->SetController(system.HIDCore().GetEmulatedController(
Core::HID::NpadIdType::Player1));
widget->SetController(system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Player1));
QLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(widget);