Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: AirPods gestures teach each selected action with a live head preview
My-Name-Is-Jeff21586567e05f
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 9ee3bf76..09ddaec2 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -218,12 +218,17 @@ Shared:against harness/haptics/, as are Native iOS's pure steps (build/track), the hook in the simulatoragainst its sim/, the settings against harness/haptics-page/HeadGestures/ AirPods gestures (HeadGestures.h lists its files): CMHeadphoneMotionManager's attitudes, while the- switch is on and Spotify plays, read for a double nod, which adds the track to Liked Songs through- Spotify's collection platform (addURL:showUIConfirmation:completion:), and a shake, which skips; a+ switch is on and Spotify plays, read for a double nod and a shake, each doing what its pull-down+ says: Nothing, back or forward 15 s, play or pause (which listens with the song paused too), next or+ previous track, shuffle, repeat, or Like, which adds the track to Liked Songs through Spotify's+ collection platform (addURL:showUIConfirmation:completion:); Like and Next track until changed. Atone played through Spotify's playback session confirms each, whatever the Ring/Silent switch says.- Learning one's own nod and shake sets the thresholds, each kept only if the detector, set to it,- finds the gesture in what was recorded; its Cancel stops listening at once, and Forget shows only- while something is learned. The one CMHeadphoneMotionManager is lent to other features+ Try it listens a few seconds and names in its row what it picked up, doing nothing to the song. A+ sheet teaches one's own nod and shake, five of each after a tone, a ring showing the head live (a dot+ that moves with it, ticks lighting toward where it went; SGHeadMotionListen) and five dots counting+ what landed, with Redo last; the threshold kept is the one that fires on all five but one at least, the nod+ stored before the shakes begin, so Cancel keeps it. Forget shows only while something is learned.+ The one CMHeadphoneMotionManager is lent to other features(SGHeadMotionListen: Sing's spatial voice), and runs for them with the switch off, the detector thenunfed. The detector istested on the Mac against harness/head-gestures/, the hook and the page in the simulator against its sim/diff --git a/harness/head-gestures/README.md b/harness/head-gestures/README.mdindex a0f5ebab..363643dd 100644--- a/harness/head-gestures/README.md+++ b/harness/head-gestures/README.md@@ -8,6 +8,10 @@ matter as much as the gestures. Learning is run on a small slow nod and shake: twas learned catches them, and a walk still fires nothing at what was learned. Learning keeps a threshold onlywhen the detector, set to it, fires on the recording: three nods and a double nod of 2 s learn nothing, and adouble nod with a hard settle, which the first margin reads as six swings, learns a closer threshold that fires.+Teaching (SGHeadLearnSamples) is run on five recordings the way the sheet takes them: five double nods of+different sizes teach a threshold under the smallest, which fires on each of the five while a walk fires nothing;+four double nods and three nods still teach; three and two of three nods teach nothing; five shakes of different+widths teach a shake threshold that fires on each../build.sh && build/head-gestures # the synthetic tracesbuild/head-gestures <trace.csv> [nod] [shake] # a recording, one "time,pitch,yaw" a line@@ -17,20 +21,37 @@ The traces are synthetic. No recording from real AirPods has been run through it## The hook and the page in the simulator (`sim/`)-The real `HeadGestures.x` (through Logos), its settings page and the detector, with Spotify's player and collection-platform stood in for and `CMHeadphoneMotionManager`'s start and stop taken over, so made-up motion reaches the-handler on the manager's own queue. It checks that nothing listens until the switch is on, a double nod adds the-playing track with Spotify's toast, a shake skips, an episode is not saved, pausing stops, learning listens while-paused and stores what it learned, learning with no motion says so, a learning alert's Cancel ends the-listening at once (but for another feature's, through SGHeadMotionListen) and keeps the nod that was stored, a-second Learn straight after starts at once, Forget shows only while something is learned, and asks before it-clears both.+The real `HeadGestures.x` (through Logos), its settings page, its teaching sheet and the detector, with Spotify's+player and collection platform stood in for and `CMHeadphoneMotionManager`'s start and stop taken over, so made-up+motion reaches the handler on the manager's own queue. It checks that nothing listens until the switch is on, the+pull-downs read Like and Next track and offer the nine actions, a double nod adds the playing track with Spotify's+toast, a shake skips, an episode is not saved, and then each action through the nod's pull-down sends its player+command: back and forward 15 s (forward stopping at the end), pause, next, previous, shuffle on and off, repeat+through its three states. Set to Play or pause, the gestures listen with the song paused and resume it, and stop+listening once nothing is set to it; a seek while paused reads the paused position; a shake set to Nothing sends+nothing. Try it names a double nod, a shake, a still head (Nothing) and no headphones (No motion) while the switch is+on and a song plays, and sends nothing to the player. The sheet's ring follows the head: a nod's dip takes the dot+down and lights the ticks below it, not those above or beside, a still head brings the dot back and the ticks fade,+and a swing to the left takes the dot left and lights the ticks there. The sheet asks for nods first, does not count one nod, Redo last+takes one off, stores the nod before the shakes and the shake at the end, with a song playing and nothing sent to+the player meanwhile, and swaps Cancel for Done; the smallest nod it was taught then likes the song. A second+sheet holds the motion while paused, and its Cancel after one nod keeps the stored nod and stops the motion. Forget+shows only while something is learned, and asks before it clears both.THEOS=$HOME/theos sim/build.sh && xcrun simctl install <udid> build/sim/HeadGesturesSim.appxcrun simctl launch <udid> com.vitrine.headgesturessim # then read the [harness] lines from the log+ xcrun simctl launch <udid> com.vitrine.headgesturessim live # no checks: page, sheet, done or live, to screenshot++`live` feeds double nods and then shakes in real time, 25 samples a second, so screenshots catch the ring mid-gesture.++The steps wait on what they check rather than on a clock (each fails after 20 s), and the sheet's recordings are+fed as they open, so the run takes about a minute and a half.2026-10-05, iPhone 17 Pro on iOS 27.0: all 15 checks pass, three runs in a row, with the cue's tones playedthrough AVAudioPlayer (the harness checks the calls, not the sound). With Cancel and Forget, all 24 pass; a simulatorslow enough to hold the alert's presentation for seconds makes the timed steps fail.Since Cancel ends learning at once and Forget shows only while something is learned: all 30 pass, three runs in arow, on the same simulator.+2026-10-06, the same: with the actions, Try it and the teaching sheet in place of the learning alerts, all 53+pass, three runs in a row.+With the live ring in place of the progress arcs: all 59 pass, one run, on the same simulator.diff --git a/harness/head-gestures/sim/build.sh b/harness/head-gestures/sim/build.shindex ecc27b3a..9c2a30dc 100755--- a/harness/head-gestures/sim/build.sh+++ b/harness/head-gestures/sim/build.sh@@ -11,7 +11,8 @@ SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g -O0 \-I"$SRC" -I"$SRC/Shared/HeadGestures" -isysroot "$SDK" -Wall -Werror -Wno-deprecated-declarations \"$(dirname "$0")/main.m" "$(dirname "$0")/../../scene.m" "$OUT/gen/HeadGestures.m" \- "$SRC"/Shared/HeadGestures/HeadGesturesSettings.m "$SRC"/Shared/HeadGestures/SGHeadDetector.m \+ "$SRC"/Shared/HeadGestures/HeadGesturesSettings.m "$SRC"/Shared/HeadGestures/HeadGesturesTeach.m \+ "$SRC"/Shared/HeadGestures/SGHeadDetector.m \"$SRC"/Settings/SGPage.m "$SRC"/Settings/SGPageStyle.m "$SRC"/Settings/SGModPage.m "$SRC"/Settings/SGGlowSwitch.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \-framework UIKit -framework QuartzCore -framework CoreGraphics -framework Foundation -framework CoreMotion -framework AudioToolbox -framework AVFoundation \diff --git a/harness/head-gestures/sim/main.m b/harness/head-gestures/sim/main.mindex 80684403..e616fcc7 100644--- a/harness/head-gestures/sim/main.m+++ b/harness/head-gestures/sim/main.m@@ -1,12 +1,17 @@-// AirPods gestures in the simulator: the real HeadGestures.x (through Logos), its settings page and the-// detector, with Spotify's player and collection platform stood in for, and CMHeadphoneMotionManager's-// start and stop taken over so the harness hands the handler made-up motion on the manager's own queue.-// The simulator has no headphones, so this proves the wiring (the switch, playing or not, the thresholds,-// the like and skip calls, the learning alerts), not the motion. A fixed run of steps, one every second;-// each check logs "ok" or "FAIL" and the last line says how many failed.+// AirPods gestures in the simulator: the real HeadGestures.x (through Logos), its settings page, its teaching+// sheet and the detector, with Spotify's player and collection platform stood in for, and+// CMHeadphoneMotionManager's start and stop taken over so the harness hands the handler made-up motion on the+// manager's own queue. The simulator has no headphones, so this proves the wiring (the switch, playing or not,+// each action's player call, Try it, the sheet's five recordings, Redo last, Cancel, Forget), not the motion.+// Steps run in order; each is asked again every quarter second until it says it is done, and fails after+// 20 s. Each check logs "ok" or "FAIL" and the last line says how many failed.//// THEOS=$HOME/theos ./build.sh && xcrun simctl install <udid> ../build/sim/HeadGesturesSim.app// xcrun simctl launch --console-pty <udid> com.vitrine.headgesturessim+//+// With an argument it runs no checks and stops on a view to screenshot: `page` (the page, switch on, a nod+// learned and a try's answer), `sheet` (the sheet at 2 of 5, recording on and off), `done` (All set) or `live`+// (head motion in real time, nods and then shakes, for the ring).#import <UIKit/UIKit.h>#import <CoreMotion/CoreMotion.h>#import <objc/runtime.h>@@ -29,9 +34,17 @@ static void check(BOOL ok, NSString *what) {@implementation FakeTrack@end+@interface FakeOptions : NSObject+@property (nonatomic) BOOL shufflingContext, repeatingContext, repeatingTrack;+@end+@implementation FakeOptions+@end+@interface FakeState : NSObject@property (nonatomic, strong) FakeTrack *track;+@property (nonatomic, strong) FakeOptions *options;@property (nonatomic) BOOL isPlaying, isPaused;+@property (nonatomic) double position, positionAsOfTimestamp, duration;@end@implementation FakeState@end@@ -47,13 +60,19 @@ NSString *SGURIString(id uri) {void SGAddPlayerStateObserver(id<SGPlayerStateObserver> observer) { sg_observer = observer; }SPTPlayerState *SGPlayerState(void) { return (SPTPlayerState *)sg_state; }+// The player's commands the actions send, each written down as it comes.@interface FakePlayer : NSObject@end@implementation FakePlayer-- (id)skipToNextTrackWithOptions:(id)options {- [sg_calls addObject:@"skip"];- return nil;-}+- (FakeState *)state { return sg_state; }+- (void)seekTo:(double)seconds { [sg_calls addObject:[NSString stringWithFormat:@"seek %.0f", seconds]]; }+- (id)pause:(id)options { [sg_calls addObject:@"pause"]; return nil; }+- (id)resume:(id)options { [sg_calls addObject:@"resume"]; return nil; }+- (id)skipToNextTrackWithOptions:(id)options { [sg_calls addObject:@"skip"]; return nil; }+- (id)skipToPreviousTrackWithOptions:(id)options { [sg_calls addObject:@"previous"]; return nil; }+- (id)setShufflingContext:(BOOL)on { [sg_calls addObject:[NSString stringWithFormat:@"shuffle %d", on]]; return nil; }+- (id)setRepeatingContext:(BOOL)on { [sg_calls addObject:[NSString stringWithFormat:@"repeat context %d", on]]; return nil; }+- (id)setRepeatingTrack:(BOOL)on { [sg_calls addObject:[NSString stringWithFormat:@"repeat track %d", on]]; return nil; }@endstatic FakePlayer *sg_player;id SGKaraokePlayer(void) { return sg_player; }@@ -129,13 +148,17 @@ static void send(double pitch, double yaw) {static void hold(double seconds) {for (int n = (int)(seconds * 25); n > 0; n--) send(0.1, 3.0);}-static void doubleNod(double depth, double length) {- for (int k = 0; k < 2; k++)+static void nods(int count, double depth, double length) {+ for (int k = 0; k < count; k++)for (int n = 0, steps = (int)(length * 25); n < steps; n++) {double s = sin(M_PI * n / steps);send(0.1 - depth * s * s, 3.0);}- hold(2);+ hold(1);+}+static void doubleNod(double depth, double length) {+ nods(2, depth, length);+ hold(1);}static void shake(double width, double hertz) {for (int n = 0, steps = (int)(4 / (2 * hertz) * 25); n < steps; n++) send(0.1, remainder(3.0 + width * sin(2 * M_PI * hertz * n / 25), 2 * M_PI));@@ -149,18 +172,38 @@ static void findViews(UIView *root, Class kind, NSMutableArray *found) {for (UIView *sub in root.subviews) findViews(sub, kind, found);}+typedef BOOL (^Step)(void); // YES once done+static NSInteger sg_nodBefore;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (nonatomic, strong) UIWindow *window;@property (nonatomic, strong) UINavigationController *nav;@property (nonatomic, strong) SPTCollectionPlatformImplementation *platform;+@property (nonatomic, copy) NSArray<Step> *steps;+@property (nonatomic) NSUInteger current;+@property (nonatomic, strong) NSDate *stepStarted;+// What the sheet's recordings are fed, one a recording, in order; the last is fed on and on.+@property (nonatomic, strong) NSMutableArray<void (^)(void)> *feeds;+@property (nonatomic) BOOL fedThisRecording;@end@implementation AppDelegate+- (SGModPage *)page {+ return (SGModPage *)self.nav.viewControllers.firstObject;+}+- (UITableView *)table {- return ((UITableViewController *)self.nav.viewControllers.lastObject).tableView;+ return self.page.tableView;+}++- (SGModRow *)rowAt:(NSInteger)row section:(NSInteger)section {+ return [self.page performSelector:@selector(rowAt:) withObject:[NSIndexPath indexPathForRow:row inSection:section]];+}++- (void)tapRow:(NSInteger)row section:(NSInteger)section {+ [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];}- (void)setPlaying:(BOOL)playing {@@ -174,10 +217,6 @@ static NSInteger sg_nodBefore;return [shown isKindOfClass:UIAlertController.class] ? shown.title : nil;}-- (void)dismissAlert {- [self.nav dismissViewControllerAnimated:NO completion:nil];-}-// The alert's button of that style, pressed: the alert goes and its handler runs.- (void)press:(UIAlertActionStyle)style {UIAlertController *alert = (UIAlertController *)self.nav.presentedViewController;@@ -188,122 +227,426 @@ static NSInteger sg_nodBefore;}}-- (NSArray<void (^)(void)> *)steps {+// The teaching sheet's controller, when it is up.+- (UIViewController *)teach {+ UINavigationController *sheet = (UINavigationController *)self.nav.presentedViewController;+ return [sheet isKindOfClass:UINavigationController.class] ? sheet.viewControllers.firstObject : nil;+}++- (id)teachValue:(NSString *)key {+ return [self.teach valueForKey:key];+}++- (NSString *)teachCount {+ return [[self teachValue:@"dial"] valueForKey:@"accessibilityValue"];+}++- (NSString *)teachText:(NSString *)key {+ return ((UILabel *)[self teachValue:key]).text;+}++// The ring's dot and its centre, in the ring's own coordinates, and one tick's light: 0 is the top, 15 the+// right, 30 the bottom, 45 the left.+- (CGPoint)ringDot {+ return ((CALayer *)[[self teachValue:@"dial"] valueForKey:@"dot"]).position;+}++- (CGPoint)ringCentre {+ UIView *ring = [self teachValue:@"dial"];+ return CGPointMake(CGRectGetMidX(ring.bounds), CGRectGetMidY(ring.bounds));+}++- (float)tick:(NSInteger)index {+ return ((NSArray<CALayer *> *)[[self teachValue:@"dial"] valueForKey:@"ticks"])[index].opacity;+}++- (void)teachButton:(NSString *)key {+ [(UIButton *)[self teachValue:key] sendActionsForControlEvents:UIControlEventPrimaryActionTriggered];+}++// One gesture through the page's own pull-down, then made: the calls it sends to the player.+- (NSArray<Step> *)action:(SGHeadAction)action makes:(NSArray<NSString *> *)want named:(NSString *)name {return @[- ^{+ ^BOOL {+ [sg_calls removeAllObjects];+ [self rowAt:0 section:1].chosen(action);+ doubleNod(0.35, 0.4);+ return YES;+ },+ ^BOOL {+ check([sg_calls isEqualToArray:want], [NSString stringWithFormat:@"double nod set to %@ sends [%@]: %@", name, [want componentsJoinedByString:@", "],[sg_calls componentsJoinedByString:@", "]]);+ return YES;+ },+ ];+}++// Waits until `done` says yes; the harness fails it after 20 s.+static Step until(BOOL (^done)(void)) {+ return done;+}++static Step waitFor(double seconds) {+ __block NSDate *end;+ return ^BOOL {+ if (!end) end = [NSDate dateWithTimeIntervalSinceNow:seconds];+ return end.timeIntervalSinceNow <= 0;+ };+}++- (NSArray<Step> *)checks {+ NSMutableArray<Step> *steps = [NSMutableArray array];+ [steps addObjectsFromArray:@[+ ^BOOL {check(sg_starts == 0, @"switch off, playing: not listening");UITableViewCell *cell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];NSMutableArray<UISwitch *> *switches = [NSMutableArray array];findViews(cell, UISwitch.class, switches);[switches.firstObject setOn:YES animated:YES];[switches.firstObject sendActionsForControlEvents:UIControlEventValueChanged];+ return YES;},- ^{+ waitFor(1),+ ^BOOL {check(sg_active, @"switch on, playing: listening");- check([self.table numberOfRowsInSection:1] == 2, @"nothing learned: no Forget row");+ check([self.table numberOfRowsInSection:2] == 3, @"nothing learned: no Forget row");+ check([[self rowAt:0 section:1].value() isEqualToString:@"Like"], @"a double nod likes until changed");+ check([[self rowAt:1 section:1].value() isEqualToString:@"Next track"], @"a shake skips until changed");+ NSMutableArray<UIButton *> *buttons = [NSMutableArray array];+ findViews([self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]], UIButton.class, buttons);+ check(buttons.firstObject.menu.children.count == SGHeadActionCount, [NSString stringWithFormat:@"the pull-down offers %ld actions", (long)buttons.firstObject.menu.children.count]);doubleNod(0.35, 0.4);+ return YES;},- ^{+ ^BOOL {check([sg_calls isEqualToArray:@[@"add spotify:track:4uLU6hMCjMI75M1A2tKUQC toast"]],- [NSString stringWithFormat:@"a double nod adds the track, calls %@", sg_calls]);+ [NSString stringWithFormat:@"a double nod adds the track, calls %@", [sg_calls componentsJoinedByString:@", "]]);[sg_calls removeAllObjects];shake(0.21, 2.5);+ return YES;},- ^{- check([sg_calls isEqualToArray:@[@"skip"]], [NSString stringWithFormat:@"a shake skips, calls %@", sg_calls]);+ ^BOOL {+ check([sg_calls isEqualToArray:@[@"skip"]], [NSString stringWithFormat:@"a shake skips, calls %@", [sg_calls componentsJoinedByString:@", "]]);[sg_calls removeAllObjects];- doubleNod(0.17, 0.5);- },- ^{- check(sg_calls.count == 0, [NSString stringWithFormat:@"a small slow double nod at the default does nothing, calls %@", sg_calls]);sg_state.track.URI = [NSURL URLWithString:@"spotify:episode:512ojhOuo1ktJprKbVcKyQ"];doubleNod(0.35, 0.4);+ return YES;},- ^{- check(sg_calls.count == 0, [NSString stringWithFormat:@"a double nod on an episode saves nothing, calls %@", sg_calls]);+ ^BOOL {+ check(sg_calls.count == 0, [NSString stringWithFormat:@"a double nod on an episode saves nothing, calls %@", [sg_calls componentsJoinedByString:@", "]]);sg_state.track.URI = @"spotify:track:4uLU6hMCjMI75M1A2tKUQC";- [self setPlaying:NO];- check(!sg_active && sg_stops == 1, @"paused: stopped");- // Learning listens whatever plays.- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];- },- ^{- NSLog(@"[harness] learning started: %@", sg_active ? @"yes" : @"no");+ sg_state.position = sg_state.positionAsOfTimestamp = 100;+ sg_state.duration = 110;+ return YES;},- ^{- check([[self alertTitle] isEqualToString:@"Nod twice"], [NSString stringWithFormat:@"learning asks for a nod: %@", [self alertTitle]]);- check(sg_active, @"learning while paused: listening");- doubleNod(0.17, 0.5);+ ]];+ // Every action, through the nod's pull-down.+ [steps addObjectsFromArray:[self action:SGHeadActionNothing makes:@[] named:@"Nothing"]];+ [steps addObjectsFromArray:[self action:SGHeadActionSeekBack makes:@[@"seek 85"] named:@"Back 15 seconds"]];+ [steps addObjectsFromArray:[self action:SGHeadActionSeekForward makes:@[@"seek 110"] named:@"Forward 15 seconds, at most to the end"]];+ [steps addObjectsFromArray:[self action:SGHeadActionPlayPause makes:@[@"pause"] named:@"Play or pause, playing"]];+ [steps addObjectsFromArray:[self action:SGHeadActionNext makes:@[@"skip"] named:@"Next track"]];+ [steps addObjectsFromArray:[self action:SGHeadActionPrevious makes:@[@"previous"] named:@"Previous track"]];+ [steps addObjectsFromArray:[self action:SGHeadActionShuffle makes:@[@"shuffle 1"] named:@"Shuffle, off"]];+ [steps addObject:^BOOL { sg_state.options.shufflingContext = YES; return YES; }];+ [steps addObjectsFromArray:[self action:SGHeadActionShuffle makes:@[@"shuffle 0"] named:@"Shuffle, on"]];+ [steps addObjectsFromArray:[self action:SGHeadActionRepeat makes:@[@"repeat context 1"] named:@"Repeat, off"]];+ [steps addObject:^BOOL { sg_state.options.repeatingContext = YES; return YES; }];+ [steps addObjectsFromArray:[self action:SGHeadActionRepeat makes:@[@"repeat track 1"] named:@"Repeat, the context"]];+ [steps addObject:^BOOL { sg_state.options.repeatingTrack = YES; return YES; }];+ [steps addObjectsFromArray:[self action:SGHeadActionRepeat makes:@[@"repeat track 0", @"repeat context 0"] named:@"Repeat, the track"]];+ [steps addObjectsFromArray:@[+ ^BOOL {+ // Play or pause listens with the song paused, so the nod can resume it.+ [self rowAt:0 section:1].chosen(SGHeadActionPlayPause);+ [self setPlaying:NO];+ check(sg_active, @"paused, a gesture set to Play or pause: listening");+ sg_state.positionAsOfTimestamp = 40;+ sg_state.position = 1000; // a paused state's position runs on; the seek must not read it+ [sg_calls removeAllObjects];+ doubleNod(0.35, 0.4);+ return YES;},- ^{}, ^{}, ^{},- ^{- check([[self alertTitle] isEqualToString:@"Learned"], [NSString stringWithFormat:@"after 4 s it says %@", [self alertTitle]]);- check(SGInt(SGKeyHeadNod, 0) > 0, [NSString stringWithFormat:@"stored nod %ld", (long)SGInt(SGKeyHeadNod, 0)]);- check(!sg_active, @"learning over, paused: stopped");- check([self.table numberOfRowsInSection:1] == 3, @"a nod learned: the Forget row shows");- [self dismissAlert];- [self.table reloadData];+ ^BOOL {+ check([sg_calls isEqualToArray:@[@"resume"]], [NSString stringWithFormat:@"paused, a double nod resumes: %@", [sg_calls componentsJoinedByString:@", "]]);+ [self rowAt:0 section:1].chosen(SGHeadActionSeekBack);+ check(!sg_active, @"paused, nothing set to Play or pause: stopped");[self setPlaying:YES];- doubleNod(0.17, 0.5);+ [sg_calls removeAllObjects];+ sg_state.position = 30;+ doubleNod(0.35, 0.4);+ return YES;},- ^{- check([sg_calls isEqualToArray:@[@"add spotify:track:4uLU6hMCjMI75M1A2tKUQC toast"]],- [NSString stringWithFormat:@"the small slow double nod, at what was learned, adds the track, calls %@", sg_calls]);+ ^BOOL {+ check([sg_calls isEqualToArray:@[@"seek 15"]], [NSString stringWithFormat:@"playing at 30 s, back 15: %@", [sg_calls componentsJoinedByString:@", "]]);+ [self rowAt:0 section:1].chosen(SGHeadActionLike);+ [self rowAt:1 section:1].chosen(SGHeadActionNothing);[sg_calls removeAllObjects];+ shake(0.21, 2.5);+ return YES;+ },+ ^BOOL {+ check(sg_calls.count == 0, [NSString stringWithFormat:@"a shake set to Nothing sends nothing: %@", [sg_calls componentsJoinedByString:@", "]]);+ [self rowAt:1 section:1].chosen(SGHeadActionNext);+ check([[self rowAt:1 section:1].value() isEqualToString:@"Next track"], @"the shake's pull-down reads Next track again");+ // Try it, while the switch is on and a song plays: it says what it picked up and does nothing.+ [self tapRow:2 section:2];+ check([[self rowAt:2 section:2].value() isEqualToString:@"Listening…"], [NSString stringWithFormat:@"Try it listens: %@", [self rowAt:2 section:2].value()]);+ doubleNod(0.35, 0.4);+ return YES;+ },+ until(^BOOL { return ![[self rowAt:2 section:2].value() isEqualToString:@"Listening…"]; }),+ ^BOOL {+ check([[self rowAt:2 section:2].value() isEqualToString:@"Double nod"], [NSString stringWithFormat:@"Try it picked up %@", [self rowAt:2 section:2].value()]);+ check(sg_calls.count == 0, [NSString stringWithFormat:@"and liked nothing: %@", [sg_calls componentsJoinedByString:@", "]]);+ [self tapRow:2 section:2];+ shake(0.21, 2.5);+ return YES;+ },+ until(^BOOL { return ![[self rowAt:2 section:2].value() isEqualToString:@"Listening…"]; }),+ ^BOOL {+ check([[self rowAt:2 section:2].value() isEqualToString:@"Shake"], [NSString stringWithFormat:@"Try it picked up %@", [self rowAt:2 section:2].value()]);+ check(sg_calls.count == 0, [NSString stringWithFormat:@"and skipped nothing: %@", [sg_calls componentsJoinedByString:@", "]]);+ [self tapRow:2 section:2];+ hold(2);+ return YES;+ },+ until(^BOOL { return ![[self rowAt:2 section:2].value() isEqualToString:@"Listening…"]; }),+ ^BOOL {+ check([[self rowAt:2 section:2].value() isEqualToString:@"Nothing"], [NSString stringWithFormat:@"a still head: %@", [self rowAt:2 section:2].value()]);[self setPlaying:NO];- // No headphones: nothing arrives while it learns the shake.- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];+ [self tapRow:2 section:2];+ return YES;+ },+ until(^BOOL { return ![[self rowAt:2 section:2].value() isEqualToString:@"Listening…"]; }),+ ^BOOL {+ check([[self rowAt:2 section:2].value() isEqualToString:@"No motion"], [NSString stringWithFormat:@"no headphones sending: %@", [self rowAt:2 section:2].value()]);+ check(!sg_active, @"the try over, paused: stopped");+ // The teaching sheet, with a song playing, to show no gesture does anything meanwhile.+ [self setPlaying:YES];+ [sg_calls removeAllObjects];+ [self tapRow:0 section:2];+ return YES;+ },+ until(^BOOL { return self.teach.viewIfLoaded.window != nil; }),+ waitFor(0.5),+ // The ring, live: the head held still, then down as in a nod's dip.+ ^BOOL {+ hold(1);+ for (int n = 0; n < 5; n++) send(0.1 - 0.25, 3.0);+ return YES;+ },+ waitFor(0.4),+ ^BOOL {+ CGPoint dot = [self ringDot], centre = [self ringCentre];+ check(dot.y > centre.y + 30 && fabs(dot.x - centre.x) < 4, [NSString stringWithFormat:@"a nod's dip takes the dot down: %.0f, %.0f from the centre", dot.x - centre.x, dot.y - centre.y]);+ check([self tick:30] > 0.4 && [self tick:0] == 0 && [self tick:15] == 0,+ [NSString stringWithFormat:@"the ticks below light, not above or beside: below %.2f, above %.2f, right %.2f", [self tick:30], [self tick:0], [self tick:15]]);+ hold(1.5);+ return YES;+ },+ waitFor(1.5),+ ^BOOL {+ CGPoint dot = [self ringDot], centre = [self ringCentre];+ check(hypot(dot.x - centre.x, dot.y - centre.y) < 12, [NSString stringWithFormat:@"still again: the dot comes back, %.0f, %.0f", dot.x - centre.x, dot.y - centre.y]);+ check([self tick:30] < 0.05, [NSString stringWithFormat:@"and the ticks fade back: below %.2f", [self tick:30]]);+ // A shake's swing to the left: yaw rising.+ for (int n = 0; n < 5; n++) send(0.1, 3.0 + 0.18);+ return YES;+ },+ waitFor(0.4),+ ^BOOL {+ CGPoint dot = [self ringDot], centre = [self ringCentre];+ check(dot.x < centre.x - 30, [NSString stringWithFormat:@"a swing left takes the dot left: %.0f, %.0f", dot.x - centre.x, dot.y - centre.y]);+ check([self tick:45] > 0.4 && [self tick:15] == 0, [NSString stringWithFormat:@"the ticks on the left light: left %.2f, right %.2f", [self tick:45], [self tick:15]]);+ hold(1.5);+ return YES;+ },+ waitFor(1.5),+ ^BOOL {+ check([[self teachText:@"heading"] isEqualToString:@"Double nod"], [NSString stringWithFormat:@"the sheet asks for nods first: %@", [self teachText:@"heading"]]);+ check([[self teachCount] isEqualToString:@"0 of 5"], [NSString stringWithFormat:@"none yet: %@", [self teachCount]]);+ self.feeds = [@[+ ^{ doubleNod(0.35, 0.4); },+ ^{ doubleNod(0.30, 0.45); },+ ^{ nods(1, 0.35, 0.4); }, // one nod: not counted+ ^{ doubleNod(0.28, 0.45); },+ ^{ doubleNod(0.32, 0.4); },+ ^{ doubleNod(0.26, 0.45); },+ ^{ doubleNod(0.30, 0.4); },+ ^{ shake(0.21, 2.5); },+ ^{ shake(0.18, 2.2); },+ ^{ shake(0.15, 2.0); },+ ^{ shake(0.2, 2.4); },+ ^{ shake(0.17, 2.2); },+ ] mutableCopy];+ [self teachButton:@"primaryButton"];+ return YES;},- ^{}, ^{}, ^{}, ^{}, ^{},- ^{- check([[self alertTitle] isEqualToString:@"No motion came"], [NSString stringWithFormat:@"learning with nothing sent says %@", [self alertTitle]]);- check(SGInt(SGKeyHeadShake, 0) == 0, @"nothing stored for the shake");- [self dismissAlert];- [self.table reloadData];+ until(^BOOL { return [[self teachText:@"line"] hasPrefix:@"That didn't read"]; }),+ ^BOOL {+ check([[self teachCount] isEqualToString:@"2 of 5"], [NSString stringWithFormat:@"one nod is not counted: %@", [self teachCount]]);+ return YES;},- ^{- // Learning the nod again, cancelled while a nod comes in, with another feature listening to the motion.+ until(^BOOL { return [[self teachCount] isEqualToString:@"3 of 5"]; }),+ ^BOOL {+ [self teachButton:@"redoButton"];+ check([[self teachCount] isEqualToString:@"2 of 5"], [NSString stringWithFormat:@"Redo last takes one off: %@", [self teachCount]]);+ check(SGInt(SGKeyHeadNod, 0) == 0, @"nothing stored before five");+ return YES;+ },+ until(^BOOL { return [[self teachText:@"heading"] isEqualToString:@"Shake"]; }),+ ^BOOL {sg_nodBefore = SGInt(SGKeyHeadNod, 0);- SGHeadMotionListen(@"harness", ^(CMDeviceMotion *motion) {});- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];+ check(sg_nodBefore > 0, [NSString stringWithFormat:@"five nods in, the nod is stored before the shakes: %ld", (long)sg_nodBefore]);+ return YES;},- ^{- check([[self alertTitle] isEqualToString:@"Nod twice"], [NSString stringWithFormat:@"learning asks again: %@", [self alertTitle]]);- [self press:UIAlertActionStyleCancel];- doubleNod(0.35, 0.4);+ until(^BOOL { return [[self teachText:@"heading"] isEqualToString:@"All set"]; }),+ ^BOOL {+ check(SGInt(SGKeyHeadShake, 0) > 0, [NSString stringWithFormat:@"five shakes in, the shake is stored: %ld", (long)SGInt(SGKeyHeadShake, 0)]);+ check(sg_calls.count == 0, [NSString stringWithFormat:@"with a song playing, no gesture did anything while it taught: %@", [sg_calls componentsJoinedByString:@", "]]);+ check(self.teach.navigationItem.leftBarButtonItem == nil, @"All set: no Cancel, Done instead");+ self.feeds = nil;+ [self teachButton:@"primaryButton"];+ return YES;+ },+ until(^BOOL { return self.nav.presentedViewController == nil; }),+ waitFor(0.6),+ ^BOOL {+ check([self.table numberOfRowsInSection:2] == 4, @"something learned: the Forget row shows");+ check([[self rowAt:0 section:2].value() isEqualToString:@"Learned"], [NSString stringWithFormat:@"Teach reads %@", [self rowAt:0 section:2].value()]);+ check(sg_active, @"the sheet gone, playing: listening for the gestures again");+ // The smallest of the five nods it was taught, at what it learned.+ doubleNod(0.26, 0.45);+ return YES;},- ^{- check([self alertTitle] == nil, [NSString stringWithFormat:@"Cancel takes the alert away: %@", [self alertTitle]]);- check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"a cancelled try keeps the nod %ld, stored %ld", (long)sg_nodBefore, (long)SGInt(SGKeyHeadNod, 0)]);- check(sg_active, @"cancelled, paused, another feature listening: the motion runs on for it");- SGHeadMotionListen(@"harness", nil);- check(!sg_active, @"that feature gone too: stopped");- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];+ ^BOOL {+ check([sg_calls isEqualToArray:@[@"add spotify:track:4uLU6hMCjMI75M1A2tKUQC toast"]],+ [NSString stringWithFormat:@"the smallest taught nod likes the song: %@", [sg_calls componentsJoinedByString:@", "]]);+ [sg_calls removeAllObjects];+ // Teaching again, cancelled after one nod: the stored nod stays, and the motion stops.+ [self setPlaying:NO];+ self.feeds = [@[^{ doubleNod(0.2, 0.45); }, ^{}] mutableCopy];+ [self tapRow:0 section:2];+ return YES;},- ^{- check([[self alertTitle] isEqualToString:@"Nod twice"], [NSString stringWithFormat:@"learning again straight after Cancel asks: %@", [self alertTitle]]);- check(sg_active, @"learning again: listening");- [self press:UIAlertActionStyleCancel];+ until(^BOOL { return self.teach.viewIfLoaded.window != nil; }),+ waitFor(0.5),+ ^BOOL {+ check(sg_active, @"paused, the sheet up: it holds the motion");+ [self teachButton:@"primaryButton"];+ return YES;},- ^{}, ^{}, ^{}, ^{},- ^{- check([self alertTitle] == nil, [NSString stringWithFormat:@"the cancelled tries say nothing when their 4 s end: %@", [self alertTitle]]);- check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"and store nothing: nod %ld", (long)SGInt(SGKeyHeadNod, 0)]);- check(!sg_active, @"cancelled learning over, paused: stopped");- [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];+ until(^BOOL { return [[self teachCount] isEqualToString:@"1 of 5"]; }),+ ^BOOL {+ UIBarButtonItem *cancel = self.teach.navigationItem.leftBarButtonItem;+ [UIApplication.sharedApplication sendAction:cancel.action to:cancel.target from:cancel forEvent:nil];+ return YES;},- ^{+ until(^BOOL { return self.nav.presentedViewController == nil; }),+ waitFor(0.6),+ ^BOOL {+ self.feeds = nil;+ check(SGInt(SGKeyHeadNod, 0) == sg_nodBefore, [NSString stringWithFormat:@"Cancel keeps the nod %ld: %ld", (long)sg_nodBefore, (long)SGInt(SGKeyHeadNod, 0)]);+ check(!sg_active, @"cancelled, paused: stopped");+ [self tapRow:3 section:2];+ return YES;+ },+ waitFor(0.5),+ ^BOOL {check([[self alertTitle] isEqualToString:@"Forget your nod and shake?"], [NSString stringWithFormat:@"Forget asks first: %@", [self alertTitle]]);[self press:UIAlertActionStyleDestructive];+ return YES;},- ^{+ waitFor(0.6),+ ^BOOL {check(SGInt(SGKeyHeadNod, 0) == 0 && SGInt(SGKeyHeadShake, 0) == 0, @"Forget clears both");- check([self.table numberOfRowsInSection:1] == 2, @"nothing learned again: the Forget row goes");- },- ^{+ check([self.table numberOfRowsInSection:2] == 3, @"nothing learned again: the Forget row goes");NSLog(@"[harness] %@", failures ? [NSString stringWithFormat:@"%d failed", failures] : @"all passed");+ return YES;},- ];+ ]];+ return steps;+}++// Views to screenshot, no checks.+- (NSArray<Step> *)shot:(NSString *)name {+ Step switchOn = ^BOOL {+ SGSetEnabled(SGKeyHeadGestures, YES);+ SGHeadGesturesSettingsChanged();+ [self.table reloadData];+ return YES;+ };+ if ([name isEqualToString:@"page"]) return @[switchOn, ^BOOL {+ SGSetInt(SGKeyHeadNod, 1200);+ [self.page refreshVisibility];+ [self tapRow:2 section:2];+ doubleNod(0.35, 0.4);+ return YES;+ }];+ if ([name isEqualToString:@"live"]) return @[switchOn, ^BOOL {+ [self tapRow:0 section:2];+ return YES;+ }, waitFor(1), ^BOOL {+ // Head motion in real time, 25 samples a second: double nods while the sheet asks for nods, then shakes.+ __block double t = 0;+ [NSTimer scheduledTimerWithTimeInterval:0.04 repeats:YES block:^(NSTimer *timer) {+ t += 0.04;+ BOOL nodding = [[self teachText:@"heading"] isEqualToString:@"Double nod"];+ double phase = fmod(t, 1.9);+ if (nodding) {+ double s = phase < 0.9 ? sin(M_PI * phase / 0.45) : 0;+ send(0.1 - 0.3 * s * s, 3.0);+ } else {+ send(0.1, remainder(3.0 + (phase < 0.9 ? 0.2 * sin(2 * M_PI * 2.2 * phase) : 0), 2 * M_PI));+ }+ }];+ [self teachButton:@"primaryButton"];+ return YES;+ }];+ NSMutableArray<void (^)(void)> *feeds = [@[^{ doubleNod(0.35, 0.4); }, ^{ doubleNod(0.3, 0.45); }, ^{ hold(2); }] mutableCopy];+ if ([name isEqualToString:@"done"]) {+ [feeds removeLastObject];+ for (int i = 0; i < 3; i++) [feeds addObject:^{ doubleNod(0.32, 0.4); }];+ for (int i = 0; i < 5; i++) [feeds addObject:^{ shake(0.2, 2.4); }];+ }+ return @[switchOn, ^BOOL {+ [self tapRow:0 section:2];+ return YES;+ }, waitFor(1), ^BOOL {+ self.feeds = feeds;+ [self teachButton:@"primaryButton"];+ return YES;+ }];+}++// Feeds the sheet's recording, once each, whenever one is open.+- (void)feed {+ if (!self.feeds.count) return;+ BOOL recording = [[self teachValue:@"listening"] boolValue];+ if (!recording) {+ self.fedThisRecording = NO;+ return;+ }+ if (self.fedThisRecording) return;+ self.fedThisRecording = YES;+ void (^next)(void) = self.feeds.firstObject;+ if (self.feeds.count > 1) [self.feeds removeObjectAtIndex:0];+ // A moment in, as someone moves after the tone.+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), next);+}++- (void)tick {+ [self feed];+ if (self.current >= self.steps.count) return;+ if (!self.stepStarted) self.stepStarted = [NSDate date];+ BOOL done = self.steps[self.current]();+ if (!done && -self.stepStarted.timeIntervalSinceNow > 20) {+ check(NO, [NSString stringWithFormat:@"step %lu timed out", (unsigned long)self.current]);+ done = YES;+ }+ if (!done) return;+ self.current++;+ self.stepStarted = nil;}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {@@ -315,6 +658,7 @@ static NSInteger sg_nodBefore;sg_player = [FakePlayer new];sg_state = [FakeState new];sg_state.track = [FakeTrack new];+ sg_state.options = [FakeOptions new];sg_state.track.URI = @"spotify:track:4uLU6hMCjMI75M1A2tKUQC";// Spotify reaching for its platform's state provider is what the hook keeps it from.self.platform = [SPTCollectionPlatformImplementation new];@@ -327,9 +671,9 @@ static NSInteger sg_nodBefore;self.window.rootViewController = self.nav;[self.window makeKeyAndVisible];- [[self steps] enumerateObjectsUsingBlock:^(void (^step)(void), NSUInteger i, BOOL *stop) {- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((1.5 + i) * NSEC_PER_SEC)), dispatch_get_main_queue(), step);- }];+ NSString *shot = NSProcessInfo.processInfo.arguments.count > 1 ? NSProcessInfo.processInfo.arguments[1] : nil;+ self.steps = [@[waitFor(1.5)] arrayByAddingObjectsFromArray:shot ? [self shot:shot] : [self checks]];+ [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(tick) userInfo:nil repeats:YES];return YES;}diff --git a/tweak/Sources/Shared/HeadGestures/HeadGestures.h b/tweak/Sources/Shared/HeadGestures/HeadGestures.hindex 68e181d3..4d60d85c 100644--- a/tweak/Sources/Shared/HeadGestures/HeadGestures.h+++ b/tweak/Sources/Shared/HeadGestures/HeadGestures.h@@ -1,11 +1,13 @@-// AirPods gestures (Mod Settings > Player > AirPods gestures), under either look: a double nod adds the-// playing song to Liked Songs and a shake of the head skips it, read from the head motion that AirPods Pro,-// AirPods 3 and later, AirPods Max and some Beats report through CMHeadphoneMotionManager.+// AirPods gestures (Mod Settings > Player > AirPods gestures), under either look: a double nod and a shake of+// the head, each doing what its pull-down says (Like and Next track until changed), read from the head motion+// that AirPods Pro, AirPods 3 and later, AirPods Max and some Beats report through CMHeadphoneMotionManager.//// SGHeadDetector.m the gestures out of the motion, plain C, tested on the Mac (harness/head-gestures)// HeadGestures.x the motion listened to while the switch is on and Spotify plays, and what each-// gesture does: Spotify's player skips, its collection platform saves the track-// HeadGesturesSettings.m the page: the switch, the sensitivity, and learning one's own nod and shake+// gesture does through Spotify's player and its collection platform+// HeadGesturesSettings.m the page: the switch, each gesture's action, the sensitivity, Try it and Forget+// HeadGesturesTeach.m the teaching sheet: five of each gesture, one after each tone, over a ring that+// shows the head live//// HeadGestures.x owns the app's one CMHeadphoneMotionManager (iOS hands a manager's motion to one handler) and// lends its motion to other features through SGHeadMotionListen: Sing's spatial voice listens there.@@ -22,21 +24,65 @@// What learning set, in thousandths of a radian a second; unset or 0 is the detector's default.#define SGKeyHeadNod @"spotifyglass.headgestures.nod"#define SGKeyHeadShake @"spotifyglass.headgestures.shake"+// Each gesture's SGHeadAction; unset is Like for the nod and Next track for the shake.+#define SGKeyHeadNodAction @"spotifyglass.headgestures.nodaction"+#define SGKeyHeadShakeAction @"spotifyglass.headgestures.shakeaction"enum { SGHeadSensitivityMin = 50, SGHeadSensitivityMax = 200, SGHeadSensitivityStep = 10 };-// From the switch, the sensitivity and learning: listens or stops, and reads the thresholds again.+// What a gesture does, stored as this number: the order is the pull-down's and only grows at the end.+typedef NS_ENUM(NSInteger, SGHeadAction) {+ SGHeadActionNothing,+ SGHeadActionSeekBack, // 15 s+ SGHeadActionSeekForward, // 15 s+ SGHeadActionPlayPause,+ SGHeadActionNext,+ SGHeadActionPrevious,+ SGHeadActionShuffle, // on or off+ SGHeadActionRepeat, // off, the context, the track, as Spotify's button goes+ SGHeadActionLike, // into Liked Songs, never out+ SGHeadActionCount,+};+SGHeadAction SGHeadGestureAction(SGHeadGesture gesture);+void SGSetHeadGestureAction(SGHeadGesture gesture, SGHeadAction action);++// The cue's sounds, mixed into the music: one tone to time a gesture by, a rising pair when one worked, a low+// tone when it did not. A haptic comes with each while Spotify is in front.+typedef NS_ENUM(NSInteger, SGHeadCue) {+ SGHeadCueReady,+ SGHeadCueWorked,+ SGHeadCueFailed,+};+void SGHeadGesturesCue(SGHeadCue cue);++// From the switch, the sensitivity, the actions and learning: listens or stops, and reads the thresholds again.void SGHeadGesturesSettingsChanged(void);// Whether this iPhone can read headphone motion at all (iOS 14 and up; the headphones are another matter).BOOL SGHeadGesturesAvailable(void);-// Listens for `seconds` whatever plays and answers the threshold learned for `axis` (0 when the motion held-// no gesture) and how many samples came (0 when no headphones sent any, or motion is not allowed).-void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double threshold, NSInteger samples));-// Ends the learning that listens now, at once: nothing is stored, its done is never called, and the motion stops-// unless the gestures or another feature want it. A new learning can start straight after.-void SGHeadGesturesCancelLearn(void);++// The page's listening, whatever plays, during which no gesture does anything to the song. One at a time:+// starting one ends the last, and a stopped one's done is never called.+//+// Records `seconds` of motion and hands it back, three doubles a sample (time, pitch, yaw), empty when no+// headphones sent any or motion is not allowed.+void SGHeadGesturesRecord(double seconds, void (^done)(NSData *motion));+// Listens up to `seconds` at the thresholds set now, sensitivity included, and hands back the first gesture,+// or SGHeadGestureNone once the time is up; `heard` says whether any motion came.+void SGHeadGesturesTry(double seconds, void (^done)(SGHeadGesture gesture, BOOL heard));+void SGHeadGesturesStopListening(void);+// While held the motion runs whatever plays and no gesture does anything: the teaching sheet holds it open, so+// the headphones are sending already when each of its recordings starts.+void SGHeadGesturesHold(BOOL hold);++// The threshold one recording of the gesture of `axis` teaches, 0 when it holds no such gesture.+double SGHeadGesturesSampleThreshold(NSData *motion, SGHeadAxis axis);+// Learns `axis` from recordings of it (SGHeadLearnSamples, all but one of them at least) and stores the+// threshold; answers it, or 0 with nothing stored.+double SGHeadGesturesLearnFrom(NSArray<NSData *> *motions, SGHeadAxis axis);UIViewController *SGHeadGesturesSettingsPage(void);+// The teaching sheet over `owner`; `closed` runs once it is gone, whatever it stored.+void SGPresentHeadGesturesTeaching(UIViewController *owner, void (^closed)(void));// Head motion for another feature, under `name`; a nil handler takes it out. While any listener is in, the// manager runs whatever the gestures' switch says. A handler is called on the motion's serial queue with everydiff --git a/tweak/Sources/Shared/HeadGestures/HeadGestures.x b/tweak/Sources/Shared/HeadGestures/HeadGestures.xindex 9b70277c..0cf628ae 100644--- a/tweak/Sources/Shared/HeadGestures/HeadGestures.x+++ b/tweak/Sources/Shared/HeadGestures/HeadGestures.x@@ -1,21 +1,29 @@// AirPods gestures: CMHeadphoneMotionManager's attitudes fed to SGHeadDetector while the switch is on and-// Spotify plays, or while the page learns a gesture, and each gesture done to Spotify's player. The same-// manager's motion goes to the features listening through SGHeadMotionListen (Sing's spatial voice), and keeps-// running for them with the switch off; the detector is fed only while the gestures want it.+// Spotify plays, or while the page records, tries or holds the motion, and each gesture's action done to+// Spotify's player. The same manager's motion goes to the features listening through SGHeadMotionListen+// (Sing's spatial voice), and keeps running for them with the switch off; the detector is fed only while+// the gestures want it.//-// Skip is the player's own skipToNextTrackWithOptions: (Headers/SPTPlayer.h). Like adds the playing track to-// the collection, which for a track is Liked Songs, through -addURL:showUIConfirmation:completion: of-// Spotify's collection platform. Spotify 9.1.78 has four classes with that selector and with -stateProvider-// (SPTCollectionPlatformImplementation, Collection_PlatformImpl's CollectionPlatformImpl and-// CollectionPlatformMigration, AlignedCuration's ACUCollectionPlatform); whichever the app last reached-// for is kept, weakly, the way Redesigned/Artist/ArtistFollow.x keeps its state provider. The lock-// screen's like (LockScreenBaseRemoteControlPolicy's likeButtonPressedWithCompletion:identifier:) is not-// used: it toggles, and a second nod would take the song out again.+// The actions are the player's own commands (Headers/SPTPlayer.h) on SGKaraokePlayer, the ones the Live+// Activity's control menu sends: seekTo:, pause: and resume:, skipToNextTrackWithOptions: and+// skipToPreviousTrackWithOptions:, setShufflingContext:, and setRepeatingContext: with setRepeatingTrack:.+// Like adds the playing track to the collection, which for a track is Liked Songs, through+// -addURL:showUIConfirmation:completion: of Spotify's collection platform. Spotify 9.1.78 has four classes+// with that selector and with -stateProvider (SPTCollectionPlatformImplementation, Collection_PlatformImpl's+// CollectionPlatformImpl and CollectionPlatformMigration, AlignedCuration's ACUCollectionPlatform);+// whichever the app last reached for is kept, weakly, the way Redesigned/Artist/ArtistFollow.x keeps its+// state provider. The lock screen's like (LockScreenBaseRemoteControlPolicy's+// likeButtonPressedWithCompletion:identifier:) is not used: it toggles, and a second nod would take the+// song out again.+//+// The gestures listen while Spotify plays, and while a track is loaded and paused too when one of them is+// set to Play or pause, which could not resume otherwise.//// The cue is a tone played through Spotify's own playback session, mixed into the music in the AirPods:// a system sound would follow the Ring/Silent switch, and a phone in a pocket is often on silent. A rising-// pair says the gesture was done, one low tone that it failed. A haptic comes with it while Spotify is in-// front, and Spotify's own "Added to Liked Songs" toast shows only then too.+// pair says the gesture was done, one low tone that it failed, and a single middle one is the teaching+// sheet's signal to move. A haptic comes with it while Spotify is in front, and Spotify's own "Added to+// Liked Songs" toast shows only then too.#import <AudioToolbox/AudioToolbox.h>#import <AVFoundation/AVFoundation.h>#import <CoreMotion/CoreMotion.h>@@ -34,15 +42,18 @@ static const SystemSoundID kCueSound = 1057;static const double kToneSeconds = 0.09;static const double kToneLevel = 0.5;static const double kMilli = 1000;+static const double kSeekSeconds = 15;static CMHeadphoneMotionManager *sg_manager;-static NSOperationQueue *sg_queue; // serial: the detector, the learning buffer and the queue's copies live on it alone+static NSOperationQueue *sg_queue; // serial: the detector, the recording and the queue's copies live on it alonestatic SGHeadDetector sg_detector;-static NSMutableData *sg_learned; // learning's samples, three doubles each-static BOOL sg_learning;-static NSUInteger sg_learnTry; // which learning is current: a cancelled one's timer finds it moved on+static NSMutableData *sg_recorded; // a recording's samples, three doubles each+static NSInteger sg_queueHeard; // samples the detector was fed since a try started+static BOOL sg_recording, sg_trying, sg_held;+static void (^sg_tryDone)(SGHeadGesture gesture, BOOL heard);+static NSUInteger sg_ticket; // which recording or try is current: a stopped one's timer finds it moved onstatic BOOL sg_listening; // started with the handler, by updateListening-static BOOL sg_detecting; // the gestures want the motion: learning, or the switch on and Spotify playing+static BOOL sg_detecting; // the gestures want the motion: the page's listening, or the switch on and Spotify playingstatic BOOL sg_queueDetecting; // sg_detecting, on sg_queuestatic NSMutableDictionary<NSString *, void (^)(CMDeviceMotion *)> *sg_listeners; // other features', by namestatic NSArray<void (^)(CMDeviceMotion *)> *sg_queueListeners; // their handlers, on sg_queue@@ -93,7 +104,7 @@ static void keepPlatform(id platform) {}%end-#pragma mark - the gestures+#pragma mark - the cue// A WAV in memory of short tones one after the other, each faded in and out so it does not click.static NSData *tones(NSArray<NSNumber *> *hertz) {@@ -123,57 +134,112 @@ static dispatch_queue_t cueQueue(void) {return queue;}-// A rising pair for a gesture done, one low tone for one that failed. On cueQueue only.-static AVAudioPlayer *cuePlayer(BOOL worked) {- static AVAudioPlayer *done, *failed;+// The middle tone, the rising pair and the low tone, in SGHeadCue's order. On cueQueue only.+static AVAudioPlayer *cuePlayer(SGHeadCue cue) {+ static AVAudioPlayer *players[3];static dispatch_once_t once;dispatch_once(&once, ^{- NSError *error = nil;- done = [[AVAudioPlayer alloc] initWithData:tones(@[@880, @1320]) error:&error];- failed = [[AVAudioPlayer alloc] initWithData:tones(@[@330]) error:&error];- if (!done || !failed) SGLog(@"head gestures: no cue player, the system sound instead: %@", error);- [done prepareToPlay];- [failed prepareToPlay];+ NSArray<NSArray<NSNumber *> *> *sounds = @[@[@660], @[@880, @1320], @[@330]];+ for (NSUInteger i = 0; i < sounds.count; i++) {+ NSError *error = nil;+ players[i] = [[AVAudioPlayer alloc] initWithData:tones(sounds[i]) error:&error];+ if (!players[i]) SGLog(@"head gestures: no cue player, the system sound instead: %@", error);+ [players[i] prepareToPlay];+ }});- return worked ? done : failed;+ return players[cue];}-static void cue(BOOL worked) {+void SGHeadGesturesCue(SGHeadCue cue) {dispatch_async(cueQueue(), ^{- AVAudioPlayer *player = cuePlayer(worked);+ AVAudioPlayer *player = cuePlayer(cue);player.currentTime = 0;if (![player play]) AudioServicesPlaySystemSound(kCueSound);});if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) return;+ if (cue == SGHeadCueReady) {+ [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];+ return;+ }UINotificationFeedbackGenerator *generator = [UINotificationFeedbackGenerator new];- [generator notificationOccurred:worked ? UINotificationFeedbackTypeSuccess : UINotificationFeedbackTypeError];+ [generator notificationOccurred:cue == SGHeadCueWorked ? UINotificationFeedbackTypeSuccess : UINotificationFeedbackTypeError];+}++#pragma mark - the actions++SGHeadAction SGHeadGestureAction(SGHeadGesture gesture) {+ BOOL nod = gesture == SGHeadGestureDoubleNod;+ NSInteger action = SGInt(nod ? SGKeyHeadNodAction : SGKeyHeadShakeAction, nod ? SGHeadActionLike : SGHeadActionNext);+ return action >= 0 && action < SGHeadActionCount ? action : SGHeadActionNothing;+}++void SGSetHeadGestureAction(SGHeadGesture gesture, SGHeadAction action) {+ SGSetInt(gesture == SGHeadGestureDoubleNod ? SGKeyHeadNodAction : SGKeyHeadShakeAction, action);+ SGHeadGesturesSettingsChanged();}-static void like(void) {+static BOOL like(void) {NSString *uri = SGURIString(SGPlayerState().track.URI);id platform = sg_platform;// Only a track: an episode is saved another way, and an ad is nothing to save.if (![uri hasPrefix:@"spotify:track:"] || !platform) {SGLog(@"head gestures: no like for %@, platform %@", uri, platform);- cue(NO);- return;+ return NO;}BOOL inFront = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;// A block that reads no arguments: whatever the completion is called with, it is safe to ignore.[(id<SGCollectionPlatform>)platform addURL:[NSURL URLWithString:uri] showUIConfirmation:inFront completion:^{SGLog(@"head gestures: liked %@", uri);}];- cue(YES);+ return YES;}-static void skip(void) {- id player = SGKaraokePlayer();- if (![player respondsToSelector:@selector(skipToNextTrackWithOptions:)]) {- cue(NO);- return;+// Whether it was sent: the player answers later, if at all.+static BOOL perform(SGHeadAction action) {+ if (action == SGHeadActionLike) return like();+ id<SPTPlayer> player = SGKaraokePlayer();+ SPTPlayerState *state = [player respondsToSelector:@selector(state)] ? player.state : nil;+ if (!state) {+ SGLog(@"head gestures: no player for action %ld", (long)action);+ return NO;+ }+ switch (action) {+ case SGHeadActionSeekBack:+ case SGHeadActionSeekForward: {+ // A paused state's position would run on from when it was made; positionAsOfTimestamp holds still.+ double at = state.isPaused ? state.positionAsOfTimestamp : state.position;+ double to = at + (action == SGHeadActionSeekBack ? -kSeekSeconds : kSeekSeconds);+ if (state.duration > 0) to = MIN(to, state.duration);+ [player seekTo:MAX(0, to)];+ return YES;+ }+ case SGHeadActionPlayPause:+ if (state.isPaused) [player resume:nil];+ else [player pause:nil];+ return YES;+ case SGHeadActionNext:+ [player skipToNextTrackWithOptions:nil];+ return YES;+ case SGHeadActionPrevious:+ [player skipToPreviousTrackWithOptions:nil];+ return YES;+ case SGHeadActionShuffle:+ [player setShufflingContext:!state.options.shufflingContext];+ return YES;+ case SGHeadActionRepeat:+ // Off, then the playlist or album, then the track, then off again, the way Spotify's button goes.+ if (state.options.repeatingTrack) {+ [player setRepeatingTrack:NO];+ [player setRepeatingContext:NO];+ } else if (state.options.repeatingContext) {+ [player setRepeatingTrack:YES];+ } else {+ [player setRepeatingContext:YES];+ }+ return YES;+ default:+ return NO;}- [(id<SPTPlayer>)player skipToNextTrackWithOptions:nil];- cue(YES);}// What learning stored for `key`, or the detector's default.@@ -187,6 +253,11 @@ static double threshold(NSString *key, double fallback) {return learnedOr(key, fallback) / sensitivity;}+// The other gesture's threshold, as set now at a sensitivity of 100%: what learning `axis` checks with.+static double otherThreshold(SGHeadAxis axis) {+ return axis == SGHeadAxisPitch ? learnedOr(SGKeyHeadShake, SGHeadDefaultShake) : learnedOr(SGKeyHeadNod, SGHeadDefaultNod);+}+#pragma mark - listening// Nil to the other features' handlers: the motion has stopped. On sg_queue.@@ -194,23 +265,31 @@ static void motionStopped(void) {for (void (^handler)(CMDeviceMotion *) in sg_queueListeners) handler(nil);}+static void endTry(NSUInteger ticket, SGHeadGesture gesture, BOOL heard);+static void sample(CMDeviceMotion *motion) {for (void (^handler)(CMDeviceMotion *) in sg_queueListeners) handler(motion);double time = motion.timestamp, pitch = motion.attitude.pitch, yaw = motion.attitude.yaw;- if (sg_learned) {+ if (sg_recorded) {double values[3] = {time, pitch, yaw};- [sg_learned appendBytes:values length:sizeof values];+ [sg_recorded appendBytes:values length:sizeof values];return;}// Running for another feature alone, the gestures off or Spotify paused: no gestures.if (!sg_queueDetecting) return;+ sg_queueHeard++;SGHeadGesture gesture = SGHeadDetectorFeed(&sg_detector, time, pitch, yaw);if (gesture == SGHeadGestureNone) return;dispatch_async(dispatch_get_main_queue(), ^{SGLog(@"head gestures: %@", gesture == SGHeadGestureDoubleNod ? @"double nod" : @"shake");- if (sg_learning || !SGFlag(SGKeyHeadGestures, NO)) return;- if (gesture == SGHeadGestureDoubleNod) like();- else skip();+ if (sg_trying) {+ endTry(sg_ticket, gesture, YES);+ return;+ }+ if (sg_recording || sg_held || !SGFlag(SGKeyHeadGestures, NO)) return;+ SGHeadAction action = SGHeadGestureAction(gesture);+ if (action == SGHeadActionNothing) return;+ SGHeadGesturesCue(perform(action) ? SGHeadCueWorked : SGHeadCueFailed);});}@@ -244,20 +323,23 @@ static void resetDetector(void) {[sg_queue addOperationWithBlock:^{ SGHeadDetectorReset(&sg_detector, nod, shake); }];}-// The gestures want the motion while learning, or while the switch is on and Spotify plays; the manager runs-// then and while another feature listens. It sends nothing until headphones that track motion are in, so-// starting it with none costs nothing.+// The gestures want the motion while the page listens or holds it, or while the switch is on and Spotify+// plays (or has a track paused, for Play or pause); the manager runs then and while another feature listens.+// It sends nothing until headphones that track motion are in, so starting it with none costs nothing.static void updateListening(void) {// isPlaying stays on while a track is loaded and paused; isPaused is what tells.SPTPlayerState *state = SGPlayerState();- BOOL detect = sg_learning || (SGFlag(SGKeyHeadGestures, NO) && state.isPlaying && !state.isPaused);+ BOOL resumes = SGHeadGestureAction(SGHeadGestureDoubleNod) == SGHeadActionPlayPause+ || SGHeadGestureAction(SGHeadGestureShake) == SGHeadActionPlayPause;+ BOOL wanted = state.isPlaying && (!state.isPaused || resumes);+ BOOL detect = sg_recording || sg_trying || sg_held || (SGFlag(SGKeyHeadGestures, NO) && wanted);if (detect != sg_detecting) {sg_detecting = detect;manager();if (detect) {resetDetector();// Loaded now rather than with the first gesture's cue, which then plays on time.- dispatch_async(cueQueue(), ^{ cuePlayer(YES); });+ dispatch_async(cueQueue(), ^{ cuePlayer(SGHeadCueWorked); });}[sg_queue addOperationWithBlock:^{ sg_queueDetecting = detect; }];}@@ -308,54 +390,110 @@ BOOL SGHeadGesturesAvailable(void) {return manager().deviceMotionAvailable;}-void SGHeadGesturesLearn(SGHeadAxis axis, double seconds, void (^done)(double threshold, NSInteger samples)) {- if (sg_learning) {- done(0, 0);- return;- }- sg_learning = YES;- NSUInteger ticket = ++sg_learnTry;+#pragma mark - the page's listening++void SGHeadGesturesStopListening(void) {+ if (!sg_recording && !sg_trying) return;+ sg_recording = sg_trying = NO;+ sg_tryDone = nil;+ sg_ticket++;+ [sg_queue addOperationWithBlock:^{ sg_recorded = nil; }];+ SGLog(@"head gestures: the page's listening stopped");+ SGHeadGesturesSettingsChanged();+}++void SGHeadGesturesHold(BOOL hold) {+ if (hold == sg_held) return;+ sg_held = hold;+ manager();+ SGHeadGesturesSettingsChanged();+}++void SGHeadGesturesRecord(double seconds, void (^done)(NSData *motion)) {+ SGHeadGesturesStopListening();+ sg_recording = YES;+ NSUInteger ticket = ++sg_ticket;manager();- // The check after learning runs with the other gesture as it is set now, at a sensitivity of 100%.- double other = axis == SGHeadAxisPitch ? learnedOr(SGKeyHeadShake, SGHeadDefaultShake) : learnedOr(SGKeyHeadNod, SGHeadDefaultNod);- [sg_queue addOperationWithBlock:^{ sg_learned = [NSMutableData data]; }];+ [sg_queue addOperationWithBlock:^{ sg_recorded = [NSMutableData data]; }];SGHeadGesturesSettingsChanged();dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{- if (ticket != sg_learnTry) return;+ if (ticket != sg_ticket) return;[sg_queue addOperationWithBlock:^{- NSData *data = sg_learned;- sg_learned = nil;- const double *values = data.bytes;- int count = (int)(data.length / (3 * sizeof(double)));- double *time = malloc(sizeof(double) * MAX(count, 1)), *pitch = malloc(sizeof(double) * MAX(count, 1)), *yaw = malloc(sizeof(double) * MAX(count, 1));- for (int i = 0; i < count; i++) {- time[i] = values[3 * i];- pitch[i] = values[3 * i + 1];- yaw[i] = values[3 * i + 2];- }- double learned = SGHeadLearn(time, pitch, yaw, count, axis, other);- free(time);- free(pitch);- free(yaw);+ NSData *motion = sg_recorded ?: [NSData data];+ sg_recorded = nil;dispatch_async(dispatch_get_main_queue(), ^{- if (ticket != sg_learnTry) return;- SGLog(@"head gestures: learned %.2f rad/s for %@ from %d samples", learned, axis == SGHeadAxisPitch ? @"nod" : @"shake", count);- if (learned > 0) SGSetInt(axis == SGHeadAxisPitch ? SGKeyHeadNod : SGKeyHeadShake, lround(learned * kMilli));- sg_learning = NO;+ if (ticket != sg_ticket) return;+ sg_recording = NO;SGHeadGesturesSettingsChanged();- done(learned, count);+ done(motion);});}];});}-void SGHeadGesturesCancelLearn(void) {- if (!sg_learning) return;- sg_learning = NO;- sg_learnTry++;- [sg_queue addOperationWithBlock:^{ sg_learned = nil; }];- SGLog(@"head gestures: learning cancelled");+static void endTry(NSUInteger ticket, SGHeadGesture gesture, BOOL heard) {+ if (!sg_trying || ticket != sg_ticket) return;+ void (^done)(SGHeadGesture, BOOL) = sg_tryDone;+ sg_trying = NO;+ sg_tryDone = nil;+ sg_ticket++;+ SGHeadGesturesSettingsChanged();+ done(gesture, heard);+}++void SGHeadGesturesTry(double seconds, void (^done)(SGHeadGesture gesture, BOOL heard)) {+ SGHeadGesturesStopListening();+ sg_trying = YES;+ sg_tryDone = [done copy];+ NSUInteger ticket = ++sg_ticket;+ manager();+ [sg_queue addOperationWithBlock:^{ sg_queueHeard = 0; }];+ // A fresh detector, at the thresholds and sensitivity set now, even if the gestures were listening already.SGHeadGesturesSettingsChanged();+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ if (ticket != sg_ticket) return;+ [sg_queue addOperationWithBlock:^{+ BOOL heard = sg_queueHeard > 0;+ dispatch_async(dispatch_get_main_queue(), ^{ endTry(ticket, SGHeadGestureNone, heard); });+ }];+ });+}++#pragma mark - learning++// A recording's samples as the detector's three arrays, in one buffer: free the time's.+static SGHeadRecording unpack(NSData *motion) {+ int count = (int)(motion.length / (3 * sizeof(double)));+ const double *values = motion.bytes;+ double *buffer = malloc(sizeof(double) * 3 * MAX(count, 1));+ for (int i = 0; i < count; i++) {+ buffer[i] = values[3 * i];+ buffer[count + i] = values[3 * i + 1];+ buffer[2 * count + i] = values[3 * i + 2];+ }+ return (SGHeadRecording){buffer, buffer + count, buffer + 2 * count, count};+}++double SGHeadGesturesSampleThreshold(NSData *motion, SGHeadAxis axis) {+ SGHeadRecording recording = unpack(motion);+ double learned = SGHeadLearn(recording.time, recording.pitch, recording.yaw, recording.count, axis, otherThreshold(axis));+ free((void *)recording.time);+ return learned;+}++double SGHeadGesturesLearnFrom(NSArray<NSData *> *motions, SGHeadAxis axis) {+ int n = (int)motions.count;+ SGHeadRecording *recordings = calloc(MAX(n, 1), sizeof *recordings);+ for (int i = 0; i < n; i++) recordings[i] = unpack(motions[i]);+ double learned = SGHeadLearnSamples(recordings, n, axis, otherThreshold(axis), MAX(1, n - 1));+ for (int i = 0; i < n; i++) free((void *)recordings[i].time);+ free(recordings);+ SGLog(@"head gestures: learned %.2f rad/s for %@ from %d recordings", learned, axis == SGHeadAxisPitch ? @"nod" : @"shake", n);+ if (learned > 0) {+ SGSetInt(axis == SGHeadAxisPitch ? SGKeyHeadNod : SGKeyHeadShake, lround(learned * kMilli));+ SGHeadGesturesSettingsChanged();+ }+ return learned;}// Playing or not, from the one player hook every feature shares.diff --git a/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m b/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.mindex 90dc8417..58c61c0e 100644--- a/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m+++ b/tweak/Sources/Shared/HeadGestures/HeadGesturesSettings.m@@ -1,15 +1,18 @@-// The AirPods gestures page: the switch, the sensitivity, and learning one's own nod and shake. Learning-// listens for a few seconds under an alert that says what to do, then says what it found.+// The AirPods gestures page: the switch, a pull-down of what each gesture does, then fitting them to the+// user: the teaching sheet, the sensitivity, Try it, which listens a few seconds and names what it picked up+// in its own row without doing anything to the song, and Forget.#import "Core/SGCore.h"#import "Settings/SGModPage.h"#import "Settings/SGPageStyle.h"#import "HeadGestures.h"-// Long enough for a slow double nod and a breath before it.-static const double kLearnSeconds = 4;+// Long enough to get the AirPods' attention and make a slow double nod.+static const double kTrySeconds = 6;-static NSString *learnedValue(NSString *key) {- return SGInt(key, 0) > 0 ? @"Learned" : @"Default";+// SGHeadAction's names, in its order.+static NSArray<NSString *> *actionNames(void) {+ return @[@"Nothing", @"Back 15 seconds", @"Forward 15 seconds", @"Play or pause", @"Next track", @"Previous track",+ @"Shuffle", @"Repeat", @"Like"];}static void say(NSString *title, NSString *message) {@@ -18,48 +21,25 @@ static void say(NSString *title, NSString *message) {[SGTopController() presentViewController:alert animated:YES completion:nil];}-static BOOL anythingLearned(void) {- return SGInt(SGKeyHeadNod, 0) > 0 || SGInt(SGKeyHeadShake, 0) > 0;+static BOOL headTracking(void) {+ if (SGHeadGesturesAvailable()) return YES;+ say(@"No head tracking", @"This iPhone cannot read the motion of headphones.");+ return NO;}-// `done` runs once something may be stored, before the alert that says what: the page shows Forget then.-static void learn(SGHeadAxis axis, void (^done)(void)) {- if (!SGHeadGesturesAvailable()) {- say(@"No head tracking", @"This iPhone cannot read the motion of headphones.");- return;- }- BOOL nod = axis == SGHeadAxisPitch;- // Cancel tapped while the alert was still coming in: learning never starts.- __block BOOL cancelled = NO;- UIAlertController *asking = [UIAlertController alertControllerWithTitle:nod ? @"Nod twice" : @"Shake your head"- message:nod ? @"Now, the way you would to like a song." : @"Now, the way you would to skip a song."- preferredStyle:UIAlertControllerStyleAlert];- [asking addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {- cancelled = YES;- SGHeadGesturesCancelLearn();- }]];- [SGTopController() presentViewController:asking animated:YES completion:^{- if (cancelled) return;- SGHeadGesturesLearn(axis, kLearnSeconds, ^(double threshold, NSInteger samples) {- done();- [asking dismissViewControllerAnimated:YES completion:^{- if (samples == 0)- say(@"No motion came", @"Put in AirPods that track head motion (AirPods Pro, AirPods 3 or later, "- "AirPods Max), and allow Spotify in Settings > Privacy & Security > Motion & Fitness.");- else if (threshold <= 0)- say(nod ? @"No double nod found" : @"No shake found",- nod ? @"Nod twice, down and up, a little quicker." : @"Shake your head left and right, a little quicker.");- else- say(@"Learned", nod ? @"A double nod this size likes the song." : @"A shake this size skips the song.");- }];- });- }];+static NSString *learnedValue(void) {+ BOOL nod = SGInt(SGKeyHeadNod, 0) > 0, shake = SGInt(SGKeyHeadShake, 0) > 0;+ return nod && shake ? @"Learned" : nod ? @"Nod learned" : shake ? @"Shake learned" : @"";+}++static BOOL anythingLearned(void) {+ return SGInt(SGKeyHeadNod, 0) > 0 || SGInt(SGKeyHeadShake, 0) > 0;}// Both learned sizes back to the defaults, asked first; `done` runs once they are.static void forget(void (^done)(void)) {UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Forget your nod and shake?"- message:@"Both go back to the default sizes. You can learn them again." preferredStyle:UIAlertControllerStyleAlert];+ message:@"Both go back to the default sizes. You can teach them again." preferredStyle:UIAlertControllerStyleAlert];[alert addAction:[UIAlertAction actionWithTitle:@"Forget" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {SGSetInt(SGKeyHeadNod, 0);SGSetInt(SGKeyHeadShake, 0);@@ -70,10 +50,24 @@ static void forget(void (^done)(void)) {[SGTopController() presentViewController:alert animated:YES completion:nil];}+static SGModRow *actionRow(NSString *title, SGHeadGesture gesture) {+ return SGMenuRow(title, actionNames(), ^NSString *{ return actionNames()[(NSUInteger)SGHeadGestureAction(gesture)]; },+ ^(NSInteger index) { SGSetHeadGestureAction(gesture, index); });+}+UIViewController *SGHeadGesturesSettingsPage(void) {- SGModRow *toggle = SGOptionRow(@"AirPods gestures", @"Nod twice to like the song, shake your head to skip it", SGKeyHeadGestures);+ SGModRow *toggle = SGOptionRow(@"AirPods gestures", @"Nod twice or shake your head to control the music", SGKeyHeadGestures);toggle.changed = ^(BOOL on) { SGHeadGesturesSettingsChanged(); };+ SGModRow *nodAction = SGWithSymbol(actionRow(@"Double nod", SGHeadGestureDoubleNod), @"arrow.up.and.down");+ SGModRow *shakeAction = SGWithSymbol(actionRow(@"Shake", SGHeadGestureShake), @"arrow.left.and.right");++ __block __weak SGModPage *page;+ SGModRow *teach = SGStatActionRow(@"Teach your gestures", @"Five of each, one after each tone", ^NSString *{ return learnedValue(); }, ^{+ if (!headTracking()) return;+ SGPresentHeadGesturesTeaching(page, ^{ [page refreshVisibility]; });+ });+SGModRow *sensitivity = SGSliderRow(@"Sensitivity", @"Higher counts a smaller move",SGHeadSensitivityMin, SGHeadSensitivityMax, SGHeadSensitivityStep,^double { return MAX(SGHeadSensitivityMin, MIN(SGHeadSensitivityMax, SGInt(SGKeyHeadSensitivity, 100))); },@@ -82,24 +76,42 @@ UIViewController *SGHeadGesturesSettingsPage(void) {SGHeadGesturesSettingsChanged();},^NSString *(double value) { return [NSString stringWithFormat:@"%ld%%", lround(value)]; });- sensitivity.waitsOn = SGKeyHeadGestures;- // Learning and forgetting change what Forget's row reads, which no switch on the page tells it.- __block __weak SGModPage *page;- void (^refresh)(void) = ^{ [page refreshVisibility]; };- SGModRow *nod = SGStatActionRow(@"Learn your nod", nil, ^NSString *{ return learnedValue(SGKeyHeadNod); }, ^{ learn(SGHeadAxisPitch, refresh); });- SGModRow *shake = SGStatActionRow(@"Learn your shake", nil, ^NSString *{ return learnedValue(SGKeyHeadShake); }, ^{ learn(SGHeadAxisYaw, refresh); });- SGModRow *forgetRow = SGActionRow(@"Forget what it learned", nil, ^{ forget(refresh); });+ // What the last try picked up, read out on its row until the next; a new page starts blank.+ __block NSString *tried = @"";+ __block BOOL trying = NO;+ SGModRow *tryRow = SGStatActionRow(@"Try it", @"Listens after the tone and says what it picked up", ^NSString *{ return tried; }, ^{+ if (trying || !headTracking()) return;+ trying = YES;+ tried = @"Listening…";+ [page.tableView reloadData];+ SGHeadGesturesCue(SGHeadCueReady);+ SGHeadGesturesTry(kTrySeconds, ^(SGHeadGesture gesture, BOOL heard) {+ trying = NO;+ tried = gesture == SGHeadGestureDoubleNod ? @"Double nod" : gesture == SGHeadGestureShake ? @"Shake" : heard ? @"Nothing" : @"No motion";+ SGHeadGesturesCue(gesture != SGHeadGestureNone ? SGHeadCueWorked : SGHeadCueFailed);+ NSString *spoken = gesture != SGHeadGestureNone ? [@"Picked up: " stringByAppendingString:tried]+ : heard ? @"Picked up nothing. Try a bigger move, or raise Sensitivity."+ : @"No motion came. Put in AirPods that track head motion.";+ UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, spoken);+ [page.tableView reloadData];+ });+ });++ SGModRow *forgetRow = SGActionRow(@"Forget what it learned", nil, ^{ forget(^{ [page refreshVisibility]; }); });forgetRow.color = SGRed();forgetRow.visible = ^BOOL { return anythingLearned(); };- nod.waitsOn = shake.waitsOn = forgetRow.waitsOn = SGKeyHeadGestures;+ for (SGModRow *row in @[nodAction, shakeAction, teach, sensitivity, tryRow, forgetRow]) row.waitsOn = SGKeyHeadGestures;SGModPage *shown = [[SGModPage alloc] initWithTitle:@"AirPods gestures" intro:nil sections:@[- SGSection(nil, @[SGWithSymbol(toggle, @"airpods.pro"), sensitivity]),- SGNotedSection(@"Learn", @[nod, shake, forgetRow],- @"Learning listens for a few seconds and sets how big a move counts, from yours. The gestures work while "- "Spotify plays through headphones that track head motion. A tone in the music confirms each one, a rising pair when "- "it worked and a low one when it did not, with the iPhone on silent too."),+ SGSection(nil, @[SGWithSymbol(toggle, @"airpods.pro")]),+ SGNotedSection(@"Gestures", @[nodAction, shakeAction],+ @"They work while Spotify plays through headphones that track head motion: AirPods Pro, AirPods 3 or later, "+ "AirPods Max and some Beats. Set to Play or pause, they listen with a song paused too. A tone in the music "+ "confirms each, a rising pair when it worked and a low one when it did not, with the iPhone on silent too."),+ SGNotedSection(@"Fit to you", @[teach, sensitivity, tryRow, forgetRow],+ @"Teaching sets how big a move counts from five of your own. Sensitivity scales that for both. Try it does "+ "nothing to the music, so you can tune it while a song plays."),] footer:nil];page = shown;return shown;diff --git a/tweak/Sources/Shared/HeadGestures/HeadGesturesTeach.m b/tweak/Sources/Shared/HeadGestures/HeadGesturesTeach.mnew file mode 100644index 00000000..cce7577f--- /dev/null+++ b/tweak/Sources/Shared/HeadGestures/HeadGesturesTeach.m@@ -0,0 +1,553 @@+// The teaching sheet: five double nods, then five shakes, each made after a tone in the music. A ring shows the+// head as the AirPods report it, live: a dot in the middle moves with the head, down and up with a nod's pitch,+// left and right with a shake's yaw, a typical gesture taking it near the ring, and the ticks round the ring+// light up toward where the head went and how far, fading back as it settles (Reduce Motion: the dot moves,+// no ticks light). Five dots and "2 of 5" under the ring count what landed. A recording that holds no such+// gesture is not counted and is taken again; Redo last drops the last one counted. Five in, the threshold is+// learned from them together (SGHeadGesturesLearnFrom) and stored, the nod's before the shake begins, so a+// Cancel then keeps it.+//+// The sheet holds the motion open while it is up (SGHeadGesturesHold), so the system's Motion & Fitness prompt,+// the first time, comes over it before Start, and no gesture does anything to the song meanwhile. The ring+// reads the motion through SGHeadMotionListen and draws at 60 fps at most, only while the sheet shows.+#import <QuartzCore/QuartzCore.h>+#import <os/lock.h>+#import "Core/SGCore.h"+#import "Settings/SGPageStyle.h"+#import "HeadGestures.h"++static const NSInteger kSamples = 5;+// After the tone: a moment to react, a slow double nod and its settle.+static const double kWindow = 2.5;+// A beat to read the line before the next tone; with VoiceOver, room for its announcement first.+static const double kGap = 1.2;+static const double kSpokenGap = 3;+static const double kFade = 0.2;++// The ring: its size, its ticks, and the dot.+static const CGFloat kRingSize = 220;+enum { kTicks = 60 };+static const CGFloat kTickLength = 10, kTickWidth = 3, kDotSize = 18;+// How far the head turns, from where it rests, to take the dot to the ring: a brisk nod dips about 17+// degrees, a shake swings about 12 either side.+static const double kPitchReach = 0.3, kYawReach = 0.22;+// Where the head rests follows it this slowly, so a gesture moves the dot and a new posture does not.+static const double kRestSeconds = 1.2;+// The dot follows the motion's 25 samples a second this closely, so it glides between them at 60 fps.+static const double kFollowSeconds = 0.05;+// A lit tick fades back this slowly; the light spreads this far either side of the head's direction.+static const double kTickFadeSeconds = 0.45, kTickSpread = M_PI / 7;+// Under this share of the reach the head is still: no ticks light.+static const double kStill = 0.12;++static UILabel *label(UIFontTextStyle style, UIFontWeight weight, UIColor *color) {+ UILabel *view = [UILabel new];+ UIFont *font = [UIFont systemFontOfSize:[UIFont preferredFontForTextStyle:style].pointSize weight:weight];+ view.font = [[UIFontMetrics metricsForTextStyle:style] scaledFontForFont:font];+ view.adjustsFontForContentSizeCategory = YES;+ view.textColor = color;+ view.textAlignment = NSTextAlignmentCenter;+ view.numberOfLines = 0;+ view.translatesAutoresizingMaskIntoConstraints = NO;+ return view;+}++static void announce(NSString *text) {+ UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, text);+}++#pragma mark - the head, live++// The last attitude and where the head rests, written on the motion's queue, read by the ring on the main one.+static os_unfair_lock sg_poseLock = OS_UNFAIR_LOCK_INIT;+static struct {+ double pitch, yaw, restPitch, restYaw, time;+ BOOL has;+} sg_pose;++static void headMoved(CMDeviceMotion *motion) {+ os_unfair_lock_lock(&sg_poseLock);+ if (!motion) {+ sg_pose.has = NO;+ } else {+ double pitch = motion.attitude.pitch, yaw = motion.attitude.yaw, time = motion.timestamp, dt = time - sg_pose.time;+ if (!sg_pose.has || dt <= 0 || dt > 0.3) {+ sg_pose.restPitch = pitch;+ sg_pose.restYaw = yaw;+ } else {+ double k = 1 - exp(-dt / kRestSeconds);+ sg_pose.restPitch += k * (pitch - sg_pose.restPitch);+ sg_pose.restYaw = remainder(sg_pose.restYaw + k * remainder(yaw - sg_pose.restYaw, 2 * M_PI), 2 * M_PI);+ }+ sg_pose.pitch = pitch;+ sg_pose.yaw = yaw;+ sg_pose.time = time;+ sg_pose.has = YES;+ }+ os_unfair_lock_unlock(&sg_poseLock);+}++// How far the head is from where it rests, in radians: down and to the left are negative pitch and positive yaw.+static BOOL headOffset(double *pitch, double *yaw) {+ os_unfair_lock_lock(&sg_poseLock);+ BOOL has = sg_pose.has;+ *pitch = has ? sg_pose.pitch - sg_pose.restPitch : 0;+ *yaw = has ? remainder(sg_pose.yaw - sg_pose.restYaw, 2 * M_PI) : 0;+ os_unfair_lock_unlock(&sg_poseLock);+ return has;+}++#pragma mark - the ring++@interface SGTeachRing : UIView+// Listening and drawing, from when the sheet shows to when it goes.+- (void)start;+- (void)stop;+// The end: every tick lit, a checkmark for the dot, nothing more drawn.+- (void)showDone;+@end++@implementation SGTeachRing {+ CAShapeLayer *_track;+ NSArray<CAShapeLayer *> *_ticks;+ double _levels[kTicks];+ CALayer *_dot;+ CGPoint _dotAt; // from the centre+ UIImageView *_check;+ CADisplayLink *_link;+ CFTimeInterval _lastFrame;+}++- (instancetype)initWithFrame:(CGRect)frame {+ if (!(self = [super initWithFrame:frame])) return nil;+ _track = [CAShapeLayer layer];+ _track.strokeColor = [UIColor colorWithWhite:1 alpha:0.16].CGColor;+ _track.lineWidth = kTickWidth;+ _track.lineCap = kCALineCapRound;+ [self.layer addSublayer:_track];+ NSMutableArray<CAShapeLayer *> *ticks = [NSMutableArray array];+ for (NSInteger i = 0; i < kTicks; i++) {+ CAShapeLayer *tick = [CAShapeLayer layer];+ tick.strokeColor = SGGreen().CGColor;+ tick.lineWidth = kTickWidth;+ tick.lineCap = kCALineCapRound;+ tick.opacity = 0;+ tick.actions = @{@"opacity": NSNull.null}; // set every frame: no implicit fade on top+ [self.layer addSublayer:tick];+ [ticks addObject:tick];+ }+ _ticks = ticks;++ _dot = [CALayer layer];+ _dot.bounds = CGRectMake(0, 0, kDotSize, kDotSize);+ _dot.cornerRadius = kDotSize / 2;+ _dot.backgroundColor = UIColor.whiteColor.CGColor;+ _dot.actions = @{@"position": NSNull.null};+ [self.layer addSublayer:_dot];++ _check = SGSymbolView(@"checkmark", 34, UIImageSymbolWeightSemibold, 44);+ _check.tintColor = SGGreen();+ _check.hidden = YES;+ [self addSubview:_check];++ self.isAccessibilityElement = YES;+ self.accessibilityTraits = UIAccessibilityTraitUpdatesFrequently;+ return self;+}++- (CGFloat)radius {+ return MIN(self.bounds.size.width, self.bounds.size.height) / 2 - kTickWidth;+}++// How far the dot goes: inside the ticks with room to spare.+- (CGFloat)reach {+ return self.radius - kTickLength - kDotSize / 2 - 6;+}++- (CGPoint)centre {+ return CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));+}++- (void)layoutSubviews {+ [super layoutSubviews];+ CGPoint centre = self.centre;+ CGFloat outer = self.radius, inner = outer - kTickLength;+ UIBezierPath *all = [UIBezierPath bezierPath];+ for (NSInteger i = 0; i < kTicks; i++) {+ CGFloat angle = 2 * M_PI * i / kTicks - M_PI_2;+ UIBezierPath *tick = [UIBezierPath bezierPath];+ [tick moveToPoint:CGPointMake(centre.x + inner * cos(angle), centre.y + inner * sin(angle))];+ [tick addLineToPoint:CGPointMake(centre.x + outer * cos(angle), centre.y + outer * sin(angle))];+ _ticks[i].path = tick.CGPath;+ [all appendPath:tick];+ }+ _track.path = all.CGPath;+ _dot.position = CGPointMake(centre.x + _dotAt.x, centre.y + _dotAt.y);+ _check.center = centre;+}++- (void)start {+ if (_link) return;+ SGHeadMotionListen(@"teaching", ^(CMDeviceMotion *motion) { headMoved(motion); });+ _lastFrame = 0;+ _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(frame:)];+ _link.preferredFrameRateRange = CAFrameRateRangeMake(30, 60, 60);+ [_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];+}++- (void)stop {+ [_link invalidate];+ _link = nil;+ SGHeadMotionListen(@"teaching", nil);+}++- (void)frame:(CADisplayLink *)link {+ double dt = _lastFrame ? MIN(link.timestamp - _lastFrame, 0.1) : 1.0 / 60;+ _lastFrame = link.timestamp;++ // Screen down is a nod's down (pitch falling), screen left the head turned left (yaw rising).+ double pitch, yaw;+ headOffset(&pitch, &yaw);+ CGFloat reach = self.reach;+ CGFloat x = -yaw / kYawReach * reach, y = -pitch / kPitchReach * reach;+ CGFloat distance = hypot(x, y);+ if (distance > reach) {+ x *= reach / distance;+ y *= reach / distance;+ }+ double follow = 1 - exp(-dt / kFollowSeconds);+ _dotAt = CGPointMake(_dotAt.x + (x - _dotAt.x) * follow, _dotAt.y + (y - _dotAt.y) * follow);+ CGPoint centre = self.centre;+ _dot.position = CGPointMake(centre.x + _dotAt.x, centre.y + _dotAt.y);++ if (UIAccessibilityIsReduceMotionEnabled()) {+ for (CAShapeLayer *tick in _ticks) tick.opacity = 0;+ return;+ }+ double strength = MIN(1, distance / reach), toward = atan2(y, x), fade = exp(-dt / kTickFadeSeconds);+ for (NSInteger i = 0; i < kTicks; i++) {+ double angle = 2 * M_PI * i / kTicks - M_PI_2;+ double off = fabs(remainder(angle - toward, 2 * M_PI));+ double lit = strength > kStill ? strength * MAX(0, 1 - off / kTickSpread) : 0;+ _levels[i] = MAX(_levels[i] * fade, lit);+ float opacity = _levels[i] < 0.02 ? 0 : (float)_levels[i];+ if (fabsf(_ticks[i].opacity - opacity) > 0.01f) _ticks[i].opacity = opacity;+ }+}++- (void)showDone {+ [self stop];+ _dot.hidden = YES;+ _check.hidden = NO;+ [CATransaction begin];+ [CATransaction setAnimationDuration:kFade];+ for (CAShapeLayer *tick in _ticks) {+ tick.actions = nil;+ tick.opacity = 1;+ }+ [CATransaction commit];+}++@end++#pragma mark - the sheet++@interface SGTeachController : UIViewController+@end++@implementation SGTeachController {+ void (^_closed)(void);+ SGTeachRing *_dial;+ NSArray<UIView *> *_dots;+ UILabel *_count, *_heading, *_line;+ UIButton *_primaryButton, *_redoButton;+ NSMutableArray<NSData *> *_samples;+ SGHeadAxis _axis;+ BOOL _running, _finished;+ BOOL _listening; // a recording is open+ NSUInteger _step; // which scheduled step is current: Redo, a restart and closing move it on+}++- (instancetype)initWithClosed:(void (^)(void))closed {+ if (!(self = [super initWithNibName:nil bundle:nil])) return nil;+ _closed = [closed copy];+ _samples = [NSMutableArray array];+ self.title = @"Teach your gestures";+ return self;+}++- (BOOL)nod {+ return _axis == SGHeadAxisPitch;+}++- (UIButton *)buttonTitled:(NSString *)title prominent:(BOOL)prominent action:(SEL)action {+ UIButtonConfiguration *config;+ if (@available(iOS 26.0, *)) config = prominent ? [UIButtonConfiguration prominentGlassButtonConfiguration] : [UIButtonConfiguration glassButtonConfiguration];+ else config = prominent ? [UIButtonConfiguration filledButtonConfiguration] : [UIButtonConfiguration grayButtonConfiguration];+ config.cornerStyle = UIButtonConfigurationCornerStyleCapsule;+ if (prominent) {+ config.baseBackgroundColor = SGGreen();+ config.baseForegroundColor = UIColor.blackColor;+ } else {+ config.baseForegroundColor = UIColor.whiteColor;+ }+ config.contentInsets = NSDirectionalEdgeInsetsMake(15, 20, 15, 20);+ config.title = title;+ config.titleTextAttributesTransformer = ^NSDictionary *(NSDictionary *attributes) {+ NSMutableDictionary *styled = [attributes mutableCopy];+ UIFont *font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];+ styled[NSFontAttributeName] = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:font];+ return styled;+ };+ UIButton *button = [UIButton buttonWithConfiguration:config primaryAction:nil];+ [button addTarget:self action:action forControlEvents:UIControlEventPrimaryActionTriggered];+ return button;+}++- (void)setPrimaryTitle:(NSString *)title {+ UIButtonConfiguration *config = _primaryButton.configuration;+ config.title = title;+ _primaryButton.configuration = config;+ _primaryButton.hidden = title == nil;+}++// Five dots and "2 of 5" beside them, one line under the ring; VoiceOver reads the count off the ring.+- (UIView *)progressRow {+ NSMutableArray<UIView *> *dots = [NSMutableArray array];+ for (NSInteger i = 0; i < kSamples; i++) {+ UIView *dot = [UIView new];+ dot.layer.cornerRadius = 4;+ dot.translatesAutoresizingMaskIntoConstraints = NO;+ [NSLayoutConstraint activateConstraints:@[[dot.widthAnchor constraintEqualToConstant:8], [dot.heightAnchor constraintEqualToConstant:8]]];+ [dots addObject:dot];+ }+ _dots = dots;+ UIStackView *row = [[UIStackView alloc] initWithArrangedSubviews:dots];+ row.spacing = 6;+ row.alignment = UIStackViewAlignmentCenter;+ UIFont *digits = [UIFont monospacedDigitSystemFontOfSize:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline].pointSize weight:UIFontWeightSemibold];+ _count = label(UIFontTextStyleSubheadline, UIFontWeightSemibold, SGGrey());+ _count.font = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleSubheadline] scaledFontForFont:digits];+ _count.numberOfLines = 1;+ UIStackView *line = [[UIStackView alloc] initWithArrangedSubviews:@[row, _count]];+ line.spacing = 12;+ line.alignment = UIStackViewAlignmentCenter;+ line.translatesAutoresizingMaskIntoConstraints = NO;+ line.accessibilityElementsHidden = YES;+ return line;+}++- (void)viewDidLoad {+ [super viewDidLoad];+ // iOS 26 gives a sheet its own material; before it the sheet takes the mod's page colour.+ if (@available(iOS 26.0, *)) {} else self.view.backgroundColor = SGPageBackground();+ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];++ _dial = [SGTeachRing new];+ _dial.translatesAutoresizingMaskIntoConstraints = NO;+ UIView *progress = [self progressRow];+ _heading = label(UIFontTextStyleTitle2, UIFontWeightBold, UIColor.whiteColor);+ _heading.accessibilityTraits = UIAccessibilityTraitHeader;+ _line = label(UIFontTextStyleBody, UIFontWeightRegular, SGGrey());+ _primaryButton = [self buttonTitled:@"Start" prominent:YES action:@selector(primary)];+ _redoButton = [self buttonTitled:@"Redo last" prominent:NO action:@selector(redo)];+ UIStackView *buttons = [[UIStackView alloc] initWithArrangedSubviews:@[_redoButton, _primaryButton]];+ buttons.axis = UILayoutConstraintAxisVertical;+ buttons.spacing = 12;+ buttons.translatesAutoresizingMaskIntoConstraints = NO;+ for (UIView *view in @[_dial, progress, _heading, _line, buttons]) [self.view addSubview:view];++ // The ring stays put as the lines under it change length.+ UILayoutGuide *safe = self.view.safeAreaLayoutGuide;+ NSLayoutConstraint *size = [_dial.widthAnchor constraintEqualToConstant:kRingSize];+ size.priority = UILayoutPriorityDefaultHigh;+ [NSLayoutConstraint activateConstraints:@[+ [_dial.topAnchor constraintEqualToAnchor:safe.topAnchor constant:28],+ [_dial.centerXAnchor constraintEqualToAnchor:safe.centerXAnchor],+ [_dial.heightAnchor constraintEqualToAnchor:_dial.widthAnchor],+ [_dial.widthAnchor constraintLessThanOrEqualToAnchor:safe.widthAnchor constant:-80],+ size,+ [progress.topAnchor constraintEqualToAnchor:_dial.bottomAnchor constant:24],+ [progress.centerXAnchor constraintEqualToAnchor:safe.centerXAnchor],+ [_heading.topAnchor constraintEqualToAnchor:progress.bottomAnchor constant:24],+ [_heading.leadingAnchor constraintEqualToAnchor:safe.leadingAnchor constant:32],+ [_heading.trailingAnchor constraintEqualToAnchor:safe.trailingAnchor constant:-32],+ [_line.topAnchor constraintEqualToAnchor:_heading.bottomAnchor constant:8],+ [_line.leadingAnchor constraintEqualToAnchor:_heading.leadingAnchor],+ [_line.trailingAnchor constraintEqualToAnchor:_heading.trailingAnchor],+ [_line.bottomAnchor constraintLessThanOrEqualToAnchor:buttons.topAnchor constant:-16],+ [buttons.leadingAnchor constraintEqualToAnchor:safe.leadingAnchor constant:24],+ [buttons.trailingAnchor constraintEqualToAnchor:safe.trailingAnchor constant:-24],+ [buttons.bottomAnchor constraintEqualToAnchor:safe.bottomAnchor constant:-16],+ ]];++ [self showGesture];+ _line.text = @"After each tone, nod twice the way you would say yes. Five nods, then five shakes.";+ _redoButton.hidden = YES;+}++- (void)viewDidAppear:(BOOL)animated {+ [super viewDidAppear:animated];+ SGHeadGesturesHold(YES);+ if (!_finished) [_dial start];+}++- (void)viewDidDisappear:(BOOL)animated {+ [super viewDidDisappear:animated];+ _step++;+ _listening = NO;+ [_dial stop];+ SGHeadGesturesStopListening();+ SGHeadGesturesHold(NO);+ if (_closed) _closed();+ _closed = nil;+}++#pragma mark - what it shows++- (void)showGesture {+ _heading.text = self.nod ? @"Double nod" : @"Shake";+ _dial.accessibilityLabel = self.nod ? @"Double nods" : @"Shakes";+ [self showCount];+}++- (void)showCount {+ NSInteger landed = (NSInteger)_samples.count;+ _count.text = [NSString stringWithFormat:@"%ld of %ld", (long)landed, (long)kSamples];+ _dial.accessibilityValue = _count.text;+ // Landed dots full, the one being recorded dim, the rest grey: a colour change, so Reduce Motion keeps it.+ [UIView animateWithDuration:kFade delay:0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState animations:^{+ for (NSInteger i = 0; i < kSamples; i++) {+ BOOL recording = i == landed && self->_listening;+ self->_dots[i].backgroundColor = i < landed ? SGGreen() : recording ? [SGGreen() colorWithAlphaComponent:0.4] : [UIColor colorWithWhite:1 alpha:0.18];+ }+ } completion:nil];+ _redoButton.hidden = !_running || _finished;+ _redoButton.enabled = landed > 0;+}++- (double)gap {+ return UIAccessibilityIsVoiceOverRunning() ? kSpokenGap : kGap;+}++#pragma mark - the steps++// Runs `next` after `delay` unless something moved the steps on meanwhile.+- (void)after:(double)delay then:(void (^)(void))next {+ NSUInteger step = ++_step;+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ if (step == self->_step) next();+ });+}++- (void)primary {+ if (_finished) {+ [self dismissViewControllerAnimated:YES completion:nil];+ return;+ }+ _running = YES;+ [self setPrimaryTitle:nil];+ [self record];+}++- (void)record {+ NSUInteger step = ++_step;+ _listening = YES;+ _line.text = self.nod ? @"Nod twice now." : @"Shake your head now.";+ [self showCount];+ SGHeadGesturesCue(SGHeadCueReady);+ SGHeadGesturesRecord(kWindow, ^(NSData *motion) {+ if (step == self->_step) [self recorded:motion];+ });+}++- (void)recorded:(NSData *)motion {+ _listening = NO;+ if (!motion.length) {+ _running = NO;+ [self showCount];+ SGHeadGesturesCue(SGHeadCueFailed);+ _line.text = @"No motion came in. Put in AirPods that track head motion, and allow Spotify in Settings > "+ "Privacy & Security > Motion & Fitness.";+ announce(_line.text);+ [self setPrimaryTitle:@"Try again"];+ return;+ }+ BOOL counted = SGHeadGesturesSampleThreshold(motion, _axis) > 0;+ if (counted) [_samples addObject:motion];+ [self showCount];+ SGHeadGesturesCue(counted ? SGHeadCueWorked : SGHeadCueFailed);+ NSInteger landed = (NSInteger)_samples.count;+ if (counted) {+ _line.text = landed < kSamples ? @"Got it." : self.nod ? @"That's five nods." : @"That's five shakes.";+ announce([NSString stringWithFormat:@"Got it, %ld of %ld.", (long)landed, (long)kSamples]);+ } else {+ _line.text = self.nod ? @"That didn't read as a double nod. Once more after the tone."+ : @"That didn't read as a shake. Once more after the tone.";+ announce(_line.text);+ }+ if (landed < kSamples) [self after:self.gap then:^{ [self record]; }];+ else [self learn];+}++- (void)learn {+ if (SGHeadGesturesLearnFrom(_samples, _axis) <= 0) {+ [_samples removeAllObjects];+ _line.text = @"Those five were too far apart to learn from. Let's take them again.";+ announce(_line.text);+ [self after:self.gap * 2 then:^{+ [self showCount];+ [self record];+ }];+ return;+ }+ if (self.nod) {+ _line.text = @"Your nod is learned. Next, five shakes.";+ announce(_line.text);+ [self after:self.gap * 1.5 then:^{+ self->_axis = SGHeadAxisYaw;+ [self->_samples removeAllObjects];+ [self showGesture];+ self->_line.text = @"After each tone, shake your head the way you would say no.";+ [self after:self.gap then:^{ [self record]; }];+ }];+ return;+ }+ _finished = YES;+ _running = NO;+ self.navigationItem.leftBarButtonItem = nil;+ _heading.text = @"All set";+ [_dial showDone];+ _dial.accessibilityLabel = @"Nod and shake";+ _line.text = @"Both are learned from yours. If one still slips past, raise Sensitivity.";+ announce([@"All set. " stringByAppendingString:_line.text]);+ [self showCount];+ [self setPrimaryTitle:@"Done"];+}++- (void)redo {+ if (!_running || !_samples.count) return;+ _listening = NO;+ SGHeadGesturesStopListening();+ [_samples removeLastObject];+ _line.text = @"Taking that one again.";+ announce([NSString stringWithFormat:@"Removed. %ld of %ld.", (long)_samples.count, (long)kSamples]);+ [self showCount];+ [self after:self.gap then:^{ [self record]; }];+}++- (void)cancel {+ [self dismissViewControllerAnimated:YES completion:nil];+}++@end++void SGPresentHeadGesturesTeaching(UIViewController *owner, void (^closed)(void)) {+ SGTeachController *teach = [[SGTeachController alloc] initWithClosed:closed];+ UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:teach];+ // Outside Spotify's own stacks the sheet would take the system's appearance, light in light mode.+ nav.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;+ nav.modalPresentationStyle = UIModalPresentationPageSheet;+ nav.sheetPresentationController.prefersGrabberVisible = YES;+ [owner presentViewController:nav animated:YES completion:nil];+}