Files
ReactorcoremeltdownandClaude Opus 5.5 64e21e0bcc iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder
- CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports;
  platform-free parser/module/clock in core (midi_test)
- Hardware keyboard and gamepads via GameController; rebindable keyboard,
  gamepad and MIDI CC actions; per-parameter MIDI learn
- Colour themes (.szt), project bundles (.szgz), backup & restore;
  ZIP in core with zlib (zip_test against jar and unzip)
- Persistent app settings; audio pauses in the background unless sound module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:26:01 +01:00

213 lines
8.6 KiB
Swift

// 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[..<eq]) else { continue }
out[code] = String(line[line.index(after: eq)...])
}
return out
}
private static func decodeChord(_ s: String) -> [GamepadChord: String] {
var out: [GamepadChord: String] = [:]
for line in s.split(separator: "\n") {
guard let eq = line.firstIndex(of: "=") else { continue }
let parts = line[..<eq].split(separator: ":")
guard parts.count == 2, let m = Int(parts[0]), let c = Int(parts[1]) else { continue }
out[GamepadChord(modifier: m, code: c)] = String(line[line.index(after: eq)...])
}
return out
}
// MARK: Keyboard (HID usages)
enum Key {
static let a = 4, l = 15, enter = 40, backspace = 42, tab = 43, space = 44, minus = 45, equals = 46, grave = 53
static let pageUp = 75, pageDown = 78, right = 79, left = 80, down = 81, up = 82
static let leftShift = 225, rightShift = 229
}
/// Android's default hotkeys, kept off the piano letters.
static let defaultKeyboard: [Int: String] = [
Key.up: "up", Key.down: "down", Key.left: "left", Key.right: "right", Key.tab: "nextColumn",
Key.space: "playPause", Key.enter: "stop", Key.l: "loop", Key.pageUp: "increment", Key.pageDown: "decrement",
Key.backspace: "clear", Key.grave: "noteOff", Key.minus: "octaveDown", Key.equals: "octaveUp",
]
/// The fixed two-octave tracker piano: Z S X D C V G B H N J M, then Q 2 W 3 E R 5 T 6 Y 7 U.
static let piano: [Int: Int] = {
let lower = "zsxdcvgbhnjm", upper = "q2w3er5t6y7u"
var m: [Int: Int] = [:]
for (i, c) in lower.enumerated() { m[hid(c)] = i }
for (i, c) in upper.enumerated() { m[hid(c)] = 12 + i }
return m
}()
private static func hid(_ c: Character) -> 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)
}
}