ObjFW  Diff

Differences From Artifact [c422cbe7b4]:

To Artifact [7c14afb2ff]:


22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36







-
+







#import "OHXInputGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHXInputGamepad.h"
#import "OHXInputExtendedGamepad.h"

#import "OFInitializationFailedException.h"
#import "OFReadFailedException.h"

#include <xinput.h>

#ifndef XINPUT_GAMEPAD_GUIDE
48
49
50
51
52
53
54
55


56
57
58
59
60
61
62
48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
63







-
+
+








int OHXInputVersion;
static WINAPI DWORD (*XInputGetStateFuncPtr)(DWORD, XINPUT_STATE *);
static WINAPI DWORD (*XInputGetCapabilitiesExFuncPtr)(DWORD, DWORD, DWORD,
    struct XInputCapabilitiesEx *);

@implementation OHXInputGameController
@synthesize vendorID = _vendorID, productID = _productID, gamepad = _gamepad;
@synthesize vendorID = _vendorID, productID = _productID;
@synthesize extendedGamepad = _extendedGamepad;

+ (void)initialize
{
	HMODULE module;

	if (self != [OHXInputGameController class])
		return;
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
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







-
+














-
+













-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+


-
+


-
+

-
+


-
+


-
+


-
+


-
+



-
+

-
+

-
+

-
+



















-
+

+
+
+
+
+

				    capabilities.vendorID];
				_productID = [[OFNumber alloc]
				    initWithUnsignedShort:
				    capabilities.productID];
			}
		}

		_gamepad = [[OHXInputGamepad alloc] init];
		_extendedGamepad = [[OHXInputExtendedGamepad alloc] init];

		[self retrieveState];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_vendorID release];
	[_productID release];
	[_gamepad release];
	[_extendedGamepad release];

	[super dealloc];
}

- (void)retrieveState
{
	XINPUT_STATE state = { 0 };

	if (XInputGetStateFuncPtr(_index, &state) != ERROR_SUCCESS)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: sizeof(state)
							    errNo: 0];

	_gamepad.northButton.value =
	_extendedGamepad.northButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_Y);
	_gamepad.southButton.value =
	_extendedGamepad.southButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_A);
	_gamepad.westButton.value =
	_extendedGamepad.westButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_X);
	_gamepad.eastButton.value =
	_extendedGamepad.eastButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_B);
	_gamepad.leftShoulderButton.value =
	_extendedGamepad.leftShoulderButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER);
	_gamepad.rightShoulderButton.value =
	_extendedGamepad.rightShoulderButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER);
	_gamepad.leftThumbstickButton.value =
	_extendedGamepad.leftThumbstickButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
	_gamepad.rightThumbstickButton.value =
	_extendedGamepad.rightThumbstickButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
	_gamepad.menuButton.value =
	_extendedGamepad.menuButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_START);
	_gamepad.optionsButton.value =
	_extendedGamepad.optionsButton.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK);
	if (OHXInputVersion != 910)
		_gamepad.homeButton.value =
		_extendedGamepad.homeButton.value =
		    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE);

	_gamepad.leftTriggerButton.value =
	_extendedGamepad.leftTriggerButton.value =
	    (float)state.Gamepad.bLeftTrigger / 255;
	_gamepad.rightTriggerButton.value =
	_extendedGamepad.rightTriggerButton.value =
	    (float)state.Gamepad.bRightTrigger / 255;

	_gamepad.leftThumbstick.xAxis.value =
	_extendedGamepad.leftThumbstick.xAxis.value =
	    (float)state.Gamepad.sThumbLX /
	    (state.Gamepad.sThumbLX < 0 ? -INT16_MIN : INT16_MAX);
	_gamepad.leftThumbstick.yAxis.value =
	_extendedGamepad.leftThumbstick.yAxis.value =
	    -(float)state.Gamepad.sThumbLY /
	    (state.Gamepad.sThumbLY < 0 ? -INT16_MIN : INT16_MAX);
	_gamepad.rightThumbstick.xAxis.value =
	_extendedGamepad.rightThumbstick.xAxis.value =
	    (float)state.Gamepad.sThumbRX /
	    (state.Gamepad.sThumbRX < 0 ? -INT16_MIN : INT16_MAX);
	_gamepad.rightThumbstick.yAxis.value =
	_extendedGamepad.rightThumbstick.yAxis.value =
	    -(float)state.Gamepad.sThumbRY /
	    (state.Gamepad.sThumbRY < 0 ? -INT16_MIN : INT16_MAX);

	_gamepad.dPad.up.value =
	_extendedGamepad.dPad.up.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP);
	_gamepad.dPad.down.value =
	_extendedGamepad.dPad.down.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
	_gamepad.dPad.left.value =
	_extendedGamepad.dPad.left.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
	_gamepad.dPad.right.value =
	_extendedGamepad.dPad.right.value =
	    !!(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
}

- (OFString *)name
{
	switch (OHXInputVersion) {
	case 14:
		return @"XInput 1.4 device";
	case 13:
		return @"XInput 1.3 device";
	case 910:
		return @"XInput 9.1.0 device";
	}

	return nil;
}

- (OHGameControllerProfile *)rawProfile
{
	return _gamepad;
	return _extendedGamepad;
}

- (OHGamepad *)gamepad
{
	return _extendedGamepad;
}
@end