From d7d1c524e3dc1ac17577bb262ec033db8ae1b8f3 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 30 May 2023 12:44:31 +1000 Subject: [PATCH] Cleanup: pass the exit-code to WM_exit Callers to WM_exit needed to set G.is_break for a predictable exit-code. This is error prone as G.is_break may be set based on the user having pressed escape during event handling. Instead, pass the exit code as an argument. --- intern/ghost/intern/GHOST_SystemCocoa.mm | 3 +-- source/blender/windowmanager/WM_api.h | 5 ++++- source/blender/windowmanager/intern/wm_init_exit.cc | 9 +++------ source/creator/creator.c | 2 +- source/creator/creator_args.c | 4 +--- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.mm b/intern/ghost/intern/GHOST_SystemCocoa.mm index a06d0c43776..2408b709ab1 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.mm +++ b/intern/ghost/intern/GHOST_SystemCocoa.mm @@ -429,8 +429,7 @@ extern "C" int GHOST_HACK_getFirstFile(char buf[FIRSTFILEBUFLG]) - (void)applicationWillTerminate:(NSNotification *)aNotification { #if 0 - G.is_break = false; /* Let Cocoa perform the termination at the end. */ - WM_exit(C); + WM_exit(C, EXIT_SUCCESS); #endif } diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index 6ee75672e76..176fa1ff0ec 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -108,8 +108,11 @@ void WM_exit_ex(struct bContext *C, bool do_python); * \brief Main exit function to close Blender ordinarily. * \note Use #wm_exit_schedule_delayed() to close Blender from an operator. * Might leak memory otherwise. + * + * \param exit_code: Passed to #exit, typically #EXIT_SUCCESS or #EXIT_FAILURE should be used. + * With failure being used for an early exit when parsing command line arguments fails. */ -void WM_exit(struct bContext *C) ATTR_NORETURN; +void WM_exit(struct bContext *C, int exit_code) ATTR_NORETURN; void WM_main(struct bContext *C) ATTR_NORETURN; diff --git a/source/blender/windowmanager/intern/wm_init_exit.cc b/source/blender/windowmanager/intern/wm_init_exit.cc index c2fddfc2d0b..287088ad8da 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.cc +++ b/source/blender/windowmanager/intern/wm_init_exit.cc @@ -450,10 +450,7 @@ static void wait_for_console_key(void) static int wm_exit_handler(bContext *C, const wmEvent *event, void *userdata) { - /* Prevent a non-zero exit code (if escape was pressed by the user). */ - G.is_break = false; - - WM_exit(C); + WM_exit(C, EXIT_SUCCESS); UNUSED_VARS(event, userdata); return WM_UI_HANDLER_BREAK; @@ -693,7 +690,7 @@ void WM_exit_ex(bContext *C, const bool do_python) CLG_exit(); } -void WM_exit(bContext *C) +void WM_exit(bContext *C, const int exit_code) { WM_exit_ex(C, true); @@ -707,7 +704,7 @@ void WM_exit(bContext *C) } #endif - exit(G.is_break == true); + exit(exit_code); } void WM_script_tag_reload(void) diff --git a/source/creator/creator.c b/source/creator/creator.c index 6aad9785fb8..c43218c81e4 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -574,7 +574,7 @@ int main(int argc, #ifndef WITH_PYTHON_MODULE if (G.background) { /* Using window-manager API in background-mode is a bit odd, but works fine. */ - WM_exit(C); + WM_exit(C, G.is_break ? EXIT_FAILURE : EXIT_SUCCESS); } else { /* Shows the splash as needed. */ diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c index b1b3947910c..d41a2ff448c 100644 --- a/source/creator/creator_args.c +++ b/source/creator/creator_args.c @@ -2271,9 +2271,7 @@ static bool handle_load_file(bContext *C, const char *filepath_arg, const bool l if (error_msg) { fprintf(stderr, "Error: %s, exiting! %s\n", error_msg, filepath); - - G.is_break = true; - WM_exit(C); + WM_exit(C, EXIT_FAILURE); /* Unreachable, return for clarity. */ return false; }