Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/keyboard-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Keyboard event tests

on:
push:
paths:
- 'OpenParsec/KoreanKeyboardRouter.swift'
- 'Tests/**'
- '.github/workflows/keyboard-tests.yml'
pull_request:
paths:
- 'OpenParsec/KoreanKeyboardRouter.swift'
- 'Tests/**'
- '.github/workflows/keyboard-tests.yml'
workflow_dispatch:

permissions:
contents: read

jobs:
keyboard-tests:
runs-on: macos-15
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: Test keyboard event sequences
run: |
swiftc OpenParsec/KoreanKeyboardRouter.swift Tests/main.swift -o "$RUNNER_TEMP/keyboard-tests"
"$RUNNER_TEMP/keyboard-tests"
4 changes: 4 additions & 0 deletions OpenParsec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
A11000000000000000000001 /* KoreanKeyboardRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A11000000000000000000002 /* KoreanKeyboardRouter.swift */; };
170E0A772BEF5AFF00B0EA32 /* ParsecViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170E0A762BEF5AFF00B0EA32 /* ParsecViewController.swift */; };
17374D412CB8E35D00EDCEE7 /* KeyCodeTranslators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */; };
17840D1D2CB639530097374B /* ParsecUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17840D1C2CB6394D0097374B /* ParsecUserData.swift */; };
Expand Down Expand Up @@ -59,6 +60,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
A11000000000000000000002 /* KoreanKeyboardRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KoreanKeyboardRouter.swift; sourceTree = "<group>"; };
170E0A762BEF5AFF00B0EA32 /* ParsecViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsecViewController.swift; sourceTree = "<group>"; };
17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyCodeTranslators.swift; sourceTree = "<group>"; };
17840D1C2CB6394D0097374B /* ParsecUserData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsecUserData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -140,6 +142,7 @@
27E61A90292965FC00FF6563 /* OpenParsec */ = {
isa = PBXGroup;
children = (
A11000000000000000000002 /* KoreanKeyboardRouter.swift */,
E76224A02F8C017700A2F86F /* ParsecBackgroundManager.swift */,
E762250A2F8D0A0000A2F86F /* PictureInPictureManager.swift */,
17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */,
Expand Down Expand Up @@ -285,6 +288,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A11000000000000000000001 /* KoreanKeyboardRouter.swift in Sources */,
FC16A7AD29A97BDA00BB70A7 /* Shared.swift in Sources */,
84480EBE2ADC4FDA007DE5F1 /* GameController.swift in Sources */,
27AC751829EA339B00E8CAF7 /* URLImage.swift in Sources */,
Expand Down
71 changes: 71 additions & 0 deletions OpenParsec/KoreanKeyboardRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// USB HID usages, matching Parsec's physical key codes. Kept independent of
// UIKit/GameController so event sequences can be tested on the build machine.
struct KoreanKeyboardRouter {
struct Event: Equatable {
let code: Int
let pressed: Bool
}

private var held: Set<Int> = []
private var forwarded: Set<Int> = []
private var consumed: Set<Int> = []
private(set) var repeatKey: Int?

mutating func handle(code: Int, pressed: Bool, mapBacktick: Bool = false,
spaceShortcut: Bool = false) -> [Event] {
guard code > 3 && code < 65535 else { return [] }
if !pressed {
held.remove(code)
if repeatKey == code { repeatKey = nil }
if consumed.remove(code) != nil { return [] }
guard forwarded.remove(code) != nil else { return [] }
return [Event(code: code, pressed: false)]
}
// A held language key must toggle once, never at the repeat rate.
guard held.insert(code).inserted else { return [] }
let modifiers = held.subtracting(consumed).filter { (224...231).contains($0) }
let shiftOnly = !modifiers.isEmpty && modifiers.allSatisfy { $0 == 225 || $0 == 229 }
let controlOnly = !modifiers.isEmpty && modifiers.allSatisfy { $0 == 224 || $0 == 228 }
// UIKit sometimes supplies only flags, without a modifier key-down.
// When we do know the physical state, prefer it: a consumed Ctrl/Shift
// may remain in those flags until its delayed key-up arrives.
let shortcutFromFlags = spaceShortcut && !held.contains(where: { (224...231).contains($0) })
let languageKey = code == 144 || code == 230 || (mapBacktick && code == 53)
if languageKey || (code == 44 && (shortcutFromFlags || shiftOnly || controlOnly)) {
consumed.insert(code)
return toggle(consumeModifiers: true)
}
forwarded.insert(code)
// Lock keys and language keys must not acquire the software repeat timer.
if !(224...231).contains(code) && ![57, 71, 83, 145].contains(code) {
repeatKey = code
}
return [Event(code: code, pressed: true)]
}

mutating func toggle(consumeModifiers: Bool = false) -> [Event] {
repeatKey = nil
let modifiers = forwarded.filter { (224...231).contains($0) }.sorted()
if consumeModifiers {
// Bluetooth language keys can emit Ctrl+Space with a delayed Ctrl-up.
// End the whole shortcut now; ignore those modifiers until real key-up.
// Re-pressing Ctrl/Shift starts an ordinary shortcut immediately.
for code in modifiers {
forwarded.remove(code)
consumed.insert(code)
}
}
return modifiers.map { Event(code: $0, pressed: false) }
+ [Event(code: 230, pressed: true), Event(code: 230, pressed: false)]
+ (consumeModifiers ? [] : modifiers.map { Event(code: $0, pressed: true) })
}

mutating func reset() -> [Event] {
let releases = forwarded.sorted().map { Event(code: $0, pressed: false) }
held.removeAll()
forwarded.removeAll()
consumed.removeAll()
repeatKey = nil
return releases
}
}
20 changes: 20 additions & 0 deletions OpenParsec/ParsecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ struct ParsecView: View {

@State var hideOverlay: Bool = false
@State var hideKeyboardButton: Bool = false
@State var koreanKeyboard: Bool = false
@AppStorage("keyboardHintShown") var keyboardHintShown: Bool = false
@AppStorage("keyboardPanHintShown") var keyboardPanHintShown: Bool = false
@State var hint: String?
Expand Down Expand Up @@ -451,6 +452,22 @@ struct ParsecView: View {
Spacer()
}
}
if koreanKeyboard {
HStack {
Button(action: { parsecViewController.toggleKoreanInput() }) {
Text("한/영")
.font(.system(size: 16, weight: .semibold))
.foregroundColor(Color("Foreground"))
.padding(10)
.background(Rectangle().fill(Color("BackgroundPrompt").opacity(0.5)))
.cornerRadius(8)
}
.accessibilityLabel("Toggle Korean input on Windows")
.padding(.leading)
.edgesIgnoringSafeArea(.all)
Spacer()
}
}
if showMenu && verticalSizeClass != .compact {
HStack {
menuPanel
Expand Down Expand Up @@ -727,6 +744,7 @@ struct ParsecView: View {

hideOverlay = SettingsHandler.noOverlay
hideKeyboardButton = !SettingsHandler.showKeyboardButton
koreanKeyboard = SettingsHandler.koreanKeyboard
parsecViewController.updateMenuEdgeSwipe(menuHidden: hideOverlay)
offerHint()

Expand Down Expand Up @@ -776,6 +794,8 @@ struct ParsecView: View {
parsecViewController.positionCursorOverlay()
hideOverlay = SettingsHandler.noOverlay
hideKeyboardButton = !SettingsHandler.showKeyboardButton
koreanKeyboard = SettingsHandler.koreanKeyboard
parsecViewController.applyKoreanKeyboardSetting()
parsecViewController.updateMenuEdgeSwipe(menuHidden: hideOverlay)
offerHint()

Expand Down
104 changes: 103 additions & 1 deletion OpenParsec/ParsecViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit
import ParsecSDK
import QuartzCore
import os
import GameController

protocol ParsecPlayground {
init(viewController: UIViewController, updateImage: @escaping () -> Void)
Expand Down Expand Up @@ -84,6 +85,10 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
private var panStartOffset: CGPoint = .zero
private var keyboardCoversPicture = false
var onKeyboardVisibilityChanged: ((Bool) -> Void)?
private var koreanKeyboard = KoreanKeyboardRouter()
private var hardwareKeyboard: GCKeyboard?
private var keyboardObservers: [NSObjectProtocol] = []
private var lastHardwareKeyTime: TimeInterval = 0
var onMenuEdgeSwipe: (() -> Void)?
var menuEdgeSwipe: UIScreenEdgePanGestureRecognizer?
var menuEdgeSwipeRight: UIScreenEdgePanGestureRecognizer?
Expand Down Expand Up @@ -385,6 +390,7 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.isIdleTimerDisabled = true
applyKoreanKeyboardSetting()
if let parent = parent {
parent.setChildForHomeIndicatorAutoHidden(self)
parent.setChildViewControllerForPointerLock(self)
Expand All @@ -398,6 +404,7 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isIdleTimerDisabled = false
stopHardwareKeyboard()
stopMomentum()
if let parent = parent {
parent.setChildForHomeIndicatorAutoHidden(nil)
Expand All @@ -414,7 +421,84 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
private var optCmdRemapActive = false
private var altKeyHeld = false

// Called on appear and when stream settings close, so the toggle applies without reconnecting.
func applyKoreanKeyboardSetting() {
if SettingsHandler.koreanKeyboard { startHardwareKeyboard() } else { stopHardwareKeyboard() }
}

// GameController reports physical keys independently of the iPad text layout.
// While it is attached, UIKit must not forward the same hardware events again.
private func startHardwareKeyboard() {
guard SettingsHandler.koreanKeyboard, keyboardObservers.isEmpty else { return }
for name in [Notification.Name.GCKeyboardDidConnect, .GCKeyboardDidDisconnect] {
keyboardObservers.append(NotificationCenter.default.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in
MainActor.assumeIsolated {
guard let self = self else { return }
self.attachHardwareKeyboard()
}
})
}
attachHardwareKeyboard()
}

private func attachHardwareKeyboard() {
hardwareKeyboard?.keyboardInput?.keyChangedHandler = nil
resetKeyState()
hardwareKeyboard = GCKeyboard.coalesced
hardwareKeyboard?.handlerQueue = .main
hardwareKeyboard?.keyboardInput?.keyChangedHandler = { [weak self] _, _, key, pressed in
MainActor.assumeIsolated {
guard let self = self, UIApplication.shared.applicationState == .active,
ParsecBackgroundManager.shared.hasActiveConnection else { return }
self.lastHardwareKeyTime = ProcessInfo.processInfo.systemUptime
self.handleKoreanKey(code: Int(key.rawValue), pressed: pressed)
}
}
}

private func stopHardwareKeyboard() {
let wasActive = hardwareKeyboard != nil || !keyboardObservers.isEmpty
hardwareKeyboard?.keyboardInput?.keyChangedHandler = nil
hardwareKeyboard = nil
keyboardObservers.forEach { NotificationCenter.default.removeObserver($0) }
keyboardObservers.removeAll()
if wasActive { resetKeyState() }
}

private func sendKoreanEvents(_ events: [KoreanKeyboardRouter.Event]) {
for event in events {
CParsec.sendKeyboardMessage(keyCode: UInt32(event.code), pressed: event.pressed)
}
let nextRepeat = koreanKeyboard.repeatKey ?? -1
if nextRepeat != repeatKeyCode {
if nextRepeat >= 0 { startKeyRepeat(keyCode: nextRepeat) }
else { stopKeyRepeat() }
}
}

private func handleKoreanKey(code: Int, pressed: Bool, spaceShortcut: Bool = false) {
let events = koreanKeyboard.handle(code: code, pressed: pressed,
mapBacktick: SettingsHandler.koreanBacktick, spaceShortcut: spaceShortcut)
sendKoreanEvents(events)
}

func toggleKoreanInput() {
guard ParsecBackgroundManager.shared.hasActiveConnection else { return }
sendKoreanEvents(koreanKeyboard.toggle())
}

override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
if SettingsHandler.koreanKeyboard {
guard hardwareKeyboard?.keyboardInput == nil else { return }
// UIKit fallback: process modifiers first if several keys arrive together.
let keys = presses.compactMap { $0.key }.sorted { isModifierKey($0.keyCode) && !isModifierKey($1.keyCode) }
for key in keys {
let flags = key.modifierFlags.intersection([.shift, .control, .alternate, .command])
handleKoreanKey(code: key.keyCode.rawValue, pressed: true,
spaceShortcut: flags == .shift || flags == .control)
}
return
}
for press in presses {
guard let key = press.key else { continue }

Expand Down Expand Up @@ -444,6 +528,13 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
}

override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
if SettingsHandler.koreanKeyboard {
guard hardwareKeyboard?.keyboardInput == nil else { return }
for press in presses {
if let key = press.key { handleKoreanKey(code: key.keyCode.rawValue, pressed: false) }
}
return
}
for press in presses {
guard let key = press.key else { continue }

Expand Down Expand Up @@ -507,6 +598,9 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
}

func resetKeyState() {
for event in koreanKeyboard.reset() {
CParsec.sendKeyboardMessage(keyCode: UInt32(event.code), pressed: false)
}
stopKeyRepeat()
optCmdRemapActive = false
altKeyHeld = false
Expand Down Expand Up @@ -789,7 +883,11 @@ extension ParsecViewController: UIGestureRecognizerDelegate {
}
}

@objc private func appWillResignActive() { flushAllTouches() }
@objc private func appWillResignActive() {
flushAllTouches()
// Hardware key-ups are dropped while inactive, so release routed keys now.
if SettingsHandler.koreanKeyboard { resetKeyState() }
}

// Drop all touch state and release any held button so nothing sticks when backgrounded.
private func flushAllTouches() {
Expand Down Expand Up @@ -1331,10 +1429,14 @@ extension ParsecViewController: UIKeyInput, UITextInputTraits {
}

func insertText(_ text: String) {
// Ignore text synthesized from an already-forwarded physical key. Software
// keyboard input still works when the hardware keyboard is idle.
if hardwareKeyboard != nil && ProcessInfo.processInfo.systemUptime - lastHardwareKeyTime < 0.15 { return }
CParsec.sendVirtualKeyboardInput(text: text)
}

func deleteBackward() {
if hardwareKeyboard != nil && ProcessInfo.processInfo.systemUptime - lastHardwareKeyTime < 0.15 { return }
CParsec.sendVirtualKeyboardInput(text: "BACKSPACE")
}

Expand Down
14 changes: 14 additions & 0 deletions OpenParsec/SettingsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class SettingsStore: ObservableObject {
"cursorScale": 0.5,
"mouseSensitivity": 1.0,
"optionAsCommand": false,
"koreanKeyboard": false,
"koreanBacktick": false,
"menuSwipeEdge": MenuSwipeEdge.off.rawValue,
"noOverlay": false,
"hideStatusBar": true,
Expand Down Expand Up @@ -86,6 +88,16 @@ final class SettingsStore: ObservableObject {
set { store(newValue, "optionAsCommand") }
}

var koreanKeyboard: Bool {
get { defaults.bool(forKey: "koreanKeyboard") }
set { store(newValue, "koreanKeyboard") }
}

var koreanBacktick: Bool {
get { defaults.bool(forKey: "koreanBacktick") }
set { store(newValue, "koreanBacktick") }
}

var menuSwipeEdge: MenuSwipeEdge {
get { MenuSwipeEdge(rawValue: defaults.integer(forKey: "menuSwipeEdge")) ?? .off }
set { store(newValue.rawValue, "menuSwipeEdge") }
Expand Down Expand Up @@ -181,6 +193,8 @@ struct SettingsHandler {
static var cursorScale: Double { get { SettingsStore.shared.cursorScale } set { SettingsStore.shared.cursorScale = newValue } }
static var mouseSensitivity: Double { get { SettingsStore.shared.mouseSensitivity } set { SettingsStore.shared.mouseSensitivity = newValue } }
static var optionAsCommand: Bool { get { SettingsStore.shared.optionAsCommand } set { SettingsStore.shared.optionAsCommand = newValue } }
static var koreanKeyboard: Bool { get { SettingsStore.shared.koreanKeyboard } set { SettingsStore.shared.koreanKeyboard = newValue } }
static var koreanBacktick: Bool { get { SettingsStore.shared.koreanBacktick } set { SettingsStore.shared.koreanBacktick = newValue } }
static var menuSwipeEdge: MenuSwipeEdge { get { SettingsStore.shared.menuSwipeEdge } set { SettingsStore.shared.menuSwipeEdge = newValue } }
static var noOverlay: Bool { get { SettingsStore.shared.noOverlay } set { SettingsStore.shared.noOverlay = newValue } }
static var hideStatusBar: Bool { get { SettingsStore.shared.hideStatusBar } set { SettingsStore.shared.hideStatusBar = newValue } }
Expand Down
Loading