display_server_wayland.cpp 51 KB

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