diff --git a/native/cocos/application/CocosApplication.cpp b/native/cocos/application/CocosApplication.cpp index 638735b279..53dea7afbf 100644 --- a/native/cocos/application/CocosApplication.cpp +++ b/native/cocos/application/CocosApplication.cpp @@ -40,8 +40,8 @@ namespace cc { CocosApplication::CocosApplication() { _engine = BaseEngine::createEngine(); - _systemWidow = _engine->getInterface(); - CC_ASSERT(_systemWidow != nullptr); + _systemWindow = _engine->getInterface(); + CC_ASSERT(_systemWindow != nullptr); } CocosApplication::~CocosApplication() { @@ -78,7 +78,7 @@ int CocosApplication::init() { se->start(); #if (CC_PLATFORM == CC_PLATFORM_IOS) - auto logicSize = _systemWidow->getViewSize(); + auto logicSize = _systemWindow->getViewSize(); IScreen *screen = _engine->getInterface(); float pixelRatio = screen->getDevicePixelRatio(); cc::EventDispatcher::dispatchResizeEvent(logicSize.x * pixelRatio, logicSize.y * pixelRatio); @@ -103,9 +103,9 @@ void CocosApplication::resume() { void CocosApplication::restart() { _engine->restart(); } - +// IMPORTANT!!The method `onClose` is a function to be listen close event, while `close` is a jsb binding method mean to close the whole application. void CocosApplication::close() { - _engine->close(); + _systemWindow->closeWindow(); } BaseEngine::Ptr CocosApplication::getEngine() const { @@ -121,7 +121,7 @@ void CocosApplication::onResume() { } void CocosApplication::onClose() { - close(); + _engine->close(); } void CocosApplication::setDebugIpAndPort(const ccstd::string &serverAddr, uint32_t port, bool isWaitForConnect) { @@ -144,13 +144,13 @@ void CocosApplication::setXXTeaKey(const ccstd::string &key) { #if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX || CC_PLATFORM == CC_PLATFORM_MACOS void CocosApplication::createWindow(const char *title, int32_t w, int32_t h, int32_t flags) { - _systemWidow->createWindow(title, w, h, flags); + _systemWindow->createWindow(title, w, h, flags); } void CocosApplication::createWindow(const char *title, int32_t x, int32_t y, int32_t w, int32_t h, int32_t flags) { - _systemWidow->createWindow(title, x, y, w, h, flags); + _systemWindow->createWindow(title, x, y, w, h, flags); } #endif diff --git a/native/cocos/application/CocosApplication.h b/native/cocos/application/CocosApplication.h index e0be79b375..df961ab6b0 100644 --- a/native/cocos/application/CocosApplication.h +++ b/native/cocos/application/CocosApplication.h @@ -123,7 +123,7 @@ public: virtual void setXXTeaKey(const ccstd::string &key); private: - ISystemWindow *_systemWidow{nullptr}; + ISystemWindow *_systemWindow{nullptr}; BaseEngine::Ptr _engine{nullptr}; }; } // namespace cc diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index 0e0049aff0..ca7c0974aa 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -60,6 +60,7 @@ #include "core/assets/FreeTypeFont.h" #include "network/HttpClient.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/UniversalPlatform.h" #if CC_USE_DEBUG_RENDERER #include "profiler/DebugRenderer.h" #endif @@ -209,6 +210,7 @@ int Engine::restart() { } void Engine::close() { // NOLINT + #if CC_USE_AUDIO cc::AudioEngine::stopAll(); #endif @@ -220,11 +222,10 @@ void Engine::close() { // NOLINT cc::DeferredReleasePool::clear(); _scheduler->removeAllFunctionsToBePerformedInCocosThread(); _scheduler->unscheduleAll(); - + cc::EventDispatcher::dispatchCloseEvent(); BasePlatform::getPlatform()->setHandleEventCallback(nullptr); - // TODO(timlyeee): The code below is a hack on v3.6, and should be replaced in the future. - exit(0); + } uint Engine::getTotalFrames() const { diff --git a/native/cocos/platform/SDLHelper.cpp b/native/cocos/platform/SDLHelper.cpp index 804e86e02d..337c285cb6 100644 --- a/native/cocos/platform/SDLHelper.cpp +++ b/native/cocos/platform/SDLHelper.cpp @@ -398,4 +398,8 @@ uintptr_t SDLHelper::getWindowHandler() const { return 0; } +SDL_Window* SDLHelper::getSDLWindowHandler() const { + return _handle; +} + } // namespace cc diff --git a/native/cocos/platform/SDLHelper.h b/native/cocos/platform/SDLHelper.h index bbf64303c1..8ac5286ddc 100644 --- a/native/cocos/platform/SDLHelper.h +++ b/native/cocos/platform/SDLHelper.h @@ -53,6 +53,7 @@ public: uintptr_t getDisplay() const; #endif void setCursorEnabled(bool value); + SDL_Window* getSDLWindowHandler() const; private: void dispatchSDLEvent(const SDL_Event& sdlEvent, bool* quit); diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 9b28172b5c..f8fc193004 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -545,7 +545,7 @@ void AndroidPlatform::pollEvent() { // } -void *AndroidPlatform::getActivity() { +void *AndroidPlatform::getActivity() { // Dangerous return _app->activity->javaGameActivity; } diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java index 8f873ad79c..94afb55ab3 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java @@ -385,7 +385,14 @@ public class CocosHelper { } return new float[]{0, 0, 0, 0}; } - + public static void finishActivity() { + sActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + sActivity.finish(); + } + }); + } public static void setKeepScreenOn(boolean keepScreenOn) { if (keepScreenOn) { sActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); diff --git a/native/cocos/platform/interfaces/modules/ISystemWindow.h b/native/cocos/platform/interfaces/modules/ISystemWindow.h index 1621537f57..8c2eb1be4f 100644 --- a/native/cocos/platform/interfaces/modules/ISystemWindow.h +++ b/native/cocos/platform/interfaces/modules/ISystemWindow.h @@ -87,6 +87,7 @@ public: int w, int h, int flags) { return true; } + virtual void closeWindow() {} virtual uintptr_t getWindowHandler() const = 0; virtual Size getViewSize() const = 0; virtual void setViewSize(uint32_t width, uint32_t height) {} diff --git a/native/cocos/platform/ios/IOSPlatform.h b/native/cocos/platform/ios/IOSPlatform.h index 6ba4b308b0..30f3ebe25e 100644 --- a/native/cocos/platform/ios/IOSPlatform.h +++ b/native/cocos/platform/ios/IOSPlatform.h @@ -49,8 +49,8 @@ public: * @brief Implement the main logic of the base platform. */ int32_t loop() override; - void setFps(int32_t fps) override; + int32_t getFps() const override; void onPause() override; diff --git a/native/cocos/platform/ios/modules/SystemWindow.h b/native/cocos/platform/ios/modules/SystemWindow.h index a523fe99f4..384068b821 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.h +++ b/native/cocos/platform/ios/modules/SystemWindow.h @@ -35,7 +35,7 @@ class SystemWindow : public ISystemWindow { public: SystemWindow() = default; ~SystemWindow() override; - + void closeWindow() override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; diff --git a/native/cocos/platform/ios/modules/SystemWindow.mm b/native/cocos/platform/ios/modules/SystemWindow.mm index aa8c8fd121..5b933ff52a 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.mm +++ b/native/cocos/platform/ios/modules/SystemWindow.mm @@ -41,7 +41,11 @@ void SystemWindow::copyTextToClipboard(const std::string& text) { UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding]; } - +void SystemWindow::closeWindow() { + // Force quit as there's no API to exit UIApplication + cc::EventDispatcher::dispatchCloseEvent(); + exit(0); +} uintptr_t SystemWindow::getWindowHandler() const { return reinterpret_cast(UIApplication.sharedApplication.delegate.window.rootViewController.view); } diff --git a/native/cocos/platform/java/jni/JniImp.cpp b/native/cocos/platform/java/jni/JniImp.cpp index 546f6b311d..422879449d 100644 --- a/native/cocos/platform/java/jni/JniImp.cpp +++ b/native/cocos/platform/java/jni/JniImp.cpp @@ -115,6 +115,9 @@ void setVibrateJNI(float duration) { JniHelper::callStaticVoidMethod(JCLS_HELPER, "vibrate", duration); } +void finishActivity() { + JniHelper::callStaticVoidMethod(JCLS_HELPER, "finishActivity"); +} int getNetworkTypeJNI() { return JniHelper::callStaticIntMethod(JCLS_HELPER, "getNetworkType"); } diff --git a/native/cocos/platform/java/jni/JniImp.h b/native/cocos/platform/java/jni/JniImp.h index c18bd741ac..82939a3556 100644 --- a/native/cocos/platform/java/jni/JniImp.h +++ b/native/cocos/platform/java/jni/JniImp.h @@ -45,4 +45,5 @@ extern void flushTasksOnGameThreadJNI(); extern void flushTasksOnGameThreadAtForegroundJNI(); extern void setAccelerometerEnabledJNI(bool isEnabled); extern void setAccelerometerIntervalJNI(float interval); -extern float *getDeviceMotionValueJNI(); \ No newline at end of file +extern float *getDeviceMotionValueJNI(); +extern void finishActivity(); diff --git a/native/cocos/platform/java/modules/SystemWindow.cpp b/native/cocos/platform/java/modules/SystemWindow.cpp index ad9464bd09..a119821def 100644 --- a/native/cocos/platform/java/modules/SystemWindow.cpp +++ b/native/cocos/platform/java/modules/SystemWindow.cpp @@ -74,5 +74,12 @@ SystemWindow::Size SystemWindow::getViewSize() const { static_cast(JNI_NATIVE_GLUE()->getHeight())}; #endif } - +void SystemWindow::closeWindow() { +#if (CC_PLATFORM == CC_PLATFORM_ANDROID) + finishActivity(); +#else + cc::EventDispatcher::dispatchCloseEvent(); + exit(0); //TODO(cc): better exit for ohos +#endif +} } // namespace cc diff --git a/native/cocos/platform/java/modules/SystemWindow.h b/native/cocos/platform/java/modules/SystemWindow.h index 3303369733..fb2a44729e 100644 --- a/native/cocos/platform/java/modules/SystemWindow.h +++ b/native/cocos/platform/java/modules/SystemWindow.h @@ -40,6 +40,7 @@ public: void copyTextToClipboard(const std::string& text) override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; + void closeWindow() override; }; } // namespace cc diff --git a/native/cocos/platform/linux/modules/SystemWindow.cpp b/native/cocos/platform/linux/modules/SystemWindow.cpp index 918531e3c2..bcc3af7a0d 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.cpp +++ b/native/cocos/platform/linux/modules/SystemWindow.cpp @@ -75,10 +75,24 @@ bool SystemWindow::createWindow(const char *title, return true; } +void SystemWindow::closeWindow() { + #ifndef CC_SERVER_MODE + auto windowHandle = getSDLWindowHandler(); + + SDL_Event et; + et.type = SDL_QUIT; + auto posted = SDL_PushEvent(&et); +#endif +} + uintptr_t SystemWindow::getWindowHandler() const { return _sdl->getWindowHandler(); } +SDL_Window* SystemWindow::getSDLWindowHandler() const { + return _sdl->getSDLWindowHandler(); +} + uintptr_t SystemWindow::getDisplay() const { return _sdl->getDisplay(); } diff --git a/native/cocos/platform/linux/modules/SystemWindow.h b/native/cocos/platform/linux/modules/SystemWindow.h index c92cfc8c23..7b723465ba 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.h +++ b/native/cocos/platform/linux/modules/SystemWindow.h @@ -29,6 +29,7 @@ #include "platform/interfaces/modules/ISystemWindow.h" +struct SDL_Window; namespace cc { class SDLHelper; class CC_DLL SystemWindow : public ISystemWindow { @@ -45,7 +46,9 @@ public: bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; + void closeWindow() override; uintptr_t getWindowHandler() const override; + SDL_Window* getSDLWindowHandler() const; uintptr_t getDisplay() const; Size getViewSize() const override; void setViewSize(uint32_t w, uint32_t h) override { diff --git a/native/cocos/platform/mac/AppDelegate.h b/native/cocos/platform/mac/AppDelegate.h index 091d21e669..be43f02dc7 100644 --- a/native/cocos/platform/mac/AppDelegate.h +++ b/native/cocos/platform/mac/AppDelegate.h @@ -32,4 +32,5 @@ - (void)windowWillCloseNotification; - (void)applicationWillTerminate:(NSNotification*)aNotification; - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication; +- (NSWindow*) getWindow; @end diff --git a/native/cocos/platform/mac/modules/SystemWindow.h b/native/cocos/platform/mac/modules/SystemWindow.h index 6879347397..2d794c9d57 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.h +++ b/native/cocos/platform/mac/modules/SystemWindow.h @@ -41,7 +41,7 @@ public: int h, int flags) override; bool createWindow(const char* title, int w, int h, int flags) override; - + void closeWindow() override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 0277947063..d5e47f35da 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -65,7 +65,12 @@ bool SystemWindow::createWindow(const char *title, [delegate createWindow:aString xPos:x yPos:y width:w height:h]; return true; } - +void SystemWindow::closeWindow() { + id window = [[[NSApplication sharedApplication] delegate] getWindow]; + if (window) { + [window close]; + } +} void SystemWindow::setCursorEnabled(bool value) { } diff --git a/native/cocos/platform/win32/modules/SystemWindow.cpp b/native/cocos/platform/win32/modules/SystemWindow.cpp index b045cb1be3..5baddf5793 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.cpp +++ b/native/cocos/platform/win32/modules/SystemWindow.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "platform/win32/modules/SystemWindow.h" +#include #include #include "base/Log.h" #include "bindings/event/EventDispatcher.h" @@ -68,7 +69,12 @@ bool SystemWindow::createWindow(const char *title, _height = h; return true; } - +void SystemWindow::closeWindow() { + HWND windowHandle = reinterpret_cast(getWindowHandler()); + if (windowHandle != 0) { + ::SendMessageA(windowHandle, WM_CLOSE, 0, 0); + } +} uintptr_t SystemWindow::getWindowHandler() const { return _sdl->getWindowHandler(); } diff --git a/native/cocos/platform/win32/modules/SystemWindow.h b/native/cocos/platform/win32/modules/SystemWindow.h index 4399e9eee8..e7513314e1 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.h +++ b/native/cocos/platform/win32/modules/SystemWindow.h @@ -45,6 +45,7 @@ public: bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; + void closeWindow() override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; void setViewSize(uint32_t width, uint32_t height) override {