| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- #include "X11FullScreenDetector.h"
- #include "WallpaperEngine/Logging/Log.h"
- #include "WallpaperEngine/Render/Drivers/X11WindowType.h"
- #include <algorithm>
- #include <cstdlib>
- #include <X11/Xatom.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/extensions/Xrandr.h>
- #include "WallpaperEngine/Render/Drivers/GLFWOpenGLDriver.h"
- #define GLFW_EXPOSE_NATIVE_X11
- #include "WallpaperEngine/Render/Drivers/VideoFactories.h"
- #include <GLFW/glfw3.h>
- #include <GLFW/glfw3native.h>
- namespace WallpaperEngine::Render::Drivers::Detectors {
- static void CustomXIOErrorExitHandler (Display* dsp, void* userdata) {
- const auto context = static_cast<X11FullScreenDetector*> (userdata);
- sLog.debugerror ("Critical XServer error detected. Attempting to recover...");
- context->reset ();
- }
- static int CustomXErrorHandler (Display* dpy, XErrorEvent* event) {
- sLog.debugerror ("Detected X error");
- return 0;
- }
- static int CustomXIOErrorHandler (Display* dsp) {
- sLog.debugerror ("Detected X error");
- return 0;
- }
- X11FullScreenDetector::X11FullScreenDetector (Application::ApplicationContext& appContext, VideoDriver& driver) :
- FullScreenDetector (appContext), m_display (nullptr), m_root (0), m_driver (driver) {
- try {
- // throws if m_driver isn't actually a GLFWOpenGLDriver, so we can catch the misuse
- std::ignore = dynamic_cast<GLFWOpenGLDriver&> (this->m_driver);
- } catch (std::exception&) {
- sLog.exception ("X11 FullScreen Detector initialized with the wrong video driver... This is a bug...");
- }
- // not chaining the previous handler, it could stop the app
- XSetErrorHandler (CustomXErrorHandler);
- XSetIOErrorHandler (CustomXIOErrorHandler);
- this->initialize ();
- if (std::getenv ("LWE_XWAYLAND_FALLBACK") != nullptr && Desktop::isPantheonSession ()) {
- this->m_gala = std::make_unique<Desktop::PantheonShell> ();
- if (!this->m_gala->isAvailable ()) {
- sLog.error ("Native Wayland windows are not seen, ", this->m_gala->reason ());
- this->m_gala.reset ();
- }
- }
- }
- X11FullScreenDetector::~X11FullScreenDetector () { this->stop (); }
- bool X11FullScreenDetector::anythingFullscreen () const {
- if (this->m_display == nullptr) {
- return false;
- }
- Window root, parent;
- Window* children = nullptr;
- unsigned int childCount = 0;
- if (!XQueryTree (this->m_display, this->m_root, &root, &parent, &children, &childCount)) {
- return false;
- }
- const Window ourWindow = glfwGetX11Window (dynamic_cast<GLFWOpenGLDriver&> (this->m_driver).getWindow ());
- const auto& render = this->getApplicationContext ().settings.render;
- bool fullscreen = false;
- for (unsigned int i = 0; i < childCount && !fullscreen && render.pauseOnFullscreen; i++) {
- XWindowAttributes attributes;
- if (!XGetWindowAttributes (this->m_display, children[i], &attributes) || attributes.map_state != IsViewable) {
- continue;
- }
- // only managed windows count, WM/compositor helpers and our desktop window cover the screen too
- const Window client = this->clientOf (children[i]);
- if (client == None || client == ourWindow || hasDesktopWindowType (this->m_display, client)
- || this->isHidden (client) || !this->onCurrentDesktop (client)) {
- continue;
- }
- fullscreen
- = this->hasFullscreenState (client) || this->hasMaximizedState (client) || this->coversScreen (client);
- }
- XFree (children);
- bool galaFocused = false;
- const bool galaCovers = this->galaWindowCovers (galaFocused);
- return fullscreen || (render.pauseOnFullscreen && galaCovers)
- || (render.pauseOnFocus && (this->applicationFocused (ourWindow) || galaFocused));
- }
- bool X11FullScreenDetector::galaWindowCovers (bool& focused) const {
- if (this->m_gala == nullptr) {
- return false;
- }
- this->m_gala->dispatch ();
- bool covers = false;
- for (const auto& window : this->m_gala->windows ()) {
- // our own background window is a window of Gala's too
- if (window.hidden || window.workspace != this->m_gala->activeWorkspace ()
- || window.wmClass == "linux-wallpaperengine") {
- continue;
- }
- focused = focused || window.focused;
- covers = covers || std::ranges::any_of (this->m_screens, [&] (const auto& entry) {
- return window.width >= static_cast<uint32_t> (entry.second.z)
- && window.height >= static_cast<uint32_t> (entry.second.w);
- });
- }
- return covers;
- }
- bool X11FullScreenDetector::applicationFocused (Window ourWindow) const {
- const Atom activeAtom = XInternAtom (this->m_display, "_NET_ACTIVE_WINDOW", False);
- Atom type;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- Window active = None;
- if (XGetWindowProperty (
- this->m_display, this->m_root, activeAtom, 0, 1, False, XA_WINDOW, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32 && count == 1) {
- active = *reinterpret_cast<const Window*> (data);
- }
- if (data != nullptr) {
- XFree (data);
- }
- // sub_14003D050
- if (active == None || active == ourWindow || hasDesktopWindowType (this->m_display, active)) {
- return false;
- }
- XWindowAttributes attributes;
- return XGetWindowAttributes (this->m_display, active, &attributes) && attributes.map_state == IsViewable;
- }
- bool X11FullScreenDetector::isHidden (Window client) const {
- // minimized, or tabbed out: some window managers keep the frame of such a window mapped
- const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
- const Atom hiddenAtom = XInternAtom (this->m_display, "_NET_WM_STATE_HIDDEN", False);
- Atom type;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- bool hidden = false;
- if (XGetWindowProperty (
- this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32) {
- const auto* atoms = reinterpret_cast<const Atom*> (data);
- hidden = std::find (atoms, atoms + count, hiddenAtom) != atoms + count;
- }
- if (data != nullptr) {
- XFree (data);
- }
- if (hidden) {
- return true;
- }
- // ICCCM: WM_STATE 3 is iconic
- const Atom wmState = XInternAtom (this->m_display, "WM_STATE", False);
- data = nullptr;
- if (XGetWindowProperty (
- this->m_display, client, wmState, 0, 2, False, wmState, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32 && count >= 1) {
- hidden = reinterpret_cast<const unsigned long*> (data)[0] == IconicState;
- }
- if (data != nullptr) {
- XFree (data);
- }
- return hidden;
- }
- bool X11FullScreenDetector::onCurrentDesktop (Window client) const {
- const auto cardinal = [this] (Window window, const char* name, unsigned long& value) {
- const Atom atom = XInternAtom (this->m_display, name, False);
- Atom type;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- bool found = false;
- if (XGetWindowProperty (
- this->m_display, window, atom, 0, 1, False, XA_CARDINAL, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32 && count == 1) {
- value = *reinterpret_cast<const unsigned long*> (data);
- found = true;
- }
- if (data != nullptr) {
- XFree (data);
- }
- return found;
- };
- unsigned long windowDesktop = 0, currentDesktop = 0;
- // no EWMH desktops, or a sticky window (0xFFFFFFFF), counts everywhere
- if (!cardinal (client, "_NET_WM_DESKTOP", windowDesktop)
- || !cardinal (this->m_root, "_NET_CURRENT_DESKTOP", currentDesktop)
- || (windowDesktop & 0xFFFFFFFF) == 0xFFFFFFFF) {
- return true;
- }
- return windowDesktop == currentDesktop;
- }
- bool X11FullScreenDetector::hasFullscreenState (Window client) const {
- const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
- const Atom fullscreenAtom = XInternAtom (this->m_display, "_NET_WM_STATE_FULLSCREEN", False);
- Atom type;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- bool fullscreen = false;
- if (XGetWindowProperty (
- this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32) {
- const auto* atoms = reinterpret_cast<const Atom*> (data);
- fullscreen = std::find (atoms, atoms + count, fullscreenAtom) != atoms + count;
- }
- if (data != nullptr) {
- XFree (data);
- }
- return fullscreen;
- }
- bool X11FullScreenDetector::hasMaximizedState (Window client) const {
- const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
- const Atom vertAtom = XInternAtom (this->m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
- const Atom horzAtom = XInternAtom (this->m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
- Atom type;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- bool maximized = false;
- if (XGetWindowProperty (
- this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
- ) == Success
- && data != nullptr && format == 32) {
- const auto* atoms = reinterpret_cast<const Atom*> (data);
- maximized = std::find (atoms, atoms + count, vertAtom) != atoms + count
- && std::find (atoms, atoms + count, horzAtom) != atoms + count;
- }
- if (data != nullptr) {
- XFree (data);
- }
- return maximized;
- }
- bool X11FullScreenDetector::coversScreen (Window client) const {
- // window managers without EWMH: the client (not its frame) has an output's geometry
- XWindowAttributes attributes;
- int x = 0, y = 0;
- Window child;
- if (!XGetWindowAttributes (this->m_display, client, &attributes)
- || !XTranslateCoordinates (this->m_display, client, this->m_root, 0, 0, &x, &y, &child)) {
- return false;
- }
- return std::ranges::any_of (this->m_screens, [&] (const auto& entry) {
- const auto& screen = entry.second;
- return x == screen.x && y == screen.y && attributes.width == screen.z && attributes.height == screen.w;
- });
- }
- Window X11FullScreenDetector::clientOf (Window window) const {
- // the client inside a WM frame is the window with WM_STATE
- const Atom wmState = XInternAtom (this->m_display, "WM_STATE", False);
- Atom type = None;
- int format;
- unsigned long count, remaining;
- unsigned char* data = nullptr;
- XGetWindowProperty (
- this->m_display, window, wmState, 0, 0, False, AnyPropertyType, &type, &format, &count, &remaining, &data
- );
- if (data != nullptr) {
- XFree (data);
- }
- if (type != None) {
- return window;
- }
- Window root, parent, *children = nullptr;
- unsigned int childCount = 0;
- Window client = None;
- if (XQueryTree (this->m_display, window, &root, &parent, &children, &childCount)) {
- for (unsigned int i = 0; i < childCount && client == None; i++) {
- client = this->clientOf (children[i]);
- }
- }
- if (children != nullptr) {
- XFree (children);
- }
- return client;
- }
- void X11FullScreenDetector::reset () {
- this->stop ();
- this->initialize ();
- }
- void X11FullScreenDetector::initialize () {
- this->m_display = XOpenDisplay (nullptr);
- this->m_screens.clear ();
- if (this->m_display == nullptr) {
- sLog.error ("Cannot open the X display, fullscreen detection is off until the next reset");
- return;
- }
- // recover from X disconnections instead of aborting
- #ifdef HAVE_XSETIOERROREXITHANDLER
- XSetIOErrorExitHandler (this->m_display, CustomXIOErrorExitHandler, this);
- #endif
- int xrandr_result, xrandr_error;
- if (!XRRQueryExtension (this->m_display, &xrandr_result, &xrandr_error)) {
- sLog.error ("XRandr is not present, fullscreen detection might not work");
- return;
- }
- this->m_root = DefaultRootWindow (this->m_display);
- XRRScreenResources* screenResources = XRRGetScreenResources (this->m_display, this->m_root);
- if (screenResources == nullptr) {
- sLog.error ("Cannot detect screen sizes using xrandr, fullscreen detection might not work");
- return;
- }
- for (int i = 0; i < screenResources->noutput; i++) {
- const XRROutputInfo* info = XRRGetOutputInfo (this->m_display, screenResources, screenResources->outputs[i]);
- if (info == nullptr || info->connection != RR_Connected) {
- continue;
- }
- XRRCrtcInfo* crtc = XRRGetCrtcInfo (this->m_display, screenResources, info->crtc);
- if (crtc == nullptr) {
- continue;
- }
- this->m_screens.emplace (std::string (info->name), glm::ivec4 (crtc->x, crtc->y, crtc->width, crtc->height));
- XRRFreeCrtcInfo (crtc);
- }
- XRRFreeScreenResources (screenResources);
- }
- void X11FullScreenDetector::stop () {
- if (this->m_display == nullptr) {
- return;
- }
- XCloseDisplay (this->m_display);
- this->m_display = nullptr;
- }
- __attribute__ ((constructor)) static void registerX11FullscreenDetector () {
- sVideoFactories.registerFullscreenDetector (
- "x11", [] (ApplicationContext& context, VideoDriver& driver) -> std::unique_ptr<FullScreenDetector> {
- return std::make_unique<X11FullScreenDetector> (context, driver);
- }
- );
- }
- }
|