CMake: Simplify X11 dependencies

This commit is contained in:
Stenzek 2023-09-16 15:01:13 +10:00 committed by Connor McLaughlin
parent 6123ef12bd
commit bd54729257
5 changed files with 27 additions and 114 deletions

View File

@ -25,7 +25,6 @@ if(UNIX AND NOT APPLE)
option(USE_LEGACY_USER_DIRECTORY "Use legacy home/PCSX2 user directory instead of XDG standard" OFF)
option(X11_API "Enable X11 support" ON)
option(WAYLAND_API "Enable Wayland support" ON)
option(DBUS_API "Enable DBus support for screensaver inhibiting" ON)
endif()
if(UNIX)

View File

@ -59,11 +59,6 @@ else()
if(USE_OPENGL)
check_lib(EGL EGL EGL/egl.h)
endif()
if(X11_API)
check_lib(X11_XCB X11-xcb X11/Xlib-xcb.h)
check_lib(XCB xcb xcb/xcb.h)
check_lib(XRANDR Xrandr X11/extensions/Xrandr.h)
endif()
if(Linux)
check_lib(AIO aio libaio.h)
@ -77,11 +72,13 @@ else()
check_lib(LIBUDEV libudev libudev.h)
endif()
endif()
endif()
if(UNIX AND NOT APPLE)
find_package(X11 REQUIRED)
make_imported_target_if_missing(X11::X11 X11)
if(X11_API)
find_package(X11 REQUIRED)
if (NOT X11_Xrandr_FOUND)
message(FATAL_ERROR "XRandR extension is required")
endif()
endif()
if(WAYLAND_API)
find_package(ECM REQUIRED NO_MODULE)
@ -90,6 +87,8 @@ else()
endif()
find_package(Libbacktrace)
find_package(PkgConfig REQUIRED)
pkg_check_modules(DBUS REQUIRED dbus-1)
endif()
endif(WIN32)

View File

@ -163,17 +163,13 @@ if(APPLE)
)
endif()
if(DBUS_API)
target_compile_definitions(common PRIVATE DBUS_API)
find_package(PkgConfig REQUIRED)
pkg_check_modules(DBUS REQUIRED dbus-1)
if(UNIX AND NOT APPLE)
target_include_directories(common PRIVATE ${DBUS_INCLUDE_DIRS})
target_link_libraries(common PRIVATE ${DBUS_LINK_LIBRARIES})
endif()
if(X11_API AND TARGET PkgConfig::XRANDR)
target_link_libraries(common PRIVATE PkgConfig::XRANDR)
target_compile_definitions(common PRIVATE "HAS_XRANDR=1")
target_link_libraries(common PRIVATE ${DBUS_LINK_LIBRARIES} X11::X11 X11::Xrandr)
if(TARGET libbacktrace::libbacktrace)
target_compile_definitions(common PRIVATE "HAS_LIBBACKTRACE=1")
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
endif()
endif()
if (USE_GCC AND CMAKE_INTERPROCEDURAL_OPTIMIZATION)
@ -190,11 +186,6 @@ if(NOT WIN32)
target_link_libraries(common PRIVATE CURL::libcurl)
endif()
if(UNIX AND NOT APPLE AND TARGET libbacktrace::libbacktrace)
target_compile_definitions(common PRIVATE "HAS_LIBBACKTRACE=1")
target_link_libraries(common PRIVATE libbacktrace::libbacktrace)
endif()
target_link_libraries(common PRIVATE
${LIBC_LIBRARIES}
PNG::PNG

View File

@ -14,16 +14,6 @@
*/
#if !defined(_WIN32) && !defined(__APPLE__)
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <optional>
#include <spawn.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fmt/core.h"
#include "common/Pcsx2Types.h"
#include "common/General.h"
@ -32,9 +22,17 @@
#include "common/Threading.h"
#include "common/WindowInfo.h"
#ifdef DBUS_API
#include "fmt/core.h"
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <optional>
#include <spawn.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dbus/dbus.h>
#endif
// Returns 0 on failure (not supported by the operating system).
u64 GetPhysicalMemory()
@ -69,8 +67,6 @@ std::string GetOSVersionString()
#endif
}
#ifdef DBUS_API
static bool SetScreensaverInhibitDBus(const bool inhibit_requested, const char* program_name, const char* reason)
{
static dbus_uint32_t s_cookie;
@ -139,81 +135,9 @@ static bool SetScreensaverInhibitDBus(const bool inhibit_requested, const char*
return true;
}
#endif
#if !defined(DBUS_API) && defined(X11_API)
static bool SetScreensaverInhibitX11(const WindowInfo& wi, bool inhibit)
{
extern char** environ;
const char* command = "xdg-screensaver";
const char* operation = inhibit ? "suspend" : "resume";
std::string id = fmt::format("0x{:X}", static_cast<u64>(reinterpret_cast<uintptr_t>(wi.window_handle)));
char* argv[4] = {const_cast<char*>(command), const_cast<char*>(operation), const_cast<char*>(id.c_str()),
nullptr};
// Since we set SA_NOCLDWAIT in Qt, we don't need to wait here.
pid_t pid;
int res = posix_spawnp(&pid, "xdg-screensaver", nullptr, nullptr, argv, environ);
return (res == 0);
}
static bool SetScreensaverInhibit(const WindowInfo& wi, bool inhibit)
{
switch (wi.type)
{
#ifdef X11_API
case WindowInfo::Type::X11:
return SetScreensaverInhibitX11(wi, inhibit);
#endif
default:
return false;
}
}
static std::optional<WindowInfo> s_inhibit_window_info;
#endif
bool WindowInfo::InhibitScreensaver(const WindowInfo& wi, bool inhibit)
{
#ifdef DBUS_API
return SetScreensaverInhibitDBus(inhibit, "PCSX2", "PCSX2 VM is running.");
#else
if (s_inhibit_window_info.has_value())
{
// Bit of extra logic here, because wx spams it and we don't want to
// spawn processes unnecessarily.
if (s_inhibit_window_info->type == wi.type &&
s_inhibit_window_info->window_handle == wi.window_handle &&
s_inhibit_window_info->surface_handle == wi.surface_handle)
{
return true;
}
// Clear the old.
SetScreensaverInhibit(s_inhibit_window_info.value(), false);
s_inhibit_window_info.reset();
}
if (!inhibit)
return true;
// New window.
if (!SetScreensaverInhibit(wi, true))
return false;
s_inhibit_window_info = wi;
return true;
#endif
}
bool Common::PlaySoundAsync(const char* path)

View File

@ -80,7 +80,7 @@ bool WindowInfo::QueryRefreshRateForWindow(const WindowInfo& wi, float* refresh_
#else
#if defined(X11_API) && defined(HAS_XRANDR)
#if defined(X11_API)
#include "common/ScopedGuard.h"
#include <X11/extensions/Xrandr.h>
@ -169,11 +169,11 @@ static bool GetRefreshRateFromXRandR(const WindowInfo& wi, float* refresh_rate)
return true;
}
#endif // X11_API && defined(HAS_XRANDR)
#endif // X11_API
bool WindowInfo::QueryRefreshRateForWindow(const WindowInfo& wi, float* refresh_rate)
{
#if defined(X11_API) && defined(HAS_XRANDR)
#if defined(X11_API)
if (wi.type == WindowInfo::Type::X11)
return GetRefreshRateFromXRandR(wi, refresh_rate);
#endif