git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

commit 354f68a8617cdfe70c2f65a13ab57eaf45360e64
parent 6547137389dc21deff518d6c8e3f370b80b87ee0
Author: Lior Halphon <LIJI32@gmail.com>
Date:   Fri, 28 Oct 2022 20:15:24 +0300

Fix uniqueIDs not being unique in combined controllers

Diffstat:
MJoyKit/JOYAxes2D.h5++---
MJoyKit/JOYAxes2D.m5++---
MJoyKit/JOYAxes3D.h5++---
MJoyKit/JOYAxes3D.m4++--
MJoyKit/JOYAxis.h5++---
MJoyKit/JOYAxis.m4++--
MJoyKit/JOYButton.h5++---
MJoyKit/JOYButton.m4++--
MJoyKit/JOYController.h1+
MJoyKit/JOYController.m23+++++++++++++++++++++++
MJoyKit/JOYEmulatedButton.m2+-
MJoyKit/JOYHat.h4++--
MJoyKit/JOYHat.m11++++++++---
AJoyKit/JOYInput.h8++++++++
AJoyKit/JOYInput.m21+++++++++++++++++++++
15 files changed, 80 insertions(+), 27 deletions(-)

diff --git a/JoyKit/JOYAxes2D.h b/JoyKit/JOYAxes2D.h @@ -1,4 +1,5 @@ #import <Foundation/Foundation.h> +#import "JOYInput.h" typedef enum { JOYAxes2DUsageNone, @@ -11,10 +12,8 @@ typedef enum { JOYAxes2DUsageGeneric0 = 0x10000, } JOYAxes2DUsage; -@interface JOYAxes2D : NSObject -- (NSString *)usageString; +@interface JOYAxes2D : JOYInput + (NSString *)usageToString: (JOYAxes2DUsage) usage; -- (uint64_t)uniqueID; - (double)distance; - (double)angle; - (NSPoint)value; diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m @@ -8,7 +8,6 @@ int32_t initialX, initialY; int32_t minX, minY; int32_t maxX, maxY; - } + (NSString *)usageToString: (JOYAxes2DUsage) usage @@ -36,12 +35,12 @@ - (uint64_t)uniqueID { - return _element1.uniqueID; + return _element1.uniqueID | (uint64_t)self.combinedIndex << 32; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %.2f%%, %.2f degrees>", self.className, self, self.usageString, self.uniqueID, self.distance * 100, self.angle]; + return [NSString stringWithFormat:@"<%@: %p, %@ (%llx); State: %.2f%%, %.2f degrees>", self.className, self, self.usageString, self.uniqueID, self.distance * 100, self.angle]; } - (instancetype)initWithFirstElement:(JOYElement *)element1 secondElement:(JOYElement *)element2 diff --git a/JoyKit/JOYAxes3D.h b/JoyKit/JOYAxes3D.h @@ -1,4 +1,5 @@ #import <Foundation/Foundation.h> +#import "JOYInput.h" typedef enum { JOYAxes3DUsageNone, @@ -14,10 +15,8 @@ typedef struct { double x, y, z; } JOYPoint3D; -@interface JOYAxes3D : NSObject -- (NSString *)usageString; +@interface JOYAxes3D : JOYInput + (NSString *)usageToString: (JOYAxes3DUsage) usage; -- (uint64_t)uniqueID; - (JOYPoint3D)rawValue; - (JOYPoint3D)normalizedValue; // For orientation - (JOYPoint3D)gUnitsValue; // For acceleration diff --git a/JoyKit/JOYAxes3D.m b/JoyKit/JOYAxes3D.m @@ -34,12 +34,12 @@ - (uint64_t)uniqueID { - return _element1.uniqueID; + return _element1.uniqueID | (uint64_t)self.combinedIndex << 32; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: (%.2f, %.2f, %.2f)>", self.className, self, self.usageString, self.uniqueID, _state1, _state2, _state3]; + return [NSString stringWithFormat:@"<%@: %p, %@ (%llx); State: (%.2f, %.2f, %.2f)>", self.className, self, self.usageString, self.uniqueID, _state1, _state2, _state3]; } - (instancetype)initWithFirstElement:(JOYElement *)element1 secondElement:(JOYElement *)element2 thirdElement:(JOYElement *)element3 diff --git a/JoyKit/JOYAxis.h b/JoyKit/JOYAxis.h @@ -1,5 +1,6 @@ #import <Foundation/Foundation.h> #import "JOYButton.h" +#import "JOYInput.h" typedef enum { JOYAxisUsageNone, @@ -24,10 +25,8 @@ typedef enum { JOYAxisUsageGeneric0 = 0x10000, } JOYAxisUsage; -@interface JOYAxis : NSObject -- (NSString *)usageString; +@interface JOYAxis : JOYInput + (NSString *)usageToString: (JOYAxisUsage) usage; -- (uint64_t)uniqueID; - (double)value; - (JOYButtonUsage)equivalentButtonUsage; @property JOYAxisUsage usage; diff --git a/JoyKit/JOYAxis.m b/JoyKit/JOYAxis.m @@ -42,12 +42,12 @@ - (uint64_t)uniqueID { - return _element.uniqueID; + return _element.uniqueID | (uint64_t)self.combinedIndex << 32; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %f%%>", self.className, self, self.usageString, self.uniqueID, _state * 100]; + return [NSString stringWithFormat:@"<%@: %p, %@ (%llx); State: %f%%>", self.className, self, self.usageString, self.uniqueID, _state * 100]; } - (instancetype)initWithElement:(JOYElement *)element diff --git a/JoyKit/JOYButton.h b/JoyKit/JOYButton.h @@ -1,4 +1,5 @@ #import <Foundation/Foundation.h> +#import "JOYInput.h" typedef enum { JOYButtonUsageNone, @@ -46,10 +47,8 @@ typedef enum { JOYButtonTypeHatEmulated, } JOYButtonType; -@interface JOYButton : NSObject -- (NSString *)usageString; +@interface JOYButton : JOYInput + (NSString *)usageToString: (JOYButtonUsage) usage; -- (uint64_t)uniqueID; - (bool) isPressed; @property JOYButtonUsage usage; @property (readonly) JOYButtonType type; diff --git a/JoyKit/JOYButton.m b/JoyKit/JOYButton.m @@ -51,12 +51,12 @@ - (uint64_t)uniqueID { - return _element.uniqueID; + return _element.uniqueID | (uint64_t)self.combinedIndex << 32; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %s>", self.className, self, self.usageString, self.uniqueID, _state? "Presssed" : "Released"]; + return [NSString stringWithFormat:@"<%@: %p, %@ (%llx); State: %s>", self.className, self, self.usageString, self.uniqueID, _state? "Presssed" : "Released"]; } - (instancetype)initWithElement:(JOYElement *)element diff --git a/JoyKit/JOYController.h b/JoyKit/JOYController.h @@ -42,6 +42,7 @@ typedef enum { - (NSArray<JOYAxes2D *> *) axes2D; - (NSArray<JOYAxes3D *> *) axes3D; - (NSArray<JOYHat *> *) hats; +- (NSArray<JOYInput *> *) allInputs; - (void)setRumbleAmplitude:(double)amp; - (void)setPlayerLEDs:(uint8_t)mask; - (uint8_t)LEDMaskForPlayer:(unsigned)player; diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m @@ -87,6 +87,10 @@ static bool hatsEmulateButtons = false; - (bool)updateState; @end +@interface JOYInput () +@property unsigned combinedIndex; +@end + static NSDictionary *CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage) { return @{ @@ -1099,6 +1103,17 @@ typedef union { return _logicallyConnected && _physicallyConnected; } +- (NSArray<JOYInput *> *)allInputs +{ + NSMutableArray<JOYInput *> *ret = [NSMutableArray array]; + [ret addObjectsFromArray:self.buttons]; + [ret addObjectsFromArray:self.axes]; + [ret addObjectsFromArray:self.axes2D]; + [ret addObjectsFromArray:self.axes3D]; + [ret addObjectsFromArray:self.hats]; + return ret; +} + + (void)controllerAdded:(IOHIDDeviceRef) device { NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); @@ -1213,6 +1228,7 @@ typedef union { } } + unsigned index = 0; for (JOYController *child in _chidlren) { for (id<JOYListener> listener in listeners) { if ([listener respondsToSelector:@selector(controllerDisconnected:)]) { @@ -1220,6 +1236,10 @@ typedef union { } } child->_parent = self; + for (JOYInput *input in child.allInputs) { + input.combinedIndex = index; + } + index++; [exposedControllers removeObject:child]; } @@ -1249,6 +1269,9 @@ typedef union { for (JOYController *child in _chidlren) { child->_parent = nil; + for (JOYInput *input in child.allInputs) { + input.combinedIndex = 0; + } [exposedControllers addObject:child]; for (id<JOYListener> listener in listeners) { if ([listener respondsToSelector:@selector(controllerConnected:)]) { diff --git a/JoyKit/JOYEmulatedButton.m b/JoyKit/JOYEmulatedButton.m @@ -25,7 +25,7 @@ - (uint64_t)uniqueID { - return _uniqueID; + return _uniqueID | (uint64_t)self.combinedIndex << 32; } - (bool)updateStateFromAxis:(JOYAxis *)axis diff --git a/JoyKit/JOYHat.h b/JoyKit/JOYHat.h @@ -1,7 +1,7 @@ #import <Foundation/Foundation.h> +#import "JOYInput.h" -@interface JOYHat : NSObject -- (uint64_t)uniqueID; +@interface JOYHat : JOYInput - (double)angle; - (unsigned)resolution; @property (readonly, getter=isPressed) bool pressed; diff --git a/JoyKit/JOYHat.m b/JoyKit/JOYHat.m @@ -10,15 +10,15 @@ - (uint64_t)uniqueID { - return _element.uniqueID; + return _element.uniqueID | (uint64_t)self.combinedIndex << 32; } - (NSString *)description { if (self.isPressed) { - return [NSString stringWithFormat:@"<%@: %p (%llu); State: %f degrees>", self.className, self, self.uniqueID, self.angle]; + return [NSString stringWithFormat:@"<%@: %p (%llx); State: %f degrees>", self.className, self, self.uniqueID, self.angle]; } - return [NSString stringWithFormat:@"<%@: %p (%llu); State: released>", self.className, self, self.uniqueID]; + return [NSString stringWithFormat:@"<%@: %p (%llx); State: released>", self.className, self, self.uniqueID]; } @@ -59,4 +59,9 @@ return false; } +- (NSString *)usageString +{ + return @"Hat switch"; +} + @end diff --git a/JoyKit/JOYInput.h b/JoyKit/JOYInput.h @@ -0,0 +1,8 @@ +#import <Foundation/Foundation.h> + +@interface JOYInput : NSObject +@property (readonly) unsigned combinedIndex; +- (NSString *)usageString; +- (uint64_t)uniqueID; +@end + diff --git a/JoyKit/JOYInput.m b/JoyKit/JOYInput.m @@ -0,0 +1,21 @@ +#import "JOYInput.h" + +@interface JOYInput () +@property unsigned combinedIndex; +@end + +@implementation JOYInput + +- (uint64_t)uniqueID +{ + [self doesNotRecognizeSelector:_cmd]; + __builtin_unreachable(); +} + +- (NSString *)usageString +{ + [self doesNotRecognizeSelector:_cmd]; + __builtin_unreachable(); +} + +@end

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.