Qt/Input: Introduce profiles

This commit is contained in:
Unknown 2017-12-23 22:25:51 +01:00 committed by Ivan
parent 4231ea2eb6
commit 51a2b43d81
24 changed files with 1194 additions and 893 deletions

View file

@ -17,8 +17,10 @@
#include <cstdio>
#include <cmath>
evdev_joystick_handler::evdev_joystick_handler()
evdev_joystick_handler::evdev_joystick_handler() : PadHandlerBase(pad_handler::evdev)
{
init_configs();
// Define border values
thumb_min = 0;
thumb_max = 255;
@ -27,47 +29,6 @@ evdev_joystick_handler::evdev_joystick_handler()
vibration_min = 0;
vibration_max = 65535;
// Set this handler's type and save location
m_pad_config.cfg_type = "evdev";
m_pad_config.cfg_name = fs::get_config_dir() + "/config_evdev.yml";
// Set default button mapping
m_pad_config.ls_left.def = rev_axis_list.at(ABS_X);
m_pad_config.ls_down.def = axis_list.at(ABS_Y);
m_pad_config.ls_right.def = axis_list.at(ABS_X);
m_pad_config.ls_up.def = rev_axis_list.at(ABS_Y);
m_pad_config.rs_left.def = rev_axis_list.at(ABS_RX);
m_pad_config.rs_down.def = axis_list.at(ABS_RY);
m_pad_config.rs_right.def = axis_list.at(ABS_RX);
m_pad_config.rs_up.def = rev_axis_list.at(ABS_RY);
m_pad_config.start.def = button_list.at(BTN_START);
m_pad_config.select.def = button_list.at(BTN_SELECT);
m_pad_config.ps.def = button_list.at(BTN_MODE);
m_pad_config.square.def = button_list.at(BTN_X);
m_pad_config.cross.def = button_list.at(BTN_A);
m_pad_config.circle.def = button_list.at(BTN_B);
m_pad_config.triangle.def = button_list.at(BTN_Y);
m_pad_config.left.def = rev_axis_list.at(ABS_HAT0X);
m_pad_config.down.def = axis_list.at(ABS_HAT0Y);
m_pad_config.right.def = axis_list.at(ABS_HAT0X);
m_pad_config.up.def = rev_axis_list.at(ABS_HAT0Y);
m_pad_config.r1.def = button_list.at(BTN_TR);
m_pad_config.r2.def = axis_list.at(ABS_RZ);
m_pad_config.r3.def = button_list.at(BTN_THUMBR);
m_pad_config.l1.def = button_list.at(BTN_TL);
m_pad_config.l2.def = axis_list.at(ABS_Z);
m_pad_config.l3.def = button_list.at(BTN_THUMBL);
// Set default misc variables
m_pad_config.lstickdeadzone.def = 30; // between 0 and 255
m_pad_config.rstickdeadzone.def = 30; // between 0 and 255
m_pad_config.ltriggerthreshold.def = 0; // between 0 and 255
m_pad_config.rtriggerthreshold.def = 0; // between 0 and 255
m_pad_config.padsquircling.def = 5000;
// apply defaults
m_pad_config.from_default();
// set capabilities
b_has_config = true;
b_has_rumble = true;
@ -82,12 +43,54 @@ evdev_joystick_handler::~evdev_joystick_handler()
Close();
}
void evdev_joystick_handler::init_config(pad_config* cfg, const std::string& name)
{
// Set this profile's save location
cfg->cfg_name = name;
// Set default button mapping
cfg->ls_left.def = rev_axis_list.at(ABS_X);
cfg->ls_down.def = axis_list.at(ABS_Y);
cfg->ls_right.def = axis_list.at(ABS_X);
cfg->ls_up.def = rev_axis_list.at(ABS_Y);
cfg->rs_left.def = rev_axis_list.at(ABS_RX);
cfg->rs_down.def = axis_list.at(ABS_RY);
cfg->rs_right.def = axis_list.at(ABS_RX);
cfg->rs_up.def = rev_axis_list.at(ABS_RY);
cfg->start.def = button_list.at(BTN_START);
cfg->select.def = button_list.at(BTN_SELECT);
cfg->ps.def = button_list.at(BTN_MODE);
cfg->square.def = button_list.at(BTN_X);
cfg->cross.def = button_list.at(BTN_A);
cfg->circle.def = button_list.at(BTN_B);
cfg->triangle.def = button_list.at(BTN_Y);
cfg->left.def = rev_axis_list.at(ABS_HAT0X);
cfg->down.def = axis_list.at(ABS_HAT0Y);
cfg->right.def = axis_list.at(ABS_HAT0X);
cfg->up.def = rev_axis_list.at(ABS_HAT0Y);
cfg->r1.def = button_list.at(BTN_TR);
cfg->r2.def = axis_list.at(ABS_RZ);
cfg->r3.def = button_list.at(BTN_THUMBR);
cfg->l1.def = button_list.at(BTN_TL);
cfg->l2.def = axis_list.at(ABS_Z);
cfg->l3.def = button_list.at(BTN_THUMBL);
// Set default misc variables
cfg->lstickdeadzone.def = 30; // between 0 and 255
cfg->rstickdeadzone.def = 30; // between 0 and 255
cfg->ltriggerthreshold.def = 0; // between 0 and 255
cfg->rtriggerthreshold.def = 0; // between 0 and 255
cfg->padsquircling.def = 5000;
// apply defaults
cfg->from_default();
}
bool evdev_joystick_handler::Init()
{
if (m_is_init)
return true;
m_pad_config.load();
m_pos_axis_config.load();
if (!m_pos_axis_config.exist())
@ -498,6 +501,7 @@ void evdev_joystick_handler::TranslateButtonPress(u64 keyCode, bool& pressed, u1
{
// Update the pad button values based on their type and thresholds.
// With this you can use axis or triggers as buttons or vice versa
auto profile = m_dev.config;
u32 code = static_cast<u32>(keyCode);
auto checkButton = [&](const EvdevButton& b)
{
@ -510,23 +514,23 @@ void evdev_joystick_handler::TranslateButtonPress(u64 keyCode, bool& pressed, u1
if (checkButton(m_dev.trigger_left))
{
pressed = value > m_pad_config.ltriggerthreshold;
value = pressed ? NormalizeTriggerInput(value, m_pad_config.ltriggerthreshold) : 0;
pressed = value > profile->ltriggerthreshold;
value = pressed ? NormalizeTriggerInput(value, profile->ltriggerthreshold) : 0;
}
else if (checkButton(m_dev.trigger_right))
{
pressed = value > m_pad_config.rtriggerthreshold;
value = pressed ? NormalizeTriggerInput(value, m_pad_config.rtriggerthreshold) : 0;
pressed = value > profile->rtriggerthreshold;
value = pressed ? NormalizeTriggerInput(value, profile->rtriggerthreshold) : 0;
}
else if (checkButtons(m_dev.axis_left))
{
pressed = value > (ignore_threshold ? 0 : m_pad_config.lstickdeadzone);
value = pressed ? NormalizeStickInput(value, m_pad_config.lstickdeadzone, ignore_threshold) : 0;
pressed = value > (ignore_threshold ? 0 : profile->lstickdeadzone);
value = pressed ? NormalizeStickInput(value, profile->lstickdeadzone, ignore_threshold) : 0;
}
else if (checkButtons(m_dev.axis_right))
{
pressed = value > (ignore_threshold ? 0 : m_pad_config.rstickdeadzone);
value = pressed ? NormalizeStickInput(value, m_pad_config.rstickdeadzone, ignore_threshold) : 0;
pressed = value > (ignore_threshold ? 0 : profile->rstickdeadzone);
value = pressed ? NormalizeStickInput(value, profile->rstickdeadzone, ignore_threshold) : 0;
}
else // normal button (should in theory also support sensitive buttons)
{
@ -608,7 +612,7 @@ std::vector<std::string> evdev_joystick_handler::ListDevices()
libevdev_has_event_code(dev, EV_ABS, ABS_Y))
{
// It's a joystick.
evdev_joystick_list.push_back(libevdev_get_name(dev));
evdev_joystick_list.push_back(et.name + ": " + libevdev_get_name(dev));
}
libevdev_free(dev);
close(fd);
@ -643,7 +647,7 @@ int evdev_joystick_handler::add_device(const std::string& device, bool in_settin
close(fd);
continue;
}
const std::string name = libevdev_get_name(dev);
const std::string name = et.name + ": " + libevdev_get_name(dev);
if (libevdev_has_event_type(dev, EV_KEY) &&
libevdev_has_event_code(dev, EV_ABS, ABS_X) &&
libevdev_has_event_code(dev, EV_ABS, ABS_Y) &&
@ -680,6 +684,7 @@ void evdev_joystick_handler::ThreadProc()
for (auto& device : devices)
{
m_dev = device;
auto profile = device.config;
auto pad = device.pad;
auto axis_orientations = device.axis_orientations;
auto& dev = device.device;
@ -704,10 +709,10 @@ void evdev_joystick_handler::ThreadProc()
padnum++;
// Handle vibration
int idx_l = m_pad_config.switch_vibration_motors ? 1 : 0;
int idx_s = m_pad_config.switch_vibration_motors ? 0 : 1;
u16 force_large = m_pad_config.enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value * 257 : vibration_min;
u16 force_small = m_pad_config.enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value * 257 : vibration_min;
int idx_l = profile->switch_vibration_motors ? 1 : 0;
int idx_s = profile->switch_vibration_motors ? 0 : 1;
u16 force_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value * 257 : vibration_min;
u16 force_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value * 257 : vibration_min;
SetRumble(&device, force_large, force_small);
// Try to query the latest event from the joystick.
@ -832,13 +837,13 @@ void evdev_joystick_handler::ThreadProc()
u16 lx, ly, rx, ry;
// Normalize our two stick's axis based on the thresholds
std::tie(lx, ly) = NormalizeStickDeadzone(device.stick_val[0], device.stick_val[1], m_pad_config.lstickdeadzone);
std::tie(rx, ry) = NormalizeStickDeadzone(device.stick_val[2], device.stick_val[3], m_pad_config.rstickdeadzone);
std::tie(lx, ly) = NormalizeStickDeadzone(device.stick_val[0], device.stick_val[1], profile->lstickdeadzone);
std::tie(rx, ry) = NormalizeStickDeadzone(device.stick_val[2], device.stick_val[3], profile->rstickdeadzone);
if (m_pad_config.padsquircling != 0)
if (profile->padsquircling != 0)
{
std::tie(lx, ly) = ConvertToSquirclePoint(lx, ly, m_pad_config.padsquircling);
std::tie(rx, ry) = ConvertToSquirclePoint(rx, ry, m_pad_config.padsquircling);
std::tie(lx, ly) = ConvertToSquirclePoint(lx, ly, profile->padsquircling);
std::tie(rx, ry) = ConvertToSquirclePoint(rx, ry, profile->padsquircling);
}
pad->m_sticks[0].m_value = lx;
@ -866,6 +871,13 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
int i = 0; // increment to know the axis location (17-24). Be careful if you ever add more find_key() calls in here (BUTTON_COUNT = 17)
int last_type = EV_ABS;
int index = static_cast<int>(devices.size());
m_pad_configs[index].load();
m_dev.config = &m_pad_configs[index];
pad_config* p_profile = m_dev.config;
if (p_profile == nullptr)
return false;
auto find_key = [&](const cfg::string& name)
{
int type = EV_ABS;
@ -909,38 +921,38 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
CELL_PAD_DEV_TYPE_STANDARD
);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.triangle), CELL_PAD_CTRL_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.circle), CELL_PAD_CTRL_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.cross), CELL_PAD_CTRL_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.square), CELL_PAD_CTRL_SQUARE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->triangle), CELL_PAD_CTRL_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->circle), CELL_PAD_CTRL_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->cross), CELL_PAD_CTRL_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->square), CELL_PAD_CTRL_SQUARE);
m_dev.trigger_left = evdevbutton(m_pad_config.l2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, m_dev.trigger_left.code, CELL_PAD_CTRL_L2);
m_dev.trigger_left = evdevbutton(p_profile->l2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, m_dev.trigger_left.code, CELL_PAD_CTRL_L2);
m_dev.trigger_right = evdevbutton(m_pad_config.r2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, m_dev.trigger_right.code, CELL_PAD_CTRL_R2);
m_dev.trigger_right = evdevbutton(p_profile->r2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, m_dev.trigger_right.code, CELL_PAD_CTRL_R2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.l1), CELL_PAD_CTRL_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.r1), CELL_PAD_CTRL_R1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.start), CELL_PAD_CTRL_START);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.select), CELL_PAD_CTRL_SELECT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.l3), CELL_PAD_CTRL_L3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.r3), CELL_PAD_CTRL_R3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(m_pad_config.ps), 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.up), CELL_PAD_CTRL_UP);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.down), CELL_PAD_CTRL_DOWN);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.left), CELL_PAD_CTRL_LEFT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(m_pad_config.right), CELL_PAD_CTRL_RIGHT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->l1), CELL_PAD_CTRL_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->r1), CELL_PAD_CTRL_R1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->start), CELL_PAD_CTRL_START);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->select), CELL_PAD_CTRL_SELECT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->l3), CELL_PAD_CTRL_L3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->r3), CELL_PAD_CTRL_R3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_key(p_profile->ps), 0x100/*CELL_PAD_CTRL_PS*/);// TODO: PS button support
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->up), CELL_PAD_CTRL_UP);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->down), CELL_PAD_CTRL_DOWN);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->left), CELL_PAD_CTRL_LEFT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_key(p_profile->right), CELL_PAD_CTRL_RIGHT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, 0, 0x0); // Reserved
m_dev.axis_left[0] = evdevbutton(m_pad_config.ls_right);
m_dev.axis_left[1] = evdevbutton(m_pad_config.ls_left);
m_dev.axis_left[2] = evdevbutton(m_pad_config.ls_up);
m_dev.axis_left[3] = evdevbutton(m_pad_config.ls_down);
m_dev.axis_right[0] = evdevbutton(m_pad_config.rs_right);
m_dev.axis_right[1] = evdevbutton(m_pad_config.rs_left);
m_dev.axis_right[2] = evdevbutton(m_pad_config.rs_up);
m_dev.axis_right[3] = evdevbutton(m_pad_config.rs_down);
m_dev.axis_left[0] = evdevbutton(p_profile->ls_right);
m_dev.axis_left[1] = evdevbutton(p_profile->ls_left);
m_dev.axis_left[2] = evdevbutton(p_profile->ls_up);
m_dev.axis_left[3] = evdevbutton(p_profile->ls_down);
m_dev.axis_right[0] = evdevbutton(p_profile->rs_right);
m_dev.axis_right[1] = evdevbutton(p_profile->rs_left);
m_dev.axis_right[2] = evdevbutton(p_profile->rs_up);
m_dev.axis_right[3] = evdevbutton(p_profile->rs_down);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, m_dev.axis_left[1].code, m_dev.axis_left[0].code);
pad->m_sticks.emplace_back(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y, m_dev.axis_left[3].code, m_dev.axis_left[2].code);