Files

1397 lines
44 KiB
C++
Raw Permalink Normal View History

2020-12-05 13:08:24 +01:00
#include "keyboard_pad_handler.h"
#include "pad_thread.h"
2020-02-22 20:42:49 +01:00
#include "Emu/Io/pad_config.h"
#include "Emu/Io/KeyboardHandler.h"
#include "Emu/Io/interception.h"
2020-04-25 02:12:19 +02:00
#include "Input/product_info.h"
#include "rpcs3qt/gs_frame.h"
2016-02-02 00:51:09 +03:00
2026-03-18 10:18:24 +01:00
#include <algorithm>
#include <QApplication>
2017-08-15 14:03:07 +02:00
bool keyboard_pad_handler::Init()
2016-02-02 00:51:09 +03:00
{
const steady_clock::time_point now = steady_clock::now();
m_last_mouse_move_left = now;
m_last_mouse_move_right = now;
m_last_mouse_move_up = now;
m_last_mouse_move_down = now;
2017-08-15 14:03:07 +02:00
return true;
2016-02-02 00:51:09 +03:00
}
keyboard_pad_handler::keyboard_pad_handler()
2020-02-21 15:20:10 +03:00
: QObject()
, PadHandlerBase(pad_handler::keyboard)
2016-02-02 00:51:09 +03:00
{
2017-12-23 22:25:51 +01:00
init_configs();
// set capabilities
b_has_config = true;
2017-08-15 14:03:07 +02:00
}
2021-08-10 21:45:26 +02:00
void keyboard_pad_handler::init_config(cfg_pad* cfg)
2017-12-23 22:25:51 +01:00
{
if (!cfg) return;
2017-12-23 22:25:51 +01:00
// Set default button mapping
cfg->ls_left.def = GetKeyName(Qt::Key_A);
cfg->ls_down.def = GetKeyName(Qt::Key_S);
cfg->ls_right.def = GetKeyName(Qt::Key_D);
cfg->ls_up.def = GetKeyName(Qt::Key_W);
2022-07-29 12:52:36 +03:00
cfg->rs_left.def = GetKeyName(Qt::Key_Delete);
cfg->rs_down.def = GetKeyName(Qt::Key_End);
cfg->rs_right.def = GetKeyName(Qt::Key_PageDown);
cfg->rs_up.def = GetKeyName(Qt::Key_Home);
2017-12-23 22:25:51 +01:00
cfg->start.def = GetKeyName(Qt::Key_Return);
cfg->select.def = GetKeyName(Qt::Key_Space);
cfg->ps.def = GetKeyName(Qt::Key_Backspace);
cfg->square.def = GetKeyName(Qt::Key_Z);
cfg->cross.def = GetKeyName(Qt::Key_X);
cfg->circle.def = GetKeyName(Qt::Key_C);
cfg->triangle.def = GetKeyName(Qt::Key_V);
cfg->left.def = GetKeyName(Qt::Key_Left);
cfg->down.def = GetKeyName(Qt::Key_Down);
cfg->right.def = GetKeyName(Qt::Key_Right);
cfg->up.def = GetKeyName(Qt::Key_Up);
cfg->r1.def = GetKeyName(Qt::Key_E);
cfg->r2.def = GetKeyName(Qt::Key_T);
cfg->r3.def = GetKeyName(Qt::Key_G);
cfg->l1.def = GetKeyName(Qt::Key_Q);
cfg->l2.def = GetKeyName(Qt::Key_R);
cfg->l3.def = GetKeyName(Qt::Key_F);
2021-08-08 16:45:45 +02:00
cfg->pressure_intensity_button.def = GetKeyName(Qt::NoButton);
2024-08-09 23:44:45 +02:00
cfg->analog_limiter_button.def = GetKeyName(Qt::NoButton);
2021-08-06 02:08:18 +02:00
2024-05-27 21:50:09 +02:00
cfg->lstick_anti_deadzone.def = 0;
cfg->rstick_anti_deadzone.def = 0;
cfg->lstickdeadzone.def = 0;
cfg->rstickdeadzone.def = 0;
cfg->ltriggerthreshold.def = 0;
cfg->rtriggerthreshold.def = 0;
2017-12-23 22:25:51 +01:00
// apply defaults
cfg->from_default();
}
2017-08-15 14:03:07 +02:00
void keyboard_pad_handler::Key(const u32 code, bool pressed, u16 value)
{
if (!pad::g_enabled)
{
return;
}
value = Clamp0To255(value);
2021-08-09 13:57:06 +02:00
for (auto& pad : m_pads_internal)
2017-08-15 14:03:07 +02:00
{
2026-03-15 11:01:58 +01:00
const auto register_new_button_value = [code, pressed, value](Button& btn) -> u16
{
// Make sure we keep this button pressed until all related keys are released.
if (pressed)
{
2026-03-15 11:01:58 +01:00
btn.m_pressed_keys[code] = value;
}
else
{
2026-03-15 11:01:58 +01:00
btn.m_pressed_keys.erase(code);
// Optimization: just skip the whole combo parsing if there are no keys pressed
if (btn.m_pressed_keys.empty())
{
return 0;
}
}
u16 actual_value = 0;
2026-03-15 11:01:58 +01:00
// Get the max value of all pressed keys for this DS3 button
for (const std::set<u32>& key_codes : btn.m_key_combos)
{
2026-03-15 11:01:58 +01:00
if (key_codes.empty()) continue;
// Every key in this combination has to be pressed for this button to be considered pressed
u16 combo_value = 0;
const bool combo_pressed = std::all_of(key_codes.cbegin(), key_codes.cend(), [&btn, &combo_value](u32 key_code)
{
const auto it = btn.m_pressed_keys.find(key_code);
if (it == btn.m_pressed_keys.cend() || it->second == 0)
{
return false; // This combo button is not pressed, so the combo is not pressed either.
}
// Take minimum combo value. Otherwise we will always end up with the max value.
combo_value = (combo_value == 0) ? it->second : std::min(combo_value, it->second);
return true;
});
if (combo_pressed)
{
actual_value = std::max(actual_value, combo_value);
}
}
return actual_value;
};
// Find out if special buttons are pressed (introduced by RPCS3).
// Activate the buttons here if possible since keys don't auto-repeat. This ensures that they are already pressed in the following loop.
2026-03-15 11:01:58 +01:00
const auto update_special_button_press = [&pad, &register_new_button_value, code](s32 index)
{
if (index < 0) return;
Button& pressure_intensity_button = pad.m_buttons[index];
if (std::any_of(pressure_intensity_button.m_key_combos.cbegin(), pressure_intensity_button.m_key_combos.cend(), [code](const std::set<u32>& key_codes){ return key_codes.contains(code); }))
{
2026-03-15 11:01:58 +01:00
const u16 actual_value = register_new_button_value(pressure_intensity_button);
pressure_intensity_button.m_pressed = actual_value > 0;
pressure_intensity_button.m_value = actual_value;
}
2026-03-15 11:01:58 +01:00
};
update_special_button_press(pad.m_pressure_intensity_button_index);
const bool adjust_pressure = pad.get_pressure_intensity_button_active(m_pressure_intensity_toggle_mode, pad.m_player_id);
const bool adjust_pressure_changed = pad.m_adjust_pressure_last != adjust_pressure;
if (adjust_pressure_changed)
{
pad.m_adjust_pressure_last = adjust_pressure;
}
2023-06-05 21:56:01 +02:00
2026-03-15 11:01:58 +01:00
update_special_button_press(pad.m_analog_limiter_button_index);
2024-08-09 23:44:45 +02:00
const bool analog_limiter_enabled = pad.get_analog_limiter_button_active(m_analog_limiter_toggle_mode, pad.m_player_id);
const bool analog_limiter_changed = pad.m_analog_limiter_enabled_last != analog_limiter_enabled;
const u32 l_stick_multiplier = analog_limiter_enabled ? m_l_stick_multiplier : 100;
const u32 r_stick_multiplier = analog_limiter_enabled ? m_r_stick_multiplier : 100;
if (analog_limiter_changed)
{
pad.m_analog_limiter_enabled_last = analog_limiter_enabled;
}
// Handle buttons
for (usz i = 0; i < pad.m_buttons.size(); i++)
2017-08-15 14:03:07 +02:00
{
2024-08-09 23:44:45 +02:00
// Ignore special buttons
if (static_cast<s32>(i) == pad.m_pressure_intensity_button_index ||
2026-03-15 11:01:58 +01:00
static_cast<s32>(i) == pad.m_analog_limiter_button_index ||
static_cast<s32>(i) == pad.m_orientation_reset_button_index)
continue;
Button& button = pad.m_buttons[i];
bool update_button = true;
2026-03-15 11:01:58 +01:00
if (std::none_of(button.m_key_combos.cbegin(), button.m_key_combos.cend(), [code](const std::set<u32>& key_codes){ return key_codes.contains(code); }))
{
// Handle pressure changes anyway
update_button = adjust_pressure_changed;
}
else
{
2026-03-15 11:01:58 +01:00
button.m_actual_value = register_new_button_value(button);
// to get the fastest response time possible we don't wanna use any lerp with factor 1
if (button.m_analog)
{
update_button = m_analog_lerp_factor >= 1.0f;
}
else if (button.m_trigger)
{
update_button = m_trigger_lerp_factor >= 1.0f;
}
}
2024-08-09 23:44:45 +02:00
if (!update_button)
continue;
2024-08-09 23:44:45 +02:00
if (button.m_actual_value > 0)
{
// Modify pressure if necessary if the button was pressed
if (adjust_pressure)
{
button.m_value = pad.m_pressure_intensity;
}
else if (m_pressure_intensity_deadzone > 0)
{
button.m_value = NormalizeDirectedInput(button.m_actual_value, m_pressure_intensity_deadzone, 255);
}
else
{
2024-08-09 23:44:45 +02:00
button.m_value = button.m_actual_value;
}
2024-08-09 23:44:45 +02:00
button.m_pressed = button.m_value > 0;
}
else
{
button.m_value = 0;
button.m_pressed = false;
}
2017-08-15 14:03:07 +02:00
}
// Handle sticks
2021-08-09 13:57:06 +02:00
for (usz i = 0; i < pad.m_sticks.size(); i++)
2017-08-15 14:03:07 +02:00
{
AnalogStick& stick = pad.m_sticks[i];
2024-08-09 23:44:45 +02:00
const bool is_left_stick = i < 2;
2026-03-15 11:01:58 +01:00
const bool is_max = std::any_of(stick.m_key_combos_max.cbegin(), stick.m_key_combos_max.cend(), [code](const std::set<u32>& combo) { return combo.contains(code); });
const bool is_min = std::any_of(stick.m_key_combos_min.cbegin(), stick.m_key_combos_min.cend(), [code](const std::set<u32>& combo) { return combo.contains(code); });
2017-12-23 22:25:51 +01:00
if (!is_max && !is_min)
{
2024-08-09 23:44:45 +02:00
if (!analog_limiter_changed)
continue;
2024-08-09 23:44:45 +02:00
// Update already pressed sticks
2026-03-15 11:01:58 +01:00
const bool is_min_pressed = !stick.m_pressed_combos_min.empty();
const bool is_max_pressed = !stick.m_pressed_combos_max.empty();
2024-08-09 23:44:45 +02:00
const u32 stick_multiplier = is_left_stick ? l_stick_multiplier : r_stick_multiplier;
const u16 actual_min_value = is_min_pressed ? MultipliedInput(255, stick_multiplier) : 255;
const u16 normalized_min_value = std::ceil(actual_min_value / 2.0);
const u16 actual_max_value = is_max_pressed ? MultipliedInput(255, stick_multiplier) : 255;
const u16 normalized_max_value = std::ceil(actual_max_value / 2.0);
stick.m_stick_min = is_min_pressed ? std::min<u8>(normalized_min_value, 128) : 0;
stick.m_stick_max = is_max_pressed ? std::min<int>(128 + normalized_max_value, 255) : 128;
}
2024-08-09 23:44:45 +02:00
else
{
2024-08-09 23:44:45 +02:00
const u16 actual_value = pressed ? MultipliedInput(value, is_left_stick ? l_stick_multiplier : r_stick_multiplier) : value;
const auto register_new_stick_value = [&](bool is_max) -> std::pair<bool, u16>
2024-08-09 23:44:45 +02:00
{
2026-03-15 11:01:58 +01:00
const std::vector<std::set<u32>>& key_combos = is_max ? stick.m_key_combos_max : stick.m_key_combos_min;
std::map<u32, u16>& pressed_keys = is_max ? stick.m_pressed_keys_max : stick.m_pressed_keys_min;
std::map<u32, u16>& pressed_combos = is_max ? stick.m_pressed_combos_max : stick.m_pressed_combos_min;
2024-08-09 23:44:45 +02:00
// Make sure we keep this stick pressed until all related keys are released.
if (pressed)
{
2026-03-15 11:01:58 +01:00
pressed_keys[code] = actual_value;
2024-08-09 23:44:45 +02:00
}
else
{
pressed_keys.erase(code);
}
2026-03-18 10:18:24 +01:00
// Get the value of all the combos for this stick direction
2026-03-15 11:01:58 +01:00
bool any_combo_pressed = false;
u16 new_val = 0;
for (const std::set<u32>& key_codes : key_combos)
2024-08-09 23:44:45 +02:00
{
2026-03-15 11:01:58 +01:00
if (key_codes.empty()) continue;
// Every key in this combination has to be pressed for this button to be considered pressed
u16 combo_value = 0;
const bool combo_pressed = std::all_of(key_codes.cbegin(), key_codes.cend(), [&pressed_keys, &combo_value, is_max, i, pressed](u32 key_code)
{
const auto it = pressed_keys.find(key_code);
if (it == pressed_keys.cend() || it->second == 0)
{
return false; // This combo button is not pressed, so the combo is not pressed either.
}
// Take minimum combo value. Otherwise we will always end up with the max value.
combo_value = (combo_value == 0) ? it->second : std::min(combo_value, it->second);
return true;
});
if (combo_pressed)
{
// Take minimum combo value. Otherwise we will always end up with the max value.
new_val = (new_val == 0) ? combo_value : std::min(new_val, combo_value);
any_combo_pressed = true;
}
2024-08-09 23:44:45 +02:00
}
2026-03-15 11:01:58 +01:00
2026-03-18 10:18:24 +01:00
// Make sure we keep this combo pressed until all related keys are released.
2026-03-15 11:01:58 +01:00
if (any_combo_pressed)
{
2026-03-18 10:18:24 +01:00
new_val = std::ceil(new_val / 2.0f);
pressed_combos[code] = new_val;
2026-03-15 11:01:58 +01:00
}
else
{
pressed_combos.erase(code);
2026-03-18 10:18:24 +01:00
if (pressed_combos.empty())
{
return std::pair(false, 0);
}
2026-03-15 11:01:58 +01:00
}
2026-03-18 10:18:24 +01:00
// Get the min/max value of all pressed combos for this stick
const auto min_max_it = is_max
? std::max_element(pressed_combos.cbegin(), pressed_combos.cend(), [](const auto& a, const auto& b) { return a.second < b.second; })
: std::min_element(pressed_combos.cbegin(), pressed_combos.cend(), [](const auto& a, const auto& b) { return a.second < b.second; });
return std::pair(!pressed_combos.empty(), min_max_it->second);
2024-08-09 23:44:45 +02:00
};
if (is_max)
{
2026-03-15 11:01:58 +01:00
const std::pair<bool, u16> stick_value = register_new_stick_value(true);
2024-08-09 23:44:45 +02:00
stick.m_stick_max = stick_value.first ? std::min<int>(128 + stick_value.second, 255) : 128;
2024-08-09 23:44:45 +02:00
}
if (is_min)
{
2026-03-15 11:01:58 +01:00
const std::pair<bool, u16> stick_value = register_new_stick_value(false);
2024-08-09 23:44:45 +02:00
stick.m_stick_min = stick_value.first ? std::min<u8>(stick_value.second, 128) : 0;
2024-08-09 23:44:45 +02:00
}
}
stick.m_stick_val = stick.m_stick_max - stick.m_stick_min;
const f32 stick_lerp_factor = is_left_stick ? m_l_stick_lerp_factor : m_r_stick_lerp_factor;
// to get the fastest response time possible we don't wanna use any lerp with factor 1
if (stick_lerp_factor >= 1.0f)
2018-12-29 15:56:18 +01:00
{
stick.m_value = stick.m_stick_val;
2018-12-29 15:56:18 +01:00
}
2017-08-15 14:03:07 +02:00
}
}
}
void keyboard_pad_handler::release_all_keys()
{
2021-08-09 13:57:06 +02:00
for (auto& pad : m_pads_internal)
{
2021-08-09 13:57:06 +02:00
for (Button& button : pad.m_buttons)
{
button.m_pressed = false;
button.m_value = 0;
button.m_actual_value = 0;
}
for (AnalogStick& stick : pad.m_sticks)
{
stick.m_value = 128;
stick.m_stick_min = 0;
stick.m_stick_max = 128;
stick.m_stick_val = 128;
stick.m_pressed_keys_min.clear();
stick.m_pressed_keys_max.clear();
stick.m_pressed_combos_min.clear();
stick.m_pressed_combos_max.clear();
}
}
m_keys_released = true;
}
2017-06-04 09:48:33 -05:00
bool keyboard_pad_handler::eventFilter(QObject* target, QEvent* ev)
2016-02-02 00:51:09 +03:00
{
if (!ev) [[unlikely]]
{
return false;
}
if (input::g_active_mouse_and_keyboard != input::active_mouse_and_keyboard::pad)
{
if (!m_keys_released)
{
release_all_keys();
}
return false;
}
m_keys_released = false;
// !m_target is for future proofing when gsrender isn't automatically initialized on load.
// !m_target->isVisible() is a hack since currently a guiless application will STILL inititialize a gsrender (providing a valid target)
if (!m_target || !m_target->isVisible()|| target == m_target)
2017-06-04 09:48:33 -05:00
{
switch (ev->type())
2017-06-04 09:48:33 -05:00
{
case QEvent::KeyPress:
2017-06-04 09:48:33 -05:00
keyPressEvent(static_cast<QKeyEvent*>(ev));
break;
case QEvent::KeyRelease:
2017-06-04 09:48:33 -05:00
keyReleaseEvent(static_cast<QKeyEvent*>(ev));
break;
case QEvent::MouseButtonPress:
mousePressEvent(static_cast<QMouseEvent*>(ev));
break;
case QEvent::MouseButtonRelease:
mouseReleaseEvent(static_cast<QMouseEvent*>(ev));
break;
case QEvent::MouseMove:
mouseMoveEvent(static_cast<QMouseEvent*>(ev));
break;
2019-12-29 09:33:15 +02:00
case QEvent::Wheel:
mouseWheelEvent(static_cast<QWheelEvent*>(ev));
break;
case QEvent::FocusOut:
release_all_keys();
break;
default:
break;
2017-06-04 09:48:33 -05:00
}
}
return false;
2016-02-02 00:51:09 +03:00
}
/* Sets the target window for the event handler, and also installs an event filter on the target. */
void keyboard_pad_handler::SetTargetWindow(QWindow* target)
{
if (target != nullptr)
{
m_target = target;
target->installEventFilter(this);
}
else
{
QApplication::instance()->installEventFilter(this);
// If this is hit, it probably means that some refactoring occurs because currently a gsframe is created in Load.
// We still want events so filter from application instead since target is null.
2020-02-01 07:15:50 +03:00
input_log.error("Trying to set pad handler to a null target window.");
}
}
void keyboard_pad_handler::processKeyEvent(QKeyEvent* event, bool pressed)
2016-02-02 00:51:09 +03:00
{
if (!event) [[unlikely]]
{
return;
}
2017-06-23 19:13:40 +03:00
if (event->isAutoRepeat())
{
event->ignore();
return;
}
const auto handle_key = [this, pressed, event]()
2017-12-22 17:03:24 +01:00
{
QStringList list = GetKeyNames(event);
if (list.isEmpty())
return;
2023-12-29 11:35:01 +01:00
const bool is_num_key = list.removeAll("Num") > 0;
2024-08-13 20:15:14 +02:00
const QString name = QString::fromStdString(GetKeyName(event, true));
2017-12-22 17:03:24 +01:00
// TODO: Edge case: switching numlock keeps numpad keys pressed due to now different modifier
// Handle every possible key combination, for example: ctrl+A -> {ctrl, A, ctrl+A}
for (const QString& keyname : list)
2017-12-22 17:03:24 +01:00
{
// skip the 'original keys' when handling numpad keys
if (is_num_key && !keyname.contains("Num"))
continue;
// skip held modifiers when handling another key
if (keyname != name && list.count() > 1 && (keyname == "Alt" || keyname == "AltGr" || keyname == "Ctrl" || keyname == "Meta" || keyname == "Shift"))
continue;
Key(GetKeyCode(keyname), pressed);
}
};
2023-11-25 01:33:53 +01:00
handle_key();
event->ignore();
2016-02-02 00:51:09 +03:00
}
void keyboard_pad_handler::keyPressEvent(QKeyEvent* event)
{
if (!event) [[unlikely]]
{
return;
}
if (event->modifiers() & Qt::AltModifier)
{
switch (event->key())
{
case Qt::Key_I:
m_deadzone_y = std::min(m_deadzone_y + 1, 255);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: deadzone y = %d", m_deadzone_y);
event->ignore();
return;
case Qt::Key_U:
m_deadzone_y = std::max(0, m_deadzone_y - 1);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: deadzone y = %d", m_deadzone_y);
event->ignore();
return;
2018-08-05 19:26:18 +02:00
case Qt::Key_Y:
m_deadzone_x = std::min(m_deadzone_x + 1, 255);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: deadzone x = %d", m_deadzone_x);
event->ignore();
return;
case Qt::Key_T:
m_deadzone_x = std::max(0, m_deadzone_x - 1);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: deadzone x = %d", m_deadzone_x);
event->ignore();
return;
case Qt::Key_K:
m_multi_y = std::min(m_multi_y + 0.1, 5.0);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: multiplier y = %d", static_cast<int>(m_multi_y * 100));
event->ignore();
return;
case Qt::Key_J:
m_multi_y = std::max(0.0, m_multi_y - 0.1);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: multiplier y = %d", static_cast<int>(m_multi_y * 100));
event->ignore();
return;
case Qt::Key_H:
m_multi_x = std::min(m_multi_x + 0.1, 5.0);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: multiplier x = %d", static_cast<int>(m_multi_x * 100));
event->ignore();
return;
case Qt::Key_G:
m_multi_x = std::max(0.0, m_multi_x - 0.1);
2020-02-01 07:15:50 +03:00
input_log.success("mouse move adjustment: multiplier x = %d", static_cast<int>(m_multi_x * 100));
event->ignore();
return;
default:
break;
}
}
processKeyEvent(event, true);
}
2017-06-04 09:48:33 -05:00
void keyboard_pad_handler::keyReleaseEvent(QKeyEvent* event)
{
processKeyEvent(event, false);
}
2017-06-23 19:13:40 +03:00
void keyboard_pad_handler::mousePressEvent(QMouseEvent* event)
{
if (!event) [[unlikely]]
{
return;
}
2026-04-13 10:47:18 +02:00
Key(mouse::button + static_cast<u32>(event->button()), true);
event->ignore();
}
void keyboard_pad_handler::mouseReleaseEvent(QMouseEvent* event)
{
if (!event) [[unlikely]]
{
return;
}
2026-04-13 10:47:18 +02:00
Key(mouse::button + static_cast<u32>(event->button()), false, 0);
2017-06-23 19:13:40 +03:00
event->ignore();
2017-06-04 09:48:33 -05:00
}
2021-04-07 23:05:18 +02:00
bool keyboard_pad_handler::get_mouse_lock_state() const
2020-08-30 15:12:36 +10:00
{
if (gs_frame* game_frame = dynamic_cast<gs_frame*>(m_target))
return game_frame->get_mouse_lock_state();
2020-08-30 15:12:36 +10:00
return false;
}
void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event)
{
if (!m_mouse_move_used || !event)
{
event->ignore();
return;
}
static int movement_x = 0;
static int movement_y = 0;
2020-08-30 15:12:36 +10:00
if (m_target && m_target->isActive() && get_mouse_lock_state())
{
2019-12-04 23:56:19 +03:00
// get the screen dimensions
const QSize screen = m_target->size();
2019-12-04 23:56:19 +03:00
// get the center of the screen in global coordinates
QPoint p_center = m_target->geometry().topLeft() + QPoint(screen.width() / 2, screen.height() / 2);
2019-12-04 23:56:19 +03:00
// reset the mouse to the center for consistent results since edge movement won't be registered
QCursor::setPos(m_target->screen(), p_center);
2019-12-04 23:56:19 +03:00
// convert the center into screen coordinates
p_center = m_target->mapFromGlobal(p_center);
2019-12-04 23:56:19 +03:00
// get the delta of the mouse position to the screen center
const QPoint p_delta = event->pos() - p_center;
2022-05-05 16:23:30 +02:00
if (m_mouse_movement_mode == mouse_movement_mode::relative)
{
movement_x = p_delta.x();
movement_y = p_delta.y();
}
else
{
// current mouse position, starting at the center
static QPoint p_real(p_center);
// update the current position without leaving the screen borders
p_real.setX(std::clamp(p_real.x() + p_delta.x(), 0, screen.width()));
p_real.setY(std::clamp(p_real.y() + p_delta.y(), 0, screen.height()));
// get the delta of the real mouse position to the screen center
const QPoint p_real_delta = p_real - p_center;
movement_x = p_real_delta.x();
movement_y = p_real_delta.y();
}
}
2022-05-05 16:23:30 +02:00
else if (m_mouse_movement_mode == mouse_movement_mode::relative)
{
static int last_pos_x = 0;
static int last_pos_y = 0;
2021-05-22 10:42:05 +02:00
const QPoint e_pos = event->pos();
2021-05-22 10:42:05 +02:00
movement_x = e_pos.x() - last_pos_x;
movement_y = e_pos.y() - last_pos_y;
2021-05-22 10:42:05 +02:00
last_pos_x = e_pos.x();
last_pos_y = e_pos.y();
}
2022-05-05 16:23:30 +02:00
else if (m_target && m_target->isActive())
{
// get the screen dimensions
const QSize screen = m_target->size();
// get the center of the screen in global coordinates
QPoint p_center = m_target->geometry().topLeft() + QPoint(screen.width() / 2, screen.height() / 2);
// convert the center into screen coordinates
p_center = m_target->mapFromGlobal(p_center);
// get the delta of the mouse position to the screen center
const QPoint p_delta = event->pos() - p_center;
movement_x = p_delta.x();
movement_y = p_delta.y();
}
2021-10-04 20:54:17 +02:00
movement_x *= m_multi_x;
movement_y *= m_multi_y;
2020-09-09 02:00:48 +02:00
int deadzone_x = 0;
int deadzone_y = 0;
if (movement_x == 0 && movement_y != 0)
{
deadzone_y = m_deadzone_y;
}
else if (movement_y == 0 && movement_x != 0)
{
deadzone_x = m_deadzone_x;
}
else if (movement_x != 0 && movement_y != 0 && m_deadzone_x != 0 && m_deadzone_y != 0)
{
// Calculate the point on our deadzone ellipsis intersected with the line (0, 0)(movement_x, movement_y)
// Ellipsis: 1 = (x²/a²) + (y²/b²) ; where: a = m_deadzone_x and b = m_deadzone_y
// Line: y = mx + t ; where: t = 0 and m = (movement_y / movement_x)
// Combined: x = +-(a*b)/sqrt(a²m²+b²) ; where +- is always +, since we only want the magnitude
const double a = m_deadzone_x;
const double b = m_deadzone_y;
2021-04-07 23:05:18 +02:00
const double m = static_cast<double>(movement_y) / static_cast<double>(movement_x);
2020-09-09 02:00:48 +02:00
deadzone_x = a * b / std::sqrt(std::pow(a, 2) * std::pow(m, 2) + std::pow(b, 2));
deadzone_y = std::abs(m * deadzone_x);
}
if (movement_x < 0)
{
Key(mouse::move_right, false);
2020-09-09 02:00:48 +02:00
Key(mouse::move_left, true, std::min(deadzone_x + std::abs(movement_x), 255));
m_last_mouse_move_left = steady_clock::now();
}
else if (movement_x > 0)
{
Key(mouse::move_left, false);
2020-09-09 02:00:48 +02:00
Key(mouse::move_right, true, std::min(deadzone_x + movement_x, 255));
m_last_mouse_move_right = steady_clock::now();
}
// in Qt mouse up is equivalent to movement_y < 0
if (movement_y < 0)
{
Key(mouse::move_down, false);
2020-09-09 02:00:48 +02:00
Key(mouse::move_up, true, std::min(deadzone_y + std::abs(movement_y), 255));
m_last_mouse_move_up = steady_clock::now();
}
else if (movement_y > 0)
{
Key(mouse::move_up, false);
2020-09-09 02:00:48 +02:00
Key(mouse::move_down, true, std::min(deadzone_y + movement_y, 255));
m_last_mouse_move_down = steady_clock::now();
}
event->ignore();
}
2019-12-29 09:33:15 +02:00
void keyboard_pad_handler::mouseWheelEvent(QWheelEvent* event)
{
if (!m_mouse_wheel_used || !event)
2021-04-19 23:09:25 +02:00
{
return;
}
const QPoint direction = event->angleDelta();
2019-12-29 09:33:15 +02:00
if (direction.isNull())
{
// Scrolling started/ended event, no direction given
return;
}
if (const int x = direction.x())
{
const bool to_left = event->inverted() ? x < 0 : x > 0;
2019-12-29 09:33:15 +02:00
if (to_left)
{
Key(mouse::wheel_left, true);
m_last_wheel_move_left = steady_clock::now();
2019-12-29 09:33:15 +02:00
}
else
{
Key(mouse::wheel_right, true);
m_last_wheel_move_right = steady_clock::now();
2019-12-29 09:33:15 +02:00
}
}
if (const int y = direction.y())
{
const bool to_up = event->inverted() ? y < 0 : y > 0;
2019-12-29 09:33:15 +02:00
if (to_up)
{
Key(mouse::wheel_up, true);
m_last_wheel_move_up = steady_clock::now();
2019-12-29 09:33:15 +02:00
}
else
{
Key(mouse::wheel_down, true);
m_last_wheel_move_down = steady_clock::now();
2019-12-29 09:33:15 +02:00
}
}
}
2022-08-13 09:56:04 +02:00
std::vector<pad_list_entry> keyboard_pad_handler::list_devices()
2016-02-02 00:51:09 +03:00
{
2022-08-13 09:56:04 +02:00
std::vector<pad_list_entry> list_devices;
list_devices.emplace_back(std::string(pad::keyboard_device_name), false);
2017-08-15 14:03:07 +02:00
return list_devices;
}
std::string keyboard_pad_handler::GetMouseName(const QMouseEvent* event)
{
2026-04-15 23:47:13 +02:00
return GetMouseName(static_cast<u32>(mouse::button) + static_cast<u32>(event->button()));
}
std::string keyboard_pad_handler::GetMouseName(u32 button)
{
2021-04-07 23:05:18 +02:00
if (const auto it = mouse_list.find(button); it != mouse_list.cend())
return it->second;
return "FAIL";
}
2017-12-22 17:03:24 +01:00
QStringList keyboard_pad_handler::GetKeyNames(const QKeyEvent* keyEvent)
{
QStringList list;
if (keyEvent->modifiers() & Qt::ShiftModifier)
{
list.append("Shift");
list.append(QKeySequence(keyEvent->key() | Qt::ShiftModifier).toString(QKeySequence::NativeText));
}
if (keyEvent->modifiers() & Qt::AltModifier)
{
list.append("Alt");
list.append(QKeySequence(keyEvent->key() | Qt::AltModifier).toString(QKeySequence::NativeText));
}
if (keyEvent->modifiers() & Qt::ControlModifier)
{
list.append("Ctrl");
list.append(QKeySequence(keyEvent->key() | Qt::ControlModifier).toString(QKeySequence::NativeText));
}
if (keyEvent->modifiers() & Qt::MetaModifier)
{
list.append("Meta");
list.append(QKeySequence(keyEvent->key() | Qt::MetaModifier).toString(QKeySequence::NativeText));
}
if (keyEvent->modifiers() & Qt::KeypadModifier)
{
list.append("Num"); // helper object, not used as actual key
list.append(QKeySequence(keyEvent->key() | Qt::KeypadModifier).toString(QKeySequence::NativeText));
}
// Handle special cases
if (const std::string name = native_scan_code_to_string(keyEvent->nativeScanCode()); !name.empty())
{
2023-06-28 22:14:56 +02:00
list.append(QString::fromStdString(name));
}
2017-12-22 17:03:24 +01:00
switch (keyEvent->key())
{
case Qt::Key_Alt:
list.append("Alt");
break;
case Qt::Key_AltGr:
list.append("AltGr");
break;
case Qt::Key_Shift:
list.append("Shift");
break;
case Qt::Key_Control:
list.append("Ctrl");
break;
case Qt::Key_Meta:
list.append("Meta");
break;
default:
list.append(QKeySequence(keyEvent->key()).toString(QKeySequence::NativeText));
break;
}
list.removeDuplicates();
return list;
}
2024-08-13 20:15:14 +02:00
std::string keyboard_pad_handler::GetKeyName(const QKeyEvent* keyEvent, bool with_modifiers)
{
// Handle special cases first
2021-04-07 23:05:18 +02:00
if (std::string name = native_scan_code_to_string(keyEvent->nativeScanCode()); !name.empty())
{
return name;
}
switch (keyEvent->key())
{
2024-01-01 20:51:50 +01:00
case Qt::Key_Alt: return "Alt";
case Qt::Key_AltGr: return "AltGr";
case Qt::Key_Shift: return "Shift";
case Qt::Key_Control: return "Ctrl";
case Qt::Key_Meta: return "Meta";
case Qt::Key_NumLock: return QKeySequence(keyEvent->key()).toString(QKeySequence::NativeText).toStdString();
2022-01-10 14:48:26 +01:00
#ifdef __APPLE__
// On macOS, the arrow keys are considered to be part of the keypad;
// since most Mac keyboards lack a keypad to begin with,
// we change them to regular arrows to avoid confusion
2024-01-01 20:51:50 +01:00
case Qt::Key_Left: return "←";
case Qt::Key_Up: return "↑";
case Qt::Key_Right: return "→";
case Qt::Key_Down: return "↓";
2022-01-10 14:48:26 +01:00
#endif
default:
break;
}
2024-08-13 20:15:14 +02:00
if (with_modifiers)
{
return QKeySequence(keyEvent->key() | keyEvent->modifiers()).toString(QKeySequence::NativeText).toStdString();
}
return QKeySequence(keyEvent->key()).toString(QKeySequence::NativeText).toStdString();
}
2026-05-20 17:32:09 +02:00
std::string keyboard_pad_handler::GetKeyName(u32 keyCode)
{
2023-06-28 22:14:56 +02:00
return QKeySequence(keyCode).toString(QKeySequence::NativeText).toStdString();
}
2026-06-14 06:21:03 +02:00
std::vector<std::set<u32>> keyboard_pad_handler::GetKeyCombos(std::string_view cfg_string)
{
2026-03-15 11:01:58 +01:00
std::vector<std::set<u32>> res;
2026-04-13 10:47:18 +02:00
for (const pad::combo& combo : cfg_pad::get_combos(cfg_string))
2023-06-13 01:19:32 +02:00
{
2026-04-13 10:47:18 +02:00
std::set<u32> key_codes = find_key_codes(mouse_list, combo);
2026-03-15 11:01:58 +01:00
for (const std::string& button : combo.buttons())
2023-06-13 01:19:32 +02:00
{
if (u32 code = GetKeyCode(QString::fromStdString(button)); code != Qt::NoButton)
2026-03-15 11:01:58 +01:00
{
key_codes.insert(code);
}
}
if (!key_codes.empty())
{
res.push_back(std::move(key_codes));
2023-06-13 01:19:32 +02:00
}
}
2026-03-15 11:01:58 +01:00
return res;
2017-12-22 17:03:24 +01:00
}
u32 keyboard_pad_handler::GetKeyCode(const QString& keyName)
{
2024-01-01 20:51:50 +01:00
if (keyName.isEmpty()) return Qt::NoButton;
2023-06-28 22:14:56 +02:00
if (const int native_scan_code = native_scan_code_from_string(keyName.toStdString()); native_scan_code >= 0)
return Qt::Key_unknown + native_scan_code; // Special cases that can't be expressed with Qt::Key
2024-01-01 20:51:50 +01:00
if (keyName == "Alt") return Qt::Key_Alt;
if (keyName == "AltGr") return Qt::Key_AltGr;
if (keyName == "Shift") return Qt::Key_Shift;
if (keyName == "Ctrl") return Qt::Key_Control;
if (keyName == "Meta") return Qt::Key_Meta;
2022-01-10 14:48:26 +01:00
#ifdef __APPLE__
// QKeySequence doesn't work properly for the arrow keys on macOS
if (keyName == "Num←" || keyName == "←") return Qt::Key_Left;
if (keyName == "Num↑" || keyName == "↑") return Qt::Key_Up;
if (keyName == "Num→" || keyName == "→") return Qt::Key_Right;
if (keyName == "Num↓" || keyName == "↓") return Qt::Key_Down;
2022-01-10 14:48:26 +01:00
#endif
2021-04-07 23:05:18 +02:00
const QKeySequence seq(keyName);
2023-06-13 01:19:32 +02:00
u32 key_code = Qt::NoButton;
2017-12-22 17:03:24 +01:00
2021-05-22 10:42:05 +02:00
if (seq.count() == 1 && seq[0].key() != Qt::Key_unknown)
key_code = seq[0].key();
else
2023-06-12 03:45:37 +02:00
input_log.notice("GetKeyCode(%s): seq.count() = %d", keyName, seq.count());
2017-12-22 17:03:24 +01:00
return key_code;
}
2021-03-05 22:05:37 +03:00
int keyboard_pad_handler::native_scan_code_from_string([[maybe_unused]] const std::string& key)
{
// NOTE: Qt throws a Ctrl key at us when using Alt Gr first, so right Alt does not work at the moment
if (key == "Shift Left") return native_key::shift_l;
if (key == "Shift Right") return native_key::shift_r;
if (key == "Ctrl Left") return native_key::ctrl_l;
if (key == "Ctrl Right") return native_key::ctrl_r;
if (key == "Alt Left") return native_key::alt_l;
if (key == "Alt Right") return native_key::alt_r;
if (key == "Meta Left") return native_key::meta_l;
if (key == "Meta Right") return native_key::meta_r;
#ifdef _WIN32
2024-01-01 20:51:50 +01:00
if (key == "Num+0" || key == "Num+Ins") return 82;
if (key == "Num+1" || key == "Num+End") return 79;
if (key == "Num+2" || key == "Num+Down") return 80;
if (key == "Num+3" || key == "Num+PgDown") return 81;
if (key == "Num+4" || key == "Num+Left") return 75;
if (key == "Num+5" || key == "Num+Clear") return 76;
if (key == "Num+6" || key == "Num+Right") return 77;
if (key == "Num+7" || key == "Num+Home") return 71;
if (key == "Num+8" || key == "Num+Up") return 72;
if (key == "Num+9" || key == "Num+PgUp") return 73;
if (key == "Num+," || key == "Num+Del") return 83;
if (key == "Num+/") return 309;
if (key == "Num+*") return 55;
if (key == "Num+-") return 74;
if (key == "Num++") return 78;
if (key == "Num+Enter") return 284;
#else
2023-12-29 11:35:01 +01:00
// TODO
#endif
return -1;
}
std::string keyboard_pad_handler::native_scan_code_to_string(int native_scan_code)
{
// NOTE: the other Qt function "nativeVirtualKey" does not distinguish between VK_SHIFT and VK_RSHIFT key in Qt at the moment
// NOTE: Qt throws a Ctrl key at us when using Alt Gr first, so right Alt does not work at the moment
// NOTE: for MacOs: nativeScanCode may not work
switch (native_scan_code)
{
case native_key::shift_l: return "Shift Left";
case native_key::shift_r: return "Shift Right";
case native_key::ctrl_l: return "Ctrl Left";
case native_key::ctrl_r: return "Ctrl Right";
case native_key::alt_l: return "Alt Left";
case native_key::alt_r: return "Alt Right";
case native_key::meta_l: return "Meta Left";
case native_key::meta_r: return "Meta Right";
#ifdef _WIN32
2024-01-01 20:51:50 +01:00
case 82: return "Num+0"; // Also "Num+Ins" depending on numlock
case 79: return "Num+1"; // Also "Num+End" depending on numlock
case 80: return "Num+2"; // Also "Num+Down" depending on numlock
case 81: return "Num+3"; // Also "Num+PgDown" depending on numlock
case 75: return "Num+4"; // Also "Num+Left" depending on numlock
case 76: return "Num+5"; // Also "Num+Clear" depending on numlock
case 77: return "Num+6"; // Also "Num+Right" depending on numlock
case 71: return "Num+7"; // Also "Num+Home" depending on numlock
case 72: return "Num+8"; // Also "Num+Up" depending on numlock
case 73: return "Num+9"; // Also "Num+PgUp" depending on numlock
case 83: return "Num+,"; // Also "Num+Del" depending on numlock
case 309: return "Num+/";
case 57397: return "Num+/";
2024-01-01 20:51:50 +01:00
case 55: return "Num+*";
case 74: return "Num+-";
case 78: return "Num++";
case 284: return "Num+Enter";
case 57372: return "Num+Enter";
#else
// TODO
#endif
2024-01-01 20:51:50 +01:00
default: return "";
}
}
2024-07-06 18:41:41 +02:00
bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad)
2017-08-15 14:03:07 +02:00
{
2024-07-06 18:41:41 +02:00
if (!pad || pad->m_player_id >= g_cfg_input.player.size())
2017-12-10 10:41:55 +01:00
return false;
2017-08-15 14:03:07 +02:00
2024-07-06 18:41:41 +02:00
const cfg_player* player_config = g_cfg_input.player[pad->m_player_id];
2022-08-13 09:56:04 +02:00
if (!player_config || player_config->device.to_string() != pad::keyboard_device_name)
return false;
2024-07-06 18:41:41 +02:00
m_pad_configs[pad->m_player_id].from_string(player_config->config.to_string());
const cfg_pad* cfg = &m_pad_configs[pad->m_player_id];
2021-08-10 21:45:26 +02:00
if (cfg == nullptr)
2017-12-23 22:25:51 +01:00
return false;
2016-02-02 00:51:09 +03:00
2022-05-05 16:23:30 +02:00
m_mouse_movement_mode = cfg->mouse_move_mode;
m_mouse_move_used = false;
2021-04-19 23:09:25 +02:00
m_mouse_wheel_used = false;
2021-08-10 21:45:26 +02:00
m_deadzone_x = cfg->mouse_deadzone_x;
m_deadzone_y = cfg->mouse_deadzone_y;
m_multi_x = cfg->mouse_acceleration_x / 100.0;
m_multi_y = cfg->mouse_acceleration_y / 100.0;
m_l_stick_lerp_factor = cfg->l_stick_lerp_factor / 100.0f;
m_r_stick_lerp_factor = cfg->r_stick_lerp_factor / 100.0f;
m_analog_lerp_factor = cfg->analog_lerp_factor / 100.0f;
m_trigger_lerp_factor = cfg->trigger_lerp_factor / 100.0f;
m_l_stick_multiplier = cfg->lstickmultiplier;
m_r_stick_multiplier = cfg->rstickmultiplier;
2024-08-09 23:44:45 +02:00
m_analog_limiter_toggle_mode = cfg->analog_limiter_toggle_mode.get();
2023-06-05 21:56:01 +02:00
m_pressure_intensity_toggle_mode = cfg->pressure_intensity_toggle_mode.get();
m_pressure_intensity_deadzone = cfg->pressure_intensity_deadzone.get();
2026-03-15 11:01:58 +01:00
const auto find_combos = [this](const cfg::string& name)
{
2026-04-13 10:47:18 +02:00
const std::vector<std::set<u32>> combos = GetKeyCombos(name.to_string());
2026-03-15 11:01:58 +01:00
if (!combos.empty())
2023-06-13 01:19:32 +02:00
{
2026-03-15 11:01:58 +01:00
if (!m_mouse_move_used)
2023-06-13 01:19:32 +02:00
{
2026-03-15 11:01:58 +01:00
if (std::any_of(combos.cbegin(), combos.cend(), [](const std::set<u32>& keys)
{
return keys.contains(mouse::move_left) || keys.contains(mouse::move_right) || keys.contains(mouse::move_up) || keys.contains(mouse::move_down);
}))
{
m_mouse_move_used = true;
}
2023-06-13 01:19:32 +02:00
}
2026-03-15 11:01:58 +01:00
if (!m_mouse_wheel_used)
2023-06-13 01:19:32 +02:00
{
2026-03-15 11:01:58 +01:00
if (std::any_of(combos.cbegin(), combos.cend(), [](const std::set<u32>& keys)
{
return keys.contains(mouse::wheel_left) || keys.contains(mouse::wheel_right) || keys.contains(mouse::wheel_up) || keys.contains(mouse::wheel_down);
}))
{
m_mouse_wheel_used = true;
}
2023-06-13 01:19:32 +02:00
}
}
2026-03-15 11:01:58 +01:00
return combos;
};
2020-04-25 02:12:19 +02:00
u32 pclass_profile = 0x0;
for (const input::product_info& product : input::get_products_by_class(cfg->device_class_type))
2020-04-25 02:12:19 +02:00
{
2021-08-10 21:45:26 +02:00
if (product.vendor_id == cfg->vendor_id && product.product_id == cfg->product_id)
2020-04-25 02:12:19 +02:00
{
pclass_profile = product.pclass_profile;
}
}
// Fixed assign change, default is both sensor and press off
pad->Init
(
2018-01-13 00:55:51 +01:00
CELL_PAD_STATUS_DISCONNECTED,
CELL_PAD_CAPABILITY_PS3_CONFORMITY | CELL_PAD_CAPABILITY_PRESS_MODE | CELL_PAD_CAPABILITY_HP_ANALOG_STICK | CELL_PAD_CAPABILITY_ACTUATOR | CELL_PAD_CAPABILITY_SENSOR_MODE,
2019-02-25 18:47:16 +01:00
CELL_PAD_DEV_TYPE_STANDARD,
2021-08-10 21:45:26 +02:00
cfg->device_class_type,
2020-04-25 02:12:19 +02:00
pclass_profile,
2021-08-10 21:45:26 +02:00
cfg->vendor_id,
cfg->product_id,
cfg->pressure_intensity
2017-08-15 14:03:07 +02:00
);
2016-02-02 00:51:09 +03:00
2024-08-09 23:44:45 +02:00
if (b_has_pressure_intensity_button)
{
2026-03-15 11:01:58 +01:00
pad->m_buttons.emplace_back(special_button_offset, find_combos(cfg->pressure_intensity_button), special_button_value::pressure_intensity);
2024-08-09 23:44:45 +02:00
pad->m_pressure_intensity_button_index = static_cast<s32>(pad->m_buttons.size()) - 1;
}
if (b_has_analog_limiter_button)
{
2026-03-15 11:01:58 +01:00
pad->m_buttons.emplace_back(special_button_offset, find_combos(cfg->analog_limiter_button), special_button_value::analog_limiter);
2024-08-09 23:44:45 +02:00
pad->m_analog_limiter_button_index = static_cast<s32>(pad->m_buttons.size()) - 1;
}
2021-08-06 02:08:18 +02:00
2026-03-15 11:01:58 +01:00
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->left), CELL_PAD_CTRL_LEFT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->down), CELL_PAD_CTRL_DOWN);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->right), CELL_PAD_CTRL_RIGHT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->up), CELL_PAD_CTRL_UP);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->start), CELL_PAD_CTRL_START);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->r3), CELL_PAD_CTRL_R3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->l3), CELL_PAD_CTRL_L3);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->select), CELL_PAD_CTRL_SELECT);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL1, find_combos(cfg->ps), CELL_PAD_CTRL_PS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->square), CELL_PAD_CTRL_SQUARE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->cross), CELL_PAD_CTRL_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->circle), CELL_PAD_CTRL_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->triangle), CELL_PAD_CTRL_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->r1), CELL_PAD_CTRL_R1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->l1), CELL_PAD_CTRL_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->r2), CELL_PAD_CTRL_R2);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_DIGITAL2, find_combos(cfg->l2), CELL_PAD_CTRL_L2);
2016-02-02 00:51:09 +03:00
2023-08-26 15:47:52 +02:00
if (pad->m_class_type == CELL_PAD_PCLASS_TYPE_SKATEBOARD)
{
2026-03-15 11:01:58 +01:00
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->ir_nose), CELL_PAD_CTRL_PRESS_TRIANGLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->ir_tail), CELL_PAD_CTRL_PRESS_CIRCLE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->ir_left), CELL_PAD_CTRL_PRESS_CROSS);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->ir_right), CELL_PAD_CTRL_PRESS_SQUARE);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->tilt_left), CELL_PAD_CTRL_PRESS_L1);
pad->m_buttons.emplace_back(CELL_PAD_BTN_OFFSET_PRESS_PIGGYBACK, find_combos(cfg->tilt_right), CELL_PAD_CTRL_PRESS_R1);
2023-08-26 15:47:52 +02:00
}
2026-03-15 11:01:58 +01:00
pad->m_sticks[0] = AnalogStick(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_X, find_combos(cfg->ls_left), find_combos(cfg->ls_right));
pad->m_sticks[1] = AnalogStick(CELL_PAD_BTN_OFFSET_ANALOG_LEFT_Y, find_combos(cfg->ls_up), find_combos(cfg->ls_down));
pad->m_sticks[2] = AnalogStick(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_X, find_combos(cfg->rs_left), find_combos(cfg->rs_right));
pad->m_sticks[3] = AnalogStick(CELL_PAD_BTN_OFFSET_ANALOG_RIGHT_Y, find_combos(cfg->rs_up), find_combos(cfg->rs_down));
2017-08-15 14:03:07 +02:00
pad->m_sensors[0] = AnalogSensor(CELL_PAD_BTN_OFFSET_SENSOR_X, 0, 0, 0, DEFAULT_MOTION_X);
pad->m_sensors[1] = AnalogSensor(CELL_PAD_BTN_OFFSET_SENSOR_Y, 0, 0, 0, DEFAULT_MOTION_Y);
pad->m_sensors[2] = AnalogSensor(CELL_PAD_BTN_OFFSET_SENSOR_Z, 0, 0, 0, DEFAULT_MOTION_Z);
pad->m_sensors[3] = AnalogSensor(CELL_PAD_BTN_OFFSET_SENSOR_G, 0, 0, 0, DEFAULT_MOTION_G);
pad->m_vibrate_motors[0] = VibrateMotor(true);
pad->m_vibrate_motors[1] = VibrateMotor(false);
2017-08-15 14:03:07 +02:00
2022-08-13 09:56:04 +02:00
m_bindings.emplace_back(pad, nullptr, nullptr);
2021-08-09 13:57:06 +02:00
m_pads_internal.push_back(*pad);
2017-08-15 14:03:07 +02:00
return true;
}
void keyboard_pad_handler::process()
2017-08-15 14:03:07 +02:00
{
2023-06-13 01:19:32 +02:00
constexpr double stick_interval = 10.0;
constexpr double button_interval = 10.0;
const auto now = steady_clock::now();
const double elapsed_stick = std::chrono::duration_cast<std::chrono::microseconds>(now - m_stick_time).count() / 1000.0;
const double elapsed_button = std::chrono::duration_cast<std::chrono::microseconds>(now - m_button_time).count() / 1000.0;
const bool update_sticks = elapsed_stick > stick_interval;
const bool update_buttons = elapsed_button > button_interval;
if (update_sticks)
{
m_stick_time = now;
}
if (update_buttons)
{
m_button_time = now;
}
2022-05-05 16:23:30 +02:00
if (m_mouse_move_used && m_mouse_movement_mode == mouse_movement_mode::relative)
{
2023-06-13 01:19:32 +02:00
constexpr double mouse_interval = 30.0;
2021-04-19 23:09:25 +02:00
const double elapsed_left = std::chrono::duration_cast<std::chrono::microseconds>(now - m_last_mouse_move_left).count() / 1000.0;
const double elapsed_right = std::chrono::duration_cast<std::chrono::microseconds>(now - m_last_mouse_move_right).count() / 1000.0;
const double elapsed_up = std::chrono::duration_cast<std::chrono::microseconds>(now - m_last_mouse_move_up).count() / 1000.0;
const double elapsed_down = std::chrono::duration_cast<std::chrono::microseconds>(now - m_last_mouse_move_down).count() / 1000.0;
// roughly 1-2 frames to process the next mouse move
if (elapsed_left > mouse_interval)
{
Key(mouse::move_left, false);
m_last_mouse_move_left = now;
}
if (elapsed_right > mouse_interval)
{
Key(mouse::move_right, false);
m_last_mouse_move_right = now;
}
if (elapsed_up > mouse_interval)
{
Key(mouse::move_up, false);
m_last_mouse_move_up = now;
}
if (elapsed_down > mouse_interval)
{
Key(mouse::move_down, false);
m_last_mouse_move_down = now;
}
}
const auto get_lerped = [](f32 v0, f32 v1, f32 lerp_factor)
{
// linear interpolation from the current value v0 to the desired value v1
const f32 res = std::lerp(v0, v1, lerp_factor);
// round to the correct direction to prevent sticky values on small factors
return (v0 <= v1) ? std::ceil(res) : std::floor(res);
};
2021-08-20 16:45:09 +02:00
for (uint i = 0; i < m_pads_internal.size(); i++)
2018-01-13 00:55:51 +01:00
{
2021-08-20 16:45:09 +02:00
auto& pad = m_pads_internal[i];
2021-08-09 13:57:06 +02:00
2018-01-13 00:55:51 +01:00
if (last_connection_status[i] == false)
{
2022-08-13 09:56:04 +02:00
ensure(m_bindings[i].pad);
m_bindings[i].pad->m_port_status |= CELL_PAD_STATUS_CONNECTED;
m_bindings[i].pad->m_port_status |= CELL_PAD_STATUS_ASSIGN_CHANGES;
2018-01-13 00:55:51 +01:00
last_connection_status[i] = true;
connected_devices++;
2018-01-13 00:55:51 +01:00
}
else
{
if (update_sticks)
2018-12-29 15:56:18 +01:00
{
for (usz j = 0; j < pad.m_sticks.size(); j++)
2018-12-29 15:56:18 +01:00
{
const f32 stick_lerp_factor = (j < 2) ? m_l_stick_lerp_factor : m_r_stick_lerp_factor;
// we already applied the following values on keypress if we used factor 1
if (stick_lerp_factor < 1.0f)
{
AnalogStick& stick = pad.m_sticks[j];
const f32 v0 = static_cast<f32>(stick.m_value);
const f32 v1 = static_cast<f32>(stick.m_stick_val);
const f32 res = get_lerped(v0, v1, stick_lerp_factor);
2018-12-29 15:56:18 +01:00
stick.m_value = static_cast<u16>(res);
2018-12-29 15:56:18 +01:00
}
}
}
2018-12-29 15:56:18 +01:00
if (update_buttons)
{
2023-06-13 01:19:32 +02:00
for (Button& button : pad.m_buttons)
{
if (button.m_analog)
{
// we already applied the following values on keypress if we used factor 1
if (m_analog_lerp_factor < 1.0f)
{
const f32 v0 = static_cast<f32>(button.m_value);
const f32 v1 = static_cast<f32>(button.m_actual_value);
const f32 res = get_lerped(v0, v1, m_analog_lerp_factor);
button.m_value = static_cast<u16>(res);
button.m_pressed = button.m_value > 0;
}
}
else if (button.m_trigger)
{
// we already applied the following values on keypress if we used factor 1
if (m_trigger_lerp_factor < 1.0f)
{
const f32 v0 = static_cast<f32>(button.m_value);
const f32 v1 = static_cast<f32>(button.m_actual_value);
const f32 res = get_lerped(v0, v1, m_trigger_lerp_factor);
button.m_value = static_cast<u16>(res);
button.m_pressed = button.m_value > 0;
}
}
}
2018-12-29 15:56:18 +01:00
}
}
2018-01-13 00:55:51 +01:00
}
2019-12-29 09:33:15 +02:00
2021-08-20 16:45:09 +02:00
if (m_mouse_wheel_used)
2021-04-19 23:09:25 +02:00
{
2021-08-20 16:45:09 +02:00
// Releases the wheel buttons 0,1 sec after they've been triggered
// Next activation is set to distant future to avoid activating this on every proc
const auto update_threshold = now - std::chrono::milliseconds(100);
const auto distant_future = now + std::chrono::hours(24);
if (update_threshold >= m_last_wheel_move_up)
{
Key(mouse::wheel_up, false);
m_last_wheel_move_up = distant_future;
}
if (update_threshold >= m_last_wheel_move_down)
{
Key(mouse::wheel_down, false);
m_last_wheel_move_down = distant_future;
}
if (update_threshold >= m_last_wheel_move_left)
{
Key(mouse::wheel_left, false);
m_last_wheel_move_left = distant_future;
}
if (update_threshold >= m_last_wheel_move_right)
{
Key(mouse::wheel_right, false);
m_last_wheel_move_right = distant_future;
}
2021-04-19 23:09:25 +02:00
}
2021-08-20 16:45:09 +02:00
for (uint i = 0; i < m_bindings.size(); i++)
2019-12-29 09:33:15 +02:00
{
2022-08-13 09:56:04 +02:00
auto& pad = m_bindings[i].pad;
ensure(pad);
const cfg_pad* cfg = &m_pad_configs[pad->m_player_id];
ensure(cfg);
const Pad& pad_internal = m_pads_internal[i];
// Normalize and apply pad squircling
// Copy sticks first. We don't want to modify the raw internal values
std::array<u16, 4> squircled_sticks =
{
pad_internal.m_sticks[0].m_value,
pad_internal.m_sticks[1].m_value,
pad_internal.m_sticks[2].m_value,
pad_internal.m_sticks[3].m_value
};
// Apply squircling
if (cfg->lpadsquircling != 0)
{
u16& lx = squircled_sticks[0];
u16& ly = squircled_sticks[1];
ConvertToSquirclePoint(lx, ly, cfg->lpadsquircling);
}
if (cfg->rpadsquircling != 0)
{
u16& rx = squircled_sticks[2];
u16& ry = squircled_sticks[3];
ConvertToSquirclePoint(rx, ry, cfg->rpadsquircling);
}
for (usz j = 0; j < pad->m_buttons.size(); j++)
{
const Button& btn_internal = pad_internal.m_buttons[j];
Button& btn = pad->m_buttons[j];
btn.m_value = btn_internal.m_value;
btn.m_pressed = btn_internal.m_pressed;
}
for (usz j = 0; j < pad->m_sticks.size(); j++)
{
pad->m_sticks[j].m_value = squircled_sticks[j];
}
2019-12-29 09:33:15 +02:00
}
2016-02-02 00:51:09 +03:00
}