input: move mouse handler code to cpp

This commit is contained in:
Megamouse 2024-05-20 09:38:24 +02:00
parent 7ffb61f650
commit bf85902485
6 changed files with 174 additions and 158 deletions

View file

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

View file

@ -35,32 +35,6 @@ void fmt_class_string<CellMouseError>::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);

View file

@ -0,0 +1,166 @@
#include "stdafx.h"
#include "MouseHandler.h"
#include "Emu/System.h"
#include "util/serialization.hpp"
#include <algorithm>
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<s8>(std::clamp(x_delta, -127, 128));
new_data.y_axis = static_cast<s8>(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 = {};
}
}
}

View file

@ -1,7 +1,6 @@
#pragma once
#include <list>
#include <algorithm>
#include <vector>
#include "Utilities/StrFmt.h"
#include "Utilities/mutex.h"
@ -128,18 +127,7 @@ protected:
std::vector<Mouse> 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<s8>(std::clamp(x_delta, -127, 128));
new_data.y_axis = static_cast<s8>(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<Mouse>& GetMice() { return m_mice; }

View file

@ -73,6 +73,7 @@
<ClCompile Include="Emu\games_config.cpp" />
<ClCompile Include="Emu\Io\camera_config.cpp" />
<ClCompile Include="Emu\Io\midi_config_types.cpp" />
<ClCompile Include="Emu\Io\MouseHandler.cpp" />
<ClCompile Include="Emu\Io\pad_types.cpp" />
<ClCompile Include="Emu\Io\rb3drums_config.cpp" />
<ClCompile Include="Emu\Io\RB3MidiDrums.cpp" />

View file

@ -1252,6 +1252,7 @@
<ClCompile Include="Emu\Io\mouse_config.cpp">
<Filter>Emu\Io</Filter>
</ClCompile>
<ClCompile Include="Emu\Io\MouseHandler.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypto\aes.h">