Skip to content
Open
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
4 changes: 4 additions & 0 deletions OpenParsec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
17CD1E032BF07BC3003D2102 /* ViewContainerPatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17CD1E022BF07BC3003D2102 /* ViewContainerPatch.swift */; };
17EA35F72BF712A8001BE0DF /* ParsecSDKBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17EA35F62BF712A8001BE0DF /* ParsecSDKBridge.swift */; };
17EEB91C2BEE62BA00502A3A /* KeyBoardTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17EEB91B2BEE62BA00502A3A /* KeyBoardTest.swift */; };
A1B2C3D42F8E000100000012 /* ClipboardSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D42F8E000100000011 /* ClipboardSync.swift */; };
271D14F9292E90EB00D7F1D6 /* ParsecView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271D14F8292E90EB00D7F1D6 /* ParsecView.swift */; };
271D14FC292EAA3600D7F1D6 /* ParsecGLKRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271D14FA292EAA3600D7F1D6 /* ParsecGLKRenderer.swift */; };
271D14FD292EAA3600D7F1D6 /* ParsecGLKViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271D14FB292EAA3600D7F1D6 /* ParsecGLKViewController.swift */; };
Expand Down Expand Up @@ -65,6 +66,7 @@
17CD1E022BF07BC3003D2102 /* ViewContainerPatch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewContainerPatch.swift; sourceTree = "<group>"; };
17EA35F62BF712A8001BE0DF /* ParsecSDKBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParsecSDKBridge.swift; sourceTree = "<group>"; };
17EEB91B2BEE62BA00502A3A /* KeyBoardTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyBoardTest.swift; sourceTree = "<group>"; };
A1B2C3D42F8E000100000011 /* ClipboardSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClipboardSync.swift; sourceTree = "<group>"; };
271D14F8292E90EB00D7F1D6 /* ParsecView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsecView.swift; sourceTree = "<group>"; };
271D14FA292EAA3600D7F1D6 /* ParsecGLKRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParsecGLKRenderer.swift; sourceTree = "<group>"; };
271D14FB292EAA3600D7F1D6 /* ParsecGLKViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParsecGLKViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,6 +166,7 @@
271D14FB292EAA3600D7F1D6 /* ParsecGLKViewController.swift */,
27B23A212B1B979C00B52F14 /* ParsecMetalRenderer.swift */,
27B23A232B1B98EF00B52F14 /* ParsecMetalViewController.swift */,
A1B2C3D42F8E000100000011 /* ClipboardSync.swift */,
17EA35F62BF712A8001BE0DF /* ParsecSDKBridge.swift */,
271D14F8292E90EB00D7F1D6 /* ParsecView.swift */,
170E0A762BEF5AFF00B0EA32 /* ParsecViewController.swift */,
Expand Down Expand Up @@ -305,6 +308,7 @@
27899AC7292FE2A9001ACA33 /* audio.c in Sources */,
27E61AA62929817700FF6563 /* LoginView.swift in Sources */,
27B8D564292DC7A000A324AD /* CParsec.swift in Sources */,
A1B2C3D42F8E000100000012 /* ClipboardSync.swift in Sources */,
27A923E229E8FEE900F54BDA /* UIViewControllerWrapper.swift in Sources */,
170E0A772BEF5AFF00B0EA32 /* ParsecViewController.swift in Sources */,
27B23A222B1B979C00B52F14 /* ParsecMetalRenderer.swift in Sources */,
Expand Down
61 changes: 61 additions & 0 deletions OpenParsec/ClipboardSync.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import UIKit
import ParsecSDK
import os

// Mirrors text between UIPasteboard and the host over user data ID 7, which the stock host
// handles in both directions when its app_copy_paste setting allows it.
final class ClipboardSync {
static let shared = ClipboardSync()

// The SDK warns that its message channel is not meant for bulk data.
private let maxTextBytes = 1 << 20
private let pollInterval: TimeInterval = 1
private var timer: Timer?

// Persisted so a copy made before launching the app still reaches the host, while an
// unchanged pasteboard does not overwrite the host clipboard on every connection.
private var lastChangeCount = UserDefaults.standard.integer(forKey: "lastClipboardChangeCount") {
didSet { UserDefaults.standard.set(lastChangeCount, forKey: "lastClipboardChangeCount") }
}

func start() {
stop()
// Polling because UIPasteboard.changedNotification misses copies made in other apps.
timer = Timer.scheduledTimer(withTimeInterval: pollInterval, repeats: true) { [weak self] _ in
self?.sendIfChanged()
}
sendIfChanged()
}

func stop() {
timer?.invalidate()
timer = nil
}

func receive(_ text: String) {
// Off by default because the host would otherwise overwrite the local clipboard.
guard SettingsHandler.clipboardSync else { return }
DispatchQueue.main.async {
UIPasteboard.general.string = text
self.lastChangeCount = UIPasteboard.general.changeCount
}
}

private func sendIfChanged() {
let pasteboard = UIPasteboard.general
guard pasteboard.changeCount != lastChangeCount,
UIApplication.shared.applicationState == .active,
CParsec.getStatus() == PARSEC_OK else { return }

// Mark it seen before reading, so a declined paste prompt is not repeated every poll.
lastChangeCount = pasteboard.changeCount
guard pasteboard.hasStrings, let text = pasteboard.string else { return }

let data = Data(text.utf8)
guard data.count <= maxTextBytes else {
os_log("%{public}@", log: sdkLog, "[clipboard] skipped \(data.count) bytes")
return
}
CParsec.sendUserData(type: .clipboard, message: data)
}
}
2 changes: 2 additions & 0 deletions OpenParsec/ParsecSDKBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ class ParsecSDKBridge: ParsecService {
return
}
switch event.id {
case ParsecUserDataType.clipboard.rawValue:
ClipboardSync.shared.receive(String(cString: pointer!.assumingMemoryBound(to: CChar.self)))
case 11:
do {
let decoder = JSONDecoder()
Expand Down
1 change: 1 addition & 0 deletions OpenParsec/ParsecUserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct ParsecDisplayConfig: Codable, Hashable {
}

enum ParsecUserDataType: UInt32 {
case clipboard = 7
case getVideoConfig = 9
case getAdapterInfo = 10
case setVideoConfig = 11
Expand Down
4 changes: 4 additions & 0 deletions OpenParsec/ParsecViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,16 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI
becomeFirstResponder()
}
// (Pinch-to-zoom is driven manually via touchOverlay; the scroll view's pinch stays off.)
if SettingsHandler.clipboardSync {
ClipboardSync.shared.start()
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isIdleTimerDisabled = false
stopMomentum()
ClipboardSync.shared.stop()
if let parent = parent {
parent.setChildForHomeIndicatorAutoHidden(nil)
parent.setChildViewControllerForPointerLock(nil)
Expand Down
7 changes: 7 additions & 0 deletions OpenParsec/SettingsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class SettingsStore: ObservableObject {
"alwaysShowStatus": false,
"enablePiP": false,
"startMuted": false,
"clipboardSync": false,
"autoReconnectEnabled": true,
"errorPrompts": false,
"showConnectionLog": true,
Expand Down Expand Up @@ -141,6 +142,11 @@ final class SettingsStore: ObservableObject {
set { store(newValue, "startMuted") }
}

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

var autoReconnect: Bool {
get { defaults.bool(forKey: "autoReconnectEnabled") }
set { store(newValue, "autoReconnectEnabled") }
Expand Down Expand Up @@ -192,6 +198,7 @@ struct SettingsHandler {
static var alwaysShowStatus: Bool { get { SettingsStore.shared.alwaysShowStatus } set { SettingsStore.shared.alwaysShowStatus = newValue } }
static var enablePiP: Bool { get { SettingsStore.shared.enablePiP } set { SettingsStore.shared.enablePiP = newValue } }
static var startMuted: Bool { get { SettingsStore.shared.startMuted } set { SettingsStore.shared.startMuted = newValue } }
static var clipboardSync: Bool { get { SettingsStore.shared.clipboardSync } set { SettingsStore.shared.clipboardSync = newValue } }
static var autoReconnect: Bool { get { SettingsStore.shared.autoReconnect } set { SettingsStore.shared.autoReconnect = newValue } }
static var errorPrompts: Bool { get { SettingsStore.shared.errorPrompts } set { SettingsStore.shared.errorPrompts = newValue } }
static var showConnectionLog: Bool { get { SettingsStore.shared.showConnectionLog } set { SettingsStore.shared.showConnectionLog = newValue } }
Expand Down
4 changes: 4 additions & 0 deletions OpenParsec/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ struct SettingsView: View {
Toggle("", isOn: $settings.startMuted)
.frame(width: 80)
}
CatItem("Sync Clipboard") {
Toggle("", isOn: $settings.clipboardSync)
.frame(width: 80)
}
}
CatTitle("Developer")
CatList {
Expand Down
Loading