From ee86fc40bde7022f51f0215f74aca5bb63d5e4d2 Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Wed, 15 Feb 2023 17:11:28 +0800 Subject: [PATCH] Native platform modifies exit process (#14141) --- @types/pal/system-info.d.ts | 8 +++- cocos/game/game.ts | 13 ++++++ native/cocos/bindings/auto/jsb_cocos_auto.cpp | 2 +- native/cocos/bindings/manual/jsb_global.cpp | 7 +++ native/cocos/engine/Engine.cpp | 2 + native/cocos/platform/BasePlatform.h | 5 +++ native/cocos/platform/SDLHelper.cpp | 5 +-- native/cocos/platform/SDLHelper.h | 36 ++++++++-------- native/cocos/platform/UniversalPlatform.cpp | 4 ++ native/cocos/platform/UniversalPlatform.h | 5 +++ .../platform/android/AndroidPlatform.cpp | 15 +++++-- .../cocos/platform/android/AndroidPlatform.h | 4 +- .../empty/modules/SystemWindowManager.cpp | 2 +- .../empty/modules/SystemWindowManager.h | 2 +- .../interfaces/modules/ISystemWindowManager.h | 36 ++++++++-------- native/cocos/platform/ios/IOSPlatform.h | 8 +++- native/cocos/platform/ios/IOSPlatform.mm | 26 +++++++++++ .../platform/ios/modules/SystemWindow.mm | 6 ++- .../ios/modules/SystemWindowManager.h | 2 +- .../java/modules/SystemWindowManager.cpp | 2 +- .../java/modules/SystemWindowManager.h | 2 +- native/cocos/platform/linux/LinuxPlatform.cpp | 6 ++- native/cocos/platform/linux/LinuxPlatform.h | 1 + .../linux/modules/SystemWindowManager.cpp | 43 +++++++++---------- .../linux/modules/SystemWindowManager.h | 36 ++++++++-------- native/cocos/platform/mac/AppDelegate.mm | 5 +++ native/cocos/platform/mac/MacPlatform.h | 4 ++ native/cocos/platform/mac/MacPlatform.mm | 15 +++++++ .../mac/modules/SystemWindowManager.h | 2 +- native/cocos/platform/ohos/OhosPlatform.cpp | 4 ++ native/cocos/platform/ohos/OhosPlatform.h | 1 + .../cocos/platform/win32/WindowsPlatform.cpp | 8 +++- native/cocos/platform/win32/WindowsPlatform.h | 2 + .../win32/modules/SystemWindowManager.cpp | 9 ++-- .../win32/modules/SystemWindowManager.h | 2 +- native/external-config.json | 2 +- pal/system-info/native/system-info.ts | 5 +++ 37 files changed, 228 insertions(+), 109 deletions(-) diff --git a/@types/pal/system-info.d.ts b/@types/pal/system-info.d.ts index fb3a00c9b7..4cb322b7af 100644 --- a/@types/pal/system-info.d.ts +++ b/@types/pal/system-info.d.ts @@ -48,8 +48,14 @@ declare module 'pal/system-info' { public openURL (url: string): void; public now (): number; public restartJSVM (): void; - + /* + * Trigger to exit the game + */ public close (): void; + /* + * Free the native's resources + */ + public exit(): void; on (event: PalSystemEvent, cb: (...args: any)=>void, target?: any): void; off (event: PalSystemEvent, cb?: (...args: any)=>void, target?: any): void; diff --git a/cocos/game/game.ts b/cocos/game/game.ts index ee79c7d4f8..a12330345b 100644 --- a/cocos/game/game.ts +++ b/cocos/game/game.ts @@ -306,6 +306,12 @@ export class Game extends EventTarget { */ public static readonly EVENT_RESUME = 'game_on_resume'; + /** + * @en Triggered when the game will be closed.
+ * @zh 游戏将要关闭时触发的事件。
+ */ + public static readonly EVENT_CLOSE = 'game_on_close'; + /** * @en Web Canvas 2d API as renderer backend. * @zh 使用 Web Canvas 2d API 作为渲染器后端。 @@ -1027,6 +1033,7 @@ export class Game extends EventTarget { private _initEvents () { systemInfo.on('show', this._onShow, this); systemInfo.on('hide', this._onHide, this); + systemInfo.on('close', this._onClose, this); } private _onHide () { @@ -1039,6 +1046,12 @@ export class Game extends EventTarget { this.resumeByEngine(); } + private _onClose () { + this.emit(Game.EVENT_CLOSE); + // TODO : Release Resources. + systemInfo.exit(); + } + // @ Persist root node section /** * @en diff --git a/native/cocos/bindings/auto/jsb_cocos_auto.cpp b/native/cocos/bindings/auto/jsb_cocos_auto.cpp index d8fbe78e71..177d44f8e1 100644 --- a/native/cocos/bindings/auto/jsb_cocos_auto.cpp +++ b/native/cocos/bindings/auto/jsb_cocos_auto.cpp @@ -4616,7 +4616,7 @@ static bool js_cc_ISystemWindowManager_processEvent(se::State& s) ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); SE_PRECONDITION2(ok, false, "Error processing arguments"); - (arg1)->processEvent(arg2); + (arg1)->processEvent(); return true; diff --git a/native/cocos/bindings/manual/jsb_global.cpp b/native/cocos/bindings/manual/jsb_global.cpp index e92d0b4f08..bf0a051d03 100644 --- a/native/cocos/bindings/manual/jsb_global.cpp +++ b/native/cocos/bindings/manual/jsb_global.cpp @@ -386,6 +386,12 @@ static bool JSB_closeWindow(se::State &s) { } SE_BIND_FUNC(JSB_closeWindow) +static bool JSB_exit(se::State &s) { + BasePlatform::getPlatform()->exit(); + return true; +} +SE_BIND_FUNC(JSB_exit); + static bool JSB_isObjectValid(se::State &s) { // NOLINT const auto &args = s.args(); int argc = static_cast(args.size()); @@ -1431,6 +1437,7 @@ bool jsb_register_global_variables(se::Object *global) { // NOLINT global->defineFunction("__restartVM", _SE(JSB_core_restartVM)); global->defineFunction("__close", _SE(JSB_closeWindow)); global->defineFunction("__isObjectValid", _SE(JSB_isObjectValid)); + global->defineFunction("__exit", _SE(JSB_exit)); se::HandleObject performanceObj(se::Object::createPlainObject()); performanceObj->defineFunction("now", _SE(js_performance_now)); diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index 03b96121de..bd20a3951f 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -338,6 +338,8 @@ bool Engine::redirectWindowEvent(const WindowEvent &ev) { } else if (ev.type == WindowEvent::Type::CLOSE) { emit(ON_CLOSE); events::Close::broadcast(); + // Increase the frame rate and get the program to exit as quickly as possible + setPreferredFramesPerSecond(1000); isHandled = true; } else if (ev.type == WindowEvent::Type::QUIT) { // There is no need to process the quit message, diff --git a/native/cocos/platform/BasePlatform.h b/native/cocos/platform/BasePlatform.h index 27a79907ae..e9a7b05985 100644 --- a/native/cocos/platform/BasePlatform.h +++ b/native/cocos/platform/BasePlatform.h @@ -68,6 +68,11 @@ public: */ virtual void pollEvent() = 0; + /** + * @brief Exit platform. + */ + virtual void exit() = 0; + /** * @brief Get target system type. */ diff --git a/native/cocos/platform/SDLHelper.cpp b/native/cocos/platform/SDLHelper.cpp index 02fdeea4e9..ee9fc72717 100644 --- a/native/cocos/platform/SDLHelper.cpp +++ b/native/cocos/platform/SDLHelper.cpp @@ -210,7 +210,7 @@ void SDLHelper::dispatchWindowEvent(uint32_t windowId, const SDL_WindowEvent &we } } -void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent, bool *quit) { +void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent) { cc::TouchEvent touch; cc::MouseEvent mouse; cc::KeyboardEvent keyboard; @@ -223,9 +223,6 @@ void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent, b switch (sdlEvent.type) { case SDL_QUIT: { - if (quit) { - *quit = true; - } WindowEvent ev; ev.type = WindowEvent::Type::QUIT; events::WindowEvent::broadcast(ev); diff --git a/native/cocos/platform/SDLHelper.h b/native/cocos/platform/SDLHelper.h index 8089d74be3..2f368a00b9 100644 --- a/native/cocos/platform/SDLHelper.h +++ b/native/cocos/platform/SDLHelper.h @@ -3,23 +3,23 @@ http://www.cocos.com - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - of the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. ****************************************************************************/ #pragma once @@ -54,7 +54,7 @@ public: static void setCursorEnabled(bool value); private: - static void dispatchSDLEvent(uint32_t windowId, const SDL_Event& sdlEvent, bool* quit); + static void dispatchSDLEvent(uint32_t windowId, const SDL_Event& sdlEvent); static void dispatchWindowEvent(uint32_t windowId, const SDL_WindowEvent& wevent); }; } // namespace cc diff --git a/native/cocos/platform/UniversalPlatform.cpp b/native/cocos/platform/UniversalPlatform.cpp index 578cfe60ad..36b149f75f 100644 --- a/native/cocos/platform/UniversalPlatform.cpp +++ b/native/cocos/platform/UniversalPlatform.cpp @@ -79,4 +79,8 @@ void UniversalPlatform::onDestroy() { cocos_destory(); } +void UniversalPlatform::exit() { + +} + } // namespace cc diff --git a/native/cocos/platform/UniversalPlatform.h b/native/cocos/platform/UniversalPlatform.h index 1c022ce8f7..0b6762c94e 100644 --- a/native/cocos/platform/UniversalPlatform.h +++ b/native/cocos/platform/UniversalPlatform.h @@ -80,6 +80,11 @@ public: */ virtual void onDestroy(); + /** + * @brief Exit platform. + */ + void exit() override; + private: ThreadCallback _mainTask{nullptr}; diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 303c2cfffb..da953da2c5 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -255,7 +255,6 @@ public: addTouchEvent(i, motionEvent); } } - events::Touch::broadcast(touchEvent); touchEvent.touches.clear(); return true; @@ -383,7 +382,6 @@ public: WindowEvent ev; ev.type = WindowEvent::Type::CLOSE; events::WindowEvent::broadcast(ev); - _androidPlatform->onDestroy(); break; } case APP_CMD_STOP: { @@ -583,6 +581,10 @@ int32_t AndroidPlatform::run(int /*argc*/, const char ** /*argv*/) { return 0; } +void AndroidPlatform::exit() { + _app->destroyRequested = 1; +} + int32_t AndroidPlatform::loop() { IXRInterface *xr = getInterface(); while (true) { @@ -599,10 +601,13 @@ int32_t AndroidPlatform::loop() { // Exit the game loop when the Activity is destroyed if (_app->destroyRequested) { - return 0; + break; } } - + // Exit the game loop when the Activity is destroyed + if (_app->destroyRequested) { + break; + } if (xr && !xr->platformLoopStart()) continue; _inputProxy->handleInput(); if (_inputProxy->isAnimating() && (xr ? xr->getXRConfig(xr::XRConfigKey::SESSION_RUNNING).getBool() : true)) { @@ -626,6 +631,8 @@ int32_t AndroidPlatform::loop() { #endif if (xr) xr->platformLoopEnd(); } + onDestroy(); + return 0; } void AndroidPlatform::pollEvent() { diff --git a/native/cocos/platform/android/AndroidPlatform.h b/native/cocos/platform/android/AndroidPlatform.h index 33af278111..de550027b4 100644 --- a/native/cocos/platform/android/AndroidPlatform.h +++ b/native/cocos/platform/android/AndroidPlatform.h @@ -47,7 +47,8 @@ public: int getSdkVersion() const override; int32_t loop() override; - + void exit() override; + void *getActivity(); static void *getEnv(); @@ -66,7 +67,6 @@ private: int _loopTimeOut{-1}; GameInputProxy *_inputProxy{nullptr}; android_app *_app{nullptr}; - friend class GameInputProxy; }; } // namespace cc diff --git a/native/cocos/platform/empty/modules/SystemWindowManager.cpp b/native/cocos/platform/empty/modules/SystemWindowManager.cpp index e8a09cb61d..e2025b59f8 100644 --- a/native/cocos/platform/empty/modules/SystemWindowManager.cpp +++ b/native/cocos/platform/empty/modules/SystemWindowManager.cpp @@ -33,7 +33,7 @@ int SystemWindowManager::init() { return 0; } -void SystemWindowManager::processEvent(bool *quit) { +void SystemWindowManager::processEvent() { } ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { diff --git a/native/cocos/platform/empty/modules/SystemWindowManager.h b/native/cocos/platform/empty/modules/SystemWindowManager.h index 30394b95db..f7d9768a04 100644 --- a/native/cocos/platform/empty/modules/SystemWindowManager.h +++ b/native/cocos/platform/empty/modules/SystemWindowManager.h @@ -36,7 +36,7 @@ public: explicit SystemWindowManager() = default; int init() override; - void processEvent(bool *quit) override; + void processEvent() override; ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; diff --git a/native/cocos/platform/interfaces/modules/ISystemWindowManager.h b/native/cocos/platform/interfaces/modules/ISystemWindowManager.h index 8e5f3377b6..c6ff903aae 100644 --- a/native/cocos/platform/interfaces/modules/ISystemWindowManager.h +++ b/native/cocos/platform/interfaces/modules/ISystemWindowManager.h @@ -3,23 +3,23 @@ http://www.cocos.com - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - of the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. ****************************************************************************/ #pragma once @@ -55,7 +55,7 @@ public: /** * Process messages at the PAL layer */ - virtual void processEvent(bool *quit) = 0; + virtual void processEvent() = 0; /** * Create an ISystemWindow object diff --git a/native/cocos/platform/ios/IOSPlatform.h b/native/cocos/platform/ios/IOSPlatform.h index e2a701e2b7..dd6ce26d64 100644 --- a/native/cocos/platform/ios/IOSPlatform.h +++ b/native/cocos/platform/ios/IOSPlatform.h @@ -44,6 +44,10 @@ public: * @brief Start base platform initialization. */ int32_t run(int argc, const char **argv) override; + + void requestExit(); + + void exit() override; /** * @brief Implement the main logic of the base platform. */ @@ -55,10 +59,12 @@ public: void onPause() override; void onResume() override; void onClose() override; - + void onDestroy() override; ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; private: + bool _requestExit{false}; + bool _quitLoop{false}; ThreadCallback _cb; }; diff --git a/native/cocos/platform/ios/IOSPlatform.mm b/native/cocos/platform/ios/IOSPlatform.mm index 2b7d8e9a72..ec3f477352 100644 --- a/native/cocos/platform/ios/IOSPlatform.mm +++ b/native/cocos/platform/ios/IOSPlatform.mm @@ -110,6 +110,16 @@ int32_t IOSPlatform::init() { return 0; } +void IOSPlatform::exit() { + if(_requestExit) { + // Manual quit requires a call to onDestory. + onDestroy(); + ::exit(0); + } else { + _quitLoop = true; + } +} + int32_t IOSPlatform::loop() { cocos_main(0, nullptr); [_timer start]; @@ -150,6 +160,22 @@ void IOSPlatform::onClose() { cc::events::WindowEvent::broadcast(ev); } +void IOSPlatform::requestExit() { + _requestExit = true; + onClose(); +} + +void IOSPlatform::onDestroy() { + if(!_requestExit) { + // ios exit process is special because it needs to wait for ts layer to destroy resources. + // The timer cannot be used here. + while (!_quitLoop) { + runTask(); + } + } + UniversalPlatform::onDestroy(); +} + ISystemWindow *IOSPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { return ccnew SystemWindow(windowId, externalHandle); } diff --git a/native/cocos/platform/ios/modules/SystemWindow.mm b/native/cocos/platform/ios/modules/SystemWindow.mm index 0efb75cf05..46a271b779 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.mm +++ b/native/cocos/platform/ios/modules/SystemWindow.mm @@ -26,6 +26,7 @@ #include "platform/ios/modules/SystemWindow.h" #import #include "platform/BasePlatform.h" +#include "platform/ios/IOSPlatform.h" #include "platform/interfaces/modules/IScreen.h" namespace cc { @@ -46,9 +47,10 @@ void SystemWindow::copyTextToClipboard(const std::string& text) { } void SystemWindow::closeWindow() { // Force quit as there's no API to exit UIApplication - cc::events::Close::broadcast(); - exit(0); + IOSPlatform* platform = dynamic_cast(BasePlatform::getPlatform()); + platform->requestExit(); } + uintptr_t SystemWindow::getWindowHandle() const { return reinterpret_cast(UIApplication.sharedApplication.delegate.window.rootViewController.view); } diff --git a/native/cocos/platform/ios/modules/SystemWindowManager.h b/native/cocos/platform/ios/modules/SystemWindowManager.h index 5950a81115..1580d3c93b 100644 --- a/native/cocos/platform/ios/modules/SystemWindowManager.h +++ b/native/cocos/platform/ios/modules/SystemWindowManager.h @@ -38,7 +38,7 @@ public: explicit SystemWindowManager() = default; int init() override { return 0; } - void processEvent(bool *quit) override {} + void processEvent() override {} ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; diff --git a/native/cocos/platform/java/modules/SystemWindowManager.cpp b/native/cocos/platform/java/modules/SystemWindowManager.cpp index a2e79fe22d..3591c1e430 100644 --- a/native/cocos/platform/java/modules/SystemWindowManager.cpp +++ b/native/cocos/platform/java/modules/SystemWindowManager.cpp @@ -32,7 +32,7 @@ int SystemWindowManager::init() { return 0; } -void SystemWindowManager::processEvent(bool *quit) { +void SystemWindowManager::processEvent() { } ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) { diff --git a/native/cocos/platform/java/modules/SystemWindowManager.h b/native/cocos/platform/java/modules/SystemWindowManager.h index 6190a10473..4c760c10af 100644 --- a/native/cocos/platform/java/modules/SystemWindowManager.h +++ b/native/cocos/platform/java/modules/SystemWindowManager.h @@ -37,7 +37,7 @@ public: SystemWindowManager() = default; int init() override; - void processEvent(bool *quit) override; + void processEvent() override; ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; const SystemWindowMap &getWindows() const override { return _windows; } diff --git a/native/cocos/platform/linux/LinuxPlatform.cpp b/native/cocos/platform/linux/LinuxPlatform.cpp index 73aa84abc3..b74a506ca9 100644 --- a/native/cocos/platform/linux/LinuxPlatform.cpp +++ b/native/cocos/platform/linux/LinuxPlatform.cpp @@ -83,6 +83,10 @@ static long getCurrentMillSecond() { return lLastTime; } +void LinuxPlatform::exit() { + _quit = true; +} + int32_t LinuxPlatform::loop() { long lastTime = 0L; long curTime = 0L; @@ -92,7 +96,7 @@ int32_t LinuxPlatform::loop() { while (!_quit) { curTime = getCurrentMillSecond(); desiredInterval = static_cast(1000.0 / getFps()); - _windowManager->processEvent(&_quit); + _windowManager->processEvent(); actualInterval = curTime - lastTime; if (actualInterval >= desiredInterval) { lastTime = getCurrentMillSecond(); diff --git a/native/cocos/platform/linux/LinuxPlatform.h b/native/cocos/platform/linux/LinuxPlatform.h index f627884a8f..df71a5201f 100644 --- a/native/cocos/platform/linux/LinuxPlatform.h +++ b/native/cocos/platform/linux/LinuxPlatform.h @@ -42,6 +42,7 @@ public: */ int32_t init() override; + void exit() override; int32_t loop() override; ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; diff --git a/native/cocos/platform/linux/modules/SystemWindowManager.cpp b/native/cocos/platform/linux/modules/SystemWindowManager.cpp index dc288827e3..a4f41c5ee4 100644 --- a/native/cocos/platform/linux/modules/SystemWindowManager.cpp +++ b/native/cocos/platform/linux/modules/SystemWindowManager.cpp @@ -3,23 +3,23 @@ http://www.cocos.com - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - of the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. ****************************************************************************/ #include "SystemWindowManager.h" @@ -35,21 +35,18 @@ int SystemWindowManager::init() { return SDLHelper::init(); } -void SystemWindowManager::processEvent(bool *quit) { +void SystemWindowManager::processEvent() { SDL_Event sdlEvent; while (SDL_PollEvent(&sdlEvent) != 0) { SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID); // SDL_Event like SDL_QUIT does not associate a window if (!sdlWindow) { - SDLHelper::dispatchSDLEvent(0, sdlEvent, quit); + SDLHelper::dispatchSDLEvent(0, sdlEvent); } else { ISystemWindow *window = getWindowFromSDLWindow(sdlWindow); CC_ASSERT(window); uint32_t windowId = window->getWindowId(); - SDLHelper::dispatchSDLEvent(windowId, sdlEvent, quit); - } - if (*quit) { - break; + SDLHelper::dispatchSDLEvent(windowId, sdlEvent); } } } diff --git a/native/cocos/platform/linux/modules/SystemWindowManager.h b/native/cocos/platform/linux/modules/SystemWindowManager.h index ab5488f0f2..6154069cf7 100644 --- a/native/cocos/platform/linux/modules/SystemWindowManager.h +++ b/native/cocos/platform/linux/modules/SystemWindowManager.h @@ -3,23 +3,23 @@ http://www.cocos.com - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - of the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. ****************************************************************************/ #pragma once @@ -38,7 +38,7 @@ public: SystemWindowManager() = default; int init() override; - void processEvent(bool *quit) override; + void processEvent() override; ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; diff --git a/native/cocos/platform/mac/AppDelegate.mm b/native/cocos/platform/mac/AppDelegate.mm index 0c69271678..40b85baf1a 100644 --- a/native/cocos/platform/mac/AppDelegate.mm +++ b/native/cocos/platform/mac/AppDelegate.mm @@ -104,6 +104,11 @@ return _window; } +- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender +{ + return _platform->readyToExit() ? NSTerminateNow : NSTerminateLater; +} + - (void)applicationWillTerminate:(NSNotification*)aNotification { // delete _game; //FIXME: will crash if relase it here. diff --git a/native/cocos/platform/mac/MacPlatform.h b/native/cocos/platform/mac/MacPlatform.h index 08fc7c7e30..13485718ca 100644 --- a/native/cocos/platform/mac/MacPlatform.h +++ b/native/cocos/platform/mac/MacPlatform.h @@ -47,6 +47,8 @@ public: ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; + bool readyToExit(); + void exit() override; /** * @brief Implement the main logic of the base platform. */ @@ -56,6 +58,8 @@ public: void onPause() override; void onResume() override; void onClose() override; +private: + bool _readyToExit{false}; }; } // namespace cc diff --git a/native/cocos/platform/mac/MacPlatform.mm b/native/cocos/platform/mac/MacPlatform.mm index e4719f905f..2266df306a 100644 --- a/native/cocos/platform/mac/MacPlatform.mm +++ b/native/cocos/platform/mac/MacPlatform.mm @@ -98,6 +98,10 @@ extern int cocos_main(int argc, const char **argv); - (void)renderScene { _platform->runTask(); } + +- (bool) isValid { + return [_timer valid]; +} #endif @end @@ -123,6 +127,17 @@ int32_t MacPlatform::init() { return 0; } +bool MacPlatform::readyToExit() { + return _readyToExit; +} + +void MacPlatform::exit() { + if(!_readyToExit) { + [[NSApplication sharedApplication] replyToApplicationShouldTerminate:true]; + _readyToExit = true; + } +} + int32_t MacPlatform::loop(void) { #if CC_EDITOR runTask(); diff --git a/native/cocos/platform/mac/modules/SystemWindowManager.h b/native/cocos/platform/mac/modules/SystemWindowManager.h index a253b646b3..b2fefa440f 100644 --- a/native/cocos/platform/mac/modules/SystemWindowManager.h +++ b/native/cocos/platform/mac/modules/SystemWindowManager.h @@ -39,7 +39,7 @@ public: explicit SystemWindowManager() = default; int init() override { return 0; } - void processEvent(bool *quit) override {} + void processEvent() override {} ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; diff --git a/native/cocos/platform/ohos/OhosPlatform.cpp b/native/cocos/platform/ohos/OhosPlatform.cpp index 3e8a80ef0f..574134629c 100644 --- a/native/cocos/platform/ohos/OhosPlatform.cpp +++ b/native/cocos/platform/ohos/OhosPlatform.cpp @@ -78,6 +78,10 @@ void OhosPlatform::waitWindowInitialized() { } } +void OhosPlatform::exit() { + +} + int32_t OhosPlatform::loop() { while (_jniNativeGlue->isRunning()) { pollEvent(); diff --git a/native/cocos/platform/ohos/OhosPlatform.h b/native/cocos/platform/ohos/OhosPlatform.h index c678c58bac..5a96942d32 100644 --- a/native/cocos/platform/ohos/OhosPlatform.h +++ b/native/cocos/platform/ohos/OhosPlatform.h @@ -36,6 +36,7 @@ public: int32_t run(int argc, const char **argv) override; int getSdkVersion() const override; int32_t loop() override; + void exit() override; ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; private: diff --git a/native/cocos/platform/win32/WindowsPlatform.cpp b/native/cocos/platform/win32/WindowsPlatform.cpp index b10d2691d1..b2b96b2c6a 100644 --- a/native/cocos/platform/win32/WindowsPlatform.cpp +++ b/native/cocos/platform/win32/WindowsPlatform.cpp @@ -113,9 +113,13 @@ int32_t WindowsPlatform::init() { return _windowManager->init(); } +void WindowsPlatform::exit() { + _quit = true; +} + int32_t WindowsPlatform::loop() { #if CC_EDITOR - _windowManager->processEvent(&_quit); + _windowManager->processEvent(); runTask(); #else /////////////////////////////////////////////////////////////////////////// @@ -147,7 +151,7 @@ int32_t WindowsPlatform::loop() { onResume(); while (!_quit) { desiredInterval = (LONGLONG)(1.0 / getFps() * nFreq.QuadPart); - _windowManager->processEvent(&_quit); + _windowManager->processEvent(); QueryPerformanceCounter(&nNow); actualInterval = nNow.QuadPart - nLast.QuadPart; diff --git a/native/cocos/platform/win32/WindowsPlatform.h b/native/cocos/platform/win32/WindowsPlatform.h index bf4474f922..64e4330c25 100644 --- a/native/cocos/platform/win32/WindowsPlatform.h +++ b/native/cocos/platform/win32/WindowsPlatform.h @@ -45,6 +45,8 @@ public: int32_t loop() override; + void exit() override; + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; private: diff --git a/native/cocos/platform/win32/modules/SystemWindowManager.cpp b/native/cocos/platform/win32/modules/SystemWindowManager.cpp index 9b430e4abc..06ed4c2063 100644 --- a/native/cocos/platform/win32/modules/SystemWindowManager.cpp +++ b/native/cocos/platform/win32/modules/SystemWindowManager.cpp @@ -35,21 +35,18 @@ int SystemWindowManager::init() { return SDLHelper::init(); } -void SystemWindowManager::processEvent(bool *quit) { +void SystemWindowManager::processEvent() { SDL_Event sdlEvent; while (SDL_PollEvent(&sdlEvent) != 0) { SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID); // SDL_Event like SDL_QUIT does not associate a window if (!sdlWindow) { - SDLHelper::dispatchSDLEvent(0, sdlEvent, quit); + SDLHelper::dispatchSDLEvent(0, sdlEvent); } else { ISystemWindow *window = getWindowFromSDLWindow(sdlWindow); CC_ASSERT(window); uint32_t windowId = window->getWindowId(); - SDLHelper::dispatchSDLEvent(windowId, sdlEvent, quit); - } - if (*quit) { - break; + SDLHelper::dispatchSDLEvent(windowId, sdlEvent); } } } diff --git a/native/cocos/platform/win32/modules/SystemWindowManager.h b/native/cocos/platform/win32/modules/SystemWindowManager.h index 127725a881..5cd86f6b60 100644 --- a/native/cocos/platform/win32/modules/SystemWindowManager.h +++ b/native/cocos/platform/win32/modules/SystemWindowManager.h @@ -38,7 +38,7 @@ public: SystemWindowManager() = default; int init() override; - void processEvent(bool *quit) override; + void processEvent() override; ISystemWindow *createWindow(const ISystemWindowInfo &info) override; ISystemWindow *getWindow(uint32_t windowId) const override; diff --git a/native/external-config.json b/native/external-config.json index e1ebf81b17..f01551f391 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.7.1-2" + "checkout": "develop-13" } } diff --git a/pal/system-info/native/system-info.ts b/pal/system-info/native/system-info.ts index 9ade346624..aaceea38d8 100644 --- a/pal/system-info/native/system-info.ts +++ b/pal/system-info/native/system-info.ts @@ -173,6 +173,11 @@ class SystemInfo extends EventTarget { // @ts-expect-error __close() is defined in JSB __close(); } + + public exit () { + // @ts-expect-error __exit() is defined in JSB + __exit(); + } } export const systemInfo = new SystemInfo();