Hello Guest

Is there a replacement for Sys.openURL()?

  • 3 Replies
  • 520 Views
*

Offline princec

  • *****
  • 1931
    • Puppygames
Is there a replacement for Sys.openURL()?
« on: February 14, 2019, 13:24:59 »
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 :)

*

Offline kappa

  • *****
  • 1314
Re: Is there a replacement for Sys.openURL()?
« Reply #1 on: February 14, 2019, 15:01:50 »
Don't think there is a method in LWJGL3 yet, for an AWT free method just use Runtime.exec (see link for example).
« Last Edit: February 14, 2019, 15:21:55 by kappa »

*

Offline spasi

  • *****
  • 2163
    • WebHotelier
Re: Is there a replacement for Sys.openURL()?
« Reply #2 on: February 14, 2019, 15:07:14 »
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:

Code: [Select]
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.

*

Offline princec

  • *****
  • 1931
    • Puppygames
Re: Is there a replacement for Sys.openURL()?
« Reply #3 on: February 14, 2019, 16:46:12 »
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 :)