ObjFW  Diff

Differences From Artifact [f71df4684b]:

  • File src/hid/OHEvdevExtendedGamepad.m — part of check-in [998339ff24] at 2024-06-09 11:43:49 on branch trunk — OHGamepad: Move some elements to OHExtendedGamepad This allows to have a profile for limited gamepads such as found on the Nintendo DS rather than only having the raw profile for those. (user: js size: 6618)

To Artifact [bb1ee3755e]:

  • File src/hid/OHEvdevExtendedGamepad.m — part of check-in [ddaa4f35d0] at 2024-06-17 00:37:04 on branch trunk — ObjFWHID: Make profiles implementation independent (user: js size: 6459)

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







-
+
+
+
+
+

-
+
+
+
+
+




-
-








-
+
+










-
-
-
-
-
-
-


-
+
-






-
+


-
+









-
+
-







#import "OHEvdevGameController.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHGameControllerEmulatedTriggerButton.h"

#import "OFInvalidArgumentException.h"

@implementation OHEvdevExtendedGamepad
- (instancetype)initWithController: (OHEvdevGameController *)controller
- (instancetype)initWithKeyBits: (unsigned long *)keyBits
			 evBits: (unsigned long *)evBits
			absBits: (unsigned long *)absBits
		       vendorID: (uint16_t)vendorID
		      productID: (uint16_t)productID
{
	self = [super init];
	self = [super initWithKeyBits: keyBits
			       evBits: evBits
			      absBits: absBits
			     vendorID: vendorID
			    productID: productID];

	@try {
		void *pool = objc_autoreleasePoolPush();

		_rawProfile = [controller.rawProfile retain];

		if (self.northButton == nil || self.southButton == nil ||
		    self.westButton == nil || self.eastButton == nil ||
		    self.leftShoulderButton == nil ||
		    self.rightShoulderButton == nil ||
		    self.leftTriggerButton == nil ||
		    self.rightTriggerButton == nil || self.menuButton == nil ||
		    self.optionsButton == nil || self.leftThumbstick == nil ||
		    self.rightThumbstick == nil || self.dPad == nil)
			@throw [OFInvalidArgumentException exception];
			object_setClass(self,
			    [OHEvdevGameControllerProfile class]);

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

	return self;
}

- (void)dealloc
{
	[_rawProfile release];

	[super dealloc];
}

- (OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *)buttons
{
	OFMutableDictionary *buttons =
	OFMutableDictionary *buttons = [[_buttons mutableCopy] autorelease];
	    [[_rawProfile.buttons mutableCopy] autorelease];

	[buttons removeObjectForKey: @"D-Pad Up"];
	[buttons removeObjectForKey: @"D-Pad Down"];
	[buttons removeObjectForKey: @"D-Pad Left"];
	[buttons removeObjectForKey: @"D-Pad Right"];

	if ([_rawProfile.axes objectForKey: @"Z"] != nil)
	if ([_axes objectForKey: @"Z"] != nil)
		[buttons setObject: self.leftTriggerButton forKey: @"LT"];

	if ([_rawProfile.axes objectForKey: @"RZ"] != nil)
	if ([_axes objectForKey: @"RZ"] != nil)
		[buttons setObject: self.rightTriggerButton forKey: @"RT"];

	[buttons makeImmutable];

	return buttons;
}

- (OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *)axes
{
	OFMutableDictionary *axes =
	OFMutableDictionary *axes = [[_axes mutableCopy] autorelease];
	    [[_rawProfile.axes mutableCopy] autorelease];

	[axes removeObjectForKey: @"X"];
	[axes removeObjectForKey: @"Y"];
	[axes removeObjectForKey: @"RX"];
	[axes removeObjectForKey: @"RY"];
	[axes removeObjectForKey: @"Z"];
	[axes removeObjectForKey: @"RZ"];
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
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







-
+




-
+




-
+




-
+




-
+




-
+




-
+






-
+




-
+






-
+




-
+




-
+




-
+




-
+




-
+




-
-
+
+












-
-
+
+












-
-
+
+








-
-
-
-
+
+
+
+












	    @"Left Thumbstick", self.leftThumbstick,
	    @"Right Thumbstick", self.rightThumbstick,
	    @"D-Pad", self.dPad, nil];
}

- (OHGameControllerButton *)northButton
{
	return [_rawProfile.buttons objectForKey: @"Y"];
	return [_buttons objectForKey: @"Y"];
}

- (OHGameControllerButton *)southButton
{
	return [_rawProfile.buttons objectForKey: @"A"];
	return [_buttons objectForKey: @"A"];
}

- (OHGameControllerButton *)westButton
{
	return [_rawProfile.buttons objectForKey: @"X"];
	return [_buttons objectForKey: @"X"];
}

- (OHGameControllerButton *)eastButton
{
	return [_rawProfile.buttons objectForKey: @"B"];
	return [_buttons objectForKey: @"B"];
}

- (OHGameControllerButton *)leftShoulderButton
{
	return [_rawProfile.buttons objectForKey: @"LB"];
	return [_buttons objectForKey: @"LB"];
}

- (OHGameControllerButton *)rightShoulderButton
{
	return [_rawProfile.buttons objectForKey: @"RB"];
	return [_buttons objectForKey: @"RB"];
}

- (OHGameControllerButton *)leftTriggerButton
{
	OHGameControllerAxis *axis = [_rawProfile.axes objectForKey: @"Z"];
	OHGameControllerAxis *axis = [_axes objectForKey: @"Z"];

	if (axis != nil)
		return [[[OHGameControllerEmulatedTriggerButton alloc]
		    initWithName: @"LT"
			    axis: axis] autorelease];

	return [_rawProfile.buttons objectForKey: @"LT"];
	return [_buttons objectForKey: @"LT"];
}

- (OHGameControllerButton *)rightTriggerButton
{
	OHGameControllerAxis *axis = [_rawProfile.axes objectForKey: @"RZ"];
	OHGameControllerAxis *axis = [_axes objectForKey: @"RZ"];

	if (axis != nil)
		return [[[OHGameControllerEmulatedTriggerButton alloc]
		    initWithName: @"RT"
			    axis: axis] autorelease];

	return [_rawProfile.buttons objectForKey: @"RT"];
	return [_buttons objectForKey: @"RT"];
}

- (OHGameControllerButton *)leftThumbstickButton
{
	return [_rawProfile.buttons objectForKey: @"LSB"];
	return [_buttons objectForKey: @"LSB"];
}

- (OHGameControllerButton *)rightThumbstickButton
{
	return [_rawProfile.buttons objectForKey: @"RSB"];
	return [_buttons objectForKey: @"RSB"];
}

- (OHGameControllerButton *)menuButton
{
	return [_rawProfile.buttons objectForKey: @"Start"];
	return [_buttons objectForKey: @"Start"];
}

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

- (OHGameControllerButton *)homeButton
{
	return [_rawProfile.buttons objectForKey: @"Guide"];
	return [_buttons objectForKey: @"Guide"];
}

- (OHGameControllerDirectionalPad *)leftThumbstick
{
	OHGameControllerAxis *xAxis = [_rawProfile.axes objectForKey: @"X"];
	OHGameControllerAxis *yAxis = [_rawProfile.axes objectForKey: @"Y"];
	OHGameControllerAxis *xAxis = [_axes objectForKey: @"X"];
	OHGameControllerAxis *yAxis = [_axes objectForKey: @"Y"];

	if (xAxis == nil || yAxis == nil)
		return nil;

	return [[[OHGameControllerDirectionalPad alloc]
	    initWithName: @"Left Thumbstick"
		   xAxis: xAxis
		   yAxis: yAxis] autorelease];
}

- (OHGameControllerDirectionalPad *)rightThumbstick
{
	OHGameControllerAxis *xAxis = [_rawProfile.axes objectForKey: @"RX"];
	OHGameControllerAxis *yAxis = [_rawProfile.axes objectForKey: @"RY"];
	OHGameControllerAxis *xAxis = [_axes objectForKey: @"RX"];
	OHGameControllerAxis *yAxis = [_axes objectForKey: @"RY"];

	if (xAxis == nil || yAxis == nil)
		return nil;

	return [[[OHGameControllerDirectionalPad alloc]
	    initWithName: @"Right Thumbstick"
		   xAxis: xAxis
		   yAxis: yAxis] autorelease];
}

- (OHGameControllerDirectionalPad *)dPad
{
	OHGameControllerAxis *xAxis = [_rawProfile.axes objectForKey: @"HAT0X"];
	OHGameControllerAxis *yAxis = [_rawProfile.axes objectForKey: @"HAT0Y"];
	OHGameControllerAxis *xAxis = [_axes objectForKey: @"HAT0X"];
	OHGameControllerAxis *yAxis = [_axes objectForKey: @"HAT0Y"];
	OHGameControllerButton *up, *down, *left, *right;

	if (xAxis != nil && yAxis != nil)
		return [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"D-Pad"
			   xAxis: xAxis
			   yAxis: yAxis] autorelease];

	up = [_rawProfile.buttons objectForKey: @"D-Pad Up"];
	down = [_rawProfile.buttons objectForKey: @"D-Pad Down"];
	left = [_rawProfile.buttons objectForKey: @"D-Pad Left"];
	right = [_rawProfile.buttons objectForKey: @"D-Pad Right"];
	up = [_buttons objectForKey: @"D-Pad Up"];
	down = [_buttons objectForKey: @"D-Pad Down"];
	left = [_buttons objectForKey: @"D-Pad Left"];
	right = [_buttons objectForKey: @"D-Pad Right"];

	if (up != nil && down != nil && left != nil && right != nil)
		return [[[OHGameControllerDirectionalPad alloc]
		    initWithName: @"D-Pad"
			      up: up
			    down: down
			    left: left
			   right: right] autorelease];

	return nil;
}
@end