| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <atomic> |
| 7 |
#include <string> |
| 8 |
#include <thread> |
| 9 |
|
| 10 |
#include <Windows.h> |
| 11 |
|
| 12 |
#include "Common/Event.h" |
| 13 |
#include "Common/Flag.h" |
| 14 |
|
| 15 |
#include "InputCommon/ControllerInterface/CoreDevice.h" |
| 16 |
#include "InputCommon/ControllerInterface/DInput/DInput8.h" |
| 17 |
|
| 18 |
namespace ciface::ForceFeedback |
| 19 |
{ |
| 20 |
class ForceFeedbackDevice : public Core::Device |
| 21 |
{ |
| 22 |
public: |
| 23 |
bool InitForceFeedback(const LPDIRECTINPUTDEVICE8, int axis_count); |
| 24 |
void DeInitForceFeedback(); |
| 25 |
|
| 26 |
private: |
| 27 |
void ThreadFunc(); |
| 28 |
|
| 29 |
class Force : public Output |
| 30 |
{ |
| 31 |
public: |
| 32 |
Force(ForceFeedbackDevice* parent, const char* name, LPDIRECTINPUTEFFECT effect); |
| 33 |
|
| 34 |
void UpdateOutput(); |
| 35 |
void Release(); |
| 36 |
|
| 37 |
void SetState(ControlState state) override; |
| 38 |
std::string GetName() const override; |
| 39 |
|
| 40 |
protected: |
| 41 |
const LPDIRECTINPUTEFFECT m_effect; |
| 42 |
|
| 43 |
private: |
| 44 |
virtual void UpdateEffect(int magnitude) = 0; |
| 45 |
|
| 46 |
ForceFeedbackDevice& m_parent; |
| 47 |
const char* const m_name; |
| 48 |
std::atomic<int> m_desired_magnitude; |
| 49 |
}; |
| 50 |
|
| 51 |
template <typename P> |
| 52 |
class TypedForce : public Force |
| 53 |
{ |
| 54 |
public: |
| 55 |
TypedForce(ForceFeedbackDevice* parent, const char* name, LPDIRECTINPUTEFFECT effect, |
| 56 |
const P& params); |
| 57 |
|
| 58 |
private: |
| 59 |
void UpdateEffect(int magnitude) override; |
| 60 |
|
| 61 |
|
| 62 |
bool UpdateParameters(int magnitude); |
| 63 |
|
| 64 |
void PlayEffect(); |
| 65 |
void StopEffect(); |
| 66 |
|
| 67 |
P m_params = {}; |
| 68 |
}; |
| 69 |
|
| 70 |
void SignalUpdateThread(); |
| 71 |
|
| 72 |
typedef TypedForce<DICONSTANTFORCE> ForceConstant; |
| 73 |
typedef TypedForce<DIRAMPFORCE> ForceRamp; |
| 74 |
typedef TypedForce<DIPERIODIC> ForcePeriodic; |
| 75 |
|
| 76 |
std::thread m_update_thread; |
| 77 |
Common::Event m_update_event; |
| 78 |
Common::Flag m_run_thread; |
| 79 |
}; |
| 80 |
} |