︙ | | | ︙ | |
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
|
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerMapping.h"
#include <sys/ioctl.h>
#include <linux/input.h>
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
@interface OHEvdevGameControllerAxis: OHGameControllerAxis
{
@public
int32_t _minValue, _maxValue;
}
@end
@interface OHEvdevGameControllerMapping: OHGameControllerMapping
- (instancetype)of_initWithButtons: (OFDictionary *)buttons
axes: (OFDictionary *)axes OF_METHOD_FAMILY(init);
@end
static const uint16_t buttonIDs[] = {
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,
|
|
|
|
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
|
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerProfile.h"
#include <sys/ioctl.h>
#include <linux/input.h>
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
@interface OHEvdevGameControllerAxis: OHGameControllerAxis
{
@public
int32_t _minValue, _maxValue;
}
@end
@interface OHEvdevGameControllerProfile: OHGameControllerProfile
- (instancetype)of_initWithButtons: (OFDictionary *)buttons
axes: (OFDictionary *)axes OF_METHOD_FAMILY(init);
@end
static const uint16_t buttonIDs[] = {
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,
|
︙ | | | ︙ | |
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
if (value > max)
value = max;
return ((value - min) / (max - min) * 2) - 1;
}
@implementation OHEvdevGameController
@synthesize name = _name, unmappedMapping = _mapping;
+ (OFArray OF_GENERIC(OHGameController *) *)controllers
{
OFMutableArray *controllers = [OFMutableArray array];
void *pool = objc_autoreleasePoolPush();
for (OFString *device in [[OFFileManager defaultManager]
|
|
|
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
if (value > max)
value = max;
return ((value - min) / (max - min) * 2) - 1;
}
@implementation OHEvdevGameController
@synthesize name = _name, rawProfile = _rawProfile;
+ (OFArray OF_GENERIC(OHGameController *) *)controllers
{
OFMutableArray *controllers = [OFMutableArray array];
void *pool = objc_autoreleasePoolPush();
for (OFString *device in [[OFFileManager defaultManager]
|
︙ | | | ︙ | |
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
[axes setObject: axis forKey: axisName];
}
}
}
[axes makeImmutable];
_mapping = [[OHEvdevGameControllerMapping alloc]
of_initWithButtons: buttons
axes: axes];
[self of_pollState];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
|
|
|
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
[axes setObject: axis forKey: axisName];
}
}
}
[axes makeImmutable];
_rawProfile = [[OHEvdevGameControllerProfile alloc]
of_initWithButtons: buttons
axes: axes];
[self of_pollState];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
|
︙ | | | ︙ | |
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
close(_fd);
OFFreeMemory(_evBits);
OFFreeMemory(_keyBits);
OFFreeMemory(_absBits);
[_name release];
[_mapping release];
[super dealloc];
}
- (OFNumber *)vendorID
{
return [OFNumber numberWithUnsignedShort: _vendorID];
|
|
|
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
close(_fd);
OFFreeMemory(_evBits);
OFFreeMemory(_keyBits);
OFFreeMemory(_absBits);
[_name release];
[_rawProfile release];
[super dealloc];
}
- (OFNumber *)vendorID
{
return [OFNumber numberWithUnsignedShort: _vendorID];
|
︙ | | | ︙ | |
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
if (!OFBitSetIsSet(_keyBits, buttonIDs[i]))
continue;
name = buttonToName(buttonIDs[i]);
if (name == nil)
continue;
button = [_mapping.buttons objectForKey: name];
if (button == nil)
continue;
if (OFBitSetIsSet(keyState, buttonIDs[i]))
button.value = 1.f;
else
button.value = 0.f;
|
|
|
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
if (!OFBitSetIsSet(_keyBits, buttonIDs[i]))
continue;
name = buttonToName(buttonIDs[i]);
if (name == nil)
continue;
button = [_rawProfile.buttons objectForKey: name];
if (button == nil)
continue;
if (OFBitSetIsSet(keyState, buttonIDs[i]))
button.value = 1.f;
else
button.value = 0.f;
|
︙ | | | ︙ | |
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
continue;
name = axisToName(axisIDs[i]);
if (name == nil)
continue;
axis = (OHEvdevGameControllerAxis *)
[_mapping.axes objectForKey: name];
if (axis == nil)
continue;
if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: sizeof(info)
|
|
|
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
continue;
name = axisToName(axisIDs[i]);
if (name == nil)
continue;
axis = (OHEvdevGameControllerAxis *)
[_rawProfile.axes objectForKey: name];
if (axis == nil)
continue;
if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: sizeof(info)
|
︙ | | | ︙ | |
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
|
}
break;
case EV_KEY:
name = buttonToName(event.code);
if (name == nil)
continue;
button = [_mapping.buttons objectForKey: name];
if (button == nil)
continue;
if (event.value)
button.value = 1.f;
else
button.value = 0.f;
break;
case EV_ABS:
name = axisToName(event.code);
if (name == nil)
continue;
axis = (OHEvdevGameControllerAxis *)
[_mapping.axes objectForKey: name];
if (axis == nil)
continue;
axis.value = scale(event.value,
axis->_minValue, axis->_maxValue);
break;
|
|
|
|
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
|
}
break;
case EV_KEY:
name = buttonToName(event.code);
if (name == nil)
continue;
button = [_rawProfile.buttons objectForKey: name];
if (button == nil)
continue;
if (event.value)
button.value = 1.f;
else
button.value = 0.f;
break;
case EV_ABS:
name = axisToName(event.code);
if (name == nil)
continue;
axis = (OHEvdevGameControllerAxis *)
[_rawProfile.axes objectForKey: name];
if (axis == nil)
continue;
axis.value = scale(event.value,
axis->_minValue, axis->_maxValue);
break;
|
︙ | | | ︙ | |
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
return OFOrderedSame;
}
@end
@implementation OHEvdevGameControllerAxis
@end
@implementation OHEvdevGameControllerMapping
- (instancetype)of_initWithButtons: (OFDictionary *)buttons
axes: (OFDictionary *)axes
{
self = [super init];
@try {
_buttons = [buttons retain];
|
|
|
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
return OFOrderedSame;
}
@end
@implementation OHEvdevGameControllerAxis
@end
@implementation OHEvdevGameControllerProfile
- (instancetype)of_initWithButtons: (OFDictionary *)buttons
axes: (OFDictionary *)axes
{
self = [super init];
@try {
_buttons = [buttons retain];
|
︙ | | | ︙ | |
︙ | | | ︙ | |
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# endif
#endif
OF_ASSUME_NONNULL_BEGIN
@class OFArray OF_GENERIC(ObjectType);
@class OFNumber;
@class OHGameControllerMapping;
/**
* @class OHGameController OHGameController.h ObjFWHID/OHGameController.h
*
* @brief A class for reading state from a game controller.
*/
@interface OHGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
OFArray <OHGameController *> *controllers;
#endif
/**
* @brief The name of the controller.
|
|
>
>
>
>
|
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
|
# endif
#endif
OF_ASSUME_NONNULL_BEGIN
@class OFArray OF_GENERIC(ObjectType);
@class OFNumber;
@class OHGameControllerProfile;
/**
* @class OHGameController OHGameController.h ObjFWHID/OHGameController.h
*
* @brief A class for reading state from a game controller.
*/
@interface OHGameController: OFObject
{
OF_RESERVE_IVARS(OHGameController, 4)
}
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
OFArray <OHGameController *> *controllers;
#endif
/**
* @brief The name of the controller.
|
︙ | | | ︙ | |
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/**
* @brief The product ID of the controller or `nil` if unavailable.
*/
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *productID;
/**
* @brief An unmapped mapping for the game controller, meaning no remapping is
* being performed.
*/
@property (readonly, nonatomic) OHGameControllerMapping *unmappedMapping;
/**
* @brief Returns the available controllers.
*
* @return The available controllers
*/
+ (OFArray OF_GENERIC(OHGameController *) *)controllers;
|
|
|
|
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/**
* @brief The product ID of the controller or `nil` if unavailable.
*/
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *productID;
/**
* @brief The raw profile for the game controller, meaning no remapping is
* being performed.
*/
@property (readonly, nonatomic) OHGameControllerProfile *rawProfile;
/**
* @brief Returns the available controllers.
*
* @return The available controllers
*/
+ (OFArray OF_GENERIC(OHGameController *) *)controllers;
|
︙ | | | ︙ | |
︙ | | | ︙ | |
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#import "OFThread.h"
#import "OFValue.h"
#import "OTTestCase.h"
#import "OHGameController.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerMapping.h"
#import "OTAssertionFailedException.h"
#import "OTTestSkippedException.h"
#ifdef OF_IOS
# include <CoreFoundation/CoreFoundation.h>
#endif
|
|
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#import "OFThread.h"
#import "OFValue.h"
#import "OTTestCase.h"
#import "OHGameController.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerProfile.h"
#import "OTAssertionFailedException.h"
#import "OTTestSkippedException.h"
#ifdef OF_IOS
# include <CoreFoundation/CoreFoundation.h>
#endif
|
︙ | | | ︙ | |
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
[OFStdOut writeLine: @"Press A to continue"];
for (;;) {
void *pool = objc_autoreleasePoolPush();
OHGameController *controller =
[[OHGameController controllers] objectAtIndex: 0];
OHGameControllerButton *button =
[controller.unmappedMapping.buttons
objectForKey: @"A"];
[controller retrieveState];
if (button.pressed)
break;
[OFThread waitForVerticalBlank];
|
<
|
|
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
[OFStdOut writeLine: @"Press A to continue"];
for (;;) {
void *pool = objc_autoreleasePoolPush();
OHGameController *controller =
[[OHGameController controllers] objectAtIndex: 0];
OHGameControllerButton *button =
[controller.rawProfile.buttons objectForKey: @"A"];
[controller retrieveState];
if (button.pressed)
break;
[OFThread waitForVerticalBlank];
|
︙ | | | ︙ | |
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
for (;;) {
void *pool = objc_autoreleasePoolPush();
OHGameController *controller =
[[OHGameController controllers] objectAtIndex: 0];
OHGameControllerButton *button =
# ifdef OF_WII
[controller.unmappedMapping.buttons objectForKey: @"Home"];
# else
[controller.unmappedMapping.buttons objectForKey: @"Start"];
# endif
[controller retrieveState];
if (button.pressed)
break;
|
|
|
|
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
for (;;) {
void *pool = objc_autoreleasePoolPush();
OHGameController *controller =
[[OHGameController controllers] objectAtIndex: 0];
OHGameControllerButton *button =
# ifdef OF_WII
[controller.rawProfile.buttons objectForKey: @"Home"];
# else
[controller.rawProfile.buttons objectForKey: @"Start"];
# endif
[controller retrieveState];
if (button.pressed)
break;
|
︙ | | | ︙ | |
︙ | | | ︙ | |
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"
#import "OHGameController.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerMapping.h"
#import "OFReadFailedException.h"
#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
|
|
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#import "OFNumber.h"
#import "OFStdIOStream.h"
#import "OFThread.h"
#import "OHGameController.h"
#import "OHGameControllerAxis.h"
#import "OHGameControllerButton.h"
#import "OHGameControllerProfile.h"
#import "OFReadFailedException.h"
#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
|
︙ | | | ︙ | |
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
|
[OFStdOut clear];
}
[OFStdOut setCursorPosition: OFMakePoint(0, 0)];
for (OHGameController *controller in _controllers) {
OHGameControllerMapping *mapping =
controller.unmappedMapping;
OFArray OF_GENERIC(OFString *) *buttons =
mapping.buttons.allKeys.sortedArray;
OFArray OF_GENERIC(OFString *) *axes =
mapping.axes.allKeys.sortedArray;
size_t i;
[OFStdOut setForegroundColor: [OFColor green]];
[OFStdOut writeString: controller.description];
@try {
[controller retrieveState];
} @catch (OFReadFailedException *e) {
[OFStdOut setForegroundColor: [OFColor red]];
[OFStdOut writeFormat: @"\n%@", e.description];
continue;
}
i = 0;
for (OFString *name in buttons) {
OHGameControllerButton *button =
[mapping.buttons objectForKey: name];
if (i == 0)
[OFStdOut writeString: @"\n"];
if (button.value == 1)
[OFStdOut setForegroundColor:
[OFColor red]];
|
|
|
|
|
|
|
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
|
[OFStdOut clear];
}
[OFStdOut setCursorPosition: OFMakePoint(0, 0)];
for (OHGameController *controller in _controllers) {
OHGameControllerProfile *profile =
controller.rawProfile;
OFArray OF_GENERIC(OFString *) *buttons =
profile.buttons.allKeys.sortedArray;
OFArray OF_GENERIC(OFString *) *axes =
profile.axes.allKeys.sortedArray;
size_t i;
[OFStdOut setForegroundColor: [OFColor green]];
[OFStdOut writeString: controller.description];
@try {
[controller retrieveState];
} @catch (OFReadFailedException *e) {
[OFStdOut setForegroundColor: [OFColor red]];
[OFStdOut writeFormat: @"\n%@", e.description];
continue;
}
i = 0;
for (OFString *name in buttons) {
OHGameControllerButton *button =
[profile.buttons objectForKey: name];
if (i == 0)
[OFStdOut writeString: @"\n"];
if (button.value == 1)
[OFStdOut setForegroundColor:
[OFColor red]];
|
︙ | | | ︙ | |
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
}
[OFStdOut setForegroundColor: [OFColor gray]];
[OFStdOut writeString: @"\n"];
i = 0;
for (OFString *name in axes) {
OHGameControllerAxis *axis =
[mapping.axes objectForKey: name];
if (i == 0)
[OFStdOut writeString: @"\n"];
[OFStdOut writeFormat: @"%@: %5.2f ",
name, axis.value];
|
|
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
}
[OFStdOut setForegroundColor: [OFColor gray]];
[OFStdOut writeString: @"\n"];
i = 0;
for (OFString *name in axes) {
OHGameControllerAxis *axis =
[profile.axes objectForKey: name];
if (i == 0)
[OFStdOut writeString: @"\n"];
[OFStdOut writeFormat: @"%@: %5.2f ",
name, axis.value];
|
︙ | | | ︙ | |