display_server_wayland.cpp 42 KB

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