|
81 | 81 | static id<CHHapticPatternPlayer> keypressHapticPlayer KEYPRESS_HAPTIC_AVAIL; |
82 | 82 | /* Fallback for iOS 10-13 */ |
83 | 83 | static UISelectionFeedbackGenerator *feedbackGenerator; |
| 84 | +static void cocoa_input_init_haptic_engine(void) KEYPRESS_HAPTIC_AVAIL; |
84 | 85 | #endif |
85 | 86 | #endif |
86 | 87 |
|
@@ -388,28 +389,7 @@ void apple_input_keyboard_event(bool down, |
388 | 389 |
|
389 | 390 | #if TARGET_OS_IOS |
390 | 391 | 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(); |
413 | 393 | else |
414 | 394 | { |
415 | 395 | /* Fallback for iOS 10-13 */ |
@@ -897,10 +877,38 @@ static float cocoa_input_get_sensor_input(void *data, unsigned port, unsigned id |
897 | 877 | } |
898 | 878 |
|
899 | 879 | #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 | + |
900 | 904 | static void cocoa_input_keypress_vibrate(void) |
901 | 905 | { |
902 | 906 | if (@available(iOS 14, *)) |
903 | 907 | { |
| 908 | + /* Reinitialize engine if iOS stopped it (e.g., during backgrounding) */ |
| 909 | + if (!keypressHapticEngine) |
| 910 | + cocoa_input_init_haptic_engine(); |
| 911 | + |
904 | 912 | settings_t *settings = config_get_ptr(); |
905 | 913 | if (!settings || !keypressHapticEngine) |
906 | 914 | return; |
|
0 commit comments