display_server_wayland.cpp 42 KB

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