@factiii/runner 0.14.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +455 -5
- package/native/simhid.m +291 -0
- package/native/simstream.m +319 -0
- package/package.json +2 -1
package/dist/cli.js
CHANGED
|
@@ -8642,12 +8642,12 @@ function cleanRegex(source) {
|
|
|
8642
8642
|
return source.slice(start, end);
|
|
8643
8643
|
}
|
|
8644
8644
|
function floatSafeRemainder(val, step) {
|
|
8645
|
-
const
|
|
8646
|
-
const roundedRatio = Math.round(
|
|
8647
|
-
const tolerance = 4 * Number.EPSILON * Math.max(Math.abs(
|
|
8648
|
-
if (Math.abs(
|
|
8645
|
+
const ratio2 = val / step;
|
|
8646
|
+
const roundedRatio = Math.round(ratio2);
|
|
8647
|
+
const tolerance = 4 * Number.EPSILON * Math.max(Math.abs(ratio2), 1);
|
|
8648
|
+
if (Math.abs(ratio2 - roundedRatio) < tolerance)
|
|
8649
8649
|
return 0;
|
|
8650
|
-
return
|
|
8650
|
+
return ratio2 - roundedRatio;
|
|
8651
8651
|
}
|
|
8652
8652
|
var EVALUATING = /* @__PURE__ */ Symbol("evaluating");
|
|
8653
8653
|
function defineLazy(object2, key, getter) {
|
|
@@ -34863,6 +34863,209 @@ var ScratchpadStore = class {
|
|
|
34863
34863
|
}
|
|
34864
34864
|
};
|
|
34865
34865
|
|
|
34866
|
+
// ../../shared/all/helpers/board-agent-core/simulator-store.ts
|
|
34867
|
+
var FRAMEWORKS = {
|
|
34868
|
+
simstream: "-framework Foundation -framework IOSurface -framework VideoToolbox -framework CoreMedia -framework CoreVideo",
|
|
34869
|
+
simhid: "-framework Foundation -framework AppKit"
|
|
34870
|
+
};
|
|
34871
|
+
function shEscape3(s) {
|
|
34872
|
+
return `'${s.replace(/'/g, `'\\''`)}'`;
|
|
34873
|
+
}
|
|
34874
|
+
var udid = external_exports.string().trim().min(1).max(128);
|
|
34875
|
+
var ratio = external_exports.number().finite().catch(0).transform((n) => Math.min(1, Math.max(0, n)));
|
|
34876
|
+
var simulatorInputSchema = external_exports.intersection(
|
|
34877
|
+
external_exports.object({ udid }),
|
|
34878
|
+
external_exports.discriminatedUnion("type", [
|
|
34879
|
+
external_exports.object({
|
|
34880
|
+
type: external_exports.literal("touch"),
|
|
34881
|
+
phase: external_exports.enum(["down", "move", "up"]),
|
|
34882
|
+
x: ratio,
|
|
34883
|
+
y: ratio
|
|
34884
|
+
}),
|
|
34885
|
+
external_exports.object({ type: external_exports.literal("tap"), x: ratio, y: ratio }),
|
|
34886
|
+
// Capped because the helper types it a keystroke at a time, sleeping
|
|
34887
|
+
// between them: an unbounded string is minutes of a device typing.
|
|
34888
|
+
external_exports.object({ type: external_exports.literal("text"), text: external_exports.string().max(2e3) }),
|
|
34889
|
+
external_exports.object({
|
|
34890
|
+
type: external_exports.literal("button"),
|
|
34891
|
+
button: external_exports.enum(["HOME", "LOCK", "SIRI", "SIDE", "APPLE_PAY"])
|
|
34892
|
+
}),
|
|
34893
|
+
external_exports.object({
|
|
34894
|
+
type: external_exports.literal("biometric"),
|
|
34895
|
+
kind: external_exports.enum(["face", "touch"]),
|
|
34896
|
+
match: external_exports.boolean()
|
|
34897
|
+
})
|
|
34898
|
+
])
|
|
34899
|
+
);
|
|
34900
|
+
var simulatorAttachSchema = external_exports.object({
|
|
34901
|
+
udid,
|
|
34902
|
+
width: external_exports.number().int().catch(0).transform((n) => n === 0 ? 0 : Math.min(4096, Math.max(160, n))),
|
|
34903
|
+
fps: external_exports.number().int().catch(30).transform((n) => Math.min(60, Math.max(1, n))),
|
|
34904
|
+
bitrate: external_exports.number().int().catch(25e5).transform((n) => Math.min(2e7, Math.max(1e5, n)))
|
|
34905
|
+
});
|
|
34906
|
+
var SimulatorStore = class {
|
|
34907
|
+
/**
|
|
34908
|
+
* @param target Resolved per call, never held — the space's exec target is
|
|
34909
|
+
* rebuilt as the space provisions.
|
|
34910
|
+
* @param emit One H.264 access unit, Annex-B.
|
|
34911
|
+
*/
|
|
34912
|
+
constructor(target, emit, nativeDir) {
|
|
34913
|
+
this.target = target;
|
|
34914
|
+
this.emit = emit;
|
|
34915
|
+
this.nativeDir = nativeDir;
|
|
34916
|
+
this.video = null;
|
|
34917
|
+
/**
|
|
34918
|
+
* One input process per device: the helper is bound to the udid it was
|
|
34919
|
+
* spawned with. Holds the *promise*, not the process — spawning is async and
|
|
34920
|
+
* a drag fires several events at once, so storing the result let each of them
|
|
34921
|
+
* see an empty slot and start its own helper.
|
|
34922
|
+
*/
|
|
34923
|
+
this.hid = /* @__PURE__ */ new Map();
|
|
34924
|
+
this.built = /* @__PURE__ */ new Set();
|
|
34925
|
+
}
|
|
34926
|
+
/** Running simulators only — a shutdown one has no display to attach to.
|
|
34927
|
+
*
|
|
34928
|
+
* `run`, not `sh`: the watcher asks this every few seconds while a session
|
|
34929
|
+
* window is open, and `sh` sources a whole login profile per call. */
|
|
34930
|
+
async list() {
|
|
34931
|
+
const out = await this.target().run(["xcrun", "simctl", "list", "devices", "available", "-j"]).catch(() => "");
|
|
34932
|
+
if (!out.trim()) return [];
|
|
34933
|
+
let parsed;
|
|
34934
|
+
try {
|
|
34935
|
+
parsed = JSON.parse(out);
|
|
34936
|
+
} catch {
|
|
34937
|
+
return [];
|
|
34938
|
+
}
|
|
34939
|
+
const devices = [];
|
|
34940
|
+
for (const [runtime, entries] of Object.entries(parsed.devices ?? {})) {
|
|
34941
|
+
for (const entry of entries) {
|
|
34942
|
+
if (entry.state !== "Booted") continue;
|
|
34943
|
+
devices.push({
|
|
34944
|
+
udid: entry.udid,
|
|
34945
|
+
name: entry.name,
|
|
34946
|
+
// "com.apple.CoreSimulator.SimRuntime.iOS-26-4" reads badly in a 190px
|
|
34947
|
+
// panel; the tail is the part anyone recognises.
|
|
34948
|
+
runtime: runtime.split(".").pop()?.replace(/-/g, " ") ?? runtime
|
|
34949
|
+
});
|
|
34950
|
+
}
|
|
34951
|
+
}
|
|
34952
|
+
return devices.sort((a, b) => a.name.localeCompare(b.name));
|
|
34953
|
+
}
|
|
34954
|
+
/** A set rather than "the first one": with two up, picking by sort order
|
|
34955
|
+
* makes the auto-open land on an arbitrary device and never fire again. */
|
|
34956
|
+
async status() {
|
|
34957
|
+
return { booted: (await this.list()).map((d) => d.udid) };
|
|
34958
|
+
}
|
|
34959
|
+
/** Start pushing frames. Idempotent for the same options; different ones
|
|
34960
|
+
* restart the encoder, whose output size is fixed at session start. */
|
|
34961
|
+
async attach(raw) {
|
|
34962
|
+
const options = simulatorAttachSchema.parse(raw);
|
|
34963
|
+
if (this.video) {
|
|
34964
|
+
const current = this.video.options;
|
|
34965
|
+
if (current.udid === options.udid && current.width === options.width && current.fps === options.fps && current.bitrate === options.bitrate) {
|
|
34966
|
+
return;
|
|
34967
|
+
}
|
|
34968
|
+
this.detach();
|
|
34969
|
+
}
|
|
34970
|
+
const binary = await this.build("simstream");
|
|
34971
|
+
const proc = await this.target().spawn([
|
|
34972
|
+
binary,
|
|
34973
|
+
options.udid,
|
|
34974
|
+
"--fps",
|
|
34975
|
+
String(options.fps),
|
|
34976
|
+
"--width",
|
|
34977
|
+
String(options.width),
|
|
34978
|
+
"--bitrate",
|
|
34979
|
+
String(options.bitrate)
|
|
34980
|
+
]);
|
|
34981
|
+
this.video = { proc, options };
|
|
34982
|
+
let buffered = new Uint8Array(0);
|
|
34983
|
+
proc.stdout?.on("data", (chunk) => {
|
|
34984
|
+
if (this.video?.proc !== proc) return;
|
|
34985
|
+
const merged = new Uint8Array(buffered.length + chunk.length);
|
|
34986
|
+
merged.set(buffered);
|
|
34987
|
+
merged.set(chunk, buffered.length);
|
|
34988
|
+
buffered = merged;
|
|
34989
|
+
for (; ; ) {
|
|
34990
|
+
if (buffered.length < 4) return;
|
|
34991
|
+
const view = new DataView(
|
|
34992
|
+
buffered.buffer,
|
|
34993
|
+
buffered.byteOffset,
|
|
34994
|
+
buffered.length
|
|
34995
|
+
);
|
|
34996
|
+
const length = view.getUint32(0);
|
|
34997
|
+
if (buffered.length < 4 + length) return;
|
|
34998
|
+
const unit = buffered.subarray(4, 4 + length);
|
|
34999
|
+
buffered = buffered.subarray(4 + length);
|
|
35000
|
+
try {
|
|
35001
|
+
this.emit(unit, Date.now());
|
|
35002
|
+
} catch {
|
|
35003
|
+
}
|
|
35004
|
+
}
|
|
35005
|
+
});
|
|
35006
|
+
proc.on("exit", () => {
|
|
35007
|
+
if (this.video?.proc === proc) this.video = null;
|
|
35008
|
+
});
|
|
35009
|
+
}
|
|
35010
|
+
/** Stop encoding: nobody watching, nothing to encode. */
|
|
35011
|
+
detach() {
|
|
35012
|
+
const proc = this.video?.proc;
|
|
35013
|
+
this.video = null;
|
|
35014
|
+
proc?.kill("SIGKILL");
|
|
35015
|
+
}
|
|
35016
|
+
/** One input event. Touch carries 0..1 ratios: the HID layer normalises
|
|
35017
|
+
* against the screen, so nothing here needs the device's size. */
|
|
35018
|
+
async input(raw) {
|
|
35019
|
+
const command = simulatorInputSchema.parse(raw);
|
|
35020
|
+
let pending = this.hid.get(command.udid);
|
|
35021
|
+
if (!pending) {
|
|
35022
|
+
pending = this.build("simhid").then(async (binary) => {
|
|
35023
|
+
const proc = await this.target().spawn([binary, command.udid]);
|
|
35024
|
+
proc.on("exit", () => {
|
|
35025
|
+
if (this.hid.get(command.udid) === pending) {
|
|
35026
|
+
this.hid.delete(command.udid);
|
|
35027
|
+
}
|
|
35028
|
+
});
|
|
35029
|
+
return proc;
|
|
35030
|
+
});
|
|
35031
|
+
this.hid.set(command.udid, pending);
|
|
35032
|
+
pending.catch(() => this.hid.delete(command.udid));
|
|
35033
|
+
}
|
|
35034
|
+
const hid = await pending;
|
|
35035
|
+
if (hid.exitCode !== null) {
|
|
35036
|
+
this.hid.delete(command.udid);
|
|
35037
|
+
return;
|
|
35038
|
+
}
|
|
35039
|
+
hid.stdin?.write(`${JSON.stringify(command)}
|
|
35040
|
+
`);
|
|
35041
|
+
}
|
|
35042
|
+
destroy() {
|
|
35043
|
+
this.detach();
|
|
35044
|
+
for (const pending of this.hid.values()) {
|
|
35045
|
+
void pending.then((hid) => hid.kill()).catch(() => {
|
|
35046
|
+
});
|
|
35047
|
+
}
|
|
35048
|
+
this.hid.clear();
|
|
35049
|
+
}
|
|
35050
|
+
/** Compile if missing or stale. `clang` ships with the Xcode a simulator
|
|
35051
|
+
* already requires, so this adds no dependency. */
|
|
35052
|
+
async build(helper) {
|
|
35053
|
+
if (!this.nativeDir) {
|
|
35054
|
+
throw new Error("This runner has no simulator helpers installed.");
|
|
35055
|
+
}
|
|
35056
|
+
const source = `${this.nativeDir}/${helper}.m`;
|
|
35057
|
+
const binary = `${this.target().paths.bin}/${helper}`;
|
|
35058
|
+
if (this.built.has(helper)) return binary;
|
|
35059
|
+
await this.target().sh(
|
|
35060
|
+
`if [ ! -x ${shEscape3(binary)} ] || [ ${shEscape3(source)} -nt ${shEscape3(binary)} ]; then
|
|
35061
|
+
clang -fobjc-arc -Wno-objc-method-access -Wno-deprecated-declarations ${FRAMEWORKS[helper]} -o ${shEscape3(binary)} ${shEscape3(source)}
|
|
35062
|
+
fi`
|
|
35063
|
+
);
|
|
35064
|
+
this.built.add(helper);
|
|
35065
|
+
return binary;
|
|
35066
|
+
}
|
|
35067
|
+
};
|
|
35068
|
+
|
|
34866
35069
|
// ../../shared/all/helpers/board-agent-core/space-core.ts
|
|
34867
35070
|
var import_promises10 = require("fs/promises");
|
|
34868
35071
|
var import_path14 = __toESM(require("path"));
|
|
@@ -35778,6 +35981,12 @@ var BoardAgentEngine = class _BoardAgentEngine {
|
|
|
35778
35981
|
() => this.core.target(),
|
|
35779
35982
|
(payload) => this.emitter.emitScratchpad(payload)
|
|
35780
35983
|
);
|
|
35984
|
+
this.simulator = new SimulatorStore(
|
|
35985
|
+
() => this.core.target(),
|
|
35986
|
+
(unit, at) => options.onSimulatorFrame?.(unit, at),
|
|
35987
|
+
options.nativeDir ?? ""
|
|
35988
|
+
);
|
|
35989
|
+
this.onSimulatorAttach = options.onSimulatorAttach;
|
|
35781
35990
|
this.gate.subscribe(() => this.projectGateOntoRun());
|
|
35782
35991
|
}
|
|
35783
35992
|
/** Everything the runner does for this space that isn't a card session, so
|
|
@@ -36775,6 +36984,22 @@ var BoardAgentEngine = class _BoardAgentEngine {
|
|
|
36775
36984
|
scratchpadClear() {
|
|
36776
36985
|
return this.scratchpad.clear();
|
|
36777
36986
|
}
|
|
36987
|
+
simulatorList() {
|
|
36988
|
+
return this.simulator.list();
|
|
36989
|
+
}
|
|
36990
|
+
simulatorStatus() {
|
|
36991
|
+
return this.simulator.status();
|
|
36992
|
+
}
|
|
36993
|
+
simulatorAttach(p) {
|
|
36994
|
+
this.onSimulatorAttach?.();
|
|
36995
|
+
return this.simulator.attach(p);
|
|
36996
|
+
}
|
|
36997
|
+
simulatorDetach() {
|
|
36998
|
+
this.simulator.detach();
|
|
36999
|
+
}
|
|
37000
|
+
simulatorInput(p) {
|
|
37001
|
+
return this.simulator.input(p);
|
|
37002
|
+
}
|
|
36778
37003
|
// ── Cross-mode coordinators ──
|
|
36779
37004
|
async cardExecute(payload) {
|
|
36780
37005
|
const {
|
|
@@ -37105,6 +37330,7 @@ ${c.content.trim() || "(no description)"}`
|
|
|
37105
37330
|
this.claude.destroy();
|
|
37106
37331
|
this.terminal.destroy();
|
|
37107
37332
|
this.scratchpad.destroy();
|
|
37333
|
+
this.simulator.destroy();
|
|
37108
37334
|
this.core.destroy();
|
|
37109
37335
|
}
|
|
37110
37336
|
/** Route one action/payload to the right engine method, for both transports.
|
|
@@ -37457,6 +37683,23 @@ ${c.content.trim() || "(no description)"}`
|
|
|
37457
37683
|
case "scratchpadClear":
|
|
37458
37684
|
await this.scratchpadClear();
|
|
37459
37685
|
return void 0;
|
|
37686
|
+
case "simulatorList":
|
|
37687
|
+
return await this.simulatorList();
|
|
37688
|
+
case "simulatorStatus":
|
|
37689
|
+
return await this.simulatorStatus();
|
|
37690
|
+
case "simulatorAttach":
|
|
37691
|
+
await this.simulatorAttach(
|
|
37692
|
+
payload
|
|
37693
|
+
);
|
|
37694
|
+
return void 0;
|
|
37695
|
+
case "simulatorDetach":
|
|
37696
|
+
this.simulatorDetach();
|
|
37697
|
+
return void 0;
|
|
37698
|
+
case "simulatorInput":
|
|
37699
|
+
await this.simulatorInput(
|
|
37700
|
+
payload
|
|
37701
|
+
);
|
|
37702
|
+
return void 0;
|
|
37460
37703
|
case "secureStatus":
|
|
37461
37704
|
return this.secureStatus();
|
|
37462
37705
|
default:
|
|
@@ -38749,6 +38992,141 @@ function agentSettledPhase(previous, current) {
|
|
|
38749
38992
|
var import_crypto11 = require("crypto");
|
|
38750
38993
|
var import_fs12 = __toESM(require("fs"));
|
|
38751
38994
|
var import_node_datachannel = require("node-datachannel");
|
|
38995
|
+
|
|
38996
|
+
// ../../shared/all/helpers/board-agent-core/rtp-h264.ts
|
|
38997
|
+
var MAX_PAYLOAD = 1200;
|
|
38998
|
+
var CLOCK_HZ = 9e4;
|
|
38999
|
+
var RTP_HEADER_BYTES = 12;
|
|
39000
|
+
var FU_A = 28;
|
|
39001
|
+
var H264Packetizer = class {
|
|
39002
|
+
constructor({ payloadType, ssrc }) {
|
|
39003
|
+
this.sequence = Math.floor(Math.random() * 65535);
|
|
39004
|
+
this.midExtensionId = 0;
|
|
39005
|
+
this.mid = "";
|
|
39006
|
+
this.payloadType = payloadType;
|
|
39007
|
+
this.ssrc = ssrc;
|
|
39008
|
+
}
|
|
39009
|
+
/** Known only once the answer exists. A packet stamped with a type the far
|
|
39010
|
+
* end never mapped is dropped silently. */
|
|
39011
|
+
setPayloadType(payloadType) {
|
|
39012
|
+
this.payloadType = payloadType;
|
|
39013
|
+
}
|
|
39014
|
+
/** Required whenever the answer declares no `a=ssrc:` — libdatachannel's
|
|
39015
|
+
* does not. Under BUNDLE the receiver demuxes on SSRC or this; with neither,
|
|
39016
|
+
* packets reach the transport and are discarded unattributed. */
|
|
39017
|
+
setMidExtension(id, mid) {
|
|
39018
|
+
this.midExtensionId = id;
|
|
39019
|
+
this.mid = mid;
|
|
39020
|
+
}
|
|
39021
|
+
/** Every packet of a frame shares a timestamp; only the last carries the
|
|
39022
|
+
* marker bit, which is how the far end knows the frame is whole. */
|
|
39023
|
+
packetize(accessUnit, timestampMs) {
|
|
39024
|
+
const timestamp = Math.round(timestampMs * (CLOCK_HZ / 1e3)) >>> 0;
|
|
39025
|
+
const units = splitAnnexB(accessUnit);
|
|
39026
|
+
const packets = [];
|
|
39027
|
+
units.forEach((nal, index) => {
|
|
39028
|
+
const last = index === units.length - 1;
|
|
39029
|
+
if (nal.length <= MAX_PAYLOAD) {
|
|
39030
|
+
packets.push(this.packet(nal, timestamp, last));
|
|
39031
|
+
return;
|
|
39032
|
+
}
|
|
39033
|
+
const indicator = nal[0] & 224 | FU_A;
|
|
39034
|
+
const type = nal[0] & 31;
|
|
39035
|
+
const body = nal.subarray(1);
|
|
39036
|
+
const perFragment = MAX_PAYLOAD - 2;
|
|
39037
|
+
for (let offset = 0; offset < body.length; offset += perFragment) {
|
|
39038
|
+
const chunk = body.subarray(offset, offset + perFragment);
|
|
39039
|
+
const first = offset === 0;
|
|
39040
|
+
const final = offset + perFragment >= body.length;
|
|
39041
|
+
const payload = new Uint8Array(chunk.length + 2);
|
|
39042
|
+
payload[0] = indicator;
|
|
39043
|
+
payload[1] = (first ? 128 : 0) | (final ? 64 : 0) | type;
|
|
39044
|
+
payload.set(chunk, 2);
|
|
39045
|
+
packets.push(this.packet(payload, timestamp, last && final));
|
|
39046
|
+
}
|
|
39047
|
+
});
|
|
39048
|
+
return packets;
|
|
39049
|
+
}
|
|
39050
|
+
packet(payload, timestamp, marker) {
|
|
39051
|
+
const extension2 = this.midBlock();
|
|
39052
|
+
const packet = new Uint8Array(
|
|
39053
|
+
RTP_HEADER_BYTES + extension2.length + payload.length
|
|
39054
|
+
);
|
|
39055
|
+
const view = new DataView(packet.buffer);
|
|
39056
|
+
packet[0] = 128 | (extension2.length ? 16 : 0);
|
|
39057
|
+
packet[1] = (marker ? 128 : 0) | this.payloadType;
|
|
39058
|
+
view.setUint16(2, this.sequence);
|
|
39059
|
+
view.setUint32(4, timestamp);
|
|
39060
|
+
view.setUint32(8, this.ssrc);
|
|
39061
|
+
packet.set(extension2, RTP_HEADER_BYTES);
|
|
39062
|
+
packet.set(payload, RTP_HEADER_BYTES + extension2.length);
|
|
39063
|
+
this.sequence = this.sequence + 1 & 65535;
|
|
39064
|
+
return packet;
|
|
39065
|
+
}
|
|
39066
|
+
/** The RFC 8285 one-byte extension carrying the media id, 4-byte padded. */
|
|
39067
|
+
midBlock() {
|
|
39068
|
+
if (!this.midExtensionId || !this.mid) return new Uint8Array(0);
|
|
39069
|
+
const value2 = new TextEncoder().encode(this.mid);
|
|
39070
|
+
const body = 1 + value2.length;
|
|
39071
|
+
const padded = Math.ceil(body / 4) * 4;
|
|
39072
|
+
const block = new Uint8Array(4 + padded);
|
|
39073
|
+
const view = new DataView(block.buffer);
|
|
39074
|
+
view.setUint16(0, 48862);
|
|
39075
|
+
view.setUint16(2, padded / 4);
|
|
39076
|
+
block[4] = this.midExtensionId << 4 | value2.length - 1;
|
|
39077
|
+
block.set(value2, 5);
|
|
39078
|
+
return block;
|
|
39079
|
+
}
|
|
39080
|
+
};
|
|
39081
|
+
function splitAnnexB(data) {
|
|
39082
|
+
const units = [];
|
|
39083
|
+
let start = -1;
|
|
39084
|
+
for (let i = 0; i + 2 < data.length; i++) {
|
|
39085
|
+
if (data[i] !== 0 || data[i + 1] !== 0 || data[i + 2] !== 1) continue;
|
|
39086
|
+
if (start >= 0) {
|
|
39087
|
+
const end = i > start && data[i - 1] === 0 ? i - 1 : i;
|
|
39088
|
+
if (end > start) units.push(data.subarray(start, end));
|
|
39089
|
+
}
|
|
39090
|
+
start = i + 3;
|
|
39091
|
+
i += 2;
|
|
39092
|
+
}
|
|
39093
|
+
if (start >= 0 && start < data.length) units.push(data.subarray(start));
|
|
39094
|
+
return units;
|
|
39095
|
+
}
|
|
39096
|
+
function negotiatedH264PayloadType(sdp, profileLevelId) {
|
|
39097
|
+
const video = sdp.split(/^m=/m).find((section) => section.startsWith("video"));
|
|
39098
|
+
if (!video) return null;
|
|
39099
|
+
const h264 = new Set(
|
|
39100
|
+
[...video.matchAll(/^a=rtpmap:(\d+) H264\//gm)].map((m) => Number(m[1]))
|
|
39101
|
+
);
|
|
39102
|
+
if (!h264.size) return null;
|
|
39103
|
+
const fmtp = /* @__PURE__ */ new Map();
|
|
39104
|
+
for (const [, pt, params] of video.matchAll(/^a=fmtp:(\d+) (.+)$/gm)) {
|
|
39105
|
+
fmtp.set(Number(pt), params);
|
|
39106
|
+
}
|
|
39107
|
+
const offered = video.split("\n")[0].trim().split(/\s+/).slice(3).map(Number);
|
|
39108
|
+
const usable = offered.filter(
|
|
39109
|
+
(pt) => h264.has(pt) && /packetization-mode=1/.test(fmtp.get(pt) ?? "")
|
|
39110
|
+
);
|
|
39111
|
+
if (!usable.length) return null;
|
|
39112
|
+
if (profileLevelId) {
|
|
39113
|
+
const exact = usable.find(
|
|
39114
|
+
(pt) => (fmtp.get(pt) ?? "").includes(`profile-level-id=${profileLevelId}`)
|
|
39115
|
+
);
|
|
39116
|
+
if (exact !== void 0) return exact;
|
|
39117
|
+
}
|
|
39118
|
+
return usable[0];
|
|
39119
|
+
}
|
|
39120
|
+
function negotiatedMid(sdp) {
|
|
39121
|
+
const video = sdp.split(/^m=/m).find((section) => section.startsWith("video"));
|
|
39122
|
+
if (!video) return null;
|
|
39123
|
+
const id = /^a=extmap:(\d+) urn:ietf:params:rtp-hdrext:sdes:mid/m.exec(video);
|
|
39124
|
+
const mid = /^a=mid:(\S+)/m.exec(video);
|
|
39125
|
+
if (!id || !mid) return null;
|
|
39126
|
+
return { id: Number(id[1]), mid: mid[1] };
|
|
39127
|
+
}
|
|
39128
|
+
|
|
39129
|
+
// src/daemon.ts
|
|
38752
39130
|
var import_path24 = __toESM(require("path"));
|
|
38753
39131
|
|
|
38754
39132
|
// ../../node_modules/engine.io-client/build/esm-debug/transports/polling-xhr.node.js
|
|
@@ -42526,6 +42904,9 @@ function toChannelScope(raw) {
|
|
|
42526
42904
|
if (!raw || raw.role === "owner") return { role: "owner" };
|
|
42527
42905
|
return { role: "member", spaceSlugs: new Set(raw.spaceSlugs) };
|
|
42528
42906
|
}
|
|
42907
|
+
var SIMULATOR_PAYLOAD_TYPE = 96;
|
|
42908
|
+
var SIMULATOR_PROFILE = "f4001f";
|
|
42909
|
+
var SIMULATOR_SSRC = 1364675889;
|
|
42529
42910
|
function scopeAllows(scope, spaceSlug) {
|
|
42530
42911
|
return scope.role === "owner" || scope.spaceSlugs.has(spaceSlug);
|
|
42531
42912
|
}
|
|
@@ -42572,6 +42953,32 @@ async function startDaemon(config2) {
|
|
|
42572
42953
|
});
|
|
42573
42954
|
const engines = /* @__PURE__ */ new Map();
|
|
42574
42955
|
const channelScopes = /* @__PURE__ */ new Map();
|
|
42956
|
+
const simulatorSinks = /* @__PURE__ */ new Set();
|
|
42957
|
+
let warnedNoSink = false;
|
|
42958
|
+
const sendSimulatorFrame = (accessUnit, capturedAt) => {
|
|
42959
|
+
if (!simulatorSinks.size) {
|
|
42960
|
+
if (!warnedNoSink) {
|
|
42961
|
+
warnedNoSink = true;
|
|
42962
|
+
console.log("[simulator] frames produced but no track is registered");
|
|
42963
|
+
}
|
|
42964
|
+
return;
|
|
42965
|
+
}
|
|
42966
|
+
for (const sink of simulatorSinks) {
|
|
42967
|
+
if (!sink.track.isOpen()) continue;
|
|
42968
|
+
try {
|
|
42969
|
+
for (const packet of sink.packetizer.packetize(accessUnit, capturedAt)) {
|
|
42970
|
+
sink.track.sendMessageBinary(Buffer.from(packet));
|
|
42971
|
+
}
|
|
42972
|
+
} catch (err) {
|
|
42973
|
+
simulatorSinks.delete(sink);
|
|
42974
|
+
console.log(
|
|
42975
|
+
"[simulator] dropped a closed track:",
|
|
42976
|
+
err instanceof Error ? err.message : err
|
|
42977
|
+
);
|
|
42978
|
+
continue;
|
|
42979
|
+
}
|
|
42980
|
+
}
|
|
42981
|
+
};
|
|
42575
42982
|
const terminalByChannel = /* @__PURE__ */ new WeakMap();
|
|
42576
42983
|
const findCardTargetInScope = (scope, postId) => {
|
|
42577
42984
|
for (const [spaceSlug, engine] of engines) {
|
|
@@ -42748,11 +43155,24 @@ async function startDaemon(config2) {
|
|
|
42748
43155
|
}
|
|
42749
43156
|
};
|
|
42750
43157
|
engine = new BoardAgentEngine({
|
|
43158
|
+
// Shipped as source and compiled per host: the private frameworks the
|
|
43159
|
+
// simulator helpers bind to move between Xcode versions.
|
|
43160
|
+
nativeDir: import_path24.default.join(__dirname, "..", "native"),
|
|
42751
43161
|
spaceSlug,
|
|
42752
43162
|
// Stable board id, carried so a slug rename doesn't lose the board's
|
|
42753
43163
|
// identity. Nothing reads it since containers went away.
|
|
42754
43164
|
spaceId,
|
|
42755
43165
|
emitter: createEventSink(send, spaceSlug),
|
|
43166
|
+
onSimulatorFrame: sendSimulatorFrame,
|
|
43167
|
+
// One encoder for the machine. Every peer has a single video track, so
|
|
43168
|
+
// a second space attaching would put two H.264 streams on it —
|
|
43169
|
+
// different SPS, independent sequence numbers and timestamps — and
|
|
43170
|
+
// neither would decode.
|
|
43171
|
+
onSimulatorAttach: () => {
|
|
43172
|
+
for (const [slug, other] of engines) {
|
|
43173
|
+
if (slug !== spaceSlug) other.simulatorDetach();
|
|
43174
|
+
}
|
|
43175
|
+
},
|
|
42756
43176
|
// Re-report connection facts after any config/OneDrive write so the
|
|
42757
43177
|
// UI badges update when a token is added or cleared.
|
|
42758
43178
|
configProvider: {
|
|
@@ -42901,6 +43321,12 @@ async function startDaemon(config2) {
|
|
|
42901
43321
|
});
|
|
42902
43322
|
});
|
|
42903
43323
|
peer.onLocalDescription((sdp, _type) => {
|
|
43324
|
+
const payloadType = negotiatedH264PayloadType(sdp, SIMULATOR_PROFILE);
|
|
43325
|
+
if (payloadType !== null) {
|
|
43326
|
+
simulatorSink?.packetizer.setPayloadType(payloadType);
|
|
43327
|
+
}
|
|
43328
|
+
const mid = negotiatedMid(sdp);
|
|
43329
|
+
if (mid) simulatorSink?.packetizer.setMidExtension(mid.id, mid.mid);
|
|
42904
43330
|
console.log(`Sending answer to ${data.fromSocketId}`);
|
|
42905
43331
|
socket.emit("runnerAnswer", {
|
|
42906
43332
|
targetSocketId: data.fromSocketId,
|
|
@@ -43027,6 +43453,30 @@ async function startDaemon(config2) {
|
|
|
43027
43453
|
detachIceHandler();
|
|
43028
43454
|
});
|
|
43029
43455
|
});
|
|
43456
|
+
let simulatorSink = null;
|
|
43457
|
+
if (!data.offer.sdp.includes("m=video")) {
|
|
43458
|
+
console.log("[simulator] offer has no video m-line; no track for this peer");
|
|
43459
|
+
}
|
|
43460
|
+
if (data.offer.sdp.includes("m=video")) {
|
|
43461
|
+
try {
|
|
43462
|
+
const media = new import_node_datachannel.Video("sim", "SendOnly");
|
|
43463
|
+
media.addH264Codec(SIMULATOR_PAYLOAD_TYPE);
|
|
43464
|
+
media.addSSRC(SIMULATOR_SSRC, "sim");
|
|
43465
|
+
const track = peer.addTrack(media);
|
|
43466
|
+
simulatorSink = {
|
|
43467
|
+
track,
|
|
43468
|
+
packetizer: new H264Packetizer({
|
|
43469
|
+
payloadType: SIMULATOR_PAYLOAD_TYPE,
|
|
43470
|
+
ssrc: SIMULATOR_SSRC
|
|
43471
|
+
})
|
|
43472
|
+
};
|
|
43473
|
+
const sink = simulatorSink;
|
|
43474
|
+
simulatorSinks.add(sink);
|
|
43475
|
+
track.onClosed(() => simulatorSinks.delete(sink));
|
|
43476
|
+
} catch (err) {
|
|
43477
|
+
console.error("Could not add the simulator video track:", err);
|
|
43478
|
+
}
|
|
43479
|
+
}
|
|
43030
43480
|
try {
|
|
43031
43481
|
peer.setRemoteDescription(data.offer.sdp, import_node_datachannel.DescriptionType.Offer);
|
|
43032
43482
|
} catch (err) {
|
package/native/simhid.m
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// Native simulator input over the legacy Indigo HID path. Reads one JSON
|
|
2
|
+
// command per line on stdin. Unlike idb's input, this exposes a real move
|
|
3
|
+
// phase, so a drag tracks the pointer instead of being a canned swipe.
|
|
4
|
+
#import <Foundation/Foundation.h>
|
|
5
|
+
#import <AppKit/AppKit.h>
|
|
6
|
+
#import <dlfcn.h>
|
|
7
|
+
#import <mach/mach_time.h>
|
|
8
|
+
|
|
9
|
+
@interface SimServiceContext : NSObject
|
|
10
|
+
+ (instancetype)sharedServiceContextForDeveloperDir:(NSString *)dir error:(NSError **)error;
|
|
11
|
+
- (id)defaultDeviceSetWithError:(NSError **)error;
|
|
12
|
+
@end
|
|
13
|
+
|
|
14
|
+
@interface NSObject (SimHID)
|
|
15
|
+
- (instancetype)initWithDevice:(id)device error:(NSError **)error;
|
|
16
|
+
- (BOOL)postDarwinNotification:(NSString *)name error:(NSError **)error;
|
|
17
|
+
- (BOOL)darwinNotificationSetState:(uint64_t)state
|
|
18
|
+
name:(NSString *)name
|
|
19
|
+
error:(NSError **)error;
|
|
20
|
+
- (void)sendWithMessage:(void *)message freeWhenDone:(BOOL)freeWhenDone
|
|
21
|
+
completionQueue:(dispatch_queue_t)queue completion:(void (^)(NSError *))completion;
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
// SimulatorKit builds the Indigo wire messages; we only address and send them.
|
|
25
|
+
typedef void *(*MouseFn)(CGPoint *, CGPoint *, uint32_t, NSUInteger, CGSize, uint32_t);
|
|
26
|
+
typedef void *(*KeyFn)(uint32_t, uint32_t);
|
|
27
|
+
typedef void *(*ButtonFn)(uint32_t, uint32_t, uint32_t);
|
|
28
|
+
static MouseFn gMouse;
|
|
29
|
+
static KeyFn gKey;
|
|
30
|
+
static ButtonFn gButton;
|
|
31
|
+
static id gClient;
|
|
32
|
+
static id gDevice;
|
|
33
|
+
|
|
34
|
+
// The HID service the touchscreen lives behind, and a size of 1x1 because the
|
|
35
|
+
// builder divides the point by it and we already pass a 0..1 ratio.
|
|
36
|
+
static const uint32_t kTouchTarget = 0x32;
|
|
37
|
+
static const CGSize kUnit = { 1, 1 };
|
|
38
|
+
static const uint32_t kNoEdge = 0;
|
|
39
|
+
|
|
40
|
+
// Xcode is not always at /Applications/Xcode.app: betas, renamed installs and
|
|
41
|
+
// CI images all move it, and `xcode-select` is what decides which one is
|
|
42
|
+
// active. Resolving it at run time is the difference between working on this
|
|
43
|
+
// Mac and working on any Mac.
|
|
44
|
+
static NSString *developerDir(void) {
|
|
45
|
+
const char *override = getenv("DEVELOPER_DIR");
|
|
46
|
+
if (override && *override) return @(override);
|
|
47
|
+
|
|
48
|
+
NSTask *task = [NSTask new];
|
|
49
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/xcode-select"];
|
|
50
|
+
task.arguments = @[ @"-p" ];
|
|
51
|
+
NSPipe *pipe = [NSPipe pipe];
|
|
52
|
+
task.standardOutput = pipe;
|
|
53
|
+
task.standardError = [NSFileHandle fileHandleWithNullDevice];
|
|
54
|
+
NSError *err = nil;
|
|
55
|
+
if ([task launchAndReturnError:&err]) {
|
|
56
|
+
NSData *out = [pipe.fileHandleForReading readDataToEndOfFile];
|
|
57
|
+
[task waitUntilExit];
|
|
58
|
+
NSString *path = [[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding];
|
|
59
|
+
path = [path stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
60
|
+
if (path.length) return path;
|
|
61
|
+
}
|
|
62
|
+
return @"/Applications/Xcode.app/Contents/Developer";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** CoreSimulator is installed outside Xcode.app, but a developer dir may carry
|
|
66
|
+
* its own copy; try both rather than assuming. */
|
|
67
|
+
static BOOL loadPrivateFrameworks(NSString *devDir, void **simulatorKit) {
|
|
68
|
+
NSArray<NSString *> *coreSim = @[
|
|
69
|
+
@"/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
|
|
70
|
+
[devDir stringByAppendingString:@"/Library/PrivateFrameworks/CoreSimulator.framework/CoreSimulator"]
|
|
71
|
+
];
|
|
72
|
+
BOOL loadedCore = NO;
|
|
73
|
+
for (NSString *path in coreSim) {
|
|
74
|
+
if (dlopen(path.UTF8String, RTLD_NOW)) { loadedCore = YES; break; }
|
|
75
|
+
}
|
|
76
|
+
NSString *kitPath = [devDir stringByAppendingString:
|
|
77
|
+
@"/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit"];
|
|
78
|
+
void *kit = dlopen(kitPath.UTF8String, RTLD_NOW);
|
|
79
|
+
if (simulatorKit) *simulatorKit = kit;
|
|
80
|
+
if (!loadedCore || !kit) {
|
|
81
|
+
fprintf(stderr, "Could not load the simulator frameworks from %s\n", devDir.UTF8String);
|
|
82
|
+
return NO;
|
|
83
|
+
}
|
|
84
|
+
return YES;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static void sendMessage(void *message) {
|
|
88
|
+
if (!message) return;
|
|
89
|
+
[gClient sendWithMessage:message freeWhenDone:YES completionQueue:dispatch_get_main_queue()
|
|
90
|
+
completion:^(NSError *e) { if (e) fprintf(stderr, "send: %s\n", e.localizedDescription.UTF8String); }];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Indigo wire offsets (Indigo.h, all structs packed). A message is a mach
|
|
94
|
+
// header, then innerSize/eventType, then payloads.
|
|
95
|
+
static const size_t kMachHeaderBytes = 0x18;
|
|
96
|
+
static const size_t kInnerSizeOffset = 0x18;
|
|
97
|
+
static const size_t kEventTypeOffset = 0x1c;
|
|
98
|
+
static const size_t kPayloadOffset = 0x20; // eventKind, +4 timestamp, +0x10 event
|
|
99
|
+
static const size_t kEventOffset = 0x30; // the IndigoTouch inside payload 1
|
|
100
|
+
static const size_t kTouchBytes = 0x74; // through the trailing field
|
|
101
|
+
static const size_t kPayloadSize = 0x90; // Swift's sizeof(IndigoPayload)
|
|
102
|
+
static const size_t kSecondPayload = 0xb0; // hand-built messages use the Swift stride
|
|
103
|
+
static const size_t kMessageBytes = 0x140; // sizeof(IndigoMessage) + sizeof(IndigoPayload)
|
|
104
|
+
static const uint8_t kEventTypeSingleTouch = 0x02;
|
|
105
|
+
static const uint32_t kEventKindTouch = 0x0b;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A single-touch Indigo message for one contact.
|
|
109
|
+
*
|
|
110
|
+
* SimulatorKit has no single-touch builder — `IndigoHIDMessageForMouseNSEvent`
|
|
111
|
+
* always emits a *multi*-touch message (eventType 0x03). Sending that raw looks
|
|
112
|
+
* like it works, because SpringBoard's lock screen accepts it, but an unlocked
|
|
113
|
+
* device's apps ignore it entirely. So take the valid IndigoTouch the builder
|
|
114
|
+
* produces and re-envelope it as single-touch (0x02) with a duplicated second
|
|
115
|
+
* contact, which is what Simulator.app itself sends.
|
|
116
|
+
*/
|
|
117
|
+
static void touch(double x, double y, NSEventType type) {
|
|
118
|
+
CGPoint p = CGPointMake(x, y);
|
|
119
|
+
void *source = gMouse(&p, NULL, kTouchTarget, (NSUInteger)type, kUnit, kNoEdge);
|
|
120
|
+
if (!source) return;
|
|
121
|
+
uint8_t *src = (uint8_t *)source;
|
|
122
|
+
|
|
123
|
+
// The builder normalises by the NSSize it was handed; we pass a unit size and
|
|
124
|
+
// a ratio, so write the coordinates back explicitly.
|
|
125
|
+
memcpy(src + kEventOffset + 0x0c, &x, sizeof(double));
|
|
126
|
+
memcpy(src + kEventOffset + 0x14, &y, sizeof(double));
|
|
127
|
+
|
|
128
|
+
uint8_t *dst = (uint8_t *)calloc(1, kMessageBytes);
|
|
129
|
+
if (!dst) { free(source); return; }
|
|
130
|
+
|
|
131
|
+
// The mach header the builder produced, kept verbatim. A zeroed one is not
|
|
132
|
+
// rejected loudly — the message is simply never delivered, which is why
|
|
133
|
+
// buttons (sent raw, header intact) worked while touches disappeared.
|
|
134
|
+
memcpy(dst, src, kMachHeaderBytes);
|
|
135
|
+
|
|
136
|
+
const uint32_t innerSize = (uint32_t)kPayloadSize;
|
|
137
|
+
memcpy(dst + kInnerSizeOffset, &innerSize, sizeof(uint32_t));
|
|
138
|
+
dst[kEventTypeOffset] = kEventTypeSingleTouch;
|
|
139
|
+
memcpy(dst + kPayloadOffset, &kEventKindTouch, sizeof(uint32_t));
|
|
140
|
+
const uint64_t now = mach_absolute_time();
|
|
141
|
+
memcpy(dst + kPayloadOffset + 0x04, &now, sizeof(uint64_t));
|
|
142
|
+
memcpy(dst + kEventOffset, src + kEventOffset, kTouchBytes);
|
|
143
|
+
free(source);
|
|
144
|
+
|
|
145
|
+
// The second contact is a copy of the first, marked as the duplicate.
|
|
146
|
+
memcpy(dst + kSecondPayload, dst + kPayloadOffset, kPayloadSize);
|
|
147
|
+
const uint32_t one = 1, two = 2;
|
|
148
|
+
memcpy(dst + kSecondPayload + 0x10, &one, sizeof(uint32_t));
|
|
149
|
+
memcpy(dst + kSecondPayload + 0x14, &two, sizeof(uint32_t));
|
|
150
|
+
|
|
151
|
+
sendMessage(dst);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static void key(uint32_t code, BOOL down) { sendMessage(gKey(code, down ? 1 : 2)); }
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Biometric auth, which is not HID at all — it is a Darwin notification, the
|
|
158
|
+
* same one Simulator.app's Features menu posts. Names taken from that binary:
|
|
159
|
+
* "pearl" is Face ID, "fingerTouch" is Touch ID.
|
|
160
|
+
*
|
|
161
|
+
* A match on an unenrolled device is ignored in silence, so enrolment is set
|
|
162
|
+
* first: a state flag, then the change notification BiometricKit watches for.
|
|
163
|
+
*/
|
|
164
|
+
static void biometric(BOOL face, BOOL match) {
|
|
165
|
+
NSError *err = nil;
|
|
166
|
+
NSString *enrolledKey =
|
|
167
|
+
[NSString stringWithFormat:@"com.apple.BiometricKit_Sim.%@id_enrolled",
|
|
168
|
+
face ? @"pearl" : @"touch"];
|
|
169
|
+
[gDevice darwinNotificationSetState:1 name:enrolledKey error:&err];
|
|
170
|
+
[gDevice postDarwinNotification:@"com.apple.BiometricKit.enrollmentChanged"
|
|
171
|
+
error:&err];
|
|
172
|
+
|
|
173
|
+
NSString *name =
|
|
174
|
+
[NSString stringWithFormat:@"com.apple.BiometricKit_Sim.%@.%@",
|
|
175
|
+
face ? @"pearl" : @"fingerTouch",
|
|
176
|
+
match ? @"match" : @"nomatch"];
|
|
177
|
+
if (![gDevice postDarwinNotification:name error:&err]) {
|
|
178
|
+
fprintf(stderr, "biometric: %s\n", err.localizedDescription.UTF8String);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ButtonEventSource values from Indigo.h. Not sequential, and not the same
|
|
183
|
+
// numbering any public API uses.
|
|
184
|
+
static uint32_t buttonSource(NSString *name) {
|
|
185
|
+
if ([name isEqual:@"HOME"]) return 0x0;
|
|
186
|
+
if ([name isEqual:@"LOCK"]) return 0x1;
|
|
187
|
+
if ([name isEqual:@"SIDE"]) return 0xbb8;
|
|
188
|
+
if ([name isEqual:@"SIRI"]) return 0x400002;
|
|
189
|
+
if ([name isEqual:@"APPLE_PAY"]) return 0x1f4;
|
|
190
|
+
return UINT32_MAX;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static void button(uint32_t source, BOOL down) {
|
|
194
|
+
sendMessage(gButton(source, down ? 1 : 2, kTouchTarget));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// USB HID usage codes. There is no "type a string" event at this level.
|
|
198
|
+
static BOOL keycodeFor(unichar c, uint32_t *code, BOOL *shift) {
|
|
199
|
+
*shift = NO;
|
|
200
|
+
if (c >= 'a' && c <= 'z') { *code = c - 'a' + 4; return YES; }
|
|
201
|
+
if (c >= 'A' && c <= 'Z') { *code = c - 'A' + 4; *shift = YES; return YES; }
|
|
202
|
+
if (c >= '1' && c <= '9') { *code = c - '1' + 30; return YES; }
|
|
203
|
+
if (c == '0') { *code = 39; return YES; }
|
|
204
|
+
static const unichar plain[] = { ' ', '\n', '\t', '-', '=', '[', ']', '\\', ';', '\'', '`', ',', '.', '/' };
|
|
205
|
+
static const uint32_t plainCode[] = { 44, 40, 43, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56 };
|
|
206
|
+
static const unichar shifted[] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':', '"', '~', '<', '>', '?' };
|
|
207
|
+
static const uint32_t shiftedCode[] = { 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56 };
|
|
208
|
+
for (size_t i = 0; i < sizeof(plain) / sizeof(*plain); i++)
|
|
209
|
+
if (plain[i] == c) { *code = plainCode[i]; return YES; }
|
|
210
|
+
for (size_t i = 0; i < sizeof(shifted) / sizeof(*shifted); i++)
|
|
211
|
+
if (shifted[i] == c) { *code = shiftedCode[i]; *shift = YES; return YES; }
|
|
212
|
+
return NO;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
int main(int argc, const char **argv) {
|
|
216
|
+
@autoreleasepool {
|
|
217
|
+
if (argc < 2) { fprintf(stderr, "usage: simhid <udid> (JSON commands on stdin)\n"); return 2; }
|
|
218
|
+
NSString *wanted = @(argv[1]);
|
|
219
|
+
NSString *devDir = developerDir();
|
|
220
|
+
void *kit = NULL;
|
|
221
|
+
if (!loadPrivateFrameworks(devDir, &kit)) return 1;
|
|
222
|
+
|
|
223
|
+
gMouse = (MouseFn)dlsym(kit, "IndigoHIDMessageForMouseNSEvent");
|
|
224
|
+
gKey = (KeyFn)dlsym(kit, "IndigoHIDMessageForKeyboardArbitrary");
|
|
225
|
+
gButton = (ButtonFn)dlsym(kit, "IndigoHIDMessageForButton");
|
|
226
|
+
if (!gMouse || !gKey || !gButton) { fprintf(stderr, "indigo builders missing\n"); return 1; }
|
|
227
|
+
|
|
228
|
+
NSError *err = nil;
|
|
229
|
+
id ctx = [NSClassFromString(@"SimServiceContext") sharedServiceContextForDeveloperDir:devDir error:&err];
|
|
230
|
+
id set = [ctx defaultDeviceSetWithError:&err];
|
|
231
|
+
id device = nil;
|
|
232
|
+
for (id d in [set performSelector:@selector(devices)])
|
|
233
|
+
if ([[[d performSelector:@selector(UDID)] description] caseInsensitiveCompare:wanted] == NSOrderedSame) { device = d; break; }
|
|
234
|
+
if (!device) { fprintf(stderr, "device not found\n"); return 1; }
|
|
235
|
+
|
|
236
|
+
gDevice = device;
|
|
237
|
+
gClient = [[NSClassFromString(@"SimulatorKit.SimDeviceLegacyHIDClient") alloc] initWithDevice:device error:&err];
|
|
238
|
+
if (!gClient) { fprintf(stderr, "hid client: %s\n", err.description.UTF8String); return 1; }
|
|
239
|
+
fprintf(stderr, "indigo hid ready\n");
|
|
240
|
+
|
|
241
|
+
NSFileHandle *in = [NSFileHandle fileHandleWithStandardInput];
|
|
242
|
+
NSMutableData *pending = [NSMutableData data];
|
|
243
|
+
while (YES) {
|
|
244
|
+
NSData *chunk = [in availableData];
|
|
245
|
+
if (!chunk.length) break;
|
|
246
|
+
[pending appendData:chunk];
|
|
247
|
+
for (;;) {
|
|
248
|
+
NSRange nl = [pending rangeOfData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]
|
|
249
|
+
options:0 range:NSMakeRange(0, pending.length)];
|
|
250
|
+
if (nl.location == NSNotFound) break;
|
|
251
|
+
NSData *line = [pending subdataWithRange:NSMakeRange(0, nl.location)];
|
|
252
|
+
[pending replaceBytesInRange:NSMakeRange(0, nl.location + 1) withBytes:NULL length:0];
|
|
253
|
+
NSDictionary *cmd = [NSJSONSerialization JSONObjectWithData:line options:0 error:NULL];
|
|
254
|
+
if (![cmd isKindOfClass:NSDictionary.class]) continue;
|
|
255
|
+
NSString *type = cmd[@"type"];
|
|
256
|
+
|
|
257
|
+
if ([type isEqual:@"touch"]) {
|
|
258
|
+
NSString *phase = cmd[@"phase"];
|
|
259
|
+
NSEventType e = [phase isEqual:@"down"] ? NSEventTypeLeftMouseDown
|
|
260
|
+
: [phase isEqual:@"up"] ? NSEventTypeLeftMouseUp
|
|
261
|
+
: NSEventTypeLeftMouseDragged;
|
|
262
|
+
touch([cmd[@"x"] doubleValue], [cmd[@"y"] doubleValue], e);
|
|
263
|
+
} else if ([type isEqual:@"tap"]) {
|
|
264
|
+
double x = [cmd[@"x"] doubleValue], y = [cmd[@"y"] doubleValue];
|
|
265
|
+
touch(x, y, NSEventTypeLeftMouseDown);
|
|
266
|
+
usleep(30000);
|
|
267
|
+
touch(x, y, NSEventTypeLeftMouseUp);
|
|
268
|
+
} else if ([type isEqual:@"text"]) {
|
|
269
|
+
NSString *text = cmd[@"text"];
|
|
270
|
+
for (NSUInteger i = 0; i < text.length; i++) {
|
|
271
|
+
uint32_t code; BOOL shift;
|
|
272
|
+
if (!keycodeFor([text characterAtIndex:i], &code, &shift)) continue;
|
|
273
|
+
if (shift) key(225, YES);
|
|
274
|
+
key(code, YES); usleep(6000); key(code, NO);
|
|
275
|
+
if (shift) key(225, NO);
|
|
276
|
+
usleep(6000);
|
|
277
|
+
}
|
|
278
|
+
} else if ([type isEqual:@"button"]) {
|
|
279
|
+
uint32_t source = buttonSource(cmd[@"button"]);
|
|
280
|
+
if (source == UINT32_MAX) { fprintf(stderr, "unknown button\n"); continue; }
|
|
281
|
+
button(source, YES); usleep(60000); button(source, NO);
|
|
282
|
+
} else if ([type isEqual:@"biometric"]) {
|
|
283
|
+
biometric(![cmd[@"kind"] isEqual:@"touch"], [cmd[@"match"] boolValue]);
|
|
284
|
+
} else if ([type isEqual:@"key"]) {
|
|
285
|
+
key((uint32_t)[cmd[@"code"] unsignedIntValue], [cmd[@"down"] boolValue]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return 0;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// Native simulator capture + encode. Frames come from the display's own
|
|
2
|
+
// callbacks (no polling, no screenshots); each H.264 access unit is written to
|
|
3
|
+
// stdout as [4-byte BE length][Annex-B payload].
|
|
4
|
+
#import <Foundation/Foundation.h>
|
|
5
|
+
#import <IOSurface/IOSurface.h>
|
|
6
|
+
#import <VideoToolbox/VideoToolbox.h>
|
|
7
|
+
#import <dlfcn.h>
|
|
8
|
+
#import <stdatomic.h>
|
|
9
|
+
#import <os/lock.h>
|
|
10
|
+
|
|
11
|
+
@interface SimServiceContext : NSObject
|
|
12
|
+
+ (instancetype)sharedServiceContextForDeveloperDir:(NSString *)dir error:(NSError **)error;
|
|
13
|
+
- (id)defaultDeviceSetWithError:(NSError **)error;
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
@protocol SimDescriptorState <NSObject>
|
|
17
|
+
- (unsigned short)displayClass;
|
|
18
|
+
@end
|
|
19
|
+
@protocol SimPortDescriptor <NSObject>
|
|
20
|
+
- (id<SimDescriptorState>)state;
|
|
21
|
+
@end
|
|
22
|
+
|
|
23
|
+
@interface NSObject (SimPrivate)
|
|
24
|
+
- (id)io;
|
|
25
|
+
- (NSArray *)ioPorts;
|
|
26
|
+
- (id)descriptor;
|
|
27
|
+
- (id)framebufferSurface;
|
|
28
|
+
- (id)ioSurface;
|
|
29
|
+
- (void)registerScreenCallbacksWithUUID:(NSUUID *)uuid callbackQueue:(dispatch_queue_t)queue
|
|
30
|
+
frameCallback:(void (^)(void))frame surfacesChangedCallback:(void (^)(id, id))surfaces
|
|
31
|
+
propertiesChangedCallback:(void (^)(id))props;
|
|
32
|
+
- (void)registerCallbackWithUUID:(NSUUID *)uuid ioSurfacesChangeCallback:(void (^)(id))cb;
|
|
33
|
+
- (void)registerCallbackWithUUID:(NSUUID *)uuid damageRectanglesCallback:(void (^)(NSArray *))cb;
|
|
34
|
+
@end
|
|
35
|
+
|
|
36
|
+
// These port objects raise instead of answering respondsToSelector:, so every
|
|
37
|
+
// call has to be attempted rather than checked.
|
|
38
|
+
static NSError *guarded(void (^body)(void)) {
|
|
39
|
+
@try { body(); return nil; }
|
|
40
|
+
@catch (NSException *e) { return [NSError errorWithDomain:@"sim" code:1
|
|
41
|
+
userInfo:@{NSLocalizedDescriptionKey: e.reason ?: e.name}]; }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Xcode is not always at /Applications/Xcode.app: betas, renamed installs and
|
|
45
|
+
// CI images all move it, and `xcode-select` is what decides which one is
|
|
46
|
+
// active. Resolving it at run time is the difference between working on this
|
|
47
|
+
// Mac and working on any Mac.
|
|
48
|
+
static NSString *developerDir(void) {
|
|
49
|
+
const char *override = getenv("DEVELOPER_DIR");
|
|
50
|
+
if (override && *override) return @(override);
|
|
51
|
+
|
|
52
|
+
NSTask *task = [NSTask new];
|
|
53
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/xcode-select"];
|
|
54
|
+
task.arguments = @[ @"-p" ];
|
|
55
|
+
NSPipe *pipe = [NSPipe pipe];
|
|
56
|
+
task.standardOutput = pipe;
|
|
57
|
+
task.standardError = [NSFileHandle fileHandleWithNullDevice];
|
|
58
|
+
NSError *err = nil;
|
|
59
|
+
if ([task launchAndReturnError:&err]) {
|
|
60
|
+
NSData *out = [pipe.fileHandleForReading readDataToEndOfFile];
|
|
61
|
+
[task waitUntilExit];
|
|
62
|
+
NSString *path = [[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding];
|
|
63
|
+
path = [path stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
64
|
+
if (path.length) return path;
|
|
65
|
+
}
|
|
66
|
+
return @"/Applications/Xcode.app/Contents/Developer";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** CoreSimulator is installed outside Xcode.app, but a developer dir may carry
|
|
70
|
+
* its own copy; try both rather than assuming. */
|
|
71
|
+
static BOOL loadPrivateFrameworks(NSString *devDir, void **simulatorKit) {
|
|
72
|
+
NSArray<NSString *> *coreSim = @[
|
|
73
|
+
@"/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
|
|
74
|
+
[devDir stringByAppendingString:@"/Library/PrivateFrameworks/CoreSimulator.framework/CoreSimulator"]
|
|
75
|
+
];
|
|
76
|
+
BOOL loadedCore = NO;
|
|
77
|
+
for (NSString *path in coreSim) {
|
|
78
|
+
if (dlopen(path.UTF8String, RTLD_NOW)) { loadedCore = YES; break; }
|
|
79
|
+
}
|
|
80
|
+
NSString *kitPath = [devDir stringByAppendingString:
|
|
81
|
+
@"/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit"];
|
|
82
|
+
void *kit = dlopen(kitPath.UTF8String, RTLD_NOW);
|
|
83
|
+
if (simulatorKit) *simulatorKit = kit;
|
|
84
|
+
if (!loadedCore || !kit) {
|
|
85
|
+
fprintf(stderr, "Could not load the simulator frameworks from %s\n", devDir.UTF8String);
|
|
86
|
+
return NO;
|
|
87
|
+
}
|
|
88
|
+
return YES;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static IOSurfaceRef gSurface = NULL;
|
|
92
|
+
static os_unfair_lock gLock = OS_UNFAIR_LOCK_INIT;
|
|
93
|
+
static int gFPS = 30, gWidth = 0;
|
|
94
|
+
static int gBitrate = 2500000;
|
|
95
|
+
static VTCompressionSessionRef gSession = NULL;
|
|
96
|
+
/** Source size the session was built for. A rotation changes the surface, and a
|
|
97
|
+
* session only accepts the dimensions it was created with — so the session has
|
|
98
|
+
* to be rebuilt rather than fed buffers it will reject. */
|
|
99
|
+
static size_t gSourceWidth = 0, gSourceHeight = 0;
|
|
100
|
+
static dispatch_queue_t gWrite;
|
|
101
|
+
|
|
102
|
+
static void emit(const void *bytes, size_t len) {
|
|
103
|
+
dispatch_sync(gWrite, ^{
|
|
104
|
+
uint8_t header[4] = { (uint8_t)(len >> 24), (uint8_t)(len >> 16),
|
|
105
|
+
(uint8_t)(len >> 8), (uint8_t)len };
|
|
106
|
+
fwrite(header, 1, 4, stdout);
|
|
107
|
+
fwrite(bytes, 1, len, stdout);
|
|
108
|
+
fflush(stdout);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// H.264 on the wire is Annex-B: each NAL prefixed with 00 00 00 01. VideoToolbox
|
|
113
|
+
// hands back AVCC (4-byte lengths), and parameter sets separately — they must be
|
|
114
|
+
// re-sent before every keyframe or a decoder joining late sees nothing.
|
|
115
|
+
static void appendAnnexB(NSMutableData *out, const uint8_t *nal, size_t len) {
|
|
116
|
+
const uint8_t start[4] = { 0, 0, 0, 1 };
|
|
117
|
+
[out appendBytes:start length:4];
|
|
118
|
+
[out appendBytes:nal length:len];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static void onEncoded(void *ctx, void *src, OSStatus status, VTEncodeInfoFlags flags,
|
|
122
|
+
CMSampleBufferRef sample) {
|
|
123
|
+
if (status != noErr) { fprintf(stderr, "encoder callback status %d\n", (int)status); return; }
|
|
124
|
+
if (!sample || !CMSampleBufferDataIsReady(sample)) { fprintf(stderr, "sample not ready\n"); return; }
|
|
125
|
+
NSMutableData *out = [NSMutableData data];
|
|
126
|
+
|
|
127
|
+
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
|
|
128
|
+
BOOL keyframe = YES;
|
|
129
|
+
if (attachments && CFArrayGetCount(attachments)) {
|
|
130
|
+
CFDictionaryRef d = CFArrayGetValueAtIndex(attachments, 0);
|
|
131
|
+
keyframe = !CFDictionaryContainsKey(d, kCMSampleAttachmentKey_NotSync);
|
|
132
|
+
}
|
|
133
|
+
if (keyframe) {
|
|
134
|
+
CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sample);
|
|
135
|
+
size_t count = 0;
|
|
136
|
+
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, 0, NULL, NULL, &count, NULL);
|
|
137
|
+
for (size_t i = 0; i < count; i++) {
|
|
138
|
+
const uint8_t *ps = NULL; size_t psLen = 0;
|
|
139
|
+
if (CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, i, &ps, &psLen, NULL, NULL) == noErr)
|
|
140
|
+
appendAnnexB(out, ps, psLen);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample);
|
|
145
|
+
size_t total = 0; char *data = NULL;
|
|
146
|
+
if (CMBlockBufferGetDataPointer(block, 0, NULL, &total, &data) != noErr) return;
|
|
147
|
+
size_t offset = 0;
|
|
148
|
+
while (offset + 4 <= total) {
|
|
149
|
+
uint32_t len = 0;
|
|
150
|
+
memcpy(&len, data + offset, 4);
|
|
151
|
+
len = CFSwapInt32BigToHost(len);
|
|
152
|
+
offset += 4;
|
|
153
|
+
if (len == 0 || offset + len > total) break;
|
|
154
|
+
appendAnnexB(out, (const uint8_t *)(data + offset), len);
|
|
155
|
+
offset += len;
|
|
156
|
+
}
|
|
157
|
+
emit(out.bytes, out.length);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static void encodeH264(IOSurfaceRef surface) {
|
|
161
|
+
CVPixelBufferRef pixels = NULL;
|
|
162
|
+
CVReturn cvr = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface, NULL, &pixels);
|
|
163
|
+
if (cvr != kCVReturnSuccess) { fprintf(stderr, "CVPixelBufferCreateWithIOSurface failed: %d\n", cvr); return; }
|
|
164
|
+
size_t sourceWidth = IOSurfaceGetWidth(surface);
|
|
165
|
+
size_t sourceHeight = IOSurfaceGetHeight(surface);
|
|
166
|
+
if (gSession && (sourceWidth != gSourceWidth || sourceHeight != gSourceHeight)) {
|
|
167
|
+
fprintf(stderr, "source %zux%zu -> %zux%zu, rebuilding encoder\n",
|
|
168
|
+
gSourceWidth, gSourceHeight, sourceWidth, sourceHeight);
|
|
169
|
+
VTCompressionSessionCompleteFrames(gSession, kCMTimeInvalid);
|
|
170
|
+
VTCompressionSessionInvalidate(gSession);
|
|
171
|
+
CFRelease(gSession);
|
|
172
|
+
gSession = NULL;
|
|
173
|
+
}
|
|
174
|
+
if (!gSession) {
|
|
175
|
+
size_t w = sourceWidth, h = sourceHeight;
|
|
176
|
+
if (gWidth > 0 && w > (size_t)gWidth) {
|
|
177
|
+
h = h * gWidth / w;
|
|
178
|
+
w = gWidth;
|
|
179
|
+
}
|
|
180
|
+
// H.264 chroma is subsampled, so both axes must be even.
|
|
181
|
+
w -= w & 1;
|
|
182
|
+
h -= h & 1;
|
|
183
|
+
NSDictionary *props = @{ (id)kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder: @YES };
|
|
184
|
+
OSStatus cs;
|
|
185
|
+
if ((cs = VTCompressionSessionCreate(NULL, (int32_t)w, (int32_t)h, kCMVideoCodecType_H264,
|
|
186
|
+
(__bridge CFDictionaryRef)props, NULL, NULL, onEncoded, NULL, &gSession)) != noErr) {
|
|
187
|
+
fprintf(stderr, "VTCompressionSessionCreate failed: %d\n", (int)cs);
|
|
188
|
+
CVPixelBufferRelease(pixels); return;
|
|
189
|
+
}
|
|
190
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
|
|
191
|
+
// B-frames buy compression and cost a frame of latency. Wrong trade for a
|
|
192
|
+
// remote control.
|
|
193
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
|
|
194
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
|
|
195
|
+
// One a second. A viewer joining mid-stream, or one that has lost a
|
|
196
|
+
// packet, decodes nothing until the next keyframe arrives — and the far end
|
|
197
|
+
// has no way to ask us for one, so the interval is the only recovery.
|
|
198
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameInterval,
|
|
199
|
+
(__bridge CFTypeRef)@(gFPS));
|
|
200
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration,
|
|
201
|
+
(__bridge CFTypeRef)@1.0);
|
|
202
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ExpectedFrameRate, (__bridge CFTypeRef)@(gFPS));
|
|
203
|
+
// Without a ceiling the encoder spends whatever a full-screen scroll asks
|
|
204
|
+
// for — measured at 9.4 Mbps. A cap plus a one-second burst limit keeps a
|
|
205
|
+
// busy screen inside something a phone connection can carry.
|
|
206
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(gBitrate));
|
|
207
|
+
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_DataRateLimits,
|
|
208
|
+
(__bridge CFArrayRef)@[@(gBitrate / 8 * 1.5), @1.0]);
|
|
209
|
+
gSourceWidth = sourceWidth;
|
|
210
|
+
gSourceHeight = sourceHeight;
|
|
211
|
+
fprintf(stderr, "h264 session %zux%zu\n", w, h);
|
|
212
|
+
}
|
|
213
|
+
static int64_t n = 0;
|
|
214
|
+
CMTime pts = CMTimeMake(n++, gFPS);
|
|
215
|
+
OSStatus es = VTCompressionSessionEncodeFrame(gSession, pixels, pts, kCMTimeInvalid, NULL, NULL, NULL);
|
|
216
|
+
if (es != noErr) fprintf(stderr, "encode failed: %d\n", (int)es);
|
|
217
|
+
CVPixelBufferRelease(pixels);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
int main(int argc, const char **argv) {
|
|
221
|
+
@autoreleasepool {
|
|
222
|
+
if (argc < 2) { fprintf(stderr, "usage: simstream <udid> [--fps N] [--width N] [--bitrate N]\n"); return 2; }
|
|
223
|
+
NSString *wanted = @(argv[1]);
|
|
224
|
+
for (int i = 2; i < argc; i++) {
|
|
225
|
+
if (!strcmp(argv[i], "--fps") && i + 1 < argc) gFPS = atoi(argv[++i]);
|
|
226
|
+
else if (!strcmp(argv[i], "--width") && i + 1 < argc) gWidth = atoi(argv[++i]);
|
|
227
|
+
else if (!strcmp(argv[i], "--bitrate") && i + 1 < argc) gBitrate = atoi(argv[++i]);
|
|
228
|
+
}
|
|
229
|
+
setvbuf(stderr, NULL, _IONBF, 0);
|
|
230
|
+
gWrite = dispatch_queue_create("sim.write", DISPATCH_QUEUE_SERIAL);
|
|
231
|
+
|
|
232
|
+
NSString *devDir = developerDir();
|
|
233
|
+
if (!loadPrivateFrameworks(devDir, NULL)) return 1;
|
|
234
|
+
|
|
235
|
+
NSError *err = nil;
|
|
236
|
+
id ctx = [NSClassFromString(@"SimServiceContext") sharedServiceContextForDeveloperDir:devDir error:&err];
|
|
237
|
+
id set = [ctx defaultDeviceSetWithError:&err];
|
|
238
|
+
id device = nil;
|
|
239
|
+
for (id d in [set performSelector:@selector(devices)])
|
|
240
|
+
if ([[[d performSelector:@selector(UDID)] description] caseInsensitiveCompare:wanted] == NSOrderedSame) { device = d; break; }
|
|
241
|
+
if (!device) { fprintf(stderr, "device not found\n"); return 1; }
|
|
242
|
+
id ioClient = [device io];
|
|
243
|
+
if (!ioClient) { fprintf(stderr, "no io client\n"); return 1; }
|
|
244
|
+
|
|
245
|
+
id display = nil, fallback = nil;
|
|
246
|
+
for (id port in [ioClient ioPorts]) {
|
|
247
|
+
__block id descriptor = nil;
|
|
248
|
+
guarded(^{ descriptor = [port descriptor]; });
|
|
249
|
+
if (!descriptor) continue;
|
|
250
|
+
__block id<SimDescriptorState> st = nil;
|
|
251
|
+
id<SimPortDescriptor> typed = descriptor;
|
|
252
|
+
if (guarded(^{ st = [typed state]; }) || !st) continue;
|
|
253
|
+
__block int cls = -1;
|
|
254
|
+
if (guarded(^{ cls = [st displayClass]; })) continue;
|
|
255
|
+
if (cls == 0) { display = descriptor; break; }
|
|
256
|
+
if (!fallback) fallback = descriptor;
|
|
257
|
+
}
|
|
258
|
+
if (!display) display = fallback;
|
|
259
|
+
if (!display) { fprintf(stderr, "no display\n"); return 1; }
|
|
260
|
+
|
|
261
|
+
void (^onSurface)(id) = ^(id s) {
|
|
262
|
+
os_unfair_lock_lock(&gLock);
|
|
263
|
+
if (gSurface) { CFRelease(gSurface); gSurface = NULL; }
|
|
264
|
+
if (s) gSurface = (IOSurfaceRef)CFRetain((__bridge CFTypeRef)s);
|
|
265
|
+
os_unfair_lock_unlock(&gLock);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
NSUUID *token = [NSUUID UUID];
|
|
269
|
+
dispatch_queue_t cbq = dispatch_queue_create("sim.cb", DISPATCH_QUEUE_SERIAL);
|
|
270
|
+
NSError *newStyle = guarded(^{
|
|
271
|
+
[display registerScreenCallbacksWithUUID:token callbackQueue:cbq
|
|
272
|
+
frameCallback:^{}
|
|
273
|
+
surfacesChangedCallback:^(id fb, id masked) { onSurface(fb); }
|
|
274
|
+
propertiesChangedCallback:^(id p) {}];
|
|
275
|
+
});
|
|
276
|
+
if (newStyle) {
|
|
277
|
+
guarded(^{ [display registerCallbackWithUUID:token ioSurfacesChangeCallback:^(id s) { onSurface(s); }]; });
|
|
278
|
+
guarded(^{ [display registerCallbackWithUUID:token damageRectanglesCallback:^(NSArray *r) {}]; });
|
|
279
|
+
fprintf(stderr, "old-style callbacks\n");
|
|
280
|
+
} else {
|
|
281
|
+
fprintf(stderr, "new-style SimScreen callbacks\n");
|
|
282
|
+
}
|
|
283
|
+
if (!gSurface) {
|
|
284
|
+
__block id s = nil;
|
|
285
|
+
guarded(^{ s = [display framebufferSurface]; });
|
|
286
|
+
if (!s) guarded(^{ s = [display ioSurface]; });
|
|
287
|
+
if (s) onSurface(s);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Encode on a timer rather than per callback: the display fires ~60 times a
|
|
291
|
+
// second and the surface is a live buffer, so sampling it on our own cadence
|
|
292
|
+
// costs nothing and never queues work we would have to drop later.
|
|
293
|
+
dispatch_source_t tick = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
|
|
294
|
+
dispatch_queue_create("sim.encode", DISPATCH_QUEUE_SERIAL));
|
|
295
|
+
dispatch_source_set_timer(tick, dispatch_time(DISPATCH_TIME_NOW, 0),
|
|
296
|
+
(uint64_t)(NSEC_PER_SEC / gFPS), NSEC_PER_SEC / 100);
|
|
297
|
+
dispatch_source_set_event_handler(tick, ^{
|
|
298
|
+
os_unfair_lock_lock(&gLock);
|
|
299
|
+
IOSurfaceRef s = gSurface ? (IOSurfaceRef)CFRetain(gSurface) : NULL;
|
|
300
|
+
os_unfair_lock_unlock(&gLock);
|
|
301
|
+
// A blanked or sleeping display vends no surface, and the symptom is
|
|
302
|
+
// otherwise a black panel with no error anywhere. Logged on the change
|
|
303
|
+
// only — a heartbeat here is thousands of lines an hour.
|
|
304
|
+
static int had = -1;
|
|
305
|
+
const int has = s ? 1 : 0;
|
|
306
|
+
if (has != had) {
|
|
307
|
+
had = has;
|
|
308
|
+
fprintf(stderr, "display surface %s\n", has ? "available" : "gone");
|
|
309
|
+
}
|
|
310
|
+
if (!s) return;
|
|
311
|
+
encodeH264(s);
|
|
312
|
+
CFRelease(s);
|
|
313
|
+
});
|
|
314
|
+
dispatch_resume(tick);
|
|
315
|
+
fprintf(stderr, "streaming h264 at %d fps\n", gFPS);
|
|
316
|
+
[[NSRunLoop currentRunLoop] run];
|
|
317
|
+
return 0;
|
|
318
|
+
}
|
|
319
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@factiii/runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Factiii Runner, run Board AI agents on a machine you control. Pairs with the Factiii web/mobile clients over WebRTC.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"keywords": [
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist/cli.js",
|
|
26
|
+
"native/",
|
|
26
27
|
"plugin/",
|
|
27
28
|
"index/index.js",
|
|
28
29
|
"index/image-package.json",
|