ObjFW  Check-in [21bcf92ecd]

Overview
Comment:OFEvdevGameController: Rotate Joy-Cons

Since both are detected as separate devices, rotate them so that they
can be used as a single device.

A new class will be added to combine two Joy-Cons into one controller.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 21bcf92ecdb9c4de026705545b1f6ae50a3369ec22663cd7ffb97568e3f403e9
User & Date: js on 2024-05-22 23:18:16
Other Links: manifest | tags
Context
2024-05-22
23:54
Add OFCombinedJoyConsGameController check-in: 68ef38f917 user: js tags: trunk
23:18
OFEvdevGameController: Rotate Joy-Cons check-in: 21bcf92ecd user: js tags: trunk
19:55
PLATFORMS.md: Add LoongArch 64 to Linux check-in: 6319f802bb user: js tags: trunk
Changes

Modified src/hid/OFEvdevGameController.h from [5ec72d013e] to [c297eb99a1].

33
34
35
36
37
38
39

40
41
42
43
44
45
46
	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;

	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;







>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	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;

Modified src/hid/OFEvdevGameController.m from [be8e76e300] to [bcc812f365].

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
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_Z:
			return OFGameControllerCaptureButton;
		case BTN_TR:
			return @"SL";
		case BTN_TR2:
			return @"SR";
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDRightJoycon) {
		switch (button) {
		case BTN_NORTH:
			return OFGameControllerNorthButton;
		case BTN_WEST:


			return OFGameControllerWestButton;


		case BTN_TL:
			return @"SL";
		case BTN_TL2:
			return @"SR";
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {







|
|




















|

>
>
>
>
>
>
>
>








|

|

|
>
>

>
>







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
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) {
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	return false;
}

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

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








<







233
234
235
236
237
238
239

240
241
242
243
244
245
246
	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();

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

		if (OFBitSetIsSet(evBits, EV_ABS)) {
			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),
			    absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (_vendorID == vendorIDGoogle &&













			    _productID == productIDStadia) {
				/*
				 * It's unclear how this can be screwed up
				 * *this* bad.
				 */


				_rightAnalogStickXBit = ABS_Z;
				_rightAnalogStickYBit = ABS_RZ;
				_leftTriggerPressureBit = ABS_BRAKE;
				_rightTriggerPressureBit = ABS_GAS;
			} else {


				_rightAnalogStickXBit = ABS_RX;
				_rightAnalogStickYBit = ABS_RY;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			}

			if (OFBitSetIsSet(absBits, ABS_X) &&
			    OFBitSetIsSet(absBits, ABS_Y))
				_hasLeftAnalogStick = true;

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

			if (_vendorID == vendorIDNintendo &&







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





>
>





>
>






|
|







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

		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 &&
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
			[_pressedButtons addObject:
			    OFGameControllerDPadDownButton];
	}

	if (_hasLeftAnalogStick) {
		struct input_absinfo infoX, infoY;

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

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

		_leftAnalogStickMinX = infoX.minimum;
		_leftAnalogStickMaxX = infoX.maximum;







|





|







522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
			[_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;
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641

				emulateRightAnalogStick(_vendorID, _productID,
				    _pressedButtons,
				    &_rightAnalogStickPosition);
			}
			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,
				    _leftAnalogStickMinY, _leftAnalogStickMaxY);
			else if (event.code == _rightAnalogStickXBit)
				_rightAnalogStickPosition.x = scale(event.value,
				    _rightAnalogStickMinX,
				    _rightAnalogStickMaxX);
			else if (event.code == _rightAnalogStickYBit)







|


|







652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

				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)
732
733
734
735
736
737
738












739
740
741
742
743
744
745
746
747
748
749
750
751
		if (![button hasPrefix: @"_"])
			[pressedButtons addObject: button];

	[pressedButtons makeImmutable];

	return pressedButtons;
}













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

	return [super pressureForButton: button];
}
@end







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













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