X11FullScreenDetector.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "X11FullScreenDetector.h"
  2. #include "WallpaperEngine/Logging/Log.h"
  3. #include "WallpaperEngine/Render/Drivers/X11WindowType.h"
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <X11/Xatom.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/Xutil.h>
  9. #include <X11/extensions/Xrandr.h>
  10. #include "WallpaperEngine/Render/Drivers/GLFWOpenGLDriver.h"
  11. #define GLFW_EXPOSE_NATIVE_X11
  12. #include "WallpaperEngine/Render/Drivers/VideoFactories.h"
  13. #include <GLFW/glfw3.h>
  14. #include <GLFW/glfw3native.h>
  15. namespace WallpaperEngine::Render::Drivers::Detectors {
  16. static void CustomXIOErrorExitHandler (Display* dsp, void* userdata) {
  17. const auto context = static_cast<X11FullScreenDetector*> (userdata);
  18. sLog.debugerror ("Critical XServer error detected. Attempting to recover...");
  19. context->reset ();
  20. }
  21. static int CustomXErrorHandler (Display* dpy, XErrorEvent* event) {
  22. sLog.debugerror ("Detected X error");
  23. return 0;
  24. }
  25. static int CustomXIOErrorHandler (Display* dsp) {
  26. sLog.debugerror ("Detected X error");
  27. return 0;
  28. }
  29. X11FullScreenDetector::X11FullScreenDetector (Application::ApplicationContext& appContext, VideoDriver& driver) :
  30. FullScreenDetector (appContext), m_display (nullptr), m_root (0), m_driver (driver) {
  31. try {
  32. // throws if m_driver isn't actually a GLFWOpenGLDriver, so we can catch the misuse
  33. std::ignore = dynamic_cast<GLFWOpenGLDriver&> (this->m_driver);
  34. } catch (std::exception&) {
  35. sLog.exception ("X11 FullScreen Detector initialized with the wrong video driver... This is a bug...");
  36. }
  37. // not chaining the previous handler, it could stop the app
  38. XSetErrorHandler (CustomXErrorHandler);
  39. XSetIOErrorHandler (CustomXIOErrorHandler);
  40. this->initialize ();
  41. if (std::getenv ("LWE_XWAYLAND_FALLBACK") != nullptr && Desktop::isPantheonSession ()) {
  42. this->m_gala = std::make_unique<Desktop::PantheonShell> ();
  43. if (!this->m_gala->isAvailable ()) {
  44. sLog.error ("Native Wayland windows are not seen, ", this->m_gala->reason ());
  45. this->m_gala.reset ();
  46. }
  47. }
  48. }
  49. X11FullScreenDetector::~X11FullScreenDetector () { this->stop (); }
  50. bool X11FullScreenDetector::anythingFullscreen () const {
  51. if (this->m_display == nullptr) {
  52. return false;
  53. }
  54. Window root, parent;
  55. Window* children = nullptr;
  56. unsigned int childCount = 0;
  57. if (!XQueryTree (this->m_display, this->m_root, &root, &parent, &children, &childCount)) {
  58. return false;
  59. }
  60. const Window ourWindow = glfwGetX11Window (dynamic_cast<GLFWOpenGLDriver&> (this->m_driver).getWindow ());
  61. const auto& render = this->getApplicationContext ().settings.render;
  62. bool fullscreen = false;
  63. for (unsigned int i = 0; i < childCount && !fullscreen && render.pauseOnFullscreen; i++) {
  64. XWindowAttributes attributes;
  65. if (!XGetWindowAttributes (this->m_display, children[i], &attributes) || attributes.map_state != IsViewable) {
  66. continue;
  67. }
  68. // only managed windows count, WM/compositor helpers and our desktop window cover the screen too
  69. const Window client = this->clientOf (children[i]);
  70. if (client == None || client == ourWindow || hasDesktopWindowType (this->m_display, client)
  71. || this->isHidden (client) || !this->onCurrentDesktop (client)) {
  72. continue;
  73. }
  74. fullscreen
  75. = this->hasFullscreenState (client) || this->hasMaximizedState (client) || this->coversScreen (client);
  76. }
  77. XFree (children);
  78. bool galaFocused = false;
  79. const bool galaCovers = this->galaWindowCovers (galaFocused);
  80. return fullscreen || (render.pauseOnFullscreen && galaCovers)
  81. || (render.pauseOnFocus && (this->applicationFocused (ourWindow) || galaFocused));
  82. }
  83. bool X11FullScreenDetector::galaWindowCovers (bool& focused) const {
  84. if (this->m_gala == nullptr) {
  85. return false;
  86. }
  87. this->m_gala->dispatch ();
  88. bool covers = false;
  89. for (const auto& window : this->m_gala->windows ()) {
  90. // our own background window is a window of Gala's too
  91. if (window.hidden || window.workspace != this->m_gala->activeWorkspace ()
  92. || window.wmClass == "linux-wallpaperengine") {
  93. continue;
  94. }
  95. focused = focused || window.focused;
  96. covers = covers || std::ranges::any_of (this->m_screens, [&] (const auto& entry) {
  97. return window.width >= static_cast<uint32_t> (entry.second.z)
  98. && window.height >= static_cast<uint32_t> (entry.second.w);
  99. });
  100. }
  101. return covers;
  102. }
  103. bool X11FullScreenDetector::applicationFocused (Window ourWindow) const {
  104. const Atom activeAtom = XInternAtom (this->m_display, "_NET_ACTIVE_WINDOW", False);
  105. Atom type;
  106. int format;
  107. unsigned long count, remaining;
  108. unsigned char* data = nullptr;
  109. Window active = None;
  110. if (XGetWindowProperty (
  111. this->m_display, this->m_root, activeAtom, 0, 1, False, XA_WINDOW, &type, &format, &count, &remaining, &data
  112. ) == Success
  113. && data != nullptr && format == 32 && count == 1) {
  114. active = *reinterpret_cast<const Window*> (data);
  115. }
  116. if (data != nullptr) {
  117. XFree (data);
  118. }
  119. // sub_14003D050
  120. if (active == None || active == ourWindow || hasDesktopWindowType (this->m_display, active)) {
  121. return false;
  122. }
  123. XWindowAttributes attributes;
  124. return XGetWindowAttributes (this->m_display, active, &attributes) && attributes.map_state == IsViewable;
  125. }
  126. bool X11FullScreenDetector::isHidden (Window client) const {
  127. // minimized, or tabbed out: some window managers keep the frame of such a window mapped
  128. const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
  129. const Atom hiddenAtom = XInternAtom (this->m_display, "_NET_WM_STATE_HIDDEN", False);
  130. Atom type;
  131. int format;
  132. unsigned long count, remaining;
  133. unsigned char* data = nullptr;
  134. bool hidden = false;
  135. if (XGetWindowProperty (
  136. this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
  137. ) == Success
  138. && data != nullptr && format == 32) {
  139. const auto* atoms = reinterpret_cast<const Atom*> (data);
  140. hidden = std::find (atoms, atoms + count, hiddenAtom) != atoms + count;
  141. }
  142. if (data != nullptr) {
  143. XFree (data);
  144. }
  145. if (hidden) {
  146. return true;
  147. }
  148. // ICCCM: WM_STATE 3 is iconic
  149. const Atom wmState = XInternAtom (this->m_display, "WM_STATE", False);
  150. data = nullptr;
  151. if (XGetWindowProperty (
  152. this->m_display, client, wmState, 0, 2, False, wmState, &type, &format, &count, &remaining, &data
  153. ) == Success
  154. && data != nullptr && format == 32 && count >= 1) {
  155. hidden = reinterpret_cast<const unsigned long*> (data)[0] == IconicState;
  156. }
  157. if (data != nullptr) {
  158. XFree (data);
  159. }
  160. return hidden;
  161. }
  162. bool X11FullScreenDetector::onCurrentDesktop (Window client) const {
  163. const auto cardinal = [this] (Window window, const char* name, unsigned long& value) {
  164. const Atom atom = XInternAtom (this->m_display, name, False);
  165. Atom type;
  166. int format;
  167. unsigned long count, remaining;
  168. unsigned char* data = nullptr;
  169. bool found = false;
  170. if (XGetWindowProperty (
  171. this->m_display, window, atom, 0, 1, False, XA_CARDINAL, &type, &format, &count, &remaining, &data
  172. ) == Success
  173. && data != nullptr && format == 32 && count == 1) {
  174. value = *reinterpret_cast<const unsigned long*> (data);
  175. found = true;
  176. }
  177. if (data != nullptr) {
  178. XFree (data);
  179. }
  180. return found;
  181. };
  182. unsigned long windowDesktop = 0, currentDesktop = 0;
  183. // no EWMH desktops, or a sticky window (0xFFFFFFFF), counts everywhere
  184. if (!cardinal (client, "_NET_WM_DESKTOP", windowDesktop)
  185. || !cardinal (this->m_root, "_NET_CURRENT_DESKTOP", currentDesktop)
  186. || (windowDesktop & 0xFFFFFFFF) == 0xFFFFFFFF) {
  187. return true;
  188. }
  189. return windowDesktop == currentDesktop;
  190. }
  191. bool X11FullScreenDetector::hasFullscreenState (Window client) const {
  192. const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
  193. const Atom fullscreenAtom = XInternAtom (this->m_display, "_NET_WM_STATE_FULLSCREEN", False);
  194. Atom type;
  195. int format;
  196. unsigned long count, remaining;
  197. unsigned char* data = nullptr;
  198. bool fullscreen = false;
  199. if (XGetWindowProperty (
  200. this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
  201. ) == Success
  202. && data != nullptr && format == 32) {
  203. const auto* atoms = reinterpret_cast<const Atom*> (data);
  204. fullscreen = std::find (atoms, atoms + count, fullscreenAtom) != atoms + count;
  205. }
  206. if (data != nullptr) {
  207. XFree (data);
  208. }
  209. return fullscreen;
  210. }
  211. bool X11FullScreenDetector::hasMaximizedState (Window client) const {
  212. const Atom stateAtom = XInternAtom (this->m_display, "_NET_WM_STATE", False);
  213. const Atom vertAtom = XInternAtom (this->m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  214. const Atom horzAtom = XInternAtom (this->m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  215. Atom type;
  216. int format;
  217. unsigned long count, remaining;
  218. unsigned char* data = nullptr;
  219. bool maximized = false;
  220. if (XGetWindowProperty (
  221. this->m_display, client, stateAtom, 0, 32, False, XA_ATOM, &type, &format, &count, &remaining, &data
  222. ) == Success
  223. && data != nullptr && format == 32) {
  224. const auto* atoms = reinterpret_cast<const Atom*> (data);
  225. maximized = std::find (atoms, atoms + count, vertAtom) != atoms + count
  226. && std::find (atoms, atoms + count, horzAtom) != atoms + count;
  227. }
  228. if (data != nullptr) {
  229. XFree (data);
  230. }
  231. return maximized;
  232. }
  233. bool X11FullScreenDetector::coversScreen (Window client) const {
  234. // window managers without EWMH: the client (not its frame) has an output's geometry
  235. XWindowAttributes attributes;
  236. int x = 0, y = 0;
  237. Window child;
  238. if (!XGetWindowAttributes (this->m_display, client, &attributes)
  239. || !XTranslateCoordinates (this->m_display, client, this->m_root, 0, 0, &x, &y, &child)) {
  240. return false;
  241. }
  242. return std::ranges::any_of (this->m_screens, [&] (const auto& entry) {
  243. const auto& screen = entry.second;
  244. return x == screen.x && y == screen.y && attributes.width == screen.z && attributes.height == screen.w;
  245. });
  246. }
  247. Window X11FullScreenDetector::clientOf (Window window) const {
  248. // the client inside a WM frame is the window with WM_STATE
  249. const Atom wmState = XInternAtom (this->m_display, "WM_STATE", False);
  250. Atom type = None;
  251. int format;
  252. unsigned long count, remaining;
  253. unsigned char* data = nullptr;
  254. XGetWindowProperty (
  255. this->m_display, window, wmState, 0, 0, False, AnyPropertyType, &type, &format, &count, &remaining, &data
  256. );
  257. if (data != nullptr) {
  258. XFree (data);
  259. }
  260. if (type != None) {
  261. return window;
  262. }
  263. Window root, parent, *children = nullptr;
  264. unsigned int childCount = 0;
  265. Window client = None;
  266. if (XQueryTree (this->m_display, window, &root, &parent, &children, &childCount)) {
  267. for (unsigned int i = 0; i < childCount && client == None; i++) {
  268. client = this->clientOf (children[i]);
  269. }
  270. }
  271. if (children != nullptr) {
  272. XFree (children);
  273. }
  274. return client;
  275. }
  276. void X11FullScreenDetector::reset () {
  277. this->stop ();
  278. this->initialize ();
  279. }
  280. void X11FullScreenDetector::initialize () {
  281. this->m_display = XOpenDisplay (nullptr);
  282. this->m_screens.clear ();
  283. if (this->m_display == nullptr) {
  284. sLog.error ("Cannot open the X display, fullscreen detection is off until the next reset");
  285. return;
  286. }
  287. // recover from X disconnections instead of aborting
  288. #ifdef HAVE_XSETIOERROREXITHANDLER
  289. XSetIOErrorExitHandler (this->m_display, CustomXIOErrorExitHandler, this);
  290. #endif
  291. int xrandr_result, xrandr_error;
  292. if (!XRRQueryExtension (this->m_display, &xrandr_result, &xrandr_error)) {
  293. sLog.error ("XRandr is not present, fullscreen detection might not work");
  294. return;
  295. }
  296. this->m_root = DefaultRootWindow (this->m_display);
  297. XRRScreenResources* screenResources = XRRGetScreenResources (this->m_display, this->m_root);
  298. if (screenResources == nullptr) {
  299. sLog.error ("Cannot detect screen sizes using xrandr, fullscreen detection might not work");
  300. return;
  301. }
  302. for (int i = 0; i < screenResources->noutput; i++) {
  303. const XRROutputInfo* info = XRRGetOutputInfo (this->m_display, screenResources, screenResources->outputs[i]);
  304. if (info == nullptr || info->connection != RR_Connected) {
  305. continue;
  306. }
  307. XRRCrtcInfo* crtc = XRRGetCrtcInfo (this->m_display, screenResources, info->crtc);
  308. if (crtc == nullptr) {
  309. continue;
  310. }
  311. this->m_screens.emplace (std::string (info->name), glm::ivec4 (crtc->x, crtc->y, crtc->width, crtc->height));
  312. XRRFreeCrtcInfo (crtc);
  313. }
  314. XRRFreeScreenResources (screenResources);
  315. }
  316. void X11FullScreenDetector::stop () {
  317. if (this->m_display == nullptr) {
  318. return;
  319. }
  320. XCloseDisplay (this->m_display);
  321. this->m_display = nullptr;
  322. }
  323. __attribute__ ((constructor)) static void registerX11FullscreenDetector () {
  324. sVideoFactories.registerFullscreenDetector (
  325. "x11", [] (ApplicationContext& context, VideoDriver& driver) -> std::unique_ptr<FullScreenDetector> {
  326. return std::make_unique<X11FullScreenDetector> (context, driver);
  327. }
  328. );
  329. }
  330. }