display_server_wayland.cpp 41 KB

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