Hey, I'm trying to achivie something with multipla...
# multiplatform
r
Hey, I'm trying to achivie something with multiplatform and I got stuck, let me explain my problem: I've an application that now runs on jvm and controls sounds. I do need to change the system volume for mac os x and windows, and sadly, volumen cannot be changed with java normal apis (Value is changed by sound isn't), so I'm creating two implementations for do it, first one is for mac os x and it uses NSSound class from AppKit platform. My problem comes when I'm tryng to use that class, is impossible to me from jvm module.. I've tried with expect/actual approach but always requires a JVM implementation which is the one used in the end. I've created a macosMain module which allows me to import NSSound class but it is not callable from JVM to use it as a implementation. I've put macosMain module as dependency for the JVM one and when that is done, platform.AppKit dependencies are not importable anymore.. So, that is my question, is there anyway to implement that in kotlin multiplatform? Is there any example of it?
j
Sounds like you want a Native library with OS-specific implementations as a dependency for a JVM app. I tried asking about that on SO a while ago, but never got a response: https://stackoverflow.com/q/75485391/8681 I figured it's just too early and not possible yet. But if it is, I'd love to hear about it.
r
Ow, thanks for quick response..
I will keep investigating then :(
c
erm... you won't really need multiplatform for this, given that the platform you're targetting is just JVM? If you are using JVM then you can make your native libraries for controlling volume and detect which one to load using
System.loadLibrary
at runtime and then ??? profit?
j
But how do you call the native code from the JVM? Just plain old
native
declarations? Any tricks to easily generate bindings that fix types?
Both ends being Kotlin sounds like a great opportunity to make that easy
c
plain old native declarations! You can use javap to generate C headers from java code I think
you can do some shenanigans with the JNI loading to make things a little more tolerable but it's been years since I played around with it now. I'm fairly certain you can define a sort of 'entry point' where you can configure the JNI loading to do away with the grotesque looking function names (because you can explain what the FQCNs would be).
I'm not sure how difficult it will be to marshal kotlin objecs to kotlin native objects from jvm, I'd be interested to see how it goes
though I assumed you'd have
native fun setVolume(volume: Int)
😂
e
I can understand why you'd like Kotlin on both sides of the interface, but currently there's no automated support for it
in the past there was https://github.com/JetBrains/kotlin-native/issues/2252 but that was an experiment that was never really publicized or released
j
Thanks @czuckie and @ephemient, that's about what I figured. I never managed to get native working well, must be my complete lack of C/C++ experience 🙂 Hope we'll get something from Kotlin eventually, but I can imagine it's not a priority.
r
Looks like I need to go with native implementation. Would be awesome to be able to do this in a future ..
e
Kotlin/Native implementation is fine, you'll still have to go through cinterop and JNI to use it from the JVM (same as any other native⇔Java communication)
but if you're just calling a single system API, I'd just use JNA or project panama to access it "directly" instead of wrapping it in another layer
r
I’m thinking about just create a C code compatible with osx and windows, compile it and call it using jni..is the only way I know to handle this native calls
e
if you already know how to interact with K/N cinterop, using https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md or https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_ffi.md instead shouldn't be a challenge. it just works a bit different since it's linking at runtime instead of compile time
r
Taking a look! Thanks!!