1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "InputCommon/ControllerInterface/ForceFeedback/ForceFeedbackDevice.h"
#include <array>
#include <string>
#include "Common/Thread.h"
namespace ciface::ForceFeedback
{
// Template instantiation:
template class ForceFeedbackDevice::TypedForce<DICONSTANTFORCE>;
template class ForceFeedbackDevice::TypedForce<DIRAMPFORCE>;
template class ForceFeedbackDevice::TypedForce<DIPERIODIC>;
struct ForceType
{
GUID guid;
const char* name;
};
static const ForceType force_type_names[] = {
{GUID_ConstantForce, "Constant"}, // DICONSTANTFORCE
{GUID_RampForce, "Ramp"}, // DIRAMPFORCE
{GUID_Square, "Square"}, // DIPERIODIC ...
{GUID_Sine, "Sine"},
{GUID_Triangle, "Triangle"},
{GUID_SawtoothUp, "Sawtooth Up"},
{GUID_SawtoothDown, "Sawtooth Down"},
//{GUID_Spring, "Spring"}, // DICUSTOMFORCE ... < I think
//{GUID_Damper, "Damper"},
//{GUID_Inertia, "Inertia"},
//{GUID_Friction, "Friction"},
};
void ForceFeedbackDevice::DeInitForceFeedback()
{
if (!m_run_thread.TestAndClear())
return;
SignalUpdateThread();
m_update_thread.join();
}
void ForceFeedbackDevice::ThreadFunc()
{
Common::SetCurrentThreadName("ForceFeedback update thread");
while (m_run_thread.IsSet())
{
m_update_event.Wait();
for (auto output : Outputs())
{
auto& force = *static_cast<Force*>(output);
force.UpdateOutput();
}
}
for (auto output : Outputs())
{
auto& force = *static_cast<Force*>(output);
force.Release();
}
}
void ForceFeedbackDevice::SignalUpdateThread()
{
m_update_event.Set();
}
bool ForceFeedbackDevice::InitForceFeedback(const LPDIRECTINPUTDEVICE8 device, int axis_count)
{
if (axis_count == 0)
return false;
// We just use the X axis (for wheel left/right).
// Gamepads seem to not care which axis you use.
// These are temporary for creating the effect:
std::array<DWORD, 1> rgdwAxes = {DIJOFS_X};
std::array<LONG, 1> rglDirection = {-200};
DIEFFECT eff{};
eff.dwSize = sizeof(eff);
eff.dwFlags = DIEFF_CARTESIAN | DIEFF_OBJECTOFFSETS;
eff.dwDuration = DI_SECONDS / 1000 * RUMBLE_LENGTH_MS;
eff.dwSamplePeriod = 0;
eff.dwGain = DI_FFNOMINALMAX;
eff.dwTriggerButton = DIEB_NOTRIGGER;
eff.dwTriggerRepeatInterval = 0;
eff.cAxes = DWORD(rgdwAxes.size());
eff.rgdwAxes = rgdwAxes.data();
eff.rglDirection = rglDirection.data();
eff.dwStartDelay = 0;
// Initialize parameters with zero force (their current state).
DICONSTANTFORCE diCF{};
diCF.lMagnitude = 0;
DIRAMPFORCE diRF{};
diRF.lStart = diRF.lEnd = 0;
DIPERIODIC diPE{};
diPE.dwMagnitude = 0;
diPE.lOffset = 0;
diPE.dwPhase = 0;
diPE.dwPeriod = DI_SECONDS / 1000 * RUMBLE_PERIOD_MS;
for (auto& f : force_type_names)
{
if (f.guid == GUID_ConstantForce)
{
eff.cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
eff.lpvTypeSpecificParams = &diCF;
}
else if (f.guid == GUID_RampForce)
{
eff.cbTypeSpecificParams = sizeof(DIRAMPFORCE);
eff.lpvTypeSpecificParams = &diRF;
}
else
{
// All other forces need periodic parameters:
eff.cbTypeSpecificParams = sizeof(DIPERIODIC);
eff.lpvTypeSpecificParams = &diPE;
}
LPDIRECTINPUTEFFECT pEffect;
if (SUCCEEDED(device->CreateEffect(f.guid, &eff, &pEffect, nullptr)))
{
if (f.guid == GUID_ConstantForce)
AddOutput(new ForceConstant(this, f.name, pEffect, diCF));
else if (f.guid == GUID_RampForce)
AddOutput(new ForceRamp(this, f.name, pEffect, diRF));
else
AddOutput(new ForcePeriodic(this, f.name, pEffect, diPE));
}
}
// Disable autocentering:
if (Outputs().size())
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = DIPROPAUTOCENTER_OFF;
device->SetProperty(DIPROP_AUTOCENTER, &dipdw.diph);
m_run_thread.Set();
m_update_thread = std::thread(&ForceFeedbackDevice::ThreadFunc, this);
}
return true;
}
template <typename P>
void ForceFeedbackDevice::TypedForce<P>::PlayEffect()
{
DIEFFECT eff{};
eff.dwSize = sizeof(eff);
eff.cbTypeSpecificParams = sizeof(m_params);
eff.lpvTypeSpecificParams = &m_params;
m_effect->SetParameters(&eff, DIEP_START | DIEP_TYPESPECIFICPARAMS);
}
template <typename P>
void ForceFeedbackDevice::TypedForce<P>::StopEffect()
{
m_effect->Stop();
}
template <>
bool ForceFeedbackDevice::ForceConstant::UpdateParameters(int magnitude)
{
const auto old_magnitude = m_params.lMagnitude;
m_params.lMagnitude = magnitude;
return old_magnitude != m_params.lMagnitude;
}
template <>
bool ForceFeedbackDevice::ForceRamp::UpdateParameters(int magnitude)
{
const auto old_magnitude = m_params.lStart;
// Having the same "start" and "end" here is a bit odd..
// But ramp forces don't really make sense for our rumble effects anyways..
m_params.lStart = m_params.lEnd = magnitude;
return old_magnitude != m_params.lStart;
}
template <>
bool ForceFeedbackDevice::ForcePeriodic::UpdateParameters(int magnitude)
{
const auto old_magnitude = m_params.dwMagnitude;
m_params.dwMagnitude = magnitude;
return old_magnitude != m_params.dwMagnitude;
}
template <typename P>
ForceFeedbackDevice::TypedForce<P>::TypedForce(ForceFeedbackDevice* parent, const char* name,
LPDIRECTINPUTEFFECT effect, const P& params)
: Force(parent, name, effect), m_params(params)
{
}
template <typename P>
void ForceFeedbackDevice::TypedForce<P>::UpdateEffect(int magnitude)
{
if (UpdateParameters(magnitude))
{
if (magnitude)
PlayEffect();
else
StopEffect();
}
}
std::string ForceFeedbackDevice::Force::GetName() const
{
return m_name;
}
ForceFeedbackDevice::Force::Force(ForceFeedbackDevice* parent, const char* name,
LPDIRECTINPUTEFFECT effect)
: m_effect(effect), m_parent(*parent), m_name(name), m_desired_magnitude()
{
}
void ForceFeedbackDevice::Force::SetState(ControlState state)
{
const auto new_val = int(DI_FFNOMINALMAX * state);
if (m_desired_magnitude.exchange(new_val) != new_val)
m_parent.SignalUpdateThread();
}
void ForceFeedbackDevice::Force::UpdateOutput()
{
UpdateEffect(m_desired_magnitude);
}
void ForceFeedbackDevice::Force::Release()
{
// This isn't in the destructor because it should happen before the device is released.
m_effect->Stop();
m_effect->Unload();
m_effect->Release();
}
} // namespace ciface::ForceFeedback
|