Skip to content

Commit

Permalink
Input: avoid mouse events when unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Apr 19, 2021
1 parent a0d1cf2 commit aa0afc9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
4 changes: 2 additions & 2 deletions rpcs3/Emu/Cell/Modules/cellPad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ error_code cellPadGetData(u32 port_no, vm::ptr<CellPadData> data)
}
else
{
u16 d1Initial = pad->m_digital_1;
u16 d2Initial = pad->m_digital_2;
const u16 d1Initial = pad->m_digital_1;
const u16 d2Initial = pad->m_digital_2;

for (Button& button : pad->m_buttons)
{
Expand Down
42 changes: 21 additions & 21 deletions rpcs3/Emu/Io/pad_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct cfg_input final : cfg::node

struct pad_config final : cfg::node
{
std::string cfg_name = "";
std::string cfg_name{};

cfg::string ls_left{ this, "Left Stick Left", "" };
cfg::string ls_down{ this, "Left Stick Down", "" };
Expand Down Expand Up @@ -69,40 +69,40 @@ struct pad_config final : cfg::node
cfg::string l2{ this, "L2", "" };
cfg::string l3{ this, "L3", "" };

cfg::_int<0, 200> lstickmultiplier{ this, "Left Stick Multiplier", 100 };
cfg::_int<0, 200> rstickmultiplier{ this, "Right Stick Multiplier", 100 };
cfg::_int<0, 1000000> lstickdeadzone{ this, "Left Stick Deadzone", 0 };
cfg::_int<0, 1000000> rstickdeadzone{ this, "Right Stick Deadzone", 0 };
cfg::_int<0, 1000000> ltriggerthreshold{ this, "Left Trigger Threshold", 0 };
cfg::_int<0, 1000000> rtriggerthreshold{ this, "Right Trigger Threshold", 0 };
cfg::_int<0, 1000000> lpadsquircling{ this, "Left Pad Squircling Factor", 0 };
cfg::_int<0, 1000000> rpadsquircling{ this, "Right Pad Squircling Factor", 0 };
cfg::uint<0, 200> lstickmultiplier{ this, "Left Stick Multiplier", 100 };
cfg::uint<0, 200> rstickmultiplier{ this, "Right Stick Multiplier", 100 };
cfg::uint<0, 1000000> lstickdeadzone{ this, "Left Stick Deadzone", 0 };
cfg::uint<0, 1000000> rstickdeadzone{ this, "Right Stick Deadzone", 0 };
cfg::uint<0, 1000000> ltriggerthreshold{ this, "Left Trigger Threshold", 0 };
cfg::uint<0, 1000000> rtriggerthreshold{ this, "Right Trigger Threshold", 0 };
cfg::uint<0, 1000000> lpadsquircling{ this, "Left Pad Squircling Factor", 0 };
cfg::uint<0, 1000000> rpadsquircling{ this, "Right Pad Squircling Factor", 0 };

cfg::uint<0, 255> colorR{ this, "Color Value R", 0 };
cfg::uint<0, 255> colorG{ this, "Color Value G", 0 };
cfg::uint<0, 255> colorB{ this, "Color Value B", 0 };

cfg::_bool led_low_battery_blink{ this, "Blink LED when battery is below 20%", true };
cfg::_bool led_battery_indicator{ this, "Use LED as a battery indicator", false };
cfg::_int<0, 100> led_battery_indicator_brightness{ this, "LED battery indicator brightness", 50 };
cfg::uint<0, 100> led_battery_indicator_brightness{ this, "LED battery indicator brightness", 50 };

cfg::_bool enable_vibration_motor_large{ this, "Enable Large Vibration Motor", true };
cfg::_bool enable_vibration_motor_small{ this, "Enable Small Vibration Motor", true };
cfg::_bool switch_vibration_motors{ this, "Switch Vibration Motors", false };

cfg::_int<0, 255> mouse_deadzone_x{ this, "Mouse Deadzone X Axis", 60 };
cfg::_int<0, 255> mouse_deadzone_y{ this, "Mouse Deadzone Y Axis", 60 };
cfg::_int<0, 3000> mouse_acceleration_x{ this, "Mouse Acceleration X Axis", 200 };
cfg::_int<0, 3000> mouse_acceleration_y{ this, "Mouse Acceleration Y Axis", 250 };
cfg::uint<0, 255> mouse_deadzone_x{ this, "Mouse Deadzone X Axis", 60 };
cfg::uint<0, 255> mouse_deadzone_y{ this, "Mouse Deadzone Y Axis", 60 };
cfg::uint<0, 3000> mouse_acceleration_x{ this, "Mouse Acceleration X Axis", 200 };
cfg::uint<0, 3000> mouse_acceleration_y{ this, "Mouse Acceleration Y Axis", 250 };

cfg::_int<0, 100> l_stick_lerp_factor{ this, "Left Stick Lerp Factor", 100 };
cfg::_int<0, 100> r_stick_lerp_factor{ this, "Right Stick Lerp Factor", 100 };
cfg::_int<0, 100> analog_lerp_factor{ this, "Analog Button Lerp Factor", 100 };
cfg::_int<0, 100> trigger_lerp_factor{ this, "Trigger Lerp Factor", 100 };
cfg::uint<0, 100> l_stick_lerp_factor{ this, "Left Stick Lerp Factor", 100 };
cfg::uint<0, 100> r_stick_lerp_factor{ this, "Right Stick Lerp Factor", 100 };
cfg::uint<0, 100> analog_lerp_factor{ this, "Analog Button Lerp Factor", 100 };
cfg::uint<0, 100> trigger_lerp_factor{ this, "Trigger Lerp Factor", 100 };

cfg::_int<0, 5> device_class_type{ this, "Device Class Type", 0 };
cfg::_int<0, 65535> vendor_id{ this, "Vendor ID", 0 };
cfg::_int<0, 65535> product_id{ this, "Product ID", 0 };
cfg::uint<0, 5> device_class_type{ this, "Device Class Type", 0 };
cfg::uint<0, 65535> vendor_id{ this, "Vendor ID", 0 };
cfg::uint<0, 65535> product_id{ this, "Product ID", 0 };

bool exist() const;
bool load();
Expand Down
66 changes: 42 additions & 24 deletions rpcs3/Input/keyboard_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event)

void keyboard_pad_handler::mouseWheelEvent(QWheelEvent* event)
{
if (!m_mouse_wheel_used)
{
return;
}

const QPoint direction = event->angleDelta();

if (direction.isNull())
Expand Down Expand Up @@ -695,6 +700,7 @@ bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::
return false;

m_mouse_move_used = false;
m_mouse_wheel_used = false;
m_deadzone_x = p_profile->mouse_deadzone_x;
m_deadzone_y = p_profile->mouse_deadzone_y;
m_multi_x = p_profile->mouse_acceleration_x / 100.0;
Expand All @@ -713,6 +719,8 @@ bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::
key = 0;
else if (!m_mouse_move_used && (key == mouse::move_left || key == mouse::move_right || key == mouse::move_up || key == mouse::move_down))
m_mouse_move_used = true;
else if (!m_mouse_wheel_used && (key == mouse::wheel_left || key == mouse::wheel_right || key == mouse::wheel_up || key == mouse::wheel_down))
m_mouse_wheel_used = true;
return key;
};

Expand Down Expand Up @@ -777,16 +785,11 @@ bool keyboard_pad_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::

void keyboard_pad_handler::ThreadProc()
{
static const double mouse_interval = 30.0;
static const double stick_interval = 10.0;
static const double button_interval = 10.0;

const auto now = steady_clock::now();

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;
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;

Expand All @@ -803,26 +806,36 @@ void keyboard_pad_handler::ThreadProc()
m_button_time = now;
}

// 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)
if (m_mouse_move_used)
{
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;
static const double mouse_interval = 30.0;

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)
Expand Down Expand Up @@ -898,6 +911,11 @@ void keyboard_pad_handler::ThreadProc()
}
}

if (!m_mouse_wheel_used)
{
return;
}

// 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);
Expand Down
1 change: 1 addition & 0 deletions rpcs3/Input/keyboard_pad_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class keyboard_pad_handler final : public QObject, public PadHandlerBase
private:
QWindow* m_target = nullptr;
bool m_mouse_move_used = false;
bool m_mouse_wheel_used = false;
bool get_mouse_lock_state() const;
void release_all_keys();

Expand Down

0 comments on commit aa0afc9

Please sign in to comment.