ObjFW  Check-in [6e64dd58cb]

Overview
Comment:ObjFWHID: Restore support for Wii
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6e64dd58cbfff2b1c596213e8af30df2a2e78437642dfdeb44cc2d7d548133b9
User & Date: js on 2024-06-09 16:36:01
Other Links: manifest | tags
Context
2024-06-09
17:25
Fix build on Windows check-in: 80f3a121e5 user: js tags: trunk
16:36
ObjFWHID: Restore support for Wii check-in: 6e64dd58cb user: js tags: trunk
15:12
ObjFWHID: Make OHGameControllerProfile a protocol check-in: 8be13b6bc6 user: js tags: trunk
Changes

Modified configure.ac from [074a738523] to [9cf24819d0].

174
175
176
177
178
179
180

181
182
183
184
185
186
187
	LIBS="$LIBS -L$DEVKITPRO/libogc/lib/wii -lfat -logc"
	HID_LIBS="$HID_LIBS -lwiiuse -lbte"
	enable_shared="no"
	enable_threads="no"	# TODO
	with_tls="no"

	AC_DEFINE(OF_WII, 1, [Whether we are compiling for Wii])

	AC_SUBST(MAP_LDFLAGS, ['-Wl,-Map,$@.map'])
])

AC_ARG_WITH(wii-u,
	AS_HELP_STRING([--with-wii-u], [build for Wii U]))
AS_IF([test x"$with_wii_u" = x"yes"], [
	AS_IF([test x"$DEVKITPRO" = x""], [







>







174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	LIBS="$LIBS -L$DEVKITPRO/libogc/lib/wii -lfat -logc"
	HID_LIBS="$HID_LIBS -lwiiuse -lbte"
	enable_shared="no"
	enable_threads="no"	# TODO
	with_tls="no"

	AC_DEFINE(OF_WII, 1, [Whether we are compiling for Wii])
	AC_SUBST(USE_SRCS_WII, '${SRCS_WII}')
	AC_SUBST(MAP_LDFLAGS, ['-Wl,-Map,$@.map'])
])

AC_ARG_WITH(wii-u,
	AS_HELP_STRING([--with-wii-u], [build for Wii U]))
AS_IF([test x"$with_wii_u" = x"yes"], [
	AS_IF([test x"$DEVKITPRO" = x""], [

Modified extra.mk.in from [5c1c95c165] to [9f19d25a21].

105
106
107
108
109
110
111

112
113
114
115
USE_SRCS_PLUGINS = @USE_SRCS_PLUGINS@
USE_SRCS_SCTP = @USE_SRCS_SCTP@
USE_SRCS_SOCKETS = @USE_SRCS_SOCKETS@
USE_SRCS_SUBPROCESSES = @USE_SRCS_SUBPROCESSES@
USE_SRCS_TAGGED_POINTERS = @USE_SRCS_TAGGED_POINTERS@
USE_SRCS_THREADS = @USE_SRCS_THREADS@
USE_SRCS_UNIX_SOCKETS = @USE_SRCS_UNIX_SOCKETS@

USE_SRCS_WINDOWS = @USE_SRCS_WINDOWS@
USE_SRCS_XINPUT = @USE_SRCS_XINPUT@
WII_U_TESTS_LIBS = @WII_U_TESTS_LIBS@
WRAPPER = @WRAPPER@







>




105
106
107
108
109
110
111
112
113
114
115
116
USE_SRCS_PLUGINS = @USE_SRCS_PLUGINS@
USE_SRCS_SCTP = @USE_SRCS_SCTP@
USE_SRCS_SOCKETS = @USE_SRCS_SOCKETS@
USE_SRCS_SUBPROCESSES = @USE_SRCS_SUBPROCESSES@
USE_SRCS_TAGGED_POINTERS = @USE_SRCS_TAGGED_POINTERS@
USE_SRCS_THREADS = @USE_SRCS_THREADS@
USE_SRCS_UNIX_SOCKETS = @USE_SRCS_UNIX_SOCKETS@
USE_SRCS_WII = @USE_SRCS_WII@
USE_SRCS_WINDOWS = @USE_SRCS_WINDOWS@
USE_SRCS_XINPUT = @USE_SRCS_XINPUT@
WII_U_TESTS_LIBS = @WII_U_TESTS_LIBS@
WRAPPER = @WRAPPER@

Modified src/hid/Makefile from [7d8ac1b256] to [b9d799174b].

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
       OHGameControllerAxis.m		\
       OHGameControllerButton.m		\
       OHGameControllerDirectionalPad.m	\
       OHGameControllerElement.m	\
       ${USE_SRCS_EVDEV}		\
       ${USE_SRCS_NINTENDO_3DS}		\
       ${USE_SRCS_NINTENDO_DS}		\

       ${USE_SRCS_XINPUT}
SRCS_EVDEV = OHEvdevDualSense.m				\
	     OHEvdevDualShock4.m			\
	     OHEvdevExtendedGamepad.m			\
	     OHEvdevGameController.m			\
	     OHEvdevPlayStationExtendedGamepad.m	\
	     OHEvdevStadiaExtendedGamepad.m
SRCS_NINTENDO_3DS = OHNintendo3DSExtendedGamepad.m	\
		    OHNintendo3DSGameController.m
SRCS_NINTENDO_DS = OHNintendoDSGamepad.m	\
		   OHNintendoDSGameController.m


SRCS_XINPUT = OHXInputExtendedGamepad.m	\
	      OHXInputGameController.m

INCLUDES := ${SRCS:.m=.h}		\
	    OHExtendedGamepad.h		\
	    OHGameControllerProfile.h	\
	    OHGamepad.h			\







>











>
>







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
       OHGameControllerAxis.m		\
       OHGameControllerButton.m		\
       OHGameControllerDirectionalPad.m	\
       OHGameControllerElement.m	\
       ${USE_SRCS_EVDEV}		\
       ${USE_SRCS_NINTENDO_3DS}		\
       ${USE_SRCS_NINTENDO_DS}		\
       ${USE_SRCS_WII}			\
       ${USE_SRCS_XINPUT}
SRCS_EVDEV = OHEvdevDualSense.m				\
	     OHEvdevDualShock4.m			\
	     OHEvdevExtendedGamepad.m			\
	     OHEvdevGameController.m			\
	     OHEvdevPlayStationExtendedGamepad.m	\
	     OHEvdevStadiaExtendedGamepad.m
SRCS_NINTENDO_3DS = OHNintendo3DSExtendedGamepad.m	\
		    OHNintendo3DSGameController.m
SRCS_NINTENDO_DS = OHNintendoDSGamepad.m	\
		   OHNintendoDSGameController.m
SRCS_WII = OHWiiClassicController.m	\
	   OHWiiGameController.m
SRCS_XINPUT = OHXInputExtendedGamepad.m	\
	      OHXInputGameController.m

INCLUDES := ${SRCS:.m=.h}		\
	    OHExtendedGamepad.h		\
	    OHGameControllerProfile.h	\
	    OHGamepad.h			\

Modified src/hid/OHEvdevGameController.h from [60077568e2] to [428d509e52].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 */

#import "OHGameController.h"
#import "OHGameControllerProfile.h"

OF_ASSUME_NONNULL_BEGIN

@class OHGameControllerProfile;

@interface OHEvdevGameController: OHGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;
	uint16_t _vendorID, _productID;







<
<







18
19
20
21
22
23
24


25
26
27
28
29
30
31
 */

#import "OHGameController.h"
#import "OHGameControllerProfile.h"

OF_ASSUME_NONNULL_BEGIN



@interface OHEvdevGameController: OHGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;
	uint16_t _vendorID, _productID;

Modified src/hid/OHGameController.m from [386bef4691] to [9eedd672bc].

32
33
34
35
36
37
38



39
40
41
42
43
44
45
#endif
#ifdef OF_NINTENDO_DS
# import "OHNintendoDSGameController.h"
#endif
#ifdef OF_NINTENDO_3DS
# import "OHNintendo3DSGameController.h"
#endif




const uint16_t OHVendorIDSony = 0x054C;
const uint16_t OHVendorIDNintendo = 0x057E;
const uint16_t OHVendorIDGoogle = 0x18D1;
const uint16_t OHProductIDDualShock4 = 0x09CC;
const uint16_t OHProductIDDualSense = 0x0CE6;
const uint16_t OHProductIDLeftJoyCon = 0x2006;







>
>
>







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#endif
#ifdef OF_NINTENDO_DS
# import "OHNintendoDSGameController.h"
#endif
#ifdef OF_NINTENDO_3DS
# import "OHNintendo3DSGameController.h"
#endif
#ifdef OF_WII
# import "OHWiiGameController.h"
#endif

const uint16_t OHVendorIDSony = 0x054C;
const uint16_t OHVendorIDNintendo = 0x057E;
const uint16_t OHVendorIDGoogle = 0x18D1;
const uint16_t OHProductIDDualShock4 = 0x09CC;
const uint16_t OHProductIDDualSense = 0x0CE6;
const uint16_t OHProductIDLeftJoyCon = 0x2006;
56
57
58
59
60
61
62


63
64
65
66
67
68
69
	return [OHEvdevGameController controllers];
#elif defined(OF_WINDOWS)
	return [OHXInputGameController controllers];
#elif defined(OF_NINTENDO_DS)
	return [OHNintendoDSGameController controllers];
#elif defined(OF_NINTENDO_3DS)
	return [OHNintendo3DSGameController controllers];


#else
	return [OFArray array];
#endif
}

- (instancetype)init
{







>
>







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	return [OHEvdevGameController controllers];
#elif defined(OF_WINDOWS)
	return [OHXInputGameController controllers];
#elif defined(OF_NINTENDO_DS)
	return [OHNintendoDSGameController controllers];
#elif defined(OF_NINTENDO_3DS)
	return [OHNintendo3DSGameController controllers];
#elif defined(OF_WII)
	return [OHWiiGameController controllers];
#else
	return [OFArray array];
#endif
}

- (instancetype)init
{

Modified src/hid/OHNintendo3DSGameController.m from [0167f74bbf] to [492047b1e9].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 */

#include "config.h"

#import "OHNintendo3DSGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHNintendo3DSExtendedGamepad.h"

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







<







18
19
20
21
22
23
24

25
26
27
28
29
30
31
 */

#include "config.h"

#import "OHNintendo3DSGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"

#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHNintendo3DSExtendedGamepad.h"

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

Modified src/hid/OHNintendoDSGameController.m from [a4f9f01eef] to [7132510c4f].

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 */

#include "config.h"

#import "OHNintendoDSGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHNintendoDSGamepad.h"

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








<







18
19
20
21
22
23
24

25
26
27
28
29
30
31
 */

#include "config.h"

#import "OHNintendoDSGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"

#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHNintendoDSGamepad.h"

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

Added src/hid/OHWiiClassicController.h version [88de8f00f1].

































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OHExtendedGamepad.h"

OF_ASSUME_NONNULL_BEGIN

@interface OHWiiClassicController: OFObject <OHExtendedGamepad>
{
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *_directionalPads;
}
@end

OF_ASSUME_NONNULL_END

Added src/hid/OHWiiClassicController.m version [a6258ae305].























































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OHWiiClassicController.h"
#import "OFDictionary.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"

static OFString *const buttonNames[] = {
	@"A", @"B", @"X", @"Y", @"L", @"R", @"ZL", @"ZR", @"+", @"-", @"Home"
};
static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames);

@implementation OHWiiClassicController
@synthesize buttons = _buttons, directionalPads = _directionalPads;

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

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFMutableDictionary *buttons =
		    [OFMutableDictionary dictionaryWithCapacity: numButtons];
		OFMutableDictionary *directionalPads;
		OHGameControllerAxis *xAxis, *yAxis;
		OHGameControllerDirectionalPad *directionalPad;
		OHGameControllerButton *up, *down, *left, *right;

		for (size_t i = 0; i < numButtons; i++) {
			OHGameControllerButton *button =
			    [[OHGameControllerButton alloc]
			    initWithName: buttonNames[i]];
			[buttons setObject: button forKey: buttonNames[i]];
		}
		[buttons makeImmutable];
		_buttons = [buttons retain];

		directionalPads =
		    [OFMutableDictionary dictionaryWithCapacity: 3];

		xAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"X"] autorelease];
		yAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"Y"] autorelease];
		directionalPad = [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"Left Thumbstick"
			   xAxis: xAxis
			   yAxis: yAxis] autorelease];
		[directionalPads setObject: directionalPad
				    forKey: @"Left Thumbstick"];

		xAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"RX"] autorelease];
		yAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"RY"] autorelease];
		directionalPad = [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"Right Thumbstick"
			   xAxis: xAxis
			   yAxis: yAxis] autorelease];
		[directionalPads setObject: directionalPad
				    forKey: @"Right Thumbstick"];

		up = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Up"] autorelease];
		down = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Down"] autorelease];
		left = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Left"] autorelease];
		right = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Right"] autorelease];
		directionalPad = [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"D-Pad"
			      up: up
			    down: down
			    left: left
			   right: right] autorelease];
		[directionalPads setObject: directionalPad forKey: @"D-Pad"];

		[directionalPads makeImmutable];
		_directionalPads = [directionalPads retain];

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

	return self;
}

- (void)dealloc
{
	[_buttons release];
	[_directionalPads release];

	[super dealloc];
}

- (OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *)axes
{
	return [OFDictionary dictionary];
}

- (OHGameControllerButton *)northButton
{
	return [_buttons objectForKey: @"X"];
}

- (OHGameControllerButton *)southButton
{
	return [_buttons objectForKey: @"B"];
}

- (OHGameControllerButton *)westButton
{
	return [_buttons objectForKey: @"Y"];
}

- (OHGameControllerButton *)eastButton
{
	return [_buttons objectForKey: @"A"];
}

- (OHGameControllerButton *)leftShoulderButton
{
	return [_buttons objectForKey: @"ZL"];
}

- (OHGameControllerButton *)rightShoulderButton
{
	return [_buttons objectForKey: @"ZR"];
}

- (OHGameControllerButton *)leftTriggerButton
{
	return [_buttons objectForKey: @"L"];
}

- (OHGameControllerButton *)rightTriggerButton
{
	return [_buttons objectForKey: @"R"];
}

- (OHGameControllerButton *)leftThumbstickButton
{
	return nil;
}

- (OHGameControllerButton *)rightThumbstickButton
{
	return nil;
}

- (OHGameControllerButton *)menuButton
{
	return [_buttons objectForKey: @"+"];
}

- (OHGameControllerButton *)optionsButton
{
	return [_buttons objectForKey: @"-"];
}

- (OHGameControllerButton *)homeButton
{
	return [_buttons objectForKey: @"Home"];
}

- (OHGameControllerDirectionalPad *)leftThumbstick
{
	return [_directionalPads objectForKey: @"Left Thumbstick"];
}

- (OHGameControllerDirectionalPad *)rightThumbstick
{
	return [_directionalPads objectForKey: @"Right Thumbstick"];
}

- (OHGameControllerDirectionalPad *)dPad
{
	return [_directionalPads objectForKey: @"D-Pad"];
}
@end

Added src/hid/OHWiiGameController.h version [a940a20263].





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OHGameController.h"

OF_ASSUME_NONNULL_BEGIN

@interface OHWiiGameController: OHGameController
{
	int32_t _index;
	uint32_t _type;
	id <OHGameControllerProfile> _rawProfile;
}

- (instancetype)initWithIndex: (int32_t)index type: (uint32_t)type;
@end

OF_ASSUME_NONNULL_END

Added src/hid/OHWiiGameController.m version [38aa35cd32].





































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
302
303
304
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
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
/*
 * 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 "OHWiiGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHWiiClassicController.h"

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

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

@interface OHWiiGameControllerProfile: OFObject <OHGameControllerProfile>
{
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *_directionalPads;
}

- (instancetype)initWithType: (uint32_t)type;
@end

static OFString *const buttonNames[] = {
	@"A", @"B", @"1", @"2", @"+", @"-", @"Home"
};
static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames);
static OFString *const nunchukButtonNames[] = {
	@"C", @"Z"
};
static const size_t numNunchukButtons =
    sizeof(nunchukButtonNames) / sizeof(*nunchukButtonNames);

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 OHWiiGameController
@synthesize rawProfile = _rawProfile;

+ (void)initialize
{
	if (self != [OHWiiGameController class])
		return;

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

+ (OFArray OF_GENERIC(OHGameController *) *)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: [[[OHWiiGameController alloc]
			    initWithIndex: i
				     type: type] autorelease]];
	}

	[controllers makeImmutable];

	objc_autoreleasePoolPop(pool);

	return controllers;
}

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

	@try {
		_index = index;
		_type = type;

		if (type == WPAD_EXP_CLASSIC)
			_rawProfile = [[OHWiiClassicController alloc] init];
		else
			_rawProfile = [[OHWiiGameControllerProfile alloc]
			    initWithType: type];

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

	return self;
}

- (void)dealloc
{
	[_rawProfile release];

	[super dealloc];
}

- (void)retrieveState
{
	OFDictionary *buttons = _rawProfile.buttons;
	OFDictionary *directionalPads = _rawProfile.directionalPads;
	WPADData *data;

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

	data = WPAD_Data(_index);

	if (_type == WPAD_EXP_NONE || _type == WPAD_EXP_NUNCHUK) {
		OHGameControllerDirectionalPad *dPad =
		    [directionalPads objectForKey: @"D-Pad"];

		[[buttons objectForKey: @"A"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_A)];
		[[buttons objectForKey: @"B"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_B)];
		[[buttons objectForKey: @"1"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_1)];
		[[buttons objectForKey: @"2"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_2)];
		[[buttons objectForKey: @"+"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_PLUS)];
		[[buttons objectForKey: @"-"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_MINUS)];
		[[buttons objectForKey: @"Home"]
		    setValue: !!(data->btns_h & WPAD_BUTTON_HOME)];

		[dPad.up setValue: !!(data->btns_h & WPAD_BUTTON_UP)];
		[dPad.down setValue: !!(data->btns_h & WPAD_BUTTON_DOWN)];
		[dPad.left setValue: !!(data->btns_h & WPAD_BUTTON_LEFT)];
		[dPad.right setValue: !!(data->btns_h & WPAD_BUTTON_RIGHT)];
	}

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

		[[buttons objectForKey: @"C"]
		    setValue: !!(data->btns_h & WPAD_NUNCHUK_BUTTON_C)];
		[[buttons objectForKey: @"Z"]
		    setValue: !!(data->btns_h & WPAD_NUNCHUK_BUTTON_Z)];

		directionalPad =
		    [directionalPads objectForKey: @"Analog Stick"];
		directionalPad.xAxis.value =
		    scale(js->pos.x, js->min.x, js->max.x, js->center.x);
		directionalPad.yAxis.value =
		    -scale(js->pos.y, js->min.y, js->max.y, js->center.y);
	}

	if (_type == WPAD_EXP_CLASSIC) {
		joystick_t *ljs = &data->exp.classic.ljs;
		joystick_t *rjs = &data->exp.classic.rjs;
		OHGameControllerDirectionalPad *directionalPad;

		[[buttons objectForKey: @"X"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_X)];
		[[buttons objectForKey: @"B"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_B)];
		[[buttons objectForKey: @"Y"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_Y)];
		[[buttons objectForKey: @"A"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_A)];
		[[buttons objectForKey: @"ZL"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_ZL)];
		[[buttons objectForKey: @"ZR"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_ZR)];
		[[buttons objectForKey: @"+"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_PLUS)];
		[[buttons objectForKey: @"-"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_MINUS)];
		[[buttons objectForKey: @"Home"]
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_HOME)];

		directionalPad =
		    [directionalPads objectForKey: @"Left Thumbstick"];
		directionalPad.xAxis.value =
		    scale(ljs->pos.x, ljs->min.x, ljs->max.x, ljs->center.x);
		directionalPad.yAxis.value =
		    -scale(ljs->pos.y, ljs->min.y, ljs->max.y, ljs->center.y);

		directionalPad =
		    [directionalPads objectForKey: @"Right Thumbstick"];
		directionalPad.xAxis.value =
		    scale(rjs->pos.x, rjs->min.x, rjs->max.x, rjs->center.x);
		directionalPad.yAxis.value =
		    -scale(rjs->pos.y, rjs->min.y, rjs->max.y, rjs->center.y);

		[[buttons objectForKey: @"L"]
		    setValue: data->exp.classic.l_shoulder];
		[[buttons objectForKey: @"R"]
		    setValue: data->exp.classic.r_shoulder];

		directionalPad = [directionalPads objectForKey: @"D-Pad"];
		[directionalPad.up
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_UP)];
		[directionalPad.down
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_DOWN)];
		[directionalPad.left
		    setValue: !!(data->btns_h & WPAD_CLASSIC_BUTTON_LEFT)];
		[directionalPad.right
		    setValue:  !!(data->btns_h & WPAD_CLASSIC_BUTTON_RIGHT)];
	}
}

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

- (id <OHGamepad>)gamepad
{
	if (_type == WPAD_EXP_CLASSIC)
		return (id <OHGamepad>)_rawProfile;

	return nil;
}

- (id <OHExtendedGamepad>)extendedGamepad
{
	if (_type == WPAD_EXP_CLASSIC)
		return (id <OHExtendedGamepad>)_rawProfile;

	return nil;
}
@end

@implementation OHWiiGameControllerProfile
@synthesize buttons = _buttons, directionalPads = _directionalPads;

- (instancetype)initWithType: (uint32_t)type
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFMutableDictionary *buttons;
		OFMutableDictionary *directionalPads;
		OHGameControllerDirectionalPad *directionalPad;
		OHGameControllerButton *up, *down, *left, *right;
		OHGameControllerAxis *xAxis, *yAxis;

		if (type != WPAD_EXP_NONE && type != WPAD_EXP_NUNCHUK)
			@throw [OFInvalidArgumentException exception];

		buttons = [OFMutableDictionary
		    dictionaryWithCapacity: numButtons + numNunchukButtons];

		for (size_t i = 0; i < numButtons; i++) {
			OHGameControllerButton *button =
			    [[[OHGameControllerButton alloc]
			    initWithName: buttonNames[i]] autorelease];
			[buttons setObject: button forKey: buttonNames[i]];
		}

		directionalPads = [OFMutableDictionary dictionary];

		up = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Up"] autorelease];
		down = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Down"] autorelease];
		left = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Left"] autorelease];
		right = [[[OHGameControllerButton alloc]
		    initWithName: @"D-Pad Right"] autorelease];
		directionalPad = [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"D-Pad"
			      up: up
			    down: down
			    left: left
			   right: right] autorelease];
		[directionalPads setObject: directionalPad forKey: @"D-Pad"];

		if (type == WPAD_EXP_NUNCHUK) {
			for (size_t i = 0; i < numNunchukButtons; i++) {
				OHGameControllerButton *button =
				    [[[OHGameControllerButton alloc]
				    initWithName: nunchukButtonNames[i]]
					autorelease];

				[buttons setObject: button
					    forKey: nunchukButtonNames[i]];
			}

			xAxis = [[[OHGameControllerAxis alloc]
			    initWithName: @"X"] autorelease];
			yAxis = [[[OHGameControllerAxis alloc]
			    initWithName: @"Y"] autorelease];
			directionalPad = [[[OHGameControllerDirectionalPad alloc]
			    initWithName: @"Analog Stick"
				   xAxis: xAxis
				   yAxis: yAxis] autorelease];
			[directionalPads setObject: directionalPad
					    forKey: @"Analog Stick"];
		}

		[buttons makeImmutable];
		[directionalPads makeImmutable];
		_buttons = [buttons retain];
		_directionalPads = [directionalPads retain];

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

	return self;
}

- (void)dealloc
{
	[_buttons release];
	[_directionalPads release];

	[super dealloc];
}

- (OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *)axes
{
	return [OFDictionary dictionary];
}
@end

Modified src/hid/OHXInputExtendedGamepad.m from [c47a92c25d] to [845a68fc44].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "config.h"

#import "OHXInputExtendedGamepad.h"
#import "OFDictionary.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHXInputGameController.h"

static OFString *const buttonNames[] = {
	@"A", @"B", @"X", @"Y", @"LB", @"RB", @"LT", @"RT", @"LSB", @"RSB",
	@"Start", @"Back", @"Guide"
};
static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames);








<







20
21
22
23
24
25
26

27
28
29
30
31
32
33
#include "config.h"

#import "OHXInputExtendedGamepad.h"
#import "OFDictionary.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"


static OFString *const buttonNames[] = {
	@"A", @"B", @"X", @"Y", @"LB", @"RB", @"LT", @"RT", @"LSB", @"RSB",
	@"Start", @"Back", @"Guide"
};
static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames);