ObjFW  Diff

Differences From Artifact [102a468920]:

To Artifact [ff51d408df]:


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
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFGameController.h"
#import "OFArray.h"

#import "OFSet.h"

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

#include <xinput.h>

@interface OFGameController ()
- (instancetype)of_initWithIndex: (DWORD)index OF_METHOD_FAMILY(init);
@end










static WINAPI DWORD (*XInputGetStateFuncPtr)(DWORD, XINPUT_STATE *);



@implementation OFGameController

@synthesize leftAnalogStickPosition = _leftAnalogStickPosition;
@synthesize rightAnalogStickPosition = _rightAnalogStickPosition;

+ (void)initialize
{
	HMODULE module;

	if (self != [OFGameController class])
		return;

	if ((module = LoadLibraryA("xinput1_3.dll")) != NULL)
		XInputGetStateFuncPtr =
		    (WINAPI DWORD (*)(DWORD, XINPUT_STATE *))
		    GetProcAddress(module, "XInputGetState");




}

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
{
	OFMutableArray *controllers = [OFMutableArray array];

	if (XInputGetStateFuncPtr != NULL) {







>











>
>
>
>
>
>
>
>
>

>
>


>










|



>
>
>
>







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
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFGameController.h"
#import "OFArray.h"
#import "OFNumber.h"
#import "OFSet.h"

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

#include <xinput.h>

@interface OFGameController ()
- (instancetype)of_initWithIndex: (DWORD)index OF_METHOD_FAMILY(init);
@end

struct XInputCapabilitiesEx {
	XINPUT_CAPABILITIES capabilities;
	WORD vendorID;
	WORD productID;
	WORD versionNumber;
	WORD unknown1;
	DWORD unknown2;
};

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

@implementation OFGameController
@synthesize vendorID = _vendorID, productID = _productID;
@synthesize leftAnalogStickPosition = _leftAnalogStickPosition;
@synthesize rightAnalogStickPosition = _rightAnalogStickPosition;

+ (void)initialize
{
	HMODULE module;

	if (self != [OFGameController class])
		return;

	if ((module = LoadLibraryA("xinput1_4.dll")) != NULL) {
		XInputGetStateFuncPtr =
		    (WINAPI DWORD (*)(DWORD, XINPUT_STATE *))
		    GetProcAddress(module, "XInputGetState");
		XInputGetCapabilitiesExFuncPtr = (WINAPI DWORD (*)(DWORD, DWORD,
		    DWORD, struct XInputCapabilitiesEx *))
		    GetProcAddress(module, "XInputGetCapabilitiesEx");
	}
}

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
{
	OFMutableArray *controllers = [OFMutableArray array];

	if (XInputGetStateFuncPtr != NULL) {
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
		XINPUT_STATE state = { 0 };

		if (XInputGetStateFuncPtr(index, &state) ==
		    ERROR_DEVICE_NOT_CONNECTED)
			@throw [OFInitializationFailedException exception];

		_index = index;
















		_pressedButtons = [[OFMutableSet alloc] initWithCapacity: 16];

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

	return self;
}

- (void)dealloc
{


	[_pressedButtons release];

	[super dealloc];
}

- (void)retrieveState
{







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













>
>







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
		XINPUT_STATE state = { 0 };

		if (XInputGetStateFuncPtr(index, &state) ==
		    ERROR_DEVICE_NOT_CONNECTED)
			@throw [OFInitializationFailedException exception];

		_index = index;

		if (XInputGetCapabilitiesExFuncPtr != NULL) {
			struct XInputCapabilitiesEx capabilities;

			if (XInputGetCapabilitiesExFuncPtr(1, _index,
			    XINPUT_FLAG_GAMEPAD, &capabilities) ==
			    ERROR_SUCCESS) {
				_vendorID = [[OFNumber alloc]
				    initWithUnsignedShort:
				    capabilities.vendorID];
				_productID = [[OFNumber alloc]
				    initWithUnsignedShort:
				    capabilities.productID];
			}
		}

		_pressedButtons = [[OFMutableSet alloc] initWithCapacity: 16];

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

	return self;
}

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

	[super dealloc];
}

- (void)retrieveState
{
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}

- (OFString *)name
{
	return @"XInput 1.3";
}

- (OFNumber *)vendorID
{
	return nil;
}

- (OFNumber *)productID
{
	return nil;
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerButtonA, OFGameControllerButtonB,
	    OFGameControllerButtonX, OFGameControllerButtonY,
	    OFGameControllerButtonL, OFGameControllerButtonR,
	    OFGameControllerButtonZL, OFGameControllerButtonZR,







<
<
<
<
<
<
<
<
<
<







211
212
213
214
215
216
217










218
219
220
221
222
223
224
}

- (OFString *)name
{
	return @"XInput 1.3";
}











- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerButtonA, OFGameControllerButtonB,
	    OFGameControllerButtonX, OFGameControllerButtonY,
	    OFGameControllerButtonL, OFGameControllerButtonR,
	    OFGameControllerButtonZL, OFGameControllerButtonZR,