ObjFW  Diff

Differences From Artifact [3b09a9b74f]:

  • File src/hid/platform/Linux/OFGameController.m — part of check-in [5fdf28dd29] at 2024-05-15 19:34:12 on branch gamecontroller — OFGameController: Undo the last commit

    It seems that just checking for the Xbox 360 controller VID/PID is not
    sufficient, as there are a lot of controllers that are compatible with
    it, but have a different VID/PID. However, they all use the xpad driver,
    which returns BTN_X and BTN_Y, which in the Linux headers are equal to
    BTN_NORTH and BTN_WEST - which is incorrect for an Xbox 360 controller.
    It seems the Linux kernel is a huge mess where some drivers return what
    is written on the button (such as Xbox 360 via the xpad driver, which
    returns BTN_X/BTN_Y) and others where they are located (Sony, Nintendo,
    which return BTN_NORTH/BTN_WEST). However, since Linux also maps
    BTN_X/BTN_NORTH and BTN_Y/BTN_WEST to the same code, it's impossible to
    figure out what the driver meant to do.

    As grepping the kernel code for BTN_NORTH has revealed that not a lot of
    drivers use it and given that most controllers these days are XInput
    controllers (which are handled by the xpad driver), it makes more sense
    to assume that a controller is an XInput controller and map the Xbox 360
    buttons to their locations. It's much easier to keep a list of
    controllers with drivers that actually return BTN_NORTH/BTN_WEST instead
    of what's written on the button (so far it seems to be only Nintendo and
    Sony) than to keep a list of all XInput controllers. (user: js, size: 15216) [annotate] [blame] [check-ins using]

To Artifact [8fba4596f9]:


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







+





+












+
+
+







-
+
+







 *
 *   Microsoft X-Box 360 pad [045E:028E]
 *   Joy-Con (L) [057E:2006]
 *   Joy-Con (R) [057E:2007]
 *   N64 Controller [057E:2019]
 *   Sony Interactive Entertainment DualSense Wireless Controller [054C:0CE6]
 *   8BitDo Pro 2 Wired Controller [2DC8:3106]
 *   Stadia2SZY-0d6c [18D1:9400]
 */

static const uint16_t vendorIDMicrosoft = 0x045E;
static const uint16_t vendorIDNintendo = 0x057E;
static const uint16_t vendorIDSony = 0x054C;
static const uint16_t vendorIDGoogle = 0x18D1;

/* Microsoft controllers */
static const uint16_t productIDXbox360 = 0x028E;

/* Nintendo controllers */
static const uint16_t productIDLeftJoycon = 0x2006;
static const uint16_t productIDRightJoycon = 0x2007;
static const uint16_t productIDN64Controller = 0x2019;

/* Sony controllers */
static const uint16_t productIDDualSense = 0x0CE6;

/* Google controllers */
static const uint16_t productIDStadia = 0x9400;

@interface OFGameController ()
- (instancetype)of_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
@end

static const uint16_t buttons[] = {
	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2,
	BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_THUMBL, BTN_THUMBR,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2
};

static OFGameControllerButton
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDLeftJoycon) {
125
126
127
128
129
130
131







132
133
134
135
136
137
138
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151







+
+
+
+
+
+
+







	} else if (vendorID == vendorIDSony &&
	    productID == productIDDualSense) {
		switch (button) {
		case BTN_NORTH:
			return OFGameControllerNorthButton;
		case BTN_WEST:
			return OFGameControllerWestButton;
		}
	} else if (vendorID == vendorIDGoogle && productID == productIDStadia) {
		switch (button) {
		case BTN_TRIGGER_HAPPY1:
			return OFGameControllerAssistantButton;
		case BTN_TRIGGER_HAPPY2:
			return OFGameControllerCaptureButton;
		}
	}

	switch (button) {
	case BTN_Y:
		return OFGameControllerNorthButton;
	case BTN_A:
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
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
344
345
346







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








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







			@throw [OFInitializationFailedException exception];

		_name = [[OFString alloc] initWithCString: name
						 encoding: encoding];

		_buttons = [[OFMutableSet alloc] init];
		for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons);
		    i++)
			if (OFBitSetIsSet(keyBits, buttons[i]))
				[_buttons addObject: buttonToName(
				    buttons[i], _vendorID, _productID)];
		    i++) {
			if (OFBitSetIsSet(keyBits, buttons[i])) {
				OFGameControllerButton button = buttonToName(
				    buttons[i], _vendorID, _productID);

				if (button != nil)
					[_buttons addObject: button];
			}
		}

		_pressedButtons = [[OFMutableSet alloc] init];

		if (OFBitSetIsSet(evBits, EV_ABS)) {
			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),
			    absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (_vendorID == vendorIDGoogle &&
			    _productID == productIDStadia) {
				/*
				 * It's unclear how this can be screwed up
				 * *this* bad.
				 */
				_rightAnalogStickXBit = ABS_Z;
				_rightAnalogStickYBit = ABS_RZ;
				_leftTriggerPressureBit = ABS_BRAKE;
				_rightTriggerPressureBit = ABS_GAS;
			} else {
				_rightAnalogStickXBit = ABS_RX;
				_rightAnalogStickYBit = ABS_RY;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			}

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

				_hasLeftAnalogStick = true;

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
354
355
356
357
358
359
360


361
362
363
364
365
366

367
368
369
370
371

372
373
374
375
376
377
378
379
380







-
-
+
+




-
+
+



-
+
+








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

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

				_hasRightAnalogStick = true;

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

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

				_rightAnalogStickMinX = infoX.minimum;
				_rightAnalogStickMaxX = infoX.maximum;
				_rightAnalogStickMinY = infoY.minimum;
				_rightAnalogStickMaxY = infoY.maximum;
351
352
353
354
355
356
357
358

359
360
361
362
363


364
365
366
367
368
369
370
371
372
373
374

375
376
377
378
379


380
381
382
383
384
385
386
388
389
390
391
392
393
394

395
396
397
398
399

400
401
402
403
404
405
406
407
408
409
410
411

412
413
414
415
416

417
418
419
420
421
422
423
424
425







-
+




-
+
+










-
+




-
+
+







				    OFGameControllerDPadRightButton];
				[_buttons addObject:
				    OFGameControllerDPadUpButton];
				[_buttons addObject:
				    OFGameControllerDPadDownButton];
			}

			if (OFBitSetIsSet(absBits, ABS_Z)) {
			if (OFBitSetIsSet(absBits, _leftTriggerPressureBit)) {
				struct input_absinfo info;

				_hasLeftTriggerPressure = true;

				if (ioctl(_fd, EVIOCGABS(ABS_Z), &info) == -1)
				if (ioctl(_fd, EVIOCGABS(
				    _leftTriggerPressureBit), &info) == -1)
					@throw [OFInitializationFailedException
					    exception];

				_leftTriggerMinPressure = info.minimum;
				_leftTriggerMaxPressure = info.maximum;

				[_buttons addObject:
				    OFGameControllerLeftTriggerButton];
			}

			if (OFBitSetIsSet(absBits, ABS_RZ)) {
			if (OFBitSetIsSet(absBits, _rightTriggerPressureBit)) {
				struct input_absinfo info;

				_hasRightTriggerPressure = true;

				if (ioctl(_fd, EVIOCGABS(ABS_RZ), &info) == -1)
				if (ioctl(_fd, EVIOCGABS(
				    _rightTriggerPressureBit), &info) == -1)
					@throw [OFInitializationFailedException
					    exception];

				_rightTriggerMinPressure = info.minimum;
				_rightTriggerMaxPressure = info.maximum;

				[_buttons addObject:
449
450
451
452
453
454
455
456

457
458
459
460
461

462
463
464
465

466
467
468
469
470

471
472
473
474
475

476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511

512
513
514
515
516
517
518
519
520
521
522

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
488
489
490
491
492
493
494

495

496
497


498
499
500


501
502
503
504


505
506
507
508


509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525


526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542


543
544
545
546
547
548
549
550
551
552
553

554

555
556
557
558
559
560
561
562
563
564

565

566
567
568
569
570
571
572







-
+
-


-
-
+


-
-
+



-
-
+



-
-
+
















-
-
+
















-
-
+










-
+
-










-

-







				if (event.value)
					[_pressedButtons addObject: button];
				else
					[_pressedButtons removeObject: button];
			}
			break;
		case EV_ABS:
			switch (event.code) {
			if (event.code == ABS_X)
			case ABS_X:
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);
				break;
			case ABS_Y:
			else if (event.code == ABS_Y)
				_leftAnalogStickPosition.y = scale(event.value,
				    _leftAnalogStickMinY, _leftAnalogStickMaxY);
				break;
			case ABS_RX:
			else if (event.code == _rightAnalogStickXBit)
				_rightAnalogStickPosition.x = scale(event.value,
				    _rightAnalogStickMinX,
				    _rightAnalogStickMaxX);
				break;
			case ABS_RY:
			else if (event.code == _rightAnalogStickYBit)
				_rightAnalogStickPosition.y = scale(event.value,
				    _rightAnalogStickMinY,
				    _rightAnalogStickMaxY);
				break;
			case ABS_HAT0X:
			else if (event.code == ABS_HAT0X) {
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadRightButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadRightButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadRightButton];
				}
				break;
			case ABS_HAT0Y:
			} else if (event.code == ABS_HAT0Y) {
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadDownButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadDownButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadDownButton];
				}
				break;
			case ABS_Z:
			} else if (event.code == _leftTriggerPressureBit) {
				_leftTriggerPressure = scale(event.value,
				    _leftTriggerMinPressure,
				    _leftTriggerMaxPressure);

				if (_leftTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerLeftTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerLeftTriggerButton];
				break;
			} else if (event.code == _rightTriggerPressureBit) {
			case ABS_RZ:
				_rightTriggerPressure = scale(event.value,
				    _rightTriggerMinPressure,
				    _rightTriggerMaxPressure);

				if (_rightTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerRightTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerRightTriggerButton];
				break;
			}

			break;
		}
	}
}

- (OFComparisonResult)compare: (OFGameController *)otherController
{