ObjFW  Check-in [96c7c533b1]

Overview
Comment:ObjFWHID: Add profiles for Joy-Cons
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 96c7c533b11fa5f0b6c35f74107fc5002ab5586fa5c04aa33029dc0be299b7e7
User & Date: js on 2024-06-30 12:17:20
Other Links: manifest | tags
Context
2024-07-01
18:41
ObjFWHID: Make most profiles public check-in: dfaf830200 user: js tags: trunk
2024-06-30
12:17
ObjFWHID: Add profiles for Joy-Cons check-in: 96c7c533b1 user: js tags: trunk
2024-06-23
22:04
runtime: Properly hide symbols check-in: a38d711e37 user: js tags: trunk
Changes

Modified src/hid/Makefile from [9ac3135daf] to [f27c2669b4].

24
25
26
27
28
29
30

31

32
33
34
35
36
37
38

SRCS += OHDualSenseGamepad.m			\
	OHDualShock4Gamepad.m			\
	OHEmulatedGameControllerAxis.m		\
	OHEmulatedGameControllerButton.m	\
	OHEmulatedGameControllerTriggerButton.m	\
	OHExtendedN64Controller.m		\

	OHN64Controller.m			\

	OHStadiaGamepad.m			\
	OHXboxGamepad.m				\
	${USE_SRCS_EVDEV}			\
	${USE_SRCS_NINTENDO_3DS}		\
	${USE_SRCS_NINTENDO_DS}			\
	${USE_SRCS_NINTENDO_SWITCH}		\
	${USE_SRCS_WII}				\







>

>







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

SRCS += OHDualSenseGamepad.m			\
	OHDualShock4Gamepad.m			\
	OHEmulatedGameControllerAxis.m		\
	OHEmulatedGameControllerButton.m	\
	OHEmulatedGameControllerTriggerButton.m	\
	OHExtendedN64Controller.m		\
	OHLeftJoyCon.m				\
	OHN64Controller.m			\
	OHRightJoyCon.m				\
	OHStadiaGamepad.m			\
	OHXboxGamepad.m				\
	${USE_SRCS_EVDEV}			\
	${USE_SRCS_NINTENDO_3DS}		\
	${USE_SRCS_NINTENDO_DS}			\
	${USE_SRCS_NINTENDO_SWITCH}		\
	${USE_SRCS_WII}				\

Modified src/hid/OHCombinedJoyCons.m from [90a7f9c337] to [0639ff2a5d].

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFDictionary *leftButtons, *rightButtons;
		OFMutableDictionary *buttons, *directionalPads;
		OHGameControllerDirectionalPad *directionalPad;

		if (leftJoyCon.vendorID.unsignedShortValue !=
		    OHVendorIDNintendo ||
		    rightJoyCon.vendorID.unsignedShortValue !=
		    OHVendorIDNintendo)
			@throw [OFInvalidArgumentException exception];








<







47
48
49
50
51
52
53

54
55
56
57
58
59
60
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFDictionary *leftButtons, *rightButtons;
		OFMutableDictionary *buttons, *directionalPads;


		if (leftJoyCon.vendorID.unsignedShortValue !=
		    OHVendorIDNintendo ||
		    rightJoyCon.vendorID.unsignedShortValue !=
		    OHVendorIDNintendo)
			@throw [OFInvalidArgumentException exception];

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
		leftButtons = _leftJoyCon.buttons;
		rightButtons = _rightJoyCon.buttons;

		buttons = [OFMutableDictionary dictionaryWithCapacity:
		    leftButtons.count + rightButtons.count];
		[buttons addEntriesFromDictionary: leftButtons];
		[buttons addEntriesFromDictionary: rightButtons];
		[buttons removeObjectForKey: @"D-Pad Up"];
		[buttons removeObjectForKey: @"D-Pad Down"];
		[buttons removeObjectForKey: @"D-Pad Left"];
		[buttons removeObjectForKey: @"D-Pad Right"];
		[buttons removeObjectForKey: @"SL"];
		[buttons removeObjectForKey: @"SR"];
		[buttons makeImmutable];
		_buttons = [buttons retain];

		directionalPads =
		    [OFMutableDictionary dictionaryWithCapacity: 3];

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

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

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

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

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







<
<
<
<







|
<
<
<
<
<
|
<
|
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<







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
		leftButtons = _leftJoyCon.buttons;
		rightButtons = _rightJoyCon.buttons;

		buttons = [OFMutableDictionary dictionaryWithCapacity:
		    leftButtons.count + rightButtons.count];
		[buttons addEntriesFromDictionary: leftButtons];
		[buttons addEntriesFromDictionary: rightButtons];




		[buttons removeObjectForKey: @"SL"];
		[buttons removeObjectForKey: @"SR"];
		[buttons makeImmutable];
		_buttons = [buttons retain];

		directionalPads =
		    [OFMutableDictionary dictionaryWithCapacity: 3];
		[directionalPads addEntriesFromDictionary:





		    _leftJoyCon.directionalPads];

		[directionalPads addEntriesFromDictionary:





		    _rightJoyCon.directionalPads];











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

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

Modified src/hid/OHEvdevGameController.m from [7e3c50d414] to [2b74578768].

35
36
37
38
39
40
41


42
43
44
45
46
47
48
#import "OHDualShock4Gamepad.h"
#import "OHEvdevExtendedGamepad.h"
#import "OHExtendedN64Controller.h"
#import "OHGameControllerAxis+Private.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerProfile.h"


#import "OHStadiaGamepad.h"

#include <sys/ioctl.h>
#include <linux/input.h>

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







>
>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#import "OHDualShock4Gamepad.h"
#import "OHEvdevExtendedGamepad.h"
#import "OHExtendedN64Controller.h"
#import "OHGameControllerAxis+Private.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerProfile.h"
#import "OHLeftJoyCon.h"
#import "OHRightJoyCon.h"
#import "OHStadiaGamepad.h"

#include <sys/ioctl.h>
#include <linux/input.h>

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
203
204
205
206
207
208
209






210
211
212
213
214
215
216
			_profile = [[OHDualSenseGamepad alloc] init];
		else if (_vendorID == OHVendorIDSony &&
		    _productID == OHProductIDDualShock4)
			_profile = [[OHDualShock4Gamepad alloc] init];
		else if (_vendorID == OHVendorIDNintendo &&
		    _productID == OHProductIDN64Controller)
			_profile = [[OHExtendedN64Controller alloc] init];






		else if (_vendorID == OHVendorIDGoogle &&
		    _productID == OHProductIDStadiaController)
			_profile = [[OHStadiaGamepad alloc] init];
		else
			_profile = [[OHEvdevExtendedGamepad alloc]
			    initWithKeyBits: _keyBits
				     evBits: _evBits







>
>
>
>
>
>







205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
			_profile = [[OHDualSenseGamepad alloc] init];
		else if (_vendorID == OHVendorIDSony &&
		    _productID == OHProductIDDualShock4)
			_profile = [[OHDualShock4Gamepad alloc] init];
		else if (_vendorID == OHVendorIDNintendo &&
		    _productID == OHProductIDN64Controller)
			_profile = [[OHExtendedN64Controller alloc] init];
		else if (_vendorID == OHVendorIDNintendo &&
		    _productID == OHProductIDLeftJoyCon)
			_profile = [[OHLeftJoyCon alloc] init];
		else if (_vendorID == OHVendorIDNintendo &&
		    _productID == OHProductIDRightJoyCon)
			_profile = [[OHRightJoyCon alloc] init];
		else if (_vendorID == OHVendorIDGoogle &&
		    _productID == OHProductIDStadiaController)
			_profile = [[OHStadiaGamepad alloc] init];
		else
			_profile = [[OHEvdevExtendedGamepad alloc]
			    initWithKeyBits: _keyBits
				     evBits: _evBits

Modified src/hid/OHEvdevGameControllerProfile.m from [566139f646] to [b041d2d6ee].

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
#import "OHGameControllerButton.h"

#include <linux/input.h>

static OFString *
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
{
	if (vendorID == OHVendorIDNintendo &&
	    productID == OHProductIDLeftJoyCon) {
		switch (button) {
		case BTN_TL:
			return @"L";
		case BTN_TL2:
			return @"ZL";
		case BTN_THUMBL:
			return @"Left Thumbstick";
		case BTN_SELECT:
			return @"-";
		case BTN_Z:
			return @"Capture";
		case BTN_TR:
			return @"SL";
		case BTN_TR2:
			return @"SR";
		}
	} else if (vendorID == OHVendorIDNintendo &&
	    productID == OHProductIDRightJoyCon) {
		switch (button) {
		case BTN_NORTH:
			return @"X";
		case BTN_SOUTH:
			return @"B";
		case BTN_WEST:
			return @"Y";
		case BTN_EAST:
			return @"A";
		case BTN_TR:
			return @"R";
		case BTN_TR2:
			return @"ZR";
		case BTN_THUMBR:
			return @"Right Thumbstick";
		case BTN_START:
			return @"+";
		case BTN_MODE:
			return @"Home";
		case BTN_TL:
			return @"SL";
		case BTN_TL2:
			return @"SR";
		}
	}

	switch (button) {
	case BTN_A:
		return @"A";
	case BTN_B:
		return @"B";
	case BTN_C:
		return @"C";







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







25
26
27
28
29
30
31














































32
33
34
35
36
37
38
#import "OHGameControllerButton.h"

#include <linux/input.h>

static OFString *
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
{














































	switch (button) {
	case BTN_A:
		return @"A";
	case BTN_B:
		return @"B";
	case BTN_C:
		return @"C";

Added src/hid/OHLeftJoyCon.h version [6ef79f8ba1].















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OHGameControllerProfile.h"
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# import "OHEvdevGameController.h"
#endif

OF_ASSUME_NONNULL_BEGIN

@interface OHLeftJoyCon: OFObject <OHGameControllerProfile
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
    , OHEvdevMapping
#endif
>
{
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *_directionalPads;
}
@end

OF_ASSUME_NONNULL_END

Added src/hid/OHLeftJoyCon.m version [5fba4772d9].





































































































































































































































































































































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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include <linux/input.h>
#endif

static OFString *const buttonNames[] = {
	@"L", @"ZL", @"Left Thumbstick", @"-", @"Capture", @"SL", @"SR"
};
static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames);

@implementation OHLeftJoyCon
@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]] autorelease];
			[buttons setObject: button forKey: buttonNames[i]];
		}
		[buttons makeImmutable];
		_buttons = [buttons retain];

		directionalPads =
		    [OFMutableDictionary dictionaryWithCapacity: 2];

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

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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
- (OHGameControllerButton *)oh_buttonForEvdevButton: (uint16_t)button
{
	switch (button) {
	case BTN_DPAD_UP:
		return [[_directionalPads objectForKey: @"D-Pad"] up];
	case BTN_DPAD_DOWN:
		return [[_directionalPads objectForKey: @"D-Pad"] down];
	case BTN_DPAD_LEFT:
		return [[_directionalPads objectForKey: @"D-Pad"] left];
	case BTN_DPAD_RIGHT:
		return [[_directionalPads objectForKey: @"D-Pad"] right];
	case BTN_TL:
		return [_buttons objectForKey: @"L"];
	case BTN_TL2:
		return [_buttons objectForKey: @"ZL"];
	case BTN_THUMBL:
		return [_buttons objectForKey: @"Left Thumbstick"];
	case BTN_SELECT:
		return [_buttons objectForKey: @"-"];
	case BTN_Z:
		return [_buttons objectForKey: @"Capture"];
	case BTN_TR:
		return [_buttons objectForKey: @"SL"];
	case BTN_TR2:
		return [_buttons objectForKey: @"SR"];
	}

	return nil;
}

- (OHGameControllerAxis *)oh_axisForEvdevAxis: (uint16_t)axis
{
	switch (axis) {
	case ABS_X:
		return [[_directionalPads
		    objectForKey: @"Left Thumbstick"] xAxis];
	case ABS_Y:
		return [[_directionalPads
		    objectForKey: @"Left Thumbstick"] yAxis];
	}

	return nil;
}
#endif
@end

Added src/hid/OHRightJoyCon.h version [640bb5aeb4].















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OHGameControllerProfile.h"
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# import "OHEvdevGameController.h"
#endif

OF_ASSUME_NONNULL_BEGIN

@interface OHRightJoyCon: OFObject <OHGameControllerProfile
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
    , OHEvdevMapping
#endif
>
{
	OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons;
	OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *)
	    *_directionalPads;
}
@end

OF_ASSUME_NONNULL_END

Added src/hid/OHRightJoyCon.m version [d697a2abb8].

























































































































































































































































































































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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include <linux/input.h>
#endif

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

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

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

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

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

		xAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"X"] autorelease];
		yAxis = [[[OHGameControllerAxis alloc]
		    initWithName: @"Y"] autorelease];
		directionalPad = [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"Right Thumbstick"
			   xAxis: xAxis
			   yAxis: yAxis] autorelease];

		_directionalPads = [[OFDictionary alloc]
		    initWithObject: directionalPad
			    forKey: @"Right Thumbstick"];

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

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
- (OHGameControllerButton *)oh_buttonForEvdevButton: (uint16_t)button
{
	OFString *name;

	switch (button) {
	case BTN_NORTH:
		name = @"X";
		break;
	case BTN_SOUTH:
		name = @"B";
		break;
	case BTN_EAST:
		name = @"A";
		break;
	case BTN_WEST:
		name = @"Y";
		break;
	case BTN_TR:
		name = @"R";
		break;
	case BTN_TR2:
		name = @"ZR";
		break;
	case BTN_THUMBR:
		name = @"Right Thumbstick";
		break;
	case BTN_START:
		name = @"+";
		break;
	case BTN_MODE:
		name = @"Home";
		break;
	case BTN_TL:
		name = @"SL";
		break;
	case BTN_TL2:
		name = @"SR";
		break;
	default:
		return nil;
	}

	return [_buttons objectForKey: name];
}

- (OHGameControllerAxis *)oh_axisForEvdevAxis: (uint16_t)axis
{
	switch (axis) {
	case ABS_RX:
		return [[_directionalPads
		    objectForKey: @"Right Thumbstick"] xAxis];
	case ABS_RY:
		return [[_directionalPads
		    objectForKey: @"Right Thumbstick"] yAxis];
	}

	return nil;
}
#endif
@end