display_server_wayland.cpp 49 KB

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