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