display_server_wayland.cpp 45 KB

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