ObjFW  Diff

Differences From Artifact [cec84b63cd]:

To Artifact [91b0510999]:


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







-
+

-
+

-
+

-
+




-
-
-
-
+
+
+
+

-
+

-
+

-
+




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

-
+




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

-
+

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







static OFGameControllerButton
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDLeftJoycon) {
		switch (button) {
		case BTN_SELECT:
			return OFGameControllerButtonMinus;
			return OFGameControllerMinusButton;
		case BTN_Z:
			return OFGameControllerButtonCapture;
			return OFGameControllerCaptureButton;
		case BTN_TR:
			return OFGameControllerButtonSL;
			return OFGameControllerSLButton;
		case BTN_TR2:
			return OFGameControllerButtonSR;
			return OFGameControllerSRButton;
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDRightJoycon) {
		switch (button) {
		case BTN_B:
			return OFGameControllerButtonA;
		case BTN_A:
			return OFGameControllerButtonB;
		case BTN_X:
			return OFGameControllerNorthButton;
		case BTN_Y:
			return OFGameControllerWestButton;
		case BTN_START:
			return OFGameControllerButtonPlus;
			return OFGameControllerPlusButton;
		case BTN_TL:
			return OFGameControllerButtonSL;
			return OFGameControllerSLButton;
		case BTN_TL2:
			return OFGameControllerButtonSR;
			return OFGameControllerSRButton;
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		switch (button) {
		case BTN_TL2:
			return OFGameControllerButtonZ;
		case BTN_Y:
			return OFGameControllerButtonCPadLeft;
		case BTN_C:
			return OFGameControllerButtonCPadRight;
		case BTN_SELECT:
			return OFGameControllerButtonCPadUp;
		case BTN_X:
			return OFGameControllerButtonCPadDown;
		case BTN_MODE:
			return OFGameControllerButtonHome;
		case BTN_A:
			return OFGameControllerAButton;
		case BTN_B:
			return OFGameControllerBButton;
		case BTN_SELECT:
			return OFGameControllerCPadUpButton;
		case BTN_X:
			return OFGameControllerCPadDownButton;
		case BTN_Y:
			return OFGameControllerCPadLeftButton;
		case BTN_C:
			return OFGameControllerCPadRightButton;
		case BTN_Z:
			return OFGameControllerButtonCapture;
			return OFGameControllerCaptureButton;
		}
	}

	switch (button) {
	case BTN_A:
		return OFGameControllerButtonA;
	case BTN_B:
		return OFGameControllerButtonB;
	case BTN_C:
		return OFGameControllerButtonC;
	case BTN_X:
		return OFGameControllerButtonX;
	case BTN_Y:
		return OFGameControllerButtonY;
	case BTN_Z:
		return OFGameControllerButtonZ;
	case BTN_Y:
		return OFGameControllerNorthButton;
	case BTN_A:
		return OFGameControllerSouthButton;
	case BTN_X:
		return OFGameControllerWestButton;
	case BTN_B:
		return OFGameControllerEastButton;
	case BTN_TL2:
		return OFGameControllerLeftTriggerButton;
	case BTN_TR2:
		return OFGameControllerRightTriggerButton;
	case BTN_TL:
		return OFGameControllerButtonL;
		return OFGameControllerLeftShoulderButton;
	case BTN_TR:
		return OFGameControllerButtonR;
	case BTN_TL2:
		return OFGameControllerButtonZL;
	case BTN_TR2:
		return OFGameControllerButtonZR;
	case BTN_SELECT:
		return OFGameControllerButtonSelect;
	case BTN_START:
		return OFGameControllerButtonStart;
	case BTN_MODE:
		return OFGameControllerButtonHome;
	case BTN_THUMBL:
		return OFGameControllerButtonLeftStick;
	case BTN_THUMBR:
		return OFGameControllerButtonRightStick;
	case BTN_DPAD_UP:
		return OFGameControllerButtonDPadUp;
	case BTN_DPAD_DOWN:
		return OFGameControllerButtonDPadDown;
	case BTN_DPAD_LEFT:
		return OFGameControllerButtonDPadLeft;
	case BTN_DPAD_RIGHT:
		return OFGameControllerButtonDPadRight;
		return OFGameControllerRightShoulderButton;
	case BTN_THUMBL:
		return OFGameControllerLeftStickButton;
	case BTN_THUMBR:
		return OFGameControllerRightStickButton;
	case BTN_DPAD_UP:
		return OFGameControllerDPadUpButton;
	case BTN_DPAD_DOWN:
		return OFGameControllerDPadDownButton;
	case BTN_DPAD_LEFT:
		return OFGameControllerDPadLeftButton;
	case BTN_DPAD_RIGHT:
		return OFGameControllerDPadRightButton;
	case BTN_START:
		return OFGameControllerStartButton;
	case BTN_SELECT:
		return OFGameControllerSelectButton;
	case BTN_MODE:
		return OFGameControllerHomeButton;
	case BTN_C:
		return OFGameControllerCButton;
	case BTN_Z:
		return OFGameControllerZButton;
	}

	return nil;
}

static float
scale(float value, float min, float max)
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
347

348
349
350
351
352
353
354


355
356


357
358
359
360
361
362
363
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
347

348
349
350
351
352
353


354
355
356

357
358
359
360
361
362
363
364
365







-
+

-
+

-
+

-
+





-
+





-
-
+
+

-
+
+





-
+





-
-
+
+

-
+
+







				_rightAnalogStickMinY = infoY.minimum;
				_rightAnalogStickMaxY = infoY.maximum;
			}

			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				[_buttons addObject:
				    OFGameControllerButtonDPadLeft];
				    OFGameControllerDPadLeftButton];
				[_buttons addObject:
				    OFGameControllerButtonDPadRight];
				    OFGameControllerDPadRightButton];
				[_buttons addObject:
				    OFGameControllerButtonDPadUp];
				    OFGameControllerDPadUpButton];
				[_buttons addObject:
				    OFGameControllerButtonDPadDown];
				    OFGameControllerDPadDownButton];
			}

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

				_hasZLPressure = true;
				_hasLeftTriggerPressure = true;

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

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

				[_buttons addObject: OFGameControllerButtonZL];
				[_buttons addObject:
				    OFGameControllerLeftTriggerButton];
			}

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

				_hasZRPressure = true;
				_hasRightTriggerPressure = true;

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

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

				[_buttons addObject: OFGameControllerButtonZR];
				[_buttons addObject:
				    OFGameControllerRightTriggerButton];
			}
		}

		[_buttons makeImmutable];

		[self retrieveState];
	} @catch (id e) {
437
438
439
440
441
442
443
444

445
446

447
448
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
439
440
441
442
443
444
445

446
447

448
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







-
+

-
+


-
+

-
+


-
+

-
+





-
+

-
+


-
+

-
+


-
+

-
+



-
-
+
+
+

-
+

-
+


-
+


-
-
+
+
+

-
+

-
+


-
+







				_rightAnalogStickPosition.y = scale(event.value,
				    _rightAnalogStickMinY,
				    _rightAnalogStickMaxY);
				break;
			case ABS_HAT0X:
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerButtonDPadLeft];
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadRight];
					    OFGameControllerDPadRightButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerButtonDPadRight];
					    OFGameControllerDPadRightButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadLeft];
					    OFGameControllerDPadLeftButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadLeft];
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadRight];
					    OFGameControllerDPadRightButton];
				}
				break;
			case ABS_HAT0Y:
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerButtonDPadUp];
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadDown];
					    OFGameControllerDPadDownButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerButtonDPadDown];
					    OFGameControllerDPadDownButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadUp];
					    OFGameControllerDPadUpButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadUp];
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerButtonDPadDown];
					    OFGameControllerDPadDownButton];
				}
				break;
			case ABS_Z:
				_ZLPressure = scale(event.value,
				    _ZLMinPressure, _ZLMaxPressure);
				_leftTriggerPressure = scale(event.value,
				    _leftTriggerMinPressure,
				    _leftTriggerMaxPressure);

				if (_ZLPressure > 0)
				if (_leftTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerButtonZL];
					    OFGameControllerLeftTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerButtonZL];
					    OFGameControllerLeftTriggerButton];
				break;
			case ABS_RZ:
				_ZRPressure = scale(event.value,
				    _ZRMinPressure, _ZRMaxPressure);
				_rightTriggerPressure = scale(event.value,
				    _rightTriggerMinPressure,
				    _rightTriggerMaxPressure);

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

			break;
		}
	}
}
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
529
530
531
532
533
534
535




536
537
538
539
540
541
542
543
544
545




















































546
547
548
549
550







-
-
-
-
+
+
+
+
+
+




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





- (OFSet *)pressedButtons
{
	return [[_pressedButtons copy] autorelease];
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	if (button == OFGameControllerButtonZL && _hasZLPressure)
		return _ZLPressure;
	if (button == OFGameControllerButtonZR && _hasZRPressure)
		return _ZRPressure;
	if (button == OFGameControllerLeftTriggerButton &&
	    _hasLeftTriggerPressure)
		return _leftTriggerPressure;
	if (button == OFGameControllerRightTriggerButton &&
	    _hasRightTriggerPressure)
		return _rightTriggerPressure;

	return ([self.pressedButtons containsObject: button] ? 1 : 0);
}

- (OFGameControllerButton)northButton
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoycon)
		return nil;
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoycon)
		return OFGameControllerButtonX;
	if (_vendorID == vendorIDNintendo &&
	    _productID == productIDN64Controller)
		return nil;

	return OFGameControllerButtonY;
}

- (OFGameControllerButton)southButton
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoycon)
		return nil;
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoycon)
		return OFGameControllerButtonB;
	if (_vendorID == vendorIDNintendo &&
	    _productID == productIDN64Controller)
		return nil;

	return OFGameControllerButtonA;
}

- (OFGameControllerButton)westButton
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoycon)
		return nil;
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoycon)
		return OFGameControllerButtonY;
	if (_vendorID == vendorIDNintendo &&
	    _productID == productIDN64Controller)
		return nil;

	return OFGameControllerButtonX;
}

- (OFGameControllerButton)eastButton
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoycon)
		return nil;
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoycon)
		return OFGameControllerButtonA;
	if (_vendorID == vendorIDNintendo &&
	    _productID == productIDN64Controller)
		return nil;

	return OFGameControllerButtonB;
}

- (OFString *)description
{
	return [OFString stringWithFormat: @"<%@: %@>", self.class, self.name];
}
@end