javajava-native-interfaceproject-panamajawt

How do I get the HWND of a Canvas using Panama?


I'm porting a program that uses JNI to use Panama instead. This program uses OpenGL to draw onto a Canvas object. I can port the Windows and OpenGL calls to use Panama, but the problem is I need to get the HWND of the Canvas I want to draw to. The C code looks like this:

JNIEXPORT void JNICALL Java_somepackage_SomeCanvas_paint(JNIEnv* env, jobject canvas, jobject graphics)
{
    JAWT awt;
    awt.version = JAWT_VERSION_1_4;
    JAWT_GetAWT(env, &awt);
    JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);
    ds->Lock(ds);
    JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
    JAWT_Win32DrawingSurfaceInfo* dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
    HWND hwnd = dsi_win->hwnd;
    ...
}

I can't call JAWT_GetAWT from Panama though, because it needs a JNIEnv argument, which is only available in JNI calls. How do I get the HWND (or JAWT_DrawingSurface) of the Canvas instead?

I've tried looking online for any sort of API I could use to do this, but I haven't found anything. I'm hoping someone here might know.


Solution

  • You could theoretically get a JNIEnv* using the Panama API by loading the jvm library, and using the invocation API's JNI_GetCreatedJavaVMs and then GetEnv on the retrieved JavaVM* to get a JNIEnv*.

    I think after that you're still stuck when needing to call GetDrawingSurface though, because it takes an object handle of the canvas object as an argument, and there is currently no way to turn a Java object into a handle in pure Java code (at least, not that I'm aware of).

    The Java AWT Native Interface that you're using has a dependency on JNI, so I don't think you can get rid of JNI entirely (and the Panama API currently doesn't really have specific JNI interop support)