WaylandOutputViewport.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "WaylandOutputViewport.h"
  2. #include "WallpaperEngine/Desktop/GnomeShell.h"
  3. #include "WallpaperEngine/Logging/Log.h"
  4. #include <unistd.h>
  5. #define class _class
  6. #define namespace _namespace
  7. #define static
  8. extern "C" {
  9. #include "color-management-v1-protocol.h"
  10. #include "wlr-layer-shell-unstable-v1-protocol.h"
  11. #include "xdg-output-unstable-v1-protocol.h"
  12. #include "xdg-shell-protocol.h"
  13. #ifdef ENABLE_KDE_FEATURES
  14. #include "plasma-shell-protocol.h"
  15. #endif
  16. }
  17. #undef class
  18. #undef namespace
  19. #undef static
  20. using namespace WallpaperEngine::Render::Drivers;
  21. using namespace WallpaperEngine::Render::Drivers::Output;
  22. static void handleLSConfigure (void* data, zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t w, uint32_t h) {
  23. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  24. viewport->size = { w, h };
  25. viewport->logicalSize = { w, h };
  26. viewport->viewport = { 0, 0, viewport->size.x * viewport->scale, viewport->size.y * viewport->scale };
  27. viewport->resize ();
  28. zwlr_layer_surface_v1_ack_configure (surface, serial);
  29. }
  30. static void handleLSClosed (void* data, zwlr_layer_surface_v1* surface) {
  31. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  32. viewport->closed = true;
  33. viewport->rendering = false;
  34. }
  35. static void handleXdgSurfaceConfigure (void* data, xdg_surface* surface, uint32_t serial) {
  36. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  37. const auto pending = viewport->pendingToplevelSize;
  38. xdg_surface_ack_configure (surface, serial);
  39. viewport->toplevelConfigured = true;
  40. if (pending.x <= 0 || pending.y <= 0 || pending == viewport->size) {
  41. return;
  42. }
  43. viewport->size = pending;
  44. viewport->logicalSize = pending;
  45. viewport->viewport = { 0, 0, viewport->size.x * viewport->scale, viewport->size.y * viewport->scale };
  46. viewport->resize ();
  47. }
  48. static void
  49. handleXdgToplevelConfigure (void* data, xdg_toplevel*, int32_t width, int32_t height, wl_array* /* states */) {
  50. static_cast<WaylandOutputViewport*> (data)->pendingToplevelSize = { width, height };
  51. }
  52. static void handleXdgToplevelClose (void* data, xdg_toplevel*) {
  53. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  54. viewport->closed = true;
  55. viewport->rendering = false;
  56. }
  57. static void handleXdgToplevelBounds (void*, xdg_toplevel*, int32_t, int32_t) { }
  58. static void handleXdgToplevelCapabilities (void*, xdg_toplevel*, wl_array*) { }
  59. constexpr xdg_surface_listener xdgSurfaceListener = { .configure = handleXdgSurfaceConfigure };
  60. constexpr xdg_toplevel_listener xdgToplevelListener = {
  61. .configure = handleXdgToplevelConfigure,
  62. .close = handleXdgToplevelClose,
  63. .configure_bounds = handleXdgToplevelBounds,
  64. .wm_capabilities = handleXdgToplevelCapabilities,
  65. };
  66. static void geometry (
  67. void* data, wl_output* output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
  68. const char* make, const char* model, int32_t transform
  69. ) {
  70. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  71. // only use geometry position as fallback if xdg-output hasn't provided one
  72. if (!viewport->hasXdgLogicalPosition) {
  73. viewport->globalPosition = { x, y };
  74. }
  75. sLog.debug ("SPAN DEBUG geometry: output '", viewport->name, "' position=(", x, ",", y, ") transform=", transform);
  76. }
  77. static void mode (void* data, wl_output* output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
  78. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  79. // physical pixels; logicalSize comes from xdg-output or the layer shell configure
  80. viewport->size = { width, height };
  81. viewport->viewport = { 0, 0, viewport->size.x * viewport->scale, viewport->size.y * viewport->scale };
  82. if (viewport->hasShellSurface ()) {
  83. viewport->resize ();
  84. }
  85. if (viewport->initialized) {
  86. viewport->getDriver ()->getOutput ().reset ();
  87. }
  88. }
  89. static void done (void* data, wl_output* wl_output) { static_cast<WaylandOutputViewport*> (data)->initialized = true; }
  90. static void scale (void* data, wl_output* wl_output, int32_t scale) {
  91. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  92. viewport->scale = scale;
  93. if (viewport->hasShellSurface ()) {
  94. viewport->resize ();
  95. }
  96. if (viewport->initialized) {
  97. viewport->getDriver ()->getOutput ().reset ();
  98. }
  99. }
  100. static void name (void* data, wl_output* wl_output, const char* name) {
  101. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  102. if (name) {
  103. viewport->name = name;
  104. }
  105. viewport->getDriver ()->getOutput ().reset ();
  106. }
  107. static void description (void* data, wl_output* wl_output, const char* description) {
  108. // ignored
  109. }
  110. static void surfaceFrameCallback (void* data, struct wl_callback* cb, uint32_t time) {
  111. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  112. wl_callback_destroy (cb);
  113. viewport->frameCallback = nullptr;
  114. viewport->renderFrame ();
  115. }
  116. constexpr struct wl_callback_listener frameListener = { .done = surfaceFrameCallback };
  117. constexpr wl_output_listener outputListener
  118. = { .geometry = geometry, .mode = mode, .done = done, .scale = scale, .name = name, .description = description };
  119. constexpr struct zwlr_layer_surface_v1_listener layerSurfaceListener = {
  120. .configure = handleLSConfigure,
  121. .closed = handleLSClosed,
  122. };
  123. static void xdgOutputLogicalPosition (void* data, struct zxdg_output_v1* xdg_output, int32_t x, int32_t y) {
  124. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  125. viewport->globalPosition = { x, y };
  126. viewport->hasXdgLogicalPosition = true;
  127. sLog.debug ("SPAN DEBUG xdg-output logical_position: '", viewport->name, "' position=(", x, ",", y, ")");
  128. }
  129. static void xdgOutputLogicalSize (void* data, struct zxdg_output_v1* xdg_output, int32_t width, int32_t height) {
  130. const auto viewport = static_cast<WaylandOutputViewport*> (data);
  131. viewport->logicalSize = { width, height };
  132. if (viewport->initialized) {
  133. viewport->getDriver ()->getOutput ().reset ();
  134. }
  135. }
  136. static void xdgOutputDone (void* data, struct zxdg_output_v1* xdg_output) {
  137. // deprecated since xdg-output v3, compositor uses wl_output.done instead
  138. }
  139. static void xdgOutputName (void* data, struct zxdg_output_v1* xdg_output, const char* name) {
  140. // already handled by wl_output.name
  141. }
  142. static void xdgOutputDescription (void* data, struct zxdg_output_v1* xdg_output, const char* description) {
  143. // ignored
  144. }
  145. constexpr struct zxdg_output_v1_listener xdgOutputListener = {
  146. .logical_position = xdgOutputLogicalPosition,
  147. .logical_size = xdgOutputLogicalSize,
  148. .done = xdgOutputDone,
  149. .name = xdgOutputName,
  150. .description = xdgOutputDescription,
  151. };
  152. WaylandOutputViewport::WaylandOutputViewport (
  153. WaylandOpenGLDriver* driver, uint32_t waylandName, struct wl_registry* registry
  154. ) : OutputViewport ({ 0, 0, 0, 0 }, "", true), size ({ 0, 0 }), waylandName (waylandName), m_driver (driver) {
  155. this->output = static_cast<wl_output*> (wl_registry_bind (registry, waylandName, &wl_output_interface, 4));
  156. wl_output_add_listener (output, &outputListener, this);
  157. }
  158. void WaylandOutputViewport::setupXdgOutput (zxdg_output_manager_v1* manager) {
  159. this->xdgOutput = zxdg_output_manager_v1_get_xdg_output (manager, this->output);
  160. zxdg_output_v1_add_listener (this->xdgOutput, &xdgOutputListener, this);
  161. }
  162. static void descriptionInfoDone (void* data, wp_image_description_info_v1* info) {
  163. auto* viewport = static_cast<WaylandOutputViewport*> (data);
  164. const auto& description = viewport->pendingDescription;
  165. const uint32_t peak = description.targetMaxLuminance ? description.targetMaxLuminance : description.maxLuminance;
  166. // KWin describes an HDR output as PQ; a plain SDR output peaks at about its reference white
  167. const bool hdr = description.tf == WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_ST2084_PQ
  168. || (description.referenceLuminance > 0 && peak * 2 > description.referenceLuminance * 3);
  169. wp_image_description_info_v1_destroy (info);
  170. viewport->outputLuminance = glm::vec2 (description.referenceLuminance, peak);
  171. if (hdr != viewport->outputHDR) {
  172. viewport->outputHDR = hdr;
  173. sLog.out ("Output ", viewport->name, hdr ? " switched to HDR, drawing PQ" : " switched to SDR");
  174. viewport->applyImageDescription ();
  175. }
  176. }
  177. static void descriptionInfoIcc (void*, wp_image_description_info_v1*, int32_t fd, uint32_t) { close (fd); }
  178. static void descriptionInfoPrimaries (
  179. void*, wp_image_description_info_v1*, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t
  180. ) { }
  181. static void descriptionInfoPrimariesNamed (void*, wp_image_description_info_v1*, uint32_t) { }
  182. static void descriptionInfoTransferPower (void*, wp_image_description_info_v1*, uint32_t) { }
  183. static void descriptionInfoTransferNamed (void* data, wp_image_description_info_v1*, uint32_t tf) {
  184. static_cast<WaylandOutputViewport*> (data)->pendingDescription.tf = tf;
  185. }
  186. static void
  187. descriptionInfoLuminances (void* data, wp_image_description_info_v1*, uint32_t, uint32_t max, uint32_t reference) {
  188. auto& description = static_cast<WaylandOutputViewport*> (data)->pendingDescription;
  189. description.maxLuminance = max;
  190. description.referenceLuminance = reference;
  191. }
  192. static void descriptionInfoTargetLuminance (void* data, wp_image_description_info_v1*, uint32_t, uint32_t max) {
  193. static_cast<WaylandOutputViewport*> (data)->pendingDescription.targetMaxLuminance = max;
  194. }
  195. static void descriptionInfoTargetMaxCll (void*, wp_image_description_info_v1*, uint32_t) { }
  196. static void descriptionInfoTargetMaxFall (void*, wp_image_description_info_v1*, uint32_t) { }
  197. constexpr wp_image_description_info_v1_listener descriptionInfoListener = {
  198. .done = descriptionInfoDone,
  199. .icc_file = descriptionInfoIcc,
  200. .primaries = descriptionInfoPrimaries,
  201. .primaries_named = descriptionInfoPrimariesNamed,
  202. .tf_power = descriptionInfoTransferPower,
  203. .tf_named = descriptionInfoTransferNamed,
  204. .luminances = descriptionInfoLuminances,
  205. .target_primaries = descriptionInfoPrimaries,
  206. .target_luminance = descriptionInfoTargetLuminance,
  207. .target_max_cll = descriptionInfoTargetMaxCll,
  208. .target_max_fall = descriptionInfoTargetMaxFall,
  209. };
  210. static void outputDescriptionFailed (void* data, wp_image_description_v1* description, uint32_t, const char* message) {
  211. sLog.error ("Cannot tell whether ", static_cast<WaylandOutputViewport*> (data)->name, " runs in HDR: ", message);
  212. wp_image_description_v1_destroy (description);
  213. }
  214. static void outputDescriptionReady (void* data, wp_image_description_v1* description, uint32_t) {
  215. auto* viewport = static_cast<WaylandOutputViewport*> (data);
  216. viewport->pendingDescription = {};
  217. wp_image_description_info_v1_add_listener (
  218. wp_image_description_v1_get_information (description), &descriptionInfoListener, viewport
  219. );
  220. wp_image_description_v1_destroy (description);
  221. }
  222. constexpr wp_image_description_v1_listener outputDescriptionListener = {
  223. .failed = outputDescriptionFailed,
  224. .ready = outputDescriptionReady,
  225. };
  226. static void outputDescriptionChanged (void* data, wp_color_management_output_v1*) {
  227. static_cast<WaylandOutputViewport*> (data)->queryOutputDescription ();
  228. }
  229. constexpr wp_color_management_output_v1_listener colorOutputListener = {
  230. .image_description_changed = outputDescriptionChanged,
  231. };
  232. void WaylandOutputViewport::setupColorManagement () {
  233. if (this->colorOutput != nullptr || !m_driver->isHDRAvailable ()) {
  234. return;
  235. }
  236. this->colorOutput = wp_color_manager_v1_get_output (m_driver->getWaylandContext ()->colorManager, this->output);
  237. wp_color_management_output_v1_add_listener (this->colorOutput, &colorOutputListener, this);
  238. this->queryOutputDescription ();
  239. }
  240. void WaylandOutputViewport::queryOutputDescription () {
  241. wp_image_description_v1_add_listener (
  242. wp_color_management_output_v1_get_image_description (this->colorOutput), &outputDescriptionListener, this
  243. );
  244. }
  245. void WaylandOutputViewport::applyImageDescription () {
  246. if (this->colorSurface == nullptr) {
  247. return;
  248. }
  249. // takes effect with the next frame's commit
  250. if (this->outputHDR) {
  251. wp_color_management_surface_v1_set_image_description (
  252. this->colorSurface, m_driver->getHDRDescription (), WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL
  253. );
  254. } else {
  255. wp_color_management_surface_v1_unset_image_description (this->colorSurface);
  256. }
  257. }
  258. bool WaylandOutputViewport::isHDR () const { return this->colorSurface != nullptr && this->outputHDR; }
  259. void WaylandOutputViewport::setupLS () {
  260. surface = wl_compositor_create_surface (m_driver->getWaylandContext ()->compositor);
  261. zwlr_layer_shell_v1_layer wlrLayer;
  262. switch (m_driver->getApp ().getContext ().settings.render.wayland.layer) {
  263. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_BACKGROUND:
  264. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
  265. break;
  266. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_TOP:
  267. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
  268. break;
  269. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_OVERLAY:
  270. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
  271. break;
  272. case WallpaperEngine::Application::ApplicationContext::WAYLAND_LAYER_BOTTOM:
  273. default:
  274. wlrLayer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
  275. break;
  276. }
  277. // KWin treats the "desktop" namespace as a desktop window, so 'show desktop' leaves it alone
  278. layerSurface = zwlr_layer_shell_v1_get_layer_surface (
  279. m_driver->getWaylandContext ()->layerShell, surface, output, wlrLayer, "desktop"
  280. );
  281. if (!layerSurface) {
  282. sLog.exception ("Failed to get a layer surface");
  283. }
  284. zwlr_layer_surface_v1_set_size (layerSurface, 0, 0);
  285. zwlr_layer_surface_v1_set_anchor (
  286. layerSurface,
  287. ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
  288. | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
  289. );
  290. zwlr_layer_surface_v1_set_keyboard_interactivity (layerSurface, false);
  291. zwlr_layer_surface_v1_add_listener (layerSurface, &layerSurfaceListener, this);
  292. zwlr_layer_surface_v1_set_exclusive_zone (layerSurface, -1);
  293. #ifdef ENABLE_KDE_FEATURES
  294. if (m_driver->getWaylandContext ()->plasmaShell) {
  295. plasmaSurface = org_kde_plasma_shell_get_surface (m_driver->getWaylandContext ()->plasmaShell, surface);
  296. if (plasmaSurface) {
  297. org_kde_plasma_surface_set_role (plasmaSurface, ORG_KDE_PLASMA_SURFACE_ROLE_DESKTOP);
  298. org_kde_plasma_surface_set_skip_taskbar (plasmaSurface, 1);
  299. org_kde_plasma_surface_set_skip_switcher (plasmaSurface, 1);
  300. }
  301. }
  302. #endif
  303. this->prepareSurface ();
  304. wl_surface_commit (surface);
  305. wl_display_roundtrip (m_driver->getWaylandContext ()->display);
  306. // the roundtrip can already deliver closed (river without a layout)
  307. if (closed) {
  308. return;
  309. }
  310. this->createEGLSurface ();
  311. }
  312. void WaylandOutputViewport::setupToplevel () {
  313. auto* context = m_driver->getWaylandContext ();
  314. surface = wl_compositor_create_surface (context->compositor);
  315. xdgSurface = xdg_wm_base_get_xdg_surface (context->xdgWmBase, surface);
  316. xdg_surface_add_listener (xdgSurface, &xdgSurfaceListener, this);
  317. xdgToplevel = xdg_surface_get_toplevel (xdgSurface);
  318. xdg_toplevel_add_listener (xdgToplevel, &xdgToplevelListener, this);
  319. // the extension matches on these, set before the first commit
  320. const std::string title = std::string (WallpaperEngine::Desktop::GnomeShell::TITLE_PREFIX) + this->name;
  321. xdg_toplevel_set_app_id (xdgToplevel, WallpaperEngine::Desktop::GnomeShell::APP_ID);
  322. xdg_toplevel_set_title (xdgToplevel, title.c_str ());
  323. // not fullscreen, Mutter would hide the top bar; the extension places it by the title's output
  324. this->prepareSurface ();
  325. wl_surface_commit (surface);
  326. for (int i = 0; i < 10 && !toplevelConfigured && !closed; i++) {
  327. if (wl_display_roundtrip (context->display) == -1) {
  328. break;
  329. }
  330. }
  331. if (closed) {
  332. return;
  333. }
  334. if (!toplevelConfigured) {
  335. sLog.error ("Output ", this->name, ": the compositor never configured the wallpaper window");
  336. }
  337. // 0x0 configure: use the output's logical size
  338. if (this->size.x <= 0 || this->size.y <= 0 || this->pendingToplevelSize.x <= 0) {
  339. this->size = this->logicalSize.x > 0 ? this->logicalSize : this->size / glm::max (this->scale, 1);
  340. this->logicalSize = this->size;
  341. this->viewport = { 0, 0, this->size.x * this->scale, this->size.y * this->scale };
  342. }
  343. this->createEGLSurface ();
  344. }
  345. void WaylandOutputViewport::prepareSurface () {
  346. this->setInputRegion (m_driver->getApp ().getContext ().settings.mouse.enabled);
  347. // fully opaque: lets the compositor skip alpha-blending, always a win since wallpapers are the
  348. // bottommost visible content
  349. wl_region* opaqueRegion = wl_compositor_create_region (m_driver->getWaylandContext ()->compositor);
  350. wl_region_add (opaqueRegion, 0, 0, INT32_MAX, INT32_MAX);
  351. wl_surface_set_opaque_region (surface, opaqueRegion);
  352. wl_region_destroy (opaqueRegion);
  353. if (m_driver->isHDRAvailable ()) {
  354. this->setupColorManagement ();
  355. this->colorSurface = wp_color_manager_v1_get_surface (m_driver->getWaylandContext ()->colorManager, surface);
  356. this->applyImageDescription ();
  357. }
  358. }
  359. void WaylandOutputViewport::createEGLSurface () {
  360. eglWindow = wl_egl_window_create (surface, size.x * scale, size.y * scale);
  361. eglSurface = m_driver->getEGLContext ()->eglCreatePlatformWindowSurfaceEXT (
  362. m_driver->getEGLContext ()->display, m_driver->getEGLContext ()->config, eglWindow, nullptr
  363. );
  364. // Mutter flags an empty commit between a toplevel's configure ack and its first buffer
  365. if (!this->xdgToplevel) {
  366. wl_surface_commit (surface);
  367. }
  368. wl_display_roundtrip (m_driver->getWaylandContext ()->display);
  369. wl_display_flush (m_driver->getWaylandContext ()->display);
  370. if (closed) {
  371. return;
  372. }
  373. static const auto XCURSORSIZE = getenv ("XCURSOR_SIZE") ? std::stoi (getenv ("XCURSOR_SIZE")) : 24;
  374. cursorTheme
  375. = wl_cursor_theme_load (getenv ("XCURSOR_THEME"), XCURSORSIZE * scale, m_driver->getWaylandContext ()->shm);
  376. if (!cursorTheme) {
  377. sLog.exception ("Failed to get a cursor theme");
  378. }
  379. pointer = wl_cursor_theme_get_cursor (cursorTheme, "left_ptr");
  380. cursorSurface = wl_compositor_create_surface (m_driver->getWaylandContext ()->compositor);
  381. if (!cursorSurface) {
  382. sLog.exception ("Failed to get a cursor surface");
  383. }
  384. if (eglMakeCurrent (
  385. m_driver->getEGLContext ()->display, eglSurface, eglSurface, m_driver->getEGLContext ()->context
  386. )
  387. == EGL_FALSE) {
  388. sLog.exception ("Failed to make egl current");
  389. }
  390. // frames are paced by our own frame callbacks; with the default interval of 1 Mesa blocks inside
  391. // eglSwapBuffers until the compositor shows the previous frame, which never happens while the
  392. // surface isn't presented and stalls the whole main loop
  393. eglSwapInterval (m_driver->getEGLContext ()->display, 0);
  394. this->m_driver->getOutput ().reset ();
  395. }
  396. WaylandOpenGLDriver* WaylandOutputViewport::getDriver () const { return this->m_driver; }
  397. void WaylandOutputViewport::makeCurrent () {
  398. const EGLBoolean result = eglMakeCurrent (
  399. m_driver->getEGLContext ()->display, eglSurface, eglSurface, m_driver->getEGLContext ()->context
  400. );
  401. if (result == EGL_FALSE) {
  402. sLog.error ("Couldn't make egl current");
  403. }
  404. }
  405. void WaylandOutputViewport::setInputRegion (bool everything) {
  406. wl_region* region = wl_compositor_create_region (m_driver->getWaylandContext ()->compositor);
  407. if (everything) {
  408. wl_region_add (region, 0, 0, INT32_MAX, INT32_MAX);
  409. }
  410. wl_surface_set_input_region (this->surface, region);
  411. wl_region_destroy (region);
  412. }
  413. void WaylandOutputViewport::setWantsPointerButtons (bool wants) {
  414. if (this->surface == nullptr || !m_driver->getApp ().getContext ().settings.mouse.enabled) {
  415. return;
  416. }
  417. // without a global cursor source, hover and parallax need the pointer on our surface
  418. const bool passthrough = !wants && m_driver->hasGlobalCursor ();
  419. if (passthrough == this->inputPassthrough) {
  420. return;
  421. }
  422. this->inputPassthrough = passthrough;
  423. // applied by the next commit
  424. this->setInputRegion (!passthrough);
  425. // the release would go to the desktop now
  426. this->leftClick = WallpaperEngine::Input::MouseClickStatus::Released;
  427. this->rightClick = WallpaperEngine::Input::MouseClickStatus::Released;
  428. sLog.out (
  429. "Output ", this->name,
  430. passthrough ? ": the wallpaper does not use clicks, they go to the desktop below"
  431. : ": the wallpaper uses clicks, the pointer stays on it"
  432. );
  433. }
  434. void WaylandOutputViewport::renderFrame () {
  435. // the driver frees closed viewports on its next pass
  436. if (this->closed) {
  437. return;
  438. }
  439. this->rendering = true;
  440. this->m_driver->getApp ().update (this);
  441. this->rendering = false;
  442. }
  443. void WaylandOutputViewport::swapOutput () {
  444. this->callbackInitialized = true;
  445. this->lastSwap = std::chrono::steady_clock::now ();
  446. this->makeCurrent ();
  447. // a forced frame replaces the pending callback, two live callbacks would double the frame rate
  448. if (frameCallback) {
  449. wl_callback_destroy (frameCallback);
  450. }
  451. frameCallback = wl_surface_frame (surface);
  452. wl_callback_add_listener (frameCallback, &frameListener, this);
  453. eglSwapBuffers (m_driver->getEGLContext ()->display, this->eglSurface);
  454. wl_surface_set_buffer_scale (surface, scale);
  455. wl_surface_damage_buffer (surface, 0, 0, INT32_MAX, INT32_MAX);
  456. wl_surface_commit (surface);
  457. }
  458. void WaylandOutputViewport::resize () {
  459. if (!this->eglWindow) {
  460. return;
  461. }
  462. wl_egl_window_resize (this->eglWindow, this->size.x * this->scale, this->size.y * this->scale, 0, 0);
  463. this->getDriver ()->getOutput ().reset ();
  464. }