ObjFW  Diff

Differences From Artifact [0c35b7944c]:

To Artifact [79671ffcff]:


24
25
26
27
28
29
30

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







+







#import "OFColor.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"

#import "OHCombinedJoyCons.h"
#import "OHGameController.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerDirectionalPad.h"
#import "OHGameControllerProfile.h"
#import "OHGamepad.h"

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







-
+





+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+










+






-
+
-











-
-
-
-
-
-
-












-
-
-
-
-
+
-
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
+
-
-
+
-
-
-
-
-
+
-
-

-
-
-
+
-
-
-
-
-

-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+
-
-

-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+












# define red maroon
# define yellow olive
# define gray silver
#endif

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

OF_APPLICATION_DELEGATE(GameControllerTests)

static void printProfile(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 (button.value == 1)
			[OFStdOut setForegroundColor: [OFColor red]];
		else if (button.value > 0.5)
			[OFStdOut setForegroundColor: [OFColor yellow]];
		else if (button.value > 0)
			[OFStdOut setForegroundColor: [OFColor green]];
		else
			[OFStdOut setForegroundColor: [OFColor gray]];

		[OFStdOut writeFormat: @"[%@] ", name];
	}
	[OFStdOut setForegroundColor: [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;
		}

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

	if ([profile isKindOfClass: [OHGamepad class]]) {
		OHGamepad *gamepad = (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];
		[OFStdOut writeFormat:
		    @"[Map] Left Trigger: %@  Right Trigger: %@\n",
		    gamepad.leftTriggerButton.name,
		    gamepad.rightTriggerButton.name];
		[OFStdOut writeFormat:
		    @"[Map] Left Thumbstick: %@  Right Thumbstick: %@\n",
		    gamepad.leftThumbstickButton.name,
		    gamepad.rightThumbstickButton.name];
		[OFStdOut writeFormat:
		    @"[Map] Menu: %@  Options: %@  Home: %@\n",
		    gamepad.menuButton.name, gamepad.optionsButton.name,
		    gamepad.homeButton.name];
	}
}

@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();
		OHGameController *leftJoyCon = nil, *rightJoyCon = nil;

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

			_controllers = [[OHGameController controllers]
			_controllers = [[OHGameController controllers] retain];
			    mutableCopy];
			_lastControllersUpdate = [[OFDate alloc] init];

			[OFStdOut clear];
		}

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

		for (OHGameController *controller in _controllers) {
			OHGameControllerProfile *profile =
			    (controller.gamepad != nil
			    ? controller.gamepad : controller.rawProfile);
			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;

			[OFStdOut setForegroundColor: [OFColor green]];
			[OFStdOut writeLine: controller.description];

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

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

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


			if (controller.vendorID.unsignedShortValue ==
				if (button.value == 1)
					[OFStdOut setForegroundColor:
					    [OFColor red]];
			    OHVendorIDNintendo) {
				else if (button.value > 0.5)
					[OFStdOut setForegroundColor:
					    [OFColor yellow]];
				else if (button.value > 0)
					[OFStdOut setForegroundColor:
					    [OFColor green]];
				else
					[OFStdOut setForegroundColor:
					    [OFColor gray]];

				if (controller.productID.unsignedShortValue ==
				[OFStdOut writeFormat: @"[%@] ", name];
			}
			[OFStdOut setForegroundColor: [OFColor gray]];
			[OFStdOut writeString: @"\n"];

			i = 0;
				    OHProductIDLeftJoyCon)
					leftJoyCon = controller;
			for (OFString *name in axes) {
				OHGameControllerAxis *axis =
				if (controller.productID.unsignedShortValue ==
				    [profile.axes objectForKey: name];

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

					rightJoyCon = controller;
				[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) {
		if (leftJoyCon != nil && rightJoyCon != nil) {
					[OFStdOut writeString: @"\n"];
					i = 0;
			OHCombinedJoyCons *combinedJoyCons = [OHCombinedJoyCons
				}

			    gamepadWithLeftJoyCon: leftJoyCon
				[OFStdOut writeFormat:
				    @"%@: (%5.2f, %5.2f)  ",
				    name, directionalPad.xAxis.value,
				    directionalPad.yAxis.value];
			}
			if (directionalPads.count > 0)
				[OFStdOut writeString: @"\n"];

				      rightJoyCon: rightJoyCon];
			if ([profile isKindOfClass: [OHGamepad class]]) {
				OHGamepad *gamepad = (OHGamepad *)profile;

				[OFStdOut writeFormat:
			[OFStdOut setForegroundColor: [OFColor green]];
				    @"[Map] North: %@  South: %@"
				    @"  West: %@  East: %@\n",
				    gamepad.northButton.name,
				    gamepad.southButton.name,
				    gamepad.westButton.name,
				    gamepad.eastButton.name];
				[OFStdOut writeFormat:
			[OFStdOut writeLine: @"Combined Joy-Cons"];
				    @"[Map] Left Shoulder: %@"
				    @"  Right Shoulder: %@\n",
				    gamepad.leftShoulderButton.name,
				    gamepad.rightShoulderButton.name];
				[OFStdOut writeFormat:
				    @"[Map] Left Trigger: %@"
				    @"  Right Trigger: %@\n",
				    gamepad.leftTriggerButton.name,
				    gamepad.rightTriggerButton.name];
				[OFStdOut writeFormat:
				    @"[Map] Left Thumbstick: %@"
				    @"  Right Thumbstick: %@\n",
				    gamepad.leftThumbstickButton.name,
				    gamepad.rightThumbstickButton.name];
				[OFStdOut writeFormat:
				    @"[Map] Menu: %@  Options: %@  Home: %@\n",
				    gamepad.menuButton.name,
				    gamepad.optionsButton.name,
				    gamepad.homeButton.name];
			}

			printProfile(combinedJoyCons);
		}

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFThread waitForVerticalBlank];
#else
		[OFThread sleepForTimeInterval: 1.f / 60.f];
#endif

		objc_autoreleasePoolPop(pool);
	}
}
@end