ObjFW  Diff

Differences From Artifact [d527831ab5]:

To Artifact [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