Skip to content

Commit

Permalink
feat: add option USE_QT_EVENT_LOOP to support external message pump
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion committed May 29, 2024
1 parent 931323b commit 3d0371d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
90 changes: 59 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,46 @@ cmake_minimum_required(VERSION 3.18)
project(QCefView)

# arguments:
# CEF_SDK_VERSION: specify the CEF version to be used
# QT_SDK_DIR: specify the Qt SDK path
# CEF_SDK_VERSION:
# - specify the CEF version to be used,
# - refer to: cmake\CefViewCoreConfig.cmake
#
# QT_SDK_DIR:
# - specify the Qt SDK path
# - refer to: cmake\QtConfig.cmake
#

# options
option(BUILD_DEMO "Build the demo" OFF)
option(BUILD_STATIC "Build QCefView as static library" OFF)
option(USE_SANDBOX "Enable CEF Sandbox" OFF)
option(STATIC_CRT "Use MultiThreaded linkage for MSVC" OFF)
option(BUILD_DEMO
"Build the demo"
OFF
)
option(BUILD_STATIC
"Build QCefView as static library"
OFF
)
option(STATIC_CRT
"Use MultiThreaded linkage for MSVC"
OFF
)
option(USE_SANDBOX
"Enable CEF Sandbox"
OFF
)

# Only works for Windows & Linux, always enabled on macOS
# If enable then:
# CefSettings.multi_threaded_message_loop = false && CefSettings.external_message_pump = true
# else:
# CefSettings.multi_threaded_message_loop = true && CefSettings.external_message_pump = false
option(USE_QT_EVENT_LOOP
"Enable the integration of CEF message loop thread into Qt event loop"
OFF
)

# append cmake config module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# detect whether we are in sub folder
get_directory_property(QCefView_HAS_PARENT_DIRECTORY PARENT_DIRECTORY)

# Only generate Debug and Release configuration types.
set(CMAKE_CONFIGURATION_TYPES Debug Release)

Expand Down Expand Up @@ -55,6 +80,11 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(OS_MACOS 1)
set(OS_POSIX 1)
add_definitions(-DOS_MACOS=1 -DOS_POSIX=1)
add_compile_options(
"-g"
"$<$<CONFIG:DEBUG>:-O0>"
"$<$<CONFIG:RELEASE>:-O3>"
)

# Target architecture.
if(PROJECT_ARCH STREQUAL "x86_64")
Expand All @@ -68,6 +98,11 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(OS_LINUX 1)
set(OS_POSIX 1)
add_definitions(-DOS_LINUX=1 -DOS_POSIX=1)
add_compile_options(
"-g"
"$<$<CONFIG:DEBUG>:-O0>"
"$<$<CONFIG:RELEASE>:-O3>"
)

# Target architecture.
if(PROJECT_ARCH STREQUAL "x86_64")
Expand All @@ -81,7 +116,12 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
endif()
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(OS_WINDOWS 1)

# Disable the sandbox on Windows, because the sandbox.lib is MT which is conflict with Qt
set(USE_SANDBOX OFF CACHE BOOL "Disable sandbox on Windows" FORCE)
add_definitions(-DOS_WINDOWS=1)
add_compile_options("/M$<IF:$<BOOL:${STATIC_CRT}>,T,D>$<$<CONFIG:Debug>:d>")
add_link_options(/DEBUG)
endif()

if(BUILD_STATIC)
Expand All @@ -91,6 +131,16 @@ else()
set(QCEFVIEW_LIB_TYPE SHARED)
endif()

# detect whether we are in sub folder
get_directory_property(QCefView_HAS_PARENT_DIRECTORY PARENT_DIRECTORY)

if(NOT QCefView_HAS_PARENT_DIRECTORY)
message(STATUS "QCefView is not in subdirectory, put all output together")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/lib)
endif()

# Config the QT package
# ##############################################################
set(QT_SDK_DIR "" CACHE PATH "Qt build toolchain path")
Expand All @@ -101,28 +151,6 @@ set(Qt_VERSION ${Qt${QT_VERSION_MAJOR}Core_VERSION})

# Config the CEF
# ##############################################################
if(OS_WINDOWS)
add_link_options(/DEBUG)

# Disable the sandbox on Windows, because the sandbox.lib is MT which
# is conflict with Qt
set(USE_SANDBOX OFF CACHE BOOL "Disable sandbox on Windows" FORCE)
add_compile_options("/M$<IF:$<BOOL:${STATIC_CRT}>,T,D>$<$<CONFIG:Debug>:d>")
else()
add_compile_options(
"-g"
"$<$<CONFIG:DEBUG>:-O0>"
"$<$<CONFIG:RELEASE>:-O3>"
)
endif()

if(NOT QCefView_HAS_PARENT_DIRECTORY)
message(STATUS "QCefView is not in subdirectory, put all output together")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/$<CONFIG>/lib)
endif()

# Fetch CefViewCore
include(CefViewCoreConfig)

Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ if(USE_SANDBOX AND(OS_WINDOWS OR OS_MACOS))
add_definitions(-DCEF_USE_SANDBOX)
endif()

if(USE_QT_EVENT_LOOP)
add_definitions(-DCEF_USE_QT_EVENT_LOOP)
endif()

if(OS_WINDOWS)
file(GLOB_RECURSE QCefView_Windows_SRC_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/win/*.h"
Expand Down
2 changes: 1 addition & 1 deletion src/details/QCefContextPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ QCefContextPrivate::QCefContextPrivate(QCoreApplication* app, int argc, char** a
: argc_(argc)
, argv_(argv)
{
#if defined(Q_OS_MACOS)
#if defined(Q_OS_MACOS) || defined(CEF_USE_QT_EVENT_LOOP)
cefWorkerTimer_.setTimerType(Qt::PreciseTimer);
cefWorkerTimer_.start(kCefWorkerIntervalMs);
connect(&cefWorkerTimer_, SIGNAL(timeout()), this, SLOT(performCefLoopWork()));
Expand Down
2 changes: 1 addition & 1 deletion src/details/QCefContextPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class QCefContextPrivate : public QObject
QList<FolderResourceMapping> folderResourceMappingList_;
QList<ArchiveResourceMapping> archiveResourceMappingList_;

#if defined(Q_OS_MACOS)
#if defined(Q_OS_MACOS) || defined(CEF_USE_QT_EVENT_LOOP)
QTimer cefWorkerTimer_;
#endif

Expand Down
6 changes: 6 additions & 0 deletions src/linux/details/QCefContextPrivate_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ QCefContextPrivate::initializeCef(const QCefConfig* config)

// fixed values
cef_settings.pack_loading_disabled = false;

#if defined(CEF_USE_QT_EVENT_LOOP)
cef_settings.multi_threaded_message_loop = false;
cef_settings.external_message_pump = true;
#else
cef_settings.multi_threaded_message_loop = true;
cef_settings.external_message_pump = false;
#endif

#if !defined(CEF_USE_SANDBOX)
cef_settings.no_sandbox = true;
Expand Down
6 changes: 6 additions & 0 deletions src/win/details/QCefContextPrivate_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ QCefContextPrivate::initializeCef(const QCefConfig* config)

// fixed values
cef_settings.pack_loading_disabled = false;

#if defined(CEF_USE_QT_EVENT_LOOP)
cef_settings.multi_threaded_message_loop = false;
cef_settings.external_message_pump = true;
#else
cef_settings.multi_threaded_message_loop = true;
cef_settings.external_message_pump = false;
#endif

#if !defined(CEF_USE_SANDBOX)
cef_settings.no_sandbox = true;
Expand Down

0 comments on commit 3d0371d

Please sign in to comment.