-
-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f9caa1
commit da6fa8c
Showing
8 changed files
with
201 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "core/libraries/kernel/time_management.h" | ||
#include "input/mouse.h" | ||
|
||
namespace Input { | ||
|
||
GameMouse::GameMouse() { | ||
m_states_num = 0; | ||
m_last_state = MouseState(); | ||
} | ||
|
||
int GameMouse::ReadStates(MouseState* states, int states_num, bool* isConnected) { | ||
std::scoped_lock lock{m_mutex}; | ||
|
||
*isConnected = m_connected; | ||
|
||
int ret_num = 0; | ||
|
||
if (m_connected) { | ||
if (m_states_num == 0) { | ||
ret_num = 1; | ||
states[0] = m_last_state; | ||
} else { | ||
for (uint32_t i = 0; i < m_states_num; i++) { | ||
if (ret_num >= states_num) { | ||
break; | ||
} | ||
auto index = (m_first_state + i) % MAX_MOUSE_STATES; | ||
if (!m_private[index].obtained) { | ||
m_private[index].obtained = true; | ||
|
||
states[ret_num++] = m_states[index]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ret_num; | ||
} | ||
|
||
MouseState GameMouse::GetLastState() const { | ||
if (m_states_num == 0) { | ||
return m_last_state; | ||
} | ||
|
||
auto last = (m_first_state + m_states_num - 1) % MAX_MOUSE_STATES; | ||
|
||
return m_states[last]; | ||
} | ||
|
||
void GameMouse::AddState(const MouseState& state) { | ||
if (m_states_num >= MAX_MOUSE_STATES) { | ||
m_states_num = MAX_MOUSE_STATES - 1; | ||
m_first_state = (m_first_state + 1) % MAX_MOUSE_STATES; | ||
} | ||
|
||
auto index = (m_first_state + m_states_num) % MAX_MOUSE_STATES; | ||
|
||
m_states[index] = state; | ||
m_last_state = state; | ||
m_private[index].obtained = false; | ||
m_states_num++; | ||
} | ||
|
||
void GameMouse::CheckButton(int id, u32 button, bool isPressed) { | ||
std::scoped_lock lock{m_mutex}; | ||
auto state = GetLastState(); | ||
state.time = Libraries::Kernel::sceKernelGetProcessTime(); | ||
if (isPressed) { | ||
state.buttonsState |= button; | ||
} else { | ||
state.buttonsState &= ~button; | ||
} | ||
|
||
AddState(state); | ||
} | ||
|
||
}; // namespace Input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
#include <mutex> | ||
#include "common/types.h" | ||
|
||
namespace Input { | ||
|
||
struct MouseState { | ||
u32 buttonsState = 0; | ||
u64 time = 0; | ||
}; | ||
|
||
constexpr u32 MAX_MOUSE_STATES = 64; | ||
|
||
class GameMouse { | ||
public: | ||
GameMouse(); | ||
virtual ~GameMouse() = default; | ||
|
||
int ReadStates(MouseState* states, int states_num, bool* isConnected); | ||
MouseState GetLastState() const; | ||
void CheckButton(int id, u32 button, bool isPressed); | ||
void AddState(const MouseState& state); | ||
|
||
private: | ||
struct StateInternal { | ||
bool obtained = false; | ||
}; | ||
|
||
std::mutex m_mutex; | ||
bool m_connected = true; | ||
MouseState m_last_state; | ||
int m_connected_count = 0; | ||
u32 m_states_num = 0; | ||
u32 m_first_state = 0; | ||
std::array<MouseState, MAX_MOUSE_STATES> m_states; | ||
std::array<StateInternal, MAX_MOUSE_STATES> m_private; | ||
|
||
}; | ||
|
||
} // namespace Input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters