display_server_wayland.cpp 42 KB

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