cen64/os/winapi/console.c
Giovanni Bajo 5431cd25b3 cen64: cleanup console/windowed support
Currently, the experience of cen64 binaries of Windows is not the
greatest, to be kind. If you get the binary and double-click on it,
you get nothing (no feedback whatsoever). If you run it from the
console, you get nothing (no command line help is shown). I am not sure
how Windows users ever manage to use it.

This happens because the binary is linked as a windowed application,
but when run with no command line applications, it exits after printing
the help with printf, which does nothing since no console is
attached to the windowed application.

This PR improves the usability on Windows. It compiles cen64 has a
console application (as was meant to be used), so that the help text
or any other stdout/stderr output is now visible on console. Moreover,
to provide a decent experience to users double-clicking on the
binary, it displays an error message explaining that it should be run
from the command line instead.
2022-03-19 22:59:20 +01:00

44 lines
1 KiB
C

//
// os/winapi/console.c
//
// Functions for manipulating the console.
//
// CEN64: Cycle-Accurate Nintendo 64 Emulator.
// Copyright (C) 2015, Tyler J. Stachecki.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#include "cen64.h"
#include <windows.h>
bool has_console(void) {
DWORD procId;
DWORD count = GetConsoleProcessList(&procId, 1);
return (count >= 2);
}
void check_start_from_explorer(void) {
if (has_console()) return;
system("cmd /S /K \"echo cen64 is a command-line application, don't double-click on it! & echo: & echo Type ^\"cen64.exe^\" from this prompt to get the usage.\"");
exit(0);
}
// "Hides" the console window (after waiting for input).
void hide_console(void) {
printf("\n");
system("PAUSE");
FreeConsole();
}
// "Unhides" the console window.
void show_console(void) {
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0)
AllocConsole();
freopen("CONOUT$", "wb", stdout);
freopen("CONOUT$", "wb", stderr);
}