File: DirectInputAdapter.h

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (223 lines) | stat: -rw-r--r-- 5,232 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

/*
 * The OS X Force Feedback API is very similar to the DirectInput API,
 * but it is no longer object-oriented and all prefixes have been changed.
 *
 * Our implementation uses the Windows API names so we need to adapt
 * for these differences on OS X.
 */

#pragma once

#include <atomic>

typedef LONG* LPLONG; // Missing type for ForceFeedback.h
#include <CoreFoundation/CoreFoundation.h>
#include <ForceFeedback/ForceFeedback.h>
#include "DirectInputConstants.h" // Not stricty necessary
#include "Common/CommonTypes.h" // for LONG

namespace ciface
{
namespace ForceFeedback
{


// Prototypes
class IUnknownImpl;
class FFEffectAdapter;
class FFDeviceAdapter;

// Structs
typedef FFCAPABILITIES              DICAPABILITIES;
typedef FFCONDITION                 DICONDITION;
typedef FFCONSTANTFORCE             DICONSTANTFORCE;
typedef FFCUSTOMFORCE               DICUSTOMFORCE;
typedef FFEFFECT                    DIEFFECT;
typedef FFEFFESCAPE                 DIEFFESCAPE;
typedef FFENVELOPE                  DIENVELOPE;
typedef FFPERIODIC                  DIPERIODIC;
typedef FFRAMPFORCE                 DIRAMPFORCE;

// Other types
typedef CFUUIDRef                   GUID;
typedef FFDeviceAdapter*            FFDeviceAdapterReference;
typedef FFEffectAdapter*            FFEffectAdapterReference;
typedef FFDeviceAdapterReference    LPDIRECTINPUTDEVICE8;
typedef FFEffectAdapterReference    LPDIRECTINPUTEFFECT;

// Property structures
#define DIPH_DEVICE 0

typedef struct DIPROPHEADER {
	DWORD dwSize;
	DWORD dwHeaderSize;
	DWORD dwObj;
	DWORD dwHow;
} DIPROPHEADER, *LPDIPROPHEADER;

typedef struct DIPROPDWORD {
	DIPROPHEADER diph;
	DWORD dwData;
} DIPROPDWORD, *LPDIPROPDWORD;

class IUnknownImpl : public IUnknown
{
private:
	std::atomic<ULONG> m_cRef;

public:
	IUnknownImpl() : m_cRef(1) {}
	virtual ~IUnknownImpl() {}

	HRESULT QueryInterface(REFIID iid, LPVOID *ppv)
	{
		*ppv = nullptr;

		if (CFEqual(&iid, IUnknownUUID))
			*ppv = this;
		if (nullptr == *ppv)
			return E_NOINTERFACE;

		((IUnknown*)*ppv)->AddRef();

		return S_OK;
	}

	ULONG AddRef()
	{
		return ++m_cRef;
	}

	ULONG Release()
	{
		if (--m_cRef == 0)
			delete this;

		return m_cRef;
	}
};

class FFEffectAdapter : public IUnknownImpl
{
private:
	// Only used for destruction
	FFDeviceObjectReference m_device;

public:
	FFEffectObjectReference m_effect;

	FFEffectAdapter(FFDeviceObjectReference device, FFEffectObjectReference effect) : m_device(device), m_effect(effect) {}
	~FFEffectAdapter() { FFDeviceReleaseEffect(m_device, m_effect); }

	HRESULT Download()
	{
		return FFEffectDownload(m_effect);
	}

	HRESULT Escape(FFEFFESCAPE *pFFEffectEscape)
	{
		return FFEffectEscape(m_effect, pFFEffectEscape);
	}

	HRESULT GetEffectStatus(FFEffectStatusFlag *pFlags)
	{
		return FFEffectGetEffectStatus(m_effect, pFlags);
	}

	HRESULT GetParameters(FFEFFECT *pFFEffect, FFEffectParameterFlag flags)
	{
		return FFEffectGetParameters(m_effect, pFFEffect, flags);
	}

	HRESULT SetParameters(FFEFFECT *pFFEffect, FFEffectParameterFlag flags)
	{
		return FFEffectSetParameters(m_effect, pFFEffect, flags);
	}

	HRESULT Start(UInt32 iterations, FFEffectStartFlag flags)
	{
		return FFEffectStart(m_effect, iterations, flags);
	}

	HRESULT Stop()
	{
		return FFEffectStop(m_effect);
	}

	HRESULT Unload()
	{
		return FFEffectUnload(m_effect);
	}
};

class FFDeviceAdapter : public IUnknownImpl
{
public:
	FFDeviceObjectReference m_device;

	FFDeviceAdapter(FFDeviceObjectReference device) : m_device(device) {}
	~FFDeviceAdapter() { FFReleaseDevice(m_device); }

	static HRESULT Create(io_service_t hidDevice, FFDeviceAdapterReference *pDeviceReference)
	{
		FFDeviceObjectReference ref;

		HRESULT hr = FFCreateDevice(hidDevice, &ref);
		if (SUCCEEDED(hr))
			*pDeviceReference = new FFDeviceAdapter(ref);

		return hr;
	}

	HRESULT CreateEffect(CFUUIDRef uuidRef, FFEFFECT *pEffectDefinition, FFEffectAdapterReference *pEffectReference, IUnknown *punkOuter)
	{
		FFEffectObjectReference ref;

		HRESULT hr = FFDeviceCreateEffect(m_device, uuidRef, pEffectDefinition, &ref);
		if (SUCCEEDED(hr))
			*pEffectReference = new FFEffectAdapter(m_device, ref);

		return hr;
	}

	HRESULT Escape(FFEFFESCAPE *pFFEffectEscape)
	{
		return FFDeviceEscape(m_device, pFFEffectEscape);
	}

	HRESULT GetForceFeedbackState(FFState *pFFState)
	{
		return FFDeviceGetForceFeedbackState(m_device, pFFState);
	}

	HRESULT SendForceFeedbackCommand(FFCommandFlag flags)
	{
		return FFDeviceSendForceFeedbackCommand(m_device, flags);
	}

	HRESULT SetCooperativeLevel(void *taskIdentifier, FFCooperativeLevelFlag flags)
	{
		return FFDeviceSetCooperativeLevel(m_device, taskIdentifier, flags);
	}

	HRESULT SetProperty(FFProperty property, const LPDIPROPHEADER pdiph)
	{
		// There are only two properties supported
		if (property != DIPROP_FFGAIN && property != DIPROP_AUTOCENTER)
			return DIERR_UNSUPPORTED;

		// And they are both device properties
		if (pdiph->dwHow != DIPH_DEVICE)
			return DIERR_INVALIDPARAM;

		UInt32 value = ((const LPDIPROPDWORD)pdiph)->dwData;
		return FFDeviceSetForceFeedbackProperty(m_device, property, &value);
	}
};

}
}