display_server_wayland.cpp 50 KB

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