Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Music Haptics and In the Background are separate switches with setup guidance
My-Name-Is-Jeffa32da48826a1
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex ff70ead5..dfed6a6b 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -210,13 +210,16 @@ Shared:and bass analyzer on the render thread (SGMusicAnalyzer.m, plain C), and a thread of its own schedulesthe taps and the rumble for when the sound is heard, at their strength and leaving out what Followsleaves out (MusicHaptics.x). Everything applies at once; nothing plays while Spotify is not the active- app. Music Haptics is a choice of None, Generated (the above) or Native iOS (iOS 18 and up), never- both: Native iOS asks Spotify's extended-metadata for the track's ISRC with Spotify's own spclient- headers, asks Apple Music's catalog for that ISRC, and names the matched song's catalog id (same ISRC,- length within 2 s, a haptic track) or else the ISRC in the now playing info for iOS's own Music- Haptics (SystemMusicHaptics.x, its pure steps in SGHapticTrack.m). The analyzer is scored on the Mac- against harness/haptics/, as are Native iOS's pure steps (build/track), the hook in the simulator- against its sim/, the settings against harness/haptics-page/+ app. Beside it, In the Background (iOS 18 and up) asks Spotify's extended-metadata for the track's+ ISRC with Spotify's own spclient headers, asks Apple Music's catalog for that ISRC, and names the+ matched song's catalog id (same ISRC, length within 2 s, a haptic track) or else the ISRC in the now+ playing info for iOS's own Music Haptics, which plays on the lock screen and in other apps too+ (SystemMusicHaptics.x, its pure steps in SGHapticTrack.m). Both may be on: while it asks and while+ iOS has a haptic track for the song, the mod's own stays quiet, since iOS plays the track in front+ of Spotify too; once iOS has none, the mod's own plays while Spotify is in front. The analyzer is+ scored on the Mac against harness/haptics/, as are the pure steps (build/track), the hook, the two+ switches, the stand-down and the move from the old choice in the simulator against its sim/, the+ settings against harness/haptics-page/HeadGestures/ AirPods gestures (HeadGestures.h lists its files): CMHeadphoneMotionManager's attitudes, while theswitch is on and Spotify plays, read for a double nod and a shake, each doing what its pull-downsays: Nothing, back or forward 15 s, play or pause (which listens with the song paused too), next or@@ -466,18 +469,20 @@ Spotify's device button in that minimized row, off by default) and the device buchecked in the simulator against harness/player/ (`settings`). Vibrations, a page of its own under eitherlook opened from the main page (the row reads out which of the two are on), leads with a preview (Shared/Haptics/SGVibrationsPreview.m): rings of dots that a tap sends a crest across, outfrom the middle in the accent colour, higher and further the stronger the Strength, while the first of Controls and-Generated that is on plays its own tap through its own path (Controls' add tap, or one kick of Generated's with its-rumble unless it follows Beat), the line under it naming which, or saying why there is none. While the page shows,-each tap Generated plays sends a low crest too. Nothing runs between crests, and with Reduce Motion the rings light-up together and fade instead of moving. Under it a card for-Controls (on until switched off) and one for Music Haptics, a choice of None, Generated or Native iOS-(left out below iOS 18) with an ⓘ saying what each does. Someone with no choice stored gets Native iOS-when Music Haptics is on in Accessibility, else Generated if the old switch was on, else None. Controls+Music Haptics that is on plays its own tap through its own path (Controls' add tap, or one kick of Music Haptics'+with its rumble unless it follows Beat), the line under it naming which, or saying why there is none. While the page+shows, each tap Music Haptics plays sends a low crest too. Nothing runs between crests, and with Reduce Motion the+rings light up together and fade instead of moving. Under it a card for+Controls (on until switched off) and one for Music Haptics, which holds two switches, both off until switched on,+each with an ⓘ: Music Haptics, the mod's own from the sound while Spotify is in front, and In the Background+(left out below iOS 18), the song named to iOS's own Music Haptics. The one choice of before moves to them once+(Generated to Music Haptics, Native iOS to In the Background, None to neither). Controlshas its Strength under it, greyed out while the switch is off (10 to 100%, a tap at the new strength with each step);-Generated into its Strength (20 to 200%, 100% being how it first shipped, a kick at the new strength with each step) and Follows, Everything (a tap on-each kick and snare and a rumble under the bass), Beat (the taps without the rumble) or Bass (the kicks'-taps and the rumble); Native iOS into a Status row (Off in iOS, Paused, Waiting, Checking, Ready, Playing-or Unavailable), all applying straight away. Live Activity, on iOS 17 and up under either look: its switch and which view it+Music Haptics its Strength (20 to 200%, 100% being how it first shipped, a kick at the new strength with each step) and+Follows, greyed out the same way, Everything (a tap on each kick and snare and a rumble under the bass), Beat (the+taps without the rumble) or Bass (the kicks' taps and the rumble); In the Background a Status row (Paused, Waiting,+Checking, Ready, Playing or Unavailable) while it is on, or, while Music Haptics is off in Settings > Accessibility, a+row saying so whose tap opens its ⓘ, all applying straight away. Live Activity, on iOS 17 and up under either look: its switch and which view itshows, Lyrics, Queue or Control menu, and for Lyrics its Translations (off until switched on) and Textsize (Small, Medium or Large), all applying straight away, the row reading out the view or Off. Audio effects, in either look (Shared/AudioEffects/AudioEffectsPage.m):the effects' switch with what the engine is doing under it, Presets (built-in ones and your own, saved, loadeddiff --git a/harness/haptics-page/README.md b/harness/haptics-page/README.mdindex 6b066add..6b42de11 100644--- a/harness/haptics-page/README.md+++ b/harness/haptics-page/README.md@@ -12,8 +12,9 @@ the preview's watcher for `pulse` to hand a tap of the music's to.xcrun simctl io <udid> screenshot shot.pngOther agents use the simulator too: make a device of your own (`xcrun simctl create`) and address it by UDID.-Every launch clears the `spotifyglass.redesign.haptics` keys first unless `keep` is on the line; `main.m` lists the-setup words (`controls-off`, `music`, `follows=<n>`, `slow`) and the actions, played one every 0.7 s from 1 s in:+Every launch clears the `spotifyglass.*haptics*` keys first unless `keep` is on the line; `main.m` lists the+setup words (`controls-off`, `music`, `background`, `mode=<n>`, `ios-off`, `follows=<n>`, `slow`) and the actions,+played one every 0.7 s from 1 s in:flipping a switch, dragging a slider and letting go, VoiceOver's swipe on one, the ⓘ, a tap on a row (the Followsrow, then a name in its list), and `dump`, which logs the stored keys, what the hooks read and the rows shown:@@ -38,7 +39,7 @@ Strength back to full strength without a row moving.What it does not cover: a real finger on the slider, and how any strength feels, which only a phone can tell.2026-10-06, iPhone 17 Pro on iOS 27.0: the page opens on the preview, its line reading Tap to feel Controls, Tap to-feel Music Haptics (`controls-off music`), the Native iOS note (`controls-off native`) or what to turn on+feel Music Haptics (`controls-off music`), the Native iOS note (`controls-off native`, now `controls-off background`) or what to turn on(`controls-off`), and Tap to feel Controls again once `toggle=0.0` turns Controls on; with `slow tap`, six shots1.3 s apart show the middle dipping and one lit ring travelling out to the edge and the field settling back torest; with Reduce Motion on (`simctl spawn <udid> defaults write com.apple.Accessibility ReduceMotionEnabled -bool@@ -48,3 +49,15 @@ wraps under accessibility-medium text and the cards move down under it.Not covered here: how the taps feel, whether the preview kick reaches a running engine on a phone, and themusic's pulses with real music playing (`pulse` stands in for them).++2026-10-06, iPhone 17 Pro on iOS 27.0, the two switches: with nothing stored both read off, Strength and Follows+greyed under Music Haptics and In the Background below them. `mode=0`, `mode=1`, `mode=2` and `mode=7`, each with+`expect=` and `dump`, move the old choice to off/off, on/off, off/on and off/off and leave it unstored. `background`+shows Status under In the Background; with `ios-off` the row "Music Haptics is off in iOS" takes its place, `ios=on`+and `ios=off` swap them while the page shows, and `select=1.4 alert` opens In the Background's ⓘ from it. `toggle=1.0`+and `toggle=1.3` post the switches' change (the stub logs what the engines read), the preview line follows (Tap to+feel Music Haptics, or "In the Background is played by iOS" when it is the only one on) and the main page's row reads+Music Haptics with either on.++ xcrun simctl launch --console-pty <udid> com.vojta.hapticspageharness mode=2 expect=off,on dump+ xcrun simctl launch --console-pty <udid> com.vojta.hapticspageharness music background ios-off dump ios=on dump select=1.4 alertdiff --git a/harness/haptics-page/main.m b/harness/haptics-page/main.mindex b0ba5845..ec564291 100644--- a/harness/haptics-page/main.m+++ b/harness/haptics-page/main.m@@ -6,8 +6,12 @@// xcrun simctl launch <udid> com.vojta.hapticspageharness [setup...] [action...]//// Setup: keep (the stored settings stay; otherwise every haptics key is cleared first),-// controls-off (Controls switched off), music (Music Haptics on Generated), native (on Native iOS), follows=<n>, slow (animations at a twentieth-// of their speed).+// controls-off (Controls switched off), music (Music Haptics switched on), background (In the Background switched on),+// mode=<n> (the one choice of before stored, None 0, Generated 1, Native iOS 2, for the page to move to the switches),+// ios-off (Music Haptics off in Settings > Accessibility, as SystemMusicHaptics.x would report it), follows=<n>,+// slow (animations at a twentieth of their speed).+// More actions: ios=on|off (iOS's switch changing while the page shows), expect=<music>,<background> (on or off+// each: what the switches read, logged as ok or FAILED).// Actions: toggle=<section>.<row> (that row's switch flipped the way a tap does), slide=<section>.<row>:<value>// (that slider dragged there and let go), tap (the preview tapped, as VoiceOver's double tap does it),// pulse=<intensity> (a tap of Music Haptics' handed to the preview, as the engine does while the page shows), swipe=<section>.<row>:<n> (VoiceOver's swipe up on it, n times, down@@ -44,8 +48,10 @@ static void findViews(UIView *root, Class kind, NSMutableArray *found) {NSMutableArray<NSString *> *actions = [NSMutableArray array];for (NSString *arg in [args subarrayWithRange:NSMakeRange(1, args.count - 1)]) {if ([arg isEqualToString:@"controls-off"]) SGSetEnabled(SGKeyControlHaptics, NO);- else if ([arg isEqualToString:@"music"]) SGSetInt(SGKeyMusicHapticsMode, SGMusicHapticsGenerated);- else if ([arg isEqualToString:@"native"]) SGSetInt(SGKeyMusicHapticsMode, SGMusicHapticsNative);+ else if ([arg isEqualToString:@"music"]) SGSetEnabled(SGKeyMusicHaptics, YES);+ else if ([arg isEqualToString:@"background"]) SGSetEnabled(SGKeyMusicHapticsBackground, YES);+ else if ([arg hasPrefix:@"mode="]) SGSetInt(SGKeyMusicHapticsMode, [arg substringFromIndex:5].integerValue);+ else if ([arg isEqualToString:@"ios-off"]) SGSetSystemMusicHapticsOn(NO);else if ([arg hasPrefix:@"follows="]) SGSetInt(SGKeyMusicFollows, [arg substringFromIndex:8].integerValue);else if (![@[@"keep", @"slow"] containsObject:arg]) [actions addObject:arg];}@@ -126,14 +132,26 @@ static void findViews(UIView *root, Class kind, NSMutableArray *found) {} else if ([verb isEqualToString:@"bottom"]) {CGFloat max = MAX(-table.adjustedContentInset.top, table.contentSize.height - table.bounds.size.height + table.adjustedContentInset.bottom);[table setContentOffset:CGPointMake(0, max) animated:NO];+ } else if ([verb isEqualToString:@"ios"]) {+ SGSetSystemMusicHapticsOn([value isEqualToString:@"on"]);+ } else if ([verb isEqualToString:@"expect"]) {+ NSString *got = [NSString stringWithFormat:@"%@,%@", SGMusicHapticsOn() ? @"on" : @"off", SGMusicHapticsInBackground() ? @"on" : @"off"];+ NSLog(@"[harness] %@ Music Haptics, In the Background read %@ (want %@), the old choice %@", [got isEqualToString:value] ? @" ok " : @"FAILED",+ got, value, [NSUserDefaults.standardUserDefaults objectForKey:SGKeyMusicHapticsMode] ? @"still stored" : @"gone");+ } else if ([verb isEqualToString:@"alert"]) {+ UIAlertController *alert = (UIAlertController *)self.nav.topViewController.presentedViewController;+ NSLog(@"[harness] on top: %@", [alert isKindOfClass:UIAlertController.class]+ ? [NSString stringWithFormat:@"\"%@\": %@", alert.title, [alert.message substringToIndex:MIN(70, alert.message.length)]] : @"no alert");} else if ([verb isEqualToString:@"dump"]) {NSDictionary *all = NSUserDefaults.standardUserDefaults.dictionaryRepresentation;for (NSString *key in [all.allKeys sortedArrayUsingSelector:@selector(compare:)]) {if ([key hasPrefix:@"spotifyglass."] && [key containsString:@"haptics"]) NSLog(@"[harness] stored %@ = %@", key, all[key]);}- NSLog(@"[harness] the hooks read: Controls %@ at %.0f%%, Music Haptics %@ at %.0f%% following %ld", SGEnabled(SGKeyControlHaptics) ? @"on" : @"off",- SGHapticsStrength(SGKeyControlStrength) * 100, @[@"None", @"Generated", @"Native iOS"][SGMusicHapticsModeNow()],- SGHapticsStrength(SGKeyMusicStrength) * 100, (long)SGMusicHapticsFollows());+ NSLog(@"[harness] the hooks read: Controls %@ at %.0f%%, Music Haptics %@ at %.0f%% following %ld, In the Background %@, iOS's Music Haptics %@; "+ @"the main page's row reads %@, the preview says \"%@\"", SGEnabled(SGKeyControlHaptics) ? @"on" : @"off",+ SGHapticsStrength(SGKeyControlStrength) * 100, SGMusicHapticsOn() ? @"on" : @"off", SGHapticsStrength(SGKeyMusicStrength) * 100,+ (long)SGMusicHapticsFollows(), SGMusicHapticsInBackground() ? @"on" : @"off", SGSystemMusicHapticsOn() ? @"on" : @"off",+ SGVibrationsSummary(), table.tableHeaderView.accessibilityHint);for (NSInteger section = 0; section < table.numberOfSections; section++) {NSMutableArray<NSString *> *rows = [NSMutableArray array];for (NSInteger row = 0; row < [table numberOfRowsInSection:section]; row++) {diff --git a/harness/haptics-page/stubs.m b/harness/haptics-page/stubs.mindex 674c6a81..634de27f 100644--- a/harness/haptics-page/stubs.m+++ b/harness/haptics-page/stubs.m@@ -1,12 +1,12 @@-// Music Haptics' side of Haptics.h (MusicHaptics.x in the tweak), which the page calls as its choice and its+// Music Haptics' side of Haptics.h (MusicHaptics.x in the tweak), which the page calls as its switches and its// settings change and its preview is tapped: each is logged with what the hooks would read at that moment.// The preview's watcher is kept, for the harness's pulse to hand a tap to.#import "Core/SGCore.h"#import "Shared/Haptics/Haptics.h"-__attribute__((constructor)) static void sg_logModeChanges(void) {- [NSNotificationCenter.defaultCenter addObserverForName:SGMusicHapticsModeChangedNotification object:nil queue:nil usingBlock:^(NSNotification *note) {- NSLog(@"[harness] Music Haptics plays %@", @[@"None", @"Generated", @"Native iOS"][SGMusicHapticsModeNow()]);+__attribute__((constructor)) static void sg_logSwitches(void) {+ [NSNotificationCenter.defaultCenter addObserverForName:SGMusicHapticsSwitchesChangedNotification object:nil queue:nil usingBlock:^(NSNotification *note) {+ NSLog(@"[harness] the engines read Music Haptics %@, In the Background %@", SGMusicHapticsOn() ? @"on" : @"off", SGMusicHapticsInBackground() ? @"on" : @"off");}];}diff --git a/harness/haptics/README.md b/harness/haptics/README.mdindex 470710d5..c758d405 100644--- a/harness/haptics/README.md+++ b/harness/haptics/README.md@@ -1,6 +1,6 @@# Music Haptics harness-Two halves: the analyzer on the Mac, and the hook in the simulator. Beside them, `track.m` checks Native iOS's+Two halves: the analyzer on the Mac, and the hook in the simulator. Beside them, `track.m` checks In the Background'spure steps on the Mac (`./build.sh && build/track`): the ISRC check, hand-built TRACK_V4 replies and Apple'scatalog songs matched or turned down.@@ -56,6 +56,13 @@ back at the next, then a 16-bit interleaved client. The app has harness/scene.m'`./build-sim.sh before <dir>` builds the same against an older `MusicHaptics.x`, `Haptics.h` and analyzer put in`<dir>`, running only the format steps. Use a device of your own (`xcrun simctl create`), by UDID.+2026-10-06, iPhone 17 Pro on iOS 27.0, the two switches: every step passes. Before the audio, the old choice moves+for each thing that could be stored (choices 0, 1, 2 and 7 beside an old switch, the old switch alone, the redesign's+old key alone, nothing), run twice each. Then Music Haptics off plays nothing and on plays again; In the Background+on beside it changes nothing until `SGMusicHapticsSetSystemCovers(YES)` (what SystemMusicHaptics.x sends while it asks+about the song and while iOS has a track), which stops every tap and the rumble, flipping Music Haptics off and on+under it too; NO (iOS has none for the next song) brings them back; In the Background alone plays nothing.+2026-10-05, iPhone 17 Pro on iOS 27.0: every step passes, the stand-down and its return too.2026-09-19: every step passes. Beat sends no rumble, Bass no snare, 50% halves the taps (0.44 from 0.88) and thediff --git a/harness/haptics/sim/main.m b/harness/haptics/sim/main.mindex 3e0db00b..abe2e747 100644--- a/harness/haptics/sim/main.m+++ b/harness/haptics/sim/main.m@@ -8,7 +8,9 @@// The generator plays a beat at 120 BPM, a kick on each whole second and a snare on each half, so the analyzer// hears a kick and a snare a second. A script changes the settings every 6 s and checks what the stand-in was// asked to play in the 4 s after the change has settled: what each Follows choice leaves out, the strength-// scaling the taps and the rumble, then a 16-bit interleaved client at 48 kHz.+// scaling the taps and the rumble, the two switches and the stand-down while iOS's own covers the song (as+// SystemMusicHaptics.x reports it), then a 16-bit interleaved client at 48 kHz. Before the audio starts, the+// move from the one choice of before to the two switches is checked for each stored choice.//// THEOS=$HOME/theos ../build-sim.sh// xcrun simctl install <udid> build/sim/HapticsHarness.app@@ -169,7 +171,40 @@ static int sg_failures;rumble ? (isnan(level) ? @"some" : [NSString stringWithFormat:@"%.3f", level]) : @"none");}+#ifndef BEFORE+// The move from the one choice of before to the two switches, from each thing someone could have stored: the+// choice (beside an old switch it overrides), the old switch alone, the redesign's old key alone, nothing. Run+// twice each, since every reader of the switches runs it. Leaves Music Haptics on and In the Background off.+- (void)checkMigration {+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;+ id none = NSNull.null;+ // The choice, the switch, the redesign's old key; then what the two switches should read.+ NSArray<NSArray *> *cases = @[+ @[@0, @YES, none, @NO, @NO], @[@1, @NO, none, @YES, @NO], @[@2, @YES, none, @NO, @YES], @[@7, none, none, @NO, @NO],+ @[none, @YES, none, @YES, @NO], @[none, none, @YES, @YES, @NO], @[none, none, none, @NO, @NO],+ ];+ NSArray<NSString *> *keys = @[SGKeyMusicHapticsMode, SGKeyMusicHaptics, SGKeyMusicHapticsWas];+ for (NSArray *c in cases) {+ for (NSString *key in [keys arrayByAddingObject:SGKeyMusicHapticsBackground]) [store removeObjectForKey:key];+ for (NSUInteger i = 0; i < keys.count; i++) if (c[i] != none) [store setObject:c[i] forKey:keys[i]];+ SGMigrateMusicHaptics();+ SGMigrateMusicHaptics();+ BOOL music = SGMusicHapticsOn(), background = SGMusicHapticsInBackground(), left = [store objectForKey:SGKeyMusicHapticsMode] != nil;+ BOOL ok = music == [c[3] boolValue] && background == [c[4] boolValue] && !left;+ if (!ok) sg_failures++;+ NSLog(@"[harness] %@ stored choice %@, switch %@, old key %@: Music Haptics %@, In the Background %@ (want %@, %@)%@", ok ? @" ok " : @"FAILED",+ c[0], c[1], c[2], music ? @"on" : @"off", background ? @"on" : @"off", [c[3] boolValue] ? @"on" : @"off", [c[4] boolValue] ? @"on" : @"off",+ left ? @", the choice still stored" : @"");+ }+ for (NSString *key in [keys arrayByAddingObject:SGKeyMusicHapticsBackground]) [store removeObjectForKey:key];+ SGSetEnabled(SGKeyMusicHaptics, YES);+}+#endif+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {+#ifndef BEFORE+ [self checkMigration];+#endifself.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];self.window.rootViewController = [UIViewController new];[self.window makeKeyAndVisible];@@ -190,10 +225,10 @@ static int sg_failures;ok ? @" ok " : @"FAILED", @"44.1 kHz client, Everything, 100%", peek.kicks, peek.snares, s, s, s, tap100, peek.levels, level100);}]];#ifndef BEFORE- // What the settings' choice does: stores it and tells both engines.- void (^choose)(SGMusicHapticsMode) = ^(SGMusicHapticsMode mode) {- SGSetInt(SGKeyMusicHapticsMode, mode);- [NSNotificationCenter.defaultCenter postNotificationName:SGMusicHapticsModeChangedNotification object:nil];+ // What the page's switches do: store one and tell both engines.+ void (^flip)(NSString *, BOOL) = ^(NSString *key, BOOL on) {+ SGSetEnabled(key, on);+ [NSNotificationCenter.defaultCenter postNotificationName:SGMusicHapticsSwitchesChangedNotification object:nil];};void (^set)(NSInteger, NSInteger) = ^(NSInteger follows, NSInteger strength) {SGSetInt(SGKeyMusicFollows, follows);@@ -213,21 +248,46 @@ static int sg_failures;@[@"Everything at 200%", ^{ set(SGMusicFollowsEverything, 200); }, ^(double s) {[self report:@"Everything at 200%" seconds:s kicks:1 snares:1 rumble:YES intensity:NAN level:MIN(1, level100 * 2)];}],- @[@"None chosen", ^{ choose(SGMusicHapticsOff); }, ^(double s) {- [self report:@"None chosen" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];+ @[@"Music Haptics off", ^{ flip(SGKeyMusicHaptics, NO); }, ^(double s) {+ [self report:@"Music Haptics off" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];}],- @[@"Generated again, Everything, 100%", ^{+ @[@"Music Haptics on, Everything, 100%", ^{set(SGMusicFollowsEverything, 100);- choose(SGMusicHapticsGenerated);+ flip(SGKeyMusicHaptics, YES);}, ^(double s) {- [self report:@"Generated again, Everything, 100%" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];+ [self report:@"Music Haptics on, Everything, 100%" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];}],- // With Native iOS chosen the mod's stays quiet, whatever iOS does with the song.- @[@"Native iOS chosen", ^{ choose(SGMusicHapticsNative); }, ^(double s) {- [self report:@"Native iOS chosen" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];+ // In the Background on with iOS's switch off or nothing asked yet: iOS covers nothing, the mod's plays.+ @[@"both on, iOS covers nothing", ^{ flip(SGKeyMusicHapticsBackground, YES); }, ^(double s) {+ [self report:@"both on, iOS covers nothing" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];}],- @[@"Generated chosen again", ^{ choose(SGMusicHapticsGenerated); }, ^(double s) {- [self report:@"Generated chosen again" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];+ // SystemMusicHaptics.x asking about the song (Checking) or having a haptic track named (Ready).+ @[@"both on, iOS covers the song", ^{ SGMusicHapticsSetSystemCovers(YES); }, ^(double s) {+ [self report:@"both on, iOS covers the song" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];+ }],+ // A next song iOS has no haptic track for (Unavailable).+ @[@"both on, iOS has none for the next", ^{ SGMusicHapticsSetSystemCovers(NO); }, ^(double s) {+ [self report:@"both on, iOS has none for the next" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];+ }],+ // Music Haptics flipped while iOS covers: still quiet, then on again once iOS lets go.+ @[@"covered, Music Haptics off and on", ^{+ SGMusicHapticsSetSystemCovers(YES);+ flip(SGKeyMusicHaptics, NO);+ flip(SGKeyMusicHaptics, YES);+ }, ^(double s) {+ [self report:@"covered, Music Haptics off and on" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];+ }],+ @[@"In the Background alone", ^{+ SGMusicHapticsSetSystemCovers(NO);+ flip(SGKeyMusicHaptics, NO);+ }, ^(double s) {+ [self report:@"In the Background alone" seconds:s kicks:0 snares:0 rumble:NO intensity:NAN level:NAN];+ }],+ @[@"Music Haptics alone again", ^{+ flip(SGKeyMusicHapticsBackground, NO);+ flip(SGKeyMusicHaptics, YES);+ }, ^(double s) {+ [self report:@"Music Haptics alone again" seconds:s kicks:1 snares:1 rumble:YES intensity:tap100 level:level100];}],]];#endif@@ -259,8 +319,8 @@ static int sg_failures;@end-// Music Haptics on (the old switch, which reads as Generated with no choice stored), and its settings at-// their defaults, before MusicHaptics.x's constructor reads them; the redesign on, which gates it.+// Music Haptics on and its settings at their defaults, before MusicHaptics.x's constructor reads them; the+// redesign on, as in the tweak.__attribute__((constructor(101))) static void sgr_harnessDefaults(void) {NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;for (NSString *key in defaults.dictionaryRepresentation.allKeys) {diff --git a/tweak/Sources/Shared/Haptics/Haptics.h b/tweak/Sources/Shared/Haptics/Haptics.hindex 565bb22b..e43c3c5f 100644--- a/tweak/Sources/Shared/Haptics/Haptics.h+++ b/tweak/Sources/Shared/Haptics/Haptics.h@@ -1,13 +1,14 @@-// Vibrations (Mod Settings > Player > Vibrations), under either look: the Taptic Engine answering+// Vibrations (Mod Settings > Vibrations), under either look: the Taptic Engine answering// what a finger does to playback (Controls), and playing along with the music (Music Haptics).//// SGFeedback.m which tap each kind of control gets, played while Controls is on// ControlHaptics.x the player's and the now playing bar's controls, the scrubber, the cover swipes, the gestures// MusicHaptics.x Spotify's audio output listened to, and Core Haptics played along with it// SGMusicAnalyzer.m the listening: taps and a rumble out of the samples-// SystemMusicHaptics.x iOS's own Music Haptics given the song's Apple Music id or ISRC (Native iOS)+// SystemMusicHaptics.x iOS's own Music Haptics given the song's Apple Music id or ISRC (In the Background)// SGHapticTrack.m the pure steps behind it: Spotify's extended metadata asked and read, Apple's songs matched-// HapticsSettings.m the Vibrations page: its cards, with each one's strength, what Music Haptics plays and follows+// HapticsSettings.m the Vibrations page: its cards, each one's strength, what Music Haptics follows, its two+// switches, and the move to them from the one choice of before// SGVibrationsPreview.m the rings at the top of the page, which ripple and play the first feature on when tapped//// Everything on them applies at once, without a restart. Everything hooked is Spotify's own (its controls@@ -18,9 +19,12 @@#import <UIKit/UIKit.h>#define SGKeyControlHaptics @"spotifyglass.haptics.controls"-// The Music Haptics switch of before the choice below; read once to pick the choice for someone who had it.+// Music Haptics, the mod's own, worked out from the sound while Spotify is in front. Off until switched on.#define SGKeyMusicHaptics @"spotifyglass.haptics.music"-// Which Music Haptics plays, an SGMusicHapticsMode.+// In the Background: the song named to iOS's own Music Haptics, which plays on the lock screen and in other apps+// too. Off until switched on.+#define SGKeyMusicHapticsBackground @"spotifyglass.haptics.music.background"+// The one choice of before the two switches, None 0, Generated 1 or Native iOS 2; SGMigrateMusicHaptics moves it.#define SGKeyMusicHapticsMode @"spotifyglass.haptics.music.mode"// How hard the taps are, a percentage within the range below; 100 is the feel each shipped with.#define SGKeyControlStrength @"spotifyglass.haptics.controls.strength"@@ -48,13 +52,6 @@ typedef NS_ENUM(NSInteger, SGMusicFollows) {SGMusicFollowsBass, // a tap on each kick, and the rumble};-// Stored as the index into the settings' list, which leaves the last one out below iOS 18.-typedef NS_ENUM(NSInteger, SGMusicHapticsMode) {- SGMusicHapticsOff,- SGMusicHapticsGenerated, // the mod's, worked out from the sound (MusicHaptics.x)- SGMusicHapticsNative, // iOS's own, iOS 18 and later (SystemMusicHaptics.x)-};-typedef NS_ENUM(NSInteger, SGFeedback) {SGFeedbackPlay, // playback startsSGFeedbackPause, // playback stops@@ -72,22 +69,35 @@ void SGPlayFeedback(SGFeedback feedback);// Wakes the Taptic Engine for feedback about to follow quickly (a finger on the scrubber).void SGPrepareFeedback(SGFeedback feedback);-// Which Music Haptics plays. Native reads Off below iOS 18; nothing stored yet reads the old switch, on as-// Generated. Main thread.-SGMusicHapticsMode SGMusicHapticsModeNow(void);-// Posted on the main thread when the choice changes; both engines take it up at once.-extern NSNotificationName const SGMusicHapticsModeChangedNotification;+// The two Music Haptics switches. Both may be on: while iOS plays its haptic track for the song (In the+// Background), the mod's own stays quiet; while Spotify is in front and iOS has none, the mod's own plays.+// In the Background reads off below iOS 18, which has no Music Haptics. Main thread.+BOOL SGMusicHapticsOn(void);+BOOL SGMusicHapticsInBackground(void);+// The choice of before moved to the switches, once: Generated turns Music Haptics on, Native iOS In the+// Background, None neither. Each reader of the switches calls it first, since none can know it runs first.+void SGMigrateMusicHaptics(void);+// Posted on the main thread when either switch flips; both engines take it up at once.+extern NSNotificationName const SGMusicHapticsSwitchesChangedNotification;+// From SystemMusicHaptics.x: YES while it finds out whether iOS has a haptic track for the song and while iOS+// has one named, so the mod's own stays quiet and the two never play at once. Main thread.+void SGMusicHapticsSetSystemCovers(BOOL covers);// From its strength and its choice of what to follow: reads them again, for the next tap.void SGMusicHapticsSettingsChanged(void);-// One kick of Generated's at its strength, with the rumble under it unless it follows the beat alone, played-// at once: the Vibrations preview's tap. Nothing while Generated is not listening.+// One kick of the mod's own at its strength, with the rumble under it unless it follows the beat alone, played+// at once: the Vibrations preview's tap. Nothing while the mod's own is not listening.void SGMusicHapticsPreview(void);-// Each tap Generated plays from now on is handed to `watcher` on the main thread as it is felt, with its+// Each tap the mod's own plays from now on is handed to `watcher` on the main thread as it is felt, with its// intensity before the strength, 0 to 1; nil stops it. Main thread.void SGMusicHapticsWatchTaps(void (^watcher)(float intensity));// What iOS's own Music Haptics is doing, for the settings to read out: SystemMusicHaptics.x sets it. Main thread.NSString *SGMusicHapticsStatus(void);void SGSetMusicHapticsStatus(NSString *status);+// Whether Music Haptics is on in Settings > Accessibility, as SystemMusicHaptics.x last read it, YES until it has.+// A change posts SGSystemMusicHapticsChangedNotification, for the page to show or hide its note. Main thread.+BOOL SGSystemMusicHapticsOn(void);+void SGSetSystemMusicHapticsOn(BOOL on);+extern NSNotificationName const SGSystemMusicHapticsChangedNotification;// A strength key's percentage as a factor, 1 for 100%, kept within its range.double SGHapticsStrength(NSString *key);diff --git a/tweak/Sources/Shared/Haptics/HapticsSettings.m b/tweak/Sources/Shared/Haptics/HapticsSettings.mindex db02f25d..7995987e 100644--- a/tweak/Sources/Shared/Haptics/HapticsSettings.m+++ b/tweak/Sources/Shared/Haptics/HapticsSettings.m@@ -1,20 +1,26 @@-// The Vibrations page, under either look (App/Pages.m links it from the Player page): the preview at the+// The Vibrations page, under either look (App/ModSettings.x links it from the main page): the preview at the// top (SGVibrationsPreview.m), then a card per kind, the way the Audio effects page has one per effect.-// Controls is a switch that opens out into its strength. Music Haptics is a choice of which plays, None,-// Generated or Native iOS, since the two never play at once: Generated opens out into its strength and what-// it follows (a choice that also says whether the rumble plays, rather than a switch of its own that one-// choice would leave with nothing to do), Native into what iOS's own is doing. Each strength's slider plays-// a tap at the new strength with each step, and the preview ripples with it.+// Controls is a switch with its strength under it. Music Haptics is two switches in one card that work together:+// Music Haptics itself, the mod's own from the sound while Spotify is in front, with its strength and what it+// follows (a choice that also says whether the rumble plays, rather than a switch of its own that one choice would+// leave with nothing to do) under it; and In the Background, which names the song to iOS's own Music Haptics, with+// what iOS's own is doing under it, or, while Music Haptics is off in Settings > Accessibility, a row saying so.+// The rows under a switch stay where they are, greyed, while it is off. Each strength's slider plays a tap at the+// new strength with each step, and the preview ripples with it.#import "Core/SGCore.h"#import "Settings/SGModPage.h"#import "Haptics.h"#import "SGVibrationsPreview.h"-static NSString *const kMusicHapticsInfo = @"Generated: the iPhone taps along with the drums and rumbles under the bass of whatever Spotify is playing, worked out from the sound as it plays. It follows the sound this iPhone plays while Spotify is open: iOS plays no haptics for an app in the background, and a song playing on another device through Connect has no sound here to follow.\n\nNative iOS: iOS plays Apple Music's own haptic track for the song, in the background and on the lock screen too, for songs Apple Music has one for. It needs iOS 18 and Music Haptics on in Settings > Accessibility, and stays quiet for podcasts and songs Apple has none for.\n\nNone: no Music Haptics. Controls is separate.";+static NSString *const kMusicInfo = @"The iPhone taps along with the drums and rumbles under the bass, worked out from the sound as Spotify plays it. Only while Spotify is open: iOS stops an app's own haptics in the background, and a song playing on another device through Connect has no sound here to follow.\n\nWith In the Background on too, iOS plays its own haptic track for the songs it has one for, and this waits, so the two never double up.";-NSNotificationName const SGMusicHapticsModeChangedNotification = @"SGMusicHapticsModeChangedNotification";+static NSString *const kBackgroundInfo = @"Keeps the vibrations going on the lock screen and in other apps, where iOS stops Spotify's own. The mod tells iOS which recording is playing, by its ISRC code from Apple Music's catalog (no sound leaves the phone), and iOS's own Music Haptics, the one Apple Music uses, plays Apple's haptic track for it.\n\nIt needs Settings > Accessibility > Music Haptics on, and only songs Apple has a haptic track for vibrate. While Spotify is open, Music Haptics above plays the others, if it is on.";++NSNotificationName const SGMusicHapticsSwitchesChangedNotification = @"SGMusicHapticsSwitchesChangedNotification";+NSNotificationName const SGSystemMusicHapticsChangedNotification = @"SGSystemMusicHapticsChangedNotification";static NSString *sg_status;+static BOOL sg_systemOff;NSString *SGMusicHapticsStatus(void) {return sg_status ?: @"Waiting";@@ -24,28 +30,39 @@ void SGSetMusicHapticsStatus(NSString *status) {sg_status = [status copy];}-static BOOL nativeAvailable(void) {+BOOL SGSystemMusicHapticsOn(void) {+ return !sg_systemOff;+}++void SGSetSystemMusicHapticsOn(BOOL on) {+ if (sg_systemOff == !on) return;+ sg_systemOff = !on;+ [NSNotificationCenter.defaultCenter postNotificationName:SGSystemMusicHapticsChangedNotification object:nil];+}++static BOOL backgroundAvailable(void) {if (@available(iOS 18.0, *)) return YES;return NO;}-SGMusicHapticsMode SGMusicHapticsModeNow(void) {- NSInteger mode = SGInt(SGKeyMusicHapticsMode, -1);- if (mode < SGMusicHapticsOff || mode > SGMusicHapticsNative) mode = SGFlag(SGKeyMusicHaptics, NO) ? SGMusicHapticsGenerated : SGMusicHapticsOff;- if (mode == SGMusicHapticsNative && !nativeAvailable()) mode = SGMusicHapticsOff;- return (SGMusicHapticsMode)mode;+BOOL SGMusicHapticsOn(void) {+ return SGFlag(SGKeyMusicHaptics, NO);}-// In SGMusicHapticsMode's order, Native iOS left out where iOS has none.-static NSArray<NSString *> *modeNames(void) {- NSArray<NSString *> *names = @[@"None", @"Generated", @"Native iOS"];- return nativeAvailable() ? names : [names subarrayWithRange:NSMakeRange(0, 2)];+BOOL SGMusicHapticsInBackground(void) {+ return backgroundAvailable() && SGFlag(SGKeyMusicHapticsBackground, NO);}-static NSArray<NSString *> *modeNotes(void) {- return @[@"No Music Haptics",- @"The mod's own, from the sound, while Spotify is open",- @"Apple Music's haptic track, in the background too"];+void SGMigrateMusicHaptics(void) {+ // The redesign's old key first: SGMigrateKey leaves it where it is once the new one exists.+ SGMigrateKey(SGKeyMusicHapticsWas, SGKeyMusicHaptics);+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;+ if (![store objectForKey:SGKeyMusicHapticsMode]) return;+ NSInteger mode = SGInt(SGKeyMusicHapticsMode, 0);+ SGSetEnabled(SGKeyMusicHaptics, mode == 1);+ SGSetEnabled(SGKeyMusicHapticsBackground, mode == 2);+ [store removeObjectForKey:SGKeyMusicHapticsMode];+ SGLog(@"music haptics: the choice %ld is now Music Haptics %@, In the Background %@", (long)mode, mode == 1 ? @"on" : @"off", mode == 2 ? @"on" : @"off");}static NSArray<NSString *> *followsNames(void) {@@ -88,8 +105,10 @@ static SGModRow *strengthRow(NSString *key, void (^changed)(void)) {^NSString *(double value) { return [NSString stringWithFormat:@"%ld%%", lround(value)]; });}-// The cards, `preview` told when something it reads out changes and shown the slider's taps.-static NSArray<SGModSection *> *sections(SGVibrationsPreview *preview) {+// The cards, `preview` told when something it reads out changes and shown the slider's taps, `explain` run by a+// tap on the row that says iOS's Music Haptics is off.+static NSArray<SGModSection *> *sections(SGVibrationsPreview *preview, void (^explain)(void)) {+ SGMigrateMusicHaptics();__weak SGVibrationsPreview *weakPreview = preview;SGModRow *controls = SGSwitchRow(@"Controls", nil, SGKeyControlHaptics);controls.changed = ^(BOOL on) { [weakPreview reload]; };@@ -100,33 +119,38 @@ static NSArray<SGModSection *> *sections(SGVibrationsPreview *preview) {});controlStrength.waitsOn = SGKeyControlHaptics;- // The list reads the stored index, so someone with none stored gets the one picked for them.- if (SGInt(SGKeyMusicHapticsMode, -1) < 0) SGSetInt(SGKeyMusicHapticsMode, SGMusicHapticsModeNow());- SGModRow *music = SGChoiceRow(@"Music Haptics", nil, SGKeyMusicHapticsMode, modeNames(), SGMusicHapticsOff);- music.info = kMusicHapticsInfo;- music.choiceNotes = modeNotes();- music.chosen = ^(NSInteger index) {- [NSNotificationCenter.defaultCenter postNotificationName:SGMusicHapticsModeChangedNotification object:nil];+ void (^switched)(BOOL) = ^(BOOL on) {+ [NSNotificationCenter.defaultCenter postNotificationName:SGMusicHapticsSwitchesChangedNotification object:nil];[weakPreview reload];};- BOOL (^generated)(void) = ^BOOL { return SGMusicHapticsModeNow() == SGMusicHapticsGenerated; };+ SGModRow *music = SGOptionRow(@"Music Haptics", nil, SGKeyMusicHaptics);+ music.info = kMusicInfo;+ music.changed = switched;SGModRow *musicStrength = strengthRow(SGKeyMusicStrength, ^{// As Controls' Strength: a kick at the new strength with each step, and its ripple.SGMusicHapticsSettingsChanged();SGMusicHapticsPreview();[weakPreview rippleAt:SGHapticsStrength(SGKeyMusicStrength) / 2];});- musicStrength.visible = generated;+ musicStrength.waitsOn = SGKeyMusicHaptics;SGModRow *follows = SGChoiceRow(@"Follows", nil, SGKeyMusicFollows, followsNames(), SGMusicFollowsEverything);follows.choiceNotes = followsNotes();follows.chosen = ^(NSInteger index) { SGMusicHapticsSettingsChanged(); };- follows.visible = generated;+ follows.waitsOn = SGKeyMusicHaptics;++ SGModRow *background = SGOptionRow(@"In the Background", nil, SGKeyMusicHapticsBackground);+ background.info = kBackgroundInfo;+ background.changed = switched;+ background.visible = ^BOOL { return backgroundAvailable(); };+ // What iOS's own is doing, or, while it is off in Accessibility, a row saying where to turn it on.SGModRow *status = SGStatRow(@"Status", ^NSString *{ return SGMusicHapticsStatus(); });- status.visible = ^BOOL { return SGMusicHapticsModeNow() == SGMusicHapticsNative; };+ status.visible = ^BOOL { return SGMusicHapticsInBackground() && SGSystemMusicHapticsOn(); };+ SGModRow *systemOff = SGActionRow(@"Music Haptics is off in iOS", @"Tap to turn it on in Settings", explain);+ systemOff.visible = ^BOOL { return SGMusicHapticsInBackground() && !SGSystemMusicHapticsOn(); };return @[SGSection(nil, @[SGWithSymbol(controls, @"hand.tap"), controlStrength]),- SGSection(nil, @[SGWithSymbol(music, @"waveform"), musicStrength, follows, status]),+ SGSection(nil, @[SGWithSymbol(music, @"waveform"), musicStrength, follows, background, status, systemOff]),];}@@ -141,6 +165,12 @@ static NSArray<SGModSection *> *sections(SGVibrationsPreview *preview) {- (void)viewDidLoad {[super viewDidLoad];self.tableView.tableHeaderView = self.preview;+ // Music Haptics turned on or off in Accessibility while the page is open, or before coming back to it.+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(systemMusicHapticsChanged:) name:SGSystemMusicHapticsChangedNotification object:nil];+}++- (void)systemMusicHapticsChanged:(NSNotification *)note {+ [self refreshVisibility];}// The header keeps the height it is given, so it is sized here and handed back to the table only when that@@ -171,17 +201,35 @@ static NSArray<SGModSection *> *sections(SGVibrationsPreview *preview) {self.preview.listening = NO;}+// The row under In the Background that says iOS's Music Haptics is off: what it needs, and the way there.+- (void)explainBackground {+ // iOS opens no Settings page but an app's own from a third-party app (App-prefs: links fail on iOS 27), so the+ // message gives the path from there.+ NSString *message = @"In the Background needs iOS's Music Haptics. Settings opens on Spotify's page: go back to Settings, then Accessibility > Music Haptics, and turn it on.";+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Turn On Music Haptics" message:message preferredStyle:UIAlertControllerStyleAlert];+ [alert addAction:[UIAlertAction actionWithTitle:@"Not Now" style:UIAlertActionStyleCancel handler:nil]];+ UIAlertAction *open = [UIAlertAction actionWithTitle:@"Open Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {+ [UIApplication.sharedApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];+ }];+ [alert addAction:open];+ alert.preferredAction = open;+ [self presentViewController:alert animated:YES completion:nil];+}+@endUIViewController *SGVibrationsSettingsPage(void) {SGVibrationsPreview *preview = [SGVibrationsPreview new];- SGVibrationsPage *page = [[SGVibrationsPage alloc] initWithTitle:@"Vibrations" intro:nil sections:sections(preview) footer:nil];+ __block __weak SGVibrationsPage *weakPage;+ NSArray<SGModSection *> *cards = sections(preview, ^{ [weakPage explainBackground]; });+ SGVibrationsPage *page = [[SGVibrationsPage alloc] initWithTitle:@"Vibrations" intro:nil sections:cards footer:nil];page.preview = preview;+ weakPage = page;return page;}NSString *SGVibrationsSummary(void) {- BOOL controls = SGEnabled(SGKeyControlHaptics), music = SGMusicHapticsModeNow() != SGMusicHapticsOff;+ BOOL controls = SGEnabled(SGKeyControlHaptics), music = SGMusicHapticsOn() || SGMusicHapticsInBackground();if (controls && music) return @"On";return controls ? @"Controls" : music ? @"Music Haptics" : @"Off";}diff --git a/tweak/Sources/Shared/Haptics/MusicHaptics.x b/tweak/Sources/Shared/Haptics/MusicHaptics.xindex 26d514b7..10867374 100644--- a/tweak/Sources/Shared/Haptics/MusicHaptics.x+++ b/tweak/Sources/Shared/Haptics/MusicHaptics.x@@ -19,15 +19,17 @@// The Vibrations page's preview asks for one kick, which the same thread plays at once through the same// engine and strength, and while the page shows, each tap played is handed back to it for its rings.//-// The rebinding and the notify are in place under either look, so the choice works at once; with-// anything but Generated chosen the notify returns straight away. Nothing listens while Spotify is not the-// active app, since iOS plays no haptics for an app in the background. Sound that is not Spotify's own-// output (Connect, AirPlay to another device, video) never passes the unit, and plays no haptics.-// The choice is read again every time listening could start, not kept from launch, so with Native iOS-// chosen (SystemMusicHaptics.x) this one stays quiet whatever order the two came up in.+// The rebinding and the notify are in place under either look, so the switch works at once; with it+// off the notify returns straight away. Nothing listens while Spotify is not the active app, since iOS+// plays no haptics for an app in the background. Sound that is not Spotify's own output (Connect,+// AirPlay to another device, video) never passes the unit, and plays no haptics.+// Nothing listens either while In the Background (SystemMusicHaptics.x) is finding out whether iOS has a+// haptic track for the song, or has one named: iOS plays it in front of Spotify as well as behind it, so+// the two would double up. The switch is read again every time listening could start, not kept from+// launch, so this one is right whatever order the two came up in.//// Threading: the notify runs on the render thread and only touches atomics, the analyzer and the-// ring; the Core Haptics objects belong to the player thread; the choice, its settings and the app's+// ring; the Core Haptics objects belong to the player thread; the switches, the settings and the app's// state are set on the main thread.#import <AudioToolbox/AudioToolbox.h>#import <AVFoundation/AVFoundation.h>@@ -64,7 +66,7 @@ enum { kRingSize = 1024, kMonoFrames = 4096 };#pragma mark - shared between the threads-static atomic_bool sg_active, sg_listening;+static atomic_bool sg_active, sg_systemCovers, sg_listening;static atomic_uint sg_generation;static atomic_uint_fast64_t sg_latencyBits;static atomic_uint sg_lastFrames;@@ -535,7 +537,7 @@ static void readLatency(void) {}static void updateListening(void) {- BOOL listening = SGMusicHapticsModeNow() == SGMusicHapticsGenerated && atomic_load(&sg_active);+ BOOL listening = SGMusicHapticsOn() && atomic_load(&sg_active) && !atomic_load(&sg_systemCovers);if (atomic_exchange(&sg_listening, listening) == listening) return;if (listening) {atomic_fetch_add(&sg_generation, 1);@@ -553,6 +555,12 @@ static void updateListening(void) {dispatch_semaphore_signal(sg_wake);}+void SGMusicHapticsSetSystemCovers(BOOL covers) {+ if (atomic_exchange(&sg_systemCovers, covers) == covers || !sg_wake) return;+ if (SGMusicHapticsOn()) SGLog(@"music haptics: %@", covers ? @"iOS's own has this song, standing down" : @"iOS's own has none for this song, playing along");+ updateListening();+}+static void readSettings(void) {storeDouble(&sg_strengthBits, SGHapticsStrength(SGKeyMusicStrength));atomic_store(&sg_follows, (int)SGMusicHapticsFollows());@@ -577,7 +585,7 @@ void SGMusicHapticsWatchTaps(void (^watcher)(float intensity)) {}%ctor {- SGMigrateKey(SGKeyMusicHapticsWas, SGKeyMusicHaptics);+ SGMigrateMusicHaptics();SGMigrateKey(SGKeyMusicStrengthWas, SGKeyMusicStrength);SGMigrateKey(SGKeyMusicFollowsWas, SGKeyMusicFollows);mach_timebase_info_data_t timebase;@@ -590,7 +598,7 @@ void SGMusicHapticsWatchTaps(void (^watcher)(float intensity)) {sg_wake = dispatch_semaphore_create(0);readSettings();NSNotificationCenter *center = NSNotificationCenter.defaultCenter;- [center addObserverForName:SGMusicHapticsModeChangedNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {+ [center addObserverForName:SGMusicHapticsSwitchesChangedNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {updateListening();}];[center addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {diff --git a/tweak/Sources/Shared/Haptics/SGHapticTrack.h b/tweak/Sources/Shared/Haptics/SGHapticTrack.hindex 718bb59d..ace158d1 100644--- a/tweak/Sources/Shared/Haptics/SGHapticTrack.h+++ b/tweak/Sources/Shared/Haptics/SGHapticTrack.h@@ -1,4 +1,4 @@-// The pure steps of Native iOS Music Haptics (SystemMusicHaptics.x), Foundation only so the Mac harness+// The pure steps of Music Haptics In the Background (SystemMusicHaptics.x), Foundation only so the Mac harness// (harness/haptics/track.m) runs them as the tweak does.//// Spotify's ISRC comes from its own metadata, asked as the app asks it: a BatchedEntityRequest to spclient'sdiff --git a/tweak/Sources/Shared/Haptics/SGVibrationsPreview.m b/tweak/Sources/Shared/Haptics/SGVibrationsPreview.mindex bbdfaf08..ffc4467c 100644--- a/tweak/Sources/Shared/Haptics/SGVibrationsPreview.m+++ b/tweak/Sources/Shared/Haptics/SGVibrationsPreview.m@@ -30,25 +30,21 @@ static const CGFloat kPulse = 0.32;typedef NS_ENUM(NSInteger, Feature) {FeatureControls,FeatureMusic,- FeatureNative, // iOS plays Music Haptics itself: nothing to play from here+ FeatureNative, // only In the Background: iOS plays it, nothing to play from hereFeatureNone,};static Feature featureNow(void) {if (SGEnabled(SGKeyControlHaptics)) return FeatureControls;- switch (SGMusicHapticsModeNow()) {- case SGMusicHapticsGenerated: return FeatureMusic;- case SGMusicHapticsNative: return FeatureNative;- case SGMusicHapticsOff: return FeatureNone;- }- return FeatureNone;+ if (SGMusicHapticsOn()) return FeatureMusic;+ return SGMusicHapticsInBackground() ? FeatureNative : FeatureNone;}static NSString *captionFor(Feature feature) {switch (feature) {case FeatureControls: return @"Tap to feel Controls";case FeatureMusic: return @"Tap to feel Music Haptics";- case FeatureNative: return @"Native iOS plays on its own. Turn on Controls to feel a tap";+ case FeatureNative: return @"In the Background is played by iOS. Turn on Controls or Music Haptics to feel a tap";case FeatureNone: return @"Turn on Controls or Music Haptics to feel a tap";}return nil;diff --git a/tweak/Sources/Shared/Haptics/SystemMusicHaptics.x b/tweak/Sources/Shared/Haptics/SystemMusicHaptics.xindex 83f5d14d..3c5ba81d 100644--- a/tweak/Sources/Shared/Haptics/SystemMusicHaptics.x+++ b/tweak/Sources/Shared/Haptics/SystemMusicHaptics.x@@ -1,4 +1,4 @@-// Music Haptics > Native iOS: iOS's own Music Haptics for Spotify. The system plays Apple Music's haptic track+// Music Haptics > In the Background: iOS's own Music Haptics for Spotify. The system plays Apple Music's haptic track// for a recording, in the background and on the lock screen too, for an app that declares// MusicHapticsSupported (plist/liquid-glass.plist) and names the recording in its now playing info. Spotify// names none, so the mod adds one to Spotify's info (Shared/Player/NowPlayingExtras.h), exactly one:@@ -12,12 +12,23 @@// cannot find showed "Music Haptics unavailable" over the matched song playing (iOS 27.2).// 3. Failing a song, the ISRC itself, and iOS asked whether it has a haptic track for it.//-// A new track or a new choice takes the old identifier off at once, before anything is asked, and every+// A new track or a flipped switch takes the old identifier off at once, before anything is asked, and every// answer carries the revision it was asked under, so one for an earlier track is dropped. Nothing is-// published while Music Haptics is off in Settings > Accessibility, or with another choice than Native iOS.+// published while Music Haptics is off in Settings > Accessibility, or with In the Background off.// iOS repeats its notifications as the now playing info changes; only a change in whether it is on or// paused is acted on, since sending the info again on each would feed back into the system service.//+// The handover to the mod's own (MusicHaptics.x): iOS plays the haptic track of the now playing song+// whether the app is in front or not (MAMusicHapticsManager reports its status per now playing song, with no+// foreground or background to it), so the mod's own stands down for the song entirely, from the moment the+// track is asked about (Checking) through an identifier with a haptic track named (Ready), paused from+// Control Center or not. It plays again once the answer is that iOS has none (Unavailable), or with nothing+// asked (Waiting: In the Background off, iOS's switch off, nothing playing). Ready is taken on the catalog's+// word or iOS's availability check, not the status observer's report that iOS plays it, since when that+// observer fires for an app's song is unproven: waiting on it could leave both playing.+// ponytail: quiet while Checking means a song with no haptic track gets the mod's own only once the lookup+// answers, a moment with the ISRC kept, up to kSpotifyTries * kSpotifyRetryAfter when Spotify does not answer.+//// ponytail: the identifier rides on Spotify's info by title, as NowPlayingExtras matches it, so a next track// with the same title can carry the last one's for the moment before the player reports it. Matching by the// info's external content identifier would close that, in NowPlayingExtras for every owner.@@ -148,6 +159,8 @@ API_AVAILABLE(ios(18.0))- (void)setPhase:(SGNativePhase)phase {_phase = phase;+ // follow: leaves Waiting only with In the Background on, iOS's switch on and a track playing.+ SGMusicHapticsSetSystemCovers(phase == SGNativeChecking || phase == SGNativeReady);[self showStatus];}@@ -169,7 +182,7 @@ API_AVAILABLE(ios(18.0))if (logged++ < 3) SGLog(@"music haptics: %@ named for iOS's Music Haptics", extras.allValues.firstObject);}-// `again` follows the same track anew: the choice, or iOS's switch, changed.+// `again` follows the same track anew: a switch, or iOS's, changed.- (void)follow:(SPTPlayerState *)state again:(BOOL)again {NSString *track = SGURIString(state.track.URI);if (!again && (track == _track || [track isEqualToString:_track])) return;@@ -180,7 +193,7 @@ API_AVAILABLE(ios(18.0))_observedActive = NO;// Off before anything is asked, so no answer can arrive while the last track's is still named.SGNowPlayingSetExtras(@"haptics", nil, nil);- if (SGMusicHapticsModeNow() != SGMusicHapticsNative || !_enabled || !track) {+ if (!SGMusicHapticsInBackground() || !_enabled || !track) {self.phase = SGNativeWaiting;return;}@@ -248,6 +261,7 @@ API_AVAILABLE(ios(18.0))_known = YES;_enabled = enabled;_active = active;+ SGSetSystemMusicHapticsOn(enabled);SGLog(@"music haptics: iOS's Music Haptics %@, %@", enabled ? @"on" : @"off", active ? @"active" : @"paused");[self observe];if (first || enabled != wasEnabled) [self follow:SGPlayerState() again:YES];@@ -256,22 +270,9 @@ API_AVAILABLE(ios(18.0))@end-// For someone with no choice stored: Native iOS when iOS's Music Haptics is on, as the mod named songs to it-// before there was a choice, else what the old switch says. A reset leaves it unpicked, which reads None.-static void pickMode(void) {- // MusicHaptics.x's constructor moves this too, but may run after this one.- SGMigrateKey(SGKeyMusicHapticsWas, SGKeyMusicHaptics);- if (SGInt(SGKeyMusicHapticsMode, -1) >= 0 || [NSUserDefaults.standardUserDefaults boolForKey:SGKeyStock]) return;- SGMusicHapticsMode mode = SGMusicHapticsModeNow();- if (@available(iOS 18.0, *)) {- if (systemEnabled(MAMusicHapticsManager.sharedManager)) mode = SGMusicHapticsNative;- }- SGSetInt(SGKeyMusicHapticsMode, mode);- SGLog(@"music haptics: picked %ld for the choice", (long)mode);-}-%ctor {- pickMode();+ // MusicHaptics.x's constructor moves it too, but may run after this one.+ SGMigrateMusicHaptics();if (@available(iOS 18.0, *)) {sg_isrcs = [NSCache new];sg_isrcs.countLimit = kKeptISRCs;@@ -286,7 +287,11 @@ static void pickMode(void) {id name = enabledName ? (__bridge id)*(void **)enabledName : nil;if ([name isKindOfClass:NSString.class]) [center addObserverForName:name object:nil queue:NSOperationQueue.mainQueue usingBlock:changed];[center addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:changed];- [center addObserverForName:SGMusicHapticsModeChangedNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {+ // Only In the Background's own flip: Music Haptics' would take iOS's identifier off for nothing.+ __block BOOL background = SGMusicHapticsInBackground();+ [center addObserverForName:SGMusicHapticsSwitchesChangedNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {+ if (SGMusicHapticsInBackground() == background) return;+ background = !background;[native follow:SGPlayerState() again:YES];}];dispatch_async(dispatch_get_main_queue(), ^{ [native systemChanged]; });