ObjFW  Check-in [16aab59c3c]

Overview
Comment:Add HIDGameControllerMapping
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 16aab59c3c36d805c08aa9483dfe2fa3ad3b32c081f019ccd9b26f5473dc4788
User & Date: js on 2024-06-02 23:51:01
Other Links: manifest | tags
Context
2024-06-03
00:31
Make GCC happy again check-in: 4bee9b1b32 user: js tags: trunk
2024-06-02
23:51
Add HIDGameControllerMapping check-in: 16aab59c3c user: js tags: trunk
22:17
Fix missing import and fix typo check-in: 112d9a496f user: js tags: trunk
Changes

Modified Doxyfile from [a7728bfdca] to [9675d9af4d].

45
46
47
48
49
50
51
52
53
	     OF_WARN_UNUSED_RESULT=				\
	     OF_WEAK_UNAVAILABLE=				\
	     SIGHUP						\
	     SIGUSR1						\
	     SIGUSR2
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
IGNORE_PREFIX = OF OF_ OT OT_
EXTRACT_STATIC = yes







|

45
46
47
48
49
50
51
52
53
	     OF_WARN_UNUSED_RESULT=				\
	     OF_WEAK_UNAVAILABLE=				\
	     SIGHUP						\
	     SIGUSR1						\
	     SIGUSR2
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
IGNORE_PREFIX = HID OF OF_ OT OT_
EXTRACT_STATIC = yes

Modified src/hid/HIDEvdevGameController.h from [448b671f74] to [40e78502c5].

16
17
18
19
20
21
22


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameController.h"

OF_ASSUME_NONNULL_BEGIN



@interface HIDEvdevGameController: HIDGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;
	uint16_t _vendorID, _productID;
	OFString *_name;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *)
	    *_buttons;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *_axes;
}

- (instancetype)of_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
- (void)of_pollState;
@end

OF_ASSUME_NONNULL_END







>
>









|
<
<







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "HIDGameController.h"

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerMapping;

@interface HIDEvdevGameController: HIDGameController
{
	OFString *_path;
	int _fd;
	bool _discardUntilReport;
	unsigned long *_evBits, *_keyBits, *_absBits;
	uint16_t _vendorID, _productID;
	OFString *_name;
	HIDGameControllerMapping *_mapping;


}

- (instancetype)of_initWithPath: (OFString *)path OF_METHOD_FAMILY(init);
- (void)of_pollState;
@end

OF_ASSUME_NONNULL_END

Modified src/hid/HIDEvdevGameController.m from [d527831ab5] to [3928045f56].

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
#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#import "HIDEvdevGameController.h"

#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"

#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.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 HIDEvdevGameControllerAxis: HIDGameControllerAxis
{
@public
	int32_t _minValue, _maxValue;
}
@end

@implementation HIDEvdevGameControllerAxis


@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,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, BTN_TRIGGER_HAPPY3,







>





>


>

















|
>
>







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
#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#import "HIDEvdevGameController.h"

#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"

#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"
#import "HIDGameControllerMapping.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 HIDEvdevGameControllerAxis: HIDGameControllerAxis
{
@public
	int32_t _minValue, _maxValue;
}
@end

@interface HIDEvdevGameControllerMapping: HIDGameControllerMapping
- (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,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, BTN_TRIGGER_HAPPY3,
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	if (value > max)
		value = max;

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

@implementation HIDEvdevGameController
@synthesize name = _name, buttons = _buttons, axes = _axes;

+ (OFArray OF_GENERIC(HIDGameController *) *)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 HIDEvdevGameController
@synthesize name = _name, unmappedMapping = _mapping;

+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
	OFMutableArray *controllers = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();

	for (OFString *device in [[OFFileManager defaultManager]
304
305
306
307
308
309
310

311
312
313
314
315
316
317
}

- (instancetype)of_initWithPath: (OFString *)path
{
	self = [super init];

	@try {

		OFStringEncoding encoding = [OFLocale encoding];
		struct input_id inputID;
		char name[128];
		OFMutableDictionary *buttons, *axes;

		_path = [path copy];








>







309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
}

- (instancetype)of_initWithPath: (OFString *)path
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFStringEncoding encoding = [OFLocale encoding];
		struct input_id inputID;
		char name[128];
		OFMutableDictionary *buttons, *axes;

		_path = [path copy];

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

		if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1)
			@throw [OFInitializationFailedException exception];

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

		buttons = [[OFMutableDictionary alloc] init];
		_buttons = buttons;
		for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
		    i++) {
			if (OFBitSetIsSet(_keyBits, buttonIDs[i])) {
				OFString *buttonName;
				HIDGameControllerButton *button;

				buttonName = buttonToName(buttonIDs[i]);
				if (buttonName == nil)
					continue;

				button = [[[HIDGameControllerButton alloc]
				    initWithName: buttonName] autorelease];

				[buttons setObject: button forKey: buttonName];
			}
		}
		[buttons makeImmutable];

		axes = [[OFMutableDictionary alloc] init];
		_axes = axes;
		if (OFBitSetIsSet(_evBits, EV_ABS)) {
			_absBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT,
			    sizeof(unsigned long));

			if (ioctl(_fd, EVIOCGBIT(EV_ABS, OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT *







|
<


















|
<







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
387
388
389
390
391
392

		if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1)
			@throw [OFInitializationFailedException exception];

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

		buttons = [OFMutableDictionary dictionary];

		for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
		    i++) {
			if (OFBitSetIsSet(_keyBits, buttonIDs[i])) {
				OFString *buttonName;
				HIDGameControllerButton *button;

				buttonName = buttonToName(buttonIDs[i]);
				if (buttonName == nil)
					continue;

				button = [[[HIDGameControllerButton alloc]
				    initWithName: buttonName] autorelease];

				[buttons setObject: button forKey: buttonName];
			}
		}
		[buttons makeImmutable];

		axes = [OFMutableDictionary dictionary];

		if (OFBitSetIsSet(_evBits, EV_ABS)) {
			_absBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT,
			    sizeof(unsigned long));

			if (ioctl(_fd, EVIOCGBIT(EV_ABS, OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT *
405
406
407
408
409
410
411
412




413


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
					    autorelease];

					[axes setObject: axis forKey: axisName];
				}
			}
		}
		[axes makeImmutable];





		[self of_pollState];


	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];

	if (_fd != -1)
		close(_fd);

	OFFreeMemory(_evBits);
	OFFreeMemory(_keyBits);
	OFFreeMemory(_absBits);

	[_name release];
	[_buttons release];
	[_axes release];

	[super dealloc];
}

- (OFNumber *)vendorID
{
	return [OFNumber numberWithUnsignedShort: _vendorID];








>
>
>
>

>
>




















<
|







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

444
445
446
447
448
449
450
451
					    autorelease];

					[axes setObject: axis forKey: axisName];
				}
			}
		}
		[axes makeImmutable];

		_mapping = [[HIDEvdevGameControllerMapping alloc]
		    of_initWithButtons: buttons
				  axes: axes];

		[self of_pollState];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];

	if (_fd != -1)
		close(_fd);

	OFFreeMemory(_evBits);
	OFFreeMemory(_keyBits);
	OFFreeMemory(_absBits);

	[_name release];

	[_mapping release];

	[super dealloc];
}

- (OFNumber *)vendorID
{
	return [OFNumber numberWithUnsignedShort: _vendorID];
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
		if (!OFBitSetIsSet(_keyBits, buttonIDs[i]))
			continue;

		name = buttonToName(buttonIDs[i]);
		if (name == nil)
			continue;

		button = [_buttons objectForKey: name];
		if (button == nil)
			continue;

		button.value = (OFBitSetIsSet(keyState, buttonIDs[i]) ? 1 : 0);
	}

	if (OFBitSetIsSet(_evBits, EV_ABS)) {







|







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;

		button.value = (OFBitSetIsSet(keyState, buttonIDs[i]) ? 1 : 0);
	}

	if (OFBitSetIsSet(_evBits, EV_ABS)) {
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
				continue;

			name = axisToName(axisIDs[i]);
			if (name == nil)
				continue;

			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];
			if (axis == nil)
				continue;

			if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: sizeof(info)
						  errNo: errno];

			axis->_minValue = info.minimum;
			axis->_maxValue = info.maximum;
			axis.value = scale(info.value,
			    info.minimum, info.maximum);
		}
	}
}

- (void)retrieveState
{

	struct input_event event;

	for (;;) {
		OFString *name;
		HIDGameControllerButton *button;
		HIDEvdevGameControllerAxis *axis;








|



















>







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

			name = axisToName(axisIDs[i]);
			if (name == nil)
				continue;

			axis = (HIDEvdevGameControllerAxis *)
			    [_mapping.axes objectForKey: name];
			if (axis == nil)
				continue;

			if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: sizeof(info)
						  errNo: errno];

			axis->_minValue = info.minimum;
			axis->_maxValue = info.maximum;
			axis.value = scale(info.value,
			    info.minimum, info.maximum);
		}
	}
}

- (void)retrieveState
{
	void *pool = objc_autoreleasePoolPush();
	struct input_event event;

	for (;;) {
		OFString *name;
		HIDGameControllerButton *button;
		HIDEvdevGameControllerAxis *axis;

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
			}
			break;
		case EV_KEY:
			name = buttonToName(event.code);
			if (name == nil)
				continue;

			button = [_buttons objectForKey: name];
			if (button == nil)
				continue;

			button.value = (event.value ? 1 : 0);

			break;
		case EV_ABS:
			name = axisToName(event.code);
			if (name == nil)
				continue;

			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];
			if (axis == nil)
				continue;

			axis.value = scale(event.value,
			   axis->_minValue, axis->_maxValue);

			break;
		}
	}


}

- (OFComparisonResult)compare: (HIDEvdevGameController *)otherController
{
	unsigned long long selfIndex, otherIndex;

	if (![otherController isKindOfClass: [HIDEvdevGameController class]])







|












|









>
>







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
			}
			break;
		case EV_KEY:
			name = buttonToName(event.code);
			if (name == nil)
				continue;

			button = [_mapping.buttons objectForKey: name];
			if (button == nil)
				continue;

			button.value = (event.value ? 1 : 0);

			break;
		case EV_ABS:
			name = axisToName(event.code);
			if (name == nil)
				continue;

			axis = (HIDEvdevGameControllerAxis *)
			    [_mapping.axes objectForKey: name];
			if (axis == nil)
				continue;

			axis.value = scale(event.value,
			   axis->_minValue, axis->_maxValue);

			break;
		}
	}

	objc_autoreleasePoolPop(pool);
}

- (OFComparisonResult)compare: (HIDEvdevGameController *)otherController
{
	unsigned long long selfIndex, otherIndex;

	if (![otherController isKindOfClass: [HIDEvdevGameController class]])
592
593
594
595
596
597
598





















		return OFOrderedDescending;
	if (selfIndex < otherIndex)
		return OFOrderedAscending;

	return OFOrderedSame;
}
@end




























>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
		return OFOrderedDescending;
	if (selfIndex < otherIndex)
		return OFOrderedAscending;

	return OFOrderedSame;
}
@end

@implementation HIDEvdevGameControllerAxis
@end

@implementation HIDEvdevGameControllerMapping
- (instancetype)of_initWithButtons: (OFDictionary *)buttons
			      axes: (OFDictionary *)axes
{
	self = [super init];

	@try {
		_buttons = [buttons retain];
		_axes = [axes retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
@end

Modified src/hid/HIDGameController.h from [347fbd7215] to [e37811cd3f].

27
28
29
30
31
32
33
34
35
36
37
38
39
40


41
42
43
44
45
46
47
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerAxis;
@class HIDGameControllerButton;
@class OFArray OF_GENERIC(ObjectType);
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFNumber;

/**


 * @brief A class for reading state from a game controller.
 */
@interface HIDGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <HIDGameController *> *controllers;
#endif







|
<

<



>
>







27
28
29
30
31
32
33
34

35

36
37
38
39
40
41
42
43
44
45
46
47
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerMapping;

@class OFArray OF_GENERIC(ObjectType);

@class OFNumber;

/**
 * @class HIDGameController HIDGameController.h ObjFWHID/HIDGameController.h
 *
 * @brief A class for reading state from a game controller.
 */
@interface HIDGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic)
    OFArray <HIDGameController *> *controllers;
#endif
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

/**
 * @brief The product ID of the controller or `nil` if unavailable.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFNumber *productID;

/**
 * @brief A map of all button names to their @ref HIDGameControllerButton.
 */
@property (readonly, nonatomic)
    OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *) *buttons;

/**
 * @brief A map of all axis names to their @ref HIDGameControllerAxis.
 */
@property (readonly, nonatomic)
    OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *axes;

/**
 * @brief Returns the available controllers.
 *
 * @return The available controllers
 */
+ (OFArray OF_GENERIC(HIDGameController *) *)controllers;







|
<
<
<
|
<
<

|
<







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) HIDGameControllerMapping *unmappedMapping;


/**
 * @brief Returns the available controllers.
 *
 * @return The available controllers
 */
+ (OFArray OF_GENERIC(HIDGameController *) *)controllers;

Modified src/hid/HIDGameController.m from [f3d0e406e6] to [1e275c1bfa].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFSet.h"

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "HIDEvdevGameController.h"
#endif

@implementation HIDGameController
@dynamic name, buttons, axes;

+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
	return [HIDEvdevGameController controllers];
#else
	return [OFArray array];







|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFSet.h"

#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
# include "HIDEvdevGameController.h"
#endif

@implementation HIDGameController
@dynamic name, unmappedMapping;

+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
#if defined(OF_LINUX) && defined(OF_HAVE_FILES)
	return [HIDEvdevGameController controllers];
#else
	return [OFArray array];

Modified src/hid/HIDGameControllerAxis.h from [37943879cb] to [876c2397e0].

18
19
20
21
22
23
24



25
26
27
28
29
30
31
32
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**



 * @brief An button of a game controller.
 */
@interface HIDGameControllerAxis: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}








>
>
>
|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @class HIDGameControllerAxis \
 *	  HIDGameControllerAxis.h ObjFWHID/HIDGameControllerAxis.h
 *
 * @brief An axis of a game controller.
 */
@interface HIDGameControllerAxis: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}

Modified src/hid/HIDGameControllerButton.h from [e13894eaec] to [e6d7487002].

18
19
20
21
22
23
24



25
26
27
28
29
30
31
32
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**



 * @brief An button of a game controller.
 */
@interface HIDGameControllerButton: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}








>
>
>
|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 */

#import "HIDGameControllerElement.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @class HIDGameControllerButton \
 *	  HIDGameControllerButton.h ObjFWHID/HIDGameControllerButton.h
 *
 * @brief A button of a game controller.
 */
@interface HIDGameControllerButton: HIDGameControllerElement
{
	float _value;
	OF_RESERVE_IVARS(HIDGameControllerButton, 4)
}

Modified src/hid/HIDGameControllerDirectionalPad.h from [ba9425bef1] to [2c2bbf2751].

20
21
22
23
24
25
26




27
28
29
30
31
32
33
#import "HIDGameControllerElement.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

OF_ASSUME_NONNULL_BEGIN

/**




 * @brief An directional pad or thumb stick of a game controller.
 */
OF_SUBCLASSING_RESTRICTED
@interface HIDGameControllerDirectionalPad: HIDGameControllerElement
{
	HIDGameControllerAxis *_xAxis, *_yAxis;
	HIDGameControllerButton *_upButton, *_downButton;







>
>
>
>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import "HIDGameControllerElement.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

OF_ASSUME_NONNULL_BEGIN

/**
 * @class HIDGameControllerDirectionalPad \
 *	  HIDGameControllerDirectionalPad.h \
 *	  ObjFWHID/HIDGameControllerDirectionalPad.h
 *
 * @brief An directional pad or thumb stick of a game controller.
 */
OF_SUBCLASSING_RESTRICTED
@interface HIDGameControllerDirectionalPad: HIDGameControllerElement
{
	HIDGameControllerAxis *_xAxis, *_yAxis;
	HIDGameControllerButton *_upButton, *_downButton;

Modified src/hid/HIDGameControllerElement.h from [7e4d7b44de] to [721f4d8e3b].

28
29
30
31
32
33
34



35
36
37
38
39
40
41
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

/**



 * @brief An element of a game controller, e.g. a button, an axis or a
 *	  directional pad.
 */
@interface HIDGameControllerElement: OFObject
{
	OFString *_name;
	bool _analog;







>
>
>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

/**
 * @class HIDGameControllerElement \
 *	  HIDGameControllerElement.h ObjFWHID/HIDGameControllerElement.h
 *
 * @brief An element of a game controller, e.g. a button, an axis or a
 *	  directional pad.
 */
@interface HIDGameControllerElement: OFObject
{
	OFString *_name;
	bool _analog;

Added src/hid/HIDGameControllerMapping.h version [9d245e3712].























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#ifdef OBJFWHID_LOCAL_INCLUDES
# import "OFObject.h"
# import "OFString.h"
#else
# if defined(__has_feature) && __has_feature(modules)
@import ObjFW;
# else
#  import <ObjFW/OFObject.h>
#  import <ObjFW/OFString.h>
# endif
#endif

OF_ASSUME_NONNULL_BEGIN

@class HIDGameControllerAxis;
@class HIDGameControllerButton;
@class HIDGameControllerDirectionalPad;
@class OFDictionary OF_GENERIC(KeyType, ObjectType);

/**
 * @class HIDGameControllerMapping \
 *	  HIDGameControllerMapping.h ObjFWHID/HIDGameControllerMapping.h
 *
 * @brief A mapping for a @ref HIDGameController.
 */
@interface HIDGameControllerMapping: OFObject
{
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *)
	    *_buttons;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *_axes;
	OFDictionary OF_GENERIC(OFString *, HIDGameControllerDirectionalPad *)
	    *_directionalPads;
	OF_RESERVE_IVARS(HIDGameControllerMapping, 4)
}

/**
 * @brief A map of all button names to their @ref HIDGameControllerButton.
 */
@property (readonly, nonatomic)
    OFDictionary OF_GENERIC(OFString *, HIDGameControllerButton *) *buttons;

/**
 * @brief A map of all axis names to their @ref HIDGameControllerAxis.
 */
@property (readonly, nonatomic)
    OFDictionary OF_GENERIC(OFString *, HIDGameControllerAxis *) *axes;

/**
 * @brief A map of all directional pads to their
 *	  @ref HIDGameControllerDirectionalPad.
 */
@property (readonly, nonatomic) OFDictionary OF_GENERIC(OFString *,
    HIDGameControllerDirectionalPad *) *directionalPads;
@end

OF_ASSUME_NONNULL_END

Added src/hid/HIDGameControllerMapping.m version [5697f19019].









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "HIDGameControllerMapping.h"

@implementation HIDGameControllerMapping: OFObject
@synthesize buttons = _buttons, axes = _axes;
@synthesize directionalPads = _directionalPads;

- (void)dealloc
{
	[_buttons release];
	[_axes release];
	[_directionalPads release];

	[super dealloc];
}
@end

Modified src/hid/Makefile from [0ce62570af] to [02731ab3d0].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = HIDGameController.m			\
       HIDGameControllerAxis.m			\
       HIDGameControllerButton.m			\
       HIDGameControllerDirectionalPad.m		\
       HIDGameControllerElement.m


INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h

SRCS += HIDGameControllerEmulatedAxis.m		\
	HIDGameControllerEmulatedButton.m














|
|
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
include ../../extra.mk

DISTCLEAN = Info.plist

SHARED_LIB = ${OBJFWHID_SHARED_LIB}
STATIC_LIB = ${OBJFWHID_STATIC_LIB}
FRAMEWORK = ${OBJFWHID_FRAMEWORK}
LIB_MAJOR = ${OBJFWHID_LIB_MAJOR}
LIB_MINOR = ${OBJFWHID_LIB_MINOR}
LIB_PATCH = ${OBJFWHID_LIB_PATCH}

SRCS = HIDGameController.m			\
       HIDGameControllerAxis.m			\
       HIDGameControllerButton.m		\
       HIDGameControllerDirectionalPad.m	\
       HIDGameControllerElement.m		\
       HIDGameControllerMapping.m

INCLUDES := ${SRCS:.m=.h}	\
	    ObjFWHID.h

SRCS += HIDGameControllerEmulatedAxis.m		\
	HIDGameControllerEmulatedButton.m

Modified src/test/OTAppDelegate.m from [1cc4b85cfa] to [e739358c29].

28
29
30
31
32
33
34

35
36
37
38
39
40
41
#import "OFThread.h"
#import "OFValue.h"

#import "OTTestCase.h"

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

#import "OTAssertionFailedException.h"
#import "OTTestSkippedException.h"

#ifdef OF_IOS
# include <CoreFoundation/CoreFoundation.h>
#endif
283
284
285
286
287
288
289

290
291
292
293
294
295
296
297
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			void *pool = objc_autoreleasePoolPush();
			HIDGameController *controller =
			    [[HIDGameController controllers] objectAtIndex: 0];
			HIDGameControllerButton *button =

			    [controller.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
299
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			void *pool = objc_autoreleasePoolPush();
			HIDGameController *controller =
			    [[HIDGameController controllers] objectAtIndex: 0];
			HIDGameControllerButton *button =
			    [controller.unmappedMapping.buttons
			    objectForKey: @"A"];

			[controller retrieveState];

			if (button.pressed)
				break;

			[OFThread waitForVerticalBlank];
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		HIDGameController *controller =
		    [[HIDGameController controllers] objectAtIndex: 0];
		HIDGameControllerButton *button =
# ifdef OF_WII
		    [controller.buttons objectForKey: @"Home"];
# else
		    [controller.buttons objectForKey: @"Start"];
# endif

		[controller retrieveState];

		if (button.pressed)
			break;








|

|







549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		HIDGameController *controller =
		    [[HIDGameController controllers] objectAtIndex: 0];
		HIDGameControllerButton *button =
# ifdef OF_WII
		    [controller.unmappedMapping.buttons objectForKey: @"Home"];
# else
		    [controller.unmappedMapping.buttons objectForKey: @"Start"];
# endif

		[controller retrieveState];

		if (button.pressed)
			break;

Modified tests/gamecontroller/GameControllerTests.m from [b2c5ea408f] to [563268e4f8].

27
28
29
30
31
32
33

34
35
36
37
38
39
40
#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;







>







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

#import "OFReadFailedException.h"

#if defined(OF_NINTENDO_DS)
static size_t buttonsPerLine = 2;
#elif defined(OF_NINTENDO_3DS)
static size_t buttonsPerLine = 3;
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

			[OFStdOut clear];
		}

		[OFStdOut setCursorPosition: OFMakePoint(0, 0)];

		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) {
				HIDGameControllerButton *button =
				    [controller.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 (HIDGameController *controller in _controllers) {
			HIDGameControllerMapping *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) {
				HIDGameControllerButton *button =
				    [mapping.buttons objectForKey: name];

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

				if (button.value == 1)
					[OFStdOut setForegroundColor:
					    [OFColor red]];
125
126
127
128
129
130
131

132
133
134



135
136
137





138
139
140
141
142
143
144
					i = 0;
				} else
					[OFStdOut writeString: @" "];
			}
			[OFStdOut setForegroundColor: [OFColor gray]];
			[OFStdOut writeString: @"\n"];


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




				[OFStdOut writeFormat: @"%@: %5.2f ",
						       name, axis.value];





			}

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

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFThread waitForVerticalBlank];







>


|
>
>
>



>
>
>
>
>







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
					i = 0;
				} else
					[OFStdOut writeString: @" "];
			}
			[OFStdOut setForegroundColor: [OFColor gray]];
			[OFStdOut writeString: @"\n"];

			i = 0;
			for (OFString *name in axes) {
				HIDGameControllerAxis *axis =
				    [mapping.axes objectForKey: name];

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

				[OFStdOut writeFormat: @"%@: %5.2f ",
						       name, axis.value];

				if (++i == buttonsPerLine) {
					i = 0;
				} else
					[OFStdOut writeString: @" "];
			}

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

#if defined(OF_WII) || defined(OF_NINTENDO_DS) || defined(OF_NINTENDO_3DS)
		[OFThread waitForVerticalBlank];