Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: nothing of the mod ticks against a screen that is off

Vojtěch Škopek0685135d669f
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index a4100a0d..5a27245b 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -977,6 +977,10 @@ typedef struct {
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionEndedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGRLyricsTextDidChangeNotification object:nil];
+ // A locked phone leaves the card in its window, so the link has to be put down by the app going
+ // away rather than by the view going: see scheduleLink.
+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(scheduleLink) name:UIApplicationDidBecomeActiveNotification object:nil];
+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(scheduleLink) name:UIApplicationWillResignActiveNotification object:nil];
return self;
}
@@ -1045,6 +1049,11 @@ typedef struct {
// link now asks for 60 but takes 120, and is put down for as long as the player animates, which
// NowPlayingBar.x announces as each animation starts and again as it ends. The timer is for an end
// that is never announced.
+//
+// The link is also put down whenever the app is not in front. Spotify keeps playing there, and a
+// phone locked on the player kept the card ticking at 120 Hz against a screen that was off: 117 of
+// the 137 wake ups a second the mod cost over stock Spotify were this one link, measured with
+// scripts/battery-probe.sh. Nothing of the card is seen from the background, so nothing is drawn.
- (void)scheduleLink {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startLink) object:nil];
[_link invalidate];
@@ -1057,6 +1066,7 @@ typedef struct {
- (void)startLink {
if (!self.window || _link) return;
+ if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) return;
_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
_link.preferredFrameRateRange = CAFrameRateRangeMake(80, 120, 120);
[_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
diff --git a/tweak/Sources/Shared/JamesDSP/JamesDSP.x b/tweak/Sources/Shared/JamesDSP/JamesDSP.x
index ba2eee29..359205b2 100644
--- a/tweak/Sources/Shared/JamesDSP/JamesDSP.x
+++ b/tweak/Sources/Shared/JamesDSP/JamesDSP.x
@@ -480,10 +480,25 @@ static void tend(void) {
}
}
+// The engine is read out once a second while it runs. With the switch off there is nothing in it to
+// read, so the source is suspended rather than left ticking for the rest of the app's life: the engine
+// is never freed, and a timer tied to its making would outlive every use of it. Only ever touched from
+// applyMaster, which is to say on applyQueue().
+static dispatch_source_t sg_tendTimer;
+static BOOL sg_tending;
+
+static void setTending(BOOL on) {
+ if (!sg_tendTimer || on == sg_tending) return;
+ sg_tending = on;
+ if (on) dispatch_resume(sg_tendTimer);
+ else dispatch_suspend(sg_tendTimer);
+}
+
// The switch and the output's format: the engine made or moved to the output's rate, everything set when
// it was, and the notify let in or kept out. Answers whether every effect was set.
static BOOL applyMaster(void) {
if (!SGDSPSwitch(SGKeyDSP)) {
+ setTending(NO);
if (atomic_exchange(&sg_running, false)) SGLog(@"jamesdsp: off, Spotify's sound passes as it is");
return NO;
}
@@ -503,13 +518,12 @@ static BOOL applyMaster(void) {
SGLog(@"jamesdsp: engine made at %.0f Hz, blocks of %d frames, so the sound is %.1f ms later", rate, kSGDSPEngineBlock,
kSGDSPEngineBlock / rate * 1000);
atomic_store(&sg_engine, engine);
- static dispatch_source_t timer;
- timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, applyQueue());
- dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, NSEC_PER_SEC / 4);
- dispatch_source_set_event_handler(timer, ^{
+ // A source comes up suspended; setTending below is what starts it.
+ sg_tendTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, applyQueue());
+ dispatch_source_set_timer(sg_tendTimer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, NSEC_PER_SEC / 4);
+ dispatch_source_set_event_handler(sg_tendTimer, ^{
tend();
});
- dispatch_resume(timer);
everything = YES;
} else if (SGDSPEngineSampleRate(engine) != rate) {
SGDSPEngineSetSampleRate(engine, rate);
@@ -523,6 +537,7 @@ static BOOL applyMaster(void) {
atomic_store_explicit(&sg_running, true, memory_order_release);
SGLog(@"jamesdsp: on");
}
+ setTending(YES);
return everything;
}
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
index fbe13178..1db5bc0d 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
@@ -28,6 +28,10 @@ typedef NS_ENUM(NSInteger, SGLiveActivityTab) {
};
static const NSTimeInterval kTick = 0.25;
+// Paused, the only thing on the card that still moves is the sleep timer, and a slower tick catches
+// its end well within the second the card shows. Four ticks a second for a still card is a wake up
+// eighty times a minute for a picture that does not change.
+static const NSTimeInterval kPausedTick = 1;
// Past a line's sung end by this much, with the next line at least this far off, the line gives way to a note.
static const NSInteger kBreakMs = 4000;
// Seconds between attempts to start one, so a refused request (activities turned off) is not retried every tick.
@@ -40,6 +44,7 @@ static const NSUInteger kUpNextPanel = 3;
static const NSInteger kEndOfTrackMs = 500;
static NSTimer *sg_timer;
+static NSTimeInterval sg_tickEvery;
static NSString *sg_shown;
static NSString *sg_missingLyrics;
static NSDate *sg_lastStart;
@@ -48,6 +53,16 @@ static SGLiveActivityTab sg_tab;
static NSDate *sg_sleepEnd;
static NSString *sg_sleepTrack;
+static void tick(void) API_AVAILABLE(ios(17.0));
+
+// An NSTimer cannot be asked to change pace, so changing it is putting one down and another up.
+static void startTimer(NSTimeInterval every) API_AVAILABLE(ios(17.0)) {
+ [sg_timer invalidate];
+ sg_tickEvery = every;
+ sg_timer = [NSTimer timerWithTimeInterval:every repeats:YES block:^(NSTimer *t) { tick(); }];
+ [NSRunLoop.mainRunLoop addTimer:sg_timer forMode:NSRunLoopCommonModes];
+}
+
// Sends `shown`, what the activity is to show as one string, unless it is showing that already; starts
// the activity when there is none and the app is in front.
static void send(NSString *shown, void (^show)(void)) API_AVAILABLE(ios(17.0)) {
@@ -140,6 +155,8 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {
NSInteger view = SGInt(SGKeyLiveActivityView, SGLiveActivityLyrics);
BOOL paused = state.isPaused;
+ NSTimeInterval every = paused ? kPausedTick : kTick;
+ if (sg_timer && sg_tickEvery != every) startTimer(every);
NSString *line = @"", *next = @"";
if (view == SGLiveActivityLyrics) line = lyricsLine(trackID, &next);
@@ -260,8 +277,7 @@ void SGSetLiveActivityEnabled(BOOL on) {
if (sg_timer) tick();
}];
});
- sg_timer = [NSTimer timerWithTimeInterval:kTick repeats:YES block:^(NSTimer *t) { tick(); }];
- [NSRunLoop.mainRunLoop addTimer:sg_timer forMode:NSRunLoopCommonModes];
+ startTimer(kTick);
SGLog(@"live activity: on");
}
}
diff --git a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
index 3fbb561d..1bda0299 100644
--- a/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
+++ b/tweak/Sources/Shared/LockScreenLyrics/LockScreenLyrics.x
@@ -1,5 +1,9 @@
// Spotify's now playing info passes through with the artist swapped for the current line, and a timer
// sends it again each time the line changes. Spotify itself only ever sees its own dictionary back.
+//
+// The timer follows the playback rate rather than running from launch: a paused player's line does not
+// move, so working it out four times a second only wakes the phone. The lock screen keeps the line it
+// was left on until the sound starts again.
#import <MediaPlayer/MediaPlayer.h>
#import "Core/SGCore.h"
#import "LockScreenLyrics.h"
@@ -18,6 +22,7 @@ static NSDictionary *sg_spotifyInfo;
static CFAbsoluteTime sg_spotifyInfoAt;
static NSString *sg_shownLine;
static BOOL sg_resending;
+static NSTimer *sg_timer;
static NSString *textOf(NSArray<SGKaraokeWord *> *words) {
SGKaraokeLine *line = [SGKaraokeLine new];
@@ -103,6 +108,25 @@ static void tick(void) {
sg_resending = NO;
}
+// Main thread, as everything the timer touches is.
+static void setTicking(BOOL on) {
+ if (on == (sg_timer != nil)) return;
+ if (!on) {
+ [sg_timer invalidate];
+ sg_timer = nil;
+ return;
+ }
+ sg_timer = [NSTimer timerWithTimeInterval:kTick repeats:YES block:^(NSTimer *t) { tick(); }];
+ [NSRunLoop.mainRunLoop addTimer:sg_timer forMode:NSRunLoopCommonModes];
+}
+
+// Whether the sound is moving. A rate Spotify did not report at all counts as moving: the line would
+// stand still either way, and a missing key is no reason to leave the feature switched off for good.
+static BOOL playingBy(NSDictionary *info) {
+ NSNumber *rate = info[MPNowPlayingInfoPropertyPlaybackRate];
+ return !rate || rate.doubleValue > 0;
+}
+
%hook MPNowPlayingInfoCenter
- (void)setNowPlayingInfo:(NSDictionary *)info {
if (sg_resending) {
@@ -114,12 +138,17 @@ static void tick(void) {
sg_spotifyInfo = info;
sg_spotifyInfoAt = now;
}
+ BOOL playing = playingBy(info) && info[MPNowPlayingInfoPropertyElapsedPlaybackTime] != nil;
// Karaoke's lyrics are main-thread state; off it the info goes out as it is and the next tick adds the line.
if (!NSThread.isMainThread || !info[MPNowPlayingInfoPropertyElapsedPlaybackTime]) {
- dispatch_async(dispatch_get_main_queue(), ^{ sg_shownLine = nil; });
+ dispatch_async(dispatch_get_main_queue(), ^{
+ sg_shownLine = nil;
+ setTicking(playing);
+ });
%orig;
return;
}
+ setTicking(playing);
double elapsed = elapsedAt(info, now, now);
NSString *line = lineFor(info, elapsed);
sg_shownLine = line;
@@ -139,7 +168,6 @@ static void tick(void) {
if (!SGFlag(SGKeyLockScreenLyrics, NO)) return;
sg_lock = [NSObject new];
%init;
- NSTimer *timer = [NSTimer timerWithTimeInterval:kTick repeats:YES block:^(NSTimer *t) { tick(); }];
- [NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];
+ // The timer waits for Spotify to report a playing track; nothing before that has a line to show.
SGLog(@"lock screen lyrics: on");
}