ObjFW  Check-in [39639cd987]

Overview
Comment:Completely redesign and rewrite ObjFWHID

Right now only evdev is supported, support for others will be added back
later.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 39639cd987e8dc1a7d086ff9a2eaed68a9282c3fcd0daa5d5edb61b27f8d4d9f
User & Date: js on 2024-06-02 21:24:25
Other Links: manifest | tags
Context
2024-06-02
22:17
Fix missing import and fix typo check-in: 112d9a496f user: js tags: trunk
21:24
Completely redesign and rewrite ObjFWHID check-in: 39639cd987 user: js tags: trunk
2024-05-29
20:14
Fix forwarding-mips-elf.S check-in: 449265c5ec user: js tags: trunk
Changes

Renamed and modified src/hid/OFEvdevGameController.h [c297eb99a1] to src/hid/HIDEvdevGameController.h [448b671f74].

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
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFEvdevGameController: OFGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_keyBits;
	bool _DPadIsHAT0;
	uint16_t _vendorID, _productID;
	OFString *_name;
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_buttons;

	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
	bool _hasLeftAnalogStick, _hasRightAnalogStick;
	bool _hasLeftTriggerPressure, _hasRightTriggerPressure;
	unsigned int _leftTriggerPressureBit, _rightTriggerPressureBit;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	float _leftTriggerPressure, _rightTriggerPressure;
	unsigned int _leftAnalogStickXBit, _leftAnalogStickYBit;
	int32_t _leftAnalogStickMinX, _leftAnalogStickMaxX;
	int32_t _leftAnalogStickMinY, _leftAnalogStickMaxY;
	unsigned int _rightAnalogStickXBit, _rightAnalogStickYBit;
	int32_t _rightAnalogStickMinX, _rightAnalogStickMaxX;
	int32_t _rightAnalogStickMinY, _rightAnalogStickMaxY;
	int32_t _leftTriggerMinPressure, _leftTriggerMaxPressure;
	int32_t _rightTriggerMinPressure, _rightTriggerMaxPressure;
}

- (instancetype)of_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
- (void)of_pollState;
@end

OF_ASSUME_NONNULL_END







|



|




|
<


|
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<







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
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface HIDEvdevGameController: HIDGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;

	uint16_t _vendorID, _productID;
	OFString *_name;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *)
	    *_buttons;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *_axes;













}

- (instancetype)of_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
- (void)of_pollState;
@end

OF_ASSUME_NONNULL_END

Renamed and modified src/hid/OFEvdevGameController.m [bcc812f365] to src/hid/HIDEvdevGameController.m [d527831ab5].

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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#import "OFEvdevGameController.h"
#import "OFArray.h"

#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFSet.h"


#include <sys/ioctl.h>
#include <linux/input.h>

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"

/*
 * Controllers with tested correct mapping:
 *
 *   Microsoft X-Box 360 pad [045E:028E]
 *   Joy-Con (L) [057E:2006]
 *   Joy-Con (R) [057E:2007]
 *   N64 Controller [057E:2019]
 *   Sony Interactive Entertainment DualSense Wireless Controller [054C:0CE6]
 *   8BitDo Pro 2 Wired Controller [2DC8:3106]
 *   Stadia2SZY-0d6c [18D1:9400]
 *   Wireless Controller [054C:09CC]
 */

static const uint16_t vendorIDMicrosoft = 0x045E;
static const uint16_t vendorIDNintendo = 0x057E;
static const uint16_t vendorIDSony = 0x054C;
static const uint16_t vendorIDGoogle = 0x18D1;

/* Microsoft controllers */
static const uint16_t productIDXbox360 = 0x028E;


/* Nintendo controllers */
static const uint16_t productIDLeftJoyCon = 0x2006;
static const uint16_t productIDRightJoyCon = 0x2007;
static const uint16_t productIDN64Controller = 0x2019;

/* Sony controllers */
static const uint16_t productIDDualSense = 0x0CE6;
static const uint16_t productIDDualShock4 = 0x09CC;

/* Google controllers */
static const uint16_t productIDStadia = 0x9400;

static const uint16_t buttons[] = {
	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2,
	BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_THUMBL, BTN_THUMBR,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2


















};

static OFGameControllerButton
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDLeftJoyCon) {
		switch (button) {
		case BTN_DPAD_RIGHT:
			return OFGameControllerNorthButton;
		case BTN_DPAD_LEFT:
			return OFGameControllerSouthButton;
		case BTN_DPAD_UP:
			return OFGameControllerWestButton;
		case BTN_DPAD_DOWN:
			return OFGameControllerEastButton;


		case BTN_Z:
			return OFGameControllerCaptureButton;


		case BTN_TR:


			return @"SL";
		case BTN_TR2:
			return @"SR";
		}
	} else if (vendorID == vendorIDNintendo &&

	    productID == productIDRightJoyCon) {
		switch (button) {
		case BTN_WEST:
			return OFGameControllerNorthButton;
		case BTN_EAST:
			return OFGameControllerSouthButton;
		case BTN_SOUTH:
			return OFGameControllerWestButton;
		case BTN_NORTH:
			return OFGameControllerEastButton;
		case BTN_TL:






			return @"SL";
		case BTN_TL2:
			return @"SR";
		}

	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		switch (button) {
		case BTN_B:
			return OFGameControllerWestButton;
		case BTN_SELECT:
			return @"_C-Pad Up";
		case BTN_X:
			return @"_C-Pad Down";
		case BTN_Y:
			return @"_C-Pad Left";






		case BTN_C:
			return @"_C-Pad Right";
		case BTN_Z:
			return OFGameControllerCaptureButton;
		}
	} else if (vendorID == vendorIDSony &&

	    (productID == productIDDualSense ||
	    productID == productIDDualShock4)) {
		switch (button) {

		case BTN_NORTH:
			return OFGameControllerNorthButton;
		case BTN_WEST:
			return OFGameControllerWestButton;
		}
	} else if (vendorID == vendorIDGoogle && productID == productIDStadia) {
		switch (button) {






		case BTN_TRIGGER_HAPPY1:
			return @"Assistant";
		case BTN_TRIGGER_HAPPY2:


































			return OFGameControllerCaptureButton;
		}
	}




	switch (button) {
	case BTN_Y:
		return OFGameControllerNorthButton;
	case BTN_A:
		return OFGameControllerSouthButton;
	case BTN_X:
		return OFGameControllerWestButton;
	case BTN_B:
		return OFGameControllerEastButton;
	case BTN_TL2:
		return OFGameControllerLeftTriggerButton;
	case BTN_TR2:
		return OFGameControllerRightTriggerButton;
	case BTN_TL:
		return OFGameControllerLeftShoulderButton;
	case BTN_TR:
		return OFGameControllerRightShoulderButton;
	case BTN_THUMBL:
		return OFGameControllerLeftStickButton;
	case BTN_THUMBR:
		return OFGameControllerRightStickButton;
	case BTN_DPAD_UP:
		return OFGameControllerDPadUpButton;
	case BTN_DPAD_DOWN:
		return OFGameControllerDPadDownButton;
	case BTN_DPAD_LEFT:
		return OFGameControllerDPadLeftButton;
	case BTN_DPAD_RIGHT:
		return OFGameControllerDPadRightButton;
	case BTN_START:
		return OFGameControllerStartButton;
	case BTN_SELECT:
		return OFGameControllerSelectButton;
	case BTN_MODE:
		return OFGameControllerHomeButton;
	}





















	return nil;

}

static float
scale(float value, float min, float max)
{
	if (value < min)
		value = min;
	if (value > max)
		value = max;

	return ((value - min) / (max - min) * 2) - 1;
}

static bool
emulateRightAnalogStick(uint16_t vendorID, uint16_t productID,
    OFMutableSet *pressedButtons, OFPoint *rightAnalogStickPosition)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		if ([pressedButtons containsObject: @"_C-Pad Left"] &&
		    [pressedButtons containsObject: @"_C-Pad Right"])
			rightAnalogStickPosition->x = -0.f;
		else if ([pressedButtons containsObject: @"_C-Pad Left"])
			rightAnalogStickPosition->x = -1.f;
		else if ([pressedButtons containsObject: @"_C-Pad Right"])
			rightAnalogStickPosition->x = 1.f;
		else
			rightAnalogStickPosition->x = 0.f;

		if ([pressedButtons containsObject: @"_C-Pad Up"] &&
		    [pressedButtons containsObject: @"_C-Pad Down"])
			rightAnalogStickPosition->y = -0.f;
		else if ([pressedButtons containsObject: @"_C-Pad Up"])
			rightAnalogStickPosition->y = -1.f;
		else if ([pressedButtons containsObject: @"_C-Pad Down"])
			rightAnalogStickPosition->y = 1.f;
		else
			rightAnalogStickPosition->y = 0.f;

		return true;
	}

	return false;
}

@implementation OFEvdevGameController
@synthesize name = _name, buttons = _buttons;
@synthesize hasLeftAnalogStick = _hasLeftAnalogStick;
@synthesize hasRightAnalogStick = _hasRightAnalogStick;
@synthesize rightAnalogStickPosition = _rightAnalogStickPosition;

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

	for (OFString *device in [[OFFileManager defaultManager]
	    contentsOfDirectoryAtPath: @"/dev/input"]) {
		OFString *path;
		OFGameController *controller;

		if (![device hasPrefix: @"event"])
			continue;

		path = [@"/dev/input" stringByAppendingPathComponent: device];

		@try {
			controller = [[[OFEvdevGameController alloc]
			    of_initWithPath: path] autorelease];
		} @catch (OFOpenItemFailedException *e) {
			if (e.errNo == EACCES)
				continue;

			@throw e;
		} @catch (OFInvalidArgumentException *e) {







|

>



|
>










<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<
<
|
<
<
>
|
|
<
<
<

|
<
<
|
<
<

|



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
|

<
<
|
|
|
|
|
|
|
|
|
>
>
|
|
>
>
|
>
>
|
|
|
<
|
>
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
|
|
|
<
>
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
|
|
|
|
<
|
>
|
|
|
>
|
|
|
|
<
|
|
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
|
<
<
<
<
<
<
|
|
<
<
|
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>













<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<

|







|







|







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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
































264
265



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#import "HIDEvdevGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

#include <sys/ioctl.h>
#include <linux/input.h>

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"







@interface HIDEvdevGameControllerAxis: HIDGameControllerAxis





{




@public


	int32_t _minValue, _maxValue;
}
@end




@implementation HIDEvdevGameControllerAxis


@end



static const uint16_t buttonIDs[] = {
	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2,
	BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_THUMBL, BTN_THUMBR,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, BTN_TRIGGER_HAPPY3,
	BTN_TRIGGER_HAPPY4, BTN_TRIGGER_HAPPY5, BTN_TRIGGER_HAPPY6,
	BTN_TRIGGER_HAPPY7, BTN_TRIGGER_HAPPY8, BTN_TRIGGER_HAPPY9,
	BTN_TRIGGER_HAPPY10, BTN_TRIGGER_HAPPY11, BTN_TRIGGER_HAPPY12,
	BTN_TRIGGER_HAPPY13, BTN_TRIGGER_HAPPY14, BTN_TRIGGER_HAPPY15,
	BTN_TRIGGER_HAPPY16, BTN_TRIGGER_HAPPY17, BTN_TRIGGER_HAPPY18,
	BTN_TRIGGER_HAPPY19, BTN_TRIGGER_HAPPY20, BTN_TRIGGER_HAPPY21,
	BTN_TRIGGER_HAPPY22, BTN_TRIGGER_HAPPY23, BTN_TRIGGER_HAPPY24,
	BTN_TRIGGER_HAPPY25, BTN_TRIGGER_HAPPY26, BTN_TRIGGER_HAPPY27,
	BTN_TRIGGER_HAPPY28, BTN_TRIGGER_HAPPY29, BTN_TRIGGER_HAPPY30,
	BTN_TRIGGER_HAPPY31, BTN_TRIGGER_HAPPY32, BTN_TRIGGER_HAPPY33,
	BTN_TRIGGER_HAPPY34, BTN_TRIGGER_HAPPY35, BTN_TRIGGER_HAPPY36,
	BTN_TRIGGER_HAPPY37, BTN_TRIGGER_HAPPY38, BTN_TRIGGER_HAPPY39,
	BTN_TRIGGER_HAPPY40
};
static const uint16_t axisIDs[] = {
	ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ, ABS_THROTTLE, ABS_RUDDER,
	ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X,
	ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y, ABS_HAT3X, ABS_HAT3Y
};

static OFString *
buttonToName(uint16_t button)
{


	switch (button) {
	case BTN_A:
		return @"A";
	case BTN_B:
		return @"B";
	case BTN_C:
		return @"C";
	case BTN_X:
		return @"X";
	case BTN_Y:
		return @"Y";
	case BTN_Z:
		return @"Z";
	case BTN_TL:
		return @"TL";
	case BTN_TR:
		return @"TR";
	case BTN_TL2:
		return @"TL2";
	case BTN_TR2:
		return @"TR2";

	case BTN_SELECT:
		return @"Select";
	case BTN_START:
		return @"Start";
	case BTN_MODE:
		return @"Mode";
	case BTN_THUMBL:
		return @"Thumb L";
	case BTN_THUMBR:
		return @"Thumb R";
	case BTN_DPAD_UP:
		return @"DPad Up";
	case BTN_DPAD_DOWN:
		return @"DPad Down";
	case BTN_DPAD_LEFT:
		return @"DPad Left";
	case BTN_DPAD_RIGHT:
		return @"DPad Right";
	case BTN_TRIGGER_HAPPY1:
		return @"Trigger Happy 1";
	case BTN_TRIGGER_HAPPY2:
		return @"Trigger Happy 2";

	case BTN_TRIGGER_HAPPY3:
		return @"Trigger Happy 3";
	case BTN_TRIGGER_HAPPY4:
		return @"Trigger Happy 4";
	case BTN_TRIGGER_HAPPY5:
		return @"Trigger Happy 5";
	case BTN_TRIGGER_HAPPY6:
		return @"Trigger Happy 6";
	case BTN_TRIGGER_HAPPY7:
		return @"Trigger Happy 7";
	case BTN_TRIGGER_HAPPY8:
		return @"Trigger Happy 8";
	case BTN_TRIGGER_HAPPY9:
		return @"Trigger Happy 9";
	case BTN_TRIGGER_HAPPY10:
		return @"Trigger Happy 10";
	case BTN_TRIGGER_HAPPY11:
		return @"Trigger Happy 11";
	case BTN_TRIGGER_HAPPY12:
		return @"Trigger Happy 12";
	case BTN_TRIGGER_HAPPY13:
		return @"Trigger Happy 13";

	case BTN_TRIGGER_HAPPY14:
		return @"Trigger Happy 14";
	case BTN_TRIGGER_HAPPY15:
		return @"Trigger Happy 15";
	case BTN_TRIGGER_HAPPY16:
		return @"Trigger Happy 16";
	case BTN_TRIGGER_HAPPY17:
		return @"Trigger Happy 17";
	case BTN_TRIGGER_HAPPY18:
		return @"Trigger Happy 18";

	case BTN_TRIGGER_HAPPY19:
		return @"Trigger Happy 19";
	case BTN_TRIGGER_HAPPY20:
		return @"Trigger Happy 20";
	case BTN_TRIGGER_HAPPY21:
		return @"Trigger Happy 21";
	case BTN_TRIGGER_HAPPY22:
		return @"Trigger Happy 22";
	case BTN_TRIGGER_HAPPY23:
		return @"Trigger Happy 23";
	case BTN_TRIGGER_HAPPY24:
		return @"Trigger Happy 24";
	case BTN_TRIGGER_HAPPY25:
		return @"Trigger Happy 25";
	case BTN_TRIGGER_HAPPY26:
		return @"Trigger Happy 26";
	case BTN_TRIGGER_HAPPY27:
		return @"Trigger Happy 27";
	case BTN_TRIGGER_HAPPY28:
		return @"Trigger Happy 28";
	case BTN_TRIGGER_HAPPY29:
		return @"Trigger Happy 29";
	case BTN_TRIGGER_HAPPY30:
		return @"Trigger Happy 30";
	case BTN_TRIGGER_HAPPY31:
		return @"Trigger Happy 31";
	case BTN_TRIGGER_HAPPY32:
		return @"Trigger Happy 32";
	case BTN_TRIGGER_HAPPY33:
		return @"Trigger Happy 33";
	case BTN_TRIGGER_HAPPY34:
		return @"Trigger Happy 34";
	case BTN_TRIGGER_HAPPY35:
		return @"Trigger Happy 35";
	case BTN_TRIGGER_HAPPY36:
		return @"Trigger Happy 36";
	case BTN_TRIGGER_HAPPY37:
		return @"Trigger Happy 37";
	case BTN_TRIGGER_HAPPY38:
		return @"Trigger Happy 38";
	case BTN_TRIGGER_HAPPY39:
		return @"Trigger Happy 39";
	case BTN_TRIGGER_HAPPY40:
		return @"Trigger Happy 40";
	default:
		return nil;
	}
}

static OFString *
axisToName(uint16_t axis)
{
	switch (axis) {
	case ABS_X:
		return @"X";
	case ABS_Y:
		return @"Y";
	case ABS_Z:
		return @"Z";
	case ABS_RX:
		return @"RX";
	case ABS_RY:
		return @"RY";
	case ABS_RZ:
		return @"RZ";
	case ABS_THROTTLE:
		return @"Throttle";
	case ABS_RUDDER:






		return @"Rudder";






	case ABS_WHEEL:
		return @"Wheel";


	case ABS_GAS:



		return @"Gas";
	case ABS_BRAKE:
		return @"Brake";
	case ABS_HAT0X:
		return @"HAT0X";
	case ABS_HAT0Y:
		return @"HAT0Y";
	case ABS_HAT1X:
		return @"HAT1X";
	case ABS_HAT1Y:
		return @"HAT1Y";
	case ABS_HAT2X:
		return @"HAT2X";
	case ABS_HAT2Y:
		return @"HAT2Y";
	case ABS_HAT3X:
		return @"HAT3X";
	case ABS_HAT3Y:
		return @"HAT3Y";
	default:
		return nil;
	}
}

static float
scale(float value, float min, float max)
{
	if (value < min)
		value = min;
	if (value > max)
		value = max;

	return ((value - min) / (max - min) * 2) - 1;
}

































@implementation HIDEvdevGameController
@synthesize name = _name, buttons = _buttons, axes = _axes;




+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
	OFMutableArray *controllers = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();

	for (OFString *device in [[OFFileManager defaultManager]
	    contentsOfDirectoryAtPath: @"/dev/input"]) {
		OFString *path;
		HIDGameController *controller;

		if (![device hasPrefix: @"event"])
			continue;

		path = [@"/dev/input" stringByAppendingPathComponent: device];

		@try {
			controller = [[[HIDEvdevGameController alloc]
			    of_initWithPath: path] autorelease];
		} @catch (OFOpenItemFailedException *e) {
			if (e.errNo == EACCES)
				continue;

			@throw e;
		} @catch (OFInvalidArgumentException *e) {
280
281
282
283
284
285
286
287
288
289
290
291
292

293
294
295
296
297
298
299
300
301
302



303


304
305
306
307
308
309
310
311
312
313

- (instancetype)of_initWithPath: (OFString *)path
{
	self = [super init];

	@try {
		OFStringEncoding encoding = [OFLocale encoding];
		unsigned long evBits[OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    EV_MAX) / OF_ULONG_BIT] = { 0 };
		unsigned long absBits[OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    ABS_MAX) / OF_ULONG_BIT] = { 0 };
		struct input_id inputID;
		char name[128];


		_path = [path copy];

		if ((_fd = open([_path cStringWithEncoding: encoding],
		    O_RDONLY | O_NONBLOCK)) == -1)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: _path
					 mode: @"r"
					errNo: errno];




		if (ioctl(_fd, EVIOCGBIT(0, sizeof(evBits)), evBits) == -1)


			@throw [OFInitializationFailedException exception];

		if (!OFBitSetIsSet(evBits, EV_KEY))
			@throw [OFInvalidArgumentException exception];

		_keyBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    KEY_MAX) / OF_ULONG_BIT, sizeof(unsigned long));

		if (ioctl(_fd, EVIOCGBIT(EV_KEY, OFRoundUpToPowerOf2(
		    OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT *







<
<
<
<


>










>
>
>
|
>
>


|







305
306
307
308
309
310
311




312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

- (instancetype)of_initWithPath: (OFString *)path
{
	self = [super init];

	@try {
		OFStringEncoding encoding = [OFLocale encoding];




		struct input_id inputID;
		char name[128];
		OFMutableDictionary *buttons, *axes;

		_path = [path copy];

		if ((_fd = open([_path cStringWithEncoding: encoding],
		    O_RDONLY | O_NONBLOCK)) == -1)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: _path
					 mode: @"r"
					errNo: errno];

		_evBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    EV_MAX) / OF_ULONG_BIT, sizeof(unsigned long));

		if (ioctl(_fd, EVIOCGBIT(0, OFRoundUpToPowerOf2(
		    OF_ULONG_BIT, EV_MAX) / OF_ULONG_BIT *
		    sizeof(unsigned long)), _evBits) == -1)
			@throw [OFInitializationFailedException exception];

		if (!OFBitSetIsSet(_evBits, EV_KEY))
			@throw [OFInvalidArgumentException exception];

		_keyBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    KEY_MAX) / OF_ULONG_BIT, sizeof(unsigned long));

		if (ioctl(_fd, EVIOCGBIT(EV_KEY, OFRoundUpToPowerOf2(
		    OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT *
326
327
328
329
330
331
332

333
334
335
336

337
338



339
340
341
342

343
344
345
346


347




348

349
350
351
352
353
354

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451

452

453
454
455
456
457
458
459
460
461
462
463

		if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1)
			@throw [OFInitializationFailedException exception];

		_name = [[OFString alloc] initWithCString: name
						 encoding: encoding];


		_buttons = [[OFMutableSet alloc] init];
		for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons);
		    i++) {
			if (OFBitSetIsSet(_keyBits, buttons[i])) {

				OFGameControllerButton button = buttonToName(
				    buttons[i], _vendorID, _productID);




				if (button != nil && ![button hasPrefix: @"_"])
					[_buttons addObject: button];
			}

		}

		_pressedButtons = [[OFMutableSet alloc] init];



		if (OFBitSetIsSet(evBits, EV_ABS)) {




			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),

			    absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (_vendorID == vendorIDNintendo &&
			    _productID == productIDRightJoyCon) {

				/*
				 * Make the right analog stick on the right
				 * Joy-Con the left analog stick so that it can
				 * be used as a single controller.
				 */
				_leftAnalogStickXBit = ABS_RX;
				_leftAnalogStickYBit = ABS_RY;
				_rightAnalogStickXBit = ABS_X;
				_rightAnalogStickYBit = ABS_Y;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			} else if (_vendorID == vendorIDGoogle &&
			    _productID == productIDStadia) {
				/*
				 * It's unclear how this can be screwed up
				 * *this* bad.
				 */
				_leftAnalogStickXBit = ABS_X;
				_leftAnalogStickYBit = ABS_Y;
				_rightAnalogStickXBit = ABS_Z;
				_rightAnalogStickYBit = ABS_RZ;
				_leftTriggerPressureBit = ABS_BRAKE;
				_rightTriggerPressureBit = ABS_GAS;
			} else {
				_leftAnalogStickXBit = ABS_X;
				_leftAnalogStickYBit = ABS_Y;
				_rightAnalogStickXBit = ABS_RX;
				_rightAnalogStickYBit = ABS_RY;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			}

			if (OFBitSetIsSet(absBits, _leftAnalogStickXBit) &&
			    OFBitSetIsSet(absBits, _leftAnalogStickYBit))
				_hasLeftAnalogStick = true;

			if (OFBitSetIsSet(absBits, _rightAnalogStickXBit) &&
			    OFBitSetIsSet(absBits, _rightAnalogStickYBit))
				_hasRightAnalogStick = true;

			if (_vendorID == vendorIDNintendo &&
			    _productID == productIDN64Controller &&
			    OFBitSetIsSet(_keyBits, BTN_Y) &&
			    OFBitSetIsSet(_keyBits, BTN_C) &&
			    OFBitSetIsSet(_keyBits, BTN_SELECT) &&
			    OFBitSetIsSet(_keyBits, BTN_X))
				_hasRightAnalogStick = true;

			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				_DPadIsHAT0 = true;

				[_buttons addObject:
				    OFGameControllerDPadLeftButton];
				[_buttons addObject:
				    OFGameControllerDPadRightButton];
				[_buttons addObject:
				    OFGameControllerDPadUpButton];
				[_buttons addObject:
				    OFGameControllerDPadDownButton];
			}

			if (OFBitSetIsSet(absBits, _leftTriggerPressureBit)) {
				_hasLeftTriggerPressure = true;
				[_buttons addObject:
				    OFGameControllerLeftTriggerButton];
			}

			if (OFBitSetIsSet(absBits, _rightTriggerPressureBit)) {
				_hasRightTriggerPressure = true;
				[_buttons addObject:
				    OFGameControllerRightTriggerButton];
			}
		}

		[_buttons makeImmutable];

		@try {
			[self of_pollState];
		} @catch (OFReadFailedException *e) {
			@throw [OFInitializationFailedException exception];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];

	if (_fd != -1)
		close(_fd);


	OFFreeMemory(_keyBits);


	[_name release];
	[_buttons release];
	[_pressedButtons release];

	[super dealloc];
}

- (OFNumber *)vendorID
{
	return [OFNumber numberWithUnsignedShort: _vendorID];







>
|
|

|
>
|
|
>
>
>

|
|
|
>
|
|
|

>
>
|
>
>
>
>
|
>
|



|
|
>
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
|
<
<
<
|
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
|
<
<
<
|
<
<
|
<
|
|
<
<
<
<
|
|
|
|

<
|
<
<
<















>

>



|







353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396


397


























398
399



400



401







402









403



404


405

406
407




408
409
410
411
412

413



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

		if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1)
			@throw [OFInitializationFailedException exception];

		_name = [[OFString alloc] initWithCString: name
						 encoding: encoding];

		buttons = [[OFMutableDictionary alloc] init];
		_buttons = buttons;
		for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
		    i++) {
			if (OFBitSetIsSet(_keyBits, buttonIDs[i])) {
				OFString *buttonName;
				HIDGameControllerButton *button;

				buttonName = buttonToName(buttonIDs[i]);
				if (buttonName == nil)
					continue;

				button = [[[HIDGameControllerButton alloc]
				    initWithName: buttonName] autorelease];

				[buttons setObject: button forKey: buttonName];
			}
		}
		[buttons makeImmutable];

		axes = [[OFMutableDictionary alloc] init];
		_axes = axes;
		if (OFBitSetIsSet(_evBits, EV_ABS)) {
			_absBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT,
			    sizeof(unsigned long));

			if (ioctl(_fd, EVIOCGBIT(EV_ABS, OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT *
			    sizeof(unsigned long)), _absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			for (size_t i = 0;
			    i < sizeof(axisIDs) / sizeof(*axisIDs); i++) {
				if (OFBitSetIsSet(_absBits, axisIDs[i])) {
					OFString *axisName;


					HIDEvdevGameControllerAxis *axis;



























					axisName = axisToName(axisIDs[i]);



					if (axisName == nil)



						continue;

















					axis = [[[HIDEvdevGameControllerAxis



					    alloc] initWithName: axisName]


					    autorelease];


					[axes setObject: axis forKey: axisName];




				}
			}
		}
		[axes makeImmutable];


		[self of_pollState];



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

	return self;
}

- (void)dealloc
{
	[_path release];

	if (_fd != -1)
		close(_fd);

	OFFreeMemory(_evBits);
	OFFreeMemory(_keyBits);
	OFFreeMemory(_absBits);

	[_name release];
	[_buttons release];
	[_axes release];

	[super dealloc];
}

- (OFNumber *)vendorID
{
	return [OFNumber numberWithUnsignedShort: _vendorID];
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

553


554
555
556

557
558
559
560

561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

615

616
617
618
619
620
621
622

	if (ioctl(_fd, EVIOCGKEY(sizeof(keyState)), &keyState) == -1)
		@throw [OFReadFailedException
		    exceptionWithObject: self
			requestedLength: sizeof(keyState)
				  errNo: errno];

	[_pressedButtons removeAllObjects];

	for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons);
	    i++) {
		if (OFBitSetIsSet(_keyBits, buttons[i]) &&
		    OFBitSetIsSet(keyState, buttons[i])) {
			OFGameControllerButton button = buttonToName(
			    buttons[i], _vendorID, _productID);

			if (button != nil)
				[_pressedButtons addObject: button];
		}
	}

	if (_DPadIsHAT0) {
		struct input_absinfo infoX, infoY;

		if (ioctl(_fd, EVIOCGABS(ABS_HAT0X), &infoX) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoX)
					  errNo: errno];

		if (ioctl(_fd, EVIOCGABS(ABS_HAT0Y), &infoY) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

		if (infoX.value < 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadLeftButton];
		else if (infoX.value > 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadRightButton];

		if (infoY.value < 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadUpButton];
		else if (infoY.value > 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadDownButton];
	}

	if (_hasLeftAnalogStick) {
		struct input_absinfo infoX, infoY;

		if (ioctl(_fd, EVIOCGABS(_leftAnalogStickXBit), &infoX) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoX)
					  errNo: errno];

		if (ioctl(_fd, EVIOCGABS(_leftAnalogStickYBit), &infoY) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

		_leftAnalogStickMinX = infoX.minimum;
		_leftAnalogStickMaxX = infoX.maximum;
		_leftAnalogStickMinY = infoY.minimum;
		_leftAnalogStickMaxY = infoY.maximum;
		_leftAnalogStickPosition.x = scale(infoX.value,
		    _leftAnalogStickMinX, _leftAnalogStickMaxX);
		_leftAnalogStickPosition.y = scale(infoY.value,
		    _leftAnalogStickMinY, _leftAnalogStickMaxY);
	}

	if (!emulateRightAnalogStick(_vendorID, _productID, _pressedButtons,
	    &_rightAnalogStickPosition) && _hasRightAnalogStick) {

		struct input_absinfo infoX, infoY;



		if (ioctl(_fd, EVIOCGABS(_rightAnalogStickXBit), &infoX) == -1)
			@throw [OFReadFailedException

			    exceptionWithObject: self
				requestedLength: sizeof(infoX)
					  errNo: errno];


		if (ioctl(_fd, EVIOCGABS(_rightAnalogStickYBit), &infoY) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

		_rightAnalogStickMinX = infoX.minimum;
		_rightAnalogStickMaxX = infoX.maximum;
		_rightAnalogStickMinY = infoY.minimum;
		_rightAnalogStickMaxY = infoY.maximum;
		_rightAnalogStickPosition.x = scale(infoX.value,
		    _rightAnalogStickMinX, _rightAnalogStickMaxX);
		_rightAnalogStickPosition.y = scale(infoY.value,
		    _rightAnalogStickMinY, _rightAnalogStickMaxY);
	}


	if (_hasLeftTriggerPressure) {
		struct input_absinfo info;

		if (ioctl(_fd, EVIOCGABS( _leftTriggerPressureBit), &info) ==
		    -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(info)
					  errNo: errno];

		_leftTriggerMinPressure = info.minimum;
		_leftTriggerMaxPressure = info.maximum;
		_leftTriggerPressure = scale(info.value,
		    _leftTriggerMinPressure, _leftTriggerMaxPressure);
	}

	if (_hasRightTriggerPressure) {
		struct input_absinfo info;

		if (ioctl(_fd, EVIOCGABS(_rightTriggerPressureBit), &info) ==
		    -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(info)
					  errNo: errno];

		_rightTriggerMinPressure = info.minimum;
		_rightTriggerMaxPressure = info.maximum;
		_rightTriggerPressure = scale(info.value,
		    _rightTriggerMinPressure, _rightTriggerMaxPressure);
	}
}

- (void)retrieveState
{
	struct input_event event;

	for (;;) {

		OFGameControllerButton button;


		errno = 0;

		if (read(_fd, &event, sizeof(event)) < (int)sizeof(event)) {
			if (errno == EWOULDBLOCK)
				return;








<
<
|

<
<
<
<
|
|
<
|
<
|
<
<
|
<
<
<
<
<

<
<
<
<
<
|
|
<
|
<
<
<

<
|
<
<
|
|
|
|
<
<
|
<
<
<
<
<

<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
|
<
<
>
|
>
>

<
<
>
|
<
<

>
|
<
<
<
<
|
<
<
<
<
<
<
<
<
|
|
>
|
|

|
<
|
|
|
|

|
|
|
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<








>
|
>







454
455
456
457
458
459
460


461
462




463
464

465

466


467





468





469
470

471



472

473


474
475
476
477


478





479





480









481


482
483
484
485
486


487
488


489
490
491




492








493
494
495
496
497
498
499

500
501
502
503
504
505
506
507


508


509











510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527

	if (ioctl(_fd, EVIOCGKEY(sizeof(keyState)), &keyState) == -1)
		@throw [OFReadFailedException
		    exceptionWithObject: self
			requestedLength: sizeof(keyState)
				  errNo: errno];



	for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
	    i++) {




		OFString *name;
		HIDGameControllerButton *button;



		if (!OFBitSetIsSet(_keyBits, buttonIDs[i]))


			continue;











		name = buttonToName(buttonIDs[i]);
		if (name == nil)

			continue;





		button = [_buttons objectForKey: name];


		if (button == nil)
			continue;

		button.value = (OFBitSetIsSet(keyState, buttonIDs[i]) ? 1 : 0);


	}











	if (OFBitSetIsSet(_evBits, EV_ABS)) {









		for (size_t i = 0; i < sizeof(axisIDs) / sizeof(*axisIDs);


		    i++) {
			struct input_absinfo info;
			OFString *name;
			HIDEvdevGameControllerAxis *axis;



			if (!OFBitSetIsSet(_absBits, axisIDs[i]))
				continue;



			name = axisToName(axisIDs[i]);
			if (name == nil)




				continue;









			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];
			if (axis == nil)
				continue;

			if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)

				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: sizeof(info)
						  errNo: errno];

			axis->_minValue = info.minimum;
			axis->_maxValue = info.maximum;
			axis.value = scale(info.value,


			    info.minimum, info.maximum);


		}











	}
}

- (void)retrieveState
{
	struct input_event event;

	for (;;) {
		OFString *name;
		HIDGameControllerButton *button;
		HIDEvdevGameControllerAxis *axis;

		errno = 0;

		if (read(_fd, &event, sizeof(event)) < (int)sizeof(event)) {
			if (errno == EWOULDBLOCK)
				return;

639
640
641
642
643
644
645
646
647
648
649
650
651

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
		case EV_SYN:
			if (event.value == SYN_DROPPED) {
				_discardUntilReport = true;
				continue;
			}
			break;
		case EV_KEY:
			if ((button = buttonToName(event.code, _vendorID,
			    _productID)) != nil) {
				if (event.value)
					[_pressedButtons addObject: button];
				else
					[_pressedButtons removeObject: button];


				emulateRightAnalogStick(_vendorID, _productID,
				    _pressedButtons,
				    &_rightAnalogStickPosition);
			}
			break;
		case EV_ABS:
			if (event.code == _leftAnalogStickXBit)
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);
			else if (event.code == _leftAnalogStickYBit)
				_leftAnalogStickPosition.y = scale(event.value,
				    _leftAnalogStickMinY, _leftAnalogStickMaxY);
			else if (event.code == _rightAnalogStickXBit)
				_rightAnalogStickPosition.x = scale(event.value,
				    _rightAnalogStickMinX,
				    _rightAnalogStickMaxX);
			else if (event.code == _rightAnalogStickYBit)
				_rightAnalogStickPosition.y = scale(event.value,
				    _rightAnalogStickMinY,
				    _rightAnalogStickMaxY);
			else if (event.code == ABS_HAT0X) {
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadRightButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadRightButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadRightButton];
				}
			} else if (event.code == ABS_HAT0Y) {
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadDownButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadDownButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadDownButton];
				}
			} else if (event.code == _leftTriggerPressureBit) {
				_leftTriggerPressure = scale(event.value,
				    _leftTriggerMinPressure,
				    _leftTriggerMaxPressure);

				if (_leftTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerLeftTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerLeftTriggerButton];
			} else if (event.code == _rightTriggerPressureBit) {
				_rightTriggerPressure = scale(event.value,
				    _rightTriggerMinPressure,
				    _rightTriggerMaxPressure);

				if (_rightTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerRightTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerRightTriggerButton];
			}
			break;
		}
	}
}

- (OFComparisonResult)compare: (OFEvdevGameController *)otherController
{
	unsigned long long selfIndex, otherIndex;

	if (![otherController isKindOfClass: [OFEvdevGameController class]])
		@throw [OFInvalidArgumentException exception];

	selfIndex = [_path substringFromIndex: 16].unsignedLongLongValue;
	otherIndex = [otherController->_path substringFromIndex: 16]
	    .unsignedLongLongValue;

	if (selfIndex > otherIndex)
		return OFOrderedDescending;
	if (selfIndex < otherIndex)
		return OFOrderedAscending;

	return OFOrderedSame;
}

- (OFSet *)pressedButtons
{
	OFMutableSet *pressedButtons =
	    [OFMutableSet setWithCapacity: _pressedButtons.count];

	for (OFGameControllerButton button in _pressedButtons)
		if (![button hasPrefix: @"_"])
			[pressedButtons addObject: button];

	[pressedButtons makeImmutable];

	return pressedButtons;
}

- (OFPoint)leftAnalogStickPosition
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoyCon)
		return OFMakePoint(
		    _leftAnalogStickPosition.y, -_leftAnalogStickPosition.x);
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoyCon)
		return OFMakePoint(
		    -_leftAnalogStickPosition.y, _leftAnalogStickPosition.x);

	return _leftAnalogStickPosition;
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	if (button == OFGameControllerLeftTriggerButton &&
	    _hasLeftTriggerPressure)
		return _leftTriggerPressure;
	if (button == OFGameControllerRightTriggerButton &&
	    _hasRightTriggerPressure)
		return _rightTriggerPressure;

	return [super pressureForButton: button];
}
@end







|
|
|
|
|
|
>

<
|
<
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
|
<
|
<
<
<
<
<
<
<
|
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<





|



|













<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

559

560
561
562














563
564

565
566















567
568

569

570







571

572


573










574







575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597






































598
		case EV_SYN:
			if (event.value == SYN_DROPPED) {
				_discardUntilReport = true;
				continue;
			}
			break;
		case EV_KEY:
			name = buttonToName(event.code);
			if (name == nil)
				continue;

			button = [_buttons objectForKey: name];
			if (button == nil)
				continue;


			button.value = (event.value ? 1 : 0);


			break;
		case EV_ABS:














			name = axisToName(event.code);
			if (name == nil)

				continue;
















			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];

			if (axis == nil)

				continue;









			axis.value = scale(event.value,


			   axis->_minValue, axis->_maxValue);


















			break;
		}
	}
}

- (OFComparisonResult)compare: (HIDEvdevGameController *)otherController
{
	unsigned long long selfIndex, otherIndex;

	if (![otherController isKindOfClass: [HIDEvdevGameController class]])
		@throw [OFInvalidArgumentException exception];

	selfIndex = [_path substringFromIndex: 16].unsignedLongLongValue;
	otherIndex = [otherController->_path substringFromIndex: 16]
	    .unsignedLongLongValue;

	if (selfIndex > otherIndex)
		return OFOrderedDescending;
	if (selfIndex < otherIndex)
		return OFOrderedAscending;

	return OFOrderedSame;
}






































@end

Renamed and modified src/hid/OFGameController.h [3ca0d701fd] to src/hid/HIDGameController.h [347fbd7215].

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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

/** @file */

@class OFArray OF_GENERIC(ObjectType);
@class OFMutableSet OF_GENERIC(ObjectType);
@class OFNumber;
@class OFSet OF_GENERIC(ObjectType);

/**
 * @brief A button on a controller.
 *
 * Possible values are:
 *
 *  * @ref OFGameControllerNorthButton
 *  * @ref OFGameControllerSouthButton
 *  * @ref OFGameControllerWestButton
 *  * @ref OFGameControllerEastButton
 *  * @ref OFGameControllerLeftTriggerButton
 *  * @ref OFGameControllerRightTriggerButton
 *  * @ref OFGameControllerLeftShoulderButton
 *  * @ref OFGameControllerRightShoulderButton
 *  * @ref OFGameControllerLeftStickButton
 *  * @ref OFGameControllerRightStickButton
 *  * @ref OFGameControllerDPadUpButton
 *  * @ref OFGameControllerDPadDownButton
 *  * @ref OFGameControllerDPadLeftButton
 *  * @ref OFGameControllerDPadRightButton
 *  * @ref OFGameControllerStartButton
 *  * @ref OFGameControllerSelectButton
 *  * @ref OFGameControllerHomeButton
 *  * @ref OFGameControllerCaptureButton
 */
typedef OFConstantString *OFGameControllerButton;

#ifdef __cplusplus
extern "C" {
#endif
/**
 * @brief The north button on a game controller's diamond pad.
 */
extern const OFGameControllerButton OFGameControllerNorthButton;

/**
 * @brief The south button on a game controller's diamond pad.
 */
extern const OFGameControllerButton OFGameControllerSouthButton;

/**
 * @brief The west button on a game controller's diamond pad.
 */
extern const OFGameControllerButton OFGameControllerWestButton;

/**
 * @brief The east button on a game controller's diamond pad.
 */
extern const OFGameControllerButton OFGameControllerEastButton;

/**
 * @brief The left trigger button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerLeftTriggerButton;

/**
 * @brief The right trigger button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerRightTriggerButton;

/**
 * @brief The left shoulder button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerLeftShoulderButton;

/**
 * @brief The right shoulder button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerRightShoulderButton;

/**
 * @brief The left stick button (pressing the left stick) on a game controller.
 */
extern const OFGameControllerButton OFGameControllerLeftStickButton;

/**
 * @brief The right stick button (pressing the right stick) on a game
 *	  controller.
 */
extern const OFGameControllerButton OFGameControllerRightStickButton;

/**
 * @brief The D-Pad Up button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerDPadUpButton;

/**
 * @brief The D-Pad Down button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerDPadDownButton;

/**
 * @brief The D-Pad Left button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerDPadLeftButton;

/**
 * @brief The D-Pad Right button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerDPadRightButton;

/**
 * @brief The Start button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerStartButton;

/**
 * @brief The Select button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerSelectButton;

/**
 * @brief The Home button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerHomeButton;

/**
 * @brief The Capture button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerCaptureButton;
#ifdef __cplusplus
}
#endif

/**
 * @brief A class for reading state from a game controller.
 */
@interface OFGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <OFGameController *> *controllers;
#endif

/**
 * @brief The name of the controller.
 */
@property (readonly, nonatomic, copy) OFString *name;

/**
 * @brief The vendor ID of the controller or `nil` if unavailable.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *vendorID;

/**
 * @brief The product ID of the controller or `nil` if unavailable.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *productID;

/**
 * @brief The buttons the controller has.
 */
@property (readonly, nonatomic, copy)
    OFSet OF_GENERIC(OFGameControllerButton) *buttons;

/**
 * @brief The currently pressed buttons on the controller.
 */
@property (readonly, nonatomic, copy)
    OFSet OF_GENERIC(OFGameControllerButton) *pressedButtons;

/**
 * @brief Whether the controller has a left analog stick.
 */
@property (readonly, nonatomic) bool hasLeftAnalogStick;

/**
 * @brief The position of the left analog stick.
 *
 * The range is from (-1, -1) to (1, 1).
 */
@property (readonly, nonatomic) OFPoint leftAnalogStickPosition;

/**
 * @brief Whether the controller has a right analog stick.
 *
 * @note The Nintendo 64 controller has no right analog stick. However, the C
 *	 buttons are used to emulate one. As C buttons allow pressing buttons
 *	 for two opposite directions at the same time while an analog stick
 *	 does not allow for this, the value -0.0 is used instead of 0.0 to
 *	 indicate both opposite directions are pressed at the same time.
 */
@property (readonly, nonatomic) bool hasRightAnalogStick;

/**
 * @brief The position of the right analog stick.
 *
 * The range is from (-1, -1) to (1, 1).
 *
 * @note The Nintendo 64 controller has no right analog stick, however, the C
 *	 buttons are used to emulate one.
 */
@property (readonly, nonatomic) OFPoint rightAnalogStickPosition;

/**
 * @brief Returns the available controllers.
 *
 * @return The available controllers
 */
+ (OFArray OF_GENERIC(OFGameController *) *)controllers;

/**
 * @brief Retrieve the current state from the game controller.
 *
 * The state returned by @ref OFGameController's messages does not change until
 * this method is called.
 *
 * @throw OFReadFailedException The controller's state could not be read
 */
- (void)retrieveState;

/**
 * @brief Returns how hard the specified button is pressed.
 *
 * The returned value is in the range from 0 to 1.
 *
 * @param button The button for which to return how hard it is pressed.
 * @return How hard the specified button is pressed
 */
- (float)pressureForButton: (OFGameControllerButton)button;
@end

OF_ASSUME_NONNULL_END







|
|

|

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|


|


















<
<
<
|
<
<
<

|
<
|
<
<
<
<


<
<
<
<
<
|
<
<
<
<
<
<
<
<

|
|
<
<
|
<
<
<
<
<
<
<





|


|

|





<
<
<
<
<
<
<
<
<
<



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
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerAxis;
@class HIDGameControllerButton;
@class OFArray OF_GENERIC(ObjectType);
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;





























































































































/**
 * @brief A class for reading state from a game controller.
 */
@interface HIDGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <HIDGameController *> *controllers;
#endif

/**
 * @brief The name of the controller.
 */
@property (readonly, nonatomic, copy) OFString *name;

/**
 * @brief The vendor ID of the controller or `nil` if unavailable.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *vendorID;

/**
 * @brief The product ID of the controller or `nil` if unavailable.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *productID;

/**



 * @brief A map of all button names to their @ref HIDGameControllerButton.



 */
@property (readonly, nonatomic)

    OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *) *buttons;





/**





 * @brief A map of all axis names to their @ref HIDGameControllerAxis.








 */
@property (readonly, nonatomic)
    OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *axes;










/**
 * @brief Returns the available controllers.
 *
 * @return The available controllers
 */
+ (OFArray OF_GENERIC(HIDGameController *) *)controllers;

/**
 * @brief Retrieves the current state from the game controller.
 *
 * The state returned by @ref HIDGameController's methods does not change until
 * this method is called.
 *
 * @throw OFReadFailedException The controller's state could not be read
 */
- (void)retrieveState;










@end

OF_ASSUME_NONNULL_END

Renamed and modified src/hid/OFGameController.m [49ccebbc34] to src/hid/HIDGameController.m [f3d0e406e6].

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
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "OFEvdevGameController.h"
#endif
#ifdef OF_WINDOWS
# include "OFXInputGameController.h"
#endif
#ifdef OF_WII
# include "OFWiiGameController.h"
#endif
#ifdef OF_NINTENDO_DS
# include "OFNintendoDSGameController.h"
#endif
#ifdef OF_NINTENDO_3DS
# include "OFNintendo3DSGameController.h"
#endif

const OFGameControllerButton OFGameControllerNorthButton = @"North";
const OFGameControllerButton OFGameControllerSouthButton = @"South";
const OFGameControllerButton OFGameControllerWestButton = @"West";
const OFGameControllerButton OFGameControllerEastButton = @"East";
const OFGameControllerButton OFGameControllerLeftTriggerButton =
    @"Left Trigger";
const OFGameControllerButton OFGameControllerRightTriggerButton =
    @"Right Trigger";
const OFGameControllerButton OFGameControllerLeftShoulderButton =
    @"Left Shoulder";
const OFGameControllerButton OFGameControllerRightShoulderButton =
    @"Right Shoulder";
const OFGameControllerButton OFGameControllerLeftStickButton = @"Left Stick";
const OFGameControllerButton OFGameControllerRightStickButton = @"Right Stick";
const OFGameControllerButton OFGameControllerDPadUpButton = @"D-Pad Up";
const OFGameControllerButton OFGameControllerDPadDownButton = @"D-Pad Down";
const OFGameControllerButton OFGameControllerDPadLeftButton = @"D-Pad Left";
const OFGameControllerButton OFGameControllerDPadRightButton = @"D-Pad Right";
const OFGameControllerButton OFGameControllerStartButton = @"Start";
const OFGameControllerButton OFGameControllerSelectButton = @"Select";
const OFGameControllerButton OFGameControllerHomeButton = @"Home";
const OFGameControllerButton OFGameControllerCaptureButton = @"Capture";

@implementation OFGameController
@dynamic name, buttons, pressedButtons, hasLeftAnalogStick;
@dynamic leftAnalogStickPosition, hasRightAnalogStick, rightAnalogStickPosition;

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
{
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
	return [OFEvdevGameController controllers];
#elif defined(OF_WINDOWS)
	return [OFXInputGameController controllers];
#elif defined(OF_WII)
	return [OFWiiGameController controllers];
#elif defined(OF_NINTENDO_DS)
	return [OFNintendoDSGameController controllers];
#elif defined(OF_NINTENDO_3DS)
	return [OFNintendo3DSGameController controllers];
#else
	return [OFArray array];
#endif
}

- (instancetype)init
{
	if ([self isMemberOfClass: [OFGameController class]]) {
		@try {
			[self doesNotRecognizeSelector: _cmd];
		} @catch (id e) {
			[self release];
			@throw e;
		}








|





|

<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<

|


|
<
<
<
<
<
<
<
<







|







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
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "HIDEvdevGameController.h"
#endif




































@implementation HIDGameController
@dynamic name, buttons, axes;


+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
	return [HIDEvdevGameController controllers];








#else
	return [OFArray array];
#endif
}

- (instancetype)init
{
	if ([self isMemberOfClass: [HIDGameController class]]) {
		@try {
			[self doesNotRecognizeSelector: _cmd];
		} @catch (id e) {
			[self release];
			@throw e;
		}

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
}

- (void)retrieveState
{
	OF_UNRECOGNIZED_SELECTOR
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	return ([self.pressedButtons containsObject: button] ? 1 : 0);
}

- (OFString *)description
{
	if (self.vendorID != nil && self.productID != nil)
		return [OFString stringWithFormat:
		    @"<%@: %@ [%04X:%04X]>",
		    self.class, self.name, self.vendorID.unsignedShortValue,
		    self.productID.unsignedShortValue];
	else
		return [OFString stringWithFormat: @"<%@: %@>",
						   self.class, self.name];
}
@end

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "OFEvdevGameController.m"
#endif
#ifdef OF_WINDOWS
# include "OFXInputGameController.m"
#endif
#ifdef OF_WII
# include "OFWiiGameController.m"
#endif
#ifdef OF_NINTENDO_DS
# include "OFNintendoDSGameController.m"
#endif
#ifdef OF_NINTENDO_3DS
# include "OFNintendo3DSGameController.m"
#endif







<
<
<
<
<














|

<
<
<
<
<
<
<
<
<
<
<
<
67
68
69
70
71
72
73





74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89












}

- (void)retrieveState
{
	OF_UNRECOGNIZED_SELECTOR
}






- (OFString *)description
{
	if (self.vendorID != nil && self.productID != nil)
		return [OFString stringWithFormat:
		    @"<%@: %@ [%04X:%04X]>",
		    self.class, self.name, self.vendorID.unsignedShortValue,
		    self.productID.unsignedShortValue];
	else
		return [OFString stringWithFormat: @"<%@: %@>",
						   self.class, self.name];
}
@end

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "HIDEvdevGameController.m"
#endif












Added src/hid/HIDGameControllerAxis.h version [37943879cb].















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief An button of a game controller.
 */
@interface HIDGameControllerAxis: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}

/**
 * @brief The value of the axis.
 */
@property (nonatomic) float value;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerAxis.m version [9b1e5938fe].































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerAxis.h"

@implementation HIDGameControllerAxis
@synthesize value = _value;

- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>", self.class, self.name];
}
@end

Added src/hid/HIDGameControllerButton.h version [e13894eaec].

























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief An button of a game controller.
 */
@interface HIDGameControllerButton: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}

/**
 * @brief Whether the game controller button is pressed.
 */
@property (readonly, nonatomic, getter=isPressed) bool pressed;

/**
 * @brief The pressure with which the button is pressed.
 */
@property (nonatomic) float value;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerButton.m version [64b22e0918].









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerButton.h"

@implementation HIDGameControllerButton
@synthesize value = _value;

- (bool)isPressed
{
	return (_value > 0);
}

- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>", self.class, self.name];
}
@end

Added src/hid/HIDGameControllerDirectionalPad.h version [ba9425bef1].

































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameControllerElement.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief An directional pad or thumb stick of a game controller.
 */
OF_SUBCLASSING_RESTRICTED
@interface HIDGameControllerDirectionalPad: HIDGameControllerElement
{
	HIDGameControllerAxis *_xAxis, *_yAxis;
	HIDGameControllerButton *_upButton, *_downButton;
	HIDGameControllerButton *_leftButton, *_rightButton;
}

/**
 * @brief The X axis of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerAxis *xAxis;

/**
 * @brief The Y axis of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerAxis *yAxis;

/**
 * @brief The up button of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerButton *upButton;

/**
 * @brief The down button of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerButton *downButton;

/**
 * @brief The left button of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerButton *leftButton;

/**
 * @brief The right button of the directional pad.
 */
@property (readonly, nonatomic) HIDGameControllerButton *rightButton;

- (instancetype)initWithName: (OFString *)name OF_UNAVAILABLE;

- (instancetype)initWithName: (OFString *)name
		       xAxis: (HIDGameControllerAxis *)xAxis
		       yAxis: (HIDGameControllerAxis *)yAxis;

- (instancetype)initWithName: (OFString *)name
		    upButton: (HIDGameControllerButton *)upButton
		  downButton: (HIDGameControllerButton *)downButton
		  leftButton: (HIDGameControllerButton *)leftButton
		 rightButton: (HIDGameControllerButton *)rightButton;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerDirectionalPad.m version [6db9de440f].





























































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerDirectionalPad.h"
#import "HIDGameControllerEmulatedAxis.h"
#import "HIDGameControllerEmulatedButton.h"

@implementation HIDGameControllerDirectionalPad
@synthesize xAxis = _xAxis, yAxis = _yAxis;
@synthesize upButton = _upButton, downButton = _downButton;
@synthesize leftButton = _leftButton, rightButton = _rightButton;

- (instancetype)initWithName: (OFString *)name
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithName: (OFString *)name
		       xAxis: (HIDGameControllerAxis *)xAxis
		       yAxis: (HIDGameControllerAxis *)yAxis
{
	self = [super initWithName: name];

	@try {
		_xAxis = [xAxis retain];
		_yAxis = [yAxis retain];

		_upButton = [[HIDGameControllerEmulatedButton alloc]
		    initWithAxis: _yAxis
			positive: false];
		_downButton = [[HIDGameControllerEmulatedButton alloc]
		    initWithAxis: _yAxis
			positive: true];
		_leftButton = [[HIDGameControllerEmulatedButton alloc]
		    initWithAxis: _xAxis
			positive: false];
		_rightButton = [[HIDGameControllerEmulatedButton alloc]
		    initWithAxis: _xAxis
			positive: true];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithName: (OFString *)name
		    upButton: (HIDGameControllerButton *)upButton
		  downButton: (HIDGameControllerButton *)downButton
		  leftButton: (HIDGameControllerButton *)leftButton
		 rightButton: (HIDGameControllerButton *)rightButton
{
	self = [super initWithName: name];

	@try {
		_upButton = [upButton retain];
		_downButton = [downButton retain];
		_leftButton = [leftButton retain];
		_rightButton = [rightButton retain];

		_xAxis = [[HIDGameControllerEmulatedAxis alloc]
		    initWithNegativeButton: _leftButton
			    positiveButton: _rightButton];
		_yAxis = [[HIDGameControllerEmulatedAxis alloc]
		    initWithNegativeButton: _upButton
			    positiveButton: _downButton];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_xAxis release];
	[_yAxis release];
	[_upButton release];
	[_downButton release];
	[_leftButton release];
	[_rightButton release];

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>", self.class, self.name];
}
@end

Added src/hid/HIDGameControllerElement.h version [7e4d7b44de].

























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#ifdef OBJFWHID_LOCAL_INCLUDES
# import "OFObject.h"
# import "OFString.h"
#else
# if defined(__has_feature) && __has_feature(modules)
@import ObjFW;
# else
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief An element of a game controller, e.g. a button, an axis or a
 *	  directional pad.
 */
@interface HIDGameControllerElement: OFObject
{
	OFString *_name;
	bool _analog;
	OF_RESERVE_IVARS(HIDGameControllerElement, 4)
}

/**
 * @brief The name of the game controller element.
 */
@property (readonly, nonatomic) OFString *name;

/**
 * @brief Whether the game controller element is analog.
 */
@property (readonly, nonatomic, getter=isAnalog) bool analog;

- (instancetype)init OF_UNAVAILABLE;

- (instancetype)initWithName: (OFString *)name OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerElement.m version [35eace7e5e].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerElement.h"

@implementation HIDGameControllerElement
@synthesize name = _name, analog = _analog;

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithName: (OFString *)name
{
	self = [super init];

	@try {
		_name = [name copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_name release];

	[super dealloc];
}
@end

Added src/hid/HIDGameControllerEmulatedAxis.h version [f06104d6f6].













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameControllerAxis.h"

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerButton;

OF_SUBCLASSING_RESTRICTED
@interface HIDGameControllerEmulatedAxis: HIDGameControllerAxis
{
	HIDGameControllerButton *_negativeButton, *_positiveButton;
}

- (instancetype)initWithName: (OFString *)name OF_UNAVAILABLE;
- (instancetype)
    initWithNegativeButton: (HIDGameControllerButton *)negativeButton
	    positiveButton: (HIDGameControllerButton *)positiveButton;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerEmulatedAxis.m version [01c06a1ab0].



































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "config.h"

#import "HIDGameControllerEmulatedAxis.h"
#import "HIDGameControllerButton.h"

@implementation HIDGameControllerEmulatedAxis
- (instancetype)initWithName: (OFString *)name
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)
    initWithNegativeButton: (HIDGameControllerButton *)negativeButton
	    positiveButton: (HIDGameControllerButton *)positiveButton
{
	void *pool = objc_autoreleasePoolPush();
	OFString *name;;

	@try {
		name = [OFString stringWithFormat:
		    @"%@ and %@ as emulated axis",
		    negativeButton.name, positiveButton.name];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [super initWithName: name];

	objc_autoreleasePoolPop(pool);

	_negativeButton = [negativeButton retain];
	_positiveButton = [positiveButton retain];

	return self;
}

- (void)dealloc
{
	[_negativeButton release];
	[_positiveButton release];

	[super dealloc];
}

- (void)setValue: (float)value
{
	OF_UNRECOGNIZED_SELECTOR
}

- (float)value
{
	if (_negativeButton.pressed && _positiveButton.pressed)
		return -0;
	if (_negativeButton.pressed)
		return -1;
	if (_positiveButton.pressed)
		return 1;

	return 0;
}
@end

Added src/hid/HIDGameControllerEmulatedButton.h version [3f0e6ab4ac].













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameControllerButton.h"

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerAxis;

OF_SUBCLASSING_RESTRICTED
@interface HIDGameControllerEmulatedButton: HIDGameControllerButton
{
	HIDGameControllerAxis *_axis;
	bool _positive;
}

- (instancetype)initWithName: (OFString *)name OF_UNAVAILABLE;
- (instancetype)initWithAxis: (HIDGameControllerAxis *)axis
		    positive: (bool)positive;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerEmulatedButton.m version [fb0b1a22a4].













































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerEmulatedButton.h"
#import "HIDGameControllerAxis.h"

@implementation HIDGameControllerEmulatedButton: HIDGameControllerButton
- (instancetype)initWithName: (OFString *)name OF_UNAVAILABLE
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithAxis: (HIDGameControllerAxis *)axis
		    positive: (bool)positive
{
	void *pool = objc_autoreleasePoolPush();
	OFString *name;

	@try {
		name = [OFString stringWithFormat:
		    @"%@ %@ as emulated button",
		    (positive ? @"Positive" : @"Negative"), axis.name];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [super initWithName: name];

	objc_autoreleasePoolPop(pool);

	_axis = [axis retain];
	_positive = positive;

	return self;
}

- (void)dealloc
{
	[_axis release];

	[super dealloc];
}

- (bool)isPressed
{
	if (_positive)
		return (_axis.value > 0);
	else
		return (_axis.value < 0);
}
@end

Modified src/hid/Makefile from [8756119022] to [0ce62570af].

1
2
3
4
5
6
7
8
9
10
11
12



13
14
15
16



17
18
19
20
21
22
23
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = OFGameController.m			\



       OFCombinedJoyConsGameController.m

INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h




includesubdir = ObjFWHID

include ../../buildsys.mk

install-extra:
	i=ObjFWHID.oc; \











|
>
>
>
|



>
>
>







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
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = HIDGameController.m			\
       HIDGameControllerAxis.m			\
       HIDGameControllerButton.m			\
       HIDGameControllerDirectionalPad.m		\
       HIDGameControllerElement.m

INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h

SRCS += HIDGameControllerEmulatedAxis.m		\
	HIDGameControllerEmulatedButton.m

includesubdir = ObjFWHID

include ../../buildsys.mk

install-extra:
	i=ObjFWHID.oc; \

Deleted src/hid/OFCombinedJoyConsGameController.h version [56e6c1cdbc].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief A class for combining two Joy-Cons into a single
 *	  @ref OFGameController.
 */
OF_SUBCLASSING_RESTRICTED
@interface OFCombinedJoyConsGameController: OFGameController
{
	OFGameController *_leftJoyCon, *_rightJoyCon;
}

/**
 * @brief Creates a new game controller with the specified left and right
 *	  Joy-Con.
 *
 * @param leftJoyCon The left Joy-Con
 * @param rightJoyCon The right Joy-Con
 * @return A new game controller combining both Joy-Cons into a single game
 *	   controller
 */
+ (instancetype)controllerWithLeftJoyCon: (OFGameController *)leftJoyCon
			     rightJoyCon: (OFGameController *)rightJoyCon;

/**
 * @brief Initialized an already allocated combined Joy-Cons game controller
 *	  with the specified left and right Joy-Con.
 *
 * @param leftJoyCon The left Joy-Con
 * @param rightJoyCon The right Joy-Con
 * @return An initialized combined Joy-Cons game controller combining both
 *	   Joy-Cons into a single game controller
 */
- (instancetype)initWithLeftJoyCon: (OFGameController *)leftJoyCon
		       rightJoyCon: (OFGameController *)rightJoyCon;
@end

OF_ASSUME_NONNULL_END
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






















































































































Deleted src/hid/OFCombinedJoyConsGameController.m version [44f0093bf7].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFCombinedJoyConsGameController.h"
#import "OFNumber.h"
#import "OFSet.h"

#import "OFInvalidArgumentException.h"

@implementation OFCombinedJoyConsGameController
+ (instancetype)controllerWithLeftJoyCon: (OFGameController *)leftJoyCon
			     rightJoyCon: (OFGameController *)rightJoyCon
{
	return [[[self alloc] initWithLeftJoyCon: leftJoyCon
				     rightJoyCon: rightJoyCon] autorelease];
}

- (instancetype)initWithLeftJoyCon: (OFGameController *)leftJoyCon
		       rightJoyCon: (OFGameController *)rightJoyCon
{
	self = [super init];

	@try {
		if (leftJoyCon.vendorID.unsignedShortValue != 0x057E ||
		    rightJoyCon.vendorID.unsignedShortValue != 0x057E)
			@throw [OFInvalidArgumentException exception];

		if (leftJoyCon.productID.unsignedShortValue != 0x2006 ||
		    rightJoyCon.productID.unsignedShortValue != 0x2007)
			@throw [OFInvalidArgumentException exception];

		_leftJoyCon = [leftJoyCon retain];
		_rightJoyCon = [rightJoyCon retain];

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

	return self;
}

- (void)dealloc
{
	[_leftJoyCon release];
	[_rightJoyCon release];

	[super dealloc];
}

- (void)retrieveState
{
	[_leftJoyCon retrieveState];
	[_rightJoyCon retrieveState];
}

- (OFString *)name
{
	return @"Combined Joy-Cons";
}

- (OFSet *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerNorthButton,
	    OFGameControllerSouthButton,
	    OFGameControllerWestButton,
	    OFGameControllerEastButton,
	    OFGameControllerLeftTriggerButton,
	    OFGameControllerRightTriggerButton,
	    OFGameControllerLeftShoulderButton,
	    OFGameControllerRightShoulderButton,
	    OFGameControllerLeftStickButton,
	    OFGameControllerRightStickButton,
	    OFGameControllerDPadUpButton,
	    OFGameControllerDPadDownButton,
	    OFGameControllerDPadLeftButton,
	    OFGameControllerDPadRightButton,
	    OFGameControllerStartButton,
	    OFGameControllerSelectButton,
	    OFGameControllerHomeButton,
	    OFGameControllerCaptureButton,
	    @"Left SL",
	    @"Right SL",
	    @"Left SR",
	    @"Right SR", nil];
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	OFMutableSet *pressedButtons = [OFMutableSet setWithCapacity: 22];
	OFSet *leftPressedButtons = _leftJoyCon.pressedButtons;
	OFSet *rightPressedButtons = _rightJoyCon.pressedButtons;

	if ([rightPressedButtons containsObject: OFGameControllerEastButton])
		[pressedButtons addObject: OFGameControllerNorthButton];
	if ([rightPressedButtons containsObject: OFGameControllerWestButton])
		[pressedButtons addObject: OFGameControllerSouthButton];
	if ([rightPressedButtons containsObject: OFGameControllerNorthButton])
		[pressedButtons addObject: OFGameControllerWestButton];
	if ([rightPressedButtons containsObject: OFGameControllerSouthButton])
		[pressedButtons addObject: OFGameControllerEastButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftTriggerButton])
		[pressedButtons addObject: OFGameControllerLeftTriggerButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightTriggerButton])
		[pressedButtons addObject: OFGameControllerRightTriggerButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftShoulderButton])
		[pressedButtons addObject: OFGameControllerLeftShoulderButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightShoulderButton])
		[pressedButtons addObject: OFGameControllerRightShoulderButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftStickButton])
		[pressedButtons addObject: OFGameControllerLeftStickButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightStickButton])
		[pressedButtons addObject: OFGameControllerRightStickButton];
	if ([leftPressedButtons containsObject: OFGameControllerWestButton])
		[pressedButtons addObject: OFGameControllerDPadUpButton];
	if ([leftPressedButtons containsObject: OFGameControllerEastButton])
		[pressedButtons addObject: OFGameControllerDPadDownButton];
	if ([leftPressedButtons containsObject: OFGameControllerSouthButton])
		[pressedButtons addObject: OFGameControllerDPadLeftButton];
	if ([leftPressedButtons containsObject: OFGameControllerNorthButton])
		[pressedButtons addObject: OFGameControllerDPadRightButton];
	if ([rightPressedButtons containsObject: OFGameControllerStartButton])
		[pressedButtons addObject: OFGameControllerStartButton];
	if ([leftPressedButtons containsObject: OFGameControllerSelectButton])
		[pressedButtons addObject: OFGameControllerSelectButton];
	if ([rightPressedButtons containsObject: OFGameControllerHomeButton])
		[pressedButtons addObject: OFGameControllerHomeButton];
	if ([leftPressedButtons containsObject: OFGameControllerCaptureButton])
		[pressedButtons addObject: OFGameControllerCaptureButton];
	if ([leftPressedButtons containsObject: @"SL"])
		[pressedButtons addObject: @"Left SL"];
	if ([rightPressedButtons containsObject: @"SL"])
		[pressedButtons addObject: @"Right SL"];
	if ([leftPressedButtons containsObject: @"SR"])
		[pressedButtons addObject: @"Left SR"];
	if ([rightPressedButtons containsObject: @"SR"])
		[pressedButtons addObject: @"Right SR"];

	[pressedButtons makeImmutable];

	return pressedButtons;
}

- (bool)hasLeftAnalogStick
{
	return true;
}

- (bool)hasRightAnalogStick
{
	return true;
}

- (OFPoint)leftAnalogStickPosition
{
	OFPoint position = _leftJoyCon.leftAnalogStickPosition;

	return OFMakePoint(-position.y, position.x);
}

- (OFPoint)rightAnalogStickPosition
{
	OFPoint position = _rightJoyCon.leftAnalogStickPosition;

	return OFMakePoint(position.y, -position.x);
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


































































































































































































































































































































































































Deleted src/hid/OFNintendo3DSGameController.h version [600e88c786].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFNintendo3DSGameController: OFGameController
{
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
}
@end

OF_ASSUME_NONNULL_END
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






























































Deleted src/hid/OFNintendo3DSGameController.m version [5d5a974908].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFNintendo3DSGameController.h"
#import "OFArray.h"
#import "OFSet.h"

#import "OFOutOfRangeException.h"

#define id id_3ds
#include <3ds.h>
#undef id

static OFArray OF_GENERIC(OFGameController *) *controllers;

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

+ (void)initialize
{
	void *pool;

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

	pool = objc_autoreleasePoolPush();
	controllers = [[OFArray alloc] initWithObject:
	    [[[OFNintendo3DSGameController alloc] init] autorelease]];
	objc_autoreleasePoolPop(pool);
}

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
{
	return controllers;
}

- (instancetype)init
{
	self = [super init];

	@try {
		_pressedButtons = [[OFMutableSet alloc] initWithCapacity: 14];

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

	return self;
}

- (void)dealloc
{
	[_pressedButtons release];

	[super dealloc];
}

- (void)retrieveState
{
	u32 keys;
	circlePosition leftPos, rightPos;

	hidScanInput();

	keys = hidKeysHeld();
	hidCircleRead(&leftPos);
	hidCstickRead(&rightPos);

	[_pressedButtons removeAllObjects];

	if (keys & KEY_X)
		[_pressedButtons addObject: OFGameControllerNorthButton];
	if (keys & KEY_B)
		[_pressedButtons addObject: OFGameControllerSouthButton];
	if (keys & KEY_Y)
		[_pressedButtons addObject: OFGameControllerWestButton];
	if (keys & KEY_A)
		[_pressedButtons addObject: OFGameControllerEastButton];
	if (keys & KEY_ZL)
		[_pressedButtons addObject: OFGameControllerLeftTriggerButton];
	if (keys & KEY_ZR)
		[_pressedButtons addObject: OFGameControllerRightTriggerButton];
	if (keys & KEY_L)
		[_pressedButtons addObject: OFGameControllerLeftShoulderButton];
	if (keys & KEY_R)
		[_pressedButtons addObject:
		    OFGameControllerRightShoulderButton];
	if (keys & KEY_DUP)
		[_pressedButtons addObject: OFGameControllerDPadUpButton];
	if (keys & KEY_DDOWN)
		[_pressedButtons addObject: OFGameControllerDPadDownButton];
	if (keys & KEY_DLEFT)
		[_pressedButtons addObject: OFGameControllerDPadLeftButton];
	if (keys & KEY_DRIGHT)
		[_pressedButtons addObject: OFGameControllerDPadRightButton];
	if (keys & KEY_START)
		[_pressedButtons addObject: OFGameControllerStartButton];
	if (keys & KEY_SELECT)
		[_pressedButtons addObject: OFGameControllerSelectButton];

	if (leftPos.dx > 150)
		leftPos.dx = 150;
	if (leftPos.dx < -150)
		leftPos.dx = -150;
	if (leftPos.dy > 150)
		leftPos.dy = 150;
	if (leftPos.dy < -150)
		leftPos.dy = -150;

	if (rightPos.dx > 150)
		rightPos.dx = 150;
	if (rightPos.dx < -150)
		rightPos.dx = -150;
	if (rightPos.dy > 150)
		rightPos.dy = 150;
	if (rightPos.dy < -150)
		rightPos.dy = -150;

	_leftAnalogStickPosition = OFMakePoint(
	    (float)leftPos.dx / 150, -(float)leftPos.dy / 150);
	_rightAnalogStickPosition = OFMakePoint(
	    (float)rightPos.dx / 150, -(float)rightPos.dy / 150);
}

- (OFString *)name
{
	return @"Nintendo 3DS";
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerNorthButton,
	    OFGameControllerSouthButton,
	    OFGameControllerWestButton,
	    OFGameControllerEastButton,
	    OFGameControllerLeftTriggerButton,
	    OFGameControllerRightTriggerButton,
	    OFGameControllerRightShoulderButton,
	    OFGameControllerLeftShoulderButton,
	    OFGameControllerDPadUpButton,
	    OFGameControllerDPadDownButton,
	    OFGameControllerDPadLeftButton,
	    OFGameControllerDPadRightButton,
	    OFGameControllerStartButton,
	    OFGameControllerSelectButton, nil];
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	return [[_pressedButtons copy] autorelease];
}

- (bool)hasLeftAnalogStick
{
	return true;
}

- (bool)hasRightAnalogStick
{
	return true;
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
















































































































































































































































































































































































Deleted src/hid/OFNintendoDSGameController.h version [a3f594eb12].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFNintendoDSGameController: OFGameController
{
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
}
@end

OF_ASSUME_NONNULL_END
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




























































Deleted src/hid/OFNintendoDSGameController.m version [76ee0663ed].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFNintendoDSGameController.h"
#import "OFArray.h"
#import "OFSet.h"

#import "OFOutOfRangeException.h"

#define asm __asm__
#include <nds.h>
#undef asm

static OFArray OF_GENERIC(OFGameController *) *controllers;

@implementation OFNintendoDSGameController
+ (void)initialize
{
	void *pool;

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

	pool = objc_autoreleasePoolPush();
	controllers = [[OFArray alloc] initWithObject:
	    [[[OFNintendoDSGameController alloc] init] autorelease]];
	objc_autoreleasePoolPop(pool);
}

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
{
	return controllers;
}

- (instancetype)init
{
	self = [super init];

	@try {
		_pressedButtons = [[OFMutableSet alloc] initWithCapacity: 12];

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

	return self;
}

- (void)dealloc
{
	[_pressedButtons release];

	[super dealloc];
}

- (void)retrieveState
{
	uint32 keys;

	scanKeys();
	keys = keysCurrent();

	[_pressedButtons removeAllObjects];

	if (keys & KEY_X)
		[_pressedButtons addObject: OFGameControllerNorthButton];
	if (keys & KEY_B)
		[_pressedButtons addObject: OFGameControllerSouthButton];
	if (keys & KEY_Y)
		[_pressedButtons addObject: OFGameControllerWestButton];
	if (keys & KEY_A)
		[_pressedButtons addObject: OFGameControllerEastButton];
	if (keys & KEY_L)
		[_pressedButtons addObject: OFGameControllerLeftShoulderButton];
	if (keys & KEY_R)
		[_pressedButtons addObject:
		    OFGameControllerRightShoulderButton];
	if (keys & KEY_UP)
		[_pressedButtons addObject: OFGameControllerDPadUpButton];
	if (keys & KEY_DOWN)
		[_pressedButtons addObject: OFGameControllerDPadDownButton];
	if (keys & KEY_LEFT)
		[_pressedButtons addObject: OFGameControllerDPadLeftButton];
	if (keys & KEY_RIGHT)
		[_pressedButtons addObject: OFGameControllerDPadRightButton];
	if (keys & KEY_START)
		[_pressedButtons addObject: OFGameControllerStartButton];
	if (keys & KEY_SELECT)
		[_pressedButtons addObject: OFGameControllerSelectButton];
}

- (OFString *)name
{
	return @"Nintendo DS";
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerNorthButton,
	    OFGameControllerSouthButton,
	    OFGameControllerWestButton,
	    OFGameControllerEastButton,
	    OFGameControllerLeftShoulderButton,
	    OFGameControllerRightShoulderButton,
	    OFGameControllerDPadUpButton,
	    OFGameControllerDPadDownButton,
	    OFGameControllerDPadLeftButton,
	    OFGameControllerDPadRightButton,
	    OFGameControllerStartButton,
	    OFGameControllerSelectButton, nil];
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	return [[_pressedButtons copy] autorelease];
}

- (bool)hasLeftAnalogStick
{
	return false;
}

- (bool)hasRightAnalogStick
{
	return false;
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<








































































































































































































































































































Deleted src/hid/OFWiiGameController.h version [967feef34b].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFWiiGameController: OFGameController
{
	int32_t _index;
	uint32_t _type;
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	float _leftTriggerPressure, _rightTriggerPressure;
}

- (instancetype)of_initWithIndex: (int32_t)index
			    type: (uint32_t)type OF_METHOD_FAMILY(init);
@end

OF_ASSUME_NONNULL_END
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<










































































Deleted src/hid/OFWiiGameController.m version [c3b51e1e66].

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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFWiiGameController.h"
#import "OFMutableSet.h"

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

#define asm __asm__
#include <wiiuse/wpad.h>
#undef asm

static float
scale(float value, float min, float max, float center)
{
	if (value < min)
		value = min;
	if (value > max)
		value = max;

	if (value >= center)
		return (value - center) / (max - center);
	else
		return (value - center) / (center - min);
}

@implementation OFWiiGameController
+ (void)initialize
{
	if (self != [OFWiiGameController class])
		return;

	if (WPAD_Init() != WPAD_ERR_NONE)
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

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

	for (int32_t i = 0; i < WPAD_MAX_WIIMOTES; i++) {
		uint32_t type;

		if (WPAD_Probe(i, &type) == WPAD_ERR_NONE &&
		    (type == WPAD_EXP_NONE || type == WPAD_EXP_NUNCHUK ||
		    type == WPAD_EXP_CLASSIC))
			[controllers addObject: [[[OFWiiGameController alloc]
			    of_initWithIndex: i
					type: type] autorelease]];
	}

	[controllers makeImmutable];

	objc_autoreleasePoolPop(pool);

	return controllers;
}

- (instancetype)of_initWithIndex: (int32_t)index type: (uint32_t)type
{
	self = [super init];

	@try {
		_index = index;
		_type = type;

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

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

	return self;
}

- (void)dealloc
{
	[_pressedButtons release];

	[super dealloc];
}

- (void)retrieveState
{
	WPADData *data;

	if (WPAD_ReadPending(_index, NULL) < WPAD_ERR_NONE)
		@throw [OFReadFailedException
		    exceptionWithObject: self
			requestedLength: sizeof(WPADData)
				  errNo: 0];

	data = WPAD_Data(_index);

	[_pressedButtons removeAllObjects];

	if (data->btns_h & WPAD_BUTTON_A)
		[_pressedButtons addObject: OFGameControllerEastButton];
	if (data->btns_h & WPAD_BUTTON_B)
		[_pressedButtons addObject: OFGameControllerRightTriggerButton];
	if (data->btns_h & WPAD_BUTTON_1)
		[_pressedButtons addObject: OFGameControllerWestButton];
	if (data->btns_h & WPAD_BUTTON_2)
		[_pressedButtons addObject: OFGameControllerSouthButton];
	if (data->btns_h & WPAD_BUTTON_UP)
		[_pressedButtons addObject: OFGameControllerDPadUpButton];
	if (data->btns_h & WPAD_BUTTON_DOWN)
		[_pressedButtons addObject: OFGameControllerDPadDownButton];
	if (data->btns_h & WPAD_BUTTON_LEFT)
		[_pressedButtons addObject: OFGameControllerDPadLeftButton];
	if (data->btns_h & WPAD_BUTTON_RIGHT)
		[_pressedButtons addObject: OFGameControllerDPadRightButton];
	if (data->btns_h & WPAD_BUTTON_PLUS)
		[_pressedButtons addObject: OFGameControllerStartButton];
	if (data->btns_h & WPAD_BUTTON_MINUS)
		[_pressedButtons addObject: OFGameControllerSelectButton];
	if (data->btns_h & WPAD_BUTTON_HOME)
		[_pressedButtons addObject: OFGameControllerHomeButton];

	if (_type == WPAD_EXP_NUNCHUK) {
		joystick_t *js = &data->exp.nunchuk.js;

		if (data->btns_h & WPAD_NUNCHUK_BUTTON_C)
			[_pressedButtons addObject:
			    OFGameControllerLeftShoulderButton];
		if (data->btns_h & WPAD_NUNCHUK_BUTTON_Z)
			[_pressedButtons addObject:
			    OFGameControllerLeftTriggerButton];

		_leftAnalogStickPosition = OFMakePoint(
		    scale(js->pos.x, js->min.x, js->max.x, js->center.x),
		    -scale(js->pos.y, js->min.y, js->max.y, js->center.y));
	} else if (_type == WPAD_EXP_CLASSIC) {
		joystick_t *ljs = &data->exp.classic.ljs;
		joystick_t *rjs = &data->exp.classic.rjs;

		if (data->btns_h & WPAD_CLASSIC_BUTTON_X)
			[_pressedButtons addObject:
			    OFGameControllerNorthButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_B)
			[_pressedButtons addObject:
			    OFGameControllerSouthButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_Y)
			[_pressedButtons addObject: OFGameControllerWestButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_A)
			[_pressedButtons addObject: OFGameControllerEastButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_FULL_L)
			[_pressedButtons addObject:
			    OFGameControllerLeftTriggerButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_FULL_R)
			[_pressedButtons addObject:
			    OFGameControllerRightTriggerButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_ZL)
			[_pressedButtons addObject:
			    OFGameControllerLeftShoulderButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_ZR)
			[_pressedButtons addObject:
			    OFGameControllerRightShoulderButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_UP)
			[_pressedButtons addObject:
			    OFGameControllerDPadUpButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_DOWN)
			[_pressedButtons addObject:
			    OFGameControllerDPadDownButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_LEFT)
			[_pressedButtons addObject:
			    OFGameControllerDPadLeftButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_RIGHT)
			[_pressedButtons addObject:
			    OFGameControllerDPadRightButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_PLUS)
			[_pressedButtons addObject:
			    OFGameControllerStartButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_MINUS)
			[_pressedButtons addObject:
			    OFGameControllerSelectButton];
		if (data->btns_h & WPAD_CLASSIC_BUTTON_HOME)
			[_pressedButtons addObject: OFGameControllerHomeButton];

		_leftAnalogStickPosition = OFMakePoint(
		    scale(ljs->pos.x, ljs->min.x, ljs->max.x, ljs->center.x),
		    -scale(ljs->pos.y, ljs->min.y, ljs->max.y, ljs->center.y));
		_rightAnalogStickPosition = OFMakePoint(
		    scale(rjs->pos.x, rjs->min.x, rjs->max.x, rjs->center.x),
		    -scale(rjs->pos.y, rjs->min.y, rjs->max.y, rjs->center.y));

		_leftTriggerPressure = data->exp.classic.l_shoulder;
		_rightTriggerPressure = data->exp.classic.r_shoulder;
	}
}

- (OFString *)name
{
	if (_type == WPAD_EXP_NUNCHUK)
		return @"Wiimote with Nunchuk";
	else if (_type == WPAD_EXP_CLASSIC)
		return @"Wiimote with Classic Controller";
	else
		return @"Wiimote";
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	OFMutableSet *buttons = [OFMutableSet setWithCapacity: 15];

	[buttons addObject: OFGameControllerSouthButton];
	[buttons addObject: OFGameControllerRightTriggerButton];
	[buttons addObject: OFGameControllerWestButton];
	[buttons addObject: OFGameControllerEastButton];
	[buttons addObject: OFGameControllerDPadUpButton];
	[buttons addObject: OFGameControllerDPadDownButton];
	[buttons addObject: OFGameControllerDPadLeftButton];
	[buttons addObject: OFGameControllerDPadRightButton];
	[buttons addObject: OFGameControllerStartButton];
	[buttons addObject: OFGameControllerSelectButton];
	[buttons addObject: OFGameControllerHomeButton];

	if (_type == WPAD_EXP_NUNCHUK) {
		[buttons addObject: OFGameControllerLeftShoulderButton];
		[buttons addObject: OFGameControllerLeftTriggerButton];
	} else if (_type == WPAD_EXP_CLASSIC) {
		[buttons addObject: OFGameControllerNorthButton];
		[buttons addObject: OFGameControllerLeftTriggerButton];
		[buttons addObject: OFGameControllerLeftShoulderButton];
		[buttons addObject: OFGameControllerRightShoulderButton];
	}

	[buttons makeImmutable];

	return buttons;
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	return [[_pressedButtons copy] autorelease];
}

- (bool)hasLeftAnalogStick
{
	return (_type == WPAD_EXP_NUNCHUK || _type == WPAD_EXP_CLASSIC);
}

- (bool)hasRightAnalogStick
{
	return (_type == WPAD_EXP_CLASSIC);
}

- (OFPoint)leftAnalogStickPosition
{
	if (_type != WPAD_EXP_NUNCHUK && _type != WPAD_EXP_CLASSIC)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	return _leftAnalogStickPosition;
}

- (OFPoint)rightAnalogStickPosition
{
	if (_type != WPAD_EXP_CLASSIC)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	return _rightAnalogStickPosition;
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	if (_type == WPAD_EXP_CLASSIC) {
		if (button == OFGameControllerLeftTriggerButton)
			return _leftTriggerPressure;
		if (button == OFGameControllerRightTriggerButton)
			return _rightTriggerPressure;
	}

	return [super pressureForButton: button];
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


























































































































































































































































































































































































































































































































































































































Deleted src/hid/OFXInputGameController.h version [393b179dcd].

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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFXInputGameController: OFGameController
{
	DWORD _index;
	OFNumber *_Nullable _vendorID, *_Nullable productID;
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	float _leftTriggerPressure, _rightTriggerPressure;
}

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

OF_ASSUME_NONNULL_END
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<








































































Deleted src/hid/OFXInputGameController.m version [965b558ea8].

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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

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

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

#include <xinput.h>

#ifndef XINPUT_GAMEPAD_GUIDE
# define XINPUT_GAMEPAD_GUIDE 0x400
#endif

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 *);
static int XInputVersion;

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

+ (void)initialize
{
	HMODULE module;

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

	if ((module = LoadLibraryA("xinput1_4.dll")) != NULL) {
		XInputGetStateFuncPtr =
		    (WINAPI DWORD (*)(DWORD, XINPUT_STATE *))
		    GetProcAddress(module, (LPCSTR)100);
		XInputGetCapabilitiesExFuncPtr = (WINAPI DWORD (*)(DWORD, DWORD,
		    DWORD, struct XInputCapabilitiesEx *))
		    GetProcAddress(module, "XInputGetCapabilitiesEx");
		XInputVersion = 14;
	} else if ((module = LoadLibraryA("xinput1_3.dll")) != NULL) {
		XInputGetStateFuncPtr =
		    (WINAPI DWORD (*)(DWORD, XINPUT_STATE *))
		    GetProcAddress(module, (LPCSTR)100);
		XInputVersion = 13;
	} else if ((module = LoadLibraryA("xinput9_1_0.dll")) != NULL) {
		XInputGetStateFuncPtr =
		    (WINAPI DWORD (*)(DWORD, XINPUT_STATE *))
		    GetProcAddress(module, "XInputGetState");
		XInputVersion = 910;
	}
}

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

	if (XInputGetStateFuncPtr != NULL) {
		void *pool = objc_autoreleasePoolPush();

		for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
			OFGameController *controller;

			@try {
				controller = [[[OFXInputGameController alloc]
				    of_initWithIndex: i] autorelease];
			} @catch (OFInitializationFailedException *e) {
				/* Controller does not exist. */
				continue;
			}

			[controllers addObject: controller];
		}

		objc_autoreleasePoolPop(pool);
	}

	[controllers makeImmutable];

	return controllers;
}

- (instancetype)of_initWithIndex: (DWORD)index
{
	self = [super init];

	@try {
		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: 17];

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

	return self;
}

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

	[super dealloc];
}

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

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

	[_pressedButtons removeAllObjects];

	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_Y)
		[_pressedButtons addObject: OFGameControllerNorthButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A)
		[_pressedButtons addObject: OFGameControllerSouthButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_X)
		[_pressedButtons addObject: OFGameControllerWestButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_B)
		[_pressedButtons addObject: OFGameControllerEastButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER)
		[_pressedButtons addObject: OFGameControllerLeftShoulderButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER)
		[_pressedButtons addObject:
		    OFGameControllerRightShoulderButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB)
		[_pressedButtons addObject: OFGameControllerLeftStickButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB)
		[_pressedButtons addObject: OFGameControllerRightStickButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
		[_pressedButtons addObject: OFGameControllerDPadUpButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
		[_pressedButtons addObject: OFGameControllerDPadDownButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
		[_pressedButtons addObject: OFGameControllerDPadLeftButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
		[_pressedButtons addObject: OFGameControllerDPadRightButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_START)
		[_pressedButtons addObject: OFGameControllerStartButton];
	if (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK)
		[_pressedButtons addObject: OFGameControllerSelectButton];
	if (XInputVersion != 910 &&
	    state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)
		[_pressedButtons addObject: OFGameControllerHomeButton];

	_leftTriggerPressure = (float)state.Gamepad.bLeftTrigger / 255;
	_rightTriggerPressure = (float)state.Gamepad.bRightTrigger / 255;

	if (_leftTriggerPressure > 0)
		[_pressedButtons addObject: OFGameControllerLeftTriggerButton];
	if (_rightTriggerPressure > 0)
		[_pressedButtons addObject: OFGameControllerRightTriggerButton];

	_leftAnalogStickPosition = OFMakePoint(
	    (float)state.Gamepad.sThumbLX /
	    (state.Gamepad.sThumbLX < 0 ? -INT16_MIN : INT16_MAX),
	    -(float)state.Gamepad.sThumbLY /
	    (state.Gamepad.sThumbLY < 0 ? -INT16_MIN : INT16_MAX));
	_rightAnalogStickPosition = OFMakePoint(
	    (float)state.Gamepad.sThumbRX /
	    (state.Gamepad.sThumbRX < 0 ? -INT16_MIN : INT16_MAX),
	    -(float)state.Gamepad.sThumbRY /
	    (state.Gamepad.sThumbRY < 0 ? -INT16_MIN : INT16_MAX));
}

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

	return nil;
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerNorthButton,
	    OFGameControllerSouthButton,
	    OFGameControllerWestButton,
	    OFGameControllerEastButton,
	    OFGameControllerLeftTriggerButton,
	    OFGameControllerRightTriggerButton,
	    OFGameControllerLeftShoulderButton,
	    OFGameControllerRightShoulderButton,
	    OFGameControllerLeftStickButton,
	    OFGameControllerRightStickButton,
	    OFGameControllerDPadLeftButton,
	    OFGameControllerDPadRightButton,
	    OFGameControllerDPadUpButton,
	    OFGameControllerDPadDownButton,
	    OFGameControllerStartButton,
	    OFGameControllerSelectButton,
	    (XInputVersion != 910 ? OFGameControllerHomeButton : nil), nil];
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	return [[_pressedButtons copy] autorelease];
}

- (bool)hasLeftAnalogStick
{
	return true;
}

- (bool)hasRightAnalogStick
{
	return true;
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	if (button == OFGameControllerLeftTriggerButton)
		return _leftTriggerPressure;
	if (button == OFGameControllerRightTriggerButton)
		return _rightTriggerPressure;

	return [super pressureForButton: button];
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
























































































































































































































































































































































































































































































































































































Modified src/hid/ObjFWHID.h from [e3220aba7e] to [d1a844bf10].

13
14
15
16
17
18
19
20
21



 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFGameController.h"
#import "OFCombinedJoyConsGameController.h"










|
|
>
>
>
13
14
15
16
17
18
19
20
21
22
23
24
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameController.h"
#import "HIDGameControllerElement.h"
#import "HIDGameControllerButton.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerDirectionalPad.h"

Modified src/test/OTAppDelegate.m from [747d91c177] to [6e4666d486].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFThread.h"
#import "OFValue.h"

#import "OTTestCase.h"

#import "OFGameController.h"

#import "OTAssertionFailedException.h"
#import "OTTestSkippedException.h"

#ifdef OF_IOS
# include <CoreFoundation/CoreFoundation.h>
#endif







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFThread.h"
#import "OFValue.h"

#import "OTTestCase.h"

#import "HIDGameController.h"

#import "OTAssertionFailedException.h"
#import "OTTestSkippedException.h"

#ifdef OF_IOS
# include <CoreFoundation/CoreFoundation.h>
#endif
279
280
281
282
283
284
285
286
287


288
289
290
291
292
293
294
295
296
297
298
299
	if (status == StatusFailed) {
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			void *pool = objc_autoreleasePoolPush();
			OFGameController *controller =
			    [[OFGameController controllers] objectAtIndex: 0];



			[controller retrieveState];

			if ([controller.pressedButtons containsObject:
			    OFGameControllerEastButton])
				break;

			[OFThread waitForVerticalBlank];

			objc_autoreleasePoolPop(pool);
		}
#elif defined(OF_NINTENDO_SWITCH)







|
|
>
>



|
<







279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

294
295
296
297
298
299
300
	if (status == StatusFailed) {
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			void *pool = objc_autoreleasePoolPush();
			HIDGameController *controller =
			    [[HIDGameController controllers] objectAtIndex: 0];
			HIDGameControllerButton *button =
			    [controller.buttons objectForKey: @"A"];

			[controller retrieveState];

			if (button.pressed)

				break;

			[OFThread waitForVerticalBlank];

			objc_autoreleasePoolPop(pool);
		}
#elif defined(OF_NINTENDO_SWITCH)
541
542
543
544
545
546
547
548
549






550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
	[OFStdOut writeLine: @"Press Home button to exit"];
# else
	[OFStdOut writeLine: @"Press Start button to exit"];
# endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		OFGameController *controller =
		    [[OFGameController controllers] objectAtIndex: 0];







		[controller retrieveState];

# ifdef OF_WII
		if ([controller.pressedButtons containsObject:
		    OFGameControllerHomeButton])
# else
		if ([controller.pressedButtons containsObject:
		    OFGameControllerStartButton])
# endif
			break;

		[OFThread waitForVerticalBlank];

		objc_autoreleasePoolPop(pool);
	}
#elif defined(OF_NINTENDO_SWITCH)







|
|
>
>
>
>
>
>



<
|
<
<
<
<
<







542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

560





561
562
563
564
565
566
567
	[OFStdOut writeLine: @"Press Home button to exit"];
# else
	[OFStdOut writeLine: @"Press Start button to exit"];
# endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		HIDGameController *controller =
		    [[HIDGameController controllers] objectAtIndex: 0];
		HIDGameControlelrButton *button =
# ifdef OF_WII
		    [controller.buttons objectForKey: @"Home"];
# else
		    [controller.buttons objectForKey: @"Start"];
# endif

		[controller retrieveState];


		if (button.pressed)





			break;

		[OFThread waitForVerticalBlank];

		objc_autoreleasePoolPop(pool);
	}
#elif defined(OF_NINTENDO_SWITCH)

Modified tests/gamecontroller/GameControllerTests.m from [1bbc654209] to [b2c5ea408f].

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
 */

#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFColor.h"
#import "OFCombinedJoyConsGameController.h"
#import "OFDate.h"
#import "OFGameController.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFThread.h"





#import "OFReadFailedException.h"

#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
#else
static size_t buttonsPerLine = 5;
#endif

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
# define red maroon
# define yellow olive
# define gray silver
#endif

@interface GameControllerTests: OFObject <OFApplicationDelegate>
{
	OFMutableArray OF_GENERIC(OFGameController *) *_controllers;
	OFDate *_lastControllersUpdate;
}
@end

OF_APPLICATION_DELEGATE(GameControllerTests)

@implementation GameControllerTests
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();

		if (_lastControllersUpdate == nil ||
		    -[_lastControllersUpdate timeIntervalSinceNow] > 1) {
			OFGameController *leftJoyCon = nil, *rightJoyCon = nil;

			[_controllers release];
			[_lastControllersUpdate release];

			_controllers = [[OFGameController controllers]
			    mutableCopy];
			_lastControllersUpdate = [[OFDate alloc] init];

			for (OFGameController *controller in _controllers) {
				if (controller.vendorID.unsignedShortValue !=
				    0x057E)
					continue;

				if (controller.productID.unsignedShortValue ==
				    0x2006)
					leftJoyCon = controller;
				else if (
				    controller.productID.unsignedShortValue ==
				    0x2007)
					rightJoyCon = controller;
			}

			if (leftJoyCon != nil && rightJoyCon != nil)
				[_controllers addObject:
				    [OFCombinedJoyConsGameController
				    controllerWithLeftJoyCon: leftJoyCon
						 rightJoyCon: rightJoyCon]];

			[OFStdOut clear];
		}

		[OFStdOut setCursorPosition: OFMakePoint(0, 0)];

		for (OFGameController *controller in _controllers) {
			OFArray OF_GENERIC(OFGameControllerButton) *buttons =
			    controller.buttons.allObjects.sortedArray;


			size_t i = 0;

			[OFStdOut setForegroundColor: [OFColor green]];
			[OFStdOut writeString: controller.description];

			@try {
				[controller retrieveState];
			} @catch (OFReadFailedException *e) {
				[OFStdOut setForegroundColor: [OFColor red]];
				[OFStdOut writeFormat: @"\n%@", e.description];
				continue;
			}


			for (OFGameControllerButton button in buttons) {
				float pressure;


				if (i == 0)
					[OFStdOut writeString: @"\n"];

				pressure =
				    [controller pressureForButton: button];

				if (pressure == 1)
					[OFStdOut setForegroundColor:
					    [OFColor red]];
				else if (pressure > 0.5)
					[OFStdOut setForegroundColor:
					    [OFColor yellow]];
				else if (pressure > 0)
					[OFStdOut setForegroundColor:
					    [OFColor green]];
				else
					[OFStdOut setForegroundColor:
					    [OFColor gray]];

				[OFStdOut writeFormat: @"[%@]", button];

				if (++i == buttonsPerLine) {
					i = 0;
				} else
					[OFStdOut writeString: @" "];
			}
			[OFStdOut setForegroundColor: [OFColor gray]];
			[OFStdOut writeString: @"\n"];



			if (controller.hasLeftAnalogStick) {
				OFPoint position =
				    controller.leftAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f) ",
						       position.x, position.y];
			}
			if (controller.hasRightAnalogStick) {
				OFPoint position =
				    controller.rightAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f)",
						       position.x, position.y];
			}
			[OFStdOut writeString: @"\n"];
		}

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFThread waitForVerticalBlank];
#else
		[OFThread sleepForTimeInterval: 1.f / 60.f];
#endif

		objc_autoreleasePoolPop(pool);
	}
}
@end







<

<
|
|



>
>
>
>


















|


















<
<



|



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<





|
|
|
>
>













>
|
<
>




<
<
<
|


|


|






|









>
>
|
|
<
|
|

<
<
<
<
<
|













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
 */

#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFColor.h"

#import "OFDate.h"

#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"

#import "HIDGameController.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

#import "OFReadFailedException.h"

#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
#else
static size_t buttonsPerLine = 5;
#endif

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
# define red maroon
# define yellow olive
# define gray silver
#endif

@interface GameControllerTests: OFObject <OFApplicationDelegate>
{
	OFMutableArray OF_GENERIC(HIDGameController *) *_controllers;
	OFDate *_lastControllersUpdate;
}
@end

OF_APPLICATION_DELEGATE(GameControllerTests)

@implementation GameControllerTests
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();

		if (_lastControllersUpdate == nil ||
		    -[_lastControllersUpdate timeIntervalSinceNow] > 1) {


			[_controllers release];
			[_lastControllersUpdate release];

			_controllers = [[HIDGameController controllers]
			    mutableCopy];
			_lastControllersUpdate = [[OFDate alloc] init];





















			[OFStdOut clear];
		}

		[OFStdOut setCursorPosition: OFMakePoint(0, 0)];

		for (HIDGameController *controller in _controllers) {
			OFArray OF_GENERIC(OFString *) *buttons =
			    controller.buttons.allKeys.sortedArray;
			OFArray OF_GENERIC(OFString *) *axes =
			    controller.axes.allKeys.sortedArray;
			size_t i = 0;

			[OFStdOut setForegroundColor: [OFColor green]];
			[OFStdOut writeString: controller.description];

			@try {
				[controller retrieveState];
			} @catch (OFReadFailedException *e) {
				[OFStdOut setForegroundColor: [OFColor red]];
				[OFStdOut writeFormat: @"\n%@", e.description];
				continue;
			}

			for (OFString *name in buttons) {
				HIDGameControllerButton *button =

				    [controller.buttons objectForKey: name];

				if (i == 0)
					[OFStdOut writeString: @"\n"];




				if (button.value == 1)
					[OFStdOut setForegroundColor:
					    [OFColor red]];
				else if (button.value > 0.5)
					[OFStdOut setForegroundColor:
					    [OFColor yellow]];
				else if (button.value > 0)
					[OFStdOut setForegroundColor:
					    [OFColor green]];
				else
					[OFStdOut setForegroundColor:
					    [OFColor gray]];

				[OFStdOut writeFormat: @"[%@]", button.name];

				if (++i == buttonsPerLine) {
					i = 0;
				} else
					[OFStdOut writeString: @" "];
			}
			[OFStdOut setForegroundColor: [OFColor gray]];
			[OFStdOut writeString: @"\n"];

			for (OFString *name in axes) {
				HIDGameControllerAxis *axis =
				    [controller.axes objectForKey: name];


				[OFStdOut writeFormat: @"%@: %5.2f ",
						       name, axis.value];
			}






			[OFStdOut writeString: @"\n"];
		}

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFThread waitForVerticalBlank];
#else
		[OFThread sleepForTimeInterval: 1.f / 60.f];
#endif

		objc_autoreleasePoolPop(pool);
	}
}
@end