2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-20 17:41:15 -04:00
|
|
|
|
2021-11-26 16:45:37 -05:00
|
|
|
#include "common/settings.h"
|
2021-09-20 17:41:15 -04:00
|
|
|
#include "input_common/input_engine.h"
|
|
|
|
#include "input_common/input_mapping.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
2022-01-24 11:37:48 -05:00
|
|
|
MappingFactory::MappingFactory() = default;
|
2021-09-20 17:41:15 -04:00
|
|
|
|
|
|
|
void MappingFactory::BeginMapping(Polling::InputType type) {
|
|
|
|
is_enabled = true;
|
|
|
|
input_type = type;
|
|
|
|
input_queue.Clear();
|
|
|
|
first_axis = -1;
|
|
|
|
second_axis = -1;
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:39:17 -05:00
|
|
|
Common::ParamPackage MappingFactory::GetNextInput() {
|
2021-09-20 17:41:15 -04:00
|
|
|
Common::ParamPackage input;
|
|
|
|
input_queue.Pop(input);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingFactory::RegisterInput(const MappingData& data) {
|
|
|
|
if (!is_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-14 11:45:07 -05:00
|
|
|
if (!IsDriverValid(data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:41:15 -04:00
|
|
|
switch (input_type) {
|
|
|
|
case Polling::InputType::Button:
|
|
|
|
RegisterButton(data);
|
|
|
|
return;
|
|
|
|
case Polling::InputType::Stick:
|
|
|
|
RegisterStick(data);
|
|
|
|
return;
|
|
|
|
case Polling::InputType::Motion:
|
|
|
|
RegisterMotion(data);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingFactory::StopMapping() {
|
|
|
|
is_enabled = false;
|
|
|
|
input_type = Polling::InputType::None;
|
|
|
|
input_queue.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingFactory::RegisterButton(const MappingData& data) {
|
|
|
|
Common::ParamPackage new_input;
|
|
|
|
new_input.Set("engine", data.engine);
|
2022-01-24 11:48:59 -05:00
|
|
|
if (data.pad.guid.IsValid()) {
|
2022-02-05 00:40:28 -05:00
|
|
|
new_input.Set("guid", data.pad.guid.RawString());
|
2021-09-20 17:41:15 -04:00
|
|
|
}
|
|
|
|
new_input.Set("port", static_cast<int>(data.pad.port));
|
|
|
|
new_input.Set("pad", static_cast<int>(data.pad.pad));
|
2021-11-21 15:12:01 -05:00
|
|
|
|
2021-09-20 17:41:15 -04:00
|
|
|
switch (data.type) {
|
|
|
|
case EngineInputType::Button:
|
|
|
|
// Workaround for old compatibility
|
|
|
|
if (data.engine == "keyboard") {
|
|
|
|
new_input.Set("code", data.index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_input.Set("button", data.index);
|
|
|
|
break;
|
|
|
|
case EngineInputType::HatButton:
|
|
|
|
new_input.Set("hat", data.index);
|
|
|
|
new_input.Set("direction", data.hat_name);
|
|
|
|
break;
|
|
|
|
case EngineInputType::Analog:
|
2021-11-21 15:12:01 -05:00
|
|
|
// Ignore mouse axis when mapping buttons
|
2023-01-10 19:18:30 -05:00
|
|
|
if (data.engine == "mouse" && data.index != 4) {
|
2021-11-21 15:12:01 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-09-20 17:41:15 -04:00
|
|
|
new_input.Set("axis", data.index);
|
|
|
|
new_input.Set("threshold", 0.5f);
|
|
|
|
break;
|
2023-05-06 00:48:52 -04:00
|
|
|
case EngineInputType::Motion:
|
|
|
|
new_input.Set("motion", data.index);
|
|
|
|
break;
|
2021-09-20 17:41:15 -04:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input_queue.Push(new_input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingFactory::RegisterStick(const MappingData& data) {
|
|
|
|
Common::ParamPackage new_input;
|
|
|
|
new_input.Set("engine", data.engine);
|
2022-01-24 11:48:59 -05:00
|
|
|
if (data.pad.guid.IsValid()) {
|
2022-02-05 00:40:28 -05:00
|
|
|
new_input.Set("guid", data.pad.guid.RawString());
|
2021-09-20 17:41:15 -04:00
|
|
|
}
|
|
|
|
new_input.Set("port", static_cast<int>(data.pad.port));
|
|
|
|
new_input.Set("pad", static_cast<int>(data.pad.pad));
|
|
|
|
|
|
|
|
// If engine is mouse map the mouse position as a joystick
|
|
|
|
if (data.engine == "mouse") {
|
|
|
|
new_input.Set("axis_x", 0);
|
|
|
|
new_input.Set("axis_y", 1);
|
|
|
|
new_input.Set("threshold", 0.5f);
|
|
|
|
new_input.Set("range", 1.0f);
|
|
|
|
new_input.Set("deadzone", 0.0f);
|
|
|
|
input_queue.Push(new_input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (data.type) {
|
|
|
|
case EngineInputType::Button:
|
|
|
|
case EngineInputType::HatButton:
|
|
|
|
RegisterButton(data);
|
|
|
|
return;
|
|
|
|
case EngineInputType::Analog:
|
|
|
|
if (first_axis == data.index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (first_axis == -1) {
|
|
|
|
first_axis = data.index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_input.Set("axis_x", first_axis);
|
|
|
|
new_input.Set("axis_y", data.index);
|
|
|
|
new_input.Set("threshold", 0.5f);
|
|
|
|
new_input.Set("range", 0.95f);
|
|
|
|
new_input.Set("deadzone", 0.15f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input_queue.Push(new_input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingFactory::RegisterMotion(const MappingData& data) {
|
|
|
|
Common::ParamPackage new_input;
|
|
|
|
new_input.Set("engine", data.engine);
|
2022-01-24 11:48:59 -05:00
|
|
|
if (data.pad.guid.IsValid()) {
|
2022-02-05 00:40:28 -05:00
|
|
|
new_input.Set("guid", data.pad.guid.RawString());
|
2021-09-20 17:41:15 -04:00
|
|
|
}
|
|
|
|
new_input.Set("port", static_cast<int>(data.pad.port));
|
|
|
|
new_input.Set("pad", static_cast<int>(data.pad.pad));
|
2022-01-16 19:03:11 -05:00
|
|
|
|
2023-02-21 22:46:42 -05:00
|
|
|
// If engine is mouse map it automatically to mouse motion
|
2022-01-16 19:03:11 -05:00
|
|
|
if (data.engine == "mouse") {
|
2023-02-21 22:46:42 -05:00
|
|
|
new_input.Set("motion", 0);
|
|
|
|
new_input.Set("pad", 1);
|
2023-03-05 12:00:00 -05:00
|
|
|
new_input.Set("threshold", 0.001f);
|
2022-01-16 19:03:11 -05:00
|
|
|
input_queue.Push(new_input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:41:15 -04:00
|
|
|
switch (data.type) {
|
|
|
|
case EngineInputType::Button:
|
|
|
|
case EngineInputType::HatButton:
|
|
|
|
RegisterButton(data);
|
|
|
|
return;
|
|
|
|
case EngineInputType::Analog:
|
|
|
|
if (first_axis == data.index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (second_axis == data.index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (first_axis == -1) {
|
|
|
|
first_axis = data.index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (second_axis == -1) {
|
|
|
|
second_axis = data.index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_input.Set("axis_x", first_axis);
|
|
|
|
new_input.Set("axis_y", second_axis);
|
|
|
|
new_input.Set("axis_z", data.index);
|
|
|
|
new_input.Set("range", 1.0f);
|
|
|
|
new_input.Set("deadzone", 0.20f);
|
|
|
|
break;
|
|
|
|
case EngineInputType::Motion:
|
|
|
|
new_input.Set("motion", data.index);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input_queue.Push(new_input);
|
|
|
|
}
|
|
|
|
|
2021-11-14 11:45:07 -05:00
|
|
|
bool MappingFactory::IsDriverValid(const MappingData& data) const {
|
|
|
|
// Only port 0 can be mapped on the keyboard
|
|
|
|
if (data.engine == "keyboard" && data.pad.port != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-16 14:38:50 -05:00
|
|
|
// Only port 0 can be mapped on the mouse
|
|
|
|
if (data.engine == "mouse" && data.pad.port != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-26 16:45:37 -05:00
|
|
|
// To prevent mapping with two devices we disable any UDP except motion
|
|
|
|
if (!Settings::values.enable_udp_controller && data.engine == "cemuhookudp" &&
|
|
|
|
data.type != EngineInputType::Motion) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-14 11:45:07 -05:00
|
|
|
// The following drivers don't need to be mapped
|
|
|
|
if (data.engine == "touch_from_button") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (data.engine == "analog_from_button") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:41:15 -04:00
|
|
|
} // namespace InputCommon
|