Repository navigation
Expand file tree
/
Copy pathVirtualDirectInputEffect.cpp
More file actions
1181 lines (1008 loc) · 47.5 KB
/
Copy pathVirtualDirectInputEffect.cpp
File metadata and controls
1181 lines (1008 loc) · 47.5 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************************************
* Xidi
* DirectInput interface for XInput controllers.
***************************************************************************************************
* Authored by Samuel Grossman
* Copyright (c) 2016-2026
***********************************************************************************************//**
* @file VirtualDirectInputEffect.cpp
* Implementation of an IDirectInputEffect interface wrapper around force feedback effects that
* are associated with virtual controllers.
**************************************************************************************************/
#include "VirtualDirectInputEffect.h"
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <Infra/Core/Message.h>
#include <Infra/Core/TemporaryBuffer.h>
#include "ForceFeedbackDevice.h"
#include "ForceFeedbackEffect.h"
#include "ForceFeedbackParameters.h"
#include "ForceFeedbackTypes.h"
#include "VirtualDirectInputDevice.h"
/// Logs a DirectInput interface method invocation and returns.
#define LOG_INVOCATION_AND_RETURN(result, severity) \
do \
{ \
const HRESULT hresult = (result); \
Infra::Message::OutputFormatted( \
severity, \
L"Invoked %s on force feedback effect with identifier %llu associated with Xidi virtual controller %u, result = 0x%08x.", \
__FUNCTIONW__ L"()", \
(unsigned long long)UnderlyingEffect().Identifier(), \
(1 + associatedDevice.GetVirtualController().GetIdentifier()), \
hresult); \
return hresult; \
} \
while (false)
namespace Xidi
{
/// Severity to use for dumping the contents of structures to the log.
static constexpr Infra::Message::ESeverity kDumpSeverity = Infra::Message::ESeverity::Debug;
/// Internal implementation of downloading a force feedback effect to a force feedback device.
/// Called by interface methods that require this functionality.
/// @param [in] effect Effect to be downloaded.
/// @param [in] device Target device.
/// @return DirectInput return code indicating the result of the operation.
static HRESULT DownloadEffectToDevice(
const Controller::ForceFeedback::Effect& effect, Controller::ForceFeedback::Device& device)
{
if (false == effect.IsCompletelyDefined()) return DIERR_INCOMPLETEEFFECT;
if (false == device.AddOrUpdateEffect(effect)) return DIERR_DEVICEFULL;
return DI_OK;
}
/// Parses the specified flags, which would normally be passed to SetParameters, and extracts
/// individual strings for each flag that is present.
/// @param [in] dwFlags Flags to parse.
/// @return String representation of the flags that are set.
static Infra::TemporaryString ParameterTopLevelFlagsToString(DWORD dwFlags)
{
constexpr std::wstring_view kFlagSeparator = L" | ";
Infra::TemporaryString flagsString;
if (0 != (dwFlags & DIEP_NODOWNLOAD)) flagsString << L"DIEP_NODOWNLOAD" << kFlagSeparator;
if (0 != (dwFlags & DIEP_NORESTART)) flagsString << L"DIEP_NORESTART" << kFlagSeparator;
if (0 != (dwFlags & DIEP_START)) flagsString << L"DIEP_START" << kFlagSeparator;
if (DIEP_ALLPARAMS == (dwFlags & DIEP_ALLPARAMS))
{
flagsString << L"DIEP_ALLPARAMS" << kFlagSeparator;
}
else if (DIEP_ALLPARAMS_DX5 == (dwFlags & DIEP_ALLPARAMS_DX5))
{
flagsString << L"DIEP_ALLPARAMS_DX5" << kFlagSeparator;
}
else
{
if (0 != (dwFlags & DIEP_AXES)) flagsString << L"DIEP_AXES" << kFlagSeparator;
if (0 != (dwFlags & DIEP_DIRECTION)) flagsString << L"DIEP_DIRECTION" << kFlagSeparator;
if (0 != (dwFlags & DIEP_DURATION)) flagsString << L"DIEP_DURATION" << kFlagSeparator;
if (0 != (dwFlags & DIEP_ENVELOPE)) flagsString << L"DIEP_ENVELOPE" << kFlagSeparator;
if (0 != (dwFlags & DIEP_GAIN)) flagsString << L"DIEP_GAIN" << kFlagSeparator;
if (0 != (dwFlags & DIEP_SAMPLEPERIOD)) flagsString << L"DIEP_SAMPLEPERIOD" << kFlagSeparator;
if (0 != (dwFlags & DIEP_STARTDELAY)) flagsString << L"DIEP_STARTDELAY" << kFlagSeparator;
if (0 != (dwFlags & DIEP_TRIGGERBUTTON))
flagsString << L"DIEP_TRIGGERBUTTON" << kFlagSeparator;
if (0 != (dwFlags & DIEP_TRIGGERREPEATINTERVAL))
flagsString << L"DIEP_TRIGGERREPEATINTERVAL" << kFlagSeparator;
if (0 != (dwFlags & DIEP_TYPESPECIFICPARAMS))
flagsString << L"DIEP_TYPESPECIFICPARAMS" << kFlagSeparator;
}
if (true == flagsString.Empty()) return flagsString << L"none" << kFlagSeparator;
flagsString.RemoveSuffix((unsigned int)kFlagSeparator.length());
return flagsString;
}
/// Attempts to recognize the size of a parameter structure and returns a string representing what
/// was recognized.
/// @param [in] dwSize Size of the structure, as extracted from the relevant field.
/// @return String representation of the recognized structure.
static const wchar_t* ParameterStructSizeToString(DWORD dwSize)
{
switch (dwSize)
{
case sizeof(DIEFFECT):
return L"sizeof(DIEFFECT)";
case sizeof(DIEFFECT_DX5):
return L"sizeof(DIEFFECT_DX5)";
default:
return L"unknown";
}
}
/// Parses the specified flags, which would normally be present inside the dwFlags member of
/// DIEFFECT, and extracts individual strings for each flag that is present.
/// @param [in] dwFlags Flags to parse.
/// @return String representation of the flags that are set.
static const Infra::TemporaryString ParameterStructFlagsToString(DWORD dwFlags)
{
constexpr std::wstring_view kFlagSeparator = L" | ";
Infra::TemporaryString flagsString;
if (0 != (dwFlags & DIEFF_CARTESIAN)) flagsString << L"DIEFF_CARTESIAN" << kFlagSeparator;
if (0 != (dwFlags & DIEFF_POLAR)) flagsString << L"DIEFF_POLAR" << kFlagSeparator;
if (0 != (dwFlags & DIEFF_SPHERICAL)) flagsString << L"DIEFF_SPHERICAL" << kFlagSeparator;
if (0 != (dwFlags & DIEFF_OBJECTIDS)) flagsString << L"DIEFF_OBJECTIDS" << kFlagSeparator;
if (0 != (dwFlags & DIEFF_OBJECTOFFSETS))
flagsString << L"DIEFF_OBJECTOFFSETS" << kFlagSeparator;
if (true == flagsString.Empty()) return flagsString << L"none" << kFlagSeparator;
flagsString.RemoveSuffix((unsigned int)kFlagSeparator.length());
return flagsString;
}
/// Selects the coordinate system that should be used to represent the coordinates set in the
/// specified direction vector, subject to the specified flags.
/// @param [in] directionVector Vector from which coordinates are to be extracted.
/// @param [in] dwFlags Flags specifying the allowed coordinate systems using DirectInput
/// constants.
/// @return Selected coordinate system if it is valid. An example of an invalid situation is polar
/// coordinates specified by the flags but the direction vector contains only one axis or more
/// than two axes.
static std::optional<Controller::ForceFeedback::ECoordinateSystem> PickCoordinateSystem(
const Controller::ForceFeedback::DirectionVector& directionVector, DWORD dwFlags)
{
if (1 == directionVector.GetNumAxes())
{
// Only Cartesian coordinates are valid with a single axis.
if (0 == (dwFlags & DIEFF_CARTESIAN)) return std::nullopt;
}
else if (2 != directionVector.GetNumAxes())
{
// Polar coordinates are only valid when there are two axes present.
if (DIEFF_POLAR == (dwFlags & (DIEFF_CARTESIAN | DIEFF_POLAR | DIEFF_SPHERICAL)))
return std::nullopt;
}
// Match the original coordinate system if possible.
// The output would be exactly what was originally supplied as input when setting parameters.
switch (directionVector.GetOriginalCoordinateSystem())
{
case Controller::ForceFeedback::ECoordinateSystem::Cartesian:
if (0 != (dwFlags & DIEFF_CARTESIAN))
return Controller::ForceFeedback::ECoordinateSystem::Cartesian;
break;
case Controller::ForceFeedback::ECoordinateSystem::Polar:
if (0 != (dwFlags & DIEFF_POLAR))
return Controller::ForceFeedback::ECoordinateSystem::Polar;
break;
case Controller::ForceFeedback::ECoordinateSystem::Spherical:
if (0 != (dwFlags & DIEFF_SPHERICAL))
return Controller::ForceFeedback::ECoordinateSystem::Spherical;
break;
}
// Try other coordinate systems in Xidi-preferred order.
if (0 != (dwFlags & DIEFF_SPHERICAL))
return Controller::ForceFeedback::ECoordinateSystem::Spherical;
else if (0 != (dwFlags & DIEFF_POLAR))
return Controller::ForceFeedback::ECoordinateSystem::Polar;
else if (0 != (dwFlags & DIEFF_CARTESIAN))
return Controller::ForceFeedback::ECoordinateSystem::Cartesian;
return std::nullopt;
}
template <EDirectInputVersion diVersion> VirtualDirectInputEffect<diVersion>::
VirtualDirectInputEffect(
VirtualDirectInputDeviceBase<diVersion>& associatedDevice,
const Controller::ForceFeedback::Effect& effect,
const GUID& effectGuid)
: associatedDevice(associatedDevice),
effect(effect.Clone()),
effectGuid(effectGuid),
refCount(1)
{
associatedDevice.AddRef();
associatedDevice.ForceFeedbackEffectRegister((void*)this);
}
template <EDirectInputVersion diVersion> VirtualDirectInputEffect<
diVersion>::~VirtualDirectInputEffect(void)
{
// If the effect represented by this object is already downloaded to a device then it must be
// removed from that device.
Controller::ForceFeedback::Device* const forceFeedbackDevice =
associatedDevice.GetVirtualController().ForceFeedbackGetDevice();
if (nullptr != forceFeedbackDevice) forceFeedbackDevice->RemoveEffect(effect->Identifier());
associatedDevice.ForceFeedbackEffectUnregister((void*)this);
associatedDevice.Release();
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::DownloadInternal(void)
{
Controller::ForceFeedback::Device* const forceFeedbackDevice =
associatedDevice.AutoAcquireAndGetForceFeedbackDevice();
if (nullptr == forceFeedbackDevice) return DIERR_NOTEXCLUSIVEACQUIRED;
return DownloadEffectToDevice(*effect, *forceFeedbackDevice);
}
template <EDirectInputVersion diVersion> void
VirtualDirectInputEffect<diVersion>::DumpEffectParameters(
LPCDIEFFECT peff, DWORD dwFlags) const
{
if (Infra::Message::WillOutputMessageOfSeverity(kDumpSeverity))
{
Infra::Message::Output(kDumpSeverity, L"Begin dump of effect parameters.");
Infra::Message::Output(kDumpSeverity, L" Control:");
Infra::Message::OutputFormatted(
kDumpSeverity,
L" flags = 0x%08x (%s)",
dwFlags,
ParameterTopLevelFlagsToString(dwFlags).AsCString());
Infra::Message::Output(kDumpSeverity, L" Basics:");
if (nullptr == peff)
{
Infra::Message::Output(kDumpSeverity, L" (nullptr)");
}
else
{
Infra::Message::OutputFormatted(
kDumpSeverity,
L" dwSize = %u (%s)",
peff->dwSize,
ParameterStructSizeToString(peff->dwSize));
Infra::Message::OutputFormatted(
kDumpSeverity,
L" dwFlags = 0x%08x (%s)",
peff->dwFlags,
ParameterStructFlagsToString(peff->dwFlags).AsCString());
if (0 != (dwFlags & DIEP_DURATION))
{
if (INFINITE == peff->dwDuration)
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwDuration = %u (INFINITE)", peff->dwDuration);
else
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwDuration = %u", peff->dwDuration);
}
if (0 != (dwFlags & DIEP_SAMPLEPERIOD))
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwSamplePeriod = %u", peff->dwSamplePeriod);
if (0 != (dwFlags & DIEP_GAIN))
Infra::Message::OutputFormatted(kDumpSeverity, L" dwGain = %u", peff->dwGain);
if (0 != (dwFlags & DIEP_STARTDELAY))
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwStartDelay = %u", peff->dwStartDelay);
if (0 != (dwFlags & DIEP_TRIGGERBUTTON))
{
if (DIEB_NOTRIGGER == peff->dwTriggerButton)
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwTriggerButton = %u (DIEB_NOTRIGGER)", peff->dwTriggerButton);
else
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwTriggerButton = %u", peff->dwTriggerButton);
}
if (0 != (dwFlags & DIEP_TRIGGERREPEATINTERVAL))
Infra::Message::OutputFormatted(
kDumpSeverity, L" dwTriggerRepeatInterval = %u", peff->dwTriggerRepeatInterval);
if (0 != (dwFlags & DIEP_AXES))
{
Infra::Message::Output(kDumpSeverity, L" Axes:");
Infra::Message::OutputFormatted(kDumpSeverity, L" cAxes = %u", peff->cAxes);
if (nullptr == peff->rgdwAxes)
{
Infra::Message::Output(kDumpSeverity, L" rgdxAxes = (nullptr)");
}
else
{
for (DWORD i = 0; i < peff->cAxes; ++i)
{
std::optional<Controller::SElementIdentifier> maybeAxisElement = std::nullopt;
switch (peff->dwFlags & (DIEFF_OBJECTIDS | DIEFF_OBJECTOFFSETS))
{
case DIEFF_OBJECTIDS:
maybeAxisElement = associatedDevice.IdentifyElement(peff->rgdwAxes[i], DIPH_BYID);
break;
case DIEFF_OBJECTOFFSETS:
maybeAxisElement =
associatedDevice.IdentifyElement(peff->rgdwAxes[i], DIPH_BYOFFSET);
break;
}
if (true == maybeAxisElement.has_value())
{
Infra::TemporaryBuffer<wchar_t> axisElementString;
VirtualDirectInputDeviceBase<EDirectInputVersion::k8W>::ElementToString(
maybeAxisElement.value(),
axisElementString.Data(),
axisElementString.Capacity());
Infra::Message::OutputFormatted(
kDumpSeverity,
L" rgdwAxes[%2u] = 0x%04x (%s)",
i,
peff->rgdwAxes[i],
axisElementString.Data());
}
else
{
Infra::Message::OutputFormatted(
kDumpSeverity,
L" rgdwAxes[%2u] = 0x%04x (unable to identify)",
i,
peff->rgdwAxes[i]);
}
}
}
}
if (0 != (dwFlags & DIEP_DIRECTION))
{
Infra::Message::Output(kDumpSeverity, L" Direction:");
Infra::Message::OutputFormatted(kDumpSeverity, L" cAxes = %u", peff->cAxes);
if (nullptr == peff->rglDirection)
{
Infra::Message::Output(kDumpSeverity, L" rglDirection = (nullptr)");
}
else
{
for (DWORD i = 0; i < peff->cAxes; ++i)
{
Infra::Message::OutputFormatted(
kDumpSeverity, L" rglDirection[%2u] = %u", i, peff->rglDirection[i]);
}
}
}
if (0 != (dwFlags & DIEP_ENVELOPE))
{
Infra::Message::Output(kDumpSeverity, L" Envelope:");
if (nullptr == peff->lpEnvelope)
{
Infra::Message::Output(kDumpSeverity, L" (nullptr)");
}
else
{
const DIENVELOPE* const envelope = (const DIENVELOPE*)peff->lpEnvelope;
Infra::Message::OutputFormatted(
kDumpSeverity,
L" lpEnvelope->dwSize = %u (%s)",
envelope->dwSize,
((sizeof(DIENVELOPE) == envelope->dwSize) ? L"sizeof(DIENVELOPE)" : L"unknown"));
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpEnvelope->dwAttackLevel = %u", envelope->dwAttackLevel);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpEnvelope->dwAttackTime = %u", envelope->dwAttackTime);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpEnvelope->dwFadeLevel = %u", envelope->dwFadeLevel);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpEnvelope->dwFadeTime = %u", envelope->dwFadeTime);
}
}
if (0 != (dwFlags & DIEP_TYPESPECIFICPARAMS))
{
Infra::Message::Output(kDumpSeverity, L" Type-Specific:");
if (nullptr == peff->lpvTypeSpecificParams)
Infra::Message::Output(kDumpSeverity, L" (nullptr)");
else
DumpTypeSpecificParameters(peff);
}
}
Infra::Message::Output(kDumpSeverity, L"End dump of effect parameters.");
}
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::SetParametersInternal(
LPCDIEFFECT peff,
DWORD dwFlags,
std::optional<Controller::ForceFeedback::TEffectTimeMs> timestamp)
{
DumpEffectParameters(peff, dwFlags);
if (nullptr == peff) return DIERR_INVALIDPARAM;
// These flags control the behavior of this method if all parameters are updated successfully.
// Per DirectInput documentation, at most one of them is allowed to be passed.
switch (dwFlags & (DIEP_NODOWNLOAD | DIEP_NORESTART | DIEP_START))
{
case 0:
case DIEP_NODOWNLOAD:
case DIEP_NORESTART:
case DIEP_START:
break;
default:
return DIERR_INVALIDPARAM;
}
// This cloned effect will receive all the parameter updates and will be synced back to the
// original effect once all parameter values are accepted. Doing this means that an invalid
// value for a parameter means the original effect remains untouched.
std::unique_ptr<Controller::ForceFeedback::Effect> updatedEffect;
if (0 != (dwFlags & DIEP_TYPESPECIFICPARAMS))
{
if (nullptr == peff->lpvTypeSpecificParams) return DIERR_INVALIDPARAM;
updatedEffect = CloneAndSetTypeSpecificParameters(peff);
if (nullptr == updatedEffect) return DIERR_INVALIDPARAM;
}
else
{
updatedEffect = effect->Clone();
}
switch (peff->dwSize)
{
case sizeof(DIEFFECT):
// These parameters are present in the new version of the structure but not in the old.
if (0 != (dwFlags & DIEP_STARTDELAY))
{
if (false == updatedEffect->SetStartDelay(ConvertTimeFromDirectInput(peff->dwStartDelay)))
return DIERR_INVALIDPARAM;
}
break;
case sizeof(DIEFFECT_DX5):
// No parameters are present in the old version of the structure but removed from the new.
break;
default:
return DIERR_INVALIDPARAM;
}
if (0 != (dwFlags & DIEP_AXES))
{
if (peff->cAxes > Controller::ForceFeedback::kEffectAxesMaximumNumber)
return DIERR_INVALIDPARAM;
if (nullptr == peff->rgdwAxes) return DIERR_INVALIDPARAM;
Controller::ForceFeedback::SAssociatedAxes newAssociatedAxes = {.count = (int)peff->cAxes};
if ((size_t)newAssociatedAxes.count > newAssociatedAxes.type.size())
return DIERR_INVALIDPARAM;
DWORD identifyElementMethod = 0;
switch (peff->dwFlags & (DIEFF_OBJECTIDS | DIEFF_OBJECTOFFSETS))
{
case DIEFF_OBJECTIDS:
identifyElementMethod = DIPH_BYID;
break;
case DIEFF_OBJECTOFFSETS:
identifyElementMethod = DIPH_BYOFFSET;
break;
default:
// It is an error if the caller does not specify exactly one specific way of identifying
// axis objects.
return DIERR_INVALIDPARAM;
}
for (int i = 0; i < newAssociatedAxes.count; ++i)
{
const std::optional<Controller::SElementIdentifier> maybeElement =
associatedDevice.IdentifyElement(peff->rgdwAxes[i], identifyElementMethod);
if (false == maybeElement.has_value()) return DIERR_INVALIDPARAM;
const Controller::SElementIdentifier element = maybeElement.value();
if (Controller::EElementType::Axis != element.type) return DIERR_INVALIDPARAM;
if (false ==
associatedDevice.GetVirtualController()
.GetCapabilities()
.ForceFeedbackIsSupportedForAxis(element.axis))
return DIERR_INVALIDPARAM;
newAssociatedAxes.type[i] = element.axis;
}
if (false == updatedEffect->SetAssociatedAxes(newAssociatedAxes)) return DIERR_INVALIDPARAM;
}
if (0 != (dwFlags & DIEP_DIRECTION))
{
if (peff->cAxes > Controller::ForceFeedback::kEffectAxesMaximumNumber)
return DIERR_INVALIDPARAM;
if (nullptr == peff->rglDirection) return DIERR_INVALIDPARAM;
Controller::ForceFeedback::TEffectValue
coordinates[Controller::ForceFeedback::kEffectAxesMaximumNumber] = {};
int numCoordinates = peff->cAxes;
for (int i = 0; i < numCoordinates; ++i)
coordinates[i] = (Controller::ForceFeedback::TEffectValue)peff->rglDirection[i];
bool coordinateSetResult = false;
switch (peff->dwFlags & (DIEFF_CARTESIAN | DIEFF_POLAR | DIEFF_SPHERICAL))
{
case DIEFF_CARTESIAN:
coordinateSetResult =
updatedEffect->Direction().SetDirectionUsingCartesian(coordinates, numCoordinates);
break;
case DIEFF_POLAR:
coordinateSetResult =
updatedEffect->Direction().SetDirectionUsingPolar(coordinates, numCoordinates - 1);
break;
case DIEFF_SPHERICAL:
coordinateSetResult = updatedEffect->Direction().SetDirectionUsingSpherical(
coordinates, numCoordinates - 1);
break;
default:
// It is an error if the caller does not specify exactly one coordinate system.
return DIERR_INVALIDPARAM;
}
if (true != coordinateSetResult) return DIERR_INVALIDPARAM;
}
if (0 != (dwFlags & DIEP_DURATION))
{
if (false == updatedEffect->SetDuration(ConvertTimeFromDirectInput(peff->dwDuration)))
return DIERR_INVALIDPARAM;
}
if (0 != (dwFlags & DIEP_ENVELOPE))
{
if (nullptr == peff->lpEnvelope)
{
updatedEffect->ClearEnvelope();
}
else
{
if (sizeof(DIENVELOPE) != peff->lpEnvelope->dwSize) return DIERR_INVALIDPARAM;
const Controller::ForceFeedback::SEnvelope newEnvelope = {
.attackTime = ConvertTimeFromDirectInput(peff->lpEnvelope->dwAttackTime),
.attackLevel = (Controller::ForceFeedback::TEffectValue)peff->lpEnvelope->dwAttackLevel,
.fadeTime = ConvertTimeFromDirectInput(peff->lpEnvelope->dwFadeTime),
.fadeLevel = (Controller::ForceFeedback::TEffectValue)peff->lpEnvelope->dwFadeLevel};
if (false == updatedEffect->SetEnvelope(newEnvelope)) return DIERR_INVALIDPARAM;
}
}
if (0 != (dwFlags & DIEP_GAIN))
{
if (false == updatedEffect->SetGain((Controller::ForceFeedback::TEffectValue)peff->dwGain))
return DIERR_INVALIDPARAM;
}
if (0 != (dwFlags & DIEP_SAMPLEPERIOD))
{
if (false == updatedEffect->SetSamplePeriod(ConvertTimeFromDirectInput(peff->dwSamplePeriod)))
return DIERR_INVALIDPARAM;
}
// Final sync operation is expected to succeed.
if (false == effect->SyncParametersFrom(*updatedEffect))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Error,
L"Internal error while syncing new parameters for a force feedback effect associated with Xidi virtual controller %u.",
(1 + associatedDevice.GetVirtualController().GetIdentifier()));
return DIERR_GENERIC;
}
// Destroying this object now means that any future references to it will trigger crashes during
// testing.
updatedEffect = nullptr;
// At this point parameter updates were successful. What happens next depends on the flag
// values. The effect could either be downloaded, downloaded and (re)started, or none of these.
if (0 != (dwFlags & DIEP_NODOWNLOAD))
{
return DI_DOWNLOADSKIPPED;
}
else
{
Controller::ForceFeedback::Device* const forceFeedbackDevice =
associatedDevice.AutoAcquireAndGetForceFeedbackDevice();
if (nullptr == forceFeedbackDevice)
{
// It is not an error if the physical device has not been, or cannot be, acquired in
// exclusive mode. In that case, the download operation is skipped but parameters are still
// updated.
return DI_DOWNLOADSKIPPED;
}
// If the download operation fails the parameters have still been updated but the caller needs
// to be provided the reason for the failure.
const HRESULT downloadResult = DownloadEffectToDevice(*effect, *forceFeedbackDevice);
if (DI_OK != downloadResult) return downloadResult;
}
// Default behavior is to update an effect without changing its play state. Playing effects are
// updated on-the-fly, and non-playing effects are not started.
if (0 != (dwFlags & DIEP_START))
{
// Getting to this point means the effect exists on the device.
// Starting or restarting the effect requires that the device be acquired in exclusive mode,
// although since the download succeeded this should be the case already.
Controller::ForceFeedback::Device* const forceFeedbackDevice =
associatedDevice.GetVirtualController().ForceFeedbackGetDevice();
if (nullptr == forceFeedbackDevice)
{
// This should never happen. It means an effect exists on the device and yet the device is
// somehow not acquired in exclusive mode.
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Error,
L"Internal error while attempting to start or restart a force feedback effect after setting its parameters on Xidi virtual controller %u.",
(1 + associatedDevice.GetVirtualController().GetIdentifier()));
return DIERR_GENERIC;
}
forceFeedbackDevice->StopEffect(effect->Identifier());
if (false == forceFeedbackDevice->StartEffect(effect->Identifier(), 1, timestamp))
{
// This should never happen. It means an effect that in theory should be downloaded and
// ready to play is somehow unable to be started.
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Error,
L"Internal error while attempting to start or restart a force feedback effect after setting its parameters on Xidi virtual controller %u.",
(1 + associatedDevice.GetVirtualController().GetIdentifier()));
return DIERR_GENERIC;
}
}
return DI_OK;
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::StartInternal(
DWORD dwIterations,
DWORD dwFlags,
std::optional<Controller::ForceFeedback::TEffectTimeMs> timestamp)
{
if (0 == dwIterations) return DIERR_INVALIDPARAM;
Controller::ForceFeedback::Device* const forceFeedbackDevice =
associatedDevice.AutoAcquireAndGetForceFeedbackDevice();
if (nullptr == forceFeedbackDevice) return DIERR_NOTEXCLUSIVEACQUIRED;
if (0 != (dwFlags & DIES_NODOWNLOAD))
{
// The download operation was skipped by the caller.
// If the effect does not already exist on the device then the effect cannot be played.
if (false == forceFeedbackDevice->IsEffectOnDevice(effect->Identifier()))
return DIERR_INVALIDPARAM;
}
else
{
// The download operation was not skipped by the caller.
// If the effect exists on the device its parameters will get updated, otherwise the effect
// will be downloaded. If for some reason the download attempt fails then the effect cannot be
// played.
const HRESULT downloadResult = DownloadEffectToDevice(*effect, *forceFeedbackDevice);
if (DI_OK != downloadResult) return downloadResult;
}
if (0 != (dwFlags & DIES_SOLO))
forceFeedbackDevice->StopAllEffects();
else
forceFeedbackDevice->StopEffect(effect->Identifier());
if (false ==
forceFeedbackDevice->StartEffect(
effect->Identifier(), (unsigned int)dwIterations, timestamp))
{
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Error,
L"Internal error while starting a force feedback effect associated with Xidi virtual controller %u.",
(1 + associatedDevice.GetVirtualController().GetIdentifier()));
return DIERR_GENERIC;
}
return DI_OK;
}
template <EDirectInputVersion diVersion> void
VirtualDirectInputEffect<diVersion>::DumpTypeSpecificParameters(LPCDIEFFECT peff) const
{
Infra::Message::OutputFormatted(
kDumpSeverity, L" cbTypeSpecificParams = %u (unknown)", peff->cbTypeSpecificParams);
Infra::Message::OutputFormatted(
kDumpSeverity,
L" lpvTypeSpecificParams = (%s)",
((nullptr == peff->lpvTypeSpecificParams) ? L"nullptr" : L"present"));
}
template <EDirectInputVersion diVersion> void
ConstantForceDirectInputEffect<diVersion>::DumpTypeSpecificParameters(LPCDIEFFECT peff) const
{
if (sizeof(DICONSTANTFORCE) == peff->cbTypeSpecificParams)
{
const DICONSTANTFORCE* const typeSpecificParams =
(const DICONSTANTFORCE*)peff->lpvTypeSpecificParams;
Infra::Message::OutputFormatted(
kDumpSeverity,
L" cbTypeSpecificParams = %u (sizeof(DICONSTANTFORCE))",
peff->cbTypeSpecificParams);
Infra::Message::OutputFormatted(
kDumpSeverity,
L" lpvTypeSpecificParams->lMagnitude = %ld",
typeSpecificParams->lMagnitude);
}
else
{
VirtualDirectInputEffect<diVersion>::DumpTypeSpecificParameters(peff);
}
}
template <EDirectInputVersion diVersion> void
PeriodicDirectInputEffect<diVersion>::DumpTypeSpecificParameters(LPCDIEFFECT peff) const
{
if (sizeof(DIPERIODIC) == peff->cbTypeSpecificParams)
{
const DIPERIODIC* const typeSpecificParams = (const DIPERIODIC*)peff->lpvTypeSpecificParams;
Infra::Message::OutputFormatted(
kDumpSeverity,
L" cbTypeSpecificParams = %u (sizeof(DIPERIODIC))",
peff->cbTypeSpecificParams);
Infra::Message::OutputFormatted(
kDumpSeverity,
L" lpvTypeSpecificParams->dwMagnitude = %u",
typeSpecificParams->dwMagnitude);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpvTypeSpecificParams->lOffset = %ld", typeSpecificParams->lOffset);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpvTypeSpecificParams->dwPhase = %u", typeSpecificParams->dwPhase);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpvTypeSpecificParams->dwPeriod = %u", typeSpecificParams->dwPeriod);
}
else
{
VirtualDirectInputEffect<diVersion>::DumpTypeSpecificParameters(peff);
}
}
template <EDirectInputVersion diVersion> void
RampForceDirectInputEffect<diVersion>::DumpTypeSpecificParameters(LPCDIEFFECT peff) const
{
if (sizeof(DIRAMPFORCE) == peff->cbTypeSpecificParams)
{
const DIRAMPFORCE* const typeSpecificParams = (const DIRAMPFORCE*)peff->lpvTypeSpecificParams;
Infra::Message::OutputFormatted(
kDumpSeverity,
L" cbTypeSpecificParams = %u (sizeof(DIRAMPFORCE))",
peff->cbTypeSpecificParams);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpvTypeSpecificParams->lStart = %ld", typeSpecificParams->lStart);
Infra::Message::OutputFormatted(
kDumpSeverity, L" lpvTypeSpecificParams->lEnd = %ld", typeSpecificParams->lEnd);
}
else
{
VirtualDirectInputEffect<diVersion>::DumpTypeSpecificParameters(peff);
}
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::QueryInterface(REFIID riid, LPVOID* ppvObj)
{
if (nullptr == ppvObj) return E_POINTER;
bool validInterfaceRequested = false;
if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDirectInputEffect))
validInterfaceRequested = true;
if (true == validInterfaceRequested)
{
AddRef();
*ppvObj = this;
return S_OK;
}
return E_NOINTERFACE;
}
template <EDirectInputVersion diVersion> ULONG VirtualDirectInputEffect<diVersion>::AddRef(void)
{
return ++refCount;
}
template <EDirectInputVersion diVersion> ULONG VirtualDirectInputEffect<diVersion>::Release(void)
{
const unsigned long numRemainingRefs = --refCount;
if (0 == numRemainingRefs) delete this;
return (ULONG)numRemainingRefs;
}
template <EDirectInputVersion diVersion> HRESULT VirtualDirectInputEffect<diVersion>::Initialize(
HINSTANCE hinst, DWORD dwVersion, REFGUID rguid)
{
// Not required for Xidi virtual force feedback effects as they are implemented now.
// However, this method is needed for creating IDirectInputDevice objects via COM.
constexpr Infra::Message::ESeverity kMethodSeverity = Infra::Message::ESeverity::Info;
LOG_INVOCATION_AND_RETURN(DI_OK, kMethodSeverity);
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::GetEffectGuid(LPGUID pguid)
{
constexpr Infra::Message::ESeverity kMethodSeverity = Infra::Message::ESeverity::Info;
if (nullptr == pguid) LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
*pguid = effectGuid;
LOG_INVOCATION_AND_RETURN(DI_OK, kMethodSeverity);
}
template <EDirectInputVersion diVersion> HRESULT
VirtualDirectInputEffect<diVersion>::GetParameters(LPDIEFFECT peff, DWORD dwFlags)
{
constexpr Infra::Message::ESeverity kMethodSeverity = Infra::Message::ESeverity::Info;
if (nullptr == peff) LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
switch (peff->dwSize)
{
case sizeof(DIEFFECT):
// These parameters are present in the new version of the structure but not in the old.
if (0 != (dwFlags & DIEP_STARTDELAY))
peff->dwStartDelay = ConvertTimeToDirectInput(effect->GetStartDelay());
break;
case sizeof(DIEFFECT_DX5):
// No parameters are present in the old version of the structure but removed from the new.
break;
default:
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
}
bool axesInsufficientBuffer = false;
if (0 != (dwFlags & DIEP_AXES))
{
if (false == effect->HasAssociatedAxes())
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
const Controller::ForceFeedback::SAssociatedAxes& associatedAxes =
effect->GetAssociatedAxes().value();
if (peff->cAxes < (DWORD)associatedAxes.count)
{
peff->cAxes = (DWORD)associatedAxes.count;
axesInsufficientBuffer = true;
}
else
{
if (nullptr == peff->rgdwAxes)
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
switch (peff->dwFlags & (DIEFF_OBJECTIDS | DIEFF_OBJECTOFFSETS))
{
case DIEFF_OBJECTIDS:
for (int i = 0; i < associatedAxes.count; ++i)
{
const std::optional<DWORD> maybeObjectId = associatedDevice.IdentifyObjectById(
{.type = Controller::EElementType::Axis, .axis = associatedAxes.type[i]});
if (false == maybeObjectId.has_value())
{
// This should never happen. It means an axis object was successfully set on a force
// feedback effect but it could not be mapped back to its object ID.
Infra::Message::OutputFormatted(
Infra::Message::ESeverity::Error,
L"Internal error while mapping force feedback axes to object IDs on Xidi virtual controller %u.",
(1 + associatedDevice.GetVirtualController().GetIdentifier()));
LOG_INVOCATION_AND_RETURN(DIERR_GENERIC, kMethodSeverity);
}
peff->rgdwAxes[i] = maybeObjectId.value();
}
break;
case DIEFF_OBJECTOFFSETS:
for (int i = 0; i < associatedAxes.count; ++i)
{
const std::optional<DWORD> maybeOffset = associatedDevice.IdentifyObjectByOffset(
{.type = Controller::EElementType::Axis, .axis = associatedAxes.type[i]});
if (false == maybeOffset.has_value())
{
// This can happen if the application's data format is not set or if it somehow
// changed and now does not contain one of the axes that are associated with this
// effect object.
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
}
peff->rgdwAxes[i] = maybeOffset.value();
}
break;
default:
// It is an error if the caller does not specify exactly one specific way of identifying
// axis objects.
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
}
}
}
bool directionInsufficientBuffer = false;
if (0 != (dwFlags & DIEP_DIRECTION))
{
if (false == effect->HasDirection())
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
const Controller::ForceFeedback::DirectionVector& directionVector = effect->Direction();
if (peff->cAxes < (DWORD)directionVector.GetNumAxes())
{
peff->cAxes = (DWORD)directionVector.GetNumAxes();
directionInsufficientBuffer = true;
}
else
{
if (nullptr == peff->rglDirection)
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
if (0 == (peff->dwFlags & (DIEFF_CARTESIAN | DIEFF_POLAR | DIEFF_SPHERICAL)))
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
const std::optional<Controller::ForceFeedback::ECoordinateSystem> maybeCoordinateSystem =
PickCoordinateSystem(directionVector, peff->dwFlags);
if (false == maybeCoordinateSystem.has_value())
LOG_INVOCATION_AND_RETURN(DIERR_INVALIDPARAM, kMethodSeverity);
Controller::ForceFeedback::TEffectValue
coordinates[Controller::ForceFeedback::kEffectAxesMaximumNumber] = {};
int numCoordinates = 0;
// The selected coordinate system is identified to the caller by ensuring exactly one
// coordinate system is saved into the flags on output. First the coordinate system flags
// are all cleared, then in the switch block below a single system is added back into the
// flags.
peff->dwFlags = (peff->dwFlags & ~(DIEFF_CARTESIAN | DIEFF_POLAR | DIEFF_SPHERICAL));
switch (maybeCoordinateSystem.value())
{
case Controller::ForceFeedback::ECoordinateSystem::Cartesian:
numCoordinates =
directionVector.GetCartesianCoordinates(coordinates, _countof(coordinates));
peff->dwFlags |= DIEFF_CARTESIAN;
break;
case Controller::ForceFeedback::ECoordinateSystem::Polar: