From 18ed47aa8171344b8bdd8b0ca7b111bb523391a8 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 17 Jun 2026 11:50:24 +0200 Subject: [PATCH] Extract Win32 runtime session helper --- cmake/PanoPainterSources.cmake | 6 +- docs/modernization/roadmap.md | 5 + docs/modernization/tasks.md | 4 + src/platform_windows/windows_runtime_flow.cpp | 124 +--------------- .../windows_runtime_session.cpp | 136 ++++++++++++++++++ .../windows_runtime_session.h | 14 ++ 6 files changed, 165 insertions(+), 124 deletions(-) create mode 100644 src/platform_windows/windows_runtime_session.cpp create mode 100644 src/platform_windows/windows_runtime_session.h diff --git a/cmake/PanoPainterSources.cmake b/cmake/PanoPainterSources.cmake index 01b664ea..75a4689c 100644 --- a/cmake/PanoPainterSources.cmake +++ b/cmake/PanoPainterSources.cmake @@ -245,10 +245,12 @@ set(PP_WINDOWS_PLATFORM_SOURCES src/platform_windows/windows_main_window_session.h src/platform_windows/windows_platform_services.cpp src/platform_windows/windows_platform_services.h - src/platform_windows/windows_runtime_shell.cpp - src/platform_windows/windows_runtime_shell.h src/platform_windows/windows_runtime_flow.cpp src/platform_windows/windows_runtime_flow.h + src/platform_windows/windows_runtime_shell.cpp + src/platform_windows/windows_runtime_shell.h + src/platform_windows/windows_runtime_session.cpp + src/platform_windows/windows_runtime_session.h src/platform_windows/windows_runtime_state.cpp src/platform_windows/windows_runtime_state.h src/platform_windows/windows_splash.cpp diff --git a/docs/modernization/roadmap.md b/docs/modernization/roadmap.md index e4a64965..48b60850 100644 --- a/docs/modernization/roadmap.md +++ b/docs/modernization/roadmap.md @@ -70,6 +70,11 @@ What is already real: - `pp_app_core` Latest slice: +- `src/platform_windows/windows_runtime_session.*` now owns the bound-session + Win32 runtime loop/startup/shutdown body that had still been sitting inside + `windows_runtime_flow.cpp`. +- `src/platform_windows/windows_runtime_flow.cpp` is now a thinner handoff + layer over that Windows session helper. - `src/platform_windows/windows_runtime_flow.*` now owns the Win32 bound-app startup/message-loop/shutdown orchestration that used to live in `windows_runtime_shell.cpp`. diff --git a/docs/modernization/tasks.md b/docs/modernization/tasks.md index 59d95331..c2f42360 100644 --- a/docs/modernization/tasks.md +++ b/docs/modernization/tasks.md @@ -78,6 +78,10 @@ Completed, blocked, and superseded task history moved to the queue is now ordered by code movement instead. Current slice: +- `src/platform_windows/windows_runtime_session.*` now owns the bound-session + Win32 runtime loop/startup/shutdown body. +- `src/platform_windows/windows_runtime_flow.cpp` is now only a thin handoff + into that session helper. - `src/platform_windows/windows_runtime_flow.*` now owns the Win32 bound-app startup/message-loop/shutdown orchestration. - `src/platform_windows/windows_runtime_shell.cpp` now delegates that runtime diff --git a/src/platform_windows/windows_runtime_flow.cpp b/src/platform_windows/windows_runtime_flow.cpp index d2ac036b..85fbdfe1 100644 --- a/src/platform_windows/windows_runtime_flow.cpp +++ b/src/platform_windows/windows_runtime_flow.cpp @@ -2,107 +2,9 @@ #include "platform_windows/windows_runtime_flow.h" -#include "app.h" -#include "log.h" -#include "platform_windows/windows_lifecycle_state.h" -#include "platform_windows/windows_main_window_session.h" -#include "platform_windows/windows_platform_services.h" -#include "platform_windows/windows_runtime_state.h" -#include "platform_windows/windows_stylus_input.h" -#include "platform_windows/windows_runtime_shell.h" -#include "wacom.h" -#include "../resource.h" +#include "platform_windows/windows_runtime_session.h" namespace pp::platform::windows { -namespace { - -void register_touch_window(HWND hWnd) -{ - if (RegisterTouchWindow(hWnd, 0) == 0) - LOG("RegisterTouchWindow error: %s", GetLastErrorAsString().c_str()); -} - -void initialize_runtime_threads() -{ - auto* app = bound_app(); - auto* runtime = retained_bound_runtime(); - assert(runtime); - mark_lifecycle_running(); - runtime->render_thread_start(*app); - runtime->ui_thread_start(*app); -} - -void install_debug_gl_callbacks() -{ -#ifdef _DEBUG - glad_set_pre_callback(_pre_call_callback); - glad_set_post_callback(_post_call_callback); -#endif -} - -void initialize_wintab(HWND hWnd, bool sandboxed) -{ - auto* tablet = bound_wacom_tablet(); - if (!sandboxed) - { - LOG("init WinTab"); - tablet->init(hWnd); - } - else - { - LOG("SKIP init WinTab"); - } -} - -void set_main_window_icon(HWND hWnd) -{ - LOG("change icon"); - SendMessage( - hWnd, - WM_SETICON, - ICON_SMALL, - (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1))); -} - -void restore_window_placement(HWND hWnd, int show_command) -{ - WINDOWPLACEMENT wp; - GetWindowPlacement(hWnd, &wp); - wp.showCmd = show_command; - SetWindowPlacement(hWnd, &wp); -} - -void run_main_message_loop() -{ - MSG msg; - auto* app = bound_app(); - LOG("start main loop"); - while (lifecycle_is_running()) - { - auto present = app->animate || app->redraw ? - PeekMessage(&msg, 0, 0, 0, PM_REMOVE) : GetMessage(&msg, 0, 0, 0); - - if (msg.message == WM_QUIT) - mark_lifecycle_stopped(); - - if (present) - { - DispatchMessage(&msg); - TranslateMessage(&msg); - } - - drain_main_thread_tasks(); - } -} - -void shutdown_main_window_runtime(const MainWindowStartupState& startup, HINSTANCE hInst) -{ - retained_wacom_tablet().terminate(); - UnregisterClass(startup.window_class.lpszClassName, hInst); - LogRemote::I.stop(); -} - -} void run_bound_main_window_runtime( const MainWindowStartupState& startup, @@ -110,29 +12,7 @@ void run_bound_main_window_runtime( HINSTANCE instance, SplashScreen& splash) { - auto* app = bound_app(); - register_touch_window(retained_main_window_handle_ref()); - - wglMakeCurrent(NULL, NULL); - - initialize_runtime_threads(); - install_debug_gl_callbacks(); - initialize_wintab(retained_main_window_handle_ref(), retained_main_window_sandboxed()); - set_main_window_icon(retained_main_window_handle_ref()); - - app->ui_sync(); - restore_window_placement(retained_main_window_handle_ref(), startup.show_command); - - if (start_in_vr) - app->vr_start(); - - LOG("show main window"); - SetForegroundWindow(retained_main_window_handle_ref()); - - splash.dismiss(); - - run_main_message_loop(); - shutdown_main_window_runtime(startup, instance); + run_main_window_runtime_session(startup, start_in_vr, instance, splash); } } diff --git a/src/platform_windows/windows_runtime_session.cpp b/src/platform_windows/windows_runtime_session.cpp new file mode 100644 index 00000000..e21f6661 --- /dev/null +++ b/src/platform_windows/windows_runtime_session.cpp @@ -0,0 +1,136 @@ +#include "pch.h" + +#include "platform_windows/windows_runtime_session.h" + +#include "app.h" +#include "log.h" +#include "platform_windows/windows_lifecycle_state.h" +#include "platform_windows/windows_main_window_session.h" +#include "platform_windows/windows_runtime_shell.h" +#include "platform_windows/windows_runtime_state.h" +#include "wacom.h" +#include "../resource.h" + +namespace pp::platform::windows { + +namespace { + +void register_touch_window(HWND hWnd) +{ + // link: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registertouchwindow + if (RegisterTouchWindow(hWnd, 0) == 0) + { + LOG("RegisterTouchWindow error: %s", GetLastErrorAsString().c_str()); + } +} + +void initialize_runtime_threads() +{ + auto* app = bound_app(); + auto* runtime = retained_bound_runtime(); + assert(runtime); + mark_lifecycle_running(); + runtime->render_thread_start(*app); + runtime->ui_thread_start(*app); +} + +void install_debug_gl_callbacks() +{ +#ifdef _DEBUG + glad_set_pre_callback(_pre_call_callback); + glad_set_post_callback(_post_call_callback); +#endif +} + +void initialize_wintab(HWND hWnd, bool sandboxed) +{ + auto* tablet = bound_wacom_tablet(); + if (!sandboxed) + { + LOG("init WinTab"); + tablet->init(hWnd); + } + else + { + LOG("SKIP init WinTab"); + } +} + +void set_main_window_icon(HWND hWnd) +{ + LOG("change icon"); + SendMessage( + hWnd, + WM_SETICON, + ICON_SMALL, + (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1))); +} + +void restore_window_placement(HWND hWnd, int show_command) +{ + WINDOWPLACEMENT wp; + GetWindowPlacement(hWnd, &wp); + wp.showCmd = show_command; + SetWindowPlacement(hWnd, &wp); +} + +void run_main_message_loop() +{ + MSG msg; + auto* app = bound_app(); + LOG("start main loop"); + while (lifecycle_is_running()) + { + auto present = app->animate || app->redraw ? + PeekMessage(&msg, 0, 0, 0, PM_REMOVE) : GetMessage(&msg, 0, 0, 0); + + if (msg.message == WM_QUIT) + mark_lifecycle_stopped(); + + if (present) + { + DispatchMessage(&msg); + TranslateMessage(&msg); + } + + drain_main_thread_tasks(); + } +} + +void shutdown_main_window_runtime(const MainWindowStartupState& startup, HINSTANCE hInst) +{ + retained_wacom_tablet().terminate(); + UnregisterClass(startup.window_class.lpszClassName, hInst); + LogRemote::I.stop(); +} + +} + +void run_main_window_runtime_session(const MainWindowStartupState& startup, bool start_in_vr, HINSTANCE instance, SplashScreen& splash) +{ + auto* app = bound_app(); + register_touch_window(retained_main_window_handle_ref()); + + wglMakeCurrent(NULL, NULL); + + initialize_runtime_threads(); + install_debug_gl_callbacks(); + initialize_wintab(retained_main_window_handle_ref(), retained_main_window_sandboxed()); + set_main_window_icon(retained_main_window_handle_ref()); + + app->ui_sync(); + restore_window_placement(retained_main_window_handle_ref(), startup.show_command); + + if (start_in_vr) + app->vr_start(); + + LOG("show main window"); + SetForegroundWindow(retained_main_window_handle_ref()); + + splash.dismiss(); + + run_main_message_loop(); + shutdown_main_window_runtime(startup, instance); +} + +} diff --git a/src/platform_windows/windows_runtime_session.h b/src/platform_windows/windows_runtime_session.h new file mode 100644 index 00000000..bf6dce68 --- /dev/null +++ b/src/platform_windows/windows_runtime_session.h @@ -0,0 +1,14 @@ +#pragma once + +#include "platform_windows/windows_bootstrap_helpers.h" +#include "platform_windows/windows_splash.h" + +namespace pp::platform::windows { + +void run_main_window_runtime_session( + const MainWindowStartupState& startup, + bool start_in_vr, + HINSTANCE instance, + SplashScreen& splash); + +}