ObjFW  Check-in [a0af8f40b8]

Overview
Comment:OFGameController: Clean up buttons a bit

This also emulates a right analog stick using the C buttons on the N64
controller.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gamecontroller
Files: files | file ages | folders
SHA3-256: a0af8f40b8597b158c3616c5159484920b43857a153830998375d822c08941af
User & Date: js on 2024-05-20 15:48:03
Other Links: branch diff | manifest | tags
Context
2024-05-20
15:53
OFGameController: No symbols for custom buttons check-in: cb4c3d09f4 user: js tags: gamecontroller
15:48
OFGameController: Clean up buttons a bit check-in: a0af8f40b8 user: js tags: gamecontroller
10:49
Doxyfile: Generate documentation for ObjFWHID check-in: 8ea353d943 user: js tags: gamecontroller
Changes

Modified src/hid/OFEvdevGameController.m from [3ddc2d6279] to [c33068523f].

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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) {







|
|
|
|







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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_X, BTN_Y, 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) {
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
			return OFGameControllerSLButton;
		case BTN_TL2:
			return OFGameControllerSRButton;
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		switch (button) {
		case BTN_A:
			return OFGameControllerAButton;
		case BTN_B:
			return OFGameControllerBButton;
		case BTN_SELECT:
			return OFGameControllerCPadUpButton;
		case BTN_X:
			return OFGameControllerCPadDownButton;
		case BTN_Y:
			return OFGameControllerCPadLeftButton;
		case BTN_C:

			return OFGameControllerCPadRightButton;
		case BTN_Z:
			return OFGameControllerCaptureButton;
		}
	} else if (vendorID == vendorIDSony &&
	    (productID == productIDDualSense ||
	    productID == productIDDualShock4)) {
		switch (button) {







<
<

|

<

<

<

>
|







103
104
105
106
107
108
109


110
111
112

113

114

115
116
117
118
119
120
121
122
123
124
			return OFGameControllerSLButton;
		case BTN_TL2:
			return OFGameControllerSRButton;
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		switch (button) {


		case BTN_B:
			return OFGameControllerWestButton;
		case BTN_SELECT:

		case BTN_X:

		case BTN_Y:

		case BTN_C:
			/* Used to emulate right analog stick. */
			return nil;
		case BTN_Z:
			return OFGameControllerCaptureButton;
		}
	} else if (vendorID == vendorIDSony &&
	    (productID == productIDDualSense ||
	    productID == productIDDualShock4)) {
		switch (button) {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		return OFGameControllerDPadRightButton;
	case BTN_START:
		return OFGameControllerStartButton;
	case BTN_SELECT:
		return OFGameControllerSelectButton;
	case BTN_MODE:
		return OFGameControllerHomeButton;
	case BTN_C:
		return OFGameControllerCButton;
	case BTN_Z:
		return OFGameControllerZButton;
	}

	return nil;
}

static float
scale(float value, float min, float max)







<
<
<
<







167
168
169
170
171
172
173




174
175
176
177
178
179
180
		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)
365
366
367
368
369
370
371








372
373
374
375
376
377
378
					    exception];

				_rightAnalogStickMinX = infoX.minimum;
				_rightAnalogStickMaxX = infoX.maximum;
				_rightAnalogStickMinY = infoY.minimum;
				_rightAnalogStickMaxY = infoY.maximum;
			}









			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				[_buttons addObject:
				    OFGameControllerDPadLeftButton];
				[_buttons addObject:
				    OFGameControllerDPadRightButton];







>
>
>
>
>
>
>
>







357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
					    exception];

				_rightAnalogStickMinX = infoX.minimum;
				_rightAnalogStickMaxX = infoX.maximum;
				_rightAnalogStickMinY = infoY.minimum;
				_rightAnalogStickMaxY = infoY.maximum;
			}

			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)) {
				[_buttons addObject:
				    OFGameControllerDPadLeftButton];
				[_buttons addObject:
				    OFGameControllerDPadRightButton];
476
477
478
479
480
481
482
























483
484
485
486
487
488
489
			if ((button = buttonToName(event.code, _vendorID,
			    _productID)) != nil) {
				if (event.value)
					[_pressedButtons addObject: button];
				else
					[_pressedButtons removeObject: button];
			}
























			break;
		case EV_ABS:
			if (event.code == ABS_X)
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);
			else if (event.code == ABS_Y)
				_leftAnalogStickPosition.y = scale(event.value,







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







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
			if ((button = buttonToName(event.code, _vendorID,
			    _productID)) != nil) {
				if (event.value)
					[_pressedButtons addObject: button];
				else
					[_pressedButtons removeObject: button];
			}

			/* Use C buttons to emulate right analog stick */
			if (_vendorID == vendorIDNintendo &&
			    _productID == productIDN64Controller) {
				switch (event.code) {
				case BTN_Y:
					_rightAnalogStickPosition.x +=
					    (event.value ? -1 : 1);
					break;
				case BTN_C:
					_rightAnalogStickPosition.x +=
					    (event.value ? 1 : -1);
					break;
				case BTN_SELECT:
					_rightAnalogStickPosition.y +=
					    (event.value ? -1 : 1);
					break;
				case BTN_X:
					_rightAnalogStickPosition.y +=
					    (event.value ? 1 : -1);
					break;
				}
			}

			break;
		case EV_ABS:
			if (event.code == ABS_X)
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);
			else if (event.code == ABS_Y)
				_leftAnalogStickPosition.y = scale(event.value,

Modified src/hid/OFGameController.h from [377e60382e] to [c213cd4864].

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 *  * @ref OFGameControllerDPadDownButton
 *  * @ref OFGameControllerDPadLeftButton
 *  * @ref OFGameControllerDPadRightButton
 *  * @ref OFGameControllerStartButton
 *  * @ref OFGameControllerSelectButton
 *  * @ref OFGameControllerHomeButton
 *  * @ref OFGameControllerCaptureButton
 *  * @ref OFGameControllerAButton
 *  * @ref OFGameControllerBButton
 *  * @ref OFGameControllerCButton
 *  * @ref OFGameControllerXButton
 *  * @ref OFGameControllerYButton
 *  * @ref OFGameControllerZButton
 *  * @ref OFGameControllerCPadUpButton
 *  * @ref OFGameControllerCPadDownButton
 *  * @ref OFGameControllerCPadLeftButton
 *  * @ref OFGameControllerCPadRightButton
 *  * @ref OFGameControllerSLButton
 *  * @ref OFGameControllerSRButton
 *  * @ref OFGameControllerAssistantButton
 */
typedef OFConstantString *OFGameControllerButton;

#ifdef __cplusplus







<
<
<
<
<
<
<
<
<
<







57
58
59
60
61
62
63










64
65
66
67
68
69
70
 *  * @ref OFGameControllerDPadDownButton
 *  * @ref OFGameControllerDPadLeftButton
 *  * @ref OFGameControllerDPadRightButton
 *  * @ref OFGameControllerStartButton
 *  * @ref OFGameControllerSelectButton
 *  * @ref OFGameControllerHomeButton
 *  * @ref OFGameControllerCaptureButton










 *  * @ref OFGameControllerSLButton
 *  * @ref OFGameControllerSRButton
 *  * @ref OFGameControllerAssistantButton
 */
typedef OFConstantString *OFGameControllerButton;

#ifdef __cplusplus
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
extern const OFGameControllerButton OFGameControllerHomeButton;

/**
 * @brief The Capture button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerCaptureButton;

/**
 * @brief The A button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerAButton;

/**
 * @brief The B button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerBButton;

/**
 * @brief The C button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerCButton;

/**
 * @brief The X button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerXButton;

/**
 * @brief The Y button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerYButton;

/**
 * @brief The Z button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerZButton;

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

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

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

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

/**
 * @brief The SL button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerSLButton;

/**
 * @brief The SR button on a game controller.







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







157
158
159
160
161
162
163


















































164
165
166
167
168
169
170
extern const OFGameControllerButton OFGameControllerHomeButton;

/**
 * @brief The Capture button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerCaptureButton;



















































/**
 * @brief The SL button on a game controller.
 */
extern const OFGameControllerButton OFGameControllerSLButton;

/**
 * @brief The SR button on a game controller.
285
286
287
288
289
290
291



292
293
294
295
296
297
298



299
300
301
302
303
304
305
 *
 * The range is from (-1, -1) to (1, 1).
 */
@property (readonly, nonatomic) OFPoint leftAnalogStickPosition;

/**
 * @brief Whether the controller has a right analog stick.



 */
@property (readonly, nonatomic) bool hasRightAnalogStick;

/**
 * @brief The position of the right analog stick.
 *
 * The range is from (-1, -1) to (1, 1).



 */
@property (readonly, nonatomic) OFPoint rightAnalogStickPosition;

/**
 * @brief Returns the available controllers.
 *
 * @return The available controllers







>
>
>







>
>
>







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

Modified src/hid/OFGameController.m from [6850544fb5] to [4c858784ed].

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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";
const OFGameControllerButton OFGameControllerAButton = @"A";
const OFGameControllerButton OFGameControllerBButton = @"B";
const OFGameControllerButton OFGameControllerCButton = @"C";
const OFGameControllerButton OFGameControllerXButton = @"X";
const OFGameControllerButton OFGameControllerYButton = @"Y";
const OFGameControllerButton OFGameControllerZButton = @"Z";
const OFGameControllerButton OFGameControllerCPadUpButton = @"C-Pad Up";
const OFGameControllerButton OFGameControllerCPadDownButton = @"C-Pad Down";
const OFGameControllerButton OFGameControllerCPadLeftButton = @"C-Pad Left";
const OFGameControllerButton OFGameControllerCPadRightButton = @"C-Pad Right";
const OFGameControllerButton OFGameControllerSLButton = @"SL";
const OFGameControllerButton OFGameControllerSRButton = @"SR";
const OFGameControllerButton OFGameControllerAssistantButton = @"Assistant";

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







<
<
<
<
<
<
<
<
<
<







58
59
60
61
62
63
64










65
66
67
68
69
70
71
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";










const OFGameControllerButton OFGameControllerSLButton = @"SL";
const OFGameControllerButton OFGameControllerSRButton = @"SR";
const OFGameControllerButton OFGameControllerAssistantButton = @"Assistant";

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