display_server_wayland.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /**************************************************************************/
  2. /* display_server_wayland.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "display_server_wayland.h"
  31. #ifdef WAYLAND_ENABLED
  32. #define WAYLAND_DISPLAY_SERVER_DEBUG_LOGS_ENABLED
  33. #ifdef WAYLAND_DISPLAY_SERVER_DEBUG_LOGS_ENABLED
  34. #define DEBUG_LOG_WAYLAND(...) print_verbose(__VA_ARGS__)
  35. #else
  36. #define DEBUG_LOG_WAYLAND(...)
  37. #endif
  38. #ifdef VULKAN_ENABLED
  39. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  40. #endif
  41. #ifdef GLES3_ENABLED
  42. #include "detect_prime_egl.h"
  43. #include "drivers/gles3/rasterizer_gles3.h"
  44. #endif
  45. String DisplayServerWayland::_get_app_id_from_context(Context p_context) {
  46. String app_id;
  47. switch (p_context) {
  48. case CONTEXT_EDITOR: {
  49. app_id = "org.godotengine.Editor";
  50. } break;
  51. case CONTEXT_PROJECTMAN: {
  52. app_id = "org.godotengine.ProjectManager";
  53. } break;
  54. case CONTEXT_ENGINE:
  55. default: {
  56. String config_name = GLOBAL_GET("application/config/name");
  57. if (config_name.length() != 0) {
  58. app_id = config_name;
  59. } else {
  60. app_id = "org.godotengine.Godot";
  61. }
  62. }
  63. }
  64. return app_id;
  65. }
  66. void DisplayServerWayland::_send_window_event(WindowEvent p_event) {
  67. WindowData &wd = main_window;
  68. if (wd.window_event_callback.is_valid()) {
  69. Variant event = int(p_event);
  70. wd.window_event_callback.call(event);
  71. }
  72. }
  73. void DisplayServerWayland::dispatch_input_events(const Ref<InputEvent> &p_event) {
  74. ((DisplayServerWayland *)(get_singleton()))->_dispatch_input_event(p_event);
  75. }
  76. void DisplayServerWayland::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  77. Callable callable = main_window.input_event_callback;
  78. if (callable.is_valid()) {
  79. callable.call(p_event);
  80. }
  81. }
  82. void DisplayServerWayland::_resize_window(const Size2i &p_size) {
  83. WindowData &wd = main_window;
  84. wd.rect.size = p_size;
  85. #ifdef RD_ENABLED
  86. if (wd.visible && rendering_context) {
  87. rendering_context->window_set_size(MAIN_WINDOW_ID, wd.rect.size.width, wd.rect.size.height);
  88. }
  89. #endif
  90. #ifdef GLES3_ENABLED
  91. if (wd.visible && egl_manager) {
  92. wl_egl_window_resize(wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height, 0, 0);
  93. }
  94. #endif
  95. if (wd.rect_changed_callback.is_valid()) {
  96. wd.rect_changed_callback.call(wd.rect);
  97. }
  98. }
  99. void DisplayServerWayland::_show_window() {
  100. MutexLock mutex_lock(wayland_thread.mutex);
  101. WindowData &wd = main_window;
  102. if (!wd.visible) {
  103. DEBUG_LOG_WAYLAND("Showing window.");
  104. // Showing this window will reset its mode with whatever the compositor
  105. // reports. We'll save the mode beforehand so that we can reapply it later.
  106. // TODO: Fix/Port/Move/Whatever to `WaylandThread` APIs.
  107. WindowMode setup_mode = wd.mode;
  108. wayland_thread.window_create(MAIN_WINDOW_ID, wd.rect.size.width, wd.rect.size.height);
  109. wayland_thread.window_set_min_size(MAIN_WINDOW_ID, wd.min_size);
  110. wayland_thread.window_set_max_size(MAIN_WINDOW_ID, wd.max_size);
  111. wayland_thread.window_set_app_id(MAIN_WINDOW_ID, _get_app_id_from_context(context));
  112. wayland_thread.window_set_borderless(MAIN_WINDOW_ID, window_get_flag(WINDOW_FLAG_BORDERLESS));
  113. // NOTE: The XDG shell protocol is built in a way that causes the window to
  114. // be immediately shown as soon as a valid buffer is assigned to it. Hence,
  115. // the only acceptable way of implementing window showing is to move the
  116. // graphics context window creation logic here.
  117. #ifdef RD_ENABLED
  118. if (rendering_context) {
  119. union {
  120. #ifdef VULKAN_ENABLED
  121. RenderingContextDriverVulkanWayland::WindowPlatformData vulkan;
  122. #endif
  123. } wpd;
  124. #ifdef VULKAN_ENABLED
  125. if (rendering_driver == "vulkan") {
  126. wpd.vulkan.surface = wayland_thread.window_get_wl_surface(wd.id);
  127. wpd.vulkan.display = wayland_thread.get_wl_display();
  128. }
  129. #endif
  130. Error err = rendering_context->window_create(wd.id, &wpd);
  131. ERR_FAIL_COND_MSG(err != OK, vformat("Can't create a %s window", rendering_driver));
  132. rendering_context->window_set_size(wd.id, wd.rect.size.width, wd.rect.size.height);
  133. rendering_context->window_set_vsync_mode(wd.id, wd.vsync_mode);
  134. emulate_vsync = (rendering_context->window_get_vsync_mode(wd.id) == DisplayServer::VSYNC_ENABLED);
  135. if (emulate_vsync) {
  136. print_verbose("VSYNC: manually throttling frames using MAILBOX.");
  137. rendering_context->window_set_vsync_mode(wd.id, DisplayServer::VSYNC_MAILBOX);
  138. }
  139. }
  140. #endif
  141. #ifdef GLES3_ENABLED
  142. if (egl_manager) {
  143. struct wl_surface *wl_surface = wayland_thread.window_get_wl_surface(wd.id);
  144. wd.wl_egl_window = wl_egl_window_create(wl_surface, wd.rect.size.width, wd.rect.size.height);
  145. Error err = egl_manager->window_create(MAIN_WINDOW_ID, wayland_thread.get_wl_display(), wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height);
  146. ERR_FAIL_COND_MSG(err == ERR_CANT_CREATE, "Can't show a GLES3 window.");
  147. window_set_vsync_mode(wd.vsync_mode, MAIN_WINDOW_ID);
  148. }
  149. #endif
  150. // NOTE: The public window-handling methods might depend on this flag being
  151. // set. Ensure to not make any of these calls before this assignment.
  152. wd.visible = true;
  153. // Actually try to apply the window's mode now that it's visible.
  154. window_set_mode(setup_mode);
  155. wayland_thread.window_set_title(MAIN_WINDOW_ID, wd.title);
  156. }
  157. }
  158. // Interface methods.
  159. bool DisplayServerWayland::has_feature(Feature p_feature) const {
  160. switch (p_feature) {
  161. #ifndef DISABLE_DEPRECATED
  162. case FEATURE_GLOBAL_MENU: {
  163. return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU));
  164. } break;
  165. #endif
  166. case FEATURE_MOUSE:
  167. case FEATURE_MOUSE_WARP:
  168. case FEATURE_CLIPBOARD:
  169. case FEATURE_CURSOR_SHAPE:
  170. case FEATURE_CUSTOM_CURSOR_SHAPE:
  171. case FEATURE_WINDOW_TRANSPARENCY:
  172. case FEATURE_HIDPI:
  173. case FEATURE_SWAP_BUFFERS:
  174. case FEATURE_KEEP_SCREEN_ON:
  175. case FEATURE_CLIPBOARD_PRIMARY: {
  176. return true;
  177. } break;
  178. //case FEATURE_NATIVE_DIALOG:
  179. //case FEATURE_NATIVE_DIALOG_INPUT:
  180. #ifdef DBUS_ENABLED
  181. case FEATURE_NATIVE_DIALOG_FILE: {
  182. return true;
  183. } break;
  184. #endif
  185. #ifdef SPEECHD_ENABLED
  186. case FEATURE_TEXT_TO_SPEECH: {
  187. return true;
  188. } break;
  189. #endif
  190. default: {
  191. return false;
  192. }
  193. }
  194. }
  195. String DisplayServerWayland::get_name() const {
  196. return "Wayland";
  197. }
  198. #ifdef SPEECHD_ENABLED
  199. bool DisplayServerWayland::tts_is_speaking() const {
  200. ERR_FAIL_NULL_V(tts, false);
  201. return tts->is_speaking();
  202. }
  203. bool DisplayServerWayland::tts_is_paused() const {
  204. ERR_FAIL_NULL_V(tts, false);
  205. return tts->is_paused();
  206. }
  207. TypedArray<Dictionary> DisplayServerWayland::tts_get_voices() const {
  208. ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
  209. return tts->get_voices();
  210. }
  211. 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) {
  212. ERR_FAIL_NULL(tts);
  213. tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt);
  214. }
  215. void DisplayServerWayland::tts_pause() {
  216. ERR_FAIL_NULL(tts);
  217. tts->pause();
  218. }
  219. void DisplayServerWayland::tts_resume() {
  220. ERR_FAIL_NULL(tts);
  221. tts->resume();
  222. }
  223. void DisplayServerWayland::tts_stop() {
  224. ERR_FAIL_NULL(tts);
  225. tts->stop();
  226. }
  227. #endif
  228. #ifdef DBUS_ENABLED
  229. bool DisplayServerWayland::is_dark_mode_supported() const {
  230. return portal_desktop->is_supported();
  231. }
  232. bool DisplayServerWayland::is_dark_mode() const {
  233. switch (portal_desktop->get_appearance_color_scheme()) {
  234. case 1:
  235. // Prefers dark theme.
  236. return true;
  237. case 2:
  238. // Prefers light theme.
  239. return false;
  240. default:
  241. // Preference unknown.
  242. return false;
  243. }
  244. }
  245. void DisplayServerWayland::set_system_theme_change_callback(const Callable &p_callable) {
  246. portal_desktop->set_system_theme_change_callback(p_callable);
  247. }
  248. 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) {
  249. WindowID window_id = MAIN_WINDOW_ID;
  250. // TODO: Use window IDs for multiwindow support.
  251. WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
  252. 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);
  253. }
  254. 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) {
  255. WindowID window_id = MAIN_WINDOW_ID;
  256. // TODO: Use window IDs for multiwindow support.
  257. WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
  258. 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);
  259. }
  260. #endif
  261. void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
  262. if (p_mode == mouse_mode) {
  263. return;
  264. }
  265. MutexLock mutex_lock(wayland_thread.mutex);
  266. bool show_cursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
  267. if (show_cursor) {
  268. if (custom_cursors.has(cursor_shape)) {
  269. wayland_thread.cursor_set_custom_shape(cursor_shape);
  270. } else {
  271. wayland_thread.cursor_set_shape(cursor_shape);
  272. }
  273. } else {
  274. wayland_thread.cursor_hide();
  275. }
  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. 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. p_screen = window_get_current_screen();
  437. }
  438. return wayland_thread.screen_get_data(p_screen).scale;
  439. }
  440. float DisplayServerWayland::screen_get_refresh_rate(int p_screen) const {
  441. MutexLock mutex_lock(wayland_thread.mutex);
  442. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  443. p_screen = window_get_current_screen();
  444. }
  445. return wayland_thread.screen_get_data(p_screen).refresh_rate;
  446. }
  447. void DisplayServerWayland::screen_set_keep_on(bool p_enable) {
  448. MutexLock mutex_lock(wayland_thread.mutex);
  449. if (screen_is_kept_on() == p_enable) {
  450. return;
  451. }
  452. #ifdef DBUS_ENABLED
  453. if (screensaver) {
  454. if (p_enable) {
  455. screensaver->inhibit();
  456. } else {
  457. screensaver->uninhibit();
  458. }
  459. screensaver_inhibited = p_enable;
  460. }
  461. #endif
  462. }
  463. bool DisplayServerWayland::screen_is_kept_on() const {
  464. #ifdef DBUS_ENABLED
  465. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID) || screensaver_inhibited;
  466. #else
  467. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID);
  468. #endif
  469. }
  470. Vector<DisplayServer::WindowID> DisplayServerWayland::get_window_list() const {
  471. MutexLock mutex_lock(wayland_thread.mutex);
  472. Vector<int> ret;
  473. ret.push_back(MAIN_WINDOW_ID);
  474. return ret;
  475. }
  476. int64_t DisplayServerWayland::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  477. MutexLock mutex_lock(wayland_thread.mutex);
  478. switch (p_handle_type) {
  479. case DISPLAY_HANDLE: {
  480. return (int64_t)wayland_thread.get_wl_display();
  481. } break;
  482. case WINDOW_HANDLE: {
  483. return (int64_t)wayland_thread.window_get_wl_surface(p_window);
  484. } break;
  485. case WINDOW_VIEW: {
  486. return 0; // Not supported.
  487. } break;
  488. #ifdef GLES3_ENABLED
  489. case OPENGL_CONTEXT: {
  490. if (egl_manager) {
  491. return (int64_t)egl_manager->get_context(p_window);
  492. }
  493. return 0;
  494. } break;
  495. #endif // GLES3_ENABLED
  496. default: {
  497. return 0;
  498. } break;
  499. }
  500. }
  501. DisplayServer::WindowID DisplayServerWayland::get_window_at_screen_position(const Point2i &p_position) const {
  502. // Standard Wayland APIs don't support this.
  503. return MAIN_WINDOW_ID;
  504. }
  505. void DisplayServerWayland::window_attach_instance_id(ObjectID p_instance, WindowID p_window_id) {
  506. MutexLock mutex_lock(wayland_thread.mutex);
  507. main_window.instance_id = p_instance;
  508. }
  509. ObjectID DisplayServerWayland::window_get_attached_instance_id(WindowID p_window_id) const {
  510. MutexLock mutex_lock(wayland_thread.mutex);
  511. return main_window.instance_id;
  512. }
  513. void DisplayServerWayland::window_set_title(const String &p_title, DisplayServer::WindowID p_window_id) {
  514. MutexLock mutex_lock(wayland_thread.mutex);
  515. WindowData &wd = main_window;
  516. wd.title = p_title;
  517. wayland_thread.window_set_title(MAIN_WINDOW_ID, wd.title);
  518. }
  519. void DisplayServerWayland::window_set_mouse_passthrough(const Vector<Vector2> &p_region, DisplayServer::WindowID p_window_id) {
  520. // TODO
  521. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_mouse_passthrough region %s", p_region));
  522. }
  523. void DisplayServerWayland::window_set_rect_changed_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  524. MutexLock mutex_lock(wayland_thread.mutex);
  525. main_window.rect_changed_callback = p_callable;
  526. }
  527. void DisplayServerWayland::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  528. MutexLock mutex_lock(wayland_thread.mutex);
  529. main_window.window_event_callback = p_callable;
  530. }
  531. void DisplayServerWayland::window_set_input_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  532. MutexLock mutex_lock(wayland_thread.mutex);
  533. main_window.input_event_callback = p_callable;
  534. }
  535. void DisplayServerWayland::window_set_input_text_callback(const Callable &p_callable, WindowID p_window_id) {
  536. MutexLock mutex_lock(wayland_thread.mutex);
  537. main_window.input_text_callback = p_callable;
  538. }
  539. void DisplayServerWayland::window_set_drop_files_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  540. MutexLock mutex_lock(wayland_thread.mutex);
  541. main_window.drop_files_callback = p_callable;
  542. }
  543. int DisplayServerWayland::window_get_current_screen(DisplayServer::WindowID p_window_id) const {
  544. // Standard Wayland APIs don't support getting the screen of a window.
  545. return 0;
  546. }
  547. void DisplayServerWayland::window_set_current_screen(int p_screen, DisplayServer::WindowID p_window_id) {
  548. // Standard Wayland APIs don't support setting the screen of a window.
  549. }
  550. Point2i DisplayServerWayland::window_get_position(DisplayServer::WindowID p_window_id) const {
  551. MutexLock mutex_lock(wayland_thread.mutex);
  552. // We can't know the position of toplevels with the standard protocol.
  553. return Point2i();
  554. }
  555. Point2i DisplayServerWayland::window_get_position_with_decorations(DisplayServer::WindowID p_window_id) const {
  556. MutexLock mutex_lock(wayland_thread.mutex);
  557. // We can't know the position of toplevels with the standard protocol, nor can
  558. // we get information about the decorations, at least with SSDs.
  559. return Point2i();
  560. }
  561. void DisplayServerWayland::window_set_position(const Point2i &p_position, DisplayServer::WindowID p_window_id) {
  562. // Unsupported with toplevels.
  563. }
  564. void DisplayServerWayland::window_set_max_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  565. MutexLock mutex_lock(wayland_thread.mutex);
  566. DEBUG_LOG_WAYLAND(vformat("window max size set to %s", p_size));
  567. if (p_size.x < 0 || p_size.y < 0) {
  568. ERR_FAIL_MSG("Maximum window size can't be negative!");
  569. }
  570. WindowData &wd = main_window;
  571. // FIXME: Is `p_size.x < wd.min_size.x || p_size.y < wd.min_size.y` == `p_size < wd.min_size`?
  572. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  573. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  574. return;
  575. }
  576. wd.max_size = p_size;
  577. wayland_thread.window_set_max_size(MAIN_WINDOW_ID, p_size);
  578. }
  579. Size2i DisplayServerWayland::window_get_max_size(DisplayServer::WindowID p_window_id) const {
  580. MutexLock mutex_lock(wayland_thread.mutex);
  581. return main_window.max_size;
  582. }
  583. void DisplayServerWayland::gl_window_make_current(DisplayServer::WindowID p_window_id) {
  584. #ifdef GLES3_ENABLED
  585. if (egl_manager) {
  586. egl_manager->window_make_current(MAIN_WINDOW_ID);
  587. }
  588. #endif
  589. }
  590. void DisplayServerWayland::window_set_transient(WindowID p_window_id, WindowID p_parent) {
  591. // Currently unsupported.
  592. }
  593. void DisplayServerWayland::window_set_min_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  594. MutexLock mutex_lock(wayland_thread.mutex);
  595. DEBUG_LOG_WAYLAND(vformat("window minsize set to %s", p_size));
  596. WindowData &wd = main_window;
  597. if (p_size.x < 0 || p_size.y < 0) {
  598. ERR_FAIL_MSG("Minimum window size can't be negative!");
  599. }
  600. // FIXME: Is `p_size.x > wd.max_size.x || p_size.y > wd.max_size.y` == `p_size > wd.max_size`?
  601. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  602. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  603. return;
  604. }
  605. wd.min_size = p_size;
  606. wayland_thread.window_set_min_size(MAIN_WINDOW_ID, p_size);
  607. }
  608. Size2i DisplayServerWayland::window_get_min_size(DisplayServer::WindowID p_window_id) const {
  609. MutexLock mutex_lock(wayland_thread.mutex);
  610. return main_window.min_size;
  611. }
  612. void DisplayServerWayland::window_set_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  613. // The XDG spec doesn't allow non-interactive resizes.
  614. }
  615. Size2i DisplayServerWayland::window_get_size(DisplayServer::WindowID p_window_id) const {
  616. MutexLock mutex_lock(wayland_thread.mutex);
  617. return main_window.rect.size;
  618. }
  619. Size2i DisplayServerWayland::window_get_size_with_decorations(DisplayServer::WindowID p_window_id) const {
  620. MutexLock mutex_lock(wayland_thread.mutex);
  621. // I don't think there's a way of actually knowing the size of the window
  622. // decoration in Wayland, at least in the case of SSDs, nor that it would be
  623. // that useful in this case. We'll just return the main window's size.
  624. return main_window.rect.size;
  625. }
  626. void DisplayServerWayland::window_set_mode(WindowMode p_mode, DisplayServer::WindowID p_window_id) {
  627. MutexLock mutex_lock(wayland_thread.mutex);
  628. WindowData &wd = main_window;
  629. if (!wd.visible) {
  630. return;
  631. }
  632. wayland_thread.window_try_set_mode(p_window_id, p_mode);
  633. }
  634. DisplayServer::WindowMode DisplayServerWayland::window_get_mode(DisplayServer::WindowID p_window_id) const {
  635. MutexLock mutex_lock(wayland_thread.mutex);
  636. const WindowData &wd = main_window;
  637. if (!wd.visible) {
  638. return WINDOW_MODE_WINDOWED;
  639. }
  640. return wayland_thread.window_get_mode(p_window_id);
  641. }
  642. bool DisplayServerWayland::window_is_maximize_allowed(DisplayServer::WindowID p_window_id) const {
  643. MutexLock mutex_lock(wayland_thread.mutex);
  644. return wayland_thread.window_can_set_mode(p_window_id, WINDOW_MODE_MAXIMIZED);
  645. }
  646. void DisplayServerWayland::window_set_flag(WindowFlags p_flag, bool p_enabled, DisplayServer::WindowID p_window_id) {
  647. MutexLock mutex_lock(wayland_thread.mutex);
  648. WindowData &wd = main_window;
  649. DEBUG_LOG_WAYLAND(vformat("Window set flag %d", p_flag));
  650. switch (p_flag) {
  651. case WINDOW_FLAG_BORDERLESS: {
  652. wayland_thread.window_set_borderless(MAIN_WINDOW_ID, p_enabled);
  653. } break;
  654. default: {
  655. }
  656. }
  657. if (p_enabled) {
  658. wd.flags |= 1 << p_flag;
  659. } else {
  660. wd.flags &= ~(1 << p_flag);
  661. }
  662. }
  663. bool DisplayServerWayland::window_get_flag(WindowFlags p_flag, DisplayServer::WindowID p_window_id) const {
  664. MutexLock mutex_lock(wayland_thread.mutex);
  665. return main_window.flags & (1 << p_flag);
  666. }
  667. void DisplayServerWayland::window_request_attention(DisplayServer::WindowID p_window_id) {
  668. MutexLock mutex_lock(wayland_thread.mutex);
  669. DEBUG_LOG_WAYLAND("Requested attention.");
  670. wayland_thread.window_request_attention(MAIN_WINDOW_ID);
  671. }
  672. void DisplayServerWayland::window_move_to_foreground(DisplayServer::WindowID p_window_id) {
  673. // Standard Wayland APIs don't support this.
  674. }
  675. bool DisplayServerWayland::window_is_focused(WindowID p_window_id) const {
  676. return wayland_thread.pointer_get_pointed_window_id() == p_window_id;
  677. }
  678. bool DisplayServerWayland::window_can_draw(DisplayServer::WindowID p_window_id) const {
  679. return !suspended;
  680. }
  681. bool DisplayServerWayland::can_any_window_draw() const {
  682. return !suspended;
  683. }
  684. void DisplayServerWayland::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
  685. // TODO
  686. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_ime_active active %s", p_active ? "true" : "false"));
  687. }
  688. void DisplayServerWayland::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
  689. // TODO
  690. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_ime_position pos %s window %d", p_pos, p_window_id));
  691. }
  692. // NOTE: While Wayland is supposed to be tear-free, wayland-protocols version
  693. // 1.30 added a protocol for allowing async flips which is supposed to be
  694. // handled by drivers such as Vulkan. We can then just ask to disable v-sync and
  695. // hope for the best. See: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/commit/6394f0b4f3be151076f10a845a2fb131eeb56706
  696. void DisplayServerWayland::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, DisplayServer::WindowID p_window_id) {
  697. MutexLock mutex_lock(wayland_thread.mutex);
  698. #ifdef RD_ENABLED
  699. if (rendering_context) {
  700. rendering_context->window_set_vsync_mode(p_window_id, p_vsync_mode);
  701. emulate_vsync = (rendering_context->window_get_vsync_mode(p_window_id) == DisplayServer::VSYNC_ENABLED);
  702. if (emulate_vsync) {
  703. print_verbose("VSYNC: manually throttling frames using MAILBOX.");
  704. rendering_context->window_set_vsync_mode(p_window_id, DisplayServer::VSYNC_MAILBOX);
  705. }
  706. }
  707. #endif // VULKAN_ENABLED
  708. #ifdef GLES3_ENABLED
  709. if (egl_manager) {
  710. egl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
  711. emulate_vsync = egl_manager->is_using_vsync();
  712. if (emulate_vsync) {
  713. print_verbose("VSYNC: manually throttling frames with swap delay 0.");
  714. egl_manager->set_use_vsync(false);
  715. }
  716. }
  717. #endif // GLES3_ENABLED
  718. }
  719. DisplayServer::VSyncMode DisplayServerWayland::window_get_vsync_mode(DisplayServer::WindowID p_window_id) const {
  720. if (emulate_vsync) {
  721. return DisplayServer::VSYNC_ENABLED;
  722. }
  723. #ifdef VULKAN_ENABLED
  724. if (rendering_context) {
  725. return rendering_context->window_get_vsync_mode(p_window_id);
  726. }
  727. #endif // VULKAN_ENABLED
  728. #ifdef GLES3_ENABLED
  729. if (egl_manager) {
  730. return egl_manager->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
  731. }
  732. #endif // GLES3_ENABLED
  733. return DisplayServer::VSYNC_ENABLED;
  734. }
  735. void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
  736. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  737. MutexLock mutex_lock(wayland_thread.mutex);
  738. if (p_shape == cursor_shape) {
  739. return;
  740. }
  741. cursor_shape = p_shape;
  742. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  743. // Hidden.
  744. return;
  745. }
  746. if (custom_cursors.has(p_shape)) {
  747. wayland_thread.cursor_set_custom_shape(p_shape);
  748. } else {
  749. wayland_thread.cursor_set_shape(p_shape);
  750. }
  751. }
  752. DisplayServerWayland::CursorShape DisplayServerWayland::cursor_get_shape() const {
  753. MutexLock mutex_lock(wayland_thread.mutex);
  754. return cursor_shape;
  755. }
  756. void DisplayServerWayland::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  757. MutexLock mutex_lock(wayland_thread.mutex);
  758. bool visible = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED);
  759. if (p_cursor.is_valid()) {
  760. HashMap<CursorShape, CustomCursor>::Iterator cursor_c = custom_cursors.find(p_shape);
  761. if (cursor_c) {
  762. if (cursor_c->value.rid == p_cursor->get_rid() && cursor_c->value.hotspot == p_hotspot) {
  763. // We have a cached cursor. Nice.
  764. if (visible) {
  765. wayland_thread.cursor_set_custom_shape(p_shape);
  766. }
  767. return;
  768. }
  769. // We're changing this cursor; we'll have to rebuild it.
  770. custom_cursors.erase(p_shape);
  771. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  772. }
  773. Rect2 atlas_rect;
  774. Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot, atlas_rect);
  775. ERR_FAIL_COND(image.is_null());
  776. CustomCursor &cursor = custom_cursors[p_shape];
  777. cursor.rid = p_cursor->get_rid();
  778. cursor.hotspot = p_hotspot;
  779. wayland_thread.cursor_shape_set_custom_image(p_shape, image, p_hotspot);
  780. if (visible) {
  781. wayland_thread.cursor_set_custom_shape(p_shape);
  782. }
  783. } else {
  784. // Clear cache and reset to default system cursor.
  785. if (cursor_shape == p_shape && visible) {
  786. wayland_thread.cursor_set_shape(p_shape);
  787. }
  788. if (custom_cursors.has(p_shape)) {
  789. custom_cursors.erase(p_shape);
  790. }
  791. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  792. }
  793. }
  794. int DisplayServerWayland::keyboard_get_layout_count() const {
  795. MutexLock mutex_lock(wayland_thread.mutex);
  796. return wayland_thread.keyboard_get_layout_count();
  797. }
  798. int DisplayServerWayland::keyboard_get_current_layout() const {
  799. MutexLock mutex_lock(wayland_thread.mutex);
  800. return wayland_thread.keyboard_get_current_layout_index();
  801. }
  802. void DisplayServerWayland::keyboard_set_current_layout(int p_index) {
  803. MutexLock mutex_lock(wayland_thread.mutex);
  804. wayland_thread.keyboard_set_current_layout_index(p_index);
  805. }
  806. String DisplayServerWayland::keyboard_get_layout_language(int p_index) const {
  807. MutexLock mutex_lock(wayland_thread.mutex);
  808. // xkbcommon exposes only the layout's name, which looks like it overlaps with
  809. // its language.
  810. return wayland_thread.keyboard_get_layout_name(p_index);
  811. }
  812. String DisplayServerWayland::keyboard_get_layout_name(int p_index) const {
  813. MutexLock mutex_lock(wayland_thread.mutex);
  814. return wayland_thread.keyboard_get_layout_name(p_index);
  815. }
  816. Key DisplayServerWayland::keyboard_get_keycode_from_physical(Key p_keycode) const {
  817. MutexLock mutex_lock(wayland_thread.mutex);
  818. Key key = wayland_thread.keyboard_get_key_from_physical(p_keycode);
  819. // If not found, fallback to QWERTY.
  820. // This should match the behavior of the event pump.
  821. if (key == Key::NONE) {
  822. return p_keycode;
  823. }
  824. if (key >= Key::A + 32 && key <= Key::Z + 32) {
  825. key -= 'a' - 'A';
  826. }
  827. // Make it consistent with the keys returned by `Input`.
  828. if (key == Key::BACKTAB) {
  829. key = Key::TAB;
  830. }
  831. return key;
  832. }
  833. void DisplayServerWayland::process_events() {
  834. wayland_thread.mutex.lock();
  835. while (wayland_thread.has_message()) {
  836. Ref<WaylandThread::Message> msg = wayland_thread.pop_message();
  837. Ref<WaylandThread::WindowRectMessage> winrect_msg = msg;
  838. if (winrect_msg.is_valid()) {
  839. _resize_window(winrect_msg->rect.size);
  840. }
  841. Ref<WaylandThread::WindowEventMessage> winev_msg = msg;
  842. if (winev_msg.is_valid()) {
  843. _send_window_event(winev_msg->event);
  844. if (winev_msg->event == WINDOW_EVENT_FOCUS_IN) {
  845. if (OS::get_singleton()->get_main_loop()) {
  846. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  847. }
  848. } else if (winev_msg->event == WINDOW_EVENT_FOCUS_OUT) {
  849. if (OS::get_singleton()->get_main_loop()) {
  850. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  851. }
  852. }
  853. }
  854. Ref<WaylandThread::InputEventMessage> inputev_msg = msg;
  855. if (inputev_msg.is_valid()) {
  856. Input::get_singleton()->parse_input_event(inputev_msg->event);
  857. }
  858. Ref<WaylandThread::DropFilesEventMessage> dropfiles_msg = msg;
  859. if (dropfiles_msg.is_valid()) {
  860. WindowData wd = main_window;
  861. if (wd.drop_files_callback.is_valid()) {
  862. wd.drop_files_callback.call(dropfiles_msg->files);
  863. }
  864. }
  865. }
  866. wayland_thread.keyboard_echo_keys();
  867. if (!suspended) {
  868. if (emulate_vsync) {
  869. // Due to various reasons, we manually handle display synchronization by
  870. // waiting for a frame event (request to draw) or, if available, the actual
  871. // window's suspend status. When a window is suspended, we can avoid drawing
  872. // altogether, either because the compositor told us that we don't need to or
  873. // because the pace of the frame events became unreliable.
  874. bool frame = wayland_thread.wait_frame_suspend_ms(1000);
  875. if (!frame) {
  876. suspended = true;
  877. }
  878. } else {
  879. if (wayland_thread.is_suspended()) {
  880. suspended = true;
  881. }
  882. }
  883. if (suspended) {
  884. DEBUG_LOG_WAYLAND("Window suspended.");
  885. }
  886. } else {
  887. if (wayland_thread.get_reset_frame()) {
  888. // At last, a sign of life! We're no longer suspended.
  889. suspended = false;
  890. }
  891. }
  892. wayland_thread.mutex.unlock();
  893. Input::get_singleton()->flush_buffered_events();
  894. }
  895. void DisplayServerWayland::release_rendering_thread() {
  896. #ifdef GLES3_ENABLED
  897. if (egl_manager) {
  898. egl_manager->release_current();
  899. }
  900. #endif
  901. }
  902. void DisplayServerWayland::swap_buffers() {
  903. #ifdef GLES3_ENABLED
  904. if (egl_manager) {
  905. egl_manager->swap_buffers();
  906. }
  907. #endif
  908. }
  909. void DisplayServerWayland::set_context(Context p_context) {
  910. MutexLock mutex_lock(wayland_thread.mutex);
  911. DEBUG_LOG_WAYLAND(vformat("Setting context %d.", p_context));
  912. context = p_context;
  913. String app_id = _get_app_id_from_context(p_context);
  914. wayland_thread.window_set_app_id(MAIN_WINDOW_ID, app_id);
  915. }
  916. Vector<String> DisplayServerWayland::get_rendering_drivers_func() {
  917. Vector<String> drivers;
  918. #ifdef VULKAN_ENABLED
  919. drivers.push_back("vulkan");
  920. #endif
  921. #ifdef GLES3_ENABLED
  922. drivers.push_back("opengl3");
  923. #endif
  924. return drivers;
  925. }
  926. DisplayServer *DisplayServerWayland::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Error &r_error) {
  927. DisplayServer *ds = memnew(DisplayServerWayland(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  928. if (r_error != OK) {
  929. ERR_PRINT("Can't create the Wayland display server.");
  930. memdelete(ds);
  931. return nullptr;
  932. }
  933. return ds;
  934. }
  935. DisplayServerWayland::DisplayServerWayland(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  936. #ifdef GLES3_ENABLED
  937. #ifdef SOWRAP_ENABLED
  938. #ifdef DEBUG_ENABLED
  939. int dylibloader_verbose = 1;
  940. #else
  941. int dylibloader_verbose = 0;
  942. #endif // DEBUG_ENABLED
  943. #endif // SOWRAP_ENABLED
  944. #endif // GLES3_ENABLED
  945. r_error = ERR_UNAVAILABLE;
  946. Error thread_err = wayland_thread.init();
  947. if (thread_err != OK) {
  948. r_error = thread_err;
  949. ERR_FAIL_MSG("Could not initialize the Wayland thread.");
  950. }
  951. // Input.
  952. Input::get_singleton()->set_event_dispatch_function(dispatch_input_events);
  953. native_menu = memnew(NativeMenu);
  954. #ifdef SPEECHD_ENABLED
  955. // Init TTS
  956. tts = memnew(TTS_Linux);
  957. #endif
  958. rendering_driver = p_rendering_driver;
  959. #ifdef RD_ENABLED
  960. #ifdef VULKAN_ENABLED
  961. if (p_rendering_driver == "vulkan") {
  962. rendering_context = memnew(RenderingContextDriverVulkanWayland);
  963. }
  964. #endif
  965. if (rendering_context) {
  966. if (rendering_context->initialize() != OK) {
  967. ERR_PRINT(vformat("Could not initialize %s", p_rendering_driver));
  968. memdelete(rendering_context);
  969. rendering_context = nullptr;
  970. r_error = ERR_CANT_CREATE;
  971. return;
  972. }
  973. }
  974. #endif
  975. #ifdef GLES3_ENABLED
  976. if (p_rendering_driver == "opengl3") {
  977. if (getenv("DRI_PRIME") == nullptr) {
  978. int prime_idx = -1;
  979. if (getenv("PRIMUS_DISPLAY") ||
  980. getenv("PRIMUS_libGLd") ||
  981. getenv("PRIMUS_libGLa") ||
  982. getenv("PRIMUS_libGL") ||
  983. getenv("PRIMUS_LOAD_GLOBAL") ||
  984. getenv("BUMBLEBEE_SOCKET") ||
  985. getenv("__NV_PRIME_RENDER_OFFLOAD")) {
  986. print_verbose("Optirun/primusrun detected. Skipping GPU detection");
  987. prime_idx = 0;
  988. }
  989. // Some tools use fake libGL libraries and have them override the real one using
  990. // LD_LIBRARY_PATH, so we skip them. *But* Steam also sets LD_LIBRARY_PATH for its
  991. // runtime and includes system `/lib` and `/lib64`... so ignore Steam.
  992. if (prime_idx == -1 && getenv("LD_LIBRARY_PATH") && !getenv("STEAM_RUNTIME_LIBRARY_PATH")) {
  993. String ld_library_path(getenv("LD_LIBRARY_PATH"));
  994. Vector<String> libraries = ld_library_path.split(":");
  995. for (int i = 0; i < libraries.size(); ++i) {
  996. if (FileAccess::exists(libraries[i] + "/libGL.so.1") ||
  997. FileAccess::exists(libraries[i] + "/libGL.so")) {
  998. print_verbose("Custom libGL override detected. Skipping GPU detection");
  999. prime_idx = 0;
  1000. }
  1001. }
  1002. }
  1003. if (prime_idx == -1) {
  1004. print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic.");
  1005. prime_idx = DetectPrimeEGL::detect_prime();
  1006. }
  1007. if (prime_idx) {
  1008. print_line(vformat("Found discrete GPU, setting DRI_PRIME=%d to use it.", prime_idx));
  1009. print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU.");
  1010. setenv("DRI_PRIME", itos(prime_idx).utf8().ptr(), 1);
  1011. }
  1012. }
  1013. egl_manager = memnew(EGLManagerWayland);
  1014. #ifdef SOWRAP_ENABLED
  1015. if (initialize_wayland_egl(dylibloader_verbose) != 0) {
  1016. WARN_PRINT("Can't load the Wayland EGL library.");
  1017. return;
  1018. }
  1019. #endif // SOWRAP_ENABLED
  1020. if (egl_manager->initialize() != OK) {
  1021. memdelete(egl_manager);
  1022. egl_manager = nullptr;
  1023. r_error = ERR_CANT_CREATE;
  1024. ERR_FAIL_MSG("Could not initialize GLES3.");
  1025. }
  1026. RasterizerGLES3::make_current(true);
  1027. }
  1028. #endif // GLES3_ENABLED
  1029. cursor_set_shape(CURSOR_BUSY);
  1030. WindowData &wd = main_window;
  1031. wd.id = MAIN_WINDOW_ID;
  1032. wd.mode = p_mode;
  1033. wd.flags = p_flags;
  1034. wd.vsync_mode = p_vsync_mode;
  1035. wd.rect.size = p_resolution;
  1036. wd.title = "Godot";
  1037. _show_window();
  1038. #ifdef RD_ENABLED
  1039. if (rendering_context) {
  1040. rendering_device = memnew(RenderingDevice);
  1041. rendering_device->initialize(rendering_context, MAIN_WINDOW_ID);
  1042. rendering_device->screen_create(MAIN_WINDOW_ID);
  1043. RendererCompositorRD::make_current();
  1044. }
  1045. #endif
  1046. #ifdef DBUS_ENABLED
  1047. portal_desktop = memnew(FreeDesktopPortalDesktop);
  1048. screensaver = memnew(FreeDesktopScreenSaver);
  1049. #endif
  1050. screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  1051. r_error = OK;
  1052. }
  1053. DisplayServerWayland::~DisplayServerWayland() {
  1054. // TODO: Multiwindow support.
  1055. if (native_menu) {
  1056. memdelete(native_menu);
  1057. native_menu = nullptr;
  1058. }
  1059. if (main_window.visible) {
  1060. #ifdef VULKAN_ENABLED
  1061. if (rendering_device) {
  1062. rendering_device->screen_free(MAIN_WINDOW_ID);
  1063. }
  1064. if (rendering_context) {
  1065. rendering_context->window_destroy(MAIN_WINDOW_ID);
  1066. }
  1067. #endif
  1068. #ifdef GLES3_ENABLED
  1069. if (egl_manager) {
  1070. egl_manager->window_destroy(MAIN_WINDOW_ID);
  1071. }
  1072. #endif
  1073. }
  1074. #ifdef GLES3_ENABLED
  1075. if (main_window.wl_egl_window) {
  1076. wl_egl_window_destroy(main_window.wl_egl_window);
  1077. }
  1078. #endif
  1079. wayland_thread.destroy();
  1080. // Destroy all drivers.
  1081. #ifdef RD_ENABLED
  1082. if (rendering_device) {
  1083. memdelete(rendering_device);
  1084. }
  1085. if (rendering_context) {
  1086. memdelete(rendering_context);
  1087. }
  1088. #endif
  1089. #ifdef SPEECHD_ENABLED
  1090. if (tts) {
  1091. memdelete(tts);
  1092. }
  1093. #endif
  1094. #ifdef DBUS_ENABLED
  1095. if (portal_desktop) {
  1096. memdelete(portal_desktop);
  1097. memdelete(screensaver);
  1098. }
  1099. #endif
  1100. }
  1101. void DisplayServerWayland::register_wayland_driver() {
  1102. register_create_function("wayland", create_func, get_rendering_drivers_func);
  1103. }
  1104. #endif //WAYLAND_ENABLED