File: OFGCTester.m

package info (click to toggle)
objfw 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,644 kB
  • sloc: objc: 101,491; asm: 5,083; sh: 4,092; makefile: 1,574; ansic: 709; xml: 367; pascal: 214
file content (343 lines) | stat: -rw-r--r-- 9,165 bytes parent folder | download
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
/*
 * Copyright (c) 2008-2025 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 "OFApplication.h"
#import "OFArray.h"
#import "OFColor.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"
#import "OFTimer.h"

#import "OHExtendedGamepad.h"
#import "OHGameController.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHGameControllerProfile.h"
#import "OHGamepad.h"
#import "OHJoyConPair.h"
#import "OHLeftJoyCon.h"
#import "OHRightJoyCon.h"

#import "OFReadFailedException.h"

#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
#else
static size_t buttonsPerLine = 5;
#endif

static sig_atomic_t SIGINTReceived = false;

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS) || \
    defined(OF_NINTENDO_SWITCH)
# define red maroon
# define yellow olive
# define gray silver
#endif

#ifdef OF_NINTENDO_SWITCH
# define id nx_id
# include <switch.h>
# undef id
#endif

#ifdef OF_AMIGAOS
const char *VER = "$VER: ofgctester "
    OF_PREPROCESSOR_STRINGIFY(OBJFW_VERSION_MAJOR)
    "." OF_PREPROCESSOR_STRINGIFY(OBJFW_VERSION_MINOR) " (" BUILD_DATE ") "
    "\xA9 2008-2025 Jonathan Schleifer";
#endif

@interface OFGCTester: OFObject <OFApplicationDelegate>
{
	OFArray OF_GENERIC(OHGameController *) *_controllers;
	OFDate *_lastControllersUpdate;
	OHJoyConPair *_joyConPair;
}
@end

OF_APPLICATION_DELEGATE(OFGCTester)

static void
printProfile(id <OHGameControllerProfile> profile)
{
	OFArray OF_GENERIC(OFString *) *buttons =
	    profile.buttons.allKeys.sortedArray;
	OFArray OF_GENERIC(OFString *) *axes = profile.axes.allKeys.sortedArray;
	OFArray OF_GENERIC(OFString *) *directionalPads =
	    profile.directionalPads.allKeys.sortedArray;
	size_t i;

	i = 0;
	for (OFString *name in buttons) {
		OHGameControllerButton *button =
		    [profile.buttons objectForKey: name];

		if (i++ == buttonsPerLine) {
			[OFStdOut writeString: @"\n"];
			i = 0;
		}

		if (OFStdOut.colors >= 256) {
			float red, green, blue;

			if (button.value < 0.25f) {
				red = 0.5f - 2.0f * button.value;
				green = 0.5f + 2.0f * button.value;
				blue = 0.5f - 2.0f * button.value;
			} else if (button.value < 0.5f) {
				red = 2.0f * button.value;
				green = 1.0f;
				blue = 0.0f;
			} else {
				red = 1.0f;
				green = 1.0f - 2.0f * (button.value - 0.5f);
				blue = 0.0f;
			}

			OFStdOut.foregroundColor = [OFColor colorWithRed: red
								   green: green
								    blue: blue
								   alpha: 1.0f];
		} else {
			if (button.value == 1.0f)
				OFStdOut.foregroundColor = [OFColor red];
			else if (button.value > 0.5f)
				OFStdOut.foregroundColor = [OFColor yellow];
			else if (button.value > 0.0f)
				OFStdOut.foregroundColor = [OFColor green];
			else
				OFStdOut.foregroundColor = [OFColor gray];
		}

		[OFStdOut writeFormat: @"[%@] ", name];
	}
	if (OFStdOut.colors >= 256)
		OFStdOut.foregroundColor = [OFColor colorWithRed: 0.5f
							   green: 0.5f
							    blue: 0.5f
							   alpha: 1.0f];
	else
		OFStdOut.foregroundColor = [OFColor gray];
	[OFStdOut writeString: @"\n"];

	i = 0;
	for (OFString *name in axes) {
		OHGameControllerAxis *axis = [profile.axes objectForKey: name];

		if (i++ == buttonsPerLine) {
			[OFStdOut writeString: @"\n"];
			i = 0;
		}

		[OFStdOut writeFormat: @"%@: %5.2f  ", name, axis.value];
	}
	if (axes.count > 0)
		[OFStdOut writeString: @"\n"];

	i = 0;
	for (OFString *name in directionalPads) {
		OHGameControllerDirectionalPad *directionalPad =
		    [profile.directionalPads objectForKey: name];

		if (i++ == 2) {
			[OFStdOut writeString: @"\n"];
			i = 0;
		}

		if (OFStdOut.colors >= 256) {
			float red = 0.5f + directionalPad.xAxis.value / 2.0f;
			float blue = 0.5f + directionalPad.yAxis.value / 2.0f;
			float green = 1.0f - (red / 2.0f + blue / 2.0f);

			OFStdOut.foregroundColor = [OFColor colorWithRed: red
								   green: green
								    blue: blue
								   alpha: 1.0f];
		}

		[OFStdOut writeFormat:
		    @"%@: (%5.2f, %5.2f)  ",
		    name,
		    directionalPad.xAxis.value, directionalPad.yAxis.value];
	}
	if (OFStdOut.colors >= 256)
		OFStdOut.foregroundColor = [OFColor colorWithRed: 0.5f
							   green: 0.5f
							    blue: 0.5f
							   alpha: 1.0f];
	if (directionalPads.count > 0)
		[OFStdOut writeString: @"\n"];

	if ([profile conformsToProtocol: @protocol(OHGamepad)]) {
		id <OHGamepad> gamepad = (id <OHGamepad>)profile;

		[OFStdOut writeFormat:
		    @"[Map] North: %@  South: %@  West: %@  East: %@\n",
		    gamepad.northButton.name, gamepad.southButton.name,
		    gamepad.westButton.name, gamepad.eastButton.name];
		[OFStdOut writeFormat:
		    @"[Map] Left Shoulder: %@  Right Shoulder: %@\n",
		    gamepad.leftShoulderButton.name,
		    gamepad.rightShoulderButton.name];
	}

	if ([profile conformsToProtocol: @protocol(OHExtendedGamepad)]) {
		id <OHExtendedGamepad> extendedGamepad =
		    (id <OHExtendedGamepad>)profile;

		[OFStdOut writeFormat:
		    @"[Map] Left Trigger: %@  Right Trigger: %@\n",
		    extendedGamepad.leftTriggerButton.name,
		    extendedGamepad.rightTriggerButton.name];
		[OFStdOut writeFormat:
		    @"[Map] Left Thumbstick: %@  Right Thumbstick: %@\n",
		    extendedGamepad.leftThumbstickButton.name,
		    extendedGamepad.rightThumbstickButton.name];
	}

	if ([profile conformsToProtocol: @protocol(OHGamepad)]) {
		id <OHGamepad> gamepad = (id <OHGamepad>)profile;

		[OFStdOut writeFormat:
		    @"[Map] Menu: %@  Options: %@",
		    gamepad.menuButton.name, gamepad.optionsButton.name];
	}

	if ([profile conformsToProtocol: @protocol(OHExtendedGamepad)]) {
		id <OHExtendedGamepad> extendedGamepad =
		    (id <OHExtendedGamepad>)profile;

		[OFStdOut writeFormat: @"  Home: %@",
		    extendedGamepad.homeButton.name];
	}

	if ([profile conformsToProtocol: @protocol(OHGamepad)])
		[OFStdOut writeString: @"\n"];
}

@implementation OFGCTester
- (void)updateOutput
{
	OHLeftJoyCon *leftJoyCon = nil;
	OHRightJoyCon *rightJoyCon = nil;

	if (SIGINTReceived) {
		OFStdOut.cursorVisible = true;
		[OFStdOut reset];
		[OFApplication terminate];
	}

	if (_lastControllersUpdate == nil ||
	    -[_lastControllersUpdate timeIntervalSinceNow] > 1) {
		objc_release(_joyConPair);
		objc_release(_controllers);
		objc_release(_lastControllersUpdate);

		_joyConPair = nil;
		_controllers = objc_retain([OHGameController controllers]);
		_lastControllersUpdate = [[OFDate alloc] init];

		[OFStdOut clear];
	}

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

	for (OHGameController *controller in _controllers) {
		id <OHGameControllerProfile> profile = controller.profile;

		OFStdOut.foregroundColor = [OFColor green];
		[OFStdOut writeLine: controller.description];
		OFStdOut.foregroundColor = [OFColor teal];
		[OFStdOut writeFormat: @"Profile: %@\n", profile];

		@try {
			[controller updateState];
		} @catch (OFReadFailedException *e) {
			OFStdOut.foregroundColor = [OFColor red];
			[OFStdOut writeString: e.description];
			continue;
		}

		printProfile(profile);

		if ([profile isKindOfClass: [OHLeftJoyCon class]])
			leftJoyCon = (OHLeftJoyCon *)profile;
		else if ([profile isKindOfClass: [OHRightJoyCon class]])
			rightJoyCon = (OHRightJoyCon *)profile;

		if (_joyConPair == nil && leftJoyCon != nil &&
		    rightJoyCon != nil)
			_joyConPair = objc_retain(
			    [OHJoyConPair gamepadWithLeftJoyCon: leftJoyCon
						    rightJoyCon: rightJoyCon]);
	}

	if (_joyConPair) {
		OFStdOut.foregroundColor = [OFColor green];
		[OFStdOut writeLine: @"Joy-Con Pair"];

		printProfile(_joyConPair);
	}

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFThread waitForVerticalBlank];
#elif defined(OF_NINTENDO_SWITCH)
	consoleUpdate(NULL);
#endif
}

- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
	OFStdOut.cursorVisible = false;

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
	[OFStdIOStream setUpConsole];
#endif
#if defined(OF_NINTENDO_SWITCH)
	consoleInit(NULL);

	while (appletMainLoop()) {
		void *pool = objc_autoreleasePoolPush();

		[self updateOutput];

		objc_autoreleasePoolPop(pool);
	}
#else
	[OFTimer scheduledTimerWithTimeInterval: 1.f / 60.f
					 target: self
				       selector: @selector(updateOutput)
					repeats: true];
#endif
}

- (void)applicationDidReceiveSIGINT
{
	SIGINTReceived = true;
}
@end