Own Windows app lifetime in runtime shell

This commit is contained in:
2026-06-17 09:46:43 +02:00
parent 10a3c0498e
commit 25eff166f6
6 changed files with 73 additions and 31 deletions

View File

@@ -53,9 +53,7 @@ void handle_window_close_message(VrShellState& vr)
runtime->ui_thread_stop();
runtime->render_thread_stop();
app->terminate();
bind_app(nullptr);
bind_runtime(nullptr);
delete app;
release_bound_app();
PostQuitMessage(0);
}

View File

@@ -18,6 +18,7 @@ namespace pp::platform::windows {
namespace {
struct RetainedWindowsRuntimeState final {
std::unique_ptr<App> owned_app;
App* app = nullptr;
AppRuntime* runtime = nullptr;
WacomTablet* tablet = nullptr;
@@ -132,6 +133,14 @@ App* bound_app() noexcept
return retained_runtime_state().app;
}
void release_bound_app() noexcept
{
auto& state = retained_runtime_state();
bind_runtime(nullptr);
bind_app(nullptr);
state.owned_app.reset();
}
void bind_runtime(AppRuntime* runtime) noexcept
{
retained_runtime_state().runtime = runtime;
@@ -177,7 +186,9 @@ int run_main_application(int argc, char** argv)
auto& state = retained_state();
state.hInst = GetModuleHandle(NULL);
auto* app = new App();
auto& runtime_state = retained_runtime_state();
runtime_state.owned_app = std::make_unique<App>();
auto* app = runtime_state.owned_app.get();
bind_app(app);
bind_runtime(&app->runtime());
bind_wacom_tablet(&WacomTablet::I);
@@ -209,13 +220,11 @@ int run_main_application(int argc, char** argv)
case pp::platform::windows::MainStartupResult::Ok:
break;
case pp::platform::windows::MainStartupResult::GladLoadFailure:
bind_app(nullptr);
bind_runtime(nullptr);
release_bound_app();
bind_wacom_tablet(nullptr);
return 0;
case pp::platform::windows::MainStartupResult::MissingCoreContextSupport:
bind_app(nullptr);
bind_runtime(nullptr);
release_bound_app();
bind_wacom_tablet(nullptr);
return -1;
}
@@ -230,8 +239,7 @@ int run_main_application(int argc, char** argv)
case const_hash("convert"):
app->initShaders();
app->cmd_convert(argv[2], argv[3]);
bind_app(nullptr);
bind_runtime(nullptr);
release_bound_app();
bind_wacom_tablet(nullptr);
return 0;
case const_hash("-vrmode"):
@@ -243,6 +251,7 @@ int run_main_application(int argc, char** argv)
}
pp::platform::windows::run_main_window_runtime(startup, start_in_vr, splash);
release_bound_app();
bind_wacom_tablet(nullptr);
return 0;
}

View File

@@ -13,6 +13,7 @@ int run_main_application(int argc, char** argv);
void run_main_window_runtime(const MainWindowStartupState& startup, bool start_in_vr, SplashScreen& splash);
void bind_app(App* app) noexcept;
[[nodiscard]] App* bound_app() noexcept;
void release_bound_app() noexcept;
void bind_runtime(AppRuntime* runtime) noexcept;
[[nodiscard]] AppRuntime* bound_runtime() noexcept;
void bind_wacom_tablet(WacomTablet* tablet) noexcept;

View File

@@ -34,11 +34,42 @@ namespace {
return pointer_source;
}
[[nodiscard]] std::map<kKey, int>& retained_virtual_key_map()
{
return retained_state().vkey_map;
}
[[nodiscard]] pp::platform::windows::VrShellState& retained_vr_shell_state()
{
return retained_state().vr;
}
void synchronize_app_key_state_from_keyboard(App& app)
{
static BYTE keys[256];
if (!GetKeyboardState(keys))
return;
const bool alt = (keys[VK_MENU] & 0x80) != 0;
for (const auto& [key, vk] : retained_virtual_key_map())
{
if (alt && key == kKey::KeyTab)
continue;
const bool down = (keys[vk] & 0x80) != 0;
if (app.keys[(int)key] && !down)
app.key_up(key);
else if (!app.keys[(int)key] && down)
app.key_down(key);
}
}
}
void initialize_retained_input_state()
{
auto& state = retained_state();
auto& vkey_map = retained_virtual_key_map();
memset(&state.keys, 0, sizeof(state.keys));
for (int k = 1; k < 256; ++k) // ignore kKey::Unknown = 0
{
@@ -48,14 +79,14 @@ void initialize_retained_input_state()
continue;
auto key = (kKey)k;
if (state.vkey_map.find(key) == state.vkey_map.end())
if (vkey_map.find(key) == vkey_map.end())
{
state.vkey_map.insert({ key, vk });
vkey_map.insert({ key, vk });
}
else
{
LOG("KEY MAP COLLISION %d and %d maps to %d",
vk, state.vkey_map[key], k);
vk, vkey_map[key], k);
}
}
}
@@ -69,7 +100,6 @@ RetainedState& retained_state()
LRESULT CALLBACK main_window_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
auto& state = retained_state();
auto* app = bound_app();
static glm::vec2 lastPoint;
@@ -78,7 +108,7 @@ LRESULT CALLBACK main_window_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
switch (msg)
{
case kUserCloseMessage:
handle_window_close_message(state.vr);
handle_window_close_message(retained_vr_shell_state());
return 0;
case WM_PAINT:
app->redraw = true;
@@ -114,24 +144,10 @@ LRESULT CALLBACK main_window_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
case WM_ACTIVATE:
{
platform_services().set_cursor_visible(true);
app->ui_task_async([app, &state, wp, lp] {
app->ui_task_async([app, wp, lp] {
int active = GET_WM_ACTIVATE_STATE(wp, lp);
active_wacom_tablet().set_focus(active);
static BYTE keys[256];
if (GetKeyboardState(keys))
{
bool alt = keys[VK_MENU] & 0x80;
for (auto k : state.vkey_map)
{
if (alt && k.first == kKey::KeyTab)
continue;
bool down = keys[k.second] & 0x80;
if (app->keys[(int)k.first] && !down)
app->key_up(k.first);
else if (!app->keys[(int)k.first] && down)
app->key_down(k.first);
}
}
synchronize_app_key_state_from_keyboard(*app);
});
break;
}