Skip to content

Commit

Permalink
Support for launching a web browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jan 25, 2008
1 parent 16c2767 commit d5c776b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs.txt
Expand Up @@ -848,6 +848,19 @@ Your config file is a Lua script, and as such, has access to all of Lua's
Write debug info to the installation log, if logging settings permit.


MojoSetup.launchbrowser(url)

Launch a web browser to view the URL (url). This will try to launch a
new window/tab of the user's preferred handler for the url. Launching
"http://" URLs are probably always safe, other protocols, like "ftp://"
may or may not work, depending on what the platform offers.

Returns true if the browser launched, false otherwise. We can't know
if the URL actually loaded or rendered, just if the browser launched.
The hope is that the browser will inform the user if there's a problem
loading the URL.


MojoSetup.msgbox(title, str)

Show (str) to the user with a GUI message box, and wait until they click
Expand Down
11 changes: 9 additions & 2 deletions lua_glue.c
Expand Up @@ -596,11 +596,17 @@ static int luahook_translate(lua_State *L)

static int luahook_ticks(lua_State *L)
{
lua_pushnumber(L, MojoPlatform_ticks());
return 1;
return retvalNumber(L, MojoPlatform_ticks());
} // luahook_ticks


static int luahook_launchbrowser(lua_State *L)
{
const char *url = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_launchBrowser(url));
} // luahook_launchbrowser


static int luahook_msgbox(lua_State *L)
{
if (GGui != NULL)
Expand Down Expand Up @@ -1592,6 +1598,7 @@ boolean MojoLua_initLua(void)
set_cfunc(luaState, luahook_ticks, "ticks");
set_cfunc(luaState, luahook_format, "format");
set_cfunc(luaState, luahook_fatal, "fatal");
set_cfunc(luaState, luahook_launchbrowser, "launchbrowser");
set_cfunc(luaState, luahook_msgbox, "msgbox");
set_cfunc(luaState, luahook_promptyn, "promptyn");
set_cfunc(luaState, luahook_promptynan, "promptynan");
Expand Down
20 changes: 20 additions & 0 deletions mojosetup.c
Expand Up @@ -14,6 +14,9 @@
#include "lua_glue.h"
#include "fileio.h"

#define TEST_LAUNCH_BROWSER_CODE 0
int MojoSetup_testLaunchBrowserCode(int argc, char **argv);

#define TEST_ARCHIVE_CODE 0
int MojoSetup_testArchiveCode(int argc, char **argv);

Expand Down Expand Up @@ -717,6 +720,10 @@ int MojoSetup_main(int argc, char **argv)
return 0;
} // if

#if TEST_LAUNCH_BROWSER_CODE
return MojoSetup_testLaunchBrowserCode(argc, argv);
#endif

#if TEST_ARCHIVE_CODE
return MojoSetup_testArchiveCode(argc, argv);
#endif
Expand All @@ -738,6 +745,19 @@ int MojoSetup_main(int argc, char **argv)



#if TEST_LAUNCH_BROWSER_CODE
int MojoSetup_testLaunchBrowserCode(int argc, char **argv)
{
int i;
printf("Testing browser launching code...\n\n");
for (i = 1; i < argc; i++)
{
const boolean rc = MojoPlatform_launchBrowser(argv[i]);
printf("Launch '%s': %s\n", argv[i], rc ? "success" : "failure");
} // for
return 0;
} // MojoSetup_testLaunchBrowserCode
#endif


#if TEST_ARCHIVE_CODE
Expand Down
6 changes: 6 additions & 0 deletions platform.h
Expand Up @@ -225,6 +225,12 @@ void *MojoPlatform_dlopen(const uint8 *img, size_t len);
void *MojoPlatform_dlsym(void *lib, const char *sym);
void MojoPlatform_dlclose(void *lib);

// Launch the user's preferred browser to view the URL (url).
// Returns true if the browser launched, false otherwise. We can't know
// if the URL actually loaded, just if the browser launched. The hope is that
// the browser will inform the user if there's a problem loading the URL.
boolean MojoPlatform_launchBrowser(const char *url);

#if !SUPPORT_MULTIARCH
#define MojoPlatform_switchBin(img, len)
#else
Expand Down
14 changes: 14 additions & 0 deletions platform_unix.c
Expand Up @@ -932,6 +932,20 @@ void MojoPlatform_dlclose(void *lib)
} // MojoPlatform_dlclose


boolean MojoPlatform_launchBrowser(const char *url)
{
#if PLATFORM_MACOSX
CFURLRef cfurl = CFURLCreateWithBytes(NULL, (const UInt8 *) url,
strlen(url), kCFStringEncodingUTF8, NULL);
const OSStatus err = LSOpenCFURLRef(cfurl, NULL);
CFRelease(cfurl);
return (err == noErr);
#else
return false; // !!! FIXME: write me.
#endif
} // MojoPlatform_launchBrowser


#if SUPPORT_MULTIARCH
void MojoPlatform_switchBin(const uint8 *img, size_t len)
{
Expand Down
9 changes: 9 additions & 0 deletions platform_windows.c
Expand Up @@ -635,6 +635,15 @@ boolean MojoPlatform_istty(void)
} // MojoPlatform_istty


boolean MojoPlatform_launchBrowser(const char *url)
{
// msdn says:
// "Returns a value greater than 32 if successful, or an error value that
// is less than or equal to 32 otherwise."
return (ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL) > 32);
} // MojoPlatform_launchBrowser


void MojoPlatform_spawnTerminal(void)
{
// unsupported.
Expand Down

0 comments on commit d5c776b

Please sign in to comment.