ObjFW  Check-in [01f1d066bc]

Overview
Comment:Various cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 01f1d066bcfb25b0adad6ddcc3bb423124f45d1b4540468d3a7562286427f10d
User & Date: js on 2024-06-09 23:39:13
Other Links: manifest | tags
Context
2024-06-16
20:52
ObjFWHID: Move Wiimote profile to separate files check-in: be1b426a45 user: js tags: trunk
2024-06-09
23:39
Various cleanups check-in: 01f1d066bc user: js tags: trunk
23:24
ObjFWHID: Add support for Nintendo Switch check-in: e01b5123f1 user: js tags: trunk
Changes

Modified src/hid/OHEvdevGameController.h from [428d509e52] to [0dab2fe07e].

29
30
31
32
33
34
35
36

37
38
39
40
29
30
31
32
33
34
35

36
37
38
39
40







-
+




	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;
	uint16_t _vendorID, _productID;
	OFString *_name;
	id <OHGameControllerProfile> _rawProfile;
}

- (instancetype)oh_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
- (instancetype)initWithPath: (OFString *)path;
- (void)oh_pollState;
@end

OF_ASSUME_NONNULL_END

Modified src/hid/OHEvdevGameController.m from [98e64f1a03] to [548d505db7].

57
58
59
60
61
62
63
64
65


66
67
68
69
70
71
72
57
58
59
60
61
62
63


64
65
66
67
68
69
70
71
72







-
-
+
+








@interface OHEvdevGameControllerProfile: OFObject <OHGameControllerProfile>
{
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *_axes;
}

- (instancetype)oh_initWithButtons: (OFDictionary *)buttons
			      axes: (OFDictionary *)axes OF_METHOD_FAMILY(init);
- (instancetype)initWithButtons: (OFDictionary *)buttons
			   axes: (OFDictionary *)axes;
@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,
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434







-
+







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

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

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

			@throw e;
		} @catch (OFInvalidArgumentException *e) {
			/* Not a game controller. */
442
443
444
445
446
447
448
449

450
451
452
453
454
455
456
442
443
444
445
446
447
448

449
450
451
452
453
454
455
456







-
+







	[controllers makeImmutable];

	objc_autoreleasePoolPop(pool);

	return controllers;
}

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

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFStringEncoding encoding = [OFLocale encoding];
		struct input_id inputID;
550
551
552
553
554
555
556
557
558


559
560
561
562
563
564
565
550
551
552
553
554
555
556


557
558
559
560
561
562
563
564
565







-
-
+
+







					[axes setObject: axis forKey: axisName];
				}
			}
		}
		[axes makeImmutable];

		_rawProfile = [[OHEvdevGameControllerProfile alloc]
		    oh_initWithButtons: buttons
				  axes: axes];
		    initWithButtons: buttons
			       axes: axes];

		[self oh_pollState];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
784
785
786
787
788
789
790
791
792


793
794
795
796
797
798
799
784
785
786
787
788
789
790


791
792
793
794
795
796
797
798
799







-
-
+
+








@implementation OHEvdevGameControllerAxis
@end

@implementation OHEvdevGameControllerProfile
@synthesize buttons = _buttons, axes = _axes;

- (instancetype)oh_initWithButtons: (OFDictionary *)buttons
			      axes: (OFDictionary *)axes
- (instancetype)initWithButtons: (OFDictionary *)buttons
			   axes: (OFDictionary *)axes
{
	self = [super init];

	@try {
		_buttons = [buttons retain];
		_axes = [axes retain];
	} @catch (id e) {

Modified src/hid/OHNintendo3DSGameController.m from [492047b1e9] to [6bc450a66c].

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







+
+
+
+
+


+







-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+



















+
-
-
-
+
+
+
+
+
-
-
+

+
-
-
-
-
+
+
+
+
+
+







	[_extendedGamepad release];

	[super dealloc];
}

- (void)retrieveState
{
	void *pool = objc_autoreleasePoolPush();
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *)
	    *buttons = _extendedGamepad.buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *directionalPads = _extendedGamepad.directionalPads;
	u32 keys;
	circlePosition leftPos, rightPos;
	OHGameControllerDirectionalPad *directionalPad;

	hidScanInput();

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

	[_extendedGamepad.northButton setValue: !!(keys & KEY_X)];
	[_extendedGamepad.southButton setValue: !!(keys & KEY_B)];
	[_extendedGamepad.westButton setValue: !!(keys & KEY_Y)];
	[_extendedGamepad.eastButton setValue: !!(keys & KEY_A)];
	[_extendedGamepad.leftShoulderButton setValue: !!(keys & KEY_L)];
	[_extendedGamepad.rightShoulderButton setValue: !!(keys & KEY_R)];
	[_extendedGamepad.leftTriggerButton setValue: !!(keys & KEY_ZL)];
	[_extendedGamepad.rightTriggerButton setValue: !!(keys & KEY_ZR)];
	[_extendedGamepad.menuButton setValue: !!(keys & KEY_START)];
	[_extendedGamepad.optionsButton setValue: !!(keys & KEY_SELECT)];
	[[buttons objectForKey: @"A"] setValue: !!(keys & KEY_A)];
	[[buttons objectForKey: @"B"] setValue: !!(keys & KEY_B)];
	[[buttons objectForKey: @"X"] setValue: !!(keys & KEY_X)];
	[[buttons objectForKey: @"Y"] setValue: !!(keys & KEY_Y)];
	[[buttons objectForKey: @"L"] setValue: !!(keys & KEY_L)];
	[[buttons objectForKey: @"R"] setValue: !!(keys & KEY_R)];
	[[buttons objectForKey: @"ZL"] setValue: !!(keys & KEY_ZL)];
	[[buttons objectForKey: @"ZR"] setValue: !!(keys & KEY_ZR)];
	[[buttons objectForKey: @"Start"] setValue: !!(keys & KEY_START)];
	[[buttons objectForKey: @"Select"] setValue: !!(keys & KEY_SELECT)];

	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;

	directionalPad = [directionalPads objectForKey: @"Circle Pad"];
	_extendedGamepad.leftThumbstick.xAxis.value = (float)leftPos.dx / 150;
	_extendedGamepad.leftThumbstick.yAxis.value = -(float)leftPos.dy / 150;
	_extendedGamepad.rightThumbstick.xAxis.value = (float)rightPos.dx / 150;
	directionalPad.xAxis.value = (float)leftPos.dx / 150;
	directionalPad.yAxis.value = -(float)leftPos.dy / 150;

	directionalPad = [directionalPads objectForKey: @"C-Stick"];
	directionalPad.xAxis.value = (float)rightPos.dx / 150;
	_extendedGamepad.rightThumbstick.yAxis.value =
	    -(float)rightPos.dy / 150;
	directionalPad.yAxis.value = -(float)rightPos.dy / 150;

	directionalPad = [directionalPads objectForKey: @"D-Pad"];
	[_extendedGamepad.dPad.up setValue: !!(keys & KEY_DUP)];
	[_extendedGamepad.dPad.down setValue: !!(keys & KEY_DDOWN)];
	[_extendedGamepad.dPad.left setValue: !!(keys & KEY_DLEFT)];
	[_extendedGamepad.dPad.right setValue: !!(keys & KEY_DRIGHT)];
	[directionalPad.up setValue: !!(keys & KEY_DUP)];
	[directionalPad.down setValue: !!(keys & KEY_DDOWN)];
	[directionalPad.left setValue: !!(keys & KEY_DLEFT)];
	[directionalPad.right setValue: !!(keys & KEY_DRIGHT)];

	objc_autoreleasePoolPop(pool);
}

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

Modified src/hid/OHNintendoSwitchGameController.h from [776eb7110c] to [ed7820b3e1].

25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39







-
+








OF_ASSUME_NONNULL_BEGIN

@class OHNintendoSwitchExtendedGamepad;

@interface OHNintendoSwitchGameController: OHGameController
{
	PadState _padState;
	PadState _pad;
	OHNintendoSwitchExtendedGamepad *_extendedGamepad;
}

- (instancetype)initWithIndex: (size_t)index;
@end

OF_ASSUME_NONNULL_END

Modified src/hid/OHNintendoSwitchGameController.m from [2ac5ac1c04] to [2baa5a5492].

72
73
74
75
76
77
78
79

80
81

82
83
84

85
86
87
88
89
90
91
72
73
74
75
76
77
78

79
80

81
82


83
84
85
86
87
88
89
90







-
+

-
+

-
-
+







}

- (instancetype)initWithIndex: (size_t)index
{
	self = [super init];

	@try {
		padInitialize(&_padState, HidNpadIdType_No1 + index,
		padInitialize(&_pad, HidNpadIdType_No1 + index,
		    (index == 0 ? HidNpadIdType_Handheld : 0));
		padUpdate(&_padState);
		padUpdate(&_pad);

		if (!(padGetAttributes(&_padState) &
		    HidNpadAttribute_IsConnected))
		if (!padIsConnected(&_pad))
			@throw [OFInitializationFailedException
			    exceptionWithClass: self.class];

		_extendedGamepad =
		    [[OHNintendoSwitchExtendedGamepad alloc] init];

		[self retrieveState];
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
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







-
-
+
+
















-
+






-
+







	    *buttons = _extendedGamepad.buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *directionalPads = _extendedGamepad.directionalPads;
	u64 keys;
	HidAnalogStickState stick;
	OHGameControllerDirectionalPad *directionalPad;

	padUpdate(&_padState);
	keys = padGetButtons(&_padState);
	padUpdate(&_pad);
	keys = padGetButtons(&_pad);

	[[buttons objectForKey: @"A"] setValue: !!(keys & HidNpadButton_A)];
	[[buttons objectForKey: @"B"] setValue: !!(keys & HidNpadButton_B)];
	[[buttons objectForKey: @"X"] setValue: !!(keys & HidNpadButton_X)];
	[[buttons objectForKey: @"Y"] setValue: !!(keys & HidNpadButton_Y)];
	[[buttons objectForKey: @"L"] setValue: !!(keys & HidNpadButton_L)];
	[[buttons objectForKey: @"R"] setValue: !!(keys & HidNpadButton_R)];
	[[buttons objectForKey: @"ZL"] setValue: !!(keys & HidNpadButton_ZL)];
	[[buttons objectForKey: @"ZR"] setValue: !!(keys & HidNpadButton_ZR)];
	[[buttons objectForKey: @"Left Thumbstick"]
	    setValue: !!(keys & HidNpadButton_StickL)];
	[[buttons objectForKey: @"Right Thumbstick"]
	    setValue: !!(keys & HidNpadButton_StickR)];
	[[buttons objectForKey: @"+"] setValue: !!(keys & HidNpadButton_Plus)];
	[[buttons objectForKey: @"-"] setValue: !!(keys & HidNpadButton_Minus)];

	stick = padGetStickPos(&_padState, 0);
	stick = padGetStickPos(&_pad, 0);
	directionalPad = [directionalPads objectForKey: @"Left Thumbstick"];
	[directionalPad.xAxis setValue:
	    (float)stick.x / (stick.x < 0 ? -INT16_MIN : INT16_MAX)];
	[directionalPad.yAxis setValue:
	    -(float)stick.y / (stick.y < 0 ? -INT16_MIN : INT16_MAX)];

	stick = padGetStickPos(&_padState, 1);
	stick = padGetStickPos(&_pad, 1);
	directionalPad = [directionalPads objectForKey: @"Right Thumbstick"];
	[directionalPad.xAxis setValue:
	    (float)stick.x / (stick.x < 0 ? -INT16_MIN : INT16_MAX)];
	[directionalPad.yAxis setValue:
	    -(float)stick.y / (stick.y < 0 ? -INT16_MIN : INT16_MAX)];

	directionalPad = [directionalPads objectForKey: @"D-Pad"];

Modified src/hid/OHXInputGameController.h from [ad658e8625] to [2ab30cd6c3].

28
29
30
31
32
33
34
35

36
37
38
39
40
41
42
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42







-
+







@interface OHXInputGameController: OHGameController
{
	DWORD _index;
	OFNumber *_Nullable _vendorID, *_Nullable _productID;
	OHXInputExtendedGamepad *_extendedGamepad;
}

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

#ifdef __cplusplus
extern "C" {
#endif
extern int OHXInputVersion;
#ifdef __cplusplus

Modified src/hid/OHXInputGameController.m from [fe788fd33d] to [cf9b9c985f].

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







-
+
















-
+







		void *pool = objc_autoreleasePoolPush();

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

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

			[controllers addObject: controller];
		}

		objc_autoreleasePoolPop(pool);
	}

	[controllers makeImmutable];

	return controllers;
}

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

	@try {
		XINPUT_STATE state = { 0 };

		if (XInputGetStateFuncPtr(index, &state) ==

Modified tests/gamecontroller/GameControllerTests.m from [aae21c76af] to [c3617c1d54].

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
185
186
187
188
189
190
191

192

193
194
195
196
197
198
199







-

-







- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif
#if defined(OF_NINTENDO_SWITCH)
	consoleInit(NULL);
#endif

#ifdef OF_NINTENDO_SWITCH
	while (appletMainLoop()) {
#else
	for (;;) {
#endif
		void *pool = objc_autoreleasePoolPush();
		OHGameController *leftJoyCon = nil, *rightJoyCon = nil;