diff --git a/rpcs3/Emu/CMakeLists.txt b/rpcs3/Emu/CMakeLists.txt index 5613e49a6e..8133c5bf67 100644 --- a/rpcs3/Emu/CMakeLists.txt +++ b/rpcs3/Emu/CMakeLists.txt @@ -405,6 +405,7 @@ target_sources(rpcs3_emu PRIVATE Io/KeyboardHandler.cpp Io/midi_config_types.cpp Io/mouse_config.cpp + Io/MouseHandler.cpp Io/pad_config.cpp Io/pad_config_types.cpp Io/pad_types.cpp diff --git a/rpcs3/Emu/Cell/Modules/cellMouse.cpp b/rpcs3/Emu/Cell/Modules/cellMouse.cpp index b4919f6dfa..38df718fa6 100644 --- a/rpcs3/Emu/Cell/Modules/cellMouse.cpp +++ b/rpcs3/Emu/Cell/Modules/cellMouse.cpp @@ -35,32 +35,6 @@ void fmt_class_string::format(std::string& out, u64 arg) }); } -MouseHandlerBase::MouseHandlerBase(utils::serial* ar) -{ - if (!ar) - { - return; - } - - (*ar)(m_info.max_connect); - - if (m_info.max_connect) - { - Emu.PostponeInitCode([this]() - { - Init(m_info.max_connect); - auto lk = init.init(); - }); - } -} - -void MouseHandlerBase::save(utils::serial& ar) -{ - const auto inited = init.access(); - - ar(inited ? m_info.max_connect : 0); -} - error_code cellMouseInit(ppu_thread& ppu, u32 max_connect) { sys_io.notice("cellMouseInit(max_connect=%d)", max_connect); diff --git a/rpcs3/Emu/Io/MouseHandler.cpp b/rpcs3/Emu/Io/MouseHandler.cpp new file mode 100644 index 0000000000..5cc83cd5ab --- /dev/null +++ b/rpcs3/Emu/Io/MouseHandler.cpp @@ -0,0 +1,166 @@ +#include "stdafx.h" +#include "MouseHandler.h" +#include "Emu/System.h" +#include "util/serialization.hpp" + +#include + +MouseHandlerBase::MouseHandlerBase(utils::serial* ar) +{ + if (!ar) + { + return; + } + + (*ar)(m_info.max_connect); + + if (m_info.max_connect) + { + Emu.PostponeInitCode([this]() + { + Init(m_info.max_connect); + auto lk = init.init(); + }); + } +} + +void MouseHandlerBase::save(utils::serial& ar) +{ + const auto inited = init.access(); + + ar(inited ? m_info.max_connect : 0); +} + +bool MouseHandlerBase::is_time_for_update(double elapsed_time) +{ + steady_clock::time_point now = steady_clock::now(); + double elapsed = (now - last_update).count() / 1000'000.; + + if (elapsed > elapsed_time) + { + last_update = now; + return true; + } + return false; +} + +void MouseHandlerBase::Button(u32 index, u8 button, bool pressed) +{ + std::lock_guard lock(mutex); + + if (index < m_mice.size()) + { + if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) + { + return; + } + + Mouse& mouse = m_mice[index]; + MouseDataList& datalist = GetDataList(index); + + if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) + { + datalist.pop_front(); + } + + if (pressed) + mouse.buttons |= button; + else + mouse.buttons &= ~button; + + MouseData new_data; + new_data.update = CELL_MOUSE_DATA_UPDATE; + new_data.buttons = mouse.buttons; + + datalist.push_back(new_data); + } +} + +void MouseHandlerBase::Scroll(u32 index, s32 rotation) +{ + std::lock_guard lock(mutex); + + if (index < m_mice.size()) + { + if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) + { + return; + } + + Mouse& mouse = m_mice[index]; + MouseDataList& datalist = GetDataList(index); + + if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) + { + datalist.pop_front(); + } + + MouseData new_data; + new_data.update = CELL_MOUSE_DATA_UPDATE; + new_data.wheel = std::clamp(rotation / 120, -128, 127); // 120=event.GetWheelDelta() + new_data.buttons = mouse.buttons; + + datalist.push_back(new_data); + } +} + +void MouseHandlerBase::Move(u32 index, s32 x_pos_new, s32 y_pos_new, s32 x_max, s32 y_max, const bool is_relative, s32 x_delta, s32 y_delta) +{ + std::lock_guard lock(mutex); + + if (index < m_mice.size()) + { + if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) + { + return; + } + + Mouse& mouse = m_mice[index]; + MouseDataList& datalist = GetDataList(index); + + if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) + { + datalist.pop_front(); + } + + MouseData new_data; + new_data.update = CELL_MOUSE_DATA_UPDATE; + new_data.buttons = mouse.buttons; + + if (!is_relative) + { + // The PS3 expects relative mouse movement, so we have to calculate it with the last absolute position. + x_delta = x_pos_new - mouse.x_pos; + y_delta = y_pos_new - mouse.y_pos; + } + + new_data.x_axis = static_cast(std::clamp(x_delta, -127, 128)); + new_data.y_axis = static_cast(std::clamp(y_delta, -127, 128)); + + mouse.x_max = x_max; + mouse.y_max = y_max; + mouse.x_pos = x_pos_new; + mouse.y_pos = y_pos_new; + + //CellMouseRawData& rawdata = GetRawData(p); + //rawdata.data[rawdata.len % CELL_MOUSE_MAX_CODES] = 0; // (TODO) + //rawdata.len++; + + datalist.push_back(new_data); + } +} + +void MouseHandlerBase::SetIntercepted(bool intercepted) +{ + std::lock_guard lock(mutex); + + m_info.info = intercepted ? CELL_MOUSE_INFO_INTERCEPTED : 0; + + if (intercepted) + { + for (Mouse& mouse : m_mice) + { + mouse = {}; + } + } +} diff --git a/rpcs3/Emu/Io/MouseHandler.h b/rpcs3/Emu/Io/MouseHandler.h index 7d9b1eaac9..b30ebab56d 100644 --- a/rpcs3/Emu/Io/MouseHandler.h +++ b/rpcs3/Emu/Io/MouseHandler.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "Utilities/StrFmt.h" #include "Utilities/mutex.h" @@ -128,18 +127,7 @@ protected: std::vector m_mice; steady_clock::time_point last_update{}; - bool is_time_for_update(double elapsed_time = 10.0) // 4-10 ms, let's use 10 for now - { - steady_clock::time_point now = steady_clock::now(); - double elapsed = (now - last_update).count() / 1000'000.; - - if (elapsed > elapsed_time) - { - last_update = now; - return true; - } - return false; - } + bool is_time_for_update(double elapsed_time = 10.0); // 4-10 ms, let's use 10 for now public: shared_mutex mutex; @@ -155,126 +143,11 @@ public: MouseHandlerBase(utils::serial& ar) : MouseHandlerBase(&ar) {} void save(utils::serial& ar); - void Button(u32 index, u8 button, bool pressed) - { - std::lock_guard lock(mutex); + void Button(u32 index, u8 button, bool pressed); + void Scroll(u32 index, s32 rotation); + void Move(u32 index, s32 x_pos_new, s32 y_pos_new, s32 x_max, s32 y_max, const bool is_relative = false, s32 x_delta = 0, s32 y_delta = 0); - if (index < m_mice.size()) - { - if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) - { - return; - } - - Mouse& mouse = m_mice[index]; - MouseDataList& datalist = GetDataList(index); - - if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) - { - datalist.pop_front(); - } - - if (pressed) - mouse.buttons |= button; - else - mouse.buttons &= ~button; - - MouseData new_data; - new_data.update = CELL_MOUSE_DATA_UPDATE; - new_data.buttons = mouse.buttons; - - datalist.push_back(new_data); - } - } - - void Scroll(u32 index, s32 rotation) - { - std::lock_guard lock(mutex); - - if (index < m_mice.size()) - { - if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) - { - return; - } - - Mouse& mouse = m_mice[index]; - MouseDataList& datalist = GetDataList(index); - - if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) - { - datalist.pop_front(); - } - - MouseData new_data; - new_data.update = CELL_MOUSE_DATA_UPDATE; - new_data.wheel = std::clamp(rotation / 120, -128, 127); // 120=event.GetWheelDelta() - new_data.buttons = mouse.buttons; - - datalist.push_back(new_data); - } - } - - void Move(u32 index, s32 x_pos_new, s32 y_pos_new, s32 x_max, s32 y_max, const bool is_relative = false, s32 x_delta = 0, s32 y_delta = 0) - { - std::lock_guard lock(mutex); - - if (index < m_mice.size()) - { - if (m_info.status[index] != CELL_MOUSE_STATUS_CONNECTED) - { - return; - } - - Mouse& mouse = m_mice[index]; - MouseDataList& datalist = GetDataList(index); - - if (datalist.size() > MOUSE_MAX_DATA_LIST_NUM) - { - datalist.pop_front(); - } - - MouseData new_data; - new_data.update = CELL_MOUSE_DATA_UPDATE; - new_data.buttons = mouse.buttons; - - if (!is_relative) - { - // The PS3 expects relative mouse movement, so we have to calculate it with the last absolute position. - x_delta = x_pos_new - mouse.x_pos; - y_delta = y_pos_new - mouse.y_pos; - } - - new_data.x_axis = static_cast(std::clamp(x_delta, -127, 128)); - new_data.y_axis = static_cast(std::clamp(y_delta, -127, 128)); - - mouse.x_max = x_max; - mouse.y_max = y_max; - mouse.x_pos = x_pos_new; - mouse.y_pos = y_pos_new; - - //CellMouseRawData& rawdata = GetRawData(p); - //rawdata.data[rawdata.len % CELL_MOUSE_MAX_CODES] = 0; // (TODO) - //rawdata.len++; - - datalist.push_back(new_data); - } - } - - void SetIntercepted(bool intercepted) - { - std::lock_guard lock(mutex); - - m_info.info = intercepted ? CELL_MOUSE_INFO_INTERCEPTED : 0; - - if (intercepted) - { - for (Mouse& mouse : m_mice) - { - mouse = {}; - } - } - } + void SetIntercepted(bool intercepted); MouseInfo& GetInfo() { return m_info; } std::vector& GetMice() { return m_mice; } diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index cdb7b8c134..748ffdcb2c 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -73,6 +73,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 3131397bea..d1b893a243 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -1252,6 +1252,7 @@ Emu\Io +