Skip to content

Commit

Permalink
Merge pull request flyinghead#272 from inada-s/revert-ramp-analog
Browse files Browse the repository at this point in the history
Revert ramp analog
  • Loading branch information
inada-s authored Jan 8, 2025
2 parents 9e826e4 + 90c4ccc commit 9f36a84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 104 deletions.
101 changes: 16 additions & 85 deletions core/input/gamepad_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,24 @@ bool GamepadDevice::find_mapping(int system /* = settings.platform.system */)
return false;
}

int GamepadDevice::GetGamepadCount() {
Lock _(_gamepads_mutex);
return _gamepads.size();
int GamepadDevice::GetGamepadCount()
{
_gamepads_mutex.lock();
int count = _gamepads.size();
_gamepads_mutex.unlock();
return count;
}

std::shared_ptr<GamepadDevice> GamepadDevice::GetGamepad(int index)
{
Lock _(_gamepads_mutex);
_gamepads_mutex.lock();
std::shared_ptr<GamepadDevice> dev;
if (index >= 0 && index < (int)_gamepads.size())
return _gamepads[index];
dev = _gamepads[index];
else
return nullptr;
dev = NULL;
_gamepads_mutex.unlock();
return dev;
}

void GamepadDevice::save_mapping(int system /* = settings.platform.system */)
Expand Down Expand Up @@ -553,19 +559,21 @@ void GamepadDevice::Register(const std::shared_ptr<GamepadDevice>& gamepad)
setbuf(record_input, NULL);
}
#endif
Lock _(_gamepads_mutex);
_gamepads_mutex.lock();
_gamepads.push_back(gamepad);
_gamepads_mutex.unlock();
MapleConfigMap::UpdateVibration = updateVibration;
}

void GamepadDevice::Unregister(const std::shared_ptr<GamepadDevice>& gamepad)
{
Lock _(_gamepads_mutex);
_gamepads_mutex.lock();
for (auto it = _gamepads.begin(); it != _gamepads.end(); it++)
if (*it == gamepad) {
_gamepads.erase(it);
break;
}
_gamepads_mutex.unlock();
}

void GamepadDevice::SaveMaplePorts()
Expand All @@ -578,83 +586,6 @@ void GamepadDevice::SaveMaplePorts()
}
}

s16 (&GamepadDevice::getTargetArray(DigAnalog axis))[4]
{
switch (axis)
{
case DIGANA_LEFT:
case DIGANA_RIGHT:
return joyx;
case DIGANA_UP:;
case DIGANA_DOWN:
return joyy;
case DIGANA2_LEFT:
case DIGANA2_RIGHT:
return joyrx;
case DIGANA2_UP:
case DIGANA2_DOWN:
return joyry;
case DIGANA3_LEFT:
case DIGANA3_RIGHT:
return joy3x;
case DIGANA3_UP:
case DIGANA3_DOWN:
return joy3y;
default:
die("unknown axis");
}
}

void GamepadDevice::rampAnalog()
{
Lock _(rampMutex);
if (lastAnalogUpdate == 0)
// also used as a flag that no analog ramping is needed on this device (yet)
return;

const u64 now = getTimeMs();
const int delta = std::round(static_cast<float>(now - lastAnalogUpdate) * AnalogRamp);
lastAnalogUpdate = now;
for (unsigned port = 0; port < std::size(digitalToAnalogState); port++)
{
for (int axis = 0; axis < 12; axis += 2) // 3 sticks with 2 axes each
{
DigAnalog negDir = static_cast<DigAnalog>(1 << axis);
if ((rampAnalogState[port] & negDir) == 0)
// axis not active
continue;
DigAnalog posDir = static_cast<DigAnalog>(1 << (axis + 1));
const int socd = digitalToAnalogState[port] & (negDir | posDir);
s16& axisValue = getTargetArray(negDir)[port];
if (socd != 0 && socd != (negDir | posDir))
{
// One axis is pressed => ramp up
if (socd == posDir)
axisValue = std::min(32767, axisValue + delta);
else
axisValue = std::max(-32768, axisValue - delta);
}
else
{
// No axis is pressed (or both) => ramp down
if (axisValue > 0)
axisValue = std::max(0, axisValue - delta);
else if (axisValue < 0)
axisValue = std::min(0, axisValue + delta);
else
rampAnalogState[port] &= ~negDir;
}
}
}
}

void GamepadDevice::RampAnalog()
{
Lock _(_gamepads_mutex);
for (auto& gamepad : _gamepads)
gamepad->rampAnalog();
}

#ifdef TEST_AUTOMATION
#include "cfg/option.h"
static bool replay_inited;
Expand Down
24 changes: 9 additions & 15 deletions core/input/gamepad_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#pragma once
#include "types.h"
#include "mapping.h"
#include "stdclass.h"

#include <map>
#include <memory>
Expand Down Expand Up @@ -97,7 +96,6 @@ class GamepadDevice
static int GetGamepadCount();
static std::shared_ptr<GamepadDevice> GetGamepad(int index);
static void SaveMaplePorts();
static void RampAnalog();

template<typename T>
static std::shared_ptr<T> GetGamepad()
Expand Down Expand Up @@ -168,19 +166,20 @@ class GamepadDevice
{
if (port < 0)
return;
Lock _(rampMutex);
DigAnalog axis = key == DcNegDir ? NegDir : PosDir;
if (pressed)
digitalToAnalogState[port] |= axis;
else
digitalToAnalogState[port] &= ~axis;
rampAnalogState[port] |= NegDir;
if (lastAnalogUpdate == 0)
lastAnalogUpdate = getTimeMs();
}
const u32 socd = digitalToAnalogState[port] & (NegDir | PosDir);
if (socd == 0 || socd == (NegDir | PosDir))
joystick = 0;
else if (socd == NegDir)
joystick = -32768;
else
joystick = 32767;

s16 (&getTargetArray(DigAnalog axis))[4];
void rampAnalog();
}

std::string _api_name;
int _maple_port;
Expand All @@ -193,12 +192,7 @@ class GamepadDevice
std::map<DreamcastKey, int> lastAxisValue[4];
bool perGameMapping = false;
bool instanceMapping = false;

u64 lastAnalogUpdate = 0;
u32 rampAnalogState[4] {};
static constexpr float AnalogRamp = 32767.f / 100.f; // 100 ms ramp time
std::mutex rampMutex;


static std::vector<std::shared_ptr<GamepadDevice>> _gamepads;
static std::mutex _gamepads_mutex;

Expand Down
8 changes: 4 additions & 4 deletions core/oslib/oslib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <shlobj.h>
#endif
#include "profiler/fc_profiler.h"
#include "input/gamepad_device.h"

namespace hostfs
{
Expand Down Expand Up @@ -379,11 +378,12 @@ void os_UpdateInputState()
{
FC_PROFILE_SCOPE;

GamepadDevice::RampAnalog();
#if defined(USE_SDL)
input_sdl_handle();
#elif defined(USE_EVDEV)
input_evdev_handle();
#else
#if defined(USE_EVDEV)
input_evdev_handle();
#endif
#endif
}

Expand Down

0 comments on commit 9f36a84

Please sign in to comment.