ObjFW  Check-in [68ef38f917]

Overview
Comment:Add OFCombinedJoyConsGameController

This allows combining two Joy-Cons into one controller by remapping the
buttons and analog sticks.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 68ef38f91716432cd7fa986bab85e19d02c6a52d55c9b678fd2e53701ec5b8be
User & Date: js on 2024-05-22 23:54:29
Other Links: manifest | tags
Context
2024-05-24
21:17
Add RISC-V 64/ELF assembly lookup implementation check-in: bcff3f8c7a user: js tags: trunk
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
Changes

Modified src/hid/Makefile from [cb5296d389] to [8756119022].

1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = OFGameController.m


INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h

includesubdir = ObjFWHID

include ../../buildsys.mk











|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = OFGameController.m			\
       OFCombinedJoyConsGameController.m

INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h

includesubdir = ObjFWHID

include ../../buildsys.mk

Added src/hid/OFCombinedJoyConsGameController.h version [56e6c1cdbc].























































































































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

OF_ASSUME_NONNULL_BEGIN

/**
 * @brief A class for combining two Joy-Cons into a single
 *	  @ref OFGameController.
 */
OF_SUBCLASSING_RESTRICTED
@interface OFCombinedJoyConsGameController: OFGameController
{
	OFGameController *_leftJoyCon, *_rightJoyCon;
}

/**
 * @brief Creates a new game controller with the specified left and right
 *	  Joy-Con.
 *
 * @param leftJoyCon The left Joy-Con
 * @param rightJoyCon The right Joy-Con
 * @return A new game controller combining both Joy-Cons into a single game
 *	   controller
 */
+ (instancetype)controllerWithLeftJoyCon: (OFGameController *)leftJoyCon
			     rightJoyCon: (OFGameController *)rightJoyCon;

/**
 * @brief Initialized an already allocated combined Joy-Cons game controller
 *	  with the specified left and right Joy-Con.
 *
 * @param leftJoyCon The left Joy-Con
 * @param rightJoyCon The right Joy-Con
 * @return An initialized combined Joy-Cons game controller combining both
 *	   Joy-Cons into a single game controller
 */
- (instancetype)initWithLeftJoyCon: (OFGameController *)leftJoyCon
		       rightJoyCon: (OFGameController *)rightJoyCon;
@end

OF_ASSUME_NONNULL_END

Added src/hid/OFCombinedJoyConsGameController.m version [44f0093bf7].



































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OFCombinedJoyConsGameController.h"
#import "OFNumber.h"
#import "OFSet.h"

#import "OFInvalidArgumentException.h"

@implementation OFCombinedJoyConsGameController
+ (instancetype)controllerWithLeftJoyCon: (OFGameController *)leftJoyCon
			     rightJoyCon: (OFGameController *)rightJoyCon
{
	return [[[self alloc] initWithLeftJoyCon: leftJoyCon
				     rightJoyCon: rightJoyCon] autorelease];
}

- (instancetype)initWithLeftJoyCon: (OFGameController *)leftJoyCon
		       rightJoyCon: (OFGameController *)rightJoyCon
{
	self = [super init];

	@try {
		if (leftJoyCon.vendorID.unsignedShortValue != 0x057E ||
		    rightJoyCon.vendorID.unsignedShortValue != 0x057E)
			@throw [OFInvalidArgumentException exception];

		if (leftJoyCon.productID.unsignedShortValue != 0x2006 ||
		    rightJoyCon.productID.unsignedShortValue != 0x2007)
			@throw [OFInvalidArgumentException exception];

		_leftJoyCon = [leftJoyCon retain];
		_rightJoyCon = [rightJoyCon retain];

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

	return self;
}

- (void)dealloc
{
	[_leftJoyCon release];
	[_rightJoyCon release];

	[super dealloc];
}

- (void)retrieveState
{
	[_leftJoyCon retrieveState];
	[_rightJoyCon retrieveState];
}

- (OFString *)name
{
	return @"Combined Joy-Cons";
}

- (OFSet *)buttons
{
	return [OFSet setWithObjects:
	    OFGameControllerNorthButton,
	    OFGameControllerSouthButton,
	    OFGameControllerWestButton,
	    OFGameControllerEastButton,
	    OFGameControllerLeftTriggerButton,
	    OFGameControllerRightTriggerButton,
	    OFGameControllerLeftShoulderButton,
	    OFGameControllerRightShoulderButton,
	    OFGameControllerLeftStickButton,
	    OFGameControllerRightStickButton,
	    OFGameControllerDPadUpButton,
	    OFGameControllerDPadDownButton,
	    OFGameControllerDPadLeftButton,
	    OFGameControllerDPadRightButton,
	    OFGameControllerStartButton,
	    OFGameControllerSelectButton,
	    OFGameControllerHomeButton,
	    OFGameControllerCaptureButton,
	    @"Left SL",
	    @"Right SL",
	    @"Left SR",
	    @"Right SR", nil];
}

- (OFSet OF_GENERIC(OFGameControllerButton) *)pressedButtons
{
	OFMutableSet *pressedButtons = [OFMutableSet setWithCapacity: 22];
	OFSet *leftPressedButtons = _leftJoyCon.pressedButtons;
	OFSet *rightPressedButtons = _rightJoyCon.pressedButtons;

	if ([rightPressedButtons containsObject: OFGameControllerEastButton])
		[pressedButtons addObject: OFGameControllerNorthButton];
	if ([rightPressedButtons containsObject: OFGameControllerWestButton])
		[pressedButtons addObject: OFGameControllerSouthButton];
	if ([rightPressedButtons containsObject: OFGameControllerNorthButton])
		[pressedButtons addObject: OFGameControllerWestButton];
	if ([rightPressedButtons containsObject: OFGameControllerSouthButton])
		[pressedButtons addObject: OFGameControllerEastButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftTriggerButton])
		[pressedButtons addObject: OFGameControllerLeftTriggerButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightTriggerButton])
		[pressedButtons addObject: OFGameControllerRightTriggerButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftShoulderButton])
		[pressedButtons addObject: OFGameControllerLeftShoulderButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightShoulderButton])
		[pressedButtons addObject: OFGameControllerRightShoulderButton];
	if ([leftPressedButtons containsObject:
	    OFGameControllerLeftStickButton])
		[pressedButtons addObject: OFGameControllerLeftStickButton];
	if ([rightPressedButtons containsObject:
	    OFGameControllerRightStickButton])
		[pressedButtons addObject: OFGameControllerRightStickButton];
	if ([leftPressedButtons containsObject: OFGameControllerWestButton])
		[pressedButtons addObject: OFGameControllerDPadUpButton];
	if ([leftPressedButtons containsObject: OFGameControllerEastButton])
		[pressedButtons addObject: OFGameControllerDPadDownButton];
	if ([leftPressedButtons containsObject: OFGameControllerSouthButton])
		[pressedButtons addObject: OFGameControllerDPadLeftButton];
	if ([leftPressedButtons containsObject: OFGameControllerNorthButton])
		[pressedButtons addObject: OFGameControllerDPadRightButton];
	if ([rightPressedButtons containsObject: OFGameControllerStartButton])
		[pressedButtons addObject: OFGameControllerStartButton];
	if ([leftPressedButtons containsObject: OFGameControllerSelectButton])
		[pressedButtons addObject: OFGameControllerSelectButton];
	if ([rightPressedButtons containsObject: OFGameControllerHomeButton])
		[pressedButtons addObject: OFGameControllerHomeButton];
	if ([leftPressedButtons containsObject: OFGameControllerCaptureButton])
		[pressedButtons addObject: OFGameControllerCaptureButton];
	if ([leftPressedButtons containsObject: @"SL"])
		[pressedButtons addObject: @"Left SL"];
	if ([rightPressedButtons containsObject: @"SL"])
		[pressedButtons addObject: @"Right SL"];
	if ([leftPressedButtons containsObject: @"SR"])
		[pressedButtons addObject: @"Left SR"];
	if ([rightPressedButtons containsObject: @"SR"])
		[pressedButtons addObject: @"Right SR"];

	[pressedButtons makeImmutable];

	return pressedButtons;
}

- (bool)hasLeftAnalogStick
{
	return true;
}

- (bool)hasRightAnalogStick
{
	return true;
}

- (OFPoint)leftAnalogStickPosition
{
	OFPoint position = _leftJoyCon.leftAnalogStickPosition;

	return OFMakePoint(-position.y, position.x);
}

- (OFPoint)rightAnalogStickPosition
{
	OFPoint position = _rightJoyCon.leftAnalogStickPosition;

	return OFMakePoint(position.y, -position.x);
}
@end

Modified src/hid/ObjFWHID.h from [9c2fb5d2bc] to [e3220aba7e].

14
15
16
17
18
19
20

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








>
14
15
16
17
18
19
20
21
 *
 * 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 "OFGameController.h"
#import "OFCombinedJoyConsGameController.h"

Modified tests/gamecontroller/GameControllerTests.m from [bb51f5cad3] to [1bbc654209].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
 */

#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFColor.h"

#import "OFDate.h"
#import "OFGameController.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFThread.h"








>







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

#include "config.h"

#import "OFApplication.h"
#import "OFArray.h"
#import "OFColor.h"
#import "OFCombinedJoyConsGameController.h"
#import "OFDate.h"
#import "OFGameController.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFThread.h"

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
# define red maroon
# define yellow olive
# define gray silver
#endif

@interface GameControllerTests: OFObject <OFApplicationDelegate>
{
	OFArray OF_GENERIC(OFGameController *) *_controllers;
	OFDate *_lastControllersUpdate;
}
@end

OF_APPLICATION_DELEGATE(GameControllerTests)

@implementation GameControllerTests
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();

		if (_lastControllersUpdate == nil ||
		    -[_lastControllersUpdate timeIntervalSinceNow] > 1) {


			[_controllers release];
			[_lastControllersUpdate release];

			_controllers = [[OFGameController controllers] retain];

			_lastControllersUpdate = [[OFDate alloc] init];





















			[OFStdOut clear];
		}

		[OFStdOut setCursorPosition: OFMakePoint(0, 0)];

		for (OFGameController *controller in _controllers) {







|


















>
>



|
>

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







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
# define red maroon
# define yellow olive
# define gray silver
#endif

@interface GameControllerTests: OFObject <OFApplicationDelegate>
{
	OFMutableArray OF_GENERIC(OFGameController *) *_controllers;
	OFDate *_lastControllersUpdate;
}
@end

OF_APPLICATION_DELEGATE(GameControllerTests)

@implementation GameControllerTests
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif

	for (;;) {
		void *pool = objc_autoreleasePoolPush();

		if (_lastControllersUpdate == nil ||
		    -[_lastControllersUpdate timeIntervalSinceNow] > 1) {
			OFGameController *leftJoyCon = nil, *rightJoyCon = nil;

			[_controllers release];
			[_lastControllersUpdate release];

			_controllers = [[OFGameController controllers]
			    mutableCopy];
			_lastControllersUpdate = [[OFDate alloc] init];

			for (OFGameController *controller in _controllers) {
				if (controller.vendorID.unsignedShortValue !=
				    0x057E)
					continue;

				if (controller.productID.unsignedShortValue ==
				    0x2006)
					leftJoyCon = controller;
				else if (
				    controller.productID.unsignedShortValue ==
				    0x2007)
					rightJoyCon = controller;
			}

			if (leftJoyCon != nil && rightJoyCon != nil)
				[_controllers addObject:
				    [OFCombinedJoyConsGameController
				    controllerWithLeftJoyCon: leftJoyCon
						 rightJoyCon: rightJoyCon]];

			[OFStdOut clear];
		}

		[OFStdOut setCursorPosition: OFMakePoint(0, 0)];

		for (OFGameController *controller in _controllers) {