Early support for mFi controller

This commit is contained in:
Yoshi Sugawara 2016-04-01 16:07:46 -04:00
parent 9d3737bf1a
commit 6c3defc2e1
4 changed files with 161 additions and 1 deletions

View File

@ -237,6 +237,8 @@
7E51482F1CA6B5CE005DA0A6 /* ShastonHi640.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 7E51481C1CA6B5CE005DA0A6 /* ShastonHi640.ttf */; };
7E5148301CA6B5CE005DA0A6 /* Spin Up Search 1.wav in Resources */ = {isa = PBXBuildFile; fileRef = 7E51481D1CA6B5CE005DA0A6 /* Spin Up Search 1.wav */; };
7E5148311CA6B5CE005DA0A6 /* Spin Up Search 2.wav in Resources */ = {isa = PBXBuildFile; fileRef = 7E51481E1CA6B5CE005DA0A6 /* Spin Up Search 2.wav */; };
9250DCB31CAEEF990093CE9A /* MfiGameControllerHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 9250DCB21CAEEF990093CE9A /* MfiGameControllerHandler.m */; };
9250DCB51CAEFD3B0093CE9A /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9250DCB41CAEFD3B0093CE9A /* GameController.framework */; };
928410581CA8443A00DC5D93 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 928410571CA8443A00DC5D93 /* Images.xcassets */; };
/* End PBXBuildFile section */
@ -574,6 +576,9 @@
7E51481C1CA6B5CE005DA0A6 /* ShastonHi640.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = ShastonHi640.ttf; sourceTree = "<group>"; };
7E51481D1CA6B5CE005DA0A6 /* Spin Up Search 1.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "Spin Up Search 1.wav"; sourceTree = "<group>"; };
7E51481E1CA6B5CE005DA0A6 /* Spin Up Search 2.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "Spin Up Search 2.wav"; sourceTree = "<group>"; };
9250DCB11CAEEF990093CE9A /* MfiGameControllerHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MfiGameControllerHandler.h; sourceTree = "<group>"; };
9250DCB21CAEEF990093CE9A /* MfiGameControllerHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MfiGameControllerHandler.m; sourceTree = "<group>"; };
9250DCB41CAEFD3B0093CE9A /* GameController.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameController.framework; path = System/Library/Frameworks/GameController.framework; sourceTree = SDKROOT; };
928410571CA8443A00DC5D93 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = ActiveGS/Images.xcassets; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -582,6 +587,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9250DCB51CAEFD3B0093CE9A /* GameController.framework in Frameworks */,
095036E9190547BE006A457A /* CoreLocation.framework in Frameworks */,
095036E7190547A0006A457A /* AdSupport.framework in Frameworks */,
095036E519054799006A457A /* CoreData.framework in Frameworks */,
@ -977,6 +983,8 @@
09FA6089125A7B3E00B07F77 /* icon_APPLE 2GS.png */,
09FA608A125A7B3E00B07F77 /* KBDController.h */,
09FA608B125A7B3E00B07F77 /* KBDController.mm */,
9250DCB11CAEEF990093CE9A /* MfiGameControllerHandler.h */,
9250DCB21CAEEF990093CE9A /* MfiGameControllerHandler.m */,
);
name = Common.iphone;
path = ../Common.iphone;
@ -1045,6 +1053,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
9250DCB41CAEFD3B0093CE9A /* GameController.framework */,
095036E8190547BE006A457A /* CoreLocation.framework */,
095036E6190547A0006A457A /* AdSupport.framework */,
095036E419054799006A457A /* CoreData.framework */,
@ -1307,6 +1316,7 @@
09BB43B411D92F70005ADA46 /* CEmulatorCtrl.cpp in Sources */,
09BB43B511D92F70005ADA46 /* interface.cpp in Sources */,
09BB43B611D92F70005ADA46 /* libpng.cpp in Sources */,
9250DCB31CAEEF990093CE9A /* MfiGameControllerHandler.m in Sources */,
09BB43B711D92F70005ADA46 /* rom.cpp in Sources */,
09FA6095125A7B3E00B07F77 /* activegsAppDelegate.mm in Sources */,
09052B8119053C9F00853FAE /* pngset.cpp in Sources */,

View File

@ -21,6 +21,7 @@
#import "../kegs/iOS/emulatorView.h"
#include "../kegs/src/SaveState.h"
#include "../common/activedownload.h"
#import "MfiGameControllerHandler.h"
#ifdef ACTIVEGS_CUSTOMKEYS
#include "UICustomKey.h"
@ -278,6 +279,7 @@ int isHardwareKeyboard()
@interface KBDController() {
UISegmentedControl *saveStateSegmentedControl;
}
@property (nonatomic,strong) MfiGameControllerHandler *mfiControllerHandler;
@end
@implementation KBDController
@ -565,6 +567,47 @@ extern int findCode(const char* _s);
[self setInputMode:INPUTMODE_ACCESS+INPUTMODE_HIDDEN];
[self setMenuBarVisibility:TRUE]; // So First time users are not lost!
self.mfiControllerHandler = [[MfiGameControllerHandler alloc] init];
__weak typeof(self) weakSelf = self;
[self.mfiControllerHandler discoverController:^(GCController *gameController) {
[weakSelf setupMfiController:gameController];
[self setInputMode:inputMode&INPUTMODE_PAD];
[pManager setNotificationText:@"mFi Controller Connected"];
} disconnectedCallback:^{
[pManager setNotificationText:@"mFi Controller Disconnected"];
}];
}
-(void) setupMfiController:(GCController*)controller {
void (^appleJoyButton0Handler)(GCControllerButtonInput *, float, BOOL) = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key(0x37, 0);
} else {
add_event_key(0x37, 1);
}
};
void (^appleJoyButton1Handler)(GCControllerButtonInput *, float, BOOL) = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key(0x3a, 0);
} else {
add_event_key(0x3a, 1);
}
};
void (^appleJoystickhHandler)(GCControllerDirectionPad *, float, float) = ^(GCControllerDirectionPad *, float xvalue, float yvalue) {
joyX = xvalue;
joyY = yvalue * -1.0;
};
GCControllerButtonInput *buttonX = controller.extendedGamepad ? controller.extendedGamepad.buttonX : controller.gamepad.buttonX;
GCControllerButtonInput *buttonA = controller.extendedGamepad ? controller.extendedGamepad.buttonA : controller.gamepad.buttonA;
GCControllerDirectionPad *dpad = controller.extendedGamepad ? controller.extendedGamepad.dpad : controller.gamepad.dpad;
buttonX.valueChangedHandler = appleJoyButton0Handler;
buttonA.valueChangedHandler = appleJoyButton1Handler;
dpad.valueChangedHandler = appleJoystickhHandler;
if ( controller.extendedGamepad ) {
controller.extendedGamepad.leftThumbstick.valueChangedHandler = appleJoystickhHandler;
}
}
int hardwarekeyboard= 0;
@ -1951,7 +1994,6 @@ void paddle_trigger_icade(double dcycs)
paddle_update_trigger_dcycs(dcycs);
}
int x_adb_get_keypad_x()
{
return keypad_x;

View File

@ -0,0 +1,16 @@
//
// MfiGameControllerHandler.h
// activegs
//
// Created by Yoshi Sugawara on 4/1/16.
//
//
#import <Foundation/Foundation.h>
#import <GameController/GameController.h>
@interface MfiGameControllerHandler : NSObject
- (void)discoverController:(void (^)(GCController *gameController))controllerCallbackSetup disconnectedCallback:(void (^)(void))controllerDisconnectedCallback;
@end

View File

@ -0,0 +1,92 @@
//
// MfiGameControllerHandler.m
// activegs
//
// Created by Yoshi Sugawara on 4/1/16.
//
//
#import "MfiGameControllerHandler.h"
@interface MfiGameControllerHandler()
@property (nonatomic,copy) void (^controllerCallbackSetup)(GCController *gameController);
@property (nonatomic,copy) void (^controllerDisconnectedCallback)(void);
@end
@implementation MfiGameControllerHandler
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:GCControllerDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:GCControllerDidDisconnectNotification
object:nil];
}
- (void)discoverController:(void (^)(GCController *gameController))controllerCallbackSetup disconnectedCallback:(void (^)(void))controllerDisconnectedCallback{
self.controllerCallbackSetup = controllerCallbackSetup;
self.controllerDisconnectedCallback = controllerDisconnectedCallback;
if ([self hasControllerConnected]) {
NSLog(@"Already have a controller connected!");
[self foundController];
} else {
[self startDiscovery];
}
}
-(void) startDiscovery {
[GCController startWirelessControllerDiscoveryWithCompletionHandler:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(foundController)
name:GCControllerDidConnectNotification
object:nil];
}
- (void) stopDiscovery {
NSLog(@"Stopping controller discovery...");
[GCController stopWirelessControllerDiscovery];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:GCControllerDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:GCControllerDidDisconnectNotification
object:nil];
}
- (void)foundController {
NSLog(@"Found a controller!");
if (self.controllerCallbackSetup) {
self.controllerCallbackSetup([[GCController controllers] firstObject]);
}
[self stopDiscovery];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(controllerDisconnected)
name:GCControllerDidDisconnectNotification
object:nil];
}
- (void)controllerDisconnected {
if (self.controllerDisconnectedCallback){
self.controllerDisconnectedCallback();
}
[self startDiscovery];
}
- (BOOL)hasControllerConnected {
return [[GCController controllers] count] > 0;
}
-(GCController*) getController {
// just return the first one for now
if ( [self hasControllerConnected] ) {
return [[GCController controllers] firstObject];
}
return nil;
}
@end