Add Windows console wrapper program (mpv.com)

This commit is contained in:
Martin Herkt 2014-01-02 05:26:22 +01:00
parent e9f577eb9a
commit 1437d9b8bc
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,83 @@
/*
* conredir, a hack to get working console IO with Windows GUI applications
*
* Copyright (c) 2013, Martin Herkt
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <windows.h>
void cr_perror(const wchar_t *prefix)
{
wchar_t *error;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&error, 0, NULL);
fwprintf(stderr, L"%s: %s", prefix, error);
LocalFree(error);
}
void cr_runproc(wchar_t *name, wchar_t *cmdline)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.dwFlags |= STARTF_USESTDHANDLES;
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcessW(name, cmdline, NULL, NULL, TRUE, 0,
NULL, NULL, &si, &pi)) {
cr_perror(L"CreateProcess");
} else {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
int wmain(int argc, wchar_t **argv, wchar_t **envp)
{
wchar_t *cmd, *args, *eargs;
wchar_t exe[MAX_PATH];
size_t len;
cmd = GetCommandLineW();
args = cmd + wcslen(argv[0]);
GetModuleFileNameW(NULL, exe, MAX_PATH);
wcscpy(wcsrchr(exe, '.') + 1, L"exe");
len = wcslen(exe) + wcslen(args) + 1;
eargs = malloc(len * sizeof(wchar_t));
swprintf(eargs, len, L"%s%s", exe, args);
free(cmd);
cr_runproc(exe, eargs);
free(eargs);
return 0;
}

View File

@ -447,6 +447,19 @@ def build(ctx):
**cprog_kwargs
)
if ctx.env.DEST_OS == 'win32':
wrapctx = ctx(
target = "mpv",
source = ['osdep/win32-console-wrapper.c'],
features = "c cprogram",
install_path = ctx.env.BINDIR
)
wrapctx.env.cprogram_PATTERN = "%s.com"
wrapflags = ['-municode', '-mconsole']
wrapctx.env.CFLAGS = wrapflags
wrapctx.env.LAST_LINKFLAGS = wrapflags
if ctx.dependency_satisfied('macosx-bundle'):
from waflib import Utils
ctx.install_files(ctx.env.BINDIR, 'mpv', chmod=Utils.O755)