display_server_wayland.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /**************************************************************************/
  2. /* display_server_wayland.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "display_server_wayland.h"
  31. #ifdef WAYLAND_ENABLED
  32. #define WAYLAND_DISPLAY_SERVER_DEBUG_LOGS_ENABLED
  33. #ifdef WAYLAND_DISPLAY_SERVER_DEBUG_LOGS_ENABLED
  34. #define DEBUG_LOG_WAYLAND(...) print_verbose(__VA_ARGS__)
  35. #else
  36. #define DEBUG_LOG_WAYLAND(...)
  37. #endif
  38. #ifdef VULKAN_ENABLED
  39. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  40. #endif
  41. #ifdef GLES3_ENABLED
  42. #include "detect_prime_egl.h"
  43. #include "drivers/gles3/rasterizer_gles3.h"
  44. #endif
  45. String DisplayServerWayland::_get_app_id_from_context(Context p_context) {
  46. String app_id;
  47. switch (p_context) {
  48. case CONTEXT_EDITOR: {
  49. app_id = "org.godotengine.Editor";
  50. } break;
  51. case CONTEXT_PROJECTMAN: {
  52. app_id = "org.godotengine.ProjectManager";
  53. } break;
  54. case CONTEXT_ENGINE:
  55. default: {
  56. String config_name = GLOBAL_GET("application/config/name");
  57. if (config_name.length() != 0) {
  58. app_id = config_name;
  59. } else {
  60. app_id = "org.godotengine.Godot";
  61. }
  62. }
  63. }
  64. return app_id;
  65. }
  66. void DisplayServerWayland::_send_window_event(WindowEvent p_event) {
  67. WindowData &wd = main_window;
  68. if (wd.window_event_callback.is_valid()) {
  69. Variant event = int(p_event);
  70. wd.window_event_callback.call(event);
  71. }
  72. }
  73. void DisplayServerWayland::dispatch_input_events(const Ref<InputEvent> &p_event) {
  74. ((DisplayServerWayland *)(get_singleton()))->_dispatch_input_event(p_event);
  75. }
  76. void DisplayServerWayland::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  77. Callable callable = main_window.input_event_callback;
  78. if (callable.is_valid()) {
  79. callable.call(p_event);
  80. }
  81. }
  82. void DisplayServerWayland::_resize_window(const Size2i &p_size) {
  83. WindowData &wd = main_window;
  84. wd.rect.size = p_size;
  85. #ifdef RD_ENABLED
  86. if (wd.visible && context_rd) {
  87. context_rd->window_resize(MAIN_WINDOW_ID, wd.rect.size.width, wd.rect.size.height);
  88. }
  89. #endif
  90. #ifdef GLES3_ENABLED
  91. if (wd.visible && egl_manager) {
  92. wl_egl_window_resize(wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height, 0, 0);
  93. }
  94. #endif
  95. if (wd.rect_changed_callback.is_valid()) {
  96. wd.rect_changed_callback.call(wd.rect);
  97. }
  98. }
  99. void DisplayServerWayland::_show_window() {
  100. MutexLock mutex_lock(wayland_thread.mutex);
  101. WindowData &wd = main_window;
  102. if (!wd.visible) {
  103. DEBUG_LOG_WAYLAND("Showing window.");
  104. // Showing this window will reset its mode with whatever the compositor
  105. // reports. We'll save the mode beforehand so that we can reapply it later.
  106. // TODO: Fix/Port/Move/Whatever to `WaylandThread` APIs.
  107. WindowMode setup_mode = wd.mode;
  108. wayland_thread.window_create(MAIN_WINDOW_ID, wd.rect.size.width, wd.rect.size.height);
  109. wayland_thread.window_set_min_size(MAIN_WINDOW_ID, wd.min_size);
  110. wayland_thread.window_set_max_size(MAIN_WINDOW_ID, wd.max_size);
  111. wayland_thread.window_set_app_id(MAIN_WINDOW_ID, _get_app_id_from_context(context));
  112. wayland_thread.window_set_borderless(MAIN_WINDOW_ID, window_get_flag(WINDOW_FLAG_BORDERLESS));
  113. // NOTE: The XDG shell protocol is built in a way that causes the window to
  114. // be immediately shown as soon as a valid buffer is assigned to it. Hence,
  115. // the only acceptable way of implementing window showing is to move the
  116. // graphics context window creation logic here.
  117. #ifdef RD_ENABLED
  118. if (context_rd) {
  119. union {
  120. #ifdef VULKAN_ENABLED
  121. VulkanContextWayland::WindowPlatformData vulkan;
  122. #endif
  123. } wpd;
  124. #ifdef VULKAN_ENABLED
  125. if (rendering_driver == "vulkan") {
  126. wpd.vulkan.surface = wayland_thread.window_get_wl_surface(wd.id);
  127. wpd.vulkan.display = wayland_thread.get_wl_display();
  128. }
  129. #endif
  130. Error err = context_rd->window_create(wd.id, wd.vsync_mode, wd.rect.size.width, wd.rect.size.height, &wpd);
  131. ERR_FAIL_COND_MSG(err != OK, vformat("Can't create a %s window", context_rd->get_api_name()));
  132. emulate_vsync = (context_rd->get_vsync_mode(wd.id) == DisplayServer::VSYNC_ENABLED);
  133. if (emulate_vsync) {
  134. print_verbose("VSYNC: manually throttling frames using MAILBOX.");
  135. context_rd->set_vsync_mode(wd.id, DisplayServer::VSYNC_MAILBOX);
  136. }
  137. }
  138. #endif
  139. #ifdef GLES3_ENABLED
  140. if (egl_manager) {
  141. struct wl_surface *wl_surface = wayland_thread.window_get_wl_surface(wd.id);
  142. wd.wl_egl_window = wl_egl_window_create(wl_surface, wd.rect.size.width, wd.rect.size.height);
  143. Error err = egl_manager->window_create(MAIN_WINDOW_ID, wayland_thread.get_wl_display(), wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height);
  144. ERR_FAIL_COND_MSG(err == ERR_CANT_CREATE, "Can't show a GLES3 window.");
  145. window_set_vsync_mode(wd.vsync_mode, MAIN_WINDOW_ID);
  146. }
  147. #endif
  148. // NOTE: The public window-handling methods might depend on this flag being
  149. // set. Ensure to not make any of these calls before this assignment.
  150. wd.visible = true;
  151. // Actually try to apply the window's mode now that it's visible.
  152. window_set_mode(setup_mode);
  153. wayland_thread.window_set_title(MAIN_WINDOW_ID, wd.title);
  154. }
  155. }
  156. // Interface methods.
  157. bool DisplayServerWayland::has_feature(Feature p_feature) const {
  158. switch (p_feature) {
  159. case FEATURE_MOUSE:
  160. case FEATURE_CLIPBOARD:
  161. case FEATURE_CURSOR_SHAPE:
  162. case FEATURE_WINDOW_TRANSPARENCY:
  163. case FEATURE_SWAP_BUFFERS:
  164. case FEATURE_KEEP_SCREEN_ON:
  165. case FEATURE_CLIPBOARD_PRIMARY:
  166. case FEATURE_HIDPI: {
  167. return true;
  168. } break;
  169. default: {
  170. return false;
  171. }
  172. }
  173. }
  174. String DisplayServerWayland::get_name() const {
  175. return "Wayland";
  176. }
  177. #ifdef SPEECHD_ENABLED
  178. bool DisplayServerWayland::tts_is_speaking() const {
  179. ERR_FAIL_NULL_V(tts, false);
  180. return tts->is_speaking();
  181. }
  182. bool DisplayServerWayland::tts_is_paused() const {
  183. ERR_FAIL_NULL_V(tts, false);
  184. return tts->is_paused();
  185. }
  186. TypedArray<Dictionary> DisplayServerWayland::tts_get_voices() const {
  187. ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
  188. return tts->get_voices();
  189. }
  190. void DisplayServerWayland::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  191. ERR_FAIL_NULL(tts);
  192. tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt);
  193. }
  194. void DisplayServerWayland::tts_pause() {
  195. ERR_FAIL_NULL(tts);
  196. tts->pause();
  197. }
  198. void DisplayServerWayland::tts_resume() {
  199. ERR_FAIL_NULL(tts);
  200. tts->resume();
  201. }
  202. void DisplayServerWayland::tts_stop() {
  203. ERR_FAIL_NULL(tts);
  204. tts->stop();
  205. }
  206. #endif
  207. #ifdef DBUS_ENABLED
  208. bool DisplayServerWayland::is_dark_mode_supported() const {
  209. return portal_desktop->is_supported();
  210. }
  211. bool DisplayServerWayland::is_dark_mode() const {
  212. switch (portal_desktop->get_appearance_color_scheme()) {
  213. case 1:
  214. // Prefers dark theme.
  215. return true;
  216. case 2:
  217. // Prefers light theme.
  218. return false;
  219. default:
  220. // Preference unknown.
  221. return false;
  222. }
  223. }
  224. #endif
  225. void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
  226. if (p_mode == mouse_mode) {
  227. return;
  228. }
  229. MutexLock mutex_lock(wayland_thread.mutex);
  230. bool show_cursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
  231. if (show_cursor) {
  232. if (custom_cursors.has(cursor_shape)) {
  233. wayland_thread.cursor_set_custom_shape(cursor_shape);
  234. } else {
  235. wayland_thread.cursor_set_shape(cursor_shape);
  236. }
  237. } else {
  238. wayland_thread.cursor_hide();
  239. }
  240. WaylandThread::PointerConstraint constraint = WaylandThread::PointerConstraint::NONE;
  241. switch (p_mode) {
  242. case DisplayServer::MOUSE_MODE_CAPTURED: {
  243. constraint = WaylandThread::PointerConstraint::LOCKED;
  244. } break;
  245. case DisplayServer::MOUSE_MODE_CONFINED:
  246. case DisplayServer::MOUSE_MODE_CONFINED_HIDDEN: {
  247. constraint = WaylandThread::PointerConstraint::CONFINED;
  248. } break;
  249. default: {
  250. }
  251. }
  252. wayland_thread.pointer_set_constraint(constraint);
  253. mouse_mode = p_mode;
  254. }
  255. DisplayServerWayland::MouseMode DisplayServerWayland::mouse_get_mode() const {
  256. return mouse_mode;
  257. }
  258. // NOTE: This is hacked together (and not guaranteed to work in the first place)
  259. // as for some reason the there's no proper way to ask the compositor to warp
  260. // the pointer, although, at the time of writing, there's a proposal for a
  261. // proper protocol for this. See:
  262. // https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/158
  263. void DisplayServerWayland::warp_mouse(const Point2i &p_to) {
  264. MutexLock mutex_lock(wayland_thread.mutex);
  265. WaylandThread::PointerConstraint old_constraint = wayland_thread.pointer_get_constraint();
  266. wayland_thread.pointer_set_constraint(WaylandThread::PointerConstraint::LOCKED);
  267. wayland_thread.pointer_set_hint(p_to);
  268. wayland_thread.pointer_set_constraint(old_constraint);
  269. }
  270. Point2i DisplayServerWayland::mouse_get_position() const {
  271. MutexLock mutex_lock(wayland_thread.mutex);
  272. // We can't properly implement this method by design.
  273. // This is the best we can do unfortunately.
  274. return Input::get_singleton()->get_mouse_position();
  275. return Point2i();
  276. }
  277. BitField<MouseButtonMask> DisplayServerWayland::mouse_get_button_state() const {
  278. MutexLock mutex_lock(wayland_thread.mutex);
  279. // Are we sure this is the only way? This seems sus.
  280. // TODO: Handle tablets properly.
  281. //mouse_button_mask.set_flag(MouseButtonMask((int64_t)wls.current_seat->tablet_tool_data.pressed_button_mask));
  282. return wayland_thread.pointer_get_button_mask();
  283. }
  284. // NOTE: According to the Wayland specification, this method will only do
  285. // anything if the user has interacted with the application by sending a
  286. // "recent enough" input event.
  287. // TODO: Add this limitation to the documentation.
  288. void DisplayServerWayland::clipboard_set(const String &p_text) {
  289. MutexLock mutex_lock(wayland_thread.mutex);
  290. wayland_thread.selection_set_text(p_text);
  291. }
  292. String DisplayServerWayland::clipboard_get() const {
  293. MutexLock mutex_lock(wayland_thread.mutex);
  294. Vector<uint8_t> data;
  295. const String text_mimes[] = {
  296. "text/plain;charset=utf-8",
  297. "text/plain",
  298. };
  299. for (String mime : text_mimes) {
  300. if (wayland_thread.selection_has_mime(mime)) {
  301. print_verbose(vformat("Selecting media type \"%s\" from offered types.", mime));
  302. data = wayland_thread.selection_get_mime(mime);
  303. break;
  304. }
  305. }
  306. return String::utf8((const char *)data.ptr(), data.size());
  307. }
  308. Ref<Image> DisplayServerWayland::clipboard_get_image() const {
  309. MutexLock mutex_lock(wayland_thread.mutex);
  310. Ref<Image> image;
  311. image.instantiate();
  312. Error err = OK;
  313. // TODO: Fallback to next media type on missing module or parse error.
  314. if (wayland_thread.selection_has_mime("image/png")) {
  315. err = image->load_png_from_buffer(wayland_thread.selection_get_mime("image/png"));
  316. } else if (wayland_thread.selection_has_mime("image/jpeg")) {
  317. err = image->load_jpg_from_buffer(wayland_thread.selection_get_mime("image/jpeg"));
  318. } else if (wayland_thread.selection_has_mime("image/webp")) {
  319. err = image->load_webp_from_buffer(wayland_thread.selection_get_mime("image/webp"));
  320. } else if (wayland_thread.selection_has_mime("image/svg+xml")) {
  321. err = image->load_svg_from_buffer(wayland_thread.selection_get_mime("image/svg+xml"));
  322. } else if (wayland_thread.selection_has_mime("image/bmp")) {
  323. err = image->load_bmp_from_buffer(wayland_thread.selection_get_mime("image/bmp"));
  324. } else if (wayland_thread.selection_has_mime("image/x-tga")) {
  325. err = image->load_tga_from_buffer(wayland_thread.selection_get_mime("image/x-tga"));
  326. } else if (wayland_thread.selection_has_mime("image/x-targa")) {
  327. err = image->load_tga_from_buffer(wayland_thread.selection_get_mime("image/x-targa"));
  328. } else if (wayland_thread.selection_has_mime("image/ktx")) {
  329. err = image->load_ktx_from_buffer(wayland_thread.selection_get_mime("image/ktx"));
  330. }
  331. ERR_FAIL_COND_V(err != OK, Ref<Image>());
  332. return image;
  333. }
  334. void DisplayServerWayland::clipboard_set_primary(const String &p_text) {
  335. MutexLock mutex_lock(wayland_thread.mutex);
  336. wayland_thread.primary_set_text(p_text);
  337. }
  338. String DisplayServerWayland::clipboard_get_primary() const {
  339. MutexLock mutex_lock(wayland_thread.mutex);
  340. Vector<uint8_t> data;
  341. const String text_mimes[] = {
  342. "text/plain;charset=utf-8",
  343. "text/plain",
  344. };
  345. for (String mime : text_mimes) {
  346. if (wayland_thread.primary_has_mime(mime)) {
  347. print_verbose(vformat("Selecting media type \"%s\" from offered types.", mime));
  348. wayland_thread.primary_get_mime(mime);
  349. break;
  350. }
  351. }
  352. return String::utf8((const char *)data.ptr(), data.size());
  353. }
  354. int DisplayServerWayland::get_screen_count() const {
  355. MutexLock mutex_lock(wayland_thread.mutex);
  356. return wayland_thread.get_screen_count();
  357. }
  358. int DisplayServerWayland::get_primary_screen() const {
  359. // AFAIK Wayland doesn't allow knowing (nor we care) about which screen is
  360. // primary.
  361. return 0;
  362. }
  363. Point2i DisplayServerWayland::screen_get_position(int p_screen) const {
  364. MutexLock mutex_lock(wayland_thread.mutex);
  365. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  366. p_screen = window_get_current_screen();
  367. }
  368. return wayland_thread.screen_get_data(p_screen).position;
  369. }
  370. Size2i DisplayServerWayland::screen_get_size(int p_screen) const {
  371. MutexLock mutex_lock(wayland_thread.mutex);
  372. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  373. p_screen = window_get_current_screen();
  374. }
  375. return wayland_thread.screen_get_data(p_screen).size;
  376. }
  377. Rect2i DisplayServerWayland::screen_get_usable_rect(int p_screen) const {
  378. // Unsupported on wayland.
  379. return Rect2i(Point2i(), screen_get_size(p_screen));
  380. }
  381. int DisplayServerWayland::screen_get_dpi(int p_screen) const {
  382. MutexLock mutex_lock(wayland_thread.mutex);
  383. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  384. p_screen = window_get_current_screen();
  385. }
  386. const WaylandThread::ScreenData &data = wayland_thread.screen_get_data(p_screen);
  387. int width_mm = data.physical_size.width;
  388. int height_mm = data.physical_size.height;
  389. double xdpi = (width_mm ? data.size.width / (double)width_mm * 25.4 : 0);
  390. double ydpi = (height_mm ? data.size.height / (double)height_mm * 25.4 : 0);
  391. if (xdpi || ydpi) {
  392. return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1);
  393. }
  394. // Could not get DPI.
  395. return 96;
  396. }
  397. float DisplayServerWayland::screen_get_scale(int p_screen) const {
  398. MutexLock mutex_lock(wayland_thread.mutex);
  399. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  400. p_screen = window_get_current_screen();
  401. }
  402. return wayland_thread.screen_get_data(p_screen).scale;
  403. }
  404. float DisplayServerWayland::screen_get_refresh_rate(int p_screen) const {
  405. MutexLock mutex_lock(wayland_thread.mutex);
  406. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  407. p_screen = window_get_current_screen();
  408. }
  409. return wayland_thread.screen_get_data(p_screen).refresh_rate;
  410. }
  411. void DisplayServerWayland::screen_set_keep_on(bool p_enable) {
  412. MutexLock mutex_lock(wayland_thread.mutex);
  413. if (screen_is_kept_on() == p_enable) {
  414. return;
  415. }
  416. #ifdef DBUS_ENABLED
  417. if (screensaver) {
  418. if (p_enable) {
  419. screensaver->inhibit();
  420. } else {
  421. screensaver->uninhibit();
  422. }
  423. screensaver_inhibited = p_enable;
  424. }
  425. #endif
  426. }
  427. bool DisplayServerWayland::screen_is_kept_on() const {
  428. #ifdef DBUS_ENABLED
  429. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID) || screensaver_inhibited;
  430. #endif
  431. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID);
  432. }
  433. Vector<DisplayServer::WindowID> DisplayServerWayland::get_window_list() const {
  434. MutexLock mutex_lock(wayland_thread.mutex);
  435. Vector<int> ret;
  436. ret.push_back(MAIN_WINDOW_ID);
  437. return ret;
  438. }
  439. int64_t DisplayServerWayland::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  440. MutexLock mutex_lock(wayland_thread.mutex);
  441. switch (p_handle_type) {
  442. case DISPLAY_HANDLE: {
  443. return (int64_t)wayland_thread.get_wl_display();
  444. } break;
  445. case WINDOW_HANDLE: {
  446. return (int64_t)wayland_thread.window_get_wl_surface(p_window);
  447. } break;
  448. case WINDOW_VIEW: {
  449. return 0; // Not supported.
  450. } break;
  451. #ifdef GLES3_ENABLED
  452. case OPENGL_CONTEXT: {
  453. if (egl_manager) {
  454. return (int64_t)egl_manager->get_context(p_window);
  455. }
  456. return 0;
  457. } break;
  458. #endif // GLES3_ENABLED
  459. default: {
  460. return 0;
  461. } break;
  462. }
  463. }
  464. DisplayServer::WindowID DisplayServerWayland::get_window_at_screen_position(const Point2i &p_position) const {
  465. // Standard Wayland APIs don't support this.
  466. return MAIN_WINDOW_ID;
  467. }
  468. void DisplayServerWayland::window_attach_instance_id(ObjectID p_instance, WindowID p_window_id) {
  469. MutexLock mutex_lock(wayland_thread.mutex);
  470. main_window.instance_id = p_instance;
  471. }
  472. ObjectID DisplayServerWayland::window_get_attached_instance_id(WindowID p_window_id) const {
  473. MutexLock mutex_lock(wayland_thread.mutex);
  474. return main_window.instance_id;
  475. }
  476. void DisplayServerWayland::window_set_title(const String &p_title, DisplayServer::WindowID p_window_id) {
  477. MutexLock mutex_lock(wayland_thread.mutex);
  478. WindowData &wd = main_window;
  479. wd.title = p_title;
  480. wayland_thread.window_set_title(MAIN_WINDOW_ID, wd.title);
  481. }
  482. void DisplayServerWayland::window_set_mouse_passthrough(const Vector<Vector2> &p_region, DisplayServer::WindowID p_window_id) {
  483. // TODO
  484. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_mouse_passthrough region %s", p_region));
  485. }
  486. void DisplayServerWayland::window_set_rect_changed_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  487. MutexLock mutex_lock(wayland_thread.mutex);
  488. main_window.rect_changed_callback = p_callable;
  489. }
  490. void DisplayServerWayland::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  491. MutexLock mutex_lock(wayland_thread.mutex);
  492. main_window.window_event_callback = p_callable;
  493. }
  494. void DisplayServerWayland::window_set_input_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  495. MutexLock mutex_lock(wayland_thread.mutex);
  496. main_window.input_event_callback = p_callable;
  497. }
  498. void DisplayServerWayland::window_set_input_text_callback(const Callable &p_callable, WindowID p_window_id) {
  499. MutexLock mutex_lock(wayland_thread.mutex);
  500. main_window.input_text_callback = p_callable;
  501. }
  502. void DisplayServerWayland::window_set_drop_files_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  503. MutexLock mutex_lock(wayland_thread.mutex);
  504. main_window.drop_files_callback = p_callable;
  505. }
  506. int DisplayServerWayland::window_get_current_screen(DisplayServer::WindowID p_window_id) const {
  507. // Standard Wayland APIs don't support getting the screen of a window.
  508. return 0;
  509. }
  510. void DisplayServerWayland::window_set_current_screen(int p_screen, DisplayServer::WindowID p_window_id) {
  511. // Standard Wayland APIs don't support setting the screen of a window.
  512. }
  513. Point2i DisplayServerWayland::window_get_position(DisplayServer::WindowID p_window_id) const {
  514. MutexLock mutex_lock(wayland_thread.mutex);
  515. // We can't know the position of toplevels with the standard protocol.
  516. return Point2i();
  517. }
  518. Point2i DisplayServerWayland::window_get_position_with_decorations(DisplayServer::WindowID p_window_id) const {
  519. MutexLock mutex_lock(wayland_thread.mutex);
  520. // We can't know the position of toplevels with the standard protocol, nor can
  521. // we get information about the decorations, at least with SSDs.
  522. return Point2i();
  523. }
  524. void DisplayServerWayland::window_set_position(const Point2i &p_position, DisplayServer::WindowID p_window_id) {
  525. // Unsupported with toplevels.
  526. }
  527. void DisplayServerWayland::window_set_max_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  528. MutexLock mutex_lock(wayland_thread.mutex);
  529. DEBUG_LOG_WAYLAND(vformat("window max size set to %s", p_size));
  530. if (p_size.x < 0 || p_size.y < 0) {
  531. ERR_FAIL_MSG("Maximum window size can't be negative!");
  532. }
  533. WindowData &wd = main_window;
  534. // FIXME: Is `p_size.x < wd.min_size.x || p_size.y < wd.min_size.y` == `p_size < wd.min_size`?
  535. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  536. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  537. return;
  538. }
  539. wd.max_size = p_size;
  540. wayland_thread.window_set_max_size(MAIN_WINDOW_ID, p_size);
  541. }
  542. Size2i DisplayServerWayland::window_get_max_size(DisplayServer::WindowID p_window_id) const {
  543. MutexLock mutex_lock(wayland_thread.mutex);
  544. return main_window.max_size;
  545. }
  546. void DisplayServerWayland::gl_window_make_current(DisplayServer::WindowID p_window_id) {
  547. #ifdef GLES3_ENABLED
  548. if (egl_manager) {
  549. egl_manager->window_make_current(MAIN_WINDOW_ID);
  550. }
  551. #endif
  552. }
  553. void DisplayServerWayland::window_set_transient(WindowID p_window_id, WindowID p_parent) {
  554. // Currently unsupported.
  555. }
  556. void DisplayServerWayland::window_set_min_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  557. MutexLock mutex_lock(wayland_thread.mutex);
  558. DEBUG_LOG_WAYLAND(vformat("window minsize set to %s", p_size));
  559. WindowData &wd = main_window;
  560. if (p_size.x < 0 || p_size.y < 0) {
  561. ERR_FAIL_MSG("Minimum window size can't be negative!");
  562. }
  563. // FIXME: Is `p_size.x > wd.max_size.x || p_size.y > wd.max_size.y` == `p_size > wd.max_size`?
  564. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  565. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  566. return;
  567. }
  568. wd.min_size = p_size;
  569. wayland_thread.window_set_min_size(MAIN_WINDOW_ID, p_size);
  570. }
  571. Size2i DisplayServerWayland::window_get_min_size(DisplayServer::WindowID p_window_id) const {
  572. MutexLock mutex_lock(wayland_thread.mutex);
  573. return main_window.min_size;
  574. }
  575. void DisplayServerWayland::window_set_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  576. // The XDG spec doesn't allow non-interactive resizes.
  577. }
  578. Size2i DisplayServerWayland::window_get_size(DisplayServer::WindowID p_window_id) const {
  579. MutexLock mutex_lock(wayland_thread.mutex);
  580. return main_window.rect.size;
  581. }
  582. Size2i DisplayServerWayland::window_get_size_with_decorations(DisplayServer::WindowID p_window_id) const {
  583. MutexLock mutex_lock(wayland_thread.mutex);
  584. // I don't think there's a way of actually knowing the size of the window
  585. // decoration in Wayland, at least in the case of SSDs, nor that it would be
  586. // that useful in this case. We'll just return the main window's size.
  587. return main_window.rect.size;
  588. }
  589. void DisplayServerWayland::window_set_mode(WindowMode p_mode, DisplayServer::WindowID p_window_id) {
  590. MutexLock mutex_lock(wayland_thread.mutex);
  591. WindowData &wd = main_window;
  592. if (!wd.visible) {
  593. return;
  594. }
  595. wayland_thread.window_try_set_mode(p_window_id, p_mode);
  596. }
  597. DisplayServer::WindowMode DisplayServerWayland::window_get_mode(DisplayServer::WindowID p_window_id) const {
  598. MutexLock mutex_lock(wayland_thread.mutex);
  599. const WindowData &wd = main_window;
  600. if (!wd.visible) {
  601. return WINDOW_MODE_WINDOWED;
  602. }
  603. return wayland_thread.window_get_mode(p_window_id);
  604. }
  605. bool DisplayServerWayland::window_is_maximize_allowed(DisplayServer::WindowID p_window_id) const {
  606. MutexLock mutex_lock(wayland_thread.mutex);
  607. return wayland_thread.window_can_set_mode(p_window_id, WINDOW_MODE_MAXIMIZED);
  608. }
  609. void DisplayServerWayland::window_set_flag(WindowFlags p_flag, bool p_enabled, DisplayServer::WindowID p_window_id) {
  610. MutexLock mutex_lock(wayland_thread.mutex);
  611. WindowData &wd = main_window;
  612. DEBUG_LOG_WAYLAND(vformat("Window set flag %d", p_flag));
  613. switch (p_flag) {
  614. case WINDOW_FLAG_BORDERLESS: {
  615. wayland_thread.window_set_borderless(MAIN_WINDOW_ID, p_enabled);
  616. } break;
  617. default: {
  618. }
  619. }
  620. if (p_enabled) {
  621. wd.flags |= 1 << p_flag;
  622. } else {
  623. wd.flags &= ~(1 << p_flag);
  624. }
  625. }
  626. bool DisplayServerWayland::window_get_flag(WindowFlags p_flag, DisplayServer::WindowID p_window_id) const {
  627. MutexLock mutex_lock(wayland_thread.mutex);
  628. return main_window.flags & (1 << p_flag);
  629. }
  630. void DisplayServerWayland::window_request_attention(DisplayServer::WindowID p_window_id) {
  631. MutexLock mutex_lock(wayland_thread.mutex);
  632. DEBUG_LOG_WAYLAND("Requested attention.");
  633. wayland_thread.window_request_attention(MAIN_WINDOW_ID);
  634. }
  635. void DisplayServerWayland::window_move_to_foreground(DisplayServer::WindowID p_window_id) {
  636. // Standard Wayland APIs don't support this.
  637. }
  638. bool DisplayServerWayland::window_is_focused(WindowID p_window_id) const {
  639. return wayland_thread.pointer_get_pointed_window_id() == p_window_id;
  640. }
  641. bool DisplayServerWayland::window_can_draw(DisplayServer::WindowID p_window_id) const {
  642. return frame;
  643. }
  644. bool DisplayServerWayland::can_any_window_draw() const {
  645. return frame;
  646. }
  647. void DisplayServerWayland::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
  648. // TODO
  649. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_ime_active active %s", p_active ? "true" : "false"));
  650. }
  651. void DisplayServerWayland::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
  652. // TODO
  653. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_ime_position pos %s window %d", p_pos, p_window_id));
  654. }
  655. // NOTE: While Wayland is supposed to be tear-free, wayland-protocols version
  656. // 1.30 added a protocol for allowing async flips which is supposed to be
  657. // handled by drivers such as Vulkan. We can then just ask to disable v-sync and
  658. // hope for the best. See: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/commit/6394f0b4f3be151076f10a845a2fb131eeb56706
  659. void DisplayServerWayland::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, DisplayServer::WindowID p_window_id) {
  660. MutexLock mutex_lock(wayland_thread.mutex);
  661. #ifdef RD_ENABLED
  662. if (context_rd) {
  663. context_rd->set_vsync_mode(p_window_id, p_vsync_mode);
  664. emulate_vsync = (context_rd->get_vsync_mode(p_window_id) == DisplayServer::VSYNC_ENABLED);
  665. if (emulate_vsync) {
  666. print_verbose("VSYNC: manually throttling frames using MAILBOX.");
  667. context_rd->set_vsync_mode(p_window_id, DisplayServer::VSYNC_MAILBOX);
  668. }
  669. }
  670. #endif // VULKAN_ENABLED
  671. #ifdef GLES3_ENABLED
  672. if (egl_manager) {
  673. egl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
  674. emulate_vsync = egl_manager->is_using_vsync();
  675. if (emulate_vsync) {
  676. print_verbose("VSYNC: manually throttling frames with swap delay 0.");
  677. egl_manager->set_use_vsync(false);
  678. }
  679. }
  680. #endif // GLES3_ENABLED
  681. }
  682. DisplayServer::VSyncMode DisplayServerWayland::window_get_vsync_mode(DisplayServer::WindowID p_window_id) const {
  683. if (emulate_vsync) {
  684. return DisplayServer::VSYNC_ENABLED;
  685. }
  686. #ifdef VULKAN_ENABLED
  687. if (context_rd) {
  688. return context_rd->get_vsync_mode(p_window_id);
  689. }
  690. #endif // VULKAN_ENABLED
  691. #ifdef GLES3_ENABLED
  692. if (egl_manager) {
  693. return egl_manager->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
  694. }
  695. #endif // GLES3_ENABLED
  696. return DisplayServer::VSYNC_ENABLED;
  697. }
  698. void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
  699. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  700. MutexLock mutex_lock(wayland_thread.mutex);
  701. if (p_shape == cursor_shape) {
  702. return;
  703. }
  704. cursor_shape = p_shape;
  705. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  706. // Hidden.
  707. return;
  708. }
  709. if (custom_cursors.has(p_shape)) {
  710. wayland_thread.cursor_set_custom_shape(p_shape);
  711. } else {
  712. wayland_thread.cursor_set_shape(p_shape);
  713. }
  714. }
  715. DisplayServerWayland::CursorShape DisplayServerWayland::cursor_get_shape() const {
  716. MutexLock mutex_lock(wayland_thread.mutex);
  717. return cursor_shape;
  718. }
  719. void DisplayServerWayland::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  720. MutexLock mutex_lock(wayland_thread.mutex);
  721. bool visible = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED);
  722. if (p_cursor.is_valid()) {
  723. HashMap<CursorShape, CustomCursor>::Iterator cursor_c = custom_cursors.find(p_shape);
  724. if (cursor_c) {
  725. if (cursor_c->value.rid == p_cursor->get_rid() && cursor_c->value.hotspot == p_hotspot) {
  726. // We have a cached cursor. Nice.
  727. if (visible) {
  728. wayland_thread.cursor_set_custom_shape(p_shape);
  729. }
  730. return;
  731. }
  732. // We're changing this cursor; we'll have to rebuild it.
  733. custom_cursors.erase(p_shape);
  734. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  735. }
  736. Ref<Texture2D> texture = p_cursor;
  737. ERR_FAIL_COND(!texture.is_valid());
  738. Size2i texture_size;
  739. Ref<AtlasTexture> atlas_texture = texture;
  740. if (atlas_texture.is_valid()) {
  741. texture_size.width = atlas_texture->get_region().size.x;
  742. texture_size.height = atlas_texture->get_region().size.y;
  743. } else {
  744. texture_size.width = texture->get_width();
  745. texture_size.height = texture->get_height();
  746. }
  747. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  748. // NOTE: The Wayland protocol says nothing about cursor size limits, yet if
  749. // the texture is larger than 256x256 it won't show at least on sway.
  750. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  751. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  752. ERR_FAIL_COND(texture_size.height == 0 || texture_size.width == 0);
  753. Ref<Image> image = texture->get_image();
  754. ERR_FAIL_COND(!image.is_valid());
  755. if (image->is_compressed()) {
  756. image = image->duplicate(true);
  757. Error err = image->decompress();
  758. ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
  759. }
  760. CustomCursor &cursor = custom_cursors[p_shape];
  761. cursor.rid = p_cursor->get_rid();
  762. cursor.hotspot = p_hotspot;
  763. wayland_thread.cursor_shape_set_custom_image(p_shape, image, p_hotspot);
  764. if (visible) {
  765. wayland_thread.cursor_set_custom_shape(p_shape);
  766. }
  767. } else {
  768. // Clear cache and reset to default system cursor.
  769. if (cursor_shape == p_shape && visible) {
  770. wayland_thread.cursor_set_shape(p_shape);
  771. }
  772. if (custom_cursors.has(p_shape)) {
  773. custom_cursors.erase(p_shape);
  774. }
  775. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  776. }
  777. }
  778. int DisplayServerWayland::keyboard_get_layout_count() const {
  779. MutexLock mutex_lock(wayland_thread.mutex);
  780. return wayland_thread.keyboard_get_layout_count();
  781. }
  782. int DisplayServerWayland::keyboard_get_current_layout() const {
  783. MutexLock mutex_lock(wayland_thread.mutex);
  784. return wayland_thread.keyboard_get_current_layout_index();
  785. }
  786. void DisplayServerWayland::keyboard_set_current_layout(int p_index) {
  787. MutexLock mutex_lock(wayland_thread.mutex);
  788. wayland_thread.keyboard_set_current_layout_index(p_index);
  789. }
  790. String DisplayServerWayland::keyboard_get_layout_language(int p_index) const {
  791. MutexLock mutex_lock(wayland_thread.mutex);
  792. // xkbcommon exposes only the layout's name, which looks like it overlaps with
  793. // its language.
  794. return wayland_thread.keyboard_get_layout_name(p_index);
  795. }
  796. String DisplayServerWayland::keyboard_get_layout_name(int p_index) const {
  797. MutexLock mutex_lock(wayland_thread.mutex);
  798. return wayland_thread.keyboard_get_layout_name(p_index);
  799. }
  800. Key DisplayServerWayland::keyboard_get_keycode_from_physical(Key p_keycode) const {
  801. MutexLock mutex_lock(wayland_thread.mutex);
  802. Key key = wayland_thread.keyboard_get_key_from_physical(p_keycode);
  803. // If not found, fallback to QWERTY.
  804. // This should match the behavior of the event pump.
  805. if (key == Key::NONE) {
  806. return p_keycode;
  807. }
  808. if (key >= Key::A + 32 && key <= Key::Z + 32) {
  809. key -= 'a' - 'A';
  810. }
  811. // Make it consistent with the keys returned by `Input`.
  812. if (key == Key::BACKTAB) {
  813. key = Key::TAB;
  814. }
  815. return key;
  816. }
  817. void DisplayServerWayland::process_events() {
  818. wayland_thread.mutex.lock();
  819. while (wayland_thread.has_message()) {
  820. Ref<WaylandThread::Message> msg = wayland_thread.pop_message();
  821. Ref<WaylandThread::WindowRectMessage> winrect_msg = msg;
  822. if (winrect_msg.is_valid()) {
  823. _resize_window(winrect_msg->rect.size);
  824. }
  825. Ref<WaylandThread::WindowEventMessage> winev_msg = msg;
  826. if (winev_msg.is_valid()) {
  827. _send_window_event(winev_msg->event);
  828. if (winev_msg->event == WINDOW_EVENT_FOCUS_IN) {
  829. if (OS::get_singleton()->get_main_loop()) {
  830. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  831. }
  832. } else if (winev_msg->event == WINDOW_EVENT_FOCUS_OUT) {
  833. if (OS::get_singleton()->get_main_loop()) {
  834. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  835. }
  836. }
  837. }
  838. Ref<WaylandThread::InputEventMessage> inputev_msg = msg;
  839. if (inputev_msg.is_valid()) {
  840. Input::get_singleton()->parse_input_event(inputev_msg->event);
  841. }
  842. Ref<WaylandThread::DropFilesEventMessage> dropfiles_msg = msg;
  843. if (dropfiles_msg.is_valid()) {
  844. WindowData wd = main_window;
  845. if (wd.drop_files_callback.is_valid()) {
  846. wd.drop_files_callback.call(dropfiles_msg->files);
  847. }
  848. }
  849. }
  850. wayland_thread.keyboard_echo_keys();
  851. frame = wayland_thread.get_reset_frame();
  852. wayland_thread.mutex.unlock();
  853. Input::get_singleton()->flush_buffered_events();
  854. }
  855. void DisplayServerWayland::release_rendering_thread() {
  856. #ifdef GLES3_ENABLED
  857. if (egl_manager) {
  858. egl_manager->release_current();
  859. }
  860. #endif
  861. }
  862. void DisplayServerWayland::make_rendering_thread() {
  863. #ifdef GLES3_ENABLED
  864. if (egl_manager) {
  865. egl_manager->make_current();
  866. }
  867. #endif
  868. }
  869. void DisplayServerWayland::swap_buffers() {
  870. #ifdef GLES3_ENABLED
  871. if (egl_manager) {
  872. egl_manager->swap_buffers();
  873. }
  874. #endif
  875. }
  876. void DisplayServerWayland::set_context(Context p_context) {
  877. MutexLock mutex_lock(wayland_thread.mutex);
  878. DEBUG_LOG_WAYLAND(vformat("Setting context %d.", p_context));
  879. context = p_context;
  880. String app_id = _get_app_id_from_context(p_context);
  881. wayland_thread.window_set_app_id(MAIN_WINDOW_ID, app_id);
  882. }
  883. Vector<String> DisplayServerWayland::get_rendering_drivers_func() {
  884. Vector<String> drivers;
  885. #ifdef VULKAN_ENABLED
  886. drivers.push_back("vulkan");
  887. #endif
  888. #ifdef GLES3_ENABLED
  889. drivers.push_back("opengl3");
  890. #endif
  891. return drivers;
  892. }
  893. DisplayServer *DisplayServerWayland::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Error &r_error) {
  894. DisplayServer *ds = memnew(DisplayServerWayland(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  895. if (r_error != OK) {
  896. ERR_PRINT("Can't create the Wayland display server.");
  897. memdelete(ds);
  898. return nullptr;
  899. }
  900. return ds;
  901. }
  902. DisplayServerWayland::DisplayServerWayland(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  903. #ifdef GLES3_ENABLED
  904. #ifdef SOWRAP_ENABLED
  905. #ifdef DEBUG_ENABLED
  906. int dylibloader_verbose = 1;
  907. #else
  908. int dylibloader_verbose = 0;
  909. #endif // DEBUG_ENABLED
  910. #endif // SOWRAP_ENABLED
  911. #endif // GLES3_ENABLED
  912. r_error = ERR_UNAVAILABLE;
  913. Error thread_err = wayland_thread.init();
  914. if (thread_err != OK) {
  915. r_error = thread_err;
  916. ERR_FAIL_MSG("Could not initialize the Wayland thread.");
  917. }
  918. // Input.
  919. Input::get_singleton()->set_event_dispatch_function(dispatch_input_events);
  920. #ifdef SPEECHD_ENABLED
  921. // Init TTS
  922. tts = memnew(TTS_Linux);
  923. #endif
  924. rendering_driver = p_rendering_driver;
  925. #ifdef RD_ENABLED
  926. #ifdef VULKAN_ENABLED
  927. if (p_rendering_driver == "vulkan") {
  928. context_rd = memnew(VulkanContextWayland);
  929. }
  930. #endif
  931. if (context_rd) {
  932. if (context_rd->initialize() != OK) {
  933. memdelete(context_rd);
  934. context_rd = nullptr;
  935. r_error = ERR_CANT_CREATE;
  936. ERR_FAIL_MSG(vformat("Could not initialize %s", context_rd->get_api_name()));
  937. }
  938. }
  939. #endif
  940. #ifdef GLES3_ENABLED
  941. if (p_rendering_driver == "opengl3") {
  942. if (getenv("DRI_PRIME") == nullptr) {
  943. int prime_idx = -1;
  944. if (getenv("PRIMUS_DISPLAY") ||
  945. getenv("PRIMUS_libGLd") ||
  946. getenv("PRIMUS_libGLa") ||
  947. getenv("PRIMUS_libGL") ||
  948. getenv("PRIMUS_LOAD_GLOBAL") ||
  949. getenv("BUMBLEBEE_SOCKET") ||
  950. getenv("__NV_PRIME_RENDER_OFFLOAD")) {
  951. print_verbose("Optirun/primusrun detected. Skipping GPU detection");
  952. prime_idx = 0;
  953. }
  954. // Some tools use fake libGL libraries and have them override the real one using
  955. // LD_LIBRARY_PATH, so we skip them. *But* Steam also sets LD_LIBRARY_PATH for its
  956. // runtime and includes system `/lib` and `/lib64`... so ignore Steam.
  957. if (prime_idx == -1 && getenv("LD_LIBRARY_PATH") && !getenv("STEAM_RUNTIME_LIBRARY_PATH")) {
  958. String ld_library_path(getenv("LD_LIBRARY_PATH"));
  959. Vector<String> libraries = ld_library_path.split(":");
  960. for (int i = 0; i < libraries.size(); ++i) {
  961. if (FileAccess::exists(libraries[i] + "/libGL.so.1") ||
  962. FileAccess::exists(libraries[i] + "/libGL.so")) {
  963. print_verbose("Custom libGL override detected. Skipping GPU detection");
  964. prime_idx = 0;
  965. }
  966. }
  967. }
  968. if (prime_idx == -1) {
  969. print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic.");
  970. prime_idx = DetectPrimeEGL::detect_prime();
  971. }
  972. if (prime_idx) {
  973. print_line(vformat("Found discrete GPU, setting DRI_PRIME=%d to use it.", prime_idx));
  974. print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU.");
  975. setenv("DRI_PRIME", itos(prime_idx).utf8().ptr(), 1);
  976. }
  977. }
  978. egl_manager = memnew(EGLManagerWayland);
  979. #ifdef SOWRAP_ENABLED
  980. if (initialize_wayland_egl(dylibloader_verbose) != 0) {
  981. WARN_PRINT("Can't load the Wayland EGL library.");
  982. return;
  983. }
  984. #endif // SOWRAP_ENABLED
  985. if (egl_manager->initialize() != OK) {
  986. memdelete(egl_manager);
  987. egl_manager = nullptr;
  988. r_error = ERR_CANT_CREATE;
  989. ERR_FAIL_MSG("Could not initialize GLES3.");
  990. }
  991. RasterizerGLES3::make_current(true);
  992. }
  993. #endif // GLES3_ENABLED
  994. cursor_set_shape(CURSOR_BUSY);
  995. WindowData &wd = main_window;
  996. wd.id = MAIN_WINDOW_ID;
  997. wd.mode = p_mode;
  998. wd.flags = p_flags;
  999. wd.vsync_mode = p_vsync_mode;
  1000. wd.rect.size = p_resolution;
  1001. wd.title = "Godot";
  1002. _show_window();
  1003. #ifdef RD_ENABLED
  1004. if (context_rd) {
  1005. rendering_device = memnew(RenderingDevice);
  1006. rendering_device->initialize(context_rd);
  1007. RendererCompositorRD::make_current();
  1008. }
  1009. #endif
  1010. #ifdef DBUS_ENABLED
  1011. portal_desktop = memnew(FreeDesktopPortalDesktop);
  1012. screensaver = memnew(FreeDesktopScreenSaver);
  1013. #endif
  1014. screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  1015. r_error = OK;
  1016. }
  1017. DisplayServerWayland::~DisplayServerWayland() {
  1018. // TODO: Multiwindow support.
  1019. if (main_window.visible) {
  1020. #ifdef VULKAN_ENABLED
  1021. if (context_rd) {
  1022. context_rd->window_destroy(MAIN_WINDOW_ID);
  1023. }
  1024. #endif
  1025. #ifdef GLES3_ENABLED
  1026. if (egl_manager) {
  1027. egl_manager->window_destroy(MAIN_WINDOW_ID);
  1028. }
  1029. #endif
  1030. }
  1031. #ifdef GLES3_ENABLED
  1032. if (main_window.wl_egl_window) {
  1033. wl_egl_window_destroy(main_window.wl_egl_window);
  1034. }
  1035. #endif
  1036. wayland_thread.destroy();
  1037. // Destroy all drivers.
  1038. #ifdef RD_ENABLED
  1039. if (rendering_device) {
  1040. rendering_device->finalize();
  1041. memdelete(rendering_device);
  1042. }
  1043. if (context_rd) {
  1044. memdelete(context_rd);
  1045. }
  1046. #endif
  1047. #ifdef SPEECHD_ENABLED
  1048. if (tts) {
  1049. memdelete(tts);
  1050. }
  1051. #endif
  1052. #ifdef DBUS_ENABLED
  1053. if (portal_desktop) {
  1054. memdelete(portal_desktop);
  1055. memdelete(screensaver);
  1056. }
  1057. #endif
  1058. }
  1059. void DisplayServerWayland::register_wayland_driver() {
  1060. register_create_function("wayland", create_func, get_rendering_drivers_func);
  1061. }
  1062. #endif //WAYLAND_ENABLED