forked from Blendman974/faceclaw
Add an Apple Watch app that can control the glasses
This commit is contained in:
1 parent
c896ce9ac8
commit
2696f3b76c
46 files changed
+3877
-88
No files matched your search
@@ -57,6 +57,26 @@
|
||||
<string>Faceclaw connects to the terminal servers and developer app URLs you configure on your local network.</string>
|
||||
<key>UILaunchScreen</key>
|
||||
<dict/>
|
||||
<!-- Required: an app built with the iOS 27 SDK crashes at launch without the scene
|
||||
life cycle. @nativescript/core switches to it when it sees this key. Its side effect:
|
||||
the root page only loads once a scene connects, not on a background launch. -->
|
||||
<key>UIApplicationSceneManifest</key>
|
||||
<dict>
|
||||
<key>UIApplicationSupportsMultipleScenes</key>
|
||||
<false/>
|
||||
<key>UISceneConfigurations</key>
|
||||
<dict>
|
||||
<key>UIWindowSceneSessionRoleApplication</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>UISceneConfigurationName</key>
|
||||
<string>Default Configuration</string>
|
||||
<key>UISceneDelegateClassName</key>
|
||||
<string>SceneDelegate</string>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
|
||||
@@ -5,7 +5,7 @@ CODE_SIGN_STYLE = Automatic;
|
||||
// Set IOS_TEAM_ID in build_paths.sh (or pass --teamId) to choose the signing team.
|
||||
// FoundationModels (the on-phone LLM, FaceclawSystemLanguageModel.swift) only exists on iOS 26+,
|
||||
// so it is weak-linked: the app still launches on older iOS and reports the model unavailable.
|
||||
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer -framework AuthenticationServices -framework Security -weak_framework FoundationModels
|
||||
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework SafariServices -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit -framework MediaPlayer -framework AuthenticationServices -framework Security -framework WatchConnectivity -weak_framework FoundationModels
|
||||
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* Phone side of the Apple Watch remote: the single place that talks to
|
||||
* WatchConnectivity, so JS only ever sees path + JSON strings. The twin of the
|
||||
* Android FaceclawWearBridge; the messages are in wear/PROTOCOL.md.
|
||||
*
|
||||
* The session activates during launch, ahead of JS, because the watch can wake
|
||||
* this app in the background and messages arriving before a delegate exists
|
||||
* are lost. Until JS registers its handlers, messages are answered with
|
||||
* jsReady:NO so the watch can say "open Faceclaw on the phone".
|
||||
*
|
||||
* The public methods and both handlers belong to the main thread.
|
||||
*/
|
||||
@interface FaceclawWatchBridge : NSObject
|
||||
+ (instancetype)shared;
|
||||
/** WCSession works on this device (every iPhone; no iPad). */
|
||||
- (BOOL)isAvailable;
|
||||
/** A paired Apple Watch has the Faceclaw app. It needn't be running. */
|
||||
- (BOOL)isWatchAppAvailable;
|
||||
/**
|
||||
* Register JS. messageHandler gets every watch message; connectionHandler fires
|
||||
* when isWatchAppAvailable or the watch app's live reachability changes.
|
||||
*/
|
||||
- (void)setMessageHandler:(void (^)(NSString *path, NSString *json))messageHandler
|
||||
connectionHandler:(void (^)(void))connectionHandler;
|
||||
/**
|
||||
* Mirror the dashboard state: kept in the application context for the app's
|
||||
* next launch, and sent live too while it's in front. An unchanged state is
|
||||
* dropped unless `force`.
|
||||
*/
|
||||
- (void)publishState:(NSString *)json force:(BOOL)force;
|
||||
/** Send an event live. A sticky one also stays in the application context for a suspended app. */
|
||||
- (void)sendEvent:(NSString *)json sticky:(BOOL)sticky;
|
||||
/** A live message on any phone -> watch path; dropped if the watch app isn't in front. */
|
||||
- (void)sendToWatch:(NSString *)path json:(NSString *)json;
|
||||
/** Reply to a watch message; `seq` echoes the watch's sequence number. */
|
||||
- (void)sendAck:(double)seq ok:(BOOL)ok jsReady:(BOOL)jsReady message:(NSString *)message;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,272 @@
|
||||
#import "FaceclawWatchBridge.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <WatchConnectivity/WatchConnectivity.h>
|
||||
|
||||
// Live messages are {path, json, ...}; the watch app's Protocol.swift mirrors these keys.
|
||||
static NSString *const kKeyPath = @"path";
|
||||
static NSString *const kKeyJson = @"json";
|
||||
static NSString *const kKeyUpdatedAt = @"updatedAt";
|
||||
static NSString *const kKeyEvent = @"event";
|
||||
static NSString *const kKeyEventAt = @"eventAt";
|
||||
static NSString *const kKeyAt = @"at";
|
||||
static NSString *const kKeyBattery = @"battery";
|
||||
|
||||
static NSString *const kPathState = @"/faceclaw/state";
|
||||
static NSString *const kPathStateRequest = @"/faceclaw/state/request";
|
||||
static NSString *const kPathBattery = @"/faceclaw/battery";
|
||||
static NSString *const kPathPress = @"/faceclaw/press";
|
||||
static NSString *const kPathAck = @"/faceclaw/ack";
|
||||
static NSString *const kPathEvent = @"/faceclaw/event";
|
||||
|
||||
/** State requests kept for a JS handler that isn't registered yet. */
|
||||
static const NSUInteger kPendingLimit = 16;
|
||||
static const NSTimeInterval kPendingMaxAge = 30;
|
||||
|
||||
static long long NowMs(void) {
|
||||
return (long long)(NSDate.date.timeIntervalSince1970 * 1000);
|
||||
}
|
||||
|
||||
static double ReadSeq(NSString *json) {
|
||||
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
|
||||
id parsed = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
||||
id seq = [parsed isKindOfClass:NSDictionary.class] ? parsed[@"seq"] : nil;
|
||||
return [seq isKindOfClass:NSNumber.class] ? [seq doubleValue] : 0;
|
||||
}
|
||||
|
||||
@interface FaceclawWatchPendingMessage : NSObject
|
||||
@property(nonatomic, copy) NSString *path;
|
||||
@property(nonatomic, copy) NSString *json;
|
||||
@property(nonatomic, strong) NSDate *receivedAt;
|
||||
@end
|
||||
@implementation FaceclawWatchPendingMessage
|
||||
@end
|
||||
|
||||
@interface FaceclawWatchBridge () <WCSessionDelegate>
|
||||
@property(nonatomic, copy, nullable) void (^messageHandler)(NSString *, NSString *);
|
||||
@property(nonatomic, copy, nullable) void (^connectionHandler)(void);
|
||||
@property(nonatomic, strong) NSMutableArray<FaceclawWatchPendingMessage *> *pending;
|
||||
@property(nonatomic, copy, nullable) NSString *lastState;
|
||||
@property(nonatomic) long long lastStateAt;
|
||||
@property(nonatomic, copy, nullable) NSString *heldEvent;
|
||||
@property(nonatomic) long long heldEventAt;
|
||||
/** Sends and context updates run here: they cost IPC, and the main thread also runs BLE. */
|
||||
@property(nonatomic, strong) dispatch_queue_t sendQueue;
|
||||
/** Latest context waiting for sendQueue; nil when none is queued. Guarded by @synchronized(self). */
|
||||
@property(nonatomic, copy, nullable) NSDictionary *queuedContext;
|
||||
@end
|
||||
|
||||
@implementation FaceclawWatchBridge
|
||||
|
||||
+ (void)load {
|
||||
// Registering here is what puts the delegate in place during launch, ahead of JS.
|
||||
[NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidFinishLaunchingNotification
|
||||
object:nil
|
||||
queue:NSOperationQueue.mainQueue
|
||||
usingBlock:^(NSNotification *note) {
|
||||
[[FaceclawWatchBridge shared] activate];
|
||||
}];
|
||||
}
|
||||
|
||||
+ (instancetype)shared {
|
||||
static FaceclawWatchBridge *instance;
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{ instance = [FaceclawWatchBridge new]; });
|
||||
return instance;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if ((self = [super init])) {
|
||||
_pending = [NSMutableArray new];
|
||||
_sendQueue = dispatch_queue_create("com.faceclaw.watch.send", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)activate {
|
||||
if (!WCSession.isSupported) return;
|
||||
WCSession *session = WCSession.defaultSession;
|
||||
if (session.delegate == self) return;
|
||||
session.delegate = self;
|
||||
[session activateSession];
|
||||
}
|
||||
|
||||
#pragma mark - Public API (main thread)
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return WCSession.isSupported;
|
||||
}
|
||||
|
||||
- (BOOL)isWatchAppAvailable {
|
||||
if (!WCSession.isSupported) return NO;
|
||||
WCSession *session = WCSession.defaultSession;
|
||||
if (session.activationState != WCSessionActivationStateActivated) return NO;
|
||||
return session.isReachable || (session.isPaired && session.isWatchAppInstalled);
|
||||
}
|
||||
|
||||
- (void)setMessageHandler:(void (^)(NSString *, NSString *))messageHandler
|
||||
connectionHandler:(void (^)(void))connectionHandler {
|
||||
self.messageHandler = messageHandler;
|
||||
self.connectionHandler = connectionHandler;
|
||||
[self activate];
|
||||
NSArray<FaceclawWatchPendingMessage *> *replay = [self.pending copy];
|
||||
[self.pending removeAllObjects];
|
||||
for (FaceclawWatchPendingMessage *message in replay) {
|
||||
if (-message.receivedAt.timeIntervalSinceNow <= kPendingMaxAge) messageHandler(message.path, message.json);
|
||||
}
|
||||
// The watch's last battery report may have arrived while no one was listening.
|
||||
if (WCSession.isSupported && WCSession.defaultSession.activationState == WCSessionActivationStateActivated) {
|
||||
[self deliverBatteryFrom:WCSession.defaultSession.receivedApplicationContext];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)publishState:(NSString *)json force:(BOOL)force {
|
||||
if (!force && [json isEqualToString:self.lastState]) return;
|
||||
self.lastState = json;
|
||||
self.lastStateAt = NowMs();
|
||||
[self pushContext];
|
||||
[self sendLive:@{kKeyPath: kPathState, kKeyJson: json, kKeyUpdatedAt: @(self.lastStateAt)}];
|
||||
}
|
||||
|
||||
- (void)sendEvent:(NSString *)json sticky:(BOOL)sticky {
|
||||
long long at = NowMs();
|
||||
if (sticky) {
|
||||
self.heldEvent = json;
|
||||
self.heldEventAt = at;
|
||||
[self pushContext];
|
||||
}
|
||||
[self sendLive:@{kKeyPath: kPathEvent, kKeyJson: json, kKeyAt: @(at)}];
|
||||
}
|
||||
|
||||
- (void)sendToWatch:(NSString *)path json:(NSString *)json {
|
||||
[self sendLive:@{kKeyPath: path, kKeyJson: json}];
|
||||
}
|
||||
|
||||
- (void)sendAck:(double)seq ok:(BOOL)ok jsReady:(BOOL)jsReady message:(NSString *)message {
|
||||
NSDictionary *ack = @{@"seq": @(seq), @"ok": @(ok), @"jsReady": @(jsReady), @"message": message ?: @""};
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:ack options:0 error:nil];
|
||||
if (!data) return;
|
||||
[self sendToWatch:kPathAck json:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
|
||||
}
|
||||
|
||||
#pragma mark - Sending
|
||||
|
||||
- (void)sendLive:(NSDictionary *)message {
|
||||
if (!WCSession.isSupported) return;
|
||||
dispatch_async(self.sendQueue, ^{
|
||||
WCSession *session = WCSession.defaultSession;
|
||||
if (session.activationState != WCSessionActivationStateActivated || !session.isReachable) return;
|
||||
[session sendMessage:message replyHandler:nil errorHandler:^(NSError *error) {
|
||||
NSLog(@"[FaceclawWatch] send %@ failed: %@", message[kKeyPath], error);
|
||||
}];
|
||||
});
|
||||
}
|
||||
|
||||
/** Queue the current state and held event as the application context; only the latest matters. */
|
||||
- (void)pushContext {
|
||||
if (!WCSession.isSupported) return;
|
||||
NSMutableDictionary *context = [NSMutableDictionary new];
|
||||
if (self.lastState) {
|
||||
context[kKeyJson] = self.lastState;
|
||||
context[kKeyUpdatedAt] = @(self.lastStateAt);
|
||||
}
|
||||
if (self.heldEvent) {
|
||||
context[kKeyEvent] = self.heldEvent;
|
||||
context[kKeyEventAt] = @(self.heldEventAt);
|
||||
}
|
||||
BOOL alreadyQueued;
|
||||
@synchronized (self) {
|
||||
alreadyQueued = self.queuedContext != nil;
|
||||
self.queuedContext = context;
|
||||
}
|
||||
if (alreadyQueued) return;
|
||||
dispatch_async(self.sendQueue, ^{
|
||||
NSDictionary *next;
|
||||
@synchronized (self) {
|
||||
next = self.queuedContext;
|
||||
self.queuedContext = nil;
|
||||
}
|
||||
WCSession *session = WCSession.defaultSession;
|
||||
if (!next || session.activationState != WCSessionActivationStateActivated) return;
|
||||
if (!session.isPaired || !session.isWatchAppInstalled) return;
|
||||
NSError *error = nil;
|
||||
if (![session updateApplicationContext:next error:&error]) {
|
||||
NSLog(@"[FaceclawWatch] application context update failed: %@", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - Receiving
|
||||
|
||||
- (void)handleMessage:(NSString *)path json:(NSString *)json {
|
||||
if (self.messageHandler) {
|
||||
self.messageHandler(path, json);
|
||||
return;
|
||||
}
|
||||
// A battery report is status and a touch-down only matters live; neither expects an ack.
|
||||
if ([path isEqualToString:kPathBattery] || [path isEqualToString:kPathPress]) return;
|
||||
// Gestures and commands are refused rather than replayed: the watch reports them as failed,
|
||||
// and running them later would act on whatever the glasses happen to show by then.
|
||||
if ([path isEqualToString:kPathStateRequest]) {
|
||||
NSDate *now = NSDate.date;
|
||||
while (self.pending.count > 0 && [now timeIntervalSinceDate:self.pending.firstObject.receivedAt] > kPendingMaxAge) {
|
||||
[self.pending removeObjectAtIndex:0];
|
||||
}
|
||||
if (self.pending.count >= kPendingLimit) [self.pending removeObjectAtIndex:0];
|
||||
FaceclawWatchPendingMessage *message = [FaceclawWatchPendingMessage new];
|
||||
message.path = path;
|
||||
message.json = json;
|
||||
message.receivedAt = now;
|
||||
[self.pending addObject:message];
|
||||
}
|
||||
[self sendAck:ReadSeq(json) ok:NO jsReady:NO message:@"Faceclaw is not running on the phone. Open it to connect the glasses."];
|
||||
}
|
||||
|
||||
/** The watch leaves its battery in the application context while it can't message us live. */
|
||||
- (void)deliverBatteryFrom:(NSDictionary<NSString *, id> *)context {
|
||||
id json = context[kKeyBattery];
|
||||
if ([json isKindOfClass:NSString.class] && self.messageHandler) self.messageHandler(kPathBattery, json);
|
||||
}
|
||||
|
||||
- (void)notifyConnection {
|
||||
if (self.connectionHandler) self.connectionHandler();
|
||||
}
|
||||
|
||||
#pragma mark - WCSessionDelegate (any thread)
|
||||
|
||||
- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)state error:(NSError *)error {
|
||||
if (error) NSLog(@"[FaceclawWatch] activation failed: %@", error);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
// Anything published before activation couldn't be delivered.
|
||||
if (self.lastState || self.heldEvent) [self pushContext];
|
||||
[self deliverBatteryFrom:session.receivedApplicationContext];
|
||||
[self notifyConnection];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)sessionDidBecomeInactive:(WCSession *)session {}
|
||||
|
||||
- (void)sessionDidDeactivate:(WCSession *)session {
|
||||
// The user switched watches: the session must be reactivated for the new one.
|
||||
[session activateSession];
|
||||
}
|
||||
|
||||
- (void)sessionWatchStateDidChange:(WCSession *)session {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [self notifyConnection]; });
|
||||
}
|
||||
|
||||
- (void)sessionReachabilityDidChange:(WCSession *)session {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [self notifyConnection]; });
|
||||
}
|
||||
|
||||
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message {
|
||||
id path = message[kKeyPath], json = message[kKeyJson];
|
||||
if (![path isKindOfClass:NSString.class]) return;
|
||||
NSString *body = [json isKindOfClass:NSString.class] ? json : @"{}";
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [self handleMessage:path json:body]; });
|
||||
}
|
||||
|
||||
- (void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *, id> *)context {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [self deliverBatteryFrom:context]; });
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -14,5 +14,6 @@ module FaceclawGraphics {
|
||||
header "FaceclawLoopbackRedirect.h"
|
||||
header "FaceclawWebSignIn.h"
|
||||
header "FaceclawResourceUsage.h"
|
||||
header "FaceclawWatchBridge.h"
|
||||
export *
|
||||
}
|
||||
Reference in new issue
Block a user