ObjFW  Diff

Differences From Artifact [1bbc654209]:

To Artifact [b2c5ea408f]:


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







-

-
-
-
+
+



+
+
+
+


















-
+


















-
-



-
+



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-





-
-
-
+
+
+
+
+













+
-
+
-
+




-
-
-
-
+


-
+


-
+






-
+









+
+
-
-
+
+
-
-
-
+
+

-
-
-
-
-
-
+













 */

#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 "OFDictionary.h"
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"

#import "HIDGameController.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.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

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

@interface GameControllerTests: OFObject <OFApplicationDelegate>
{
	OFMutableArray OF_GENERIC(OFGameController *) *_controllers;
	OFMutableArray OF_GENERIC(HIDGameController *) *_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]
			_controllers = [[HIDGameController 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) {
			OFArray OF_GENERIC(OFGameControllerButton) *buttons =
			    controller.buttons.allObjects.sortedArray;
		for (HIDGameController *controller in _controllers) {
			OFArray OF_GENERIC(OFString *) *buttons =
			    controller.buttons.allKeys.sortedArray;
			OFArray OF_GENERIC(OFString *) *axes =
			    controller.axes.allKeys.sortedArray;
			size_t i = 0;

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

			@try {
				[controller retrieveState];
			} @catch (OFReadFailedException *e) {
				[OFStdOut setForegroundColor: [OFColor red]];
				[OFStdOut writeFormat: @"\n%@", e.description];
				continue;
			}

			for (OFString *name in buttons) {
			for (OFGameControllerButton button in buttons) {
				HIDGameControllerButton *button =
				float pressure;
				    [controller.buttons objectForKey: name];

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

				pressure =
				    [controller pressureForButton: button];

				if (pressure == 1)
				if (button.value == 1)
					[OFStdOut setForegroundColor:
					    [OFColor red]];
				else if (pressure > 0.5)
				else if (button.value > 0.5)
					[OFStdOut setForegroundColor:
					    [OFColor yellow]];
				else if (pressure > 0)
				else if (button.value > 0)
					[OFStdOut setForegroundColor:
					    [OFColor green]];
				else
					[OFStdOut setForegroundColor:
					    [OFColor gray]];

				[OFStdOut writeFormat: @"[%@]", button];
				[OFStdOut writeFormat: @"[%@]", button.name];

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

			for (OFString *name in axes) {
				HIDGameControllerAxis *axis =
			if (controller.hasLeftAnalogStick) {
				OFPoint position =
				    [controller.axes objectForKey: name];

				    controller.leftAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f) ",
						       position.x, position.y];
				[OFStdOut writeFormat: @"%@: %5.2f ",
						       name, axis.value];
			}
			if (controller.hasRightAnalogStick) {
				OFPoint position =
				    controller.rightAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f)",
						       position.x, position.y];
			}

			[OFStdOut writeString: @"\n"];
		}

#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