chore: move cef initialization to its own context

This commit is contained in:
Almamu 2024-05-07 03:41:51 +02:00
parent 006d4ee3d0
commit 235cda8c94
12 changed files with 126 additions and 130 deletions

View File

@ -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(

View File

@ -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; i<argc; ++i)
{
argv2[i] = new char[strlen(argv[i])+1];
strcpy(argv2[i],argv[i]);
}
CEFsetUp(argc,argv);//Cef will launch new process with main(argc,argv) twice. If we won't pass argc and argv from main, we will create fork bomb and system will freeze until reboot.
//END of CEF init block
initLogging ();
WallpaperEngine::Application::CApplicationContext appContext (argc, argv2);
WallpaperEngine::Application::CWallpaperApplication app (appContext);
WallpaperEngine::WebBrowser::CWebBrowserContext webBrowserContext(argc, argv);
WallpaperEngine::Application::CApplicationContext appContext (argc, argv);
// halt if the list-properties option was specified
if (appContext.settings.general.onlyListProperties)
return 0;
WallpaperEngine::Application::CWallpaperApplication app (appContext, webBrowserContext);
appPointer = &app;
// attach signals to gracefully stop
std::signal (SIGINT, signalhandler);
std::signal (SIGTERM, signalhandler);
if(!g_CEFused){
sLog.debug("No web wallpapers, shutting down CEF");
CefShutdown();
}
// show the wallpaper application
app.show ();
if(g_CEFused){
sLog.debug("Shutting down CEF");
CefShutdown();
}
appPointer = nullptr;
return 0;
}

View File

@ -19,9 +19,10 @@ float g_TimeLast;
float g_Daytime;
namespace WallpaperEngine::Application {
CWallpaperApplication::CWallpaperApplication (CApplicationContext& context) :
CWallpaperApplication::CWallpaperApplication (CApplicationContext& context, WallpaperEngine::WebBrowser::CWebBrowserContext& browserContext) :
m_context (context),
m_defaultBackground (nullptr) {
m_defaultBackground (nullptr),
browserContext (browserContext) {
this->loadBackgrounds ();
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;

View File

@ -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

View File

@ -3,46 +3,6 @@
#include "common.h"
#include <utility>
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";

View File

@ -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<Core::CScene> ())
return new WallpaperEngine::Render::CScene (wallpaper->as<Core::CScene> (), context, audioContext, scalingMode);
if (wallpaper->is<Core::CVideo> ())
return new WallpaperEngine::Render::CVideo (wallpaper->as<Core::CVideo> (), context, audioContext, scalingMode);
else if (wallpaper->is<Core::CWeb> ())
return new WallpaperEngine::Render::CWeb (wallpaper->as<Core::CWeb> (), context, audioContext, scalingMode);
return new WallpaperEngine::Render::CWeb (wallpaper->as<Core::CWeb> (), context, audioContext, browserContext, scalingMode);
else
sLog.exception ("Unsupported wallpaper type");
}

View File

@ -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,

View File

@ -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 ();

View File

@ -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<CefBrowser> m_browser;
CefRefPtr<BrowserClient> m_client;
RenderHandler* m_render_handler = nullptr;

View File

@ -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();
}

View File

@ -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

View File

@ -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;