ObjFW  Check-in [7f0017a058]

Overview
Comment:OFGameController: Support Wii Classic Controller
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gamecontroller
Files: files | file ages | folders
SHA3-256: 7f0017a0585074a43c639daa9a93e2ef79bdca72a15a2b466a9e3618273c13da
User & Date: js on 2024-05-20 02:43:43
Other Links: branch diff | manifest | tags
Context
2024-05-20
03:13
OFXInputGameController: Support for Home button check-in: f609457ac5 user: js tags: gamecontroller
02:43
OFGameController: Support Wii Classic Controller check-in: 7f0017a058 user: js tags: gamecontroller
01:42
tests/gamecontroller: Clear screen when necessary check-in: 6a8f71e065 user: js tags: gamecontroller
Changes

Modified src/hid/OFWiiGameController.h from [d872d533c3] to [967feef34b].

22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
OF_ASSUME_NONNULL_BEGIN

@interface OFWiiGameController: OFGameController
{
	int32_t _index;
	uint32_t _type;
	OFMutableSet OF_GENERIC(OFGameControllerButton) *_pressedButtons;
	OFPoint _leftAnalogStickPosition;

}

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

OF_ASSUME_NONNULL_END







|
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

Modified src/hid/OFWiiGameController.m from [30f9adda3d] to [c3b51e1e66].

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

			[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: 13];

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








|
>




















|







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

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
		[_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;

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

		js = &data->exp.nunchuk.js;
		_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));



	}










































}












- (OFString *)name
{
	if (_type == WPAD_EXP_NUNCHUK)
		return @"Wiimote with Nunchuk";



	return @"Wiimote";
}

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

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





	}

	[buttons makeImmutable];

	return buttons;
}

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

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

- (bool)hasRightAnalogStick
{
	return false;
}

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

	return _leftAnalogStickPosition;
}





















@end







|








<



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

>
>
>
>
>




|
>
>
|




|
















>
>
>
>
>














|




|




|





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

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