// SPDX-License-Identifier: GPL-3.0-or-later import Foundation // Layout reference: keyboardLayout.cpp, copyright 2015 Joseph Forgione. // Matrix positions follow the retained keyboardLayout.cpp. Host key codes are // used for physical CoCo positions and nonprinting keys. Natural/Compact // printable characters follow the current Mac layout, translated to CoCo chords. enum KeyboardMode: String, Codable, CaseIterable { case natural = "Natural", coco = "CoCo", compact = "Compact" } struct CocoKeyboard { var mode: KeyboardMode = .natural { didSet { if mode != oldValue { releaseAll() } } } static let shift = UInt64(1) << 55 static let control = UInt64(1) << 52 static let alt = UInt64(1) << 51 private var held: [UInt16: UInt64] = [:] private var shiftSensitive: Set = [] private var shifted = false, controlled = false, option = false var mask: UInt64 { let chords = held.values.reduce(0, |) // Natural punctuation can differ from the Mac's physical Shift state. let shiftAlone = held.isEmpty && shifted ? Self.shift : 0 return chords | shiftAlone | (controlled ? Self.control : 0) | (option ? Self.alt : 0) } mutating func modifiers(shift: Bool, control: Bool, alt: Bool) { shifted = shift; controlled = control; option = alt for code in shiftSensitive { if let chord = held[code] { held[code] = (chord & ~Self.shift) | (shift ? Self.shift : 0) } } } mutating func press(code: UInt16, characters: String) { // Ignore host repeat; the guest implements repeat from held keys. guard held[code] == nil else { return } if code == 57 { held[code] = UInt64(1) << 32 | (mode == .coco ? Self.shift : Self.control) return } if mode == .compact && (code == 48 || (code == 50 && !shifted)) { held[code] = UInt64(1) << (code == 48 ? 49 : 50) | (shifted ? Self.shift : 0) shiftSensitive.insert(code); return } if mode == .coco { // Physical US CoCo positions, independent of the host layout. let physical: [UInt16: Int] = [0:1,11:2,8:3,2:4,14:5,3:6,5:7,4:8, 34:9,38:10,40:11,37:12,46:13,45:14,31:15,35:16,12:17,15:18, 1:19,17:20,32:21,9:22,13:23,7:24,16:25,6:26,49:31, 29:32,18:33,19:34,20:35,21:36,23:37,22:38,26:39,28:40,25:41, 27:42,41:43,43:44,24:45,47:46,44:47,50:0] if let bit = physical[code] { held[code] = UInt64(1) << bit | (shifted ? Self.shift : 0) shiftSensitive.insert(code); return } } let navigation: [UInt16: UInt64] = [119: UInt64(1) << 30 | Self.shift, 83: UInt64(1) << 30 | Self.shift, 117: UInt64(1) << 29 | Self.control, 65: UInt64(1) << 29 | Self.control, 114: UInt64(1) << 30 | Self.control, 82: UInt64(1) << 30 | Self.control, 116: UInt64(1) << 27 | Self.shift, 92: UInt64(1) << 27 | Self.shift, 121: UInt64(1) << 28 | Self.shift, 85: UInt64(1) << 28 | Self.shift] if let chord = navigation[code] { held[code] = mode == .coco && (code == 119 || code == 83) ? UInt64(1) << 50 : chord return } let special: [UInt16: Int] = [36: 48, 76: 48, 53: 50, 51: 29, 123: 29, 124: 30, 125: 28, 126: 27, 115: 49, 122: 53, 120: 54, 111: 50, 89: 49, 91: 27, 84: 28, 86: 29, 88: 30] if let bit = special[code] { held[code] = UInt64(1) << bit | (shifted ? Self.shift : 0) shiftSensitive.insert(code) } else if let chord = Self.chord(for: characters) { held[code] = chord if let c = characters.utf8.first, (65...90).contains(c) || (97...122).contains(c) { held[code] = (chord & ~Self.shift) | (shifted ? Self.shift : 0) shiftSensitive.insert(code) } } } mutating func release(code: UInt16) { held.removeValue(forKey: code); shiftSensitive.remove(code) } mutating func releaseAll() { held.removeAll(); shiftSensitive.removeAll(); shifted = false; controlled = false; option = false } static func chord(for text: String) -> UInt64? { guard text.utf8.count == 1, let c = text.utf8.first else { return nil } // BASIC defaults to uppercase; physical letters are unshifted unless // the Mac event is uppercase. Shifted letters remain available to games/OS-9. if (65...90).contains(c) { return UInt64(1) << (c - 64) | shift } if (97...122).contains(c) { return UInt64(1) << (c - 96) } if c == 64 { return 1 } if c == 32 { return UInt64(1) << 31 } if (48...55).contains(c) { return UInt64(1) << (32 + c - 48) } if (56...57).contains(c) { return UInt64(1) << (40 + c - 56) } let base = Array(":;,-./".utf8) if let index = base.firstIndex(of: c) { return UInt64(1) << (42 + index) } let shifted = Array("!\"#$%&'()*+<=>?".utf8) let positions = [33,34,35,36,37,38,39,40,41,42,43,44,45,46,47] if let index = shifted.firstIndex(of: c) { return UInt64(1) << positions[index] | shift } // Extended CoCo characters use Control combinations in the natural map. let extended: [UInt8: Int] = [91: 40, 93: 41, 92: 47, 94: 39, 95: 45, 123: 44, 125: 46, 124: 33, 126: 35] if let bit = extended[c] { return UInt64(1) << bit | control } return nil } } // Clipboard input is translated independently of the selected physical keymap // and joystick bindings. Validate the entire payload before sending any keys. struct CocoPaste { enum Mode { case text, mergeBASIC, newBASIC } struct Failure: LocalizedError { let message: String var errorDescription: String? { message } } let chords: [UInt64] init(_ source: String, mode: Mode) throws { guard source.utf8.count <= 65536 else { throw Failure(message: "Paste is limited to 64 KiB of text.") } let normalized = source.replacingOccurrences(of: "\r\n", with: "\n") .replacingOccurrences(of: "\n\r", with: "\n").replacingOccurrences(of: "\r", with: "\n") .replacingOccurrences(of: "\t", with: " ") var text = normalized if mode != .text { var lines: [String] = [] for raw in normalized.split(separator: "\n", omittingEmptySubsequences: false) { let line = raw.trimmingCharacters(in: .whitespaces) if line.isEmpty { continue } let digits = line.prefix(while: { $0 >= "0" && $0 <= "9" }) guard let number = Int(digits), number <= 63999 else { throw Failure(message: "Every BASIC line must begin with a line number from 0 to 63999. Paste commands with Paste Text instead.") } guard line.utf8.count <= 249 else { throw Failure(message: "BASIC line \(number) exceeds the safe 249-character paste limit. Shorten it before pasting; nothing has been sent.") } lines.append(line + "\n") } guard !lines.isEmpty else { throw Failure(message: "The clipboard contains no numbered BASIC lines.") } text = (mode == .newBASIC ? "NEW\n" : "") + lines.joined() } var keys: [UInt64] = [] for character in text { if character == "\n" { keys.append(UInt64(1) << 48) } else if let chord = CocoKeyboard.chord(for: String(character)) { keys.append(chord) } else { throw Failure(message: "The CoCo keyboard cannot type “\(character)”. Replace that character before pasting; nothing has been sent.") } } guard !keys.isEmpty, keys.count <= 65536 else { throw Failure(message: "Paste must contain 1 to 65536 keystrokes.") } chords = keys } }