Skip to content

Commit 8aae104

Browse files
committed
Fix: making it compatible with xbox 360 controller emulator
1 parent fc342be commit 8aae104

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed

GenericFFBDriver/FFBDriver.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <fstream>
55

66
void LogMessage(const char* msg) {
7-
7+
#ifdef _DEBUG
88
SYSTEMTIME st;
99
GetSystemTime(&st);
1010
char buffer[256];
@@ -20,6 +20,7 @@ void LogMessage(const char* msg) {
2020
outfile.open("J:\\Gamepad\\driverlog.txt", std::ios_base::app);
2121
outfile << buffer;
2222
outfile.close();
23+
#endif
2324
}
2425

2526
FFBDriver::FFBDriver()
@@ -28,6 +29,7 @@ FFBDriver::FFBDriver()
2829

2930
FFBDriver::~FFBDriver()
3031
{
32+
vibration::VibrationController::Reset();
3133
}
3234

3335

@@ -66,7 +68,9 @@ HRESULT STDMETHODCALLTYPE FFBDriver::DeviceID(
6668
}
6769

6870
HRESULT STDMETHODCALLTYPE FFBDriver::GetVersions(LPDIDRIVERVERSIONS lpVersions) {
71+
#ifdef _DEBUG
6972
LogMessage("GetVersions\n");
73+
#endif
7074

7175
lpVersions->dwFFDriverVersion = 0x100;
7276
lpVersions->dwFirmwareRevision = 0x100;
@@ -75,7 +79,9 @@ HRESULT STDMETHODCALLTYPE FFBDriver::GetVersions(LPDIDRIVERVERSIONS lpVersions)
7579
return S_OK;
7680
}
7781
HRESULT STDMETHODCALLTYPE FFBDriver::Escape(THIS_ DWORD, DWORD, LPDIEFFESCAPE) {
82+
#ifdef _DEBUG
7883
LogMessage("Escape!\n");
84+
#endif
7985
return S_OK;
8086
}
8187
HRESULT STDMETHODCALLTYPE FFBDriver::SetGain(
@@ -131,7 +137,9 @@ HRESULT STDMETHODCALLTYPE FFBDriver::SendForceFeedbackCommand(
131137
}
132138

133139
HRESULT STDMETHODCALLTYPE FFBDriver::GetForceFeedbackState(THIS_ DWORD, LPDIDEVICESTATE) {
140+
#ifdef _DEBUG
134141
LogMessage("GetForceFeedbackState!\n");
142+
#endif
135143
return S_OK;
136144
}
137145

@@ -155,18 +163,26 @@ HRESULT STDMETHODCALLTYPE FFBDriver::DownloadEffect(
155163
}
156164

157165
HRESULT STDMETHODCALLTYPE FFBDriver::DestroyEffect(DWORD, DWORD) {
166+
#ifdef _DEBUG
158167
LogMessage("DestroyEffect!\n");
168+
#endif
159169
return S_OK;
160170
}
161171
HRESULT STDMETHODCALLTYPE FFBDriver::StartEffect(DWORD, DWORD, DWORD, DWORD) {
172+
#ifdef _DEBUG
162173
LogMessage("StartEffect!\n");
174+
#endif
163175
return S_OK;
164176
}
165177
HRESULT STDMETHODCALLTYPE FFBDriver::StopEffect(DWORD dwID, DWORD dwEffect) {
178+
#ifdef _DEBUG
166179
LogMessage("StopEffect!\n");
180+
#endif
167181
return S_OK;
168182
}
169183
HRESULT STDMETHODCALLTYPE FFBDriver::GetEffectStatus(DWORD, DWORD, LPDWORD) {
184+
#ifdef _DEBUG
170185
LogMessage("GetEffectStatus!\n");
186+
#endif
171187
return S_OK;
172188
}

GenericFFBDriver/vibration/VibrationController.cpp

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ namespace vibration {
6363
if (thrVibration == NULL) {
6464
quitVibrationThread = false;
6565

66-
for (int k = 0; k < MAX_EFFECTS; k++)
66+
for (int k = 0; k < MAX_EFFECTS; k++) {
6767
VibEffects[k].isActive = FALSE;
68+
VibEffects[k].dwEffectId = -1;
69+
}
6870

6971
thrVibration.reset(new std::thread(VibrationController::VibrationThreadEntryPoint));
7072
}
@@ -171,47 +173,93 @@ namespace vibration {
171173
void VibrationController::StartEffect(DWORD dwEffectID, LPCDIEFFECT peff)
172174
{
173175
mtxSync.lock();
176+
177+
int idx = -1;
178+
// Reusing the same idx if effect was already created
174179
for (int k = 0; k < MAX_EFFECTS; k++) {
175-
if (VibEffects[k].isActive && k < MAX_EFFECTS - 1)
176-
continue;
180+
if (VibEffects[k].dwEffectId == dwEffectID) {
181+
idx = k;
182+
break;
183+
}
184+
}
177185

178-
// Calculating intensity
179-
byte forceX = 0xfe;
180-
byte forceY = 0xfe;
186+
// Find a non-active idx
187+
if (idx < 0) {
188+
for (int k = 0; k < MAX_EFFECTS; k++) {
189+
if (!VibEffects[k].isActive || k == MAX_EFFECTS - 1) {
190+
idx = k;
191+
break;
192+
}
193+
}
194+
}
195+
196+
// Calculating intensity
197+
byte forceX = 0xfe;
198+
byte forceY = 0xfe;
199+
200+
byte magnitude = 0xfe;
201+
if (peff->cbTypeSpecificParams == 4) {
202+
LPDICONSTANTFORCE effParams = (LPDICONSTANTFORCE)peff->lpvTypeSpecificParams;
203+
magnitude = (byte)(round((((double)effParams->lMagnitude) / 10000.0) * 254.0));
204+
}
181205

206+
if (peff->cAxes == 1) {
207+
// If direction is negative, then it is a forceX
208+
// Otherwise it is a forceY
209+
LONG direction = peff->rglDirection[0];
210+
static byte lastForceX = 0;
211+
static byte lastForceY = 0;
212+
213+
forceX = lastForceX;
214+
forceY = lastForceY;
215+
216+
if (direction == -1) {
217+
//forceX = lastForceX = (byte)(round((((double)peff->dwGain) / 10000.0) * 254.0));
218+
forceX = lastForceX = magnitude;
219+
}
220+
else if (direction == 1) {
221+
//forceY = lastForceY = (byte)(round((((double)peff->dwGain) / 10000.0) * 254.0));
222+
forceY = lastForceY = magnitude;
223+
}
224+
225+
}
226+
else {
182227
if (peff->cAxes >= 1) {
183228
LONG fx = peff->rglDirection[0];
184-
if (fx <= 1) fx = peff->dwGain;
229+
//if (fx <= 1) fx = peff->dwGain;
185230

186-
forceX = forceY = (byte)abs(round((((double)fx) / 10000.0) * 254.0));
231+
if (fx > 0)
232+
forceX = forceY = magnitude;
233+
else
234+
forceX = forceY = 0;
187235
}
188236

189237
if (peff->cAxes >= 2) {
190238
LONG fy = peff->rglDirection[1];
191-
if (fy <= 1) fy = peff->dwGain;
239+
//if (fy <= 1) fy = peff->dwGain;
192240

193-
forceY = (byte)abs(round((((double)fy) / 10000.0) * 254.0));
241+
if (fy > 0)
242+
forceY = magnitude;
243+
else
244+
forceY = 0;
194245
}
246+
}
195247

196248

197-
DWORD frame = GetTickCount();
198-
199-
VibEffects[k].forceX = forceX;
200-
VibEffects[k].forceY = forceY;
249+
DWORD frame = GetTickCount();
201250

202-
VibEffects[k].dwEffectId = dwEffectID;
203-
VibEffects[k].dwStartFrame = frame + (peff->dwStartDelay / 1000);
204-
VibEffects[k].dwStopFrame =
205-
peff->dwDuration == INFINITE ? INFINITE :
206-
VibEffects[k].dwStartFrame + (peff->dwDuration / 1000);
207-
VibEffects[k].isActive = TRUE;
208-
VibEffects[k].started = FALSE;
251+
VibEffects[idx].forceX = forceX;
252+
VibEffects[idx].forceY = forceY;
209253

210-
break;
211-
}
254+
VibEffects[idx].dwEffectId = dwEffectID;
255+
VibEffects[idx].dwStartFrame = frame + (peff->dwStartDelay / 1000);
256+
VibEffects[idx].dwStopFrame =
257+
peff->dwDuration == INFINITE ? INFINITE :
258+
VibEffects[idx].dwStartFrame + (peff->dwDuration / 1000);
259+
VibEffects[idx].isActive = TRUE;
260+
VibEffects[idx].started = FALSE;
212261

213262
mtxSync.unlock();
214-
215263
StartVibrationThread();
216264
}
217265

0 commit comments

Comments
 (0)