X11FullScreenDetector.cpp 12 KB

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