display_server_wayland.cpp 44 KB

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