Sync source edition with VCC Plus 3.7.0 build 63
This commit is contained in:
1 parent
c652a5276d
commit
b2ecec4d87
86 files changed
+10519
-472
No files matched your search
@@ -18,9 +18,22 @@ struct InputDiagnosticObservation {
|
||||
@MainActor
|
||||
final class InputHub: ObservableObject {
|
||||
@Published var settings: InputSettings { didSet {
|
||||
guard settings != oldValue else { return }
|
||||
release(); epoch &+= 1
|
||||
do { try store.save(settings); error = nil } catch { self.error = "Could not save input settings: \(error.localizedDescription)" }
|
||||
guard !normalizingSettings else { return }
|
||||
let previousPolicy = automaticRightController
|
||||
if !applyingAutomaticSettings, settings.right != oldValue.right { automaticRightController = false }
|
||||
// Normalize this one user edit before releasing/routing/persisting it.
|
||||
// @Published can reenter this observer during normalization; only the
|
||||
// outer edit releases input, advances the epoch and persists settings.
|
||||
let assigned = settings.assigningSoleController(devices.map(\.id), automaticRight: automaticRightController)
|
||||
if settings != assigned {
|
||||
normalizingSettings = true
|
||||
settings = assigned
|
||||
normalizingSettings = false
|
||||
}
|
||||
if settings.right.source == .controller, !settings.right.device.isEmpty { automaticRightController = false }
|
||||
guard settings != oldValue || previousPolicy != automaticRightController else { return }
|
||||
if settings != oldValue { release(); epoch &+= 1 }
|
||||
saveSettings()
|
||||
} }
|
||||
@Published private(set) var devices: [ControllerReading] = []
|
||||
@Published private(set) var preview = [JoystickSample(), JoystickSample()]
|
||||
@@ -29,7 +42,11 @@ final class InputHub: ObservableObject {
|
||||
var acceptsInput: () -> Bool = { false }
|
||||
private let store: InputSettingsStore
|
||||
private let controller: VCCMachineController
|
||||
private let hardware: ControllerDevices
|
||||
private let hardware: ControllerDevices?
|
||||
private let controllerReadings: (() -> [ControllerReading])?
|
||||
private var automaticRightController = false
|
||||
private var applyingAutomaticSettings = false
|
||||
private var normalizingSettings = false
|
||||
private var timer: Timer?
|
||||
private var routing = InputRouting()
|
||||
private var last: [JoystickSample] = []
|
||||
@@ -40,20 +57,49 @@ final class InputHub: ObservableObject {
|
||||
private var lastDisplayAcceptsInput: Bool?
|
||||
private var submitted: [JoystickSample]?
|
||||
private var submittedInterfaces: [JoystickInterface]?
|
||||
init(controller: VCCMachineController, store: InputSettingsStore = .standard) {
|
||||
self.controller = controller; self.store = store; hardware = ControllerDevices()
|
||||
do { settings = try store.load() } catch {
|
||||
init(controller: VCCMachineController, store: InputSettingsStore = .standard,
|
||||
controllerReadings: (() -> [ControllerReading])? = nil, automaticallyPoll: Bool = true) {
|
||||
self.controller = controller; self.store = store
|
||||
self.controllerReadings = controllerReadings
|
||||
hardware = controllerReadings == nil ? ControllerDevices() : nil
|
||||
do {
|
||||
let saved = try store.loadState()
|
||||
settings = saved.settings; automaticRightController = saved.automaticRightController
|
||||
} catch {
|
||||
settings = InputSettings(); self.error = "Could not read input settings: \(error.localizedDescription)"
|
||||
}
|
||||
timer = Timer.scheduledTimer(withTimeInterval: 1 / 60, repeats: true) { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.poll() }
|
||||
}
|
||||
if automaticallyPoll {
|
||||
timer = Timer.scheduledTimer(withTimeInterval: 1 / 60, repeats: true) { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.poll() }
|
||||
}
|
||||
#if VCC_UI_TESTING
|
||||
// Keep the authored preview moving even during native menu tracking.
|
||||
if let timer { RunLoop.main.add(timer, forMode: .common) }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
deinit { timer?.invalidate() }
|
||||
private func saveSettings() {
|
||||
do { try store.save(settings, automaticRightController: automaticRightController); error = nil }
|
||||
catch { self.error = "Could not save input settings: \(error.localizedDescription)" }
|
||||
}
|
||||
/// Built-in reset is explicit; personal profile inputs never inherit the
|
||||
/// fresh-install controller-discovery policy from this Mac's settings store.
|
||||
func applyProfileSettings(_ value: InputSettings) { applySettings(value, automaticRight: false) }
|
||||
/// Library hints can fill unused ports without turning an unchanged fresh
|
||||
/// Right fallback into an explicit user choice.
|
||||
func applyLibrarySettings(_ value: InputSettings) {
|
||||
applySettings(value, automaticRight: automaticRightController && value.right == settings.right)
|
||||
}
|
||||
func restoreDefaultSettings() { applySettings(.freshDefaults, automaticRight: true) }
|
||||
private func applySettings(_ value: InputSettings, automaticRight: Bool) {
|
||||
let previous = settings, previousPolicy = automaticRightController
|
||||
automaticRightController = automaticRight
|
||||
applyingAutomaticSettings = true
|
||||
settings = value
|
||||
applyingAutomaticSettings = false
|
||||
if settings == previous, automaticRightController != previousPolicy { saveSettings() }
|
||||
}
|
||||
func swap() { var next = settings; next.right = settings.left; next.left = settings.right; settings = next }
|
||||
func key(_ code: UInt16, down: Bool) -> Bool {
|
||||
routing.key(code, down: down, settings: settings)
|
||||
@@ -73,17 +119,27 @@ final class InputHub: ObservableObject {
|
||||
lastPollUptime = nil; lastApplicationActive = nil; lastDisplayAcceptsInput = nil
|
||||
submit([JoystickSample(), JoystickSample()])
|
||||
}
|
||||
private func poll() {
|
||||
func poll() {
|
||||
let readings: [ControllerReading]
|
||||
if let controllerReadings { readings = controllerReadings() }
|
||||
else {
|
||||
#if VCC_UI_TESTING
|
||||
// Exercise the real preview/selection views with changing input even
|
||||
// when no physical controller is attached to the test Mac.
|
||||
let phase = ProcessInfo.processInfo.systemUptime
|
||||
let readings = [ControllerReading(id: "ui-fixture", name: "Authored test controller",
|
||||
readings = [ControllerReading(id: "ui-fixture", name: "Authored test controller",
|
||||
x: sin(phase), y: cos(phase), rx: sin(phase * 2), ry: cos(phase * 2))]
|
||||
#else
|
||||
let readings = hardware.read()
|
||||
readings = hardware?.read() ?? []
|
||||
#endif
|
||||
}
|
||||
if devices != readings { devices = readings }
|
||||
let assigned = settings.assigningSoleController(readings.map(\.id), automaticRight: automaticRightController)
|
||||
if assigned != settings {
|
||||
applyingAutomaticSettings = true
|
||||
settings = assigned
|
||||
applyingAutomaticSettings = false
|
||||
}
|
||||
let displayAcceptsInput = acceptsInput(), applicationActive = NSApp.isActive
|
||||
let active = displayAcceptsInput && applicationActive
|
||||
lastPollUptime = ProcessInfo.processInfo.systemUptime
|
||||
|
||||
Reference in new issue
Block a user