[UI] Loop thread to main thread WindowedAppContext

This commit is contained in:
Triang3l
2021-08-28 19:38:24 +03:00
parent f540c188bf
commit 6ce5330f5f
95 changed files with 2343 additions and 1235 deletions

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
<asmv3:application>
<asmv3:windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>

22
src/xenia/base/console.h Normal file
View File

@@ -0,0 +1,22 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_CONSOLE_H_
#define XENIA_BASE_CONSOLE_H_
namespace xe {
// Returns true if there is a user-visible console attached to receive stdout.
bool has_console_attached();
void AttachConsole();
} // namespace xe
#endif // XENIA_BASE_CONSOLE_H_

View File

@@ -2,54 +2,50 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2020 Ben Vanik. All rights reserved. *
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_MAIN_H_
#define XENIA_BASE_MAIN_H_
#ifndef XENIA_BASE_CONSOLE_APP_MAIN_H_
#define XENIA_BASE_CONSOLE_APP_MAIN_H_
#include <optional>
#include <string>
#include <vector>
#include "xenia/base/cvar.h"
#include "xenia/base/platform.h"
namespace xe {
// Returns true if there is a user-visible console attached to receive stdout.
bool has_console_attached();
void AttachConsole();
// Extern defined by user code. This must be present for the application to
// launch.
struct EntryInfo {
struct ConsoleAppEntryInfo {
std::string name;
int (*entry_point)(const std::vector<std::string>& args);
bool transparent_options; // no argument parsing
std::optional<std::string> positional_usage;
std::optional<std::vector<std::string>> positional_options;
std::string positional_usage;
std::vector<std::string> positional_options;
};
EntryInfo GetEntryInfo();
ConsoleAppEntryInfo GetConsoleAppEntryInfo();
#define DEFINE_ENTRY_POINT(name, entry_point, positional_usage, ...) \
xe::EntryInfo xe::GetEntryInfo() { \
// TODO(Triang3l): Multiple console app entry points on Android when a console
// activity is added. This is currently for individual executables running on
// Termux.
#define XE_DEFINE_CONSOLE_APP(name, entry_point, positional_usage, ...) \
xe::ConsoleAppEntryInfo xe::GetConsoleAppEntryInfo() { \
std::initializer_list<std::string> positional_options = {__VA_ARGS__}; \
return xe::EntryInfo{ \
return xe::ConsoleAppEntryInfo{ \
name, entry_point, false, positional_usage, \
std::vector<std::string>(std::move(positional_options))}; \
}
// TODO(Joel Linn): Add some way to filter consumed arguments in
// cvar::ParseLaunchArguments()
#define DEFINE_ENTRY_POINT_TRANSPARENT(name, entry_point) \
xe::EntryInfo xe::GetEntryInfo() { \
return xe::EntryInfo{name, entry_point, true, std::nullopt, std::nullopt}; \
#define XE_DEFINE_CONSOLE_APP_TRANSPARENT(name, entry_point) \
xe::ConsoleAppEntryInfo xe::GetConsoleAppEntryInfo() { \
return xe::ConsoleAppEntryInfo{name, entry_point, true, std::string(), \
std::vector<std::string>()}; \
}
} // namespace xe
#endif // XENIA_BASE_MAIN_H_
#endif // XENIA_BASE_CONSOLE_APP_MAIN_H_

View File

@@ -0,0 +1,10 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/console_app_posix.cc"

View File

@@ -7,42 +7,32 @@
******************************************************************************
*/
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "xenia/base/console_app_main.h"
#include "xenia/base/cvar.h"
#include "xenia/base/main.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/logging.h"
#include "xenia/base/string.h"
namespace xe {
bool has_console_attached() { return isatty(fileno(stdin)) == 1; }
void AttachConsole() {}
} // namespace xe
extern "C" int main(int argc, char** argv) {
auto entry_info = xe::GetEntryInfo();
xe::ConsoleAppEntryInfo entry_info = xe::GetConsoleAppEntryInfo();
if (!entry_info.transparent_options) {
cvar::ParseLaunchArguments(argc, argv, entry_info.positional_usage.value(),
entry_info.positional_options.value());
cvar::ParseLaunchArguments(argc, argv, entry_info.positional_usage,
entry_info.positional_options);
}
// Initialize logging. Needs parsed cvars.
xe::InitializeLogging(entry_info.name);
std::vector<std::string> args;
for (int n = 0; n < argc; n++) {
args.push_back(argv[n]);
args.emplace_back(argv[n]);
}
// Initialize logging. Needs parsed FLAGS.
xe::InitializeLogging(entry_info.name);
// Call app-provided entry point.
int result = entry_info.entry_point(args);
xe::ShutdownLogging();
return result;
}

View File

@@ -0,0 +1,36 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <cstdlib>
#include <vector>
#include "xenia/base/console_app_main.h"
#include "xenia/base/main_win.h"
int main(int argc_ignored, char** argv_ignored) {
xe::ConsoleAppEntryInfo entry_info = xe::GetConsoleAppEntryInfo();
std::vector<std::string> args;
if (!xe::ParseWin32LaunchArguments(entry_info.transparent_options,
entry_info.positional_usage,
entry_info.positional_options, &args)) {
return EXIT_FAILURE;
}
int result = xe::InitializeWin32App(entry_info.name);
if (result) {
return result;
}
result = entry_info.entry_point(args);
xe::ShutdownWin32App();
return result;
}

View File

@@ -0,0 +1,21 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <stdio.h>
#include <unistd.h>
#include "xenia/base/console.h"
namespace xe {
bool has_console_attached() { return isatty(fileno(stdin)) == 1; }
void AttachConsole() {}
} // namespace xe

View File

@@ -0,0 +1,60 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include "xenia/base/console.h"
#include "xenia/base/platform_win.h"
namespace xe {
// TODO(Triang3l): Set the default depending on the actual subsystem. Currently
// it inhibits message boxes.
static bool has_console_attached_ = true;
bool has_console_attached() { return has_console_attached_; }
static bool has_shell_environment_variable() {
size_t size = 0;
// Check if SHELL exists
// If it doesn't, then we are in a Windows Terminal
auto error = getenv_s(&size, nullptr, 0, "SHELL");
if (error) {
return false;
}
return !!size;
}
void AttachConsole() {
bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS) == TRUE;
if (!has_console || !has_shell_environment_variable()) {
// We weren't launched from a console, so just return.
has_console_attached_ = false;
return;
}
AllocConsole();
has_console_attached_ = true;
auto std_handle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
auto con_handle = _open_osfhandle(std_handle, _O_TEXT);
auto fp = _fdopen(con_handle, "w");
freopen_s(&fp, "CONOUT$", "w", stdout);
std_handle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
con_handle = _open_osfhandle(std_handle, _O_TEXT);
fp = _fdopen(con_handle, "w");
freopen_s(&fp, "CONOUT$", "w", stderr);
}
} // namespace xe

View File

@@ -14,8 +14,8 @@
#define UTF_CPP_CPLUSPLUS 201703L
#include "third_party/utfcpp/source/utf8.h"
#include "xenia/base/console.h"
#include "xenia/base/logging.h"
#include "xenia/base/main.h"
#include "xenia/base/system.h"
namespace utfcpp = utf8;

View File

@@ -18,10 +18,10 @@
#include "third_party/disruptorplus/include/disruptorplus/sequence_barrier.hpp"
#include "third_party/disruptorplus/include/disruptorplus/spin_wait_strategy.hpp"
#include "xenia/base/atomic.h"
#include "xenia/base/console.h"
#include "xenia/base/cvar.h"
#include "xenia/base/debugging.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/main.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
#include "xenia/base/ring_buffer.h"

View File

@@ -0,0 +1,10 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
// Nothing. Stub.

View File

@@ -2,13 +2,9 @@
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/main.h"
#include <cstdlib>
// Nothing. Stub.

View File

@@ -7,10 +7,11 @@
******************************************************************************
*/
#include "xenia/base/main.h"
#include "xenia/base/platform_win.h"
#include <cstdlib>
// Includes Windows headers, so it goes after platform_win.h.
#include "third_party/xbyak/xbyak/xbyak_util.h"
class StartupAvxCheck {

View File

@@ -7,14 +7,12 @@
******************************************************************************
*/
#include <fcntl.h>
#include <io.h>
#include <cstdlib>
#include <malloc.h>
#include <cstring>
#include "xenia/base/cvar.h"
#include "xenia/base/logging.h"
#include "xenia/base/main.h"
#include "xenia/base/main_win.h"
#include "xenia/base/platform_win.h"
#include "xenia/base/string.h"
@@ -24,54 +22,11 @@
// For RequestHighPerformance.
#include <winternl.h>
// Includes Windows headers, so it goes here.
#include "third_party/xbyak/xbyak/xbyak_util.h"
DEFINE_bool(win32_high_freq, true,
"Requests high performance from the NT kernel", "Kernel");
DEFINE_bool(enable_console, false, "Open a console window with the main window",
"General");
namespace xe {
bool has_console_attached_ = true;
bool has_console_attached() { return has_console_attached_; }
bool has_shell_environment_variable() {
size_t size = 0;
// Check if SHELL exists
// If it doesn't, then we are in a Windows Terminal
auto error = getenv_s(&size, nullptr, 0, "SHELL");
if (error) {
return false;
}
return !!size;
}
void AttachConsole() {
bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS) == TRUE;
if (!has_console || !has_shell_environment_variable()) {
// We weren't launched from a console, so just return.
has_console_attached_ = false;
return;
}
AllocConsole();
has_console_attached_ = true;
auto std_handle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
auto con_handle = _open_osfhandle(std_handle, _O_TEXT);
auto fp = _fdopen(con_handle, "w");
freopen_s(&fp, "CONOUT$", "w", stdout);
std_handle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
con_handle = _open_osfhandle(std_handle, _O_TEXT);
fp = _fdopen(con_handle, "w");
freopen_s(&fp, "CONOUT$", "w", stderr);
}
static void RequestHighPerformance() {
#if XE_PLATFORM_WIN32
NTSTATUS(*NtQueryTimerResolution)
@@ -97,8 +52,10 @@ static void RequestHighPerformance() {
#endif
}
static bool parse_launch_arguments(const xe::EntryInfo& entry_info,
std::vector<std::string>& args) {
bool ParseWin32LaunchArguments(
bool transparent_options, const std::string_view positional_usage,
const std::vector<std::string>& positional_options,
std::vector<std::string>* args_out) {
auto command_line = GetCommandLineW();
int wargc;
@@ -118,48 +75,24 @@ static bool parse_launch_arguments(const xe::EntryInfo& entry_info,
LocalFree(wargv);
if (!entry_info.transparent_options) {
cvar::ParseLaunchArguments(argc, argv, entry_info.positional_usage.value(),
entry_info.positional_options.value());
if (!transparent_options) {
cvar::ParseLaunchArguments(argc, argv, positional_usage,
positional_options);
}
args.clear();
for (int n = 0; n < argc; n++) {
args.push_back(std::string(argv[n]));
if (args_out) {
args_out->clear();
for (int n = 0; n < argc; n++) {
args_out->push_back(std::string(argv[n]));
}
}
return true;
}
int Main() {
auto entry_info = xe::GetEntryInfo();
std::vector<std::string> args;
if (!parse_launch_arguments(entry_info, args)) {
return 1;
}
// Attach a console so we can write output to stdout. If the user hasn't
// redirected output themselves it'll pop up a window.
if (cvars::enable_console) {
xe::AttachConsole();
}
// Setup COM on the main thread.
// NOTE: this may fail if COM has already been initialized - that's OK.
#pragma warning(suppress : 6031)
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
int InitializeWin32App(const std::string_view app_name) {
// Initialize logging. Needs parsed FLAGS.
xe::InitializeLogging(entry_info.name);
Xbyak::util::Cpu cpu;
if (!cpu.has(Xbyak::util::Cpu::tAVX)) {
xe::FatalError(
"Your CPU does not support AVX, which is required by Xenia. See the "
"FAQ for system requirements at https://xenia.jp");
return -1;
}
xe::InitializeLogging(app_name);
// Print version info.
XELOGI(
@@ -175,38 +108,9 @@ int Main() {
RequestHighPerformance();
}
// Call app-provided entry point.
int result = entry_info.entry_point(args);
xe::ShutdownLogging();
return result;
return 0;
}
void ShutdownWin32App() { xe::ShutdownLogging(); }
} // namespace xe
// Used in console mode apps; automatically picked based on subsystem.
int main(int argc_ignored, char** argv_ignored) { return xe::Main(); }
// Used in windowed apps; automatically picked based on subsystem.
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR command_line, int) {
// Run normal entry point.
return xe::Main();
}
#if defined _M_IX86
#pragma comment( \
linker, \
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#elif defined _M_IA64
#pragma comment( \
linker, \
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#elif defined _M_X64
#pragma comment( \
linker, \
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#else
#pragma comment( \
linker, \
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#endif

29
src/xenia/base/main_win.h Normal file
View File

@@ -0,0 +1,29 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2021 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_MAIN_WIN_H_
#define XENIA_BASE_MAIN_WIN_H_
#include <string>
#include <vector>
namespace xe {
// Functions for calling in both windowed and console entry points.
bool ParseWin32LaunchArguments(
bool transparent_options, const std::string_view positional_usage,
const std::vector<std::string>& positional_options,
std::vector<std::string>* args_out);
// InitializeWin32App uses cvars, call ParseWin32LaunchArguments before.
int InitializeWin32App(const std::string_view app_name);
void ShutdownWin32App();
} // namespace xe
#endif // XENIA_BASE_MAIN_WIN_H_

View File

@@ -11,7 +11,8 @@ project("xenia-base")
defines({
})
local_platform_files()
removefiles({"main_*.cc"})
removefiles({"console_app_main_*.cc"})
removefiles({"main_init_*.cc"})
files({
"debug_visualizers.natvis",
})