diff --git a/ios/PPSSPPUIApplication.h b/ios/PPSSPPUIApplication.h index e7af973f14..cd927c7d01 100644 --- a/ios/PPSSPPUIApplication.h +++ b/ios/PPSSPPUIApplication.h @@ -14,6 +14,9 @@ @interface PPSSPPUIApplication : UIApplication { } + +@property (nonatomic, strong) UISelectionFeedbackGenerator *feedbackGenerator; + @end diff --git a/ios/main.mm b/ios/main.mm index 27f9cbac71..8a5817982a 100644 --- a/ios/main.mm +++ b/ios/main.mm @@ -98,15 +98,45 @@ PermissionStatus System_GetPermissionStatus(SystemPermission permission) { retur FOUNDATION_EXTERN void AudioServicesPlaySystemSoundWithVibration(unsigned long, objc_object*, NSDictionary*); -void Vibrate(int length_ms) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - NSArray *pattern = @[@YES, @30, @NO, @2]; - - dictionary[@"VibePattern"] = pattern; - dictionary[@"Intensity"] = @2; - - AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, dictionary); - // TODO: Actually make use of length_ms if PPSSPP ever adds that in the config +BOOL SupportsTaptic() +{ + // we're on an iOS version that cannot instantiate UISelectionFeedbackGenerator, so no. + if(!NSClassFromString(@"UISelectionFeedbackGenerator")) + { + return NO; + } + + // http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/ + // use private API against UIDevice to determine the haptic stepping + // 2 - iPhone 7 or above, full taptic feedback + // 1 - iPhone 6S, limited taptic feedback + // 0 - iPhone 6 or below, no taptic feedback + NSNumber* val = (NSNumber*)[[UIDevice currentDevice] valueForKey:@"feedbackSupportLevel"]; + return [val intValue] >= 2; +} + +void Vibrate(int mode) { + + if(SupportsTaptic()) + { + PPSSPPUIApplication* app = (PPSSPPUIApplication*)[UIApplication sharedApplication]; + if(app.feedbackGenerator == nil) + { + app.feedbackGenerator = [[UISelectionFeedbackGenerator alloc] init]; + [app.feedbackGenerator prepare]; + } + [app.feedbackGenerator selectionChanged]; + } + else + { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + NSArray *pattern = @[@YES, @30, @NO, @2]; + + dictionary[@"VibePattern"] = pattern; + dictionary[@"Intensity"] = @2; + + AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, dictionary); + } } int main(int argc, char *argv[])