display_server_wayland.cpp 46 KB

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