scoop:scoop.window:0.1.0
src/input/clipboard_native.scoop
Selected production source · src/input/clipboard_native.scoop
1
package scoop.window3
internal fun platformReadClipboard(mediaType: String): String? {4
@Unsafe5
{6
val result =7
mediaType.withUtf8Bytes {8
bytes,9
length -> @Unsafe10
{11
return@withUtf8Bytes nativeClipboardRead(bytes, length)12
}13
}14
return decodeClipboardText(result, "read clipboard")15
}16
}18
internal fun decodeClipboardText(result: NativeClipboardText, operation: String): String? {19
@Unsafe20
{21
try {22
checkNativeStatus(result.error, operation)23
return when (result.bytes) {24
case Some(val bytes) -> {25
try {26
Some(String.fromUtf8(bytes, result.length))27
} catch (error: CharacterCodingException) {28
throw WindowException(operation, WindowErrorKind.NativeFailure)29
}30
}31
case None -> None32
}33
} finally {34
nativeClipboardRelease(result.bytes)35
}36
}37
}39
internal fun platformWriteClipboard(application: Ptr<Unit>, representations: List<ClipboardText>) {40
@Unsafe41
{42
val writer = created(nativeClipboardCreate(), "prepare clipboard")43
try {44
for (representation in representations) {45
representation.mediaType.withUtf8Bytes {46
typeBytes,47
typeLength -> @Unsafe48
{49
representation.text.withUtf8Bytes {50
bytes,51
length -> @Unsafe52
{53
checkNativeStatus(54
nativeClipboardAdd(55
writer,56
typeBytes,57
typeLength,58
bytes,59
length60
),61
"prepare clipboard"62
)63
}64
}65
}66
}67
}68
checkNativeStatus(nativeClipboardWrite(writer, application), "write clipboard")69
} finally {70
nativeClipboardDestroy(writer)71
}72
}73
}75
@CLayout76
internal struct NativeClipboardText(val bytes: Ptr<UInt8>?, val length: Long, val error: Long)78
@Extern(name = "scoop_window_clipboard_read")79
private fun nativeClipboardRead(mediaType: Ptr<UInt8>, length: Long): NativeClipboardText81
@Extern(name = "scoop_window_clipboard_release")82
private fun nativeClipboardRelease(bytes: Ptr<UInt8>?)84
@Extern(name = "scoop_window_clipboard_create")85
private fun nativeClipboardCreate(): NativeCreateResult87
@Extern(name = "scoop_window_clipboard_add")88
private fun nativeClipboardAdd(89
writer: Ptr<Unit>,90
mediaType: Ptr<UInt8>,91
typeLength: Long,92
bytes: Ptr<UInt8>,93
length: Long94
): Long96
@Extern(name = "scoop_window_clipboard_write")97
private fun nativeClipboardWrite(writer: Ptr<Unit>, application: Ptr<Unit>): Long99
@Extern(name = "scoop_window_clipboard_destroy")100
private fun nativeClipboardDestroy(writer: Ptr<Unit>)