Index: src/hid/Makefile ================================================================== --- src/hid/Makefile +++ src/hid/Makefile @@ -32,11 +32,13 @@ SRCS_NINTENDO_DS = OHNintendoDSGamepad.m \ OHNintendoDSGameController.m SRCS_NINTENDO_SWITCH = OHNintendoSwitchExtendedGamepad.m \ OHNintendoSwitchGameController.m SRCS_WII = OHWiiClassicController.m \ - OHWiiGameController.m + OHWiiGameController.m \ + OHWiimote.m \ + OHWiimoteWithNunchuk.m SRCS_XINPUT = OHXInputExtendedGamepad.m \ OHXInputGameController.m INCLUDES := ${SRCS:.m=.h} \ OHExtendedGamepad.h \ Index: src/hid/OHWiiGameController.m ================================================================== --- src/hid/OHWiiGameController.m +++ src/hid/OHWiiGameController.m @@ -23,39 +23,21 @@ #import "OFArray.h" #import "OFDictionary.h" #import "OHGameControllerButton.h" #import "OHGameControllerDirectionalPad.h" #import "OHWiiClassicController.h" +#import "OHWiimote.h" +#import "OHWiimoteWithNunchuk.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFReadFailedException.h" #define asm __asm__ #include #undef asm -@interface OHWiiGameControllerProfile: OFObject -{ - OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons; - OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *) - *_directionalPads; -} - -- (instancetype)initWithType: (uint32_t)type; -@end - -static OFString *const buttonNames[] = { - @"A", @"B", @"1", @"2", @"+", @"-", @"Home" -}; -static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames); -static OFString *const nunchukButtonNames[] = { - @"C", @"Z" -}; -static const size_t numNunchukButtons = - sizeof(nunchukButtonNames) / sizeof(*nunchukButtonNames); - static float scale(float value, float min, float max, float center) { if (value < min) value = min; @@ -112,13 +94,14 @@ _index = index; _type = type; if (type == WPAD_EXP_CLASSIC) _rawProfile = [[OHWiiClassicController alloc] init]; + else if (type == WPAD_EXP_NUNCHUK) + _rawProfile = [[OHWiimoteWithNunchuk alloc] init]; else - _rawProfile = [[OHWiiGameControllerProfile alloc] - initWithType: type]; + _rawProfile = [[OHWiimote alloc] init]; [self retrieveState]; } @catch (id e) { [self release]; @throw e; @@ -268,103 +251,6 @@ if (_type == WPAD_EXP_CLASSIC) return (id )_rawProfile; return nil; } -@end - -@implementation OHWiiGameControllerProfile -@synthesize buttons = _buttons, directionalPads = _directionalPads; - -- (instancetype)initWithType: (uint32_t)type -{ - self = [super init]; - - @try { - void *pool = objc_autoreleasePoolPush(); - OFMutableDictionary *buttons; - OFMutableDictionary *directionalPads; - OHGameControllerDirectionalPad *directionalPad; - OHGameControllerButton *up, *down, *left, *right; - OHGameControllerAxis *xAxis, *yAxis; - - if (type != WPAD_EXP_NONE && type != WPAD_EXP_NUNCHUK) - @throw [OFInvalidArgumentException exception]; - - buttons = [OFMutableDictionary - dictionaryWithCapacity: numButtons + numNunchukButtons]; - - for (size_t i = 0; i < numButtons; i++) { - OHGameControllerButton *button = - [[[OHGameControllerButton alloc] - initWithName: buttonNames[i]] autorelease]; - [buttons setObject: button forKey: buttonNames[i]]; - } - - directionalPads = [OFMutableDictionary dictionary]; - - up = [[[OHGameControllerButton alloc] - initWithName: @"D-Pad Up"] autorelease]; - down = [[[OHGameControllerButton alloc] - initWithName: @"D-Pad Down"] autorelease]; - left = [[[OHGameControllerButton alloc] - initWithName: @"D-Pad Left"] autorelease]; - right = [[[OHGameControllerButton alloc] - initWithName: @"D-Pad Right"] autorelease]; - directionalPad = [[[OHGameControllerDirectionalPad alloc] - initWithName: @"D-Pad" - up: up - down: down - left: left - right: right] autorelease]; - [directionalPads setObject: directionalPad forKey: @"D-Pad"]; - - if (type == WPAD_EXP_NUNCHUK) { - for (size_t i = 0; i < numNunchukButtons; i++) { - OHGameControllerButton *button = - [[[OHGameControllerButton alloc] - initWithName: nunchukButtonNames[i]] - autorelease]; - - [buttons setObject: button - forKey: nunchukButtonNames[i]]; - } - - xAxis = [[[OHGameControllerAxis alloc] - initWithName: @"X"] autorelease]; - yAxis = [[[OHGameControllerAxis alloc] - initWithName: @"Y"] autorelease]; - directionalPad = [[[OHGameControllerDirectionalPad alloc] - initWithName: @"Analog Stick" - xAxis: xAxis - yAxis: yAxis] autorelease]; - [directionalPads setObject: directionalPad - forKey: @"Analog Stick"]; - } - - [buttons makeImmutable]; - [directionalPads makeImmutable]; - _buttons = [buttons retain]; - _directionalPads = [directionalPads retain]; - - objc_autoreleasePoolPop(pool); - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (void)dealloc -{ - [_buttons release]; - [_directionalPads release]; - - [super dealloc]; -} - -- (OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *)axes -{ - return [OFDictionary dictionary]; -} @end ADDED src/hid/OHWiimote.h Index: src/hid/OHWiimote.h ================================================================== --- /dev/null +++ src/hid/OHWiimote.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * 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 + * . + */ + +#import "OHGameControllerProfile.h" + +OF_ASSUME_NONNULL_BEGIN + +@interface OHWiimote: OFObject +{ + OFDictionary OF_GENERIC(OFString *, OHGameControllerButton *) *_buttons; + OFDictionary OF_GENERIC(OFString *, OHGameControllerDirectionalPad *) + *_directionalPads; +} +@end + +OF_ASSUME_NONNULL_END ADDED src/hid/OHWiimote.m Index: src/hid/OHWiimote.m ================================================================== --- /dev/null +++ src/hid/OHWiimote.m @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * 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 + * . + */ + +#include "config.h" + +#import "OHWiimote.h" +#import "OFDictionary.h" +#import "OHGameControllerAxis.h" +#import "OHGameControllerButton.h" +#import "OHGameControllerDirectionalPad.h" + +static OFString *const buttonNames[] = { + @"A", @"B", @"1", @"2", @"+", @"-", @"Home" +}; +static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames); + +@implementation OHWiimote +@synthesize buttons = _buttons, directionalPads = _directionalPads; + +- (instancetype)init +{ + self = [super init]; + + @try { + void *pool = objc_autoreleasePoolPush(); + OFMutableDictionary *buttons; + OHGameControllerDirectionalPad *dPad; + OHGameControllerButton *up, *down, *left, *right; + + buttons = + [OFMutableDictionary dictionaryWithCapacity: numButtons]; + for (size_t i = 0; i < numButtons; i++) { + OHGameControllerButton *button = + [[[OHGameControllerButton alloc] + initWithName: buttonNames[i]] autorelease]; + [buttons setObject: button forKey: buttonNames[i]]; + } + [buttons makeImmutable]; + _buttons = [buttons retain]; + + up = [[[OHGameControllerButton alloc] + initWithName: @"D-Pad Up"] autorelease]; + down = [[[OHGameControllerButton alloc] + initWithName: @"D-Pad Down"] autorelease]; + left = [[[OHGameControllerButton alloc] + initWithName: @"D-Pad Left"] autorelease]; + right = [[[OHGameControllerButton alloc] + initWithName: @"D-Pad Right"] autorelease]; + dPad = [[[OHGameControllerDirectionalPad alloc] + initWithName: @"D-Pad" + up: up + down: down + left: left + right: right] autorelease]; + + _directionalPads = [[OFDictionary alloc] + initWithObject: dPad + forKey: @"D-Pad"]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_buttons release]; + [_directionalPads release]; + + [super dealloc]; +} + +- (OFDictionary OF_GENERIC(OFString *, OHGameControllerAxis *) *)axes +{ + return [OFDictionary dictionary]; +} +@end ADDED src/hid/OHWiimoteWithNunchuk.h Index: src/hid/OHWiimoteWithNunchuk.h ================================================================== --- /dev/null +++ src/hid/OHWiimoteWithNunchuk.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * 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 + * . + */ + +#import "OHWiimote.h" + +OF_ASSUME_NONNULL_BEGIN + +@interface OHWiimoteWithNunchuk: OHWiimote +@end + +OF_ASSUME_NONNULL_END ADDED src/hid/OHWiimoteWithNunchuk.m Index: src/hid/OHWiimoteWithNunchuk.m ================================================================== --- /dev/null +++ src/hid/OHWiimoteWithNunchuk.m @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * 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 + * . + */ + +#include "config.h" + +#import "OHWiimoteWithNunchuk.h" +#import "OFDictionary.h" +#import "OHGameControllerAxis.h" +#import "OHGameControllerButton.h" +#import "OHGameControllerDirectionalPad.h" + +static OFString *const buttonNames[] = { + @"C", @"Z" +}; +static const size_t numButtons = sizeof(buttonNames) / sizeof(*buttonNames); + +@implementation OHWiimoteWithNunchuk +- (instancetype)init +{ + self = [super init]; + + @try { + void *pool = objc_autoreleasePoolPush(); + OFMutableDictionary *buttons = + [[_buttons mutableCopy] autorelease]; + OFMutableDictionary *directionalPads = + [[_directionalPads mutableCopy] autorelease]; + OHGameControllerAxis *xAxis, *yAxis; + OHGameControllerDirectionalPad *directionalPad; + + for (size_t i = 0; i < numButtons; i++) { + OHGameControllerButton *button = + [[[OHGameControllerButton alloc] + initWithName: buttonNames[i]] autorelease]; + + [buttons setObject: button forKey: buttonNames[i]]; + } + + xAxis = [[[OHGameControllerAxis alloc] + initWithName: @"X"] autorelease]; + yAxis = [[[OHGameControllerAxis alloc] + initWithName: @"Y"] autorelease]; + directionalPad = [[[OHGameControllerDirectionalPad alloc] + initWithName: @"Analog Stick" + xAxis: xAxis + yAxis: yAxis] autorelease]; + [directionalPads setObject: directionalPad + forKey: @"Analog Stick"]; + + [buttons makeImmutable]; + [_buttons release]; + _buttons = [buttons retain]; + + [directionalPads makeImmutable]; + [_directionalPads release]; + _directionalPads = [directionalPads retain]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} +@end