@@ -2461,10 +2461,13 @@ LRESULT CALLBACK LLWindowWin32::mainWindowProc(HWND h_wnd, UINT u_msg, WPARAM w_
24612461 case WM_CLOSE:
24622462 {
24632463 LL_PROFILE_ZONE_NAMED_CATEGORY_WIN32 (" mwp - WM_CLOSE" );
2464+ // todo: WM_CLOSE can be caused by user and by task manager,
2465+ // distinguish these cases.
2466+ // For now assume it is always user.
24642467 window_imp->post ([=]()
24652468 {
24662469 // Will the app allow the window to close?
2467- if (window_imp->mCallbacks ->handleCloseRequest (window_imp))
2470+ if (window_imp->mCallbacks ->handleCloseRequest (window_imp, true ))
24682471 {
24692472 // Get the app to initiate cleanup.
24702473 window_imp->mCallbacks ->handleQuit (window_imp);
@@ -2482,6 +2485,50 @@ LRESULT CALLBACK LLWindowWin32::mainWindowProc(HWND h_wnd, UINT u_msg, WPARAM w_
24822485 }
24832486 return 0 ;
24842487 }
2488+ case WM_QUERYENDSESSION:
2489+ {
2490+ // Generally means that OS is going to shut down or user is going to log off.
2491+ // Can use ShutdownBlockReasonCreate here.
2492+ LL_INFOS (" Window" ) << " Received WM_QUERYENDSESSION with wParam: " << (U32)w_param << " lParam: " << (U32)l_param << LL_ENDL;
2493+ return TRUE ; // 1 = ok to end session. 0 no longer works by itself, use ShutdownBlockReasonCreate
2494+ }
2495+ case WM_ENDSESSION:
2496+ {
2497+ // OS session is shutting down, initiate cleanup.
2498+ // Comes after WM_QUERYENDSESSION
2499+ LL_PROFILE_ZONE_NAMED_CATEGORY_WIN32 (" mwp - WM_ENDSESSION" );
2500+ LL_INFOS (" Window" ) << " Received WM_ENDSESSION with wParam: " << (U32)w_param << " lParam: " << (U32)l_param << LL_ENDL;
2501+ unsigned int end_session_flags = (U32)w_param;
2502+ if (end_session_flags == 0 )
2503+ {
2504+ // session is not actually ending
2505+ return 0 ;
2506+ }
2507+
2508+ if ((end_session_flags & ENDSESSION_CLOSEAPP)
2509+ || (end_session_flags & ENDSESSION_CRITICAL)
2510+ || (end_session_flags & ENDSESSION_LOGOFF))
2511+ {
2512+ window_imp->post ([=]()
2513+ {
2514+ // Check if app needs cleanup or can be closed immediately.
2515+ if (window_imp->mCallbacks ->handleSessionExit (window_imp))
2516+ {
2517+ // Get the app to initiate cleanup.
2518+ window_imp->mCallbacks ->handleQuit (window_imp);
2519+ // The app is responsible for calling destroyWindow when done with GL
2520+ }
2521+ });
2522+ // Give app a second to finish up. That's not enough for a clean exit,
2523+ // but better than nothing.
2524+ // Todo: sync this better, some kind of waitForResult? Can't wait forever,
2525+ // but can potentially use ShutdownBlockReasonCreate for a bigger delay.
2526+ ms_sleep (1000 );
2527+ }
2528+ // Don't need to post quit or destroy window,
2529+ // if session is ending OS is going to take care of it.
2530+ return 0 ;
2531+ }
24852532 case WM_COMMAND:
24862533 {
24872534 LL_PROFILE_ZONE_NAMED_CATEGORY_WIN32 (" mwp - WM_COMMAND" );
0 commit comments