Skip to content

Commit b3620f1

Browse files
committed
iOS: small followup to 357fce8, restore haptics after background
1 parent bbe9f32 commit b3620f1

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

input/drivers/cocoa_input.m

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
static id<CHHapticPatternPlayer> keypressHapticPlayer KEYPRESS_HAPTIC_AVAIL;
8282
/* Fallback for iOS 10-13 */
8383
static UISelectionFeedbackGenerator *feedbackGenerator;
84+
static void cocoa_input_init_haptic_engine(void) KEYPRESS_HAPTIC_AVAIL;
8485
#endif
8586
#endif
8687

@@ -388,28 +389,7 @@ void apple_input_keyboard_event(bool down,
388389

389390
#if TARGET_OS_IOS
390391
if (@available(iOS 14, *))
391-
{
392-
if (!keypressHapticEngine && CHHapticEngine.capabilitiesForHardware.supportsHaptics)
393-
{
394-
NSError *error;
395-
keypressHapticEngine = [[CHHapticEngine alloc] initAndReturnError:&error];
396-
if (!error)
397-
{
398-
[keypressHapticEngine startAndReturnError:&error];
399-
if (!error)
400-
{
401-
keypressHapticEngine.stoppedHandler = ^(CHHapticEngineStoppedReason reason) {
402-
keypressHapticPlayer = nil;
403-
keypressHapticEngine = nil;
404-
};
405-
keypressHapticEngine.resetHandler = ^{
406-
if (keypressHapticEngine)
407-
[keypressHapticEngine startAndReturnError:nil];
408-
};
409-
}
410-
}
411-
}
412-
}
392+
cocoa_input_init_haptic_engine();
413393
else
414394
{
415395
/* Fallback for iOS 10-13 */
@@ -897,10 +877,38 @@ static float cocoa_input_get_sensor_input(void *data, unsigned port, unsigned id
897877
}
898878

899879
#if TARGET_OS_IOS
880+
static void cocoa_input_init_haptic_engine(void) KEYPRESS_HAPTIC_AVAIL
881+
{
882+
if (!keypressHapticEngine && CHHapticEngine.capabilitiesForHardware.supportsHaptics)
883+
{
884+
NSError *error;
885+
keypressHapticEngine = [[CHHapticEngine alloc] initAndReturnError:&error];
886+
if (!error)
887+
{
888+
[keypressHapticEngine startAndReturnError:&error];
889+
if (!error)
890+
{
891+
keypressHapticEngine.stoppedHandler = ^(CHHapticEngineStoppedReason reason) {
892+
keypressHapticPlayer = nil;
893+
keypressHapticEngine = nil;
894+
};
895+
keypressHapticEngine.resetHandler = ^{
896+
if (keypressHapticEngine)
897+
[keypressHapticEngine startAndReturnError:nil];
898+
};
899+
}
900+
}
901+
}
902+
}
903+
900904
static void cocoa_input_keypress_vibrate(void)
901905
{
902906
if (@available(iOS 14, *))
903907
{
908+
/* Reinitialize engine if iOS stopped it (e.g., during backgrounding) */
909+
if (!keypressHapticEngine)
910+
cocoa_input_init_haptic_engine();
911+
904912
settings_t *settings = config_get_ptr();
905913
if (!settings || !keypressHapticEngine)
906914
return;

0 commit comments

Comments
 (0)