display_server_wayland.cpp 42 KB

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