WaylandFullScreenDetector.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/Desktop/HostWayland.h"
  8. #include "WallpaperEngine/Desktop/WindowState.h"
  9. #include "WallpaperEngine/Logging/Log.h"
  10. #include "WallpaperEngine/Render/Drivers/VideoFactories.h"
  11. #include "WallpaperEngine/Render/Drivers/WaylandOpenGLDriver.h"
  12. #include "cosmic-toplevel-info-unstable-v1-protocol.h"
  13. #include "ext-foreign-toplevel-list-v1-protocol.h"
  14. #include "wlr-foreign-toplevel-management-unstable-v1-protocol.h"
  15. #include <algorithm>
  16. #include <cstring>
  17. #include <wayland-client.h>
  18. namespace WallpaperEngine::Render::Drivers::Detectors {
  19. namespace {
  20. using Output = WaylandFullScreenDetector::Output;
  21. using Toplevel = WaylandFullScreenDetector::Toplevel;
  22. void
  23. outputGeometry (void*, wl_output*, int32_t, int32_t, int32_t, int32_t, int32_t, const char*, const char*, int32_t) {
  24. }
  25. void outputMode (void*, wl_output*, uint32_t, int32_t, int32_t, int32_t) { }
  26. void outputDone (void*, wl_output*) { }
  27. void outputScale (void*, wl_output*, int32_t) { }
  28. void outputName (void* data, wl_output*, const char* name) {
  29. if (name) {
  30. static_cast<Output*> (data)->name = name;
  31. }
  32. }
  33. void outputDescription (void*, wl_output*, const char*) { }
  34. constexpr wl_output_listener outputListener = {
  35. .geometry = outputGeometry,
  36. .mode = outputMode,
  37. .done = outputDone,
  38. .scale = outputScale,
  39. .name = outputName,
  40. .description = outputDescription,
  41. };
  42. void toplevelTitle (void*, zwlr_foreign_toplevel_handle_v1*, const char*) { }
  43. void toplevelAppId (void* data, zwlr_foreign_toplevel_handle_v1*, const char* appId) {
  44. if (appId) {
  45. static_cast<Toplevel*> (data)->appId = appId;
  46. }
  47. }
  48. template <typename Visitor> void forEachState (const wl_array* state, Visitor&& visit) {
  49. const auto begin = static_cast<const uint32_t*> (state->data);
  50. for (auto it = begin; it < begin + state->size / sizeof (uint32_t); ++it) {
  51. visit (*it);
  52. }
  53. }
  54. void toplevelOutputEnter (void* data, zwlr_foreign_toplevel_handle_v1*, wl_output* output) {
  55. static_cast<Toplevel*> (data)->outputs.insert (output);
  56. }
  57. void toplevelOutputLeave (void* data, zwlr_foreign_toplevel_handle_v1*, wl_output* output) {
  58. static_cast<Toplevel*> (data)->outputs.erase (output);
  59. }
  60. void toplevelState (void* data, zwlr_foreign_toplevel_handle_v1*, wl_array* state) {
  61. const auto toplevel = static_cast<Toplevel*> (data);
  62. toplevel->fullscreen = false;
  63. toplevel->maximized = false;
  64. toplevel->activated = false;
  65. forEachState (state, [toplevel] (const uint32_t value) {
  66. switch (value) {
  67. case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN:
  68. toplevel->fullscreen = true;
  69. break;
  70. case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED:
  71. toplevel->maximized = true;
  72. break;
  73. case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED:
  74. toplevel->activated = true;
  75. break;
  76. default:
  77. break;
  78. }
  79. });
  80. }
  81. void toplevelDone (void*, zwlr_foreign_toplevel_handle_v1*) { }
  82. void toplevelClosed (void* data, zwlr_foreign_toplevel_handle_v1*) {
  83. const auto toplevel = static_cast<Toplevel*> (data);
  84. toplevel->detector->removeToplevel (toplevel);
  85. }
  86. void toplevelParent (void*, zwlr_foreign_toplevel_handle_v1*, zwlr_foreign_toplevel_handle_v1*) { }
  87. constexpr zwlr_foreign_toplevel_handle_v1_listener toplevelListener = {
  88. .title = toplevelTitle,
  89. .app_id = toplevelAppId,
  90. .output_enter = toplevelOutputEnter,
  91. .output_leave = toplevelOutputLeave,
  92. .state = toplevelState,
  93. .done = toplevelDone,
  94. .closed = toplevelClosed,
  95. .parent = toplevelParent,
  96. };
  97. void managerToplevel (void* data, zwlr_foreign_toplevel_manager_v1*, zwlr_foreign_toplevel_handle_v1* handle) {
  98. static_cast<WaylandFullScreenDetector*> (data)->addToplevel (handle);
  99. }
  100. void managerFinished (void*, zwlr_foreign_toplevel_manager_v1* manager) {
  101. zwlr_foreign_toplevel_manager_v1_destroy (manager);
  102. }
  103. constexpr zwlr_foreign_toplevel_manager_v1_listener managerListener = {
  104. .toplevel = managerToplevel,
  105. .finished = managerFinished,
  106. };
  107. // COSMIC: the ext handle names the window (app id, closed), the cosmic extension object made from it has the
  108. // outputs and the state
  109. void foreignClosed (void* data, ext_foreign_toplevel_handle_v1*) {
  110. const auto toplevel = static_cast<Toplevel*> (data);
  111. toplevel->detector->removeToplevel (toplevel);
  112. }
  113. void foreignDone (void*, ext_foreign_toplevel_handle_v1*) { }
  114. void foreignTitle (void*, ext_foreign_toplevel_handle_v1*, const char*) { }
  115. void foreignAppId (void* data, ext_foreign_toplevel_handle_v1*, const char* appId) {
  116. if (appId) {
  117. static_cast<Toplevel*> (data)->appId = appId;
  118. }
  119. }
  120. void foreignIdentifier (void*, ext_foreign_toplevel_handle_v1*, const char*) { }
  121. constexpr ext_foreign_toplevel_handle_v1_listener foreignHandleListener = {
  122. .closed = foreignClosed,
  123. .done = foreignDone,
  124. .title = foreignTitle,
  125. .app_id = foreignAppId,
  126. .identifier = foreignIdentifier,
  127. };
  128. void cosmicClosed (void*, zcosmic_toplevel_handle_v1*) { }
  129. void cosmicDone (void*, zcosmic_toplevel_handle_v1*) { }
  130. void cosmicTitle (void*, zcosmic_toplevel_handle_v1*, const char*) { }
  131. void cosmicAppId (void*, zcosmic_toplevel_handle_v1*, const char*) { }
  132. void cosmicOutputEnter (void* data, zcosmic_toplevel_handle_v1*, wl_output* output) {
  133. static_cast<Toplevel*> (data)->outputs.insert (output);
  134. }
  135. void cosmicOutputLeave (void* data, zcosmic_toplevel_handle_v1*, wl_output* output) {
  136. static_cast<Toplevel*> (data)->outputs.erase (output);
  137. }
  138. void cosmicWorkspace (void*, zcosmic_toplevel_handle_v1*, zcosmic_workspace_handle_v1*) { }
  139. void cosmicState (void* data, zcosmic_toplevel_handle_v1*, wl_array* state) {
  140. const auto toplevel = static_cast<Toplevel*> (data);
  141. toplevel->fullscreen = false;
  142. toplevel->maximized = false;
  143. toplevel->activated = false;
  144. toplevel->minimized = false;
  145. forEachState (state, [toplevel] (const uint32_t value) {
  146. switch (value) {
  147. case ZCOSMIC_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN:
  148. toplevel->fullscreen = true;
  149. break;
  150. case ZCOSMIC_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED:
  151. toplevel->maximized = true;
  152. break;
  153. case ZCOSMIC_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED:
  154. toplevel->activated = true;
  155. break;
  156. case ZCOSMIC_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED:
  157. toplevel->minimized = true;
  158. break;
  159. default:
  160. break;
  161. }
  162. });
  163. }
  164. void cosmicGeometry (void*, zcosmic_toplevel_handle_v1*, wl_output*, int32_t, int32_t, int32_t, int32_t) { }
  165. void cosmicExtWorkspace (void*, zcosmic_toplevel_handle_v1*, ext_workspace_handle_v1*) { }
  166. constexpr zcosmic_toplevel_handle_v1_listener cosmicHandleListener = {
  167. .closed = cosmicClosed,
  168. .done = cosmicDone,
  169. .title = cosmicTitle,
  170. .app_id = cosmicAppId,
  171. .output_enter = cosmicOutputEnter,
  172. .output_leave = cosmicOutputLeave,
  173. .workspace_enter = cosmicWorkspace,
  174. .workspace_leave = cosmicWorkspace,
  175. .state = cosmicState,
  176. .geometry = cosmicGeometry,
  177. .ext_workspace_enter = cosmicExtWorkspace,
  178. .ext_workspace_leave = cosmicExtWorkspace,
  179. };
  180. void foreignListToplevel (void* data, ext_foreign_toplevel_list_v1*, ext_foreign_toplevel_handle_v1* handle) {
  181. static_cast<WaylandFullScreenDetector*> (data)->addForeignToplevel (handle);
  182. }
  183. void foreignListFinished (void*, ext_foreign_toplevel_list_v1*) { }
  184. constexpr ext_foreign_toplevel_list_v1_listener foreignListListener = {
  185. .toplevel = foreignListToplevel,
  186. .finished = foreignListFinished,
  187. };
  188. void cosmicInfoToplevel (void*, zcosmic_toplevel_info_v1*, zcosmic_toplevel_handle_v1*) { }
  189. void cosmicInfoFinished (void*, zcosmic_toplevel_info_v1*) { }
  190. void cosmicInfoDone (void*, zcosmic_toplevel_info_v1*) { }
  191. constexpr zcosmic_toplevel_info_v1_listener cosmicInfoListener = {
  192. .toplevel = cosmicInfoToplevel,
  193. .finished = cosmicInfoFinished,
  194. .done = cosmicInfoDone,
  195. };
  196. void registryGlobal (void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
  197. const auto detector = static_cast<WaylandFullScreenDetector*> (data);
  198. if (strcmp (interface, wl_output_interface.name) == 0) {
  199. detector->addOutput (registry, name, version);
  200. } else if (strcmp (interface, zwlr_foreign_toplevel_manager_v1_interface.name) == 0) {
  201. detector->m_managerGlobal = name;
  202. detector->m_managerVersion = version;
  203. } else if (strcmp (interface, zcosmic_toplevel_info_v1_interface.name) == 0) {
  204. detector->m_cosmicInfoGlobal = name;
  205. detector->m_cosmicInfoVersion = version;
  206. } else if (strcmp (interface, ext_foreign_toplevel_list_v1_interface.name) == 0) {
  207. detector->m_foreignListGlobal = name;
  208. }
  209. }
  210. void registryGlobalRemove (void* data, wl_registry*, uint32_t name) {
  211. static_cast<WaylandFullScreenDetector*> (data)->removeGlobal (name);
  212. }
  213. constexpr wl_registry_listener registryListener = {
  214. .global = registryGlobal,
  215. .global_remove = registryGlobalRemove,
  216. };
  217. }
  218. WaylandFullScreenDetector::WaylandFullScreenDetector (
  219. Application::ApplicationContext& appContext, WaylandOpenGLDriver* driver
  220. ) : FullScreenDetector (appContext), m_driver (driver) {
  221. m_display = Desktop::connectWayland ();
  222. if (!m_display) {
  223. sLog.exception ("Failed to query wayland display");
  224. }
  225. m_registry = wl_display_get_registry (m_display);
  226. wl_registry_add_listener (m_registry, &registryListener, this);
  227. wl_display_roundtrip (m_display);
  228. // COSMIC (cosmic-comp): version 2 of its toplevel info works on ext-foreign-toplevel-list handles
  229. if (m_managerGlobal == 0 && m_cosmicInfoGlobal != 0 && m_cosmicInfoVersion >= 2 && m_foreignListGlobal != 0) {
  230. m_cosmicInfo = static_cast<zcosmic_toplevel_info_v1*> (wl_registry_bind (
  231. m_registry, m_cosmicInfoGlobal, &zcosmic_toplevel_info_v1_interface,
  232. std::min<uint32_t> (m_cosmicInfoVersion, 3)
  233. ));
  234. zcosmic_toplevel_info_v1_add_listener (m_cosmicInfo, &cosmicInfoListener, this);
  235. m_foreignList = static_cast<ext_foreign_toplevel_list_v1*> (
  236. wl_registry_bind (m_registry, m_foreignListGlobal, &ext_foreign_toplevel_list_v1_interface, 1)
  237. );
  238. ext_foreign_toplevel_list_v1_add_listener (m_foreignList, &foreignListListener, this);
  239. wl_display_roundtrip (m_display);
  240. wl_display_roundtrip (m_display);
  241. return;
  242. }
  243. if (m_managerGlobal == 0) {
  244. sLog.out ("Fullscreen detection not supported by your Wayland compositor");
  245. return;
  246. }
  247. // bound after the outputs, so the first output_enter events have a wl_output of ours to name
  248. m_toplevelManager = static_cast<zwlr_foreign_toplevel_manager_v1*> (wl_registry_bind (
  249. m_registry, m_managerGlobal, &zwlr_foreign_toplevel_manager_v1_interface,
  250. std::min<uint32_t> (m_managerVersion, 3)
  251. ));
  252. zwlr_foreign_toplevel_manager_v1_add_listener (m_toplevelManager, &managerListener, this);
  253. wl_display_roundtrip (m_display);
  254. wl_display_roundtrip (m_display);
  255. }
  256. WaylandFullScreenDetector::~WaylandFullScreenDetector () {
  257. for (const auto* toplevel : m_toplevels) {
  258. delete toplevel;
  259. }
  260. for (const auto* output : m_outputs) {
  261. delete output;
  262. }
  263. if (m_display) {
  264. wl_display_disconnect (m_display);
  265. }
  266. }
  267. void WaylandFullScreenDetector::addOutput (wl_registry* registry, uint32_t globalName, uint32_t version) {
  268. // wl_output.name is v4, without it the toplevels on this output count everywhere
  269. const auto output = new Output {
  270. .output = static_cast<wl_output*> (
  271. wl_registry_bind (registry, globalName, &wl_output_interface, std::min<uint32_t> (version, 4))
  272. ),
  273. .globalName = globalName,
  274. .name = {},
  275. };
  276. wl_output_add_listener (output->output, &outputListener, output);
  277. m_outputs.push_back (output);
  278. }
  279. void WaylandFullScreenDetector::removeGlobal (uint32_t globalName) {
  280. const auto it = std::ranges::find (m_outputs, globalName, &Output::globalName);
  281. if (it == m_outputs.end ()) {
  282. return;
  283. }
  284. for (auto* toplevel : m_toplevels) {
  285. toplevel->outputs.erase ((*it)->output);
  286. }
  287. if (wl_proxy_get_version (reinterpret_cast<wl_proxy*> ((*it)->output)) >= WL_OUTPUT_RELEASE_SINCE_VERSION) {
  288. wl_output_release ((*it)->output);
  289. } else {
  290. wl_output_destroy ((*it)->output);
  291. }
  292. delete *it;
  293. m_outputs.erase (it);
  294. }
  295. void WaylandFullScreenDetector::addToplevel (zwlr_foreign_toplevel_handle_v1* handle) {
  296. const auto toplevel = new Toplevel { .detector = this, .handle = handle };
  297. zwlr_foreign_toplevel_handle_v1_add_listener (handle, &toplevelListener, toplevel);
  298. m_toplevels.push_back (toplevel);
  299. }
  300. void WaylandFullScreenDetector::addForeignToplevel (ext_foreign_toplevel_handle_v1* handle) {
  301. const auto toplevel = new Toplevel { .detector = this, .foreign = handle };
  302. ext_foreign_toplevel_handle_v1_add_listener (handle, &foreignHandleListener, toplevel);
  303. toplevel->cosmic = zcosmic_toplevel_info_v1_get_cosmic_toplevel (m_cosmicInfo, handle);
  304. zcosmic_toplevel_handle_v1_add_listener (toplevel->cosmic, &cosmicHandleListener, toplevel);
  305. m_toplevels.push_back (toplevel);
  306. }
  307. void WaylandFullScreenDetector::removeToplevel (Toplevel* toplevel) {
  308. if (toplevel->handle) {
  309. zwlr_foreign_toplevel_handle_v1_destroy (toplevel->handle);
  310. }
  311. if (toplevel->cosmic) {
  312. zcosmic_toplevel_handle_v1_destroy (toplevel->cosmic);
  313. }
  314. if (toplevel->foreign) {
  315. ext_foreign_toplevel_handle_v1_destroy (toplevel->foreign);
  316. }
  317. std::erase (m_toplevels, toplevel);
  318. delete toplevel;
  319. }
  320. const std::string* WaylandFullScreenDetector::outputName (wl_output* output) const {
  321. const auto it = std::ranges::find (m_outputs, output, &Output::output);
  322. if (it == m_outputs.end () || (*it)->name.empty ()) {
  323. return nullptr;
  324. }
  325. return &(*it)->name;
  326. }
  327. std::vector<Desktop::WindowState> WaylandFullScreenDetector::windows (const std::vector<std::string>& drawn) const {
  328. const bool maximizedPauses = getApplicationContext ().settings.render.pauseOnMaximized;
  329. std::vector<Desktop::WindowState> windows;
  330. for (const auto* toplevel : m_toplevels) {
  331. const uint32_t flags = (toplevel->activated ? Desktop::WindowState::Focused : 0)
  332. | (toplevel->maximized && maximizedPauses ? Desktop::WindowState::Maximized : 0)
  333. | (toplevel->fullscreen ? Desktop::WindowState::Fullscreen : 0);
  334. if (flags == 0 || toplevel->minimized) {
  335. continue;
  336. }
  337. bool placed = false;
  338. for (auto* output : toplevel->outputs) {
  339. if (const auto* name = outputName (output)) {
  340. windows.push_back ({ toplevel->appId, *name, flags });
  341. placed = true;
  342. }
  343. }
  344. if (!placed) {
  345. for (const auto& name : drawn) {
  346. windows.push_back ({ toplevel->appId, name, flags });
  347. }
  348. }
  349. }
  350. return windows;
  351. }
  352. bool WaylandFullScreenDetector::anythingFullscreen () const {
  353. if (!m_toplevelManager && !m_cosmicInfo) {
  354. return false;
  355. }
  356. wl_display_roundtrip (m_display);
  357. // a driver that isn't ours has no outputs to tell apart, every window counts there: each named output plus "",
  358. // where windows on no named output land
  359. std::vector<std::string> drawn { "" };
  360. if (m_driver) {
  361. drawn = m_driver->drawnOutputs ();
  362. } else {
  363. for (const auto* output : m_outputs) {
  364. if (!output->name.empty ()) {
  365. drawn.push_back (output->name);
  366. }
  367. }
  368. }
  369. return Desktop::pausesAnyOf (windows (drawn), drawn, getApplicationContext ());
  370. }
  371. void WaylandFullScreenDetector::reset () { }
  372. __attribute__ ((constructor)) static void registerWaylandFullscreenDetector () {
  373. sVideoFactories.registerFullscreenDetector (
  374. "wayland", [] (ApplicationContext& context, VideoDriver& driver) -> std::unique_ptr<FullScreenDetector> {
  375. auto* wayland = dynamic_cast<WaylandOpenGLDriver*> (&driver);
  376. if (wayland && wayland->getGnomeShell ()) {
  377. return std::make_unique<GnomeFullScreenDetector> (context, *wayland);
  378. }
  379. #ifdef ENABLE_KDE_FEATURES
  380. if (wayland && WallpaperEngine::Desktop::isKDESession ()) {
  381. auto detector = std::make_unique<KDEWaylandFullScreenDetector> (context, *wayland);
  382. if (!detector->isInitialized ()) {
  383. return std::make_unique<WaylandFullScreenDetector> (context, wayland);
  384. }
  385. return detector;
  386. }
  387. #endif
  388. return std::make_unique<WaylandFullScreenDetector> (context, wayland);
  389. }
  390. );
  391. }
  392. }