Skip to content

Commit

Permalink
Add poll interval to libScePad
Browse files Browse the repository at this point in the history
  • Loading branch information
roamic committed Oct 19, 2024
1 parent 47ba6c6 commit eb2e0b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ void Emulator::Run(const std::filesystem::path& file) {
std::jthread mainthread =
std::jthread([this](std::stop_token stop_token) { linker->Execute(); });

window->initTimers();
while (window->isOpen()) {
window->waitEvent();
}
Expand Down
26 changes: 24 additions & 2 deletions src/input/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <SDL3/SDL.h>
#include "controller.h"

#include "common/assert.h"
#include "core/libraries/kernel/time_management.h"
#include "core/libraries/pad/pad.h"
#include "input/controller.h"

#include <SDL3/SDL.h>

namespace Input {

Expand Down Expand Up @@ -157,4 +160,23 @@ void GameController::TryOpenSDLController() {
}
}

u32 GameController::Poll() {
if (m_connected) {
auto time = Libraries::Kernel::sceKernelGetProcessTime();
if (m_states_num == 0) {
auto diff = (time - m_last_state.time) / 1000;
if (diff >= 100) {
AddState(GetLastState());
}
} else {
auto index = (m_first_state - 1 + m_states_num) % MAX_STATES;
auto diff = (time - m_states[index].time) / 1000;
if (m_private[index].obtained && diff >= 100) {
AddState(GetLastState());
}
}
}
return 100;
}

} // namespace Input
1 change: 1 addition & 0 deletions src/input/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class GameController {
bool SetVibration(u8 smallMotor, u8 largeMotor);
void SetTouchpadState(int touchIndex, bool touchDown, float x, float y);
void TryOpenSDLController();
u32 Poll();

private:
struct StateInternal {
Expand Down
10 changes: 10 additions & 0 deletions src/sdl_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_properties.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_video.h>
#include "common/assert.h"
#include "common/config.h"
Expand All @@ -20,6 +21,11 @@

namespace Frontend {

static Uint32 SDLCALL PollController(void* userdata, SDL_TimerID timer_id, Uint32 interval) {
auto* controller = reinterpret_cast<Input::GameController*>(userdata);
return controller->Poll();
}

WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_,
std::string_view window_title)
: width{width_}, height{height_}, controller{controller_} {
Expand Down Expand Up @@ -119,6 +125,10 @@ void WindowSDL::waitEvent() {
}
}

void WindowSDL::initTimers() {
SDL_AddTimer(100, &PollController, controller);
}

void WindowSDL::onResize() {
SDL_GetWindowSizeInPixels(window, &width, &height);
ImGui::Core::OnResize();
Expand Down
2 changes: 2 additions & 0 deletions src/sdl_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class WindowSDL {

void waitEvent();

void initTimers();

private:
void onResize();
void onKeyPress(const SDL_Event* event);
Expand Down

0 comments on commit eb2e0b3

Please sign in to comment.