Is there a replacement for Sys.openURL()?

Started by princec, February 14, 2019, 13:24:59

Previous topic - Next topic

princec

Question's in the title.

Built-in to LWGL3 somewhere of course. If not then I'll just go and use Desktop and BrowserLauncher solutions etc.

Cas :)

kappa

Don't think there is a method in LWJGL3 yet, for an AWT free method just use Runtime.exec (see link for example).

spasi

Hey Cas,

No, there isn't. You could copy the Linux & Windows implementations from LWJGL 2, they're pure java. The macOS implementation depends on the com.apple.eio.FileManager API, that has been removed from JDK 9+ afaik. A solution based on Desktop would be problematic with GLFW, because it initializes AWT. I don't know how BrowserLauncher works.

One solution for macOS would be to launch a separate Java process that uses Desktop (or anything else that depends on AWT) to open the browser, then die immediately.

A solution based on LWJGL 3 would be to use the Obj-C Runtime bindings:

long objc_msgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend");

// Implements the following Obj-C code using the Obj-C Runtime:
// [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: @"https://www.lwjgl.org"]];
try (MemoryStack stack = stackPush()) {
    long string = CFStringCreateWithCStringNoCopy(NULL, stack.UTF8("https://www.lwjgl.org"), kCFStringEncodingUTF8, kCFAllocatorNull);

    long sharedWorkspace = invokePPP(objc_msgSend, objc_getClass("NSWorkspace"), sel_getUid("sharedWorkspace"));
    long url = invokePPPP(objc_msgSend, objc_getClass("NSURL"), sel_getUid("URLWithString:"), string);
    int result = invokePPPI(objc_msgSend, sharedWorkspace, sel_getUid("openURL:"), url);
    System.out.println("SUCCESS: " + (result != 0 ? "YES" : "NO"));

    CFRelease(string);
}


Tested with LWJGL 3's Gears sample, appears to be working fine.

princec

Oof, that's a bit of crazy looking code! Thanks for helping me out there, it doesn't look like something I would just dream up overnight...

Cas :)