ObjFW  Check-in [87fa51ae2e]

Overview
Comment:OFGameController: Correctly scale axes on Linux
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gamecontroller
Files: files | file ages | folders
SHA3-256: 87fa51ae2e0c6abf92c913f314a93c2525e4c2fe1bd46a62051e5203f7c47cd2
User & Date: js on 2024-05-09 17:39:07
Other Links: branch diff | manifest | tags
Context
2024-05-09
18:01
OFGameController: Add quirks for N64 controller check-in: 21c872dbb0 user: js tags: gamecontroller
17:39
OFGameController: Correctly scale axes on Linux check-in: 87fa51ae2e user: js tags: gamecontroller
15:39
OFGameController: Change API for analog sticks check-in: 5547016712 user: js tags: gamecontroller
Changes

Modified src/OFGameController.h from [618d42f152] to [2448bc6ebe].

37
38
39
40
41
42
43




44
45
46
47
48
49
50
#ifdef OF_LINUX
	OFString *_path;
	int _fd;
	OFString *_name;
	OFMutableSet *_buttons, *_pressedButtons;
	bool _hasLeftAnalogStick, _hasRightAnalogStick;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;




#endif
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <OFGameController *> *controllers;
#endif







>
>
>
>







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifdef OF_LINUX
	OFString *_path;
	int _fd;
	OFString *_name;
	OFMutableSet *_buttons, *_pressedButtons;
	bool _hasLeftAnalogStick, _hasRightAnalogStick;
	OFPoint _leftAnalogStickPosition, _rightAnalogStickPosition;
	int32_t _leftAnalogStickMinX, _leftAnalogStickMaxX;
	int32_t _leftAnalogStickMinY, _leftAnalogStickMaxY;
	int32_t _rightAnalogStickMinX, _rightAnalogStickMaxX;
	int32_t _rightAnalogStickMinY, _rightAnalogStickMaxY;
#endif
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <OFGameController *> *controllers;
#endif

Modified src/platform/Linux/OFGameController.m from [9d006d5b77] to [5c8b1cd04f].

91
92
93
94
95
96
97











98
99
100
101
102
103
104
		return @"D-Pad Left";
	case BTN_DPAD_RIGHT:
		return @"D-Pad Right";
	}

	return nil;
}












@implementation OFGameController
@synthesize name = _name, buttons = _buttons;
@synthesize hasLeftAnalogStick = _hasLeftAnalogStick;
@synthesize hasRightAnalogStick = _hasRightAnalogStick;

+ (OFArray OF_GENERIC(OFGameController *) *)controllers







>
>
>
>
>
>
>
>
>
>
>







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
		return @"D-Pad Left";
	case BTN_DPAD_RIGHT:
		return @"D-Pad Right";
	}

	return nil;
}

static float
scale(float value, float min, float max)
{
	if (value < min)
		value = min;
	if (value > max)
		value = max;

	return ((value - min) / (max - min) * 2) - 1;
}

@implementation OFGameController
@synthesize name = _name, buttons = _buttons;
@synthesize hasLeftAnalogStick = _hasLeftAnalogStick;
@synthesize hasRightAnalogStick = _hasRightAnalogStick;

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
196
197
198
199
200
201
202
203


204
205














206
207


208














209
210
211
212
213
214
215
		if (OFBitSetIsSet(evBits, EV_ABS)) {
			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),
			    absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (OFBitSetIsSet(absBits, ABS_X) &&
			    OFBitSetIsSet(absBits, ABS_Y))


				_hasLeftAnalogStick = true;















			if (OFBitSetIsSet(absBits, ABS_RX) &&
			    OFBitSetIsSet(absBits, ABS_RY))


				_hasRightAnalogStick = true;















			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				[_buttons addObject: @"D-Pad Left"];
				[_buttons addObject: @"D-Pad Right"];
				[_buttons addObject: @"D-Pad Up"];
				[_buttons addObject: @"D-Pad Down"];







|
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
		if (OFBitSetIsSet(evBits, EV_ABS)) {
			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),
			    absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (OFBitSetIsSet(absBits, ABS_X) &&
			    OFBitSetIsSet(absBits, ABS_Y)) {
				struct input_absinfo infoX, infoY;

				_hasLeftAnalogStick = true;

				if (ioctl(_fd, EVIOCGABS(ABS_X), &infoX) == -1)
					@throw [OFInitializationFailedException
					    exception];

				if (ioctl(_fd, EVIOCGABS(ABS_Y), &infoY) == -1)
					@throw [OFInitializationFailedException
					    exception];

				_leftAnalogStickMinX = infoX.minimum;
				_leftAnalogStickMaxX = infoX.maximum;
				_leftAnalogStickMinY = infoY.minimum;
				_leftAnalogStickMaxY = infoY.maximum;
			}

			if (OFBitSetIsSet(absBits, ABS_RX) &&
			    OFBitSetIsSet(absBits, ABS_RY)) {
				struct input_absinfo infoX, infoY;

				_hasRightAnalogStick = true;

				if (ioctl(_fd, EVIOCGABS(ABS_RX), &infoX) == -1)
					@throw [OFInitializationFailedException
					    exception];

				if (ioctl(_fd, EVIOCGABS(ABS_RY), &infoY) == -1)
					@throw [OFInitializationFailedException
					    exception];

				_rightAnalogStickMinX = infoX.minimum;
				_rightAnalogStickMaxX = infoX.maximum;
				_rightAnalogStickMinY = infoY.minimum;
				_rightAnalogStickMaxY = infoY.maximum;
			}

			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				[_buttons addObject: @"D-Pad Left"];
				[_buttons addObject: @"D-Pad Right"];
				[_buttons addObject: @"D-Pad Up"];
				[_buttons addObject: @"D-Pad Down"];
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
			else
				[_pressedButtons removeObject:
				    buttonToName(event.code)];
			break;
		case EV_ABS:
			switch (event.code) {
			case ABS_X:
				_leftAnalogStickPosition.x =
				    (float)event.value /
				    (event.value < 0 ? -INT16_MIN : INT16_MAX);
				break;
			case ABS_Y:
				_leftAnalogStickPosition.y =
				    (float)event.value /
				    (event.value < 0 ? -INT16_MIN : INT16_MAX);

				break;
			case ABS_RX:
				_rightAnalogStickPosition.x =
				    (float)event.value /
				    (event.value < 0 ? -INT16_MIN : INT16_MAX);

				break;
			case ABS_RY:
				_rightAnalogStickPosition.y =
				    (float)event.value /
				    (event.value < 0 ? -INT16_MIN : INT16_MAX);

				break;
			case ABS_HAT0X:
				if (event.value < 0) {
					[_pressedButtons addObject:
					    @"D-Pad Left"];
					[_pressedButtons removeObject:
					    @"D-Pad Right"];







|
|
<


|
<
<
>


|
|
<
>


|
|
<
>







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
			else
				[_pressedButtons removeObject:
				    buttonToName(event.code)];
			break;
		case EV_ABS:
			switch (event.code) {
			case ABS_X:
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);

				break;
			case ABS_Y:
				_leftAnalogStickPosition.y = scale(event.value,


				    _leftAnalogStickMinY, _leftAnalogStickMaxY);
				break;
			case ABS_RX:
				_rightAnalogStickPosition.x = scale(event.value,
				    _rightAnalogStickMinX,

				    _rightAnalogStickMaxX);
				break;
			case ABS_RY:
				_rightAnalogStickPosition.y = scale(event.value,
				    _rightAnalogStickMinY,

				    _rightAnalogStickMaxY);
				break;
			case ABS_HAT0X:
				if (event.value < 0) {
					[_pressedButtons addObject:
					    @"D-Pad Left"];
					[_pressedButtons removeObject:
					    @"D-Pad Right"];

Modified tests/gamecontroller/GameControllerTests.m from [0632be0ddf] to [6a78591319].

79
80
81
82
83
84
85
86
87
88
89
				    controller.rightAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f)",
						       position.x, position.y];
			}
			[OFStdOut writeString: @"\n"];
		}

		[OFThread sleepForTimeInterval: 0.1];
	}
}
@end







|



79
80
81
82
83
84
85
86
87
88
89
				    controller.rightAnalogStickPosition;
				[OFStdOut writeFormat: @"(%5.2f, %5.2f)",
						       position.x, position.y];
			}
			[OFStdOut writeString: @"\n"];
		}

		[OFThread sleepForTimeInterval: 1.f / 60.f];
	}
}
@end