UI: Add obs_frontend_close_main_window()
Description
Adds the ability to close the main window from the frontend API. The behaviour is the same as clicking the Exit button in OBS Studio.
Motivation and Context
This is useful for scripting, where OBS is mainly run from the system tray and controlled through the script, i.e. through obs-websocket. Currently, the only way to close OBS from a script is to have the OS terminate the process. This is obviously not ideal: the program doesn't get the chance to properly free up resources, and in the case of Windows, the icon hangs around the system tray until it is moused-over.
Link to feature suggestion: https://ideas.obsproject.com/posts/2225/allow-clean-shutdown-via-api
How Has This Been Tested?
OS: Windows CPU: Intel i7 12700K GPU: Gigabyte Nvidia GeForce RTX 3070 Ti 8 GB RAM: 32GB
For the sake of simplicity, I hijacked the StartStream handler of obs-websocket to instead call obs_frontend_close_main_window(), then used OBS-web to trigger the handler. The OBS Studio window closed, and the log indicated that the shutdown process completed nominally, just as it would have if the Exit button was clicked.
Types of changes
- New feature (non-breaking change which adds functionality)
- Documentation (a change to documentation pages)
Checklist:
- [x] My code has been run through clang-format.
- [x] I have read the contributing document.
- [x] My code is not on the master branch.
- [x] The code has been tested.
- [x] All commit messages are properly formatted and commits squashed where appropriate.
- [x] I have included updates to all appropriate documentation.
Is this supposed to end the application? Because on macOS an application is not automatically terminated just because it has no active windows - that’s an opt-in behaviour. It could be that Qt behaves this way in our case, but that needs to be verified.
For the use case that I'm targeting, yes, the intention is to end the application.
I just tested the release version of OBS on macOS, and indeed, clicking the Exit button and closing the window both end the application.
Is this supposed to end the application? Because on macOS an application is not automatically terminated just because it has no active windows - that’s an opt-in behaviour. It could be that Qt behaves this way in our case, but that needs to be verified.
Our main window's close event handler does some cleanup and ultimately calls qApplication::quit()
https://github.com/obsproject/obs-studio/blob/c289efd84a42887feba89c3579d39a9c264b749e/UI/window-basic-main.cpp#L5042
I'd like us to include a log line indicating when something has closed OBS this way.
How does this interact with things that prevent closing the window such as running outputs?
I'd like us to include a log line indicating when something has closed OBS this way.
How does this interact with things that prevent closing the window such as running outputs?
"Closes the main window. If there are active outputs, the user will be prompted to confirm before closing."
@Warchamp7 Log line added.
How does this interact with things that prevent closing the window such as running outputs?
This was answered in the next comment. Let me know if you have any more questions/issues.