serve-sim 0.1.50-beta.151.1 → 0.1.50-beta.154.1

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.
@@ -6,7 +6,9 @@ import SimNativeSupport
6
6
  /// replies acknowledge dispatch, so a failed command never switches capture.
7
7
  final class DuoHIDBridge {
8
8
  private let udid: String
9
- private let executable: String
9
+ /// Settable so JS can swap in a launchable copy once the device is known
10
+ /// to be a Duo (resolving it costs a `codesign` probe).
11
+ var executable: String
10
12
  private var process: Process?
11
13
  private var input: FileHandle?
12
14
  private var output: FileHandle?
@@ -51,8 +53,12 @@ final class DuoHIDBridge {
51
53
  while Date() < deadline && reply.count < 64 {
52
54
  var descriptor = pollfd(fd: output.fileDescriptor, events: Int16(POLLIN), revents: 0)
53
55
  let remaining = max(1, Int32(deadline.timeIntervalSinceNow * 1000))
54
- guard poll(&descriptor, 1, remaining) > 0,
55
- let byte = try output.read(upToCount: 1), !byte.isEmpty else { break }
56
+ guard poll(&descriptor, 1, remaining) > 0 else { break }
57
+ guard let byte = try output.read(upToCount: 1), !byte.isEmpty else {
58
+ // Its own stderr (inherited above) says why, e.g. dyld refusing to start it.
59
+ fputs("[hid] Duo guest helper exited without replying to \"\(command)\": \(executable)\n", stderr)
60
+ break
61
+ }
56
62
  if byte[0] == 10 { return String(data: reply, encoding: .utf8) == "OK" }
57
63
  reply.append(byte)
58
64
  }
@@ -182,15 +182,22 @@ actor HIDInjector {
182
182
  static let edgeRight: UInt32 = 4 // Right edge
183
183
 
184
184
  /// Synchronously hand an already-built Indigo message to the guest, freeing it.
185
- /// Must run on `inputQueue`.
186
- private func rawSend(_ msg: UnsafeMutableRawPointer) {
187
- guard let client = hidClient, let sendSel = sendSel else { free(msg); return }
185
+ /// Must run on `inputQueue`. Returns false when there is no HID client to
186
+ /// send through (setup never completed), so callers can report the drop.
187
+ @discardableResult
188
+ private func rawSend(_ msg: UnsafeMutableRawPointer) -> Bool {
189
+ guard let client = hidClient, let sendSel = sendSel else {
190
+ print("[hid] Dropped HID message: no SimDeviceLegacyHIDClient")
191
+ free(msg)
192
+ return false
193
+ }
188
194
  typealias SendFunc = @convention(c) (AnyObject, Selector, UnsafeMutableRawPointer, ObjCBool, AnyObject?, AnyObject?) -> Void
189
195
  guard let sendIMP = class_getMethodImplementation(object_getClass(client)!, sendSel) else {
190
196
  free(msg)
191
- return
197
+ return false
192
198
  }
193
199
  unsafeBitCast(sendIMP, to: SendFunc.self)(client, sendSel, msg, ObjCBool(true), nil, nil)
200
+ return true
194
201
  }
195
202
 
196
203
  /// Build a single-finger touch message (normalized 0..1 coords). Pure — safe
@@ -209,25 +216,28 @@ actor HIDInjector {
209
216
 
210
217
  /// Synchronously build + send a single touch. For use inside gesture blocks
211
218
  /// already running on `inputQueue`.
212
- private func rawSendTouch(type: String, x: Double, y: Double, edge: UInt32 = 0) {
213
- if let msg = touchMessage(type: type, x: x, y: y, edge: edge) { rawSend(msg) }
219
+ @discardableResult
220
+ private func rawSendTouch(type: String, x: Double, y: Double, edge: UInt32 = 0) -> Bool {
221
+ guard let msg = touchMessage(type: type, x: x, y: y, edge: edge) else { return false }
222
+ return rawSend(msg)
214
223
  }
215
224
 
216
- func sendTouch(type: String, x: Double, y: Double, screenWidth: Int, screenHeight: Int, edge: UInt32 = 0) {
225
+ /// Returns whether the touch was handed to the HID client.
226
+ func sendTouch(type: String, x: Double, y: Double, screenWidth: Int, screenHeight: Int, edge: UInt32 = 0) -> Bool {
217
227
  updateTouchTarget(width: screenWidth, height: screenHeight)
218
- guard let msg = touchMessage(type: type, x: x, y: y, edge: edge) else { return }
228
+ guard let msg = touchMessage(type: type, x: x, y: y, edge: edge) else { return false }
219
229
  hidLog("[hid] Sending \(type) at (\(String(format:"%.3f",x)),\(String(format:"%.3f",y)))\(edge > 0 ? " edge=\(edge)" : "")")
220
- rawSend(msg)
230
+ return rawSend(msg)
221
231
  }
222
232
 
223
- func sendMultiTouch(type: String, x1: Double, y1: Double, x2: Double, y2: Double, screenWidth: Int, screenHeight: Int) {
224
- guard let mouseFunc = mouseFunc else { return }
233
+ func sendMultiTouch(type: String, x1: Double, y1: Double, x2: Double, y2: Double, screenWidth: Int, screenHeight: Int) -> Bool {
234
+ guard let mouseFunc = mouseFunc else { return false }
225
235
 
226
236
  let eventType: Int32
227
237
  switch type {
228
238
  case "begin", "move": eventType = 1
229
239
  case "end": eventType = 2
230
- default: return
240
+ default: return false
231
241
  }
232
242
 
233
243
  updateTouchTarget(width: screenWidth, height: screenHeight)
@@ -236,11 +246,11 @@ actor HIDInjector {
236
246
  var point2 = CGPoint(x: x2, y: y2)
237
247
  guard let rawMsg = mouseFunc(&point1, &point2, touchTarget, eventType, 1.0, 1.0, 0) else {
238
248
  print("[hid] IndigoHIDMessageForMouseNSEvent returned nil for multi-touch \(type)")
239
- return
249
+ return false
240
250
  }
241
251
 
242
252
  hidLog("[hid] Multi-touch \(type) f1=(\(String(format:"%.3f",x1)),\(String(format:"%.3f",y1))) f2=(\(String(format:"%.3f",x2)),\(String(format:"%.3f",y2)))")
243
- rawSend(rawMsg)
253
+ return rawSend(rawMsg)
244
254
  }
245
255
 
246
256
  // MARK: - Button events
@@ -266,13 +276,14 @@ actor HIDInjector {
266
276
 
267
277
  /// Synchronously build + send a hardware-button message. Call only inside an
268
278
  /// `inputQueue` block (button sequences below run there).
269
- private func sendHIDButton(eventSource: Int32, direction: Int32) {
270
- guard let buttonFunc = buttonFunc else { return }
279
+ @discardableResult
280
+ private func sendHIDButton(eventSource: Int32, direction: Int32) -> Bool {
281
+ guard let buttonFunc = buttonFunc else { return false }
271
282
  guard let msg = buttonFunc(eventSource, direction, Self.buttonTargetHardware) else {
272
283
  print("[hid] IndigoHIDMessageForButton returned nil")
273
- return
284
+ return false
274
285
  }
275
- rawSend(msg)
286
+ return rawSend(msg)
276
287
  }
277
288
 
278
289
  // MARK: - Keyboard events
@@ -432,43 +443,46 @@ actor HIDInjector {
432
443
  /// volume up/down, the action button, and the watch side button.
433
444
  /// - phase: "down" / "up" hold the button for natural long-presses (power
434
445
  /// off slider, side-button menus); "press" sends a momentary down+up.
435
- func sendButtonHID(page: UInt32, usage: UInt32, phase: String) async {
446
+ /// Returns whether the press was handed to the guest.
447
+ @discardableResult
448
+ func sendButtonHID(page: UInt32, usage: UInt32, phase: String) async -> Bool {
436
449
  if let duoBridge {
437
450
  switch phase {
438
- case "down": _ = duoBridge.send("key \(page) \(usage) 1")
439
- case "up": _ = duoBridge.send("key \(page) \(usage) 0")
451
+ case "down": return duoBridge.send("key \(page) \(usage) 1")
452
+ case "up": return duoBridge.send("key \(page) \(usage) 0")
440
453
  case "press":
441
- guard duoBridge.send("key \(page) \(usage) 1") else { return }
454
+ guard duoBridge.send("key \(page) \(usage) 1") else { return false }
442
455
  try? await Task.sleep(for: .milliseconds(250))
443
- _ = duoBridge.send("key \(page) \(usage) 0")
444
- default: break
456
+ return duoBridge.send("key \(page) \(usage) 0")
457
+ default: return false
445
458
  }
446
- return
447
459
  }
448
460
  guard let arb = hidArbitraryFunc else {
449
461
  print("[hid] Arbitrary HID injection unavailable (page=\(page) usage=\(usage))")
450
- return
462
+ return false
451
463
  }
452
464
  let target = Self.buttonHIDTarget
453
- func emit(_ direction: UInt32) {
465
+ func emit(_ direction: UInt32) -> Bool {
454
466
  guard let msg = arb(target, page, usage, direction) else {
455
467
  print("[hid] IndigoHIDMessageForHIDArbitrary returned nil (page=\(page) usage=\(usage) dir=\(direction))")
456
- return
468
+ return false
457
469
  }
458
- rawSend(msg)
470
+ return rawSend(msg)
459
471
  }
460
472
  hidLog("[hid] HID button page=\(page) usage=\(usage) phase=\(phase)")
461
473
  switch phase {
462
- case "down": emit(1)
463
- case "up": emit(2)
474
+ case "down": return emit(1)
475
+ case "up": return emit(2)
464
476
  default:
465
- emit(1)
477
+ guard emit(1) else { return false }
466
478
  try? await Task.sleep(for: .seconds(0.05))
467
- emit(2)
479
+ return emit(2)
468
480
  }
469
481
  }
470
482
 
471
- func sendButton(button: String, deviceUDID: String) async {
483
+ /// Returns whether the button was handed to the guest (false for an
484
+ /// unknown button or when the HID client / message builder is missing).
485
+ func sendButton(button: String, deviceUDID: String) async -> Bool {
472
486
  hidLog("[hid] Sending button: \(button)")
473
487
 
474
488
  switch button {
@@ -481,43 +495,45 @@ actor HIDInjector {
481
495
  // unconditionally rather than depending on whether the HID symbol
482
496
  // resolved.
483
497
  await launchSpringBoard(deviceUDID: deviceUDID)
498
+ return true
484
499
 
485
500
  case "swipe_home":
486
- sendSwipeHome()
501
+ return sendSwipeHome()
487
502
 
488
503
  case "app_switcher":
489
504
  if buttonFunc != nil {
490
505
  // Double home press with delay for app switcher
491
- sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonDown)
506
+ guard sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonDown) else { return false }
492
507
  sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonUp)
493
508
  try? await Task.sleep(for: .seconds(0.15))
494
509
  sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonDown)
495
- sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonUp)
510
+ return sendHIDButton(eventSource: Self.buttonSourceHome, direction: Self.buttonUp)
496
511
  } else {
497
512
  print("[hid] App switcher not available (IndigoHIDMessageForButton not loaded)")
513
+ return false
498
514
  }
499
515
 
500
516
  case "lock":
501
517
  if duoBridge != nil {
502
- await sendButtonHID(page: 0x0c, usage: 0x30, phase: "press")
503
- return
518
+ return await sendButtonHID(page: 0x0c, usage: 0x30, phase: "press")
504
519
  }
505
- sendHIDButton(eventSource: Self.buttonSourceLock, direction: Self.buttonDown)
506
- sendHIDButton(eventSource: Self.buttonSourceLock, direction: Self.buttonUp)
520
+ guard sendHIDButton(eventSource: Self.buttonSourceLock, direction: Self.buttonDown) else { return false }
521
+ return sendHIDButton(eventSource: Self.buttonSourceLock, direction: Self.buttonUp)
507
522
 
508
523
  case "siri":
509
524
  // Holding Siri for ~300ms matches Simulator.app's "hold side button
510
525
  // to invoke Siri" gesture; a tap is ignored.
511
- sendHIDButton(eventSource: Self.buttonSourceSiri, direction: Self.buttonDown)
526
+ guard sendHIDButton(eventSource: Self.buttonSourceSiri, direction: Self.buttonDown) else { return false }
512
527
  try? await Task.sleep(for: .seconds(0.3))
513
- sendHIDButton(eventSource: Self.buttonSourceSiri, direction: Self.buttonUp)
528
+ return sendHIDButton(eventSource: Self.buttonSourceSiri, direction: Self.buttonUp)
514
529
 
515
530
  case "side_button":
516
- sendHIDButton(eventSource: Self.buttonSourceSideButton, direction: Self.buttonDown)
517
- sendHIDButton(eventSource: Self.buttonSourceSideButton, direction: Self.buttonUp)
531
+ guard sendHIDButton(eventSource: Self.buttonSourceSideButton, direction: Self.buttonDown) else { return false }
532
+ return sendHIDButton(eventSource: Self.buttonSourceSideButton, direction: Self.buttonUp)
518
533
 
519
534
  default:
520
535
  print("[hid] Unknown button: \(button)")
536
+ return false
521
537
  }
522
538
  }
523
539
 
@@ -550,6 +566,8 @@ actor HIDInjector {
550
566
 
551
567
  func isFoldable() -> Bool { duoBridge != nil }
552
568
 
569
+ func setDuoHelper(_ path: String) { duoBridge?.executable = path }
570
+
553
571
  func close() { duoBridge = nil }
554
572
 
555
573
  private func updateTouchTarget(width: Int, height: Int) {
@@ -601,7 +619,7 @@ actor HIDInjector {
601
619
  /// Synthesize a swipe-up-from-bottom gesture (Face ID "go home" gesture).
602
620
  /// Uses IndigoHIDEdge.bottom to flag touches as system edge gestures,
603
621
  /// which iOS interprets as the home indicator swipe.
604
- private func sendSwipeHome() {
622
+ private func sendSwipeHome() -> Bool {
605
623
  let xPos = 0.5
606
624
  let yStart = 0.95
607
625
  let yEnd = 0.35
@@ -610,7 +628,7 @@ actor HIDInjector {
610
628
  let edge = Self.edgeBottom
611
629
 
612
630
  // Touch down at bottom edge
613
- rawSendTouch(type: "begin", x: xPos, y: yStart, edge: edge)
631
+ guard rawSendTouch(type: "begin", x: xPos, y: yStart, edge: edge) else { return false }
614
632
  Thread.sleep(forTimeInterval: stepDelay)
615
633
 
616
634
  // Interpolated moves upward
@@ -622,7 +640,7 @@ actor HIDInjector {
622
640
  }
623
641
 
624
642
  // Touch up
625
- rawSendTouch(type: "end", x: xPos, y: yEnd, edge: edge)
643
+ return rawSendTouch(type: "end", x: xPos, y: yEnd, edge: edge)
626
644
  }
627
645
 
628
646
  /// Launches SpringBoard off the actor, falling back to `simctl`, and returns
@@ -33,57 +33,75 @@ private func u32(_ v: Int) -> UInt32 {
33
33
  setupTask = Task { try await injector.setup(deviceUDID: udid, duoHelper: duoHelper) }
34
34
  }
35
35
 
36
+ // Every input method waits for `setup` first. The session is created on
37
+ // the first /ws connect, so a CLI `tap` lands milliseconds after this
38
+ // constructor; without the wait the frame reached the actor before setup
39
+ // and was dropped (no HID client yet) while the CLI exited 0 (#136).
40
+ // A failed setup (device gone / not booted) now throws to JS instead of
41
+ // silently swallowing every event.
42
+
36
43
  @NodeMethod func touch(_ type: String, _ x: Double, _ y: Double,
37
- _ w: Int, _ h: Int, _ edge: Int) async {
38
- await injector.sendTouch(type: type, x: x, y: y,
39
- screenWidth: w, screenHeight: h, edge: u32(edge))
44
+ _ w: Int, _ h: Int, _ edge: Int) async throws -> Bool {
45
+ try await setupTask.value
46
+ return await injector.sendTouch(type: type, x: x, y: y,
47
+ screenWidth: w, screenHeight: h, edge: u32(edge))
40
48
  }
41
49
 
42
50
  @NodeMethod func multiTouch(_ type: String, _ x1: Double, _ y1: Double,
43
- _ x2: Double, _ y2: Double, _ w: Int, _ h: Int) async {
44
- await injector.sendMultiTouch(type: type, x1: x1, y1: y1, x2: x2, y2: y2,
45
- screenWidth: w, screenHeight: h)
51
+ _ x2: Double, _ y2: Double, _ w: Int, _ h: Int) async throws -> Bool {
52
+ try await setupTask.value
53
+ return await injector.sendMultiTouch(type: type, x1: x1, y1: y1, x2: x2, y2: y2,
54
+ screenWidth: w, screenHeight: h)
46
55
  }
47
56
 
48
- @NodeMethod func button(_ button: String) async {
49
- await injector.sendButton(button: button, deviceUDID: udid)
57
+ @NodeMethod func button(_ button: String) async throws -> Bool {
58
+ try await setupTask.value
59
+ return await injector.sendButton(button: button, deviceUDID: udid)
50
60
  }
51
61
 
52
- @NodeMethod func buttonHid(_ page: Int, _ usage: Int, _ phase: String) async {
53
- await injector.sendButtonHID(page: u32(page), usage: u32(usage), phase: phase)
62
+ @NodeMethod func buttonHid(_ page: Int, _ usage: Int, _ phase: String) async throws -> Bool {
63
+ try await setupTask.value
64
+ return await injector.sendButtonHID(page: u32(page), usage: u32(usage), phase: phase)
54
65
  }
55
66
 
56
- @NodeMethod func key(_ type: String, _ usage: Int) async {
67
+ @NodeMethod func key(_ type: String, _ usage: Int) async throws {
68
+ try await setupTask.value
57
69
  await injector.sendKey(type: type, usage: u32(usage))
58
70
  }
59
71
 
60
72
  /// NaN anchorX/anchorY mean "center" (the Swift API's nil).
61
73
  @NodeMethod func scroll(_ dx: Double, _ dy: Double,
62
- _ anchorX: Double, _ anchorY: Double, _ w: Int, _ h: Int) async {
74
+ _ anchorX: Double, _ anchorY: Double, _ w: Int, _ h: Int) async throws {
75
+ try await setupTask.value
63
76
  await injector.sendScroll(dx: dx, dy: dy,
64
77
  anchorX: anchorX.isNaN ? nil : anchorX,
65
78
  anchorY: anchorY.isNaN ? nil : anchorY,
66
79
  screenWidth: w, screenHeight: h)
67
80
  }
68
81
 
69
- @NodeMethod func digitalCrown(_ delta: Double) async {
82
+ @NodeMethod func digitalCrown(_ delta: Double) async throws {
83
+ try await setupTask.value
70
84
  await injector.sendDigitalCrown(delta: delta)
71
85
  }
72
86
 
73
- @NodeMethod func orientation(_ orientation: Int) async -> Bool {
74
- await injector.sendOrientation(orientation: u32(orientation))
87
+ @NodeMethod func orientation(_ orientation: Int) async throws -> Bool {
88
+ try await setupTask.value
89
+ return await injector.sendOrientation(orientation: u32(orientation))
75
90
  }
76
91
 
77
- @NodeMethod func memoryWarning() async {
92
+ @NodeMethod func memoryWarning() async throws {
93
+ try await setupTask.value
78
94
  await injector.simulateMemoryWarning()
79
95
  }
80
96
 
81
- @NodeMethod func softwareKeyboard() async {
97
+ @NodeMethod func softwareKeyboard() async throws {
98
+ try await setupTask.value
82
99
  await injector.toggleSoftwareKeyboard()
83
100
  }
84
101
 
85
- @NodeMethod func caDebug(_ name: String, _ enabled: Bool) async -> Bool {
86
- await injector.setCADebugOption(name: name, enabled: enabled)
102
+ @NodeMethod func caDebug(_ name: String, _ enabled: Bool) async throws -> Bool {
103
+ try await setupTask.value
104
+ return await injector.setCADebugOption(name: name, enabled: enabled)
87
105
  }
88
106
 
89
107
  @NodeMethod func isFoldable() async throws -> Bool {
@@ -91,6 +109,12 @@ private func u32(_ v: Int) -> UInt32 {
91
109
  return await injector.isFoldable()
92
110
  }
93
111
 
112
+ /// Replace the guest helper the Duo bridge launches. No-op for other devices.
113
+ @NodeMethod func setDuoHelper(_ path: String) async throws {
114
+ try await setupTask.value
115
+ await injector.setDuoHelper(path)
116
+ }
117
+
94
118
  @NodeMethod func close() async {
95
119
  _ = try? await setupTask.value
96
120
  await injector.close()