Repository navigation
Expand file tree
/
Copy pathForceFeedbackDevice.cpp
More file actions
201 lines (161 loc) · 7.35 KB
/
Copy pathForceFeedbackDevice.cpp
File metadata and controls
201 lines (161 loc) · 7.35 KB
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
/***************************************************************************************************
* Xidi
* DirectInput interface for XInput controllers.
***************************************************************************************************
* Authored by Samuel Grossman
* Copyright (c) 2016-2026
***********************************************************************************************//**
* @file ForceFeedbackBuffer.cpp
* Implementation of functionality related to emulating force feedback systems on physical
* controller devices.
**************************************************************************************************/
#include "ForceFeedbackDevice.h"
#include <memory>
#include <mutex>
#include "ForceFeedbackEffect.h"
#include "ForceFeedbackTypes.h"
#include "ImportApiWinMM.h"
namespace Xidi
{
namespace Controller
{
namespace ForceFeedback
{
/// Computes the relative timestamp that corresponds to a given base and optional provided
/// timestamp value.
/// @param [in] timestampBase Fixed baseline timestamp.
/// @param [in] timestamp Optional absolute timestamp value, which if absent results in the
/// current system time being used.
/// @return Corresponding relative timestamp.
static inline TEffectTimeMs RelativeTimestamp(
TEffectTimeMs timestampBase, std::optional<TEffectTimeMs> timestamp)
{
return ((true == timestamp.has_value()) ? timestamp.value()
: ImportApiWinMM::timeGetTime()) -
timestampBase;
}
Device::Device(void) : Device(ImportApiWinMM::timeGetTime()) {}
Device::Device(TEffectTimeMs timestampBase)
: mutex(),
readyEffects(),
playingEffects(),
stateEffectsAreMuted(),
stateEffectsArePaused(),
timestampBase(timestampBase),
timestampRelativeLastPlay()
{}
bool Device::AddOrUpdateEffect(const Effect& effect)
{
std::unique_lock lock(mutex);
auto playingEffectIter = playingEffects.find(effect.Identifier());
if (playingEffects.end() != playingEffectIter)
return playingEffectIter->second.effect->SyncParametersFrom(effect);
auto readyEffectIter = readyEffects.find(effect.Identifier());
if (readyEffects.end() != readyEffectIter)
return readyEffectIter->second.effect->SyncParametersFrom(effect);
if ((playingEffects.size() + readyEffects.size()) >= kEffectMaxCount) return false;
readyEffects.emplace(std::make_pair(effect.Identifier(), effect.Clone()));
return true;
}
bool Device::IsEffectPlaying(TEffectIdentifier id)
{
std::shared_lock lock(mutex);
auto playingEffectIter = playingEffects.find(id);
if (playingEffects.end() == playingEffectIter) return false;
// This last check filters out effects that are pending playback but have not yet officially
// started due to a start delay.
return (timestampRelativeLastPlay >= playingEffectIter->second.startTime);
}
TOrderedMagnitudeComponents Device::PlayEffects(std::optional<TEffectTimeMs> timestamp)
{
std::unique_lock lock(mutex);
const TEffectTimeMs relativeTimestampPlayback = RelativeTimestamp(timestampBase, timestamp);
if (true == stateEffectsArePaused)
{
timestampBase += (relativeTimestampPlayback - timestampRelativeLastPlay);
return {};
}
timestampRelativeLastPlay = relativeTimestampPlayback;
TOrderedMagnitudeComponents playbackResult = {};
auto playingEffectIter = playingEffects.begin();
while (playingEffectIter != playingEffects.end())
{
SEffectData& effectData = playingEffectIter->second;
// Effects with start delays would be added to the playing effects data structure with
// start times in the future. This check skips playback of effects that have not
// officially started playing due to a start delay parameter.
if (relativeTimestampPlayback >= effectData.startTime)
{
const TEffectTimeMs effectPlayTime = relativeTimestampPlayback - effectData.startTime;
if (effectPlayTime >= effectData.effect->GetDuration())
{
// An iteration of the effect has finished playing.
// If there are iterations left then repeat the effect, otherwise remove it from
// playback.
if (effectData.numIterationsLeft > 0)
{
effectData.numIterationsLeft -= 1;
effectData.startTime = relativeTimestampPlayback;
if (false == stateEffectsAreMuted)
playbackResult += effectData.effect->ComputeOrderedMagnitudeComponents(0);
}
else
{
// As soon as the playing effect is removed from its container the iterator is
// invalidated. Therefore, move on to the next effect before removing it. This path
// is the only one that bypasses the auto-increment that occurs at the end of a loop
// iteration.
auto finishedEffectIter = playingEffectIter++;
readyEffects.insert(playingEffects.extract(finishedEffectIter));
continue;
}
}
else
{
// Effect is currently playing.
// This is as simple as computing its magnitude components and adding them to the
// result.
if (false == stateEffectsAreMuted)
playbackResult +=
effectData.effect->ComputeOrderedMagnitudeComponents(effectPlayTime);
}
}
++playingEffectIter;
}
return playbackResult;
}
bool Device::StartEffect(
TEffectIdentifier id, unsigned int numIterations, std::optional<TEffectTimeMs> timestamp)
{
if (0 == numIterations) return true;
std::unique_lock lock(mutex);
auto readyEffectIter = readyEffects.find(id);
if (readyEffects.end() == readyEffectIter) return false;
readyEffectIter->second.startTime = RelativeTimestamp(timestampBase, timestamp) +
readyEffectIter->second.effect->GetStartDelay();
readyEffectIter->second.numIterationsLeft = numIterations - 1;
return playingEffects.insert(readyEffects.extract(readyEffectIter)).inserted;
}
void Device::StopAllEffects(void)
{
std::unique_lock lock(mutex);
while (false == playingEffects.empty())
readyEffects.insert(playingEffects.extract(playingEffects.cbegin()));
}
bool Device::StopEffect(TEffectIdentifier id)
{
std::unique_lock lock(mutex);
auto playingEffectIter = playingEffects.find(id);
if (playingEffects.end() == playingEffectIter) return false;
return readyEffects.insert(playingEffects.extract(playingEffectIter)).inserted;
}
bool Device::RemoveEffect(TEffectIdentifier id)
{
std::unique_lock lock(mutex);
if (0 != readyEffects.erase(id)) return true;
if (0 != playingEffects.erase(id)) return true;
return false;
}
} // namespace ForceFeedback
} // namespace Controller
} // namespace Xidi