Skip to content
Open
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
21 changes: 15 additions & 6 deletions OpenParsec/ParsecSDKBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class ParsecSDKBridge: ParsecService {
public var clientWidth: Float = 1920
public var clientHeight: Float = 1080
private var hasFrame = false
private var didSetDimensions = false

public var netProtocol: Int32 = 1
public var mediaContainer: Int32 = 0
Expand All @@ -135,6 +136,8 @@ class ParsecSDKBridge: ParsecService {
private var capsLock = false

public var mouseInfo = MouseInfo()
// when we last told the host where to put the cursor
private var lastMoveSent: CFTimeInterval = 0

init() {
os_log("%{public}@", log: sdkLog, "Parsec SDK Version: " + String(ParsecSDKBridge.PARSEC_VER))
Expand Down Expand Up @@ -200,6 +203,7 @@ class ParsecSDKBridge: ParsecService {

self.startBackgroundTask()
didReleaseOnConnect = false
didSetDimensions = false

let status = ParsecClientConnect(_parsec, &parsecClientCfg, NetworkHandler.clinfo?.session_id, peerID)

Expand Down Expand Up @@ -285,6 +289,7 @@ class ParsecSDKBridge: ParsecService {
func setFrame(_ width: CGFloat, _ height: CGFloat, _ scale: CGFloat) {

ParsecClientSetDimensions(_parsec, UInt8(DEFAULT_STREAM), UInt32(width), UInt32(height), Float(scale))
didSetDimensions = true

let oldSize = CGSize(width: CGFloat(clientWidth), height: CGFloat(clientHeight))
clientWidth = Float(width)
Expand Down Expand Up @@ -417,10 +422,18 @@ class ParsecSDKBridge: ParsecService {
}

func handleCursorEvent(event: ParsecClientCursorEvent, sawCursor: inout Bool) {
let prevHidden = mouseInfo.cursorHidden
mouseInfo.cursorHidden = event.cursor.hidden
mouseInfo.mousePositionRelative = event.cursor.relative

// cursor events are the only time the host tells us where its cursor is, so follow it, a mouse
// on the host or another guest may have moved it. an event right after our own move is just
// that move coming back, and before setFrame the sdk has no dims to scale the position with
let isEcho = CACurrentMediaTime() - lastMoveSent < 0.3
if didSetDimensions && !isEcho && !event.cursor.relative && !event.cursor.hidden {
mouseInfo.mouseX = ParsecSDKBridge.clamp(Int32(clamping: event.cursor.positionX), minValue: 0, maxValue: Int32(clientWidth))
mouseInfo.mouseY = ParsecSDKBridge.clamp(Int32(clamping: event.cursor.positionY), minValue: 0, maxValue: Int32(clientHeight))
}

if event.cursor.imageUpdate || !sawCursor {
sawCursor = true
let imgKey = event.key
Expand All @@ -438,11 +451,6 @@ class ParsecSDKBridge: ParsecService {
mouseInfo.cursorWidth = Int(width)
mouseInfo.cursorHeight = Int(height)

if prevHidden && !event.cursor.hidden {
mouseInfo.mouseX = Int32(event.cursor.positionX)
mouseInfo.mouseY = Int32(event.cursor.positionY)
}

mouseInfo.cursorHotX = Int(event.cursor.hotX)
mouseInfo.cursorHotY = Int(event.cursor.hotY)

Expand Down Expand Up @@ -517,6 +525,7 @@ class ParsecSDKBridge: ParsecService {
}

func sendMousePosition(_ x: Int32, _ y: Int32) {
lastMoveSent = CACurrentMediaTime()
mouseInfo.mouseX = ParsecSDKBridge.clamp(x, minValue: 0, maxValue: Int32(self.clientWidth))
mouseInfo.mouseY = ParsecSDKBridge.clamp(y, minValue: 0, maxValue: Int32(self.clientHeight))
var motionMessage = ParsecMessage()
Expand Down
Loading