Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS DisplayLink improvements #1085

Merged
merged 3 commits into from
Nov 3, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -600,22 +600,24 @@ class MyDefaultGameWindow : GameWindow() {
val arena = Arena()
val displayLink = arena.alloc<CVDisplayLinkRefVar>()

private fun checkDisplayLink(code: Int) {
if (code != kCVReturnSuccess) internalException(code)
}

fun createDisplayLink(): Boolean {
//println("createDisplayLink[1]")
val displayID = CGMainDisplayID()
val error = CVDisplayLinkCreateWithCGDisplay(displayID, displayLink.ptr)
//println("createDisplayLink[2]")
if (error == kCVReturnSuccess) {
//println("createDisplayLink[3]")

CVDisplayLinkSetOutputCallback(displayLink.value, staticCFunction(::displayCallback), gameWindowStableRef.asCPointer())
CVDisplayLinkStart(displayLink.value)
//println("createDisplayLink[4]")
return true
} else {
return false
return try {
checkDisplayLink(CVDisplayLinkCreateWithCGDisplay(CGMainDisplayID(), displayLink.ptr))
checkDisplayLink(CVDisplayLinkSetOutputCallback(displayLink.value, staticCFunction(::displayCallback), gameWindowStableRef.asCPointer()))
checkDisplayLink(CVDisplayLinkStart(displayLink.value))
true
} catch (e: InternalException) {
if (displayLink.value != null) {
CVDisplayLinkRelease(displayLink.value)
}
e.printStackTrace()
false
}
//println("createDisplayLink[5]")
}

private fun timer(timer: NSTimer?) {
Expand Down Expand Up @@ -645,7 +647,7 @@ class MyDefaultGameWindow : GameWindow() {
override suspend fun clipboardRead(): ClipboardData? {
val pasteboard = NSPasteboard.generalPasteboard
val items = pasteboard.pasteboardItems as? List<NSPasteboardItem>?
if (items == null || items.isEmpty()) return null
if (items.isNullOrEmpty()) return null
return items.last().stringForType(NSPasteboardTypeString)?.let { TextClipboardData(it) }
}
}
Expand All @@ -656,8 +658,13 @@ private val atomicDisplayLinkContext = AtomicReference<COpaquePointer?>(null)

@SharedImmutable
val doDisplayCallbackRender: () -> Unit = {
val gameWindow = atomicDisplayLinkContext.value?.asStableRef<MyDefaultGameWindow>()
gameWindow?.get()?.doRender(update = true)
try {
initRuntimeIfNeeded()
val gameWindow = atomicDisplayLinkContext.value?.asStableRef<MyDefaultGameWindow>()
gameWindow?.get()?.doRender(update = true)
} catch (e: Throwable) {
e.printStackTrace()
}
}

@Suppress("UNUSED_PARAMETER")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.soywiz.korio.lang

import com.soywiz.klogger.Console
import com.soywiz.krypto.encoding.*

expect open class IOException(msg: String) : Exception
expect open class EOFException(msg: String) : IOException
Expand All @@ -11,6 +12,7 @@ open class MalformedInputException(msg: String) : Exception(msg) {

class FileAlreadyExistsException(msg: String) : IOException(msg)

class InternalException(val code: Int) : Exception("Internal Exception with code $code (0x${code.hex})")
class InvalidOperationException(str: String = "Invalid Operation") : Exception(str)
class OutOfBoundsException(index: Int = -1, str: String = "Out Of Bounds") : Exception(str)
class KeyNotFoundException(str: String = "Key Not Found") : Exception(str)
Expand All @@ -32,15 +34,15 @@ val invalidArg: Nothing get() = throw InvalidArgumentException()
val unreachable: Nothing get() = throw UnreachableException()
val reserved: Nothing get() = throw ReservedException()

fun internalException(code: Int): Nothing = throw InternalException(code)
fun deprecated(msg: String): Nothing = throw DeprecatedException(msg)
fun mustValidate(msg: String): Nothing = throw MustValidateCodeException(msg)
fun noImpl(msg: String): Nothing = throw NotImplementedException(msg)
fun invalidOp(msg: String): Nothing = throw InvalidOperationException(msg)
fun invalidArg(msg: String): Nothing = throw InvalidArgumentException(msg)
fun unreachable(msg: String): Nothing = throw UnreachableException(msg)
fun reserved(msg: String): Nothing = throw ReservedException(msg)
fun unsupported(msg: String): Nothing = throw UnsupportedOperationException(msg)
fun unsupported(): Nothing = throw UnsupportedOperationException("unsupported")
fun unsupported(msg: String = "unsupported"): Nothing = throw UnsupportedOperationException(msg)
fun invalidArgument(msg: String): Nothing = throw InvalidArgumentException(msg)
fun unexpected(msg: String): Nothing = throw UnexpectedException(msg)
fun malformedInput(msg: String): Nothing = throw MalformedInputException(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ val String.unhexIgnoreSpaces: ByteArray get() = buildString {
val String.unhex get() = Hex.decode(this)
val ByteArray.hex get() = Hex.encodeLower(this)

/**
* Returns this number as a hexadecimal string in the format:
* 0xWWXXYYZZ
*/
val Int.hex: String get() = "0x$shex"
/**
* Returns this number as a hexadecimal string in the format:
* WWXXYYZZ
*/
val Int.shex: String
get() = buildString(8) {
for (n in 0 until 8) {
Expand Down