Skip to content

Commit

Permalink
🐛 The jna window interface requires active 32 and 64 bit compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang committed Nov 30, 2023
1 parent 5136e35 commit e248290
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expect fun currentPlatform(): Platform

interface Platform {
val name: String
val bitMode: Int
val version: String

fun isWindows(): Boolean {
Expand All @@ -17,4 +18,8 @@ interface Platform {
fun isLinux(): Boolean {
return name == "Linux"
}

fun is64bit(): Boolean {
return bitMode == 64
}
}
14 changes: 14 additions & 0 deletions composeApp/src/commonMain/kotlin/com/clipevery/utils/ToolUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.clipevery.utils

class OnceFunction<T>(private val function: () -> T) {
private var hasRun = false
private var result: T? = null

fun run(): T {
if (!hasRun) {
result = function()
hasRun = true
}
return result!!
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
package com.clipevery.platform

import com.clipevery.utils.OnceFunction

actual fun currentPlatform(): Platform {
return OnceFunction { getCurrentPlatform() }.run()
}

private fun getCurrentPlatform(): Platform {
val osName = System.getProperty("os.name").lowercase()
val version = System.getProperty("os.version")
val architecture = System.getProperty("os.arch")
val bitMode = if (architecture.contains("64")) {
64
} else {
32
}
return when {
"win" in osName -> object : Platform {
override val name = "Windows"
override val bitMode: Int = bitMode
override val version = version
}
"mac" in osName -> object : Platform {
override val name = "Macos"
override val bitMode: Int = bitMode
override val version = version
}
"nix" in osName || "nux" in osName || "aix" in osName -> object : Platform {
override val name = "Linux"
override val bitMode: Int = bitMode
override val version = version
}
else -> object : Platform {
override val name = "Unknown"
override val bitMode: Int = bitMode
override val version = version
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.clipevery.windows

import com.clipevery.clip.ClipboardService
import com.clipevery.clip.TransferableConsumer
import com.clipevery.platform.currentPlatform
import com.clipevery.windows.api.User32
import com.sun.jna.Pointer
import com.sun.jna.platform.win32.Kernel32
Expand Down Expand Up @@ -36,7 +37,12 @@ class WindowsClipboardService
null, 0, 0, null
)
nextViewer = User32.INSTANCE.SetClipboardViewer(viewer)
User32.INSTANCE.SetWindowLongPtr(viewer, User32.GWL_WNDPROC, this)
val currentPlatform = currentPlatform()
if (currentPlatform.is64bit()) {
User32.INSTANCE.SetWindowLongPtr(viewer, User32.GWL_WNDPROC, this)
} else {
User32.INSTANCE.SetWindowLong(viewer, User32.GWL_WNDPROC, this)
}
val msg = MSG()
val handles = arrayOf(event)
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ interface User32 : StdCallLibrary {
}

fun SetWindowLongPtr(hWnd: HWND?, nIndex: Int, proc: WNDPROC?): Int

fun SetWindowLong(hWnd: HWND?, nIndex: Int, proc: WNDPROC?): Int

fun CreateWindowEx(
styleEx: Int, className: String?, windowName: String?,
style: Int, x: Int, y: Int, width: Int, height: Int, hndParent: HWND?,
Expand Down

0 comments on commit e248290

Please sign in to comment.