From 235cda8c9439ede14b9c402bf6f323b1bb58643d Mon Sep 17 00:00:00 2001 From: Almamu Date: Tue, 7 May 2024 03:41:51 +0200 Subject: [PATCH] chore: move cef initialization to its own context --- CMakeLists.txt | 4 +- main.cpp | 74 ++---------------- .../Application/CWallpaperApplication.cpp | 14 +++- .../Application/CWallpaperApplication.h | 4 +- src/WallpaperEngine/Core/Wallpapers/CWeb.cpp | 47 ------------ src/WallpaperEngine/Render/CWallpaper.cpp | 4 +- src/WallpaperEngine/Render/CWallpaper.h | 5 +- .../Render/Wallpapers/CWeb.cpp | 4 + src/WallpaperEngine/Render/Wallpapers/CWeb.h | 5 +- .../WebBrowsesr/CWebBrowserContext.cpp | 75 +++++++++++++++++++ .../WebBrowsesr/CWebBrowserContext.h | 17 +++++ src/common.h | 3 - 12 files changed, 126 insertions(+), 130 deletions(-) create mode 100644 src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.cpp create mode 100644 src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.h diff --git a/CMakeLists.txt b/CMakeLists.txt index efc831a..c44a769 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,7 +152,9 @@ if(WAYLAND_SUPPORT_FOUND) "src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.cpp" "src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.h" "xdg-shell-protocol.c" - "wlr-layer-shell-unstable-v1-protocol.c") + "wlr-layer-shell-unstable-v1-protocol.c" + src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.cpp + src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.h) endif() add_executable( diff --git a/main.cpp b/main.cpp index a7e7399..7f79682 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "WallpaperEngine/Application/CApplicationContext.h" #include "WallpaperEngine/Application/CWallpaperApplication.h" #include "WallpaperEngine/Core/Wallpapers/CWeb.h" +#include "WallpaperEngine/WebBrowsesr/CWebBrowserContext.h" #include "common.h" WallpaperEngine::Application::CWallpaperApplication* appPointer; @@ -22,91 +23,26 @@ void initLogging () sLog.addError (new std::ostream (std::cerr.rdbuf ())); } -static void CEFsetUp(int argc, char** argv) -{ - // This function should be called from the application entry point function to - // execute a secondary process. It can be used to run secondary processes from - // the browser client executable (default behavior) or from a separate - // executable specified by the CefSettings.browser_subprocess_path value. If - // called for the browser process (identified by no "type" command-line value) - // it will return immediately with a value of -1. If called for a recognized - // secondary process it will block until the process should exit and then return - // the process exit code. The |application| parameter may be empty. The - // |windows_sandbox_info| parameter is only used on Windows and may be NULL (see - // cef_sandbox_win.h for details). - - CefMainArgs args(argc, argv); - - int exit_code = CefExecuteProcess(args, nullptr, nullptr);//Spawned processes will terminate here(see CefIninitilize below). Maybe implementing settings.browser_subprocess_path will allow it to work not in main function. - if (exit_code >= 0) - { - // Sub proccess has endend, so exit - exit(exit_code); - } - else if (exit_code == -1) - { - // If called for the browser process (identified by no "type" command-line value) - // it will return immediately with a value of -1 - } - - // Configurate Chromium - CefSettings settings; - //CefString(&settings.locales_dir_path) = "OffScreenCEF/godot/locales"; - //CefString(&settings.resources_dir_path) = "OffScreenCEF/godot/"; - //CefString(&settings.framework_dir_path) = "OffScreenCEF/godot/"; - //CefString(&settings.cache_path) = "OffScreenCEF/godot/"; - // CefString(&settings.browser_subprocess_path) = "path/to/client" - settings.windowless_rendering_enabled = true; -#if defined(CEF_NO_SANDBOX) - settings.no_sandbox = true; -#endif - - bool result = CefInitialize(args, settings, nullptr, nullptr); //Spawn 2 new processes; Can be moved to Core::CWeb - if (!result) - { - std::cerr << "CefInitialize: failed" << std::endl; - exit(-2); - } -} - -bool g_CEFused=false;//Will be set to true if wallpaper has "web" type int main (int argc, char* argv[]) { - //START of CEF init block(it will run 3 times) - char** argv2 = new char*[argc]; //Cef modify argv on CefInit, copy it before that - - for(int i=0; iloadBackgrounds (); this->setupProperties (); } @@ -294,15 +295,20 @@ void CWallpaperApplication::show () { // set all the specific wallpapers required for (const auto& [background, info] : this->m_backgrounds) context->setWallpaper (background, WallpaperEngine::Render::CWallpaper::fromWallpaper ( - info->getWallpaper (), *context, *audioContext, + info->getWallpaper (), *context, *audioContext, browserContext, this->m_context.settings.general.screenScalings [background])); // set the default rendering wallpaper if available if (this->m_defaultBackground != nullptr) context->setDefaultWallpaper (WallpaperEngine::Render::CWallpaper::fromWallpaper ( - this->m_defaultBackground->getWallpaper (), *context, *audioContext, + this->m_defaultBackground->getWallpaper (), *context, *audioContext, browserContext, this->m_context.settings.render.window.scalingMode)); + // wallpapers are setup, free browsesr context if possible + if (!this->browserContext.isUsed()) { + this->browserContext.stop(); + } + static time_t seconds; static struct tm* timeinfo; diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.h b/src/WallpaperEngine/Application/CWallpaperApplication.h index 120c93a..43a2b26 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.h +++ b/src/WallpaperEngine/Application/CWallpaperApplication.h @@ -27,6 +27,7 @@ #include "WallpaperEngine/Audio/Drivers/CSDLAudioDriver.h" #include "WallpaperEngine/Input/CInputContext.h" +#include "WallpaperEngine/WebBrowsesr/CWebBrowserContext.h" namespace WallpaperEngine::Application { /** @@ -36,7 +37,7 @@ namespace WallpaperEngine::Application { */ class CWallpaperApplication { public: - explicit CWallpaperApplication (CApplicationContext& context); + explicit CWallpaperApplication (CApplicationContext& context, WallpaperEngine::WebBrowser::CWebBrowserContext& browserContext); ~CWallpaperApplication (); /** @@ -122,5 +123,6 @@ class CWallpaperApplication { WallpaperEngine::Audio::Drivers::CSDLAudioDriver* audioDriver; WallpaperEngine::Render::CRenderContext* context; WallpaperEngine::Audio::CAudioContext* audioContext; + WallpaperEngine::WebBrowser::CWebBrowserContext& browserContext; }; } // namespace WallpaperEngine::Application diff --git a/src/WallpaperEngine/Core/Wallpapers/CWeb.cpp b/src/WallpaperEngine/Core/Wallpapers/CWeb.cpp index 6bd9a9d..ab857be 100644 --- a/src/WallpaperEngine/Core/Wallpapers/CWeb.cpp +++ b/src/WallpaperEngine/Core/Wallpapers/CWeb.cpp @@ -3,46 +3,6 @@ #include "common.h" #include -static void CEFsetUp (int argc, char** argv) { - // This function should be called from the application entry point function to - // execute a secondary process. It can be used to run secondary processes from - // the browser client executable (default behavior) or from a separate - // executable specified by the CefSettings.browser_subprocess_path value. If - // called for the browser process (identified by no "type" command-line value) - // it will return immediately with a value of -1. If called for a recognized - // secondary process it will block until the process should exit and then return - // the process exit code. The |application| parameter may be empty. The - // |windows_sandbox_info| parameter is only used on Windows and may be NULL (see - // cef_sandbox_win.h for details). - CefMainArgs args (argc, argv); - int exit_code = CefExecuteProcess (args, nullptr, nullptr); - if (exit_code >= 0) { - sLog.debug ("CEF sub proccess has endend"); - // Sub proccess has endend, so exit - exit (exit_code); - } else if (exit_code == -1) { - // If called for the browser process (identified by no "type" command-line value) - // it will return immediately with a value of -1 - } - - // Configurate Chromium - CefSettings settings; - // CefString(&settings.locales_dir_path) = "OffScreenCEF/godot/locales"; - // CefString(&settings.resources_dir_path) = "OffScreenCEF/godot/"; - // CefString(&settings.framework_dir_path) = "OffScreenCEF/godot/"; - // CefString(&settings.cache_path) = "OffScreenCEF/godot/"; - settings.windowless_rendering_enabled = true; -#if defined(CEF_NO_SANDBOX) - settings.no_sandbox = true; -#endif - - bool result = CefInitialize (args, settings, nullptr, nullptr); - if (!result) { - sLog.error ("CefInitialize: failed"); - exit (-2); - } -} - using namespace WallpaperEngine::Core; const std::string& CWeb::getFilename () { @@ -50,13 +10,6 @@ const std::string& CWeb::getFilename () { } CWeb::CWeb (std::string filename, CProject& project) : CWallpaper (Type, project), m_filename (std::move (filename)) { - if (!g_CEFused) { - sLog.debug ("Setting up CEF"); - // char** argv = new char*("linux-wallpaper\n"); - // CEFsetUp(1, argv); - // delete argv; - g_CEFused = true; - } } const std::string CWeb::Type = "web"; diff --git a/src/WallpaperEngine/Render/CWallpaper.cpp b/src/WallpaperEngine/Render/CWallpaper.cpp index 3b0e1ac..4032ba9 100644 --- a/src/WallpaperEngine/Render/CWallpaper.cpp +++ b/src/WallpaperEngine/Render/CWallpaper.cpp @@ -280,14 +280,14 @@ CFBO* CWallpaper::getFBO () const { } CWallpaper* CWallpaper::fromWallpaper (Core::CWallpaper* wallpaper, CRenderContext& context, - CAudioContext& audioContext, + CAudioContext& audioContext, CWebBrowserContext& browserContext, const CWallpaperState::TextureUVsScaling& scalingMode) { if (wallpaper->is ()) return new WallpaperEngine::Render::CScene (wallpaper->as (), context, audioContext, scalingMode); if (wallpaper->is ()) return new WallpaperEngine::Render::CVideo (wallpaper->as (), context, audioContext, scalingMode); else if (wallpaper->is ()) - return new WallpaperEngine::Render::CWeb (wallpaper->as (), context, audioContext, scalingMode); + return new WallpaperEngine::Render::CWeb (wallpaper->as (), context, audioContext, browserContext, scalingMode); else sLog.exception ("Unsupported wallpaper type"); } \ No newline at end of file diff --git a/src/WallpaperEngine/Render/CWallpaper.h b/src/WallpaperEngine/Render/CWallpaper.h index 79b935e..bf52342 100644 --- a/src/WallpaperEngine/Render/CWallpaper.h +++ b/src/WallpaperEngine/Render/CWallpaper.h @@ -14,10 +14,13 @@ #include "WallpaperEngine/Render/CRenderContext.h" #include "WallpaperEngine/Render/Helpers/CContextAware.h" +#include "WallpaperEngine/WebBrowsesr/CWebBrowserContext.h" + #include "CWallpaperState.h" using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Audio; +using namespace WallpaperEngine::WebBrowser; namespace WallpaperEngine::Render { namespace Helpers { @@ -138,7 +141,7 @@ class CWallpaper : public Helpers::CContextAware { * @return */ static CWallpaper* fromWallpaper (Core::CWallpaper* wallpaper, CRenderContext& context, CAudioContext& audioContext, - const CWallpaperState::TextureUVsScaling& scalingMode); + CWebBrowserContext& browserContext, const CWallpaperState::TextureUVsScaling& scalingMode); protected: CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CRenderContext& context, CAudioContext& audioContext, diff --git a/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp b/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp index 92017dc..214968f 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp +++ b/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp @@ -4,14 +4,18 @@ #include "CWeb.h" using namespace WallpaperEngine::Render; +using namespace WallpaperEngine::WebBrowser; CWeb::CWeb (Core::CWeb* web, CRenderContext& context, CAudioContext& audioContext, + CWebBrowserContext& browserContext, const CWallpaperState::TextureUVsScaling& scalingMode) : CWallpaper (web, Type, context, audioContext, scalingMode), m_width (context.getOutput ().getFullWidth ()), m_height (context.getOutput ().getFullHeight ()), + m_browserContext (browserContext), m_browser (), m_client () { + this->m_browserContext.markAsUsed(); // setup framebuffers this->setupFramebuffers (); diff --git a/src/WallpaperEngine/Render/Wallpapers/CWeb.h b/src/WallpaperEngine/Render/Wallpapers/CWeb.h index ec1ae03..566d669 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CWeb.h +++ b/src/WallpaperEngine/Render/Wallpapers/CWeb.h @@ -19,7 +19,7 @@ namespace WallpaperEngine::Render class CWeb : public CWallpaper { public: - CWeb (Core::CWeb* scene, CRenderContext& context, CAudioContext& audioContext, const CWallpaperState::TextureUVsScaling& scalingMode); + CWeb (Core::CWeb* scene, CRenderContext& context, CAudioContext& audioContext, WallpaperEngine::WebBrowser::CWebBrowserContext& browserContext, const CWallpaperState::TextureUVsScaling& scalingMode); ~CWeb(); uint32_t getWidth () const override { return this->m_width; } @@ -103,7 +103,8 @@ namespace WallpaperEngine::Render IMPLEMENT_REFCOUNTING(BrowserClient); }; - + + WallpaperEngine::WebBrowser::CWebBrowserContext& m_browserContext; CefRefPtr m_browser; CefRefPtr m_client; RenderHandler* m_render_handler = nullptr; diff --git a/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.cpp b/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.cpp new file mode 100644 index 0000000..111430e --- /dev/null +++ b/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.cpp @@ -0,0 +1,75 @@ +#include "CWebBrowserContext.h" +#include "WallpaperEngine/Logging/CLog.h" +#include "include/cef_app.h" +#include "include/cef_client.h" +#include "include/cef_render_handler.h" + +using namespace WallpaperEngine::WebBrowser; + +CWebBrowserContext::CWebBrowserContext (int argc, char** argv) : m_stopped(false) { + // clone original argc/argv as they'll be modified by cef + char** argv2 = new char*[argc]; + + for(int i = 0; i < argc; i ++) { + argv2[i] = new char[strlen(argv[i]) + 1]; + strcpy(argv2[i], argv[i]); + } + + CefMainArgs args(argc, argv2); + + int exit_code = CefExecuteProcess(args, nullptr, nullptr);//Spawned processes will terminate here(see CefIninitilize below). Maybe implementing settings.browser_subprocess_path will allow it to work not in main function. + if (exit_code >= 0) + { + // Sub proccess has endend, so exit + exit(exit_code); + } + else if (exit_code == -1) + { + // If called for the browser process (identified by no "type" command-line value) + // it will return immediately with a value of -1 + } + + // Configurate Chromium + CefSettings settings; + //CefString(&settings.locales_dir_path) = "OffScreenCEF/godot/locales"; + //CefString(&settings.resources_dir_path) = "OffScreenCEF/godot/"; + //CefString(&settings.framework_dir_path) = "OffScreenCEF/godot/"; + //CefString(&settings.cache_path) = "OffScreenCEF/godot/"; + // CefString(&settings.browser_subprocess_path) = "path/to/client" + settings.windowless_rendering_enabled = true; +#if defined(CEF_NO_SANDBOX) + settings.no_sandbox = true; +#endif + + // spawns two new processess + bool result = CefInitialize(args, settings, nullptr, nullptr); + + if (!result) + { + sLog.exception ("CefInitialize: failed"); + } +} + +CWebBrowserContext::~CWebBrowserContext() { + this->stop(); +} + +void CWebBrowserContext::markAsUsed () { + this->m_inUse = true; +} + +bool CWebBrowserContext::isUsed () { + return this->m_inUse; +} + +void CWebBrowserContext::stop() { + if (this->m_stopped) { + return; + } + + sLog.out("Shutting down CEF"); + + this->m_stopped = true; + + CefShutdown(); +} \ No newline at end of file diff --git a/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.h b/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.h new file mode 100644 index 0000000..d689ff1 --- /dev/null +++ b/src/WallpaperEngine/WebBrowsesr/CWebBrowserContext.h @@ -0,0 +1,17 @@ +#pragma once + +namespace WallpaperEngine::WebBrowser { + class CWebBrowserContext { + public: + CWebBrowserContext (int argc, char** argv); + ~CWebBrowserContext(); + + void markAsUsed(); + bool isUsed(); + void stop(); + + private: + bool m_stopped; + bool m_inUse; + }; +} // namespace WallpaperEngine::WebBrowser diff --git a/src/common.h b/src/common.h index c0eb7ce..324d7e4 100644 --- a/src/common.h +++ b/src/common.h @@ -3,6 +3,3 @@ #include "WallpaperEngine/Core/Wallpapers/CWeb.h" #include "WallpaperEngine/Logging/CLog.h" -//global variables are bad -extern bool g_CEFused; -extern CefMainArgs g_args; \ No newline at end of file