Skip to content

Commit

Permalink
Allow MojoPlatform_spawnTerminal() to return on success.
Browse files Browse the repository at this point in the history
Platforms (like Windows) that can allocate a tty without
launching a new process can use this.
  • Loading branch information
icculus committed Jul 31, 2009
1 parent d0515c8 commit 8bf96ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
24 changes: 15 additions & 9 deletions mojosetup.c
Expand Up @@ -119,18 +119,24 @@ static void trySwitchBinaries(void)
#endif


static void trySpawnTerminal(void)
static boolean trySpawnTerminalGui(void)
{
if (cmdlinestr("notermspawn", "MOJOSETUP_NOTERMSPAWN", NULL) != NULL)
return; // we already spawned or the user is preventing it.
return false; // we already spawned or the user is preventing it.

if (MojoPlatform_istty()) // maybe we can spawn a terminal for stdio?
return; // We're a terminal already, no need to spawn one.
return false; // We're a terminal already, no need to spawn one.

logInfo("No usable GUI found. Trying to spawn a terminal...");
MojoPlatform_spawnTerminal(); // no return on success.
logError("...Terminal spawning failed.");
} // trySpawnTerminal
if (!MojoPlatform_spawnTerminal())
{
logError("...Terminal spawning failed.");
return false;
} // if

assert(MojoPlatform_istty());
return (MojoGui_initGuiPlugin() != NULL);
} // trySpawnTerminalGui


static boolean initEverything(void)
Expand All @@ -152,8 +158,9 @@ static boolean initEverything(void)

if (!MojoGui_initGuiPlugin())
{
trySpawnTerminal(); // may not return.
panic("Initial GUI setup failed. Cannot continue.");
// This could terminate the process (and relaunch).
if (!trySpawnTerminalGui())
panic("Initial GUI setup failed. Cannot continue.");
} // if

else if (!MojoLua_initLua())
Expand Down Expand Up @@ -1032,7 +1039,6 @@ int MojoSetup_main(int argc, char **argv)

// Jump into Lua for the heavy lifting.
MojoLua_runFile("mojosetup_mainline");

deinitEverything();

return 0;
Expand Down
8 changes: 5 additions & 3 deletions platform.h
Expand Up @@ -251,9 +251,11 @@ boolean MojoPlatform_uninstallDesktopMenuItem(const char *data);
void MojoPlatform_switchBin(const uint8 *img, size_t len);
#endif

// Try to spawn a terminal, and relaunch MojoSetup within it.
// Does not return on success (process replaces itself).
void MojoPlatform_spawnTerminal(void);
// Try to spawn a terminal, and possibly relaunch MojoSetup within it.
// If we can attach to a terminal without relaunching, do so and
// return true. false for failure to attach/spawn.
// May not return on success (process replaces itself).
boolean MojoPlatform_spawnTerminal(void);

// Put the calling process to sleep for at least (ticks) milliseconds.
// This is meant to yield the CPU while spinning in a loop that is polling
Expand Down
4 changes: 3 additions & 1 deletion platform_unix.c
Expand Up @@ -1290,7 +1290,7 @@ void MojoPlatform_switchBin(const uint8 *img, size_t len)
#endif


void MojoPlatform_spawnTerminal(void)
boolean MojoPlatform_spawnTerminal(void)
{
#if PLATFORM_BEOS
#error write me.
Expand Down Expand Up @@ -1428,6 +1428,8 @@ void MojoPlatform_spawnTerminal(void)
free(argv);
free(binpath);
#endif

return false;
} // MojoPlatform_spawnTerminal


Expand Down
53 changes: 25 additions & 28 deletions platform_windows.c
Expand Up @@ -517,12 +517,33 @@ boolean MojoPlatform_uninstallDesktopMenuItem(const char *data)
} // MojoPlatform_uninstallDesktopMenuItem


void MojoPlatform_spawnTerminal(void)
boolean MojoPlatform_spawnTerminal(void)
{
assert(!MojoPlatform_istty());
putenv("MOJOSETUP_WINDOWS_ALLOC_TTY=1"); // WinMain() will allocate a console next time.
// !!! FIXME: relaunch binary.
exit(0);

if (AllocConsole())
{
int hCrt;
FILE *hf;
hCrt = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stdout = *hf;
setvbuf( stdout, NULL, _IONBF, 0 );

hCrt = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stderr = *hf;
setvbuf( stderr, NULL, _IONBF, 0 );

hCrt = _open_osfhandle((long) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "r" );
*stdin = *hf;
setvbuf( stdin, NULL, _IONBF, 0 );

return true;
} // if

return false;
} // MojoPlatform_spawnTerminal


Expand Down Expand Up @@ -1518,30 +1539,6 @@ static boolean platformInit(void)
if (!findApiSymbols())
return false;

//if (getenv("MOJOSETUP_WINDOWS_ALLOC_TTY") != NULL)
{
if (AllocConsole())
{
// !!! FIXME: Get rid of stdio stuff.
int hCrt, i;
FILE *hf;
hCrt = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stdout = *hf;
i = setvbuf( stdout, NULL, _IONBF, 0 );

hCrt = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stderr = *hf;
i = setvbuf( stderr, NULL, _IONBF, 0 );

hCrt = _open_osfhandle((long) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "r" );
*stdin = *hf;
i = setvbuf( stdin, NULL, _IONBF, 0 );
} // if
} // if

return true;
} // platformInit

Expand Down

0 comments on commit 8bf96ca

Please sign in to comment.