WaylandFullScreenDetector.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "WaylandFullScreenDetector.h"
  2. #include "GnomeFullScreenDetector.h"
  3. #ifdef ENABLE_KDE_FEATURES
  4. #include "KDEWaylandFullScreenDetector.h"
  5. #include "WallpaperEngine/Desktop/KDESession.h"
  6. #endif
  7. #include "WallpaperEngine/Logging/Log.h"
  8. #include "WallpaperEngine/Render/Drivers/VideoFactories.h"
  9. #include "WallpaperEngine/Render/Drivers/WaylandOpenGLDriver.h"
  10. #include "wlr-foreign-toplevel-management-unstable-v1-protocol.h"
  11. #include <cstdlib>
  12. #include <cstring>
  13. #include <string>
  14. #include <string_view>
  15. namespace WallpaperEngine::Render::Drivers::Detectors {
  16. namespace {
  17. bool icontains (std::string_view haystack, std::string_view needle) {
  18. if (needle.empty ()) {
  19. return true;
  20. }
  21. if (haystack.empty ()) {
  22. return false;
  23. }
  24. auto toLower = [] (unsigned char c) {
  25. if (c >= 'A' && c <= 'Z') {
  26. return static_cast<unsigned char> (c - 'A' + 'a');
  27. }
  28. return c;
  29. };
  30. for (size_t i = 0; i + needle.size () <= haystack.size (); ++i) {
  31. bool match = true;
  32. for (size_t j = 0; j < needle.size (); ++j) {
  33. if (toLower (static_cast<unsigned char> (haystack[i + j]))
  34. != toLower (static_cast<unsigned char> (needle[j]))) {
  35. match = false;
  36. break;
  37. }
  38. }
  39. if (match) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. struct FullscreenState {
  46. bool pending = false;
  47. bool current = false;
  48. bool pendingActivated = false;
  49. bool currentActivated = false;
  50. std::string appId {};
  51. WallpaperEngine::Render::Drivers::Detectors::WaylandFullscreenDetectorCallbackData* const data;
  52. };
  53. void toplevelHandleTitle (void*, struct zwlr_foreign_toplevel_handle_v1*, const char*) { }
  54. void toplevelHandleAppId (void* data, struct zwlr_foreign_toplevel_handle_v1*, const char* appId) {
  55. const auto state = static_cast<FullscreenState*> (data);
  56. if (appId) {
  57. state->appId = appId;
  58. }
  59. }
  60. void toplevelHandleOutputEnter (void*, struct zwlr_foreign_toplevel_handle_v1*, struct wl_output*) { }
  61. void toplevelHandleOutputLeave (void*, struct zwlr_foreign_toplevel_handle_v1*, struct wl_output*) { }
  62. void
  63. toplevelHandleParent (void*, struct zwlr_foreign_toplevel_handle_v1*, struct zwlr_foreign_toplevel_handle_v1*) { }
  64. void toplevelHandleState (void* data, struct zwlr_foreign_toplevel_handle_v1*, struct wl_array* state) {
  65. const auto toplevel = static_cast<FullscreenState*> (data);
  66. const auto begin = static_cast<uint32_t*> (state->data);
  67. toplevel->pending = false;
  68. toplevel->pendingActivated = false;
  69. for (auto it = begin; it < begin + state->size / sizeof (uint32_t); ++it) {
  70. if (*it == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN) {
  71. toplevel->pending = true;
  72. }
  73. if (*it == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED) {
  74. toplevel->pendingActivated = true;
  75. }
  76. }
  77. }
  78. bool isRelevant (
  79. const WallpaperEngine::Application::ApplicationContext& ctx, const bool fullscreen, const bool activated,
  80. const std::string& appId
  81. ) {
  82. const auto& render = ctx.settings.render;
  83. const bool focusPauses = render.pauseOnFocus && activated;
  84. const bool fullscreenPauses
  85. = render.pauseOnFullscreen && fullscreen && (!render.pauseOnFullscreenOnlyWhenActive || activated);
  86. if (!focusPauses && !fullscreenPauses) {
  87. return false;
  88. }
  89. if (!appId.empty ()) {
  90. for (const auto& ignore : ctx.settings.render.fullscreenPauseIgnoreAppIds) {
  91. if (ignore.empty ()) {
  92. continue;
  93. }
  94. if (icontains (appId, ignore)) {
  95. return false;
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. bool isFullscreenRelevant (const FullscreenState& toplevel) {
  102. const auto& ctx = toplevel.data->detector->getApplicationContext ();
  103. return isRelevant (ctx, toplevel.pending, toplevel.pendingActivated, toplevel.appId);
  104. }
  105. bool isCurrentlyRelevant (const FullscreenState& toplevel) {
  106. const auto& ctx = toplevel.data->detector->getApplicationContext ();
  107. return isRelevant (ctx, toplevel.current, toplevel.currentActivated, toplevel.appId);
  108. }
  109. void toplevelHandleDone (void* data, struct zwlr_foreign_toplevel_handle_v1* handle) {
  110. const auto toplevel = static_cast<FullscreenState*> (data);
  111. const bool pendingRelevant = isFullscreenRelevant (*toplevel);
  112. const bool currentRelevant = isCurrentlyRelevant (*toplevel);
  113. if (currentRelevant != pendingRelevant) {
  114. if (pendingRelevant) {
  115. ++(*toplevel->data->fullscreenCount);
  116. } else {
  117. if (*toplevel->data->fullscreenCount == 0) {
  118. sLog.error ("Fullscreen count underflow!!!");
  119. } else {
  120. --(*toplevel->data->fullscreenCount);
  121. }
  122. }
  123. }
  124. toplevel->current = toplevel->pending;
  125. toplevel->currentActivated = toplevel->pendingActivated;
  126. }
  127. void toplevelHandleClosed (void* data, struct zwlr_foreign_toplevel_handle_v1* handle) {
  128. const auto toplevel = static_cast<FullscreenState*> (data);
  129. // If it was counted as relevant fullscreen, remove it.
  130. const bool currentRelevant = isCurrentlyRelevant (*toplevel);
  131. if (currentRelevant) {
  132. if (*toplevel->data->fullscreenCount == 0) {
  133. sLog.error ("Fullscreen count underflow!!!");
  134. } else {
  135. --(*toplevel->data->fullscreenCount);
  136. }
  137. }
  138. zwlr_foreign_toplevel_handle_v1_destroy (handle);
  139. delete toplevel;
  140. }
  141. constexpr struct zwlr_foreign_toplevel_handle_v1_listener toplevelHandleListener = {
  142. .title = toplevelHandleTitle,
  143. .app_id = toplevelHandleAppId,
  144. .output_enter = toplevelHandleOutputEnter,
  145. .output_leave = toplevelHandleOutputLeave,
  146. .state = toplevelHandleState,
  147. .done = toplevelHandleDone,
  148. .closed = toplevelHandleClosed,
  149. .parent = toplevelHandleParent,
  150. };
  151. void handleToplevel (
  152. void* data, struct zwlr_foreign_toplevel_manager_v1* manager, struct zwlr_foreign_toplevel_handle_v1* handle
  153. ) {
  154. const auto cb
  155. = static_cast<WallpaperEngine::Render::Drivers::Detectors::WaylandFullscreenDetectorCallbackData*> (data);
  156. const auto toplevel = new FullscreenState {
  157. .pending = false,
  158. .current = false,
  159. .pendingActivated = false,
  160. .currentActivated = false,
  161. .appId = {},
  162. .data = cb,
  163. };
  164. zwlr_foreign_toplevel_handle_v1_add_listener (handle, &toplevelHandleListener, toplevel);
  165. }
  166. void handleFinished (void* data, struct zwlr_foreign_toplevel_manager_v1* manager) {
  167. zwlr_foreign_toplevel_manager_v1_destroy (manager);
  168. }
  169. zwlr_foreign_toplevel_manager_v1_listener toplevelManagerListener = {
  170. .toplevel = handleToplevel,
  171. .finished = handleFinished,
  172. };
  173. }; // anonymous namespace
  174. void handleGlobal (void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
  175. const auto detector = static_cast<WaylandFullScreenDetector*> (data);
  176. if (strcmp (interface, zwlr_foreign_toplevel_manager_v1_interface.name) == 0) {
  177. detector->m_toplevelManager = static_cast<zwlr_foreign_toplevel_manager_v1*> (
  178. wl_registry_bind (registry, name, &zwlr_foreign_toplevel_manager_v1_interface, 3)
  179. );
  180. if (detector->m_toplevelManager) {
  181. zwlr_foreign_toplevel_manager_v1_add_listener (
  182. detector->m_toplevelManager, &toplevelManagerListener, &detector->m_callbackData
  183. );
  184. }
  185. }
  186. }
  187. void handleGlobalRemoved (void* data, struct wl_registry* registry, uint32_t id) {
  188. // todo: outputs
  189. }
  190. constexpr struct wl_registry_listener registryListener = {
  191. .global = handleGlobal,
  192. .global_remove = handleGlobalRemoved,
  193. };
  194. WaylandFullScreenDetector::WaylandFullScreenDetector (Application::ApplicationContext& appContext) :
  195. FullScreenDetector (appContext), m_callbackData { .detector = this, .fullscreenCount = &m_fullscreenCount } {
  196. m_display = wl_display_connect (nullptr);
  197. if (!m_display) {
  198. sLog.exception ("Failed to query wayland display");
  199. }
  200. const auto registry = wl_display_get_registry (m_display);
  201. wl_registry_add_listener (registry, &registryListener, this);
  202. wl_display_roundtrip (m_display); // load list of toplevels
  203. if (!m_toplevelManager) {
  204. sLog.out ("Fullscreen detection not supported by your Wayland compositor");
  205. } else {
  206. wl_display_roundtrip (m_display); // load toplevel details
  207. }
  208. }
  209. WaylandFullScreenDetector::~WaylandFullScreenDetector () {
  210. if (m_display) {
  211. wl_display_disconnect (m_display);
  212. }
  213. }
  214. bool WaylandFullScreenDetector::anythingFullscreen () const {
  215. if (!m_toplevelManager) {
  216. return false;
  217. }
  218. wl_display_roundtrip (m_display);
  219. return m_fullscreenCount > 0;
  220. }
  221. void WaylandFullScreenDetector::reset () { }
  222. __attribute__ ((constructor)) void registerWaylandFullscreenDetector () {
  223. sVideoFactories.registerFullscreenDetector (
  224. "wayland", [] (ApplicationContext& context, VideoDriver& driver) -> std::unique_ptr<FullScreenDetector> {
  225. if (auto* wayland = dynamic_cast<WaylandOpenGLDriver*> (&driver); wayland && wayland->getGnomeShell ()) {
  226. return std::make_unique<GnomeFullScreenDetector> (context, *wayland);
  227. }
  228. #ifdef ENABLE_KDE_FEATURES
  229. if (auto* wayland = dynamic_cast<WaylandOpenGLDriver*> (&driver);
  230. wayland && WallpaperEngine::Desktop::isKDESession ()) {
  231. auto detector = std::make_unique<KDEWaylandFullScreenDetector> (context, *wayland);
  232. if (!detector->isInitialized ()) {
  233. return std::make_unique<WaylandFullScreenDetector> (context);
  234. }
  235. return detector;
  236. }
  237. #endif
  238. return std::make_unique<WaylandFullScreenDetector> (context);
  239. }
  240. );
  241. }
  242. } // namespace WallpaperEngine::Render::Drivers::Detectors