├── .gitignore ├── CCController ├── .DS_Store ├── CCController.h └── CCController.m ├── ControllerTest ├── Info.plist └── main.m ├── README.md └── ControllerTest.xcodeproj └── project.pbxproj /.gitignore: -------------------------------------------------------------------------------- 1 | project.xcworkspace 2 | xcuserdata 3 | -------------------------------------------------------------------------------- /CCController/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slembcke/CCController/HEAD/CCController/.DS_Store -------------------------------------------------------------------------------- /CCController/CCController.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | 4 | @interface CCController : GCController 5 | @end 6 | 7 | 8 | @interface GCExtendedGamepad(SnapshotDataFast) 9 | 10 | // Get a snapshot data instance directly from a gamepad without needing to make a snapshot object. 11 | // Also a necessary workaround for a *massive* memory leak in -saveSnapshot in 10.9. 12 | -(NSData *)snapshotDataFast; 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /ControllerTest/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | com.howlingmoonsoftware.$(PRODUCT_NAME:rfc1034identifier) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | $(MACOSX_DEPLOYMENT_TARGET) 27 | NSHumanReadableCopyright 28 | Copyright © 2015 Scott Lembcke. All rights reserved. 29 | NSMainStoryboardFile 30 | Main 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | IMPORTANT! It seems there is a fatal bug in [GCControllerSnapshot snapshotData] that prevents them from being used on OS X 10.11. Until Apple fixes it, this library can only be used on 10.9 or 10.10. 2 | 3 | # CCController 4 | 5 | I rather like Apple's GameController.framework API. It's clean, simple, and fairly complete, and (mildly) cross-platform. Unfortunately it's nearly useless on the Mac since nobody really has controllers to use with it. Most computer players use their console controllers on their computer, and using gamepad snapshots it's easy to trick GameController.framework into supporting them. 6 | 7 | * PS4 controllers work out of the box using both USB and bluetooth. 8 | * 360 wired controllers work if you install a driver for them. Wireless controllers also work if you have the Microsoft USB reciever dongle. 9 | * I've been told Xbox One and PS3 controllers work as well. 10 | 11 | ## Use: 12 | 13 | Instead of calling `[GCController controllers]` to get the list of controllers, call `[CCController controllers]` instead. That's all! Everything else should "just work" as expected. 14 | 15 | ## More Controllers! 16 | 17 | Right now the mappings are hard coded specifically for Xbox/PS controllers. I'd like to make it more configurable using a plist file though. Feral Interactive gave me permission to use their controller mapping files. @geowar1 also pointed me to some existing data. It's mostly a matter of time (or pull a request?). 18 | 19 | ## License 20 | 21 | Licensed under the MIT license: 22 | 23 | Copyright (c) 2015 Scott Lembcke and Howling Moon Software 24 | 25 | Permission is hereby granted, free of charge, to any person obtaining a copy 26 | of this software and associated documentation files (the "Software"), to deal 27 | in the Software without restriction, including without limitation the rights 28 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 29 | copies of the Software, and to permit persons to whom the Software is 30 | furnished to do so, subject to the following conditions: 31 | 32 | The above copyright notice and this permission notice shall be included in 33 | all copies or substantial portions of the Software. 34 | 35 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 36 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 37 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 38 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 39 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 40 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 41 | SOFTWARE. 42 | -------------------------------------------------------------------------------- /ControllerTest/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ControllerTest 4 | // 5 | // Created by Scott Lembcke on 1/30/15. 6 | // Copyright (c) 2015 Scott Lembcke. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CCController.h" 11 | 12 | static void 13 | ActivateController(GCController *controller) 14 | { 15 | NSLog(@"Activating controller: %@", controller); 16 | NSLog(@" VendorName: %@", controller.vendorName); 17 | 18 | // controller.playerIndex = 0; 19 | 20 | controller.controllerPausedHandler = ^(GCController *controller){ 21 | NSLog(@"Pause button."); 22 | }; 23 | 24 | // controller.gamepad.valueChangedHandler = ^(GCGamepad *gamepad, GCControllerElement *element){ 25 | // NSLog(@"DPad: (% .1f, % .1f), RS: %d, LS: %d, A: %d, B: %d, X: %d, Y: %d", 26 | // gamepad.dpad.xAxis.value, gamepad.dpad.yAxis.value, 27 | // gamepad.leftShoulder.pressed, gamepad.rightShoulder.pressed, 28 | // gamepad.buttonA.pressed, gamepad.buttonB.pressed, gamepad.buttonX.pressed, gamepad.buttonY.pressed 29 | // ); 30 | // }; 31 | 32 | controller.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element){ 33 | NSLog(@"L: (% .1f, % .1f), R: (% .1f, % .1f), DPad: (% .1f, % .1f), LT: %.1f, RT: %.1f, RS: %d, LS: %d, A: %d, B: %d, X: %d, Y: %d", 34 | gamepad.leftThumbstick.xAxis.value, gamepad.leftThumbstick.yAxis.value, 35 | gamepad.rightThumbstick.xAxis.value, gamepad.rightThumbstick.yAxis.value, 36 | gamepad.dpad.xAxis.value, gamepad.dpad.yAxis.value, 37 | gamepad.leftTrigger.value, gamepad.rightTrigger.value, 38 | gamepad.leftShoulder.pressed, gamepad.rightShoulder.pressed, 39 | gamepad.buttonA.pressed, gamepad.buttonB.pressed, gamepad.buttonX.pressed, gamepad.buttonY.pressed 40 | ); 41 | }; 42 | 43 | __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidDisconnectNotification object:controller queue:nil usingBlock:^(NSNotification *notification){ 44 | NSLog(@"Deactivating controller: %@", notification.object); 45 | [[NSNotificationCenter defaultCenter] removeObserver:observer]; 46 | }]; 47 | } 48 | 49 | int main(int argc, const char * argv[]) { 50 | @autoreleasepool { 51 | NSArray *controllers = [CCController controllers]; 52 | NSLog(@"%d controllers found.", (int)controllers.count); 53 | 54 | for(CCController *controller in controllers){ 55 | ActivateController(controller); 56 | } 57 | 58 | [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidConnectNotification object:nil queue:nil 59 | usingBlock:^(NSNotification *notification){ 60 | ActivateController(notification.object); 61 | } 62 | ]; 63 | } 64 | 65 | CFRunLoopRun(); 66 | } 67 | -------------------------------------------------------------------------------- /ControllerTest.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | D355DB1F1A7C632D009B4ED1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D355DB1E1A7C632D009B4ED1 /* main.m */; }; 11 | D3756A931A7C6F3F00A66605 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D3756A921A7C6F3F00A66605 /* IOKit.framework */; }; 12 | D3EDFBC91A7CB535005615F4 /* CCController.m in Sources */ = {isa = PBXBuildFile; fileRef = D3EDFBC81A7CB535005615F4 /* CCController.m */; }; 13 | D3EDFBCB1A7CB8BE005615F4 /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D3EDFBCA1A7CB8BE005615F4 /* GameController.framework */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXFileReference section */ 17 | D355DB161A7C632D009B4ED1 /* ControllerTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ControllerTest.app; sourceTree = BUILT_PRODUCTS_DIR; }; 18 | D355DB1A1A7C632D009B4ED1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 19 | D355DB1E1A7C632D009B4ED1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 20 | D3756A921A7C6F3F00A66605 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; }; 21 | D3EDFBBE1A7CB29C005615F4 /* CCController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCController.h; sourceTree = ""; }; 22 | D3EDFBC81A7CB535005615F4 /* CCController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCController.m; sourceTree = ""; }; 23 | D3EDFBCA1A7CB8BE005615F4 /* GameController.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameController.framework; path = System/Library/Frameworks/GameController.framework; sourceTree = SDKROOT; }; 24 | /* End PBXFileReference section */ 25 | 26 | /* Begin PBXFrameworksBuildPhase section */ 27 | D355DB131A7C632D009B4ED1 /* Frameworks */ = { 28 | isa = PBXFrameworksBuildPhase; 29 | buildActionMask = 2147483647; 30 | files = ( 31 | D3EDFBCB1A7CB8BE005615F4 /* GameController.framework in Frameworks */, 32 | D3756A931A7C6F3F00A66605 /* IOKit.framework in Frameworks */, 33 | ); 34 | runOnlyForDeploymentPostprocessing = 0; 35 | }; 36 | /* End PBXFrameworksBuildPhase section */ 37 | 38 | /* Begin PBXGroup section */ 39 | D355DB0D1A7C632D009B4ED1 = { 40 | isa = PBXGroup; 41 | children = ( 42 | D3EDFBCA1A7CB8BE005615F4 /* GameController.framework */, 43 | D3756A921A7C6F3F00A66605 /* IOKit.framework */, 44 | D355DB181A7C632D009B4ED1 /* ControllerTest */, 45 | D3EDFBBD1A7CB29C005615F4 /* CCController */, 46 | D355DB171A7C632D009B4ED1 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | D355DB171A7C632D009B4ED1 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | D355DB161A7C632D009B4ED1 /* ControllerTest.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | D355DB181A7C632D009B4ED1 /* ControllerTest */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | D355DB191A7C632D009B4ED1 /* Supporting Files */, 62 | ); 63 | path = ControllerTest; 64 | sourceTree = ""; 65 | }; 66 | D355DB191A7C632D009B4ED1 /* Supporting Files */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | D355DB1A1A7C632D009B4ED1 /* Info.plist */, 70 | D355DB1E1A7C632D009B4ED1 /* main.m */, 71 | ); 72 | name = "Supporting Files"; 73 | sourceTree = ""; 74 | }; 75 | D3EDFBBD1A7CB29C005615F4 /* CCController */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | D3EDFBBE1A7CB29C005615F4 /* CCController.h */, 79 | D3EDFBC81A7CB535005615F4 /* CCController.m */, 80 | ); 81 | path = CCController; 82 | sourceTree = ""; 83 | }; 84 | /* End PBXGroup section */ 85 | 86 | /* Begin PBXNativeTarget section */ 87 | D355DB151A7C632D009B4ED1 /* ControllerTest */ = { 88 | isa = PBXNativeTarget; 89 | buildConfigurationList = D355DB361A7C632D009B4ED1 /* Build configuration list for PBXNativeTarget "ControllerTest" */; 90 | buildPhases = ( 91 | D355DB121A7C632D009B4ED1 /* Sources */, 92 | D355DB131A7C632D009B4ED1 /* Frameworks */, 93 | D355DB141A7C632D009B4ED1 /* Resources */, 94 | ); 95 | buildRules = ( 96 | ); 97 | dependencies = ( 98 | ); 99 | name = ControllerTest; 100 | productName = ControllerTest; 101 | productReference = D355DB161A7C632D009B4ED1 /* ControllerTest.app */; 102 | productType = "com.apple.product-type.application"; 103 | }; 104 | /* End PBXNativeTarget section */ 105 | 106 | /* Begin PBXProject section */ 107 | D355DB0E1A7C632D009B4ED1 /* Project object */ = { 108 | isa = PBXProject; 109 | attributes = { 110 | LastUpgradeCheck = 0610; 111 | ORGANIZATIONNAME = "Scott Lembcke"; 112 | TargetAttributes = { 113 | D355DB151A7C632D009B4ED1 = { 114 | CreatedOnToolsVersion = 6.1.1; 115 | }; 116 | }; 117 | }; 118 | buildConfigurationList = D355DB111A7C632D009B4ED1 /* Build configuration list for PBXProject "ControllerTest" */; 119 | compatibilityVersion = "Xcode 3.2"; 120 | developmentRegion = English; 121 | hasScannedForEncodings = 0; 122 | knownRegions = ( 123 | en, 124 | Base, 125 | ); 126 | mainGroup = D355DB0D1A7C632D009B4ED1; 127 | productRefGroup = D355DB171A7C632D009B4ED1 /* Products */; 128 | projectDirPath = ""; 129 | projectRoot = ""; 130 | targets = ( 131 | D355DB151A7C632D009B4ED1 /* ControllerTest */, 132 | ); 133 | }; 134 | /* End PBXProject section */ 135 | 136 | /* Begin PBXResourcesBuildPhase section */ 137 | D355DB141A7C632D009B4ED1 /* Resources */ = { 138 | isa = PBXResourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXResourcesBuildPhase section */ 145 | 146 | /* Begin PBXSourcesBuildPhase section */ 147 | D355DB121A7C632D009B4ED1 /* Sources */ = { 148 | isa = PBXSourcesBuildPhase; 149 | buildActionMask = 2147483647; 150 | files = ( 151 | D355DB1F1A7C632D009B4ED1 /* main.m in Sources */, 152 | D3EDFBC91A7CB535005615F4 /* CCController.m in Sources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXSourcesBuildPhase section */ 157 | 158 | /* Begin XCBuildConfiguration section */ 159 | D355DB341A7C632D009B4ED1 /* Debug */ = { 160 | isa = XCBuildConfiguration; 161 | buildSettings = { 162 | ALWAYS_SEARCH_USER_PATHS = NO; 163 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 164 | CLANG_CXX_LIBRARY = "libc++"; 165 | CLANG_ENABLE_MODULES = YES; 166 | CLANG_ENABLE_OBJC_ARC = YES; 167 | CLANG_WARN_BOOL_CONVERSION = YES; 168 | CLANG_WARN_CONSTANT_CONVERSION = YES; 169 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 170 | CLANG_WARN_EMPTY_BODY = YES; 171 | CLANG_WARN_ENUM_CONVERSION = YES; 172 | CLANG_WARN_INT_CONVERSION = YES; 173 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 174 | CLANG_WARN_UNREACHABLE_CODE = YES; 175 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 176 | CODE_SIGN_IDENTITY = "-"; 177 | COPY_PHASE_STRIP = NO; 178 | ENABLE_STRICT_OBJC_MSGSEND = YES; 179 | GCC_C_LANGUAGE_STANDARD = gnu99; 180 | GCC_DYNAMIC_NO_PIC = NO; 181 | GCC_OPTIMIZATION_LEVEL = 0; 182 | GCC_PREPROCESSOR_DEFINITIONS = ( 183 | "DEBUG=1", 184 | "$(inherited)", 185 | ); 186 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 187 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 188 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 189 | GCC_WARN_UNDECLARED_SELECTOR = YES; 190 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 191 | GCC_WARN_UNUSED_FUNCTION = YES; 192 | GCC_WARN_UNUSED_VARIABLE = YES; 193 | MACOSX_DEPLOYMENT_TARGET = 10.10; 194 | MTL_ENABLE_DEBUG_INFO = YES; 195 | ONLY_ACTIVE_ARCH = YES; 196 | SDKROOT = macosx; 197 | }; 198 | name = Debug; 199 | }; 200 | D355DB351A7C632D009B4ED1 /* Release */ = { 201 | isa = XCBuildConfiguration; 202 | buildSettings = { 203 | ALWAYS_SEARCH_USER_PATHS = NO; 204 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 205 | CLANG_CXX_LIBRARY = "libc++"; 206 | CLANG_ENABLE_MODULES = YES; 207 | CLANG_ENABLE_OBJC_ARC = YES; 208 | CLANG_WARN_BOOL_CONVERSION = YES; 209 | CLANG_WARN_CONSTANT_CONVERSION = YES; 210 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 211 | CLANG_WARN_EMPTY_BODY = YES; 212 | CLANG_WARN_ENUM_CONVERSION = YES; 213 | CLANG_WARN_INT_CONVERSION = YES; 214 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 215 | CLANG_WARN_UNREACHABLE_CODE = YES; 216 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 217 | CODE_SIGN_IDENTITY = "-"; 218 | COPY_PHASE_STRIP = YES; 219 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 220 | ENABLE_NS_ASSERTIONS = NO; 221 | ENABLE_STRICT_OBJC_MSGSEND = YES; 222 | GCC_C_LANGUAGE_STANDARD = gnu99; 223 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 224 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 225 | GCC_WARN_UNDECLARED_SELECTOR = YES; 226 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 227 | GCC_WARN_UNUSED_FUNCTION = YES; 228 | GCC_WARN_UNUSED_VARIABLE = YES; 229 | MACOSX_DEPLOYMENT_TARGET = 10.10; 230 | MTL_ENABLE_DEBUG_INFO = NO; 231 | SDKROOT = macosx; 232 | }; 233 | name = Release; 234 | }; 235 | D355DB371A7C632D009B4ED1 /* Debug */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 239 | COMBINE_HIDPI_IMAGES = YES; 240 | INFOPLIST_FILE = ControllerTest/Info.plist; 241 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; 242 | MACOSX_DEPLOYMENT_TARGET = 10.7; 243 | PRODUCT_NAME = "$(TARGET_NAME)"; 244 | }; 245 | name = Debug; 246 | }; 247 | D355DB381A7C632D009B4ED1 /* Release */ = { 248 | isa = XCBuildConfiguration; 249 | buildSettings = { 250 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 251 | COMBINE_HIDPI_IMAGES = YES; 252 | INFOPLIST_FILE = ControllerTest/Info.plist; 253 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; 254 | MACOSX_DEPLOYMENT_TARGET = 10.7; 255 | PRODUCT_NAME = "$(TARGET_NAME)"; 256 | }; 257 | name = Release; 258 | }; 259 | /* End XCBuildConfiguration section */ 260 | 261 | /* Begin XCConfigurationList section */ 262 | D355DB111A7C632D009B4ED1 /* Build configuration list for PBXProject "ControllerTest" */ = { 263 | isa = XCConfigurationList; 264 | buildConfigurations = ( 265 | D355DB341A7C632D009B4ED1 /* Debug */, 266 | D355DB351A7C632D009B4ED1 /* Release */, 267 | ); 268 | defaultConfigurationIsVisible = 0; 269 | defaultConfigurationName = Release; 270 | }; 271 | D355DB361A7C632D009B4ED1 /* Build configuration list for PBXNativeTarget "ControllerTest" */ = { 272 | isa = XCConfigurationList; 273 | buildConfigurations = ( 274 | D355DB371A7C632D009B4ED1 /* Debug */, 275 | D355DB381A7C632D009B4ED1 /* Release */, 276 | ); 277 | defaultConfigurationIsVisible = 0; 278 | defaultConfigurationName = Release; 279 | }; 280 | /* End XCConfigurationList section */ 281 | }; 282 | rootObject = D355DB0E1A7C632D009B4ED1 /* Project object */; 283 | } 284 | -------------------------------------------------------------------------------- /CCController/CCController.m: -------------------------------------------------------------------------------- 1 | #import "CCController.h" 2 | #import 3 | 4 | #include 5 | 6 | 7 | const float DeadZonePercent = 0.2f; 8 | 9 | 10 | @implementation CCController { 11 | GCExtendedGamepadSnapShotDataV100 _snapshot; 12 | 13 | // Gamepad ivars are lazy and are only created when requested. 14 | GCGamepadSnapshot *_gamepad; 15 | GCExtendedGamepadSnapshot *_extendedGamepad; 16 | 17 | CFIndex _lThumbXUsageID; 18 | CFIndex _lThumbYUsageID; 19 | CFIndex _rThumbXUsageID; 20 | CFIndex _rThumbYUsageID; 21 | CFIndex _lTriggerUsageID; 22 | CFIndex _rTriggerUsageID; 23 | 24 | BOOL _usesHatSwitch; 25 | CFIndex _dpadLUsageID; 26 | CFIndex _dpadRUsageID; 27 | CFIndex _dpadDUsageID; 28 | CFIndex _dpadUUsageID; 29 | 30 | CFIndex _buttonPauseUsageID; 31 | CFIndex _buttonAUsageID; 32 | CFIndex _buttonBUsageID; 33 | CFIndex _buttonXUsageID; 34 | CFIndex _buttonYUsageID; 35 | CFIndex _lShoulderUsageID; 36 | CFIndex _rShoulderUsageID; 37 | } 38 | 39 | @synthesize controllerPausedHandler = _controllerPausedHandler; 40 | @synthesize vendorName = _vendorName; 41 | @synthesize playerIndex = _playerIndex; 42 | 43 | static IOHIDManagerRef HID_MANAGER = NULL; 44 | static NSMutableArray *CONTROLLERS = nil; 45 | 46 | //MARK: Class methods 47 | 48 | +(void)initialize 49 | { 50 | if(self != [CCController class]) return; 51 | 52 | HID_MANAGER = IOHIDManagerCreate(kCFAllocatorDefault, 0); 53 | CONTROLLERS = [NSMutableArray array]; 54 | 55 | if (IOHIDManagerOpen(HID_MANAGER, kIOHIDOptionsTypeNone) != kIOReturnSuccess) { 56 | NSLog(@"Error initializing CCGameController"); 57 | return; 58 | } 59 | 60 | // Register to get callbacks when gamepads are connected. 61 | IOHIDManagerRegisterDeviceMatchingCallback(HID_MANAGER, ControllerConnected, NULL); 62 | IOHIDManagerSetDeviceMatchingMultiple(HID_MANAGER, (__bridge CFArrayRef)@[ 63 | @{@(kIOHIDDeviceUsagePageKey): @(kHIDPage_GenericDesktop), @(kIOHIDDeviceUsageKey): @(kHIDUsage_GD_GamePad)}, 64 | @{@(kIOHIDDeviceUsagePageKey): @(kHIDPage_GenericDesktop), @(kIOHIDDeviceUsageKey): @(kHIDUsage_GD_MultiAxisController)}, 65 | ]); 66 | 67 | // Pump the event loop to initially fill the [CCController +controllers] list. 68 | // Otherwise the list would be empty, immediately followed by didConnect events. 69 | // Not really a problem, but quite how the iOS API works. 70 | NSString *mode = @"CCControllerPollGamepads"; 71 | IOHIDManagerScheduleWithRunLoop(HID_MANAGER, CFRunLoopGetCurrent(), (__bridge CFStringRef)mode); 72 | 73 | while(CFRunLoopRunInMode((CFStringRef)mode, 0, TRUE) == kCFRunLoopRunHandledSource){} 74 | 75 | IOHIDManagerUnscheduleFromRunLoop(HID_MANAGER, CFRunLoopGetCurrent(), (__bridge CFStringRef)mode); 76 | 77 | // Schedule the HID manager normally to get callbacks during runtime. 78 | IOHIDManagerScheduleWithRunLoop(HID_MANAGER, CFRunLoopGetMain(), kCFRunLoopDefaultMode); 79 | 80 | NSLog(@"CCController initialized."); 81 | } 82 | 83 | + (NSArray *)controllers; 84 | { 85 | 86 | return [[super controllers] arrayByAddingObjectsFromArray:CONTROLLERS]; 87 | } 88 | 89 | //MARK: Lifecycle 90 | 91 | -(instancetype)initWithDevice:(IOHIDDeviceRef)device 92 | { 93 | if((self = [super init])){ 94 | NSString *manufacturer = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDManufacturerKey)); 95 | NSString *product = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); 96 | _vendorName = [NSString stringWithFormat:@"%@ %@", manufacturer, product]; 97 | 98 | _snapshot.version = 0x0100; 99 | _snapshot.size = sizeof(_snapshot); 100 | } 101 | 102 | return self; 103 | } 104 | 105 | static IOHIDElementRef 106 | GetAxis(IOHIDDeviceRef device, CFIndex axis) 107 | { 108 | NSDictionary *match = @{ 109 | @(kIOHIDElementUsagePageKey): @(kHIDPage_GenericDesktop), 110 | @(kIOHIDElementUsageKey): @(axis), 111 | }; 112 | 113 | NSArray *elements = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(device, (__bridge CFDictionaryRef)match, 0)); 114 | if(elements.count != 1) NSLog(@"Warning. Oops, didn't find exactly one axis?"); 115 | 116 | return (__bridge IOHIDElementRef)elements[0]; 117 | } 118 | 119 | static void 120 | SetupAxis(IOHIDDeviceRef device, IOHIDElementRef element, CFIndex dmin, CFIndex dmax, CFIndex rmin, CFIndex rmax, float deadZonePercent) 121 | { 122 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationMinKey), (__bridge CFTypeRef)@(dmin)); 123 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationMaxKey), (__bridge CFTypeRef)@(dmax)); 124 | 125 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationSaturationMinKey), (__bridge CFTypeRef)@(rmin)); 126 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationSaturationMaxKey), (__bridge CFTypeRef)@(rmax)); 127 | 128 | if(deadZonePercent > 0.0f){ 129 | CFIndex mid = (rmin + rmax)/2; 130 | CFIndex deadZone = (rmax - rmin)*(deadZonePercent/2.0f); 131 | 132 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationDeadZoneMinKey), (__bridge CFTypeRef)@(mid - deadZone)); 133 | IOHIDElementSetProperty(element, CFSTR(kIOHIDElementCalibrationDeadZoneMaxKey), (__bridge CFTypeRef)@(mid + deadZone)); 134 | } 135 | } 136 | 137 | static void 138 | ControllerConnected(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) 139 | { 140 | if(result == kIOReturnSuccess){ 141 | // NSURL *url = [[NSBundle mainBundle] URLForResource:@"CCControllerConfig.plist" withExtension:nil]; 142 | // NSDictionary *config = [NSDictionary dictionaryWithContentsOfURL:url]; 143 | // 144 | // NSAssert(@"CCControllerConfig.plist not found."); 145 | 146 | CCController *controller = [[CCController alloc] initWithDevice:device]; 147 | 148 | NSUInteger vid = [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)) unsignedIntegerValue]; 149 | NSUInteger pid = [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)) unsignedIntegerValue]; 150 | 151 | CFIndex axisMin = 0; 152 | CFIndex axisMax = 256; 153 | 154 | if(vid == 0x054C){ // Sony 155 | if(pid == 0x5C4){ // DualShock 4 156 | NSLog(@"[CCController initWithDevice:] Sony Dualshock 4 detected."); 157 | 158 | controller->_lThumbXUsageID = kHIDUsage_GD_X; 159 | controller->_lThumbYUsageID = kHIDUsage_GD_Y; 160 | controller->_rThumbXUsageID = kHIDUsage_GD_Z; 161 | controller->_rThumbYUsageID = kHIDUsage_GD_Rz; 162 | controller->_lTriggerUsageID = kHIDUsage_GD_Rx; 163 | controller->_rTriggerUsageID = kHIDUsage_GD_Ry; 164 | 165 | controller->_usesHatSwitch = YES; 166 | 167 | controller->_buttonPauseUsageID = 0x0A; 168 | controller->_buttonAUsageID = 0x02; 169 | controller->_buttonBUsageID = 0x03; 170 | controller->_buttonXUsageID = 0x01; 171 | controller->_buttonYUsageID = 0x04; 172 | controller->_lShoulderUsageID = 0x05; 173 | controller->_rShoulderUsageID = 0x06; 174 | } 175 | } else if(vid == 0x045E){ // Microsoft 176 | if(pid == 0x028E || pid == 0x028F){ // 360 wired/wireless 177 | NSLog(@"[CCController initWithDevice:] Microsoft Xbox 360 controller detected."); 178 | 179 | axisMin = -(1<<15); 180 | axisMax = (1<<15); 181 | 182 | controller->_lThumbXUsageID = kHIDUsage_GD_X; 183 | controller->_lThumbYUsageID = kHIDUsage_GD_Y; 184 | controller->_rThumbXUsageID = kHIDUsage_GD_Rx; 185 | controller->_rThumbYUsageID = kHIDUsage_GD_Ry; 186 | controller->_lTriggerUsageID = kHIDUsage_GD_Z; 187 | controller->_rTriggerUsageID = kHIDUsage_GD_Rz; 188 | 189 | controller->_dpadLUsageID = 0x0E; 190 | controller->_dpadRUsageID = 0x0F; 191 | controller->_dpadDUsageID = 0x0D; 192 | controller->_dpadUUsageID = 0x0C; 193 | 194 | controller->_buttonPauseUsageID = 0x09; 195 | controller->_buttonAUsageID = 0x01; 196 | controller->_buttonBUsageID = 0x02; 197 | controller->_buttonXUsageID = 0x03; 198 | controller->_buttonYUsageID = 0x04; 199 | controller->_lShoulderUsageID = 0x05; 200 | controller->_rShoulderUsageID = 0x06; 201 | } 202 | } 203 | 204 | // TODO can we do anything sensible with this? 205 | // else if(vid == 0x057E){ // Nintendo 206 | // if(pid == 0x0306){ 207 | // NSLog(@"[CCController initWithDevice:] Nintendo Wiimote detected."); 208 | // } 209 | // } 210 | 211 | SetupAxis(device, GetAxis(device, controller->_lThumbXUsageID), -1.0, 1.0, axisMin, axisMax, DeadZonePercent); 212 | SetupAxis(device, GetAxis(device, controller->_lThumbYUsageID), 1.0, -1.0, axisMin, axisMax, DeadZonePercent); 213 | SetupAxis(device, GetAxis(device, controller->_rThumbXUsageID), -1.0, 1.0, axisMin, axisMax, DeadZonePercent); 214 | SetupAxis(device, GetAxis(device, controller->_rThumbYUsageID), 1.0, -1.0, axisMin, axisMax, DeadZonePercent); 215 | 216 | SetupAxis(device, GetAxis(device, controller->_lTriggerUsageID), 0.0, 1.0, 0, 256, 0.0f); 217 | SetupAxis(device, GetAxis(device, controller->_rTriggerUsageID), 0.0, 1.0, 0, 256, 0.0f); 218 | 219 | IOHIDDeviceRegisterInputValueCallback(device, ControllerInput, (__bridge void *)controller); 220 | IOHIDDeviceSetInputValueMatchingMultiple(device, (__bridge CFArrayRef)@[ 221 | @{@(kIOHIDElementUsagePageKey): @(kHIDPage_GenericDesktop)}, 222 | @{@(kIOHIDElementUsagePageKey): @(kHIDPage_Button)}, 223 | ]); 224 | 225 | IOHIDDeviceRegisterRemovalCallback(device, ControllerDisconnected, (void *)CFBridgingRetain(controller)); 226 | 227 | [CONTROLLERS addObject:controller]; 228 | [[NSNotificationCenter defaultCenter] postNotificationName:GCControllerDidConnectNotification object:controller]; 229 | } 230 | } 231 | 232 | static void 233 | ControllerDisconnected(void *context, IOReturn result, void *sender) 234 | { 235 | if(result == kIOReturnSuccess){ 236 | CCController *controller = CFBridgingRelease((CFTypeRef)context); 237 | 238 | [CONTROLLERS removeObject:controller]; 239 | [[NSNotificationCenter defaultCenter] postNotificationName:GCControllerDidDisconnectNotification object:controller]; 240 | } 241 | } 242 | 243 | //MARK: Input callbacks 244 | 245 | static float 246 | Clamp(float value) 247 | { 248 | return MAX(-1.0f, MIN(value, 1.0f)); 249 | } 250 | 251 | static GCGamepadSnapShotDataV100 252 | CopyExtendedSnapshotData(GCExtendedGamepadSnapShotDataV100 *snapshot) 253 | { 254 | return (GCGamepadSnapShotDataV100){ 255 | .version = 0x0100, 256 | .size = sizeof(GCGamepadSnapShotDataV100), 257 | .dpadX = snapshot->dpadX, 258 | .dpadY = snapshot->dpadY, 259 | .buttonA = snapshot->buttonA, 260 | .buttonB = snapshot->buttonB, 261 | .buttonX = snapshot->buttonX, 262 | .buttonY = snapshot->buttonY, 263 | .leftShoulder = snapshot->leftShoulder, 264 | .rightShoulder = snapshot->rightShoulder, 265 | }; 266 | } 267 | 268 | static void 269 | ControllerUpdateSnapshot(CCController *controller) 270 | { 271 | GCExtendedGamepadSnapShotDataV100 *extendedSnapshot = &controller->_snapshot; 272 | 273 | // Update the gamepad snapshots if they currently exist. 274 | if(controller->_extendedGamepad){ 275 | NSData *data = NSDataFromGCExtendedGamepadSnapShotDataV100(extendedSnapshot); 276 | if(data) controller->_extendedGamepad.snapshotData = data; 277 | } 278 | 279 | if(controller->_gamepad){ 280 | GCGamepadSnapShotDataV100 snapshot = CopyExtendedSnapshotData(extendedSnapshot); 281 | NSData *data = NSDataFromGCGamepadSnapShotDataV100(&snapshot); 282 | if(data) controller->_gamepad.snapshotData = data; 283 | } 284 | } 285 | 286 | static void 287 | ControllerInput(void *context, IOReturn result, void *sender, IOHIDValueRef value) 288 | { 289 | if(result != kIOReturnSuccess) return; 290 | 291 | @autoreleasepool { 292 | CCController *controller = (__bridge CCController *)context; 293 | GCExtendedGamepadSnapShotDataV100 *snapshot = &controller->_snapshot; 294 | 295 | IOHIDElementRef element = IOHIDValueGetElement(value); 296 | 297 | uint32_t usagePage = IOHIDElementGetUsagePage(element); 298 | uint32_t usage = IOHIDElementGetUsage(element); 299 | 300 | CFIndex state = (int)IOHIDValueGetIntegerValue(value); 301 | float analog = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypeCalibrated); 302 | 303 | // NSLog(@"usagePage: 0x%02X, usage 0x%02X, value: %d / %f", usagePage, usage, state, analog); 304 | 305 | if(usagePage == kHIDPage_Button){ 306 | if(usage == controller->_buttonPauseUsageID){if(state) controller.controllerPausedHandler(controller);} 307 | if(usage == controller->_buttonAUsageID){snapshot->buttonA = state;} 308 | if(usage == controller->_buttonBUsageID){snapshot->buttonB = state;} 309 | if(usage == controller->_buttonXUsageID){snapshot->buttonX = state;} 310 | if(usage == controller->_buttonYUsageID){snapshot->buttonY = state;} 311 | if(usage == controller->_lShoulderUsageID){snapshot->leftShoulder = state;} 312 | if(usage == controller->_rShoulderUsageID){snapshot->rightShoulder = state;} 313 | 314 | if(!controller->_usesHatSwitch){ 315 | if(usage == controller->_dpadLUsageID){snapshot->dpadX = Clamp(snapshot->dpadX - (state ? 1.0f : -1.0f));} 316 | if(usage == controller->_dpadRUsageID){snapshot->dpadX = Clamp(snapshot->dpadX + (state ? 1.0f : -1.0f));} 317 | if(usage == controller->_dpadDUsageID){snapshot->dpadY = Clamp(snapshot->dpadY - (state ? 1.0f : -1.0f));} 318 | if(usage == controller->_dpadUUsageID){snapshot->dpadY = Clamp(snapshot->dpadY + (state ? 1.0f : -1.0f));} 319 | } 320 | } 321 | 322 | if(usagePage == kHIDPage_GenericDesktop){ 323 | if(usage == controller->_lThumbXUsageID ){snapshot->leftThumbstickX = analog;} 324 | if(usage == controller->_lThumbYUsageID ){snapshot->leftThumbstickY = analog;} 325 | if(usage == controller->_rThumbXUsageID ){snapshot->rightThumbstickX = analog;} 326 | if(usage == controller->_rThumbYUsageID ){snapshot->rightThumbstickY = analog;} 327 | if(usage == controller->_lTriggerUsageID){snapshot->leftTrigger = analog;} 328 | if(usage == controller->_rTriggerUsageID){snapshot->rightTrigger = analog;} 329 | 330 | if(controller->_usesHatSwitch && usage == kHIDUsage_GD_Hatswitch){ 331 | switch(state){ 332 | case 0: snapshot->dpadX = 0.0; snapshot->dpadY = 1.0; break; 333 | case 1: snapshot->dpadX = 1.0; snapshot->dpadY = 1.0; break; 334 | case 2: snapshot->dpadX = 1.0; snapshot->dpadY = 0.0; break; 335 | case 3: snapshot->dpadX = 1.0; snapshot->dpadY = -1.0; break; 336 | case 4: snapshot->dpadX = 0.0; snapshot->dpadY = -1.0; break; 337 | case 5: snapshot->dpadX = -1.0; snapshot->dpadY = -1.0; break; 338 | case 6: snapshot->dpadX = -1.0; snapshot->dpadY = 0.0; break; 339 | case 7: snapshot->dpadX = -1.0; snapshot->dpadY = 1.0; break; 340 | default: snapshot->dpadX = 0.0; snapshot->dpadY = 0.0; break; 341 | } 342 | } 343 | } 344 | 345 | ControllerUpdateSnapshot(controller); 346 | } 347 | } 348 | 349 | //MARK: Misc 350 | 351 | -(GCGamepad *)gamepad 352 | { 353 | if(_gamepad == nil){ 354 | _gamepad = [[GCGamepadSnapshot alloc] init]; 355 | GCGamepadSnapShotDataV100 snapshot = CopyExtendedSnapshotData(&_snapshot); 356 | NSData *data = NSDataFromGCGamepadSnapShotDataV100(&snapshot); 357 | if(data) _gamepad.snapshotData = data; 358 | } 359 | 360 | return _gamepad; 361 | } 362 | 363 | -(GCExtendedGamepad *)extendedGamepad 364 | { 365 | if(_extendedGamepad == nil){ 366 | _extendedGamepad = [[GCExtendedGamepadSnapshot alloc] init]; 367 | NSData *data = NSDataFromGCExtendedGamepadSnapShotDataV100(&_snapshot); 368 | _extendedGamepad.snapshotData = data; 369 | } 370 | 371 | return _extendedGamepad; 372 | } 373 | 374 | @end 375 | 376 | 377 | @implementation GCExtendedGamepad(SnapshotDataFast) 378 | 379 | -(NSData *)snapshotDataFast 380 | { 381 | GCExtendedGamepadSnapShotDataV100 snapshot = { 382 | .version = 0x0100, 383 | .size = sizeof(GCExtendedGamepadSnapShotDataV100), 384 | .dpadX = self.dpad.xAxis.value, 385 | .dpadY = self.dpad.yAxis.value, 386 | .buttonA = self.buttonA.value, 387 | .buttonB = self.buttonB.value, 388 | .buttonX = self.buttonX.value, 389 | .buttonY = self.buttonY.value, 390 | .leftShoulder = self.leftShoulder.value, 391 | .rightShoulder = self.rightShoulder.value, 392 | .leftThumbstickX = self.leftThumbstick.xAxis.value, 393 | .leftThumbstickY = self.leftThumbstick.yAxis.value, 394 | .rightThumbstickX = self.rightThumbstick.xAxis.value, 395 | .rightThumbstickY = self.rightThumbstick.yAxis.value, 396 | .leftTrigger = self.leftTrigger.value, 397 | .rightTrigger = self.rightTrigger.value, 398 | }; 399 | 400 | return NSDataFromGCExtendedGamepadSnapShotDataV100(&snapshot); 401 | } 402 | 403 | @end 404 | --------------------------------------------------------------------------------