ObjFW  Check-in [8815b72dad]

Overview
Comment:OFGameController: {vendor,product}ID on Windows
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gamecontroller
Files: files | file ages | folders
SHA3-256: 8815b72dad7d23b68068129df72f9f62f6001018e596b273f10c22100f30cb45
User & Date: js on 2024-05-12 13:07:24
Other Links: branch diff | manifest | tags
Context
2024-05-13
20:57
Move OFGameController into subframework ObjFWHID check-in: f5f12143ce user: js tags: gamecontroller
2024-05-12
13:07
OFGameController: {vendor,product}ID on Windows check-in: 8815b72dad user: js tags: gamecontroller
2024-05-11
21:56
OFGameController: {north,south,west,east}Button check-in: 59b45e87d1 user: js tags: gamecontroller
Changes

Modified src/OFGameController.h from [5a5c795ad8] to [3db166d1a7].

240
241
242
243
244
245
246

247
248
249
250
251
252
253
#elif defined(OF_NINTENDO_DS)
	OFMutableSet *_pressedButtons;
#elif defined(OF_NINTENDO_3DS)
	OFMutableSet *_pressedButtons;
	OFPoint _leftAnalogStickPosition;
#elif defined(OF_WINDOWS)
	DWORD _index;

	OFMutableSet *_pressedButtons;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	float _ZLPressure, _ZRPressure;
#endif
}

#ifdef OF_HAVE_CLASS_PROPERTIES







>







240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#elif defined(OF_NINTENDO_DS)
	OFMutableSet *_pressedButtons;
#elif defined(OF_NINTENDO_3DS)
	OFMutableSet *_pressedButtons;
	OFPoint _leftAnalogStickPosition;
#elif defined(OF_WINDOWS)
	DWORD _index;
	OFNumber *_Nullable _vendorID, *_Nullable productID;
	OFMutableSet *_pressedButtons;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	float _ZLPressure, _ZRPressure;
#endif
}

#ifdef OF_HAVE_CLASS_PROPERTIES

Modified src/platform/Windows/OFGameController.m from [102a468920] to [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,