display_server_wayland.cpp 43 KB

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