// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import Foundation /// A gamepad binding: a control, optionally while another is held (Android's /// GamepadChord). modifier 0 = none. struct GamepadChord: Hashable { let modifier: Int let code: Int } /// The three rebindable maps (Android's KeyboardInput / GamepadInput / MidiInput /// bindings, persisted like BindingStore: one `code=actionId` line each, gamepad /// `modifier:code=actionId`). Keyboard codes are USB HID usages (GCKeyCode). @MainActor final class InputBindings: ObservableObject { static let shared = InputBindings() @Published private(set) var keyboard: [Int: String] @Published private(set) var gamepad: [GamepadChord: String] @Published private(set) var midi: [Int: String] private init() { let s = AppSettings.shared keyboard = s.keyboardBindings.map(Self.decodeInt) ?? Self.defaultKeyboard gamepad = s.gamepadBindings.map(Self.decodeChord) ?? Self.defaultGamepad midi = s.midiBindings.map(Self.decodeInt) ?? [:] } /// After a backup restore: read the maps from the settings again. func reload() { let s = AppSettings.shared keyboard = s.keyboardBindings.map(Self.decodeInt) ?? Self.defaultKeyboard gamepad = s.gamepadBindings.map(Self.decodeChord) ?? Self.defaultGamepad midi = s.midiBindings.map(Self.decodeInt) ?? [:] } // Each action keeps at most one binding per device kind (Android's learn). func bindKey(_ code: Int, to id: String) { keyboard = keyboard.filter { $0.value != id }; keyboard[code] = id; save() } func bindGamepad(_ chord: GamepadChord, to id: String) { gamepad = gamepad.filter { $0.value != id }; gamepad[chord] = id; save() } func bindMidi(_ cc: Int, to id: String) { midi = midi.filter { $0.value != id }; midi[cc] = id; save() } func clear(_ id: String, source: InputSource) { switch source { case .keyboard: keyboard = keyboard.filter { $0.value != id } case .gamepad: gamepad = gamepad.filter { $0.value != id } case .midi: midi = midi.filter { $0.value != id } case .touch: return } save() } func resetDefaults(_ source: InputSource) { switch source { case .keyboard: keyboard = Self.defaultKeyboard case .gamepad: gamepad = Self.defaultGamepad case .midi: midi = [:] case .touch: return } save() } func keyFor(_ id: String) -> Int? { keyboard.first { $0.value == id }?.key } func chordFor(_ id: String) -> GamepadChord? { gamepad.first { $0.value == id }?.key } func ccFor(_ id: String) -> Int? { midi.first { $0.value == id }?.key } private func save() { let s = AppSettings.shared s.keyboardBindings = keyboard.map { "\($0.key)=\($0.value)" }.sorted().joined(separator: "\n") s.gamepadBindings = gamepad.map { "\($0.key.modifier):\($0.key.code)=\($0.value)" }.sorted().joined(separator: "\n") s.midiBindings = midi.map { "\($0.key)=\($0.value)" }.sorted().joined(separator: "\n") } private static func decodeInt(_ s: String) -> [Int: String] { var out: [Int: String] = [:] for line in s.split(separator: "\n") { guard let eq = line.firstIndex(of: "="), eq > line.startIndex, let code = Int(line[.. [GamepadChord: String] { var out: [GamepadChord: String] = [:] for line in s.split(separator: "\n") { guard let eq = line.firstIndex(of: "=") else { continue } let parts = line[.. Int { if let v = c.asciiValue, v >= 97, v <= 122 { return Int(v) - 97 + 4 } // a..z if c == "0" { return 39 } return Int(c.asciiValue! - 49) + 30 // 1..9 } static func keyLabel(_ code: Int) -> String { switch code { case 4...29: return String(UnicodeScalar(UInt8(code - 4 + 65))) case 30...38: return String(code - 29) case 39: return "0" case Key.enter: return "Return" case 41: return "Esc" case Key.backspace: return "Delete" case Key.tab: return "Tab" case Key.space: return "Space" case Key.minus: return "-" case Key.equals: return "=" case 47: return "[" case 48: return "]" case 49: return "\\" case 51: return ";" case 52: return "'" case Key.grave: return "`" case 54: return "," case 55: return "." case 56: return "/" case 58...69: return "F\(code - 57)" case 74: return "Home" case Key.pageUp: return "Page Up" case 76: return "Fwd Delete" case 77: return "End" case Key.pageDown: return "Page Down" case Key.right: return "→" case Key.left: return "←" case Key.down: return "↓" case Key.up: return "↑" default: return "Key \(code)" } } // MARK: Gamepad controls enum Pad { static let a = 1, b = 2, x = 3, y = 4, l1 = 5, r1 = 6, l2 = 7, r2 = 8, menu = 9, options = 10, l3 = 11, r3 = 12 static let up = 20, down = 21, left = 22, right = 23 static let lsLeft = 0x20001, lsRight = 0x20002, lsUp = 0x20003, lsDown = 0x20004 static let rsLeft = 0x20005, rsRight = 0x20006, rsUp = 0x20007, rsDown = 0x20008 } static let defaultGamepad: [GamepadChord: String] = { let plain: [(Int, String)] = [ (Pad.a, "playPause"), (Pad.b, "stop"), (Pad.x, "clear"), (Pad.y, "loop"), (Pad.l1, "prevTab"), (Pad.r1, "nextTab"), (Pad.up, "up"), (Pad.down, "down"), (Pad.left, "left"), (Pad.right, "right"), (Pad.lsUp, "up"), (Pad.lsDown, "down"), (Pad.lsLeft, "left"), (Pad.lsRight, "right"), (Pad.rsUp, "increment"), (Pad.rsDown, "decrement"), ] return Dictionary(uniqueKeysWithValues: plain.map { (GamepadChord(modifier: 0, code: $0.0), $0.1) }) }() static func padLabel(_ code: Int) -> String { switch code { case Pad.a: return "A" case Pad.b: return "B" case Pad.x: return "X" case Pad.y: return "Y" case Pad.l1: return "L1" case Pad.r1: return "R1" case Pad.l2: return "L2" case Pad.r2: return "R2" case Pad.menu: return "Menu" case Pad.options: return "Options" case Pad.l3: return "L3" case Pad.r3: return "R3" case Pad.up: return "UP" case Pad.down: return "DOWN" case Pad.left: return "LEFT" case Pad.right: return "RIGHT" case Pad.lsLeft: return "LS←" case Pad.lsRight: return "LS→" case Pad.lsUp: return "LS↑" case Pad.lsDown: return "LS↓" case Pad.rsLeft: return "RS←" case Pad.rsRight: return "RS→" case Pad.rsUp: return "RS↑" case Pad.rsDown: return "RS↓" default: return "#\(code)" } } static func chordLabel(_ c: GamepadChord) -> String { c.modifier == 0 ? padLabel(c.code) : padLabel(c.modifier) + " + " + padLabel(c.code) } }