display_server_wayland.cpp 42 KB

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