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.
This commit is contained in:
Campbell Barton 2023-05-30 12:44:31 +10:00
parent be4a9fe4df
commit d7d1c524e3
5 changed files with 10 additions and 13 deletions

@ -429,8 +429,7 @@ extern "C" int GHOST_HACK_getFirstFile(char buf[FIRSTFILEBUFLG])
- (void)applicationWillTerminate:(NSNotification *)aNotification - (void)applicationWillTerminate:(NSNotification *)aNotification
{ {
#if 0 #if 0
G.is_break = false; /* Let Cocoa perform the termination at the end. */ WM_exit(C, EXIT_SUCCESS);
WM_exit(C);
#endif #endif
} }

@ -108,8 +108,11 @@ void WM_exit_ex(struct bContext *C, bool do_python);
* \brief Main exit function to close Blender ordinarily. * \brief Main exit function to close Blender ordinarily.
* \note Use #wm_exit_schedule_delayed() to close Blender from an operator. * \note Use #wm_exit_schedule_delayed() to close Blender from an operator.
* Might leak memory otherwise. * 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; void WM_main(struct bContext *C) ATTR_NORETURN;

@ -450,10 +450,7 @@ static void wait_for_console_key(void)
static int wm_exit_handler(bContext *C, const wmEvent *event, void *userdata) 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). */ WM_exit(C, EXIT_SUCCESS);
G.is_break = false;
WM_exit(C);
UNUSED_VARS(event, userdata); UNUSED_VARS(event, userdata);
return WM_UI_HANDLER_BREAK; return WM_UI_HANDLER_BREAK;
@ -693,7 +690,7 @@ void WM_exit_ex(bContext *C, const bool do_python)
CLG_exit(); CLG_exit();
} }
void WM_exit(bContext *C) void WM_exit(bContext *C, const int exit_code)
{ {
WM_exit_ex(C, true); WM_exit_ex(C, true);
@ -707,7 +704,7 @@ void WM_exit(bContext *C)
} }
#endif #endif
exit(G.is_break == true); exit(exit_code);
} }
void WM_script_tag_reload(void) void WM_script_tag_reload(void)

@ -574,7 +574,7 @@ int main(int argc,
#ifndef WITH_PYTHON_MODULE #ifndef WITH_PYTHON_MODULE
if (G.background) { if (G.background) {
/* Using window-manager API in background-mode is a bit odd, but works fine. */ /* 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 { else {
/* Shows the splash as needed. */ /* Shows the splash as needed. */

@ -2271,9 +2271,7 @@ static bool handle_load_file(bContext *C, const char *filepath_arg, const bool l
if (error_msg) { if (error_msg) {
fprintf(stderr, "Error: %s, exiting! %s\n", error_msg, filepath); fprintf(stderr, "Error: %s, exiting! %s\n", error_msg, filepath);
WM_exit(C, EXIT_FAILURE);
G.is_break = true;
WM_exit(C);
/* Unreachable, return for clarity. */ /* Unreachable, return for clarity. */
return false; return false;
} }