display_server_wayland.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  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. #include "core/os/main_loop.h"
  39. #include "servers/rendering/dummy/rasterizer_dummy.h"
  40. #ifdef VULKAN_ENABLED
  41. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  42. #endif
  43. #ifdef GLES3_ENABLED
  44. #include "core/io/file_access.h"
  45. #include "detect_prime_egl.h"
  46. #include "drivers/gles3/rasterizer_gles3.h"
  47. #include "wayland/egl_manager_wayland.h"
  48. #include "wayland/egl_manager_wayland_gles.h"
  49. #endif
  50. #ifdef ACCESSKIT_ENABLED
  51. #include "drivers/accesskit/accessibility_driver_accesskit.h"
  52. #endif
  53. #ifdef DBUS_ENABLED
  54. #ifdef SOWRAP_ENABLED
  55. #include "dbus-so_wrap.h"
  56. #else
  57. #include <dbus/dbus.h>
  58. #endif
  59. #endif
  60. #define WAYLAND_MAX_FRAME_TIME_US (1'000'000)
  61. String DisplayServerWayland::_get_app_id_from_context(Context p_context) {
  62. String app_id;
  63. switch (p_context) {
  64. case CONTEXT_EDITOR: {
  65. app_id = "org.godotengine.Editor";
  66. } break;
  67. case CONTEXT_PROJECTMAN: {
  68. app_id = "org.godotengine.ProjectManager";
  69. } break;
  70. case CONTEXT_ENGINE:
  71. default: {
  72. String config_name = GLOBAL_GET("application/config/name");
  73. if (config_name.length() != 0) {
  74. app_id = config_name;
  75. } else {
  76. app_id = "org.godotengine.Godot";
  77. }
  78. }
  79. }
  80. return app_id;
  81. }
  82. void DisplayServerWayland::_send_window_event(WindowEvent p_event, WindowID p_window_id) {
  83. ERR_FAIL_COND(!windows.has(p_window_id));
  84. WindowData &wd = windows[p_window_id];
  85. if (wd.window_event_callback.is_valid()) {
  86. Variant event = int(p_event);
  87. wd.window_event_callback.call(event);
  88. }
  89. }
  90. void DisplayServerWayland::dispatch_input_events(const Ref<InputEvent> &p_event) {
  91. static_cast<DisplayServerWayland *>(get_singleton())->_dispatch_input_event(p_event);
  92. }
  93. void DisplayServerWayland::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  94. Ref<InputEventFromWindow> event_from_window = p_event;
  95. if (event_from_window.is_valid()) {
  96. WindowID window_id = event_from_window->get_window_id();
  97. Ref<InputEventKey> key_event = p_event;
  98. if (!popup_menu_list.is_empty() && key_event.is_valid()) {
  99. // Redirect to the highest popup menu.
  100. window_id = popup_menu_list.back()->get();
  101. }
  102. // Send to a single window.
  103. if (windows.has(window_id)) {
  104. Callable callable = windows[window_id].input_event_callback;
  105. if (callable.is_valid()) {
  106. callable.call(p_event);
  107. }
  108. }
  109. } else {
  110. // Send to all windows. Copy all pending callbacks, since callback can erase window.
  111. Vector<Callable> cbs;
  112. for (KeyValue<WindowID, WindowData> &E : windows) {
  113. Callable callable = E.value.input_event_callback;
  114. if (callable.is_valid()) {
  115. cbs.push_back(callable);
  116. }
  117. }
  118. for (const Callable &cb : cbs) {
  119. cb.call(p_event);
  120. }
  121. }
  122. }
  123. void DisplayServerWayland::_update_window_rect(const Rect2i &p_rect, WindowID p_window_id) {
  124. ERR_FAIL_COND(!windows.has(p_window_id));
  125. WindowData &wd = windows[p_window_id];
  126. if (wd.rect == p_rect) {
  127. return;
  128. }
  129. wd.rect = p_rect;
  130. #ifdef RD_ENABLED
  131. if (wd.visible && rendering_context) {
  132. rendering_context->window_set_size(p_window_id, wd.rect.size.width, wd.rect.size.height);
  133. }
  134. #endif
  135. #ifdef GLES3_ENABLED
  136. if (wd.visible && egl_manager) {
  137. wl_egl_window_resize(wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height, 0, 0);
  138. }
  139. #endif
  140. if (wd.rect_changed_callback.is_valid()) {
  141. wd.rect_changed_callback.call(wd.rect);
  142. }
  143. }
  144. // Interface methods.
  145. bool DisplayServerWayland::has_feature(Feature p_feature) const {
  146. switch (p_feature) {
  147. #ifndef DISABLE_DEPRECATED
  148. case FEATURE_GLOBAL_MENU: {
  149. return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU));
  150. } break;
  151. #endif
  152. case FEATURE_MOUSE:
  153. case FEATURE_MOUSE_WARP:
  154. case FEATURE_CLIPBOARD:
  155. case FEATURE_CURSOR_SHAPE:
  156. case FEATURE_CUSTOM_CURSOR_SHAPE:
  157. case FEATURE_WINDOW_TRANSPARENCY:
  158. case FEATURE_ICON:
  159. case FEATURE_HIDPI:
  160. case FEATURE_SWAP_BUFFERS:
  161. case FEATURE_KEEP_SCREEN_ON:
  162. case FEATURE_IME:
  163. case FEATURE_WINDOW_DRAG:
  164. case FEATURE_CLIPBOARD_PRIMARY:
  165. case FEATURE_SUBWINDOWS:
  166. case FEATURE_WINDOW_EMBEDDING:
  167. case FEATURE_SELF_FITTING_WINDOWS: {
  168. return true;
  169. } break;
  170. //case FEATURE_NATIVE_DIALOG:
  171. //case FEATURE_NATIVE_DIALOG_INPUT:
  172. #ifdef DBUS_ENABLED
  173. case FEATURE_NATIVE_DIALOG_FILE:
  174. case FEATURE_NATIVE_DIALOG_FILE_EXTRA:
  175. case FEATURE_NATIVE_DIALOG_FILE_MIME: {
  176. return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_file_chooser_supported());
  177. } break;
  178. case FEATURE_NATIVE_COLOR_PICKER: {
  179. return (portal_desktop && portal_desktop->is_supported() && portal_desktop->is_screenshot_supported());
  180. } break;
  181. #endif
  182. #ifdef SPEECHD_ENABLED
  183. case FEATURE_TEXT_TO_SPEECH: {
  184. return true;
  185. } break;
  186. #endif
  187. #ifdef ACCESSKIT_ENABLED
  188. case FEATURE_ACCESSIBILITY_SCREEN_READER: {
  189. return (accessibility_driver != nullptr);
  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. void DisplayServerWayland::initialize_tts() const {
  202. const_cast<DisplayServerWayland *>(this)->tts = memnew(TTS_Linux);
  203. }
  204. bool DisplayServerWayland::tts_is_speaking() const {
  205. if (unlikely(!tts)) {
  206. initialize_tts();
  207. }
  208. ERR_FAIL_NULL_V(tts, false);
  209. return tts->is_speaking();
  210. }
  211. bool DisplayServerWayland::tts_is_paused() const {
  212. if (unlikely(!tts)) {
  213. initialize_tts();
  214. }
  215. ERR_FAIL_NULL_V(tts, false);
  216. return tts->is_paused();
  217. }
  218. TypedArray<Dictionary> DisplayServerWayland::tts_get_voices() const {
  219. if (unlikely(!tts)) {
  220. initialize_tts();
  221. }
  222. ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
  223. return tts->get_voices();
  224. }
  225. void DisplayServerWayland::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) {
  226. if (unlikely(!tts)) {
  227. initialize_tts();
  228. }
  229. ERR_FAIL_NULL(tts);
  230. tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt);
  231. }
  232. void DisplayServerWayland::tts_pause() {
  233. if (unlikely(!tts)) {
  234. initialize_tts();
  235. }
  236. ERR_FAIL_NULL(tts);
  237. tts->pause();
  238. }
  239. void DisplayServerWayland::tts_resume() {
  240. if (unlikely(!tts)) {
  241. initialize_tts();
  242. }
  243. ERR_FAIL_NULL(tts);
  244. tts->resume();
  245. }
  246. void DisplayServerWayland::tts_stop() {
  247. if (unlikely(!tts)) {
  248. initialize_tts();
  249. }
  250. ERR_FAIL_NULL(tts);
  251. tts->stop();
  252. }
  253. #endif
  254. #ifdef DBUS_ENABLED
  255. bool DisplayServerWayland::is_dark_mode_supported() const {
  256. return portal_desktop && portal_desktop->is_supported() && portal_desktop->is_settings_supported();
  257. }
  258. bool DisplayServerWayland::is_dark_mode() const {
  259. if (!is_dark_mode_supported()) {
  260. return false;
  261. }
  262. switch (portal_desktop->get_appearance_color_scheme()) {
  263. case 1:
  264. // Prefers dark theme.
  265. return true;
  266. case 2:
  267. // Prefers light theme.
  268. return false;
  269. default:
  270. // Preference unknown.
  271. return false;
  272. }
  273. }
  274. Color DisplayServerWayland::get_accent_color() const {
  275. if (!portal_desktop) {
  276. return Color();
  277. }
  278. return portal_desktop->get_appearance_accent_color();
  279. }
  280. void DisplayServerWayland::set_system_theme_change_callback(const Callable &p_callable) {
  281. ERR_FAIL_COND(!portal_desktop);
  282. portal_desktop->set_system_theme_change_callback(p_callable);
  283. }
  284. 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, WindowID p_window_id) {
  285. ERR_FAIL_COND_V(!portal_desktop, ERR_UNAVAILABLE);
  286. MutexLock mutex_lock(wayland_thread.mutex);
  287. WindowID window_id = p_window_id;
  288. if (!windows.has(window_id) || window_get_flag(WINDOW_FLAG_POPUP_WM_HINT, window_id)) {
  289. window_id = MAIN_WINDOW_ID;
  290. }
  291. WaylandThread::WindowState *ws = wayland_thread.window_get_state(window_id);
  292. ERR_FAIL_NULL_V(ws, ERR_BUG);
  293. 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);
  294. }
  295. 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, WindowID p_window_id) {
  296. ERR_FAIL_COND_V(!portal_desktop, ERR_UNAVAILABLE);
  297. MutexLock mutex_lock(wayland_thread.mutex);
  298. WindowID window_id = p_window_id;
  299. if (!windows.has(window_id) || window_get_flag(WINDOW_FLAG_POPUP_WM_HINT, window_id)) {
  300. window_id = MAIN_WINDOW_ID;
  301. }
  302. WaylandThread::WindowState *ws = wayland_thread.window_get_state(window_id);
  303. ERR_FAIL_NULL_V(ws, ERR_BUG);
  304. 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);
  305. }
  306. #endif
  307. void DisplayServerWayland::beep() const {
  308. wayland_thread.beep();
  309. }
  310. void DisplayServerWayland::_mouse_update_mode() {
  311. MouseMode wanted_mouse_mode = mouse_mode_override_enabled
  312. ? mouse_mode_override
  313. : mouse_mode_base;
  314. if (wanted_mouse_mode == mouse_mode) {
  315. return;
  316. }
  317. MutexLock mutex_lock(wayland_thread.mutex);
  318. bool show_cursor = (wanted_mouse_mode == MOUSE_MODE_VISIBLE || wanted_mouse_mode == MOUSE_MODE_CONFINED);
  319. wayland_thread.cursor_set_visible(show_cursor);
  320. WaylandThread::PointerConstraint constraint = WaylandThread::PointerConstraint::NONE;
  321. switch (wanted_mouse_mode) {
  322. case DisplayServer::MOUSE_MODE_CAPTURED: {
  323. constraint = WaylandThread::PointerConstraint::LOCKED;
  324. } break;
  325. case DisplayServer::MOUSE_MODE_CONFINED:
  326. case DisplayServer::MOUSE_MODE_CONFINED_HIDDEN: {
  327. constraint = WaylandThread::PointerConstraint::CONFINED;
  328. } break;
  329. default: {
  330. }
  331. }
  332. wayland_thread.pointer_set_constraint(constraint);
  333. if (wanted_mouse_mode == DisplayServer::MOUSE_MODE_CAPTURED) {
  334. WindowData *pointed_win = windows.getptr(wayland_thread.pointer_get_pointed_window_id());
  335. ERR_FAIL_NULL(pointed_win);
  336. wayland_thread.pointer_set_hint(pointed_win->rect.size / 2);
  337. }
  338. mouse_mode = wanted_mouse_mode;
  339. }
  340. void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
  341. ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
  342. if (p_mode == mouse_mode_base) {
  343. return;
  344. }
  345. mouse_mode_base = p_mode;
  346. _mouse_update_mode();
  347. }
  348. DisplayServerWayland::MouseMode DisplayServerWayland::mouse_get_mode() const {
  349. return mouse_mode;
  350. }
  351. void DisplayServerWayland::mouse_set_mode_override(MouseMode p_mode) {
  352. ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
  353. if (p_mode == mouse_mode_override) {
  354. return;
  355. }
  356. mouse_mode_override = p_mode;
  357. _mouse_update_mode();
  358. }
  359. DisplayServerWayland::MouseMode DisplayServerWayland::mouse_get_mode_override() const {
  360. return mouse_mode_override;
  361. }
  362. void DisplayServerWayland::mouse_set_mode_override_enabled(bool p_override_enabled) {
  363. if (p_override_enabled == mouse_mode_override_enabled) {
  364. return;
  365. }
  366. mouse_mode_override_enabled = p_override_enabled;
  367. _mouse_update_mode();
  368. }
  369. bool DisplayServerWayland::mouse_is_mode_override_enabled() const {
  370. return mouse_mode_override_enabled;
  371. }
  372. // NOTE: This is hacked together (and not guaranteed to work in the first place)
  373. // as for some reason the there's no proper way to ask the compositor to warp
  374. // the pointer, although, at the time of writing, there's a proposal for a
  375. // proper protocol for this. See:
  376. // https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/158
  377. void DisplayServerWayland::warp_mouse(const Point2i &p_to) {
  378. MutexLock mutex_lock(wayland_thread.mutex);
  379. WaylandThread::PointerConstraint old_constraint = wayland_thread.pointer_get_constraint();
  380. wayland_thread.pointer_set_constraint(WaylandThread::PointerConstraint::LOCKED);
  381. wayland_thread.pointer_set_hint(p_to);
  382. wayland_thread.pointer_set_constraint(old_constraint);
  383. }
  384. Point2i DisplayServerWayland::mouse_get_position() const {
  385. MutexLock mutex_lock(wayland_thread.mutex);
  386. WindowID pointed_id = wayland_thread.pointer_get_pointed_window_id();
  387. if (pointed_id != INVALID_WINDOW_ID && windows.has(pointed_id)) {
  388. return Input::get_singleton()->get_mouse_position() + windows[pointed_id].rect.position;
  389. }
  390. // We can't properly implement this method by design.
  391. // This is the best we can do unfortunately.
  392. return Input::get_singleton()->get_mouse_position();
  393. }
  394. BitField<MouseButtonMask> DisplayServerWayland::mouse_get_button_state() const {
  395. MutexLock mutex_lock(wayland_thread.mutex);
  396. // Are we sure this is the only way? This seems sus.
  397. // TODO: Handle tablets properly.
  398. //mouse_button_mask.set_flag(MouseButtonMask((int64_t)wls.current_seat->tablet_tool_data.pressed_button_mask));
  399. return wayland_thread.pointer_get_button_mask();
  400. }
  401. // NOTE: According to the Wayland specification, this method will only do
  402. // anything if the user has interacted with the application by sending a
  403. // "recent enough" input event.
  404. // TODO: Add this limitation to the documentation.
  405. void DisplayServerWayland::clipboard_set(const String &p_text) {
  406. MutexLock mutex_lock(wayland_thread.mutex);
  407. wayland_thread.selection_set_text(p_text);
  408. }
  409. String DisplayServerWayland::clipboard_get() const {
  410. MutexLock mutex_lock(wayland_thread.mutex);
  411. Vector<uint8_t> data;
  412. const String text_mimes[] = {
  413. "text/plain;charset=utf-8",
  414. "text/plain",
  415. };
  416. for (String mime : text_mimes) {
  417. if (wayland_thread.selection_has_mime(mime)) {
  418. print_verbose(vformat("Selecting media type \"%s\" from offered types.", mime));
  419. data = wayland_thread.selection_get_mime(mime);
  420. break;
  421. }
  422. }
  423. return String::utf8((const char *)data.ptr(), data.size());
  424. }
  425. Ref<Image> DisplayServerWayland::clipboard_get_image() const {
  426. MutexLock mutex_lock(wayland_thread.mutex);
  427. Ref<Image> image;
  428. image.instantiate();
  429. Error err = OK;
  430. // TODO: Fallback to next media type on missing module or parse error.
  431. if (wayland_thread.selection_has_mime("image/png")) {
  432. err = image->load_png_from_buffer(wayland_thread.selection_get_mime("image/png"));
  433. } else if (wayland_thread.selection_has_mime("image/jpeg")) {
  434. err = image->load_jpg_from_buffer(wayland_thread.selection_get_mime("image/jpeg"));
  435. } else if (wayland_thread.selection_has_mime("image/webp")) {
  436. err = image->load_webp_from_buffer(wayland_thread.selection_get_mime("image/webp"));
  437. } else if (wayland_thread.selection_has_mime("image/svg+xml")) {
  438. err = image->load_svg_from_buffer(wayland_thread.selection_get_mime("image/svg+xml"));
  439. } else if (wayland_thread.selection_has_mime("image/bmp")) {
  440. err = image->load_bmp_from_buffer(wayland_thread.selection_get_mime("image/bmp"));
  441. } else if (wayland_thread.selection_has_mime("image/x-tga")) {
  442. err = image->load_tga_from_buffer(wayland_thread.selection_get_mime("image/x-tga"));
  443. } else if (wayland_thread.selection_has_mime("image/x-targa")) {
  444. err = image->load_tga_from_buffer(wayland_thread.selection_get_mime("image/x-targa"));
  445. } else if (wayland_thread.selection_has_mime("image/ktx")) {
  446. err = image->load_ktx_from_buffer(wayland_thread.selection_get_mime("image/ktx"));
  447. } else if (wayland_thread.selection_has_mime("image/x-exr")) {
  448. err = image->load_exr_from_buffer(wayland_thread.selection_get_mime("image/x-exr"));
  449. }
  450. ERR_FAIL_COND_V(err != OK, Ref<Image>());
  451. return image;
  452. }
  453. void DisplayServerWayland::clipboard_set_primary(const String &p_text) {
  454. MutexLock mutex_lock(wayland_thread.mutex);
  455. wayland_thread.primary_set_text(p_text);
  456. }
  457. String DisplayServerWayland::clipboard_get_primary() const {
  458. MutexLock mutex_lock(wayland_thread.mutex);
  459. Vector<uint8_t> data;
  460. const String text_mimes[] = {
  461. "text/plain;charset=utf-8",
  462. "text/plain",
  463. };
  464. for (String mime : text_mimes) {
  465. if (wayland_thread.primary_has_mime(mime)) {
  466. print_verbose(vformat("Selecting media type \"%s\" from offered types.", mime));
  467. data = wayland_thread.primary_get_mime(mime);
  468. break;
  469. }
  470. }
  471. return String::utf8((const char *)data.ptr(), data.size());
  472. }
  473. int DisplayServerWayland::get_screen_count() const {
  474. MutexLock mutex_lock(wayland_thread.mutex);
  475. return wayland_thread.get_screen_count();
  476. }
  477. int DisplayServerWayland::get_primary_screen() const {
  478. // AFAIK Wayland doesn't allow knowing (nor we care) about which screen is
  479. // primary.
  480. return 0;
  481. }
  482. Point2i DisplayServerWayland::screen_get_position(int p_screen) const {
  483. MutexLock mutex_lock(wayland_thread.mutex);
  484. p_screen = _get_screen_index(p_screen);
  485. int screen_count = get_screen_count();
  486. ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i());
  487. return wayland_thread.screen_get_data(p_screen).position;
  488. }
  489. Size2i DisplayServerWayland::screen_get_size(int p_screen) const {
  490. MutexLock mutex_lock(wayland_thread.mutex);
  491. p_screen = _get_screen_index(p_screen);
  492. int screen_count = get_screen_count();
  493. ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i());
  494. return wayland_thread.screen_get_data(p_screen).size;
  495. }
  496. Rect2i DisplayServerWayland::screen_get_usable_rect(int p_screen) const {
  497. p_screen = _get_screen_index(p_screen);
  498. int screen_count = get_screen_count();
  499. ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i());
  500. return Rect2i(screen_get_position(p_screen), screen_get_size(p_screen));
  501. }
  502. int DisplayServerWayland::screen_get_dpi(int p_screen) const {
  503. MutexLock mutex_lock(wayland_thread.mutex);
  504. p_screen = _get_screen_index(p_screen);
  505. int screen_count = get_screen_count();
  506. ERR_FAIL_INDEX_V(p_screen, screen_count, 96);
  507. const WaylandThread::ScreenData &data = wayland_thread.screen_get_data(p_screen);
  508. int width_mm = data.physical_size.width;
  509. int height_mm = data.physical_size.height;
  510. double xdpi = (width_mm ? data.size.width / (double)width_mm * 25.4 : 0);
  511. double ydpi = (height_mm ? data.size.height / (double)height_mm * 25.4 : 0);
  512. if (xdpi || ydpi) {
  513. return (xdpi + ydpi) / (xdpi && ydpi ? 2 : 1);
  514. }
  515. // Could not get DPI.
  516. return 96;
  517. }
  518. float DisplayServerWayland::screen_get_scale(int p_screen) const {
  519. MutexLock mutex_lock(wayland_thread.mutex);
  520. if (p_screen == SCREEN_OF_MAIN_WINDOW) {
  521. // Wayland does not expose fractional scale factors at the screen-level, but
  522. // some code relies on it. Since this special screen is the default and a lot
  523. // of code relies on it, we'll return the window's scale, which is what we
  524. // really care about. After all, we have very little use of the actual screen
  525. // enumeration APIs and we're (for now) in single-window mode anyways.
  526. struct wl_surface *wl_surface = wayland_thread.window_get_wl_surface(MAIN_WINDOW_ID);
  527. WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wl_surface);
  528. return wayland_thread.window_state_get_scale_factor(ws);
  529. }
  530. p_screen = _get_screen_index(p_screen);
  531. int screen_count = get_screen_count();
  532. ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f);
  533. return wayland_thread.screen_get_data(p_screen).scale;
  534. }
  535. float DisplayServerWayland::screen_get_refresh_rate(int p_screen) const {
  536. MutexLock mutex_lock(wayland_thread.mutex);
  537. p_screen = _get_screen_index(p_screen);
  538. int screen_count = get_screen_count();
  539. ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK);
  540. return wayland_thread.screen_get_data(p_screen).refresh_rate;
  541. }
  542. void DisplayServerWayland::screen_set_keep_on(bool p_enable) {
  543. MutexLock mutex_lock(wayland_thread.mutex);
  544. // FIXME: For some reason this does not also windows from the wayland thread.
  545. if (screen_is_kept_on() == p_enable) {
  546. return;
  547. }
  548. wayland_thread.window_set_idle_inhibition(MAIN_WINDOW_ID, p_enable);
  549. #ifdef DBUS_ENABLED
  550. if (portal_desktop && portal_desktop->is_inhibit_supported()) {
  551. if (p_enable) {
  552. // Attach the inhibit request to the main window, not the last focused window,
  553. // on the basis that inhibiting the screensaver is global state for the application.
  554. WindowID window_id = MAIN_WINDOW_ID;
  555. WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
  556. screensaver_inhibited = portal_desktop->inhibit(ws ? ws->exported_handle : String());
  557. } else {
  558. portal_desktop->uninhibit();
  559. screensaver_inhibited = false;
  560. }
  561. } else if (screensaver) {
  562. if (p_enable) {
  563. screensaver->inhibit();
  564. } else {
  565. screensaver->uninhibit();
  566. }
  567. screensaver_inhibited = p_enable;
  568. }
  569. #endif
  570. }
  571. bool DisplayServerWayland::screen_is_kept_on() const {
  572. // FIXME: Multiwindow support.
  573. #ifdef DBUS_ENABLED
  574. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID) || screensaver_inhibited;
  575. #else
  576. return wayland_thread.window_get_idle_inhibition(MAIN_WINDOW_ID);
  577. #endif
  578. }
  579. Vector<DisplayServer::WindowID> DisplayServerWayland::get_window_list() const {
  580. MutexLock mutex_lock(wayland_thread.mutex);
  581. Vector<int> ret;
  582. for (const KeyValue<WindowID, WindowData> &E : windows) {
  583. ret.push_back(E.key);
  584. }
  585. return ret;
  586. }
  587. DisplayServer::WindowID DisplayServerWayland::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent) {
  588. WindowID id = ++window_id_counter;
  589. WindowData &wd = windows[id];
  590. wd.id = id;
  591. wd.mode = p_mode;
  592. wd.flags = p_flags;
  593. wd.vsync_mode = p_vsync_mode;
  594. #ifdef ACCESSKIT_ENABLED
  595. if (accessibility_driver && !accessibility_driver->window_create(wd.id, nullptr)) {
  596. if (OS::get_singleton()->is_stdout_verbose()) {
  597. ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!");
  598. }
  599. memdelete(accessibility_driver);
  600. accessibility_driver = nullptr;
  601. }
  602. #endif
  603. // NOTE: Remember to clear its position if this window will be a toplevel. We
  604. // can only know once we show it.
  605. wd.rect = p_rect;
  606. wd.title = "Godot";
  607. wd.parent_id = p_transient_parent;
  608. return id;
  609. }
  610. void DisplayServerWayland::show_window(WindowID p_window_id) {
  611. MutexLock mutex_lock(wayland_thread.mutex);
  612. ERR_FAIL_COND(!windows.has(p_window_id));
  613. WindowData &wd = windows[p_window_id];
  614. if (!wd.visible) {
  615. DEBUG_LOG_WAYLAND(vformat("Showing window %d", p_window_id));
  616. // Showing this window will reset its mode with whatever the compositor
  617. // reports. We'll save the mode beforehand so that we can reapply it later.
  618. // TODO: Fix/Port/Move/Whatever to `WaylandThread` APIs.
  619. WindowMode setup_mode = wd.mode;
  620. // Let's determine the closest toplevel. For toplevels it will be themselves,
  621. // for popups the first toplevel ancestor it finds.
  622. WindowID root_id = wd.id;
  623. while (root_id != INVALID_WINDOW_ID && window_get_flag(WINDOW_FLAG_POPUP_WM_HINT, root_id)) {
  624. root_id = windows[root_id].parent_id;
  625. }
  626. ERR_FAIL_COND(root_id == INVALID_WINDOW_ID);
  627. wd.root_id = root_id;
  628. if (!window_get_flag(WINDOW_FLAG_POPUP_WM_HINT, p_window_id)) {
  629. // NOTE: DO **NOT** KEEP THE POSITION SET FOR TOPLEVELS. Wayland does not
  630. // track them and we're gonna get our events transformed in unexpected ways.
  631. wd.rect.position = Point2i();
  632. DEBUG_LOG_WAYLAND(vformat("Creating regular window of size %s", wd.rect.size));
  633. wayland_thread.window_create(p_window_id, wd.rect.size, wd.parent_id);
  634. wayland_thread.window_set_min_size(p_window_id, wd.min_size);
  635. wayland_thread.window_set_max_size(p_window_id, wd.max_size);
  636. wayland_thread.window_set_app_id(p_window_id, _get_app_id_from_context(context));
  637. wayland_thread.window_set_borderless(p_window_id, window_get_flag(WINDOW_FLAG_BORDERLESS, p_window_id));
  638. // Since it can't have a position. Let's tell the window node the news by
  639. // the actual rect to it.
  640. if (wd.rect_changed_callback.is_valid()) {
  641. wd.rect_changed_callback.call(wd.rect);
  642. }
  643. } else {
  644. DEBUG_LOG_WAYLAND("!!!!! Making popup !!!!!");
  645. windows[root_id].popup_stack.push_back(p_window_id);
  646. if (window_get_flag(WINDOW_FLAG_POPUP, p_window_id)) {
  647. // Reroutes all input to it.
  648. popup_menu_list.push_back(p_window_id);
  649. }
  650. wayland_thread.window_create_popup(p_window_id, wd.parent_id, wd.rect);
  651. }
  652. // NOTE: The XDG shell protocol is built in a way that causes the window to
  653. // be immediately shown as soon as a valid buffer is assigned to it. Hence,
  654. // the only acceptable way of implementing window showing is to move the
  655. // graphics context window creation logic here.
  656. #ifdef RD_ENABLED
  657. if (rendering_context) {
  658. union {
  659. #ifdef VULKAN_ENABLED
  660. RenderingContextDriverVulkanWayland::WindowPlatformData vulkan;
  661. #endif
  662. } wpd;
  663. #ifdef VULKAN_ENABLED
  664. if (rendering_driver == "vulkan") {
  665. wpd.vulkan.surface = wayland_thread.window_get_wl_surface(wd.id);
  666. wpd.vulkan.display = wayland_thread.get_wl_display();
  667. }
  668. #endif
  669. Error err = rendering_context->window_create(wd.id, &wpd);
  670. ERR_FAIL_COND_MSG(err != OK, vformat("Can't create a %s window", rendering_driver));
  671. rendering_context->window_set_size(wd.id, wd.rect.size.width, wd.rect.size.height);
  672. // NOTE: Looks like we have to set the vsync mode before creating the screen
  673. // or it won't work. Resist any temptation.
  674. window_set_vsync_mode(wd.vsync_mode, p_window_id);
  675. }
  676. if (rendering_device) {
  677. rendering_device->screen_create(wd.id);
  678. }
  679. #endif
  680. #ifdef GLES3_ENABLED
  681. if (egl_manager) {
  682. struct wl_surface *wl_surface = wayland_thread.window_get_wl_surface(wd.id);
  683. wd.wl_egl_window = wl_egl_window_create(wl_surface, wd.rect.size.width, wd.rect.size.height);
  684. Error err = egl_manager->window_create(p_window_id, wayland_thread.get_wl_display(), wd.wl_egl_window, wd.rect.size.width, wd.rect.size.height);
  685. ERR_FAIL_COND_MSG(err == ERR_CANT_CREATE, "Can't show a GLES3 window.");
  686. window_set_vsync_mode(wd.vsync_mode, p_window_id);
  687. }
  688. #endif
  689. // NOTE: Some public window-handling methods might depend on this flag being
  690. // set. Make sure the method you're calling does not depend on it before this
  691. // assignment.
  692. wd.visible = true;
  693. // Actually try to apply the window's mode now that it's visible.
  694. window_set_mode(setup_mode, wd.id);
  695. wayland_thread.window_set_title(p_window_id, wd.title);
  696. }
  697. }
  698. void DisplayServerWayland::delete_sub_window(WindowID p_window_id) {
  699. MutexLock mutex_lock(wayland_thread.mutex);
  700. ERR_FAIL_COND(!windows.has(p_window_id));
  701. WindowData &wd = windows[p_window_id];
  702. ERR_FAIL_COND(!windows.has(wd.root_id));
  703. WindowData &root_wd = windows[wd.root_id];
  704. // NOTE: By the time the Wayland thread will send a `WINDOW_EVENT_MOUSE_EXIT`
  705. // the window will be gone and the message will be discarded, confusing the
  706. // engine. We thus have to send it ourselves.
  707. if (wayland_thread.pointer_get_pointed_window_id() == p_window_id) {
  708. _send_window_event(WINDOW_EVENT_MOUSE_EXIT, p_window_id);
  709. }
  710. // The XDG shell specification requires us to clear all popups in reverse order.
  711. while (!root_wd.popup_stack.is_empty() && root_wd.popup_stack.back()->get() != p_window_id) {
  712. _send_window_event(WINDOW_EVENT_FORCE_CLOSE, root_wd.popup_stack.back()->get());
  713. }
  714. if (root_wd.popup_stack.back() && root_wd.popup_stack.back()->get() == p_window_id) {
  715. root_wd.popup_stack.pop_back();
  716. }
  717. if (popup_menu_list.back() && popup_menu_list.back()->get() == p_window_id) {
  718. popup_menu_list.pop_back();
  719. }
  720. #ifdef ACCESSKIT_ENABLED
  721. if (accessibility_driver) {
  722. accessibility_driver->window_destroy(p_window_id);
  723. }
  724. #endif
  725. if (wd.visible) {
  726. #ifdef VULKAN_ENABLED
  727. if (rendering_device) {
  728. rendering_device->screen_free(p_window_id);
  729. }
  730. if (rendering_context) {
  731. rendering_context->window_destroy(p_window_id);
  732. }
  733. #endif
  734. #ifdef GLES3_ENABLED
  735. if (egl_manager) {
  736. egl_manager->window_destroy(p_window_id);
  737. }
  738. #endif
  739. wayland_thread.window_destroy(p_window_id);
  740. }
  741. windows.erase(p_window_id);
  742. DEBUG_LOG_WAYLAND(vformat("Destroyed window %d", p_window_id));
  743. }
  744. DisplayServer::WindowID DisplayServerWayland::window_get_active_popup() const {
  745. MutexLock mutex_lock(wayland_thread.mutex);
  746. if (!popup_menu_list.is_empty()) {
  747. return popup_menu_list.back()->get();
  748. }
  749. return INVALID_WINDOW_ID;
  750. }
  751. void DisplayServerWayland::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) {
  752. MutexLock mutex_lock(wayland_thread.mutex);
  753. ERR_FAIL_COND(!windows.has(p_window));
  754. windows[p_window].safe_rect = p_rect;
  755. }
  756. Rect2i DisplayServerWayland::window_get_popup_safe_rect(WindowID p_window) const {
  757. MutexLock mutex_lock(wayland_thread.mutex);
  758. ERR_FAIL_COND_V(!windows.has(p_window), Rect2i());
  759. return windows[p_window].safe_rect;
  760. }
  761. int64_t DisplayServerWayland::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  762. MutexLock mutex_lock(wayland_thread.mutex);
  763. switch (p_handle_type) {
  764. case DISPLAY_HANDLE: {
  765. return (int64_t)wayland_thread.get_wl_display();
  766. } break;
  767. case WINDOW_HANDLE: {
  768. return (int64_t)wayland_thread.window_get_wl_surface(p_window);
  769. } break;
  770. case WINDOW_VIEW: {
  771. return 0; // Not supported.
  772. } break;
  773. #ifdef GLES3_ENABLED
  774. case OPENGL_CONTEXT: {
  775. if (egl_manager) {
  776. return (int64_t)egl_manager->get_context(p_window);
  777. }
  778. return 0;
  779. } break;
  780. case EGL_DISPLAY: {
  781. if (egl_manager) {
  782. return (int64_t)egl_manager->get_display(p_window);
  783. }
  784. return 0;
  785. }
  786. case EGL_CONFIG: {
  787. if (egl_manager) {
  788. return (int64_t)egl_manager->get_config(p_window);
  789. }
  790. return 0;
  791. }
  792. #endif // GLES3_ENABLED
  793. default: {
  794. return 0;
  795. } break;
  796. }
  797. }
  798. DisplayServer::WindowID DisplayServerWayland::get_window_at_screen_position(const Point2i &p_position) const {
  799. // Standard Wayland APIs don't support this.
  800. return MAIN_WINDOW_ID;
  801. }
  802. void DisplayServerWayland::window_attach_instance_id(ObjectID p_instance, WindowID p_window_id) {
  803. MutexLock mutex_lock(wayland_thread.mutex);
  804. ERR_FAIL_COND(!windows.has(p_window_id));
  805. windows[p_window_id].instance_id = p_instance;
  806. }
  807. ObjectID DisplayServerWayland::window_get_attached_instance_id(WindowID p_window_id) const {
  808. MutexLock mutex_lock(wayland_thread.mutex);
  809. ERR_FAIL_COND_V(!windows.has(p_window_id), ObjectID());
  810. return windows[p_window_id].instance_id;
  811. }
  812. void DisplayServerWayland::window_set_title(const String &p_title, DisplayServer::WindowID p_window_id) {
  813. MutexLock mutex_lock(wayland_thread.mutex);
  814. ERR_FAIL_COND(!windows.has(p_window_id));
  815. WindowData &wd = windows[p_window_id];
  816. wd.title = p_title;
  817. if (wd.visible) {
  818. wayland_thread.window_set_title(p_window_id, wd.title);
  819. }
  820. }
  821. void DisplayServerWayland::window_set_mouse_passthrough(const Vector<Vector2> &p_region, DisplayServer::WindowID p_window_id) {
  822. // TODO
  823. DEBUG_LOG_WAYLAND(vformat("wayland stub window_set_mouse_passthrough region %s", p_region));
  824. }
  825. void DisplayServerWayland::window_set_rect_changed_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  826. MutexLock mutex_lock(wayland_thread.mutex);
  827. ERR_FAIL_COND(!windows.has(p_window_id));
  828. windows[p_window_id].rect_changed_callback = p_callable;
  829. }
  830. void DisplayServerWayland::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  831. MutexLock mutex_lock(wayland_thread.mutex);
  832. ERR_FAIL_COND(!windows.has(p_window_id));
  833. windows[p_window_id].window_event_callback = p_callable;
  834. }
  835. void DisplayServerWayland::window_set_input_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  836. MutexLock mutex_lock(wayland_thread.mutex);
  837. ERR_FAIL_COND(!windows.has(p_window_id));
  838. windows[p_window_id].input_event_callback = p_callable;
  839. }
  840. void DisplayServerWayland::window_set_input_text_callback(const Callable &p_callable, WindowID p_window_id) {
  841. MutexLock mutex_lock(wayland_thread.mutex);
  842. ERR_FAIL_COND(!windows.has(p_window_id));
  843. windows[p_window_id].input_text_callback = p_callable;
  844. }
  845. void DisplayServerWayland::window_set_drop_files_callback(const Callable &p_callable, DisplayServer::WindowID p_window_id) {
  846. MutexLock mutex_lock(wayland_thread.mutex);
  847. ERR_FAIL_COND(!windows.has(p_window_id));
  848. windows[p_window_id].drop_files_callback = p_callable;
  849. }
  850. int DisplayServerWayland::window_get_current_screen(DisplayServer::WindowID p_window_id) const {
  851. ERR_FAIL_COND_V(!windows.has(p_window_id), INVALID_SCREEN);
  852. // Standard Wayland APIs don't support getting the screen of a window.
  853. return 0;
  854. }
  855. void DisplayServerWayland::window_set_current_screen(int p_screen, DisplayServer::WindowID p_window_id) {
  856. // Standard Wayland APIs don't support setting the screen of a window.
  857. }
  858. Point2i DisplayServerWayland::window_get_position(DisplayServer::WindowID p_window_id) const {
  859. MutexLock mutex_lock(wayland_thread.mutex);
  860. return windows[p_window_id].rect.position;
  861. }
  862. Point2i DisplayServerWayland::window_get_position_with_decorations(DisplayServer::WindowID p_window_id) const {
  863. MutexLock mutex_lock(wayland_thread.mutex);
  864. return windows[p_window_id].rect.position;
  865. }
  866. void DisplayServerWayland::window_set_position(const Point2i &p_position, DisplayServer::WindowID p_window_id) {
  867. // Unsupported with toplevels.
  868. }
  869. void DisplayServerWayland::window_set_max_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  870. MutexLock mutex_lock(wayland_thread.mutex);
  871. DEBUG_LOG_WAYLAND(vformat("window max size set to %s", p_size));
  872. if (p_size.x < 0 || p_size.y < 0) {
  873. ERR_FAIL_MSG("Maximum window size can't be negative!");
  874. }
  875. ERR_FAIL_COND(!windows.has(p_window_id));
  876. WindowData &wd = windows[p_window_id];
  877. // FIXME: Is `p_size.x < wd.min_size.x || p_size.y < wd.min_size.y` == `p_size < wd.min_size`?
  878. if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
  879. ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
  880. return;
  881. }
  882. wd.max_size = p_size;
  883. if (wd.visible) {
  884. wayland_thread.window_set_max_size(p_window_id, p_size);
  885. }
  886. }
  887. Size2i DisplayServerWayland::window_get_max_size(DisplayServer::WindowID p_window_id) const {
  888. MutexLock mutex_lock(wayland_thread.mutex);
  889. ERR_FAIL_COND_V(!windows.has(p_window_id), Size2i());
  890. return windows[p_window_id].max_size;
  891. }
  892. void DisplayServerWayland::gl_window_make_current(DisplayServer::WindowID p_window_id) {
  893. #ifdef GLES3_ENABLED
  894. if (egl_manager) {
  895. egl_manager->window_make_current(p_window_id);
  896. }
  897. #endif
  898. }
  899. void DisplayServerWayland::window_set_transient(WindowID p_window_id, WindowID p_parent) {
  900. MutexLock mutex_lock(wayland_thread.mutex);
  901. ERR_FAIL_COND(!windows.has(p_window_id));
  902. WindowData &wd = windows[p_window_id];
  903. ERR_FAIL_COND(wd.parent_id == p_parent);
  904. if (p_parent != INVALID_WINDOW_ID) {
  905. ERR_FAIL_COND(!windows.has(p_parent));
  906. ERR_FAIL_COND_MSG(wd.parent_id != INVALID_WINDOW_ID, "Window already has a transient parent");
  907. wd.parent_id = p_parent;
  908. // NOTE: Looks like live unparenting is not really practical unfortunately.
  909. // See WaylandThread::window_set_parent for more info.
  910. if (wd.visible) {
  911. wayland_thread.window_set_parent(p_window_id, p_parent);
  912. }
  913. }
  914. }
  915. void DisplayServerWayland::window_set_min_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  916. MutexLock mutex_lock(wayland_thread.mutex);
  917. DEBUG_LOG_WAYLAND(vformat("window minsize set to %s", p_size));
  918. ERR_FAIL_COND(!windows.has(p_window_id));
  919. WindowData &wd = windows[p_window_id];
  920. if (p_size.x < 0 || p_size.y < 0) {
  921. ERR_FAIL_MSG("Minimum window size can't be negative!");
  922. }
  923. // FIXME: Is `p_size.x > wd.max_size.x || p_size.y > wd.max_size.y` == `p_size > wd.max_size`?
  924. if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
  925. ERR_PRINT("Minimum window size can't be larger than maximum window size!");
  926. return;
  927. }
  928. wd.min_size = p_size;
  929. if (wd.visible) {
  930. wayland_thread.window_set_min_size(p_window_id, p_size);
  931. }
  932. }
  933. Size2i DisplayServerWayland::window_get_min_size(DisplayServer::WindowID p_window_id) const {
  934. MutexLock mutex_lock(wayland_thread.mutex);
  935. ERR_FAIL_COND_V(!windows.has(p_window_id), Size2i());
  936. return windows[p_window_id].min_size;
  937. }
  938. void DisplayServerWayland::window_set_size(const Size2i p_size, DisplayServer::WindowID p_window_id) {
  939. MutexLock mutex_lock(wayland_thread.mutex);
  940. ERR_FAIL_COND(!windows.has(p_window_id));
  941. WindowData &wd = windows[p_window_id];
  942. // The XDG spec doesn't allow non-interactive resizes. Let's update the
  943. // window's internal representation to account for that.
  944. if (wd.rect_changed_callback.is_valid()) {
  945. wd.rect_changed_callback.call(wd.rect);
  946. }
  947. }
  948. Size2i DisplayServerWayland::window_get_size(DisplayServer::WindowID p_window_id) const {
  949. MutexLock mutex_lock(wayland_thread.mutex);
  950. ERR_FAIL_COND_V(!windows.has(p_window_id), Size2i());
  951. return windows[p_window_id].rect.size;
  952. }
  953. Size2i DisplayServerWayland::window_get_size_with_decorations(DisplayServer::WindowID p_window_id) const {
  954. MutexLock mutex_lock(wayland_thread.mutex);
  955. // I don't think there's a way of actually knowing the size of the window
  956. // decoration in Wayland, at least in the case of SSDs, nor that it would be
  957. // that useful in this case. We'll just return the main window's size.
  958. ERR_FAIL_COND_V(!windows.has(p_window_id), Size2i());
  959. return windows[p_window_id].rect.size;
  960. }
  961. void DisplayServerWayland::window_set_mode(WindowMode p_mode, DisplayServer::WindowID p_window_id) {
  962. MutexLock mutex_lock(wayland_thread.mutex);
  963. ERR_FAIL_COND(!windows.has(p_window_id));
  964. WindowData &wd = windows[p_window_id];
  965. if (!wd.visible) {
  966. return;
  967. }
  968. wayland_thread.window_try_set_mode(p_window_id, p_mode);
  969. }
  970. DisplayServer::WindowMode DisplayServerWayland::window_get_mode(DisplayServer::WindowID p_window_id) const {
  971. MutexLock mutex_lock(wayland_thread.mutex);
  972. ERR_FAIL_COND_V(!windows.has(p_window_id), WINDOW_MODE_WINDOWED);
  973. const WindowData &wd = windows[p_window_id];
  974. if (!wd.visible) {
  975. return WINDOW_MODE_WINDOWED;
  976. }
  977. return wayland_thread.window_get_mode(p_window_id);
  978. }
  979. bool DisplayServerWayland::window_is_maximize_allowed(DisplayServer::WindowID p_window_id) const {
  980. MutexLock mutex_lock(wayland_thread.mutex);
  981. return wayland_thread.window_can_set_mode(p_window_id, WINDOW_MODE_MAXIMIZED);
  982. }
  983. void DisplayServerWayland::window_set_flag(WindowFlags p_flag, bool p_enabled, DisplayServer::WindowID p_window_id) {
  984. MutexLock mutex_lock(wayland_thread.mutex);
  985. ERR_FAIL_COND(!windows.has(p_window_id));
  986. WindowData &wd = windows[p_window_id];
  987. DEBUG_LOG_WAYLAND(vformat("Window set flag %d", p_flag));
  988. switch (p_flag) {
  989. case WINDOW_FLAG_BORDERLESS: {
  990. wayland_thread.window_set_borderless(p_window_id, p_enabled);
  991. } break;
  992. case WINDOW_FLAG_POPUP: {
  993. ERR_FAIL_COND_MSG(p_window_id == MAIN_WINDOW_ID, "Main window can't be popup.");
  994. ERR_FAIL_COND_MSG(wd.visible && (wd.flags & WINDOW_FLAG_POPUP_BIT) != p_enabled, "Popup flag can't changed while window is opened.");
  995. } break;
  996. case WINDOW_FLAG_POPUP_WM_HINT: {
  997. ERR_FAIL_COND_MSG(p_window_id == MAIN_WINDOW_ID, "Main window can't have popup hint.");
  998. ERR_FAIL_COND_MSG(wd.visible && (wd.flags & WINDOW_FLAG_POPUP_WM_HINT_BIT) != p_enabled, "Popup hint can't changed while window is opened.");
  999. } break;
  1000. default: {
  1001. }
  1002. }
  1003. if (p_enabled) {
  1004. wd.flags |= 1 << p_flag;
  1005. } else {
  1006. wd.flags &= ~(1 << p_flag);
  1007. }
  1008. }
  1009. bool DisplayServerWayland::window_get_flag(WindowFlags p_flag, DisplayServer::WindowID p_window_id) const {
  1010. MutexLock mutex_lock(wayland_thread.mutex);
  1011. ERR_FAIL_COND_V(!windows.has(p_window_id), false);
  1012. return windows[p_window_id].flags & (1 << p_flag);
  1013. }
  1014. void DisplayServerWayland::window_request_attention(DisplayServer::WindowID p_window_id) {
  1015. MutexLock mutex_lock(wayland_thread.mutex);
  1016. DEBUG_LOG_WAYLAND("Requested attention.");
  1017. wayland_thread.window_request_attention(p_window_id);
  1018. }
  1019. void DisplayServerWayland::window_move_to_foreground(DisplayServer::WindowID p_window_id) {
  1020. // Standard Wayland APIs don't support this.
  1021. }
  1022. bool DisplayServerWayland::window_is_focused(WindowID p_window_id) const {
  1023. MutexLock mutex_lock(wayland_thread.mutex);
  1024. return wayland_thread.pointer_get_pointed_window_id() == p_window_id;
  1025. }
  1026. bool DisplayServerWayland::window_can_draw(DisplayServer::WindowID p_window_id) const {
  1027. MutexLock mutex_lock(wayland_thread.mutex);
  1028. uint64_t last_frame_time = wayland_thread.window_get_last_frame_time(p_window_id);
  1029. uint64_t time_since_frame = OS::get_singleton()->get_ticks_usec() - last_frame_time;
  1030. if (time_since_frame > WAYLAND_MAX_FRAME_TIME_US) {
  1031. return false;
  1032. }
  1033. if (wayland_thread.window_is_suspended(p_window_id)) {
  1034. return false;
  1035. }
  1036. return suspend_state == SuspendState::NONE;
  1037. }
  1038. bool DisplayServerWayland::can_any_window_draw() const {
  1039. return suspend_state == SuspendState::NONE;
  1040. }
  1041. void DisplayServerWayland::window_set_ime_active(const bool p_active, DisplayServer::WindowID p_window_id) {
  1042. MutexLock mutex_lock(wayland_thread.mutex);
  1043. wayland_thread.window_set_ime_active(p_active, p_window_id);
  1044. }
  1045. void DisplayServerWayland::window_set_ime_position(const Point2i &p_pos, DisplayServer::WindowID p_window_id) {
  1046. MutexLock mutex_lock(wayland_thread.mutex);
  1047. wayland_thread.window_set_ime_position(p_pos, p_window_id);
  1048. }
  1049. int DisplayServerWayland::accessibility_should_increase_contrast() const {
  1050. #ifdef DBUS_ENABLED
  1051. if (!portal_desktop) {
  1052. return -1;
  1053. }
  1054. return portal_desktop->get_high_contrast();
  1055. #endif
  1056. return -1;
  1057. }
  1058. int DisplayServerWayland::accessibility_screen_reader_active() const {
  1059. #ifdef DBUS_ENABLED
  1060. if (atspi_monitor && atspi_monitor->is_supported()) {
  1061. return atspi_monitor->is_active();
  1062. }
  1063. #endif
  1064. return -1;
  1065. }
  1066. Point2i DisplayServerWayland::ime_get_selection() const {
  1067. return ime_selection;
  1068. }
  1069. String DisplayServerWayland::ime_get_text() const {
  1070. return ime_text;
  1071. }
  1072. // NOTE: While Wayland is supposed to be tear-free, wayland-protocols version
  1073. // 1.30 added a protocol for allowing async flips which is supposed to be
  1074. // handled by drivers such as Vulkan. We can then just ask to disable v-sync and
  1075. // hope for the best. See: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/commit/6394f0b4f3be151076f10a845a2fb131eeb56706
  1076. void DisplayServerWayland::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, DisplayServer::WindowID p_window_id) {
  1077. MutexLock mutex_lock(wayland_thread.mutex);
  1078. WindowData &wd = windows[p_window_id];
  1079. #ifdef RD_ENABLED
  1080. if (rendering_context) {
  1081. rendering_context->window_set_vsync_mode(p_window_id, p_vsync_mode);
  1082. wd.emulate_vsync = (!wayland_thread.is_fifo_available() && rendering_context->window_get_vsync_mode(p_window_id) == DisplayServer::VSYNC_ENABLED);
  1083. if (wd.emulate_vsync) {
  1084. print_verbose("VSYNC: manually throttling frames using MAILBOX.");
  1085. rendering_context->window_set_vsync_mode(p_window_id, DisplayServer::VSYNC_MAILBOX);
  1086. }
  1087. }
  1088. #endif // VULKAN_ENABLED
  1089. #ifdef GLES3_ENABLED
  1090. if (egl_manager) {
  1091. egl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
  1092. // NOTE: Mesa's EGL implementation does not seem to make use of fifo_v1 so
  1093. // we'll have to always emulate V-Sync.
  1094. wd.emulate_vsync = egl_manager->is_using_vsync();
  1095. if (wd.emulate_vsync) {
  1096. print_verbose("VSYNC: manually throttling frames with swap delay 0.");
  1097. egl_manager->set_use_vsync(false);
  1098. }
  1099. }
  1100. #endif // GLES3_ENABLED
  1101. }
  1102. DisplayServer::VSyncMode DisplayServerWayland::window_get_vsync_mode(DisplayServer::WindowID p_window_id) const {
  1103. const WindowData &wd = windows[p_window_id];
  1104. if (wd.emulate_vsync) {
  1105. return DisplayServer::VSYNC_ENABLED;
  1106. }
  1107. #ifdef VULKAN_ENABLED
  1108. if (rendering_context) {
  1109. return rendering_context->window_get_vsync_mode(p_window_id);
  1110. }
  1111. #endif // VULKAN_ENABLED
  1112. #ifdef GLES3_ENABLED
  1113. if (egl_manager) {
  1114. return egl_manager->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED;
  1115. }
  1116. #endif // GLES3_ENABLED
  1117. return DisplayServer::VSYNC_ENABLED;
  1118. }
  1119. void DisplayServerWayland::window_start_drag(WindowID p_window) {
  1120. MutexLock mutex_lock(wayland_thread.mutex);
  1121. wayland_thread.window_start_drag(p_window);
  1122. }
  1123. void DisplayServerWayland::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) {
  1124. MutexLock mutex_lock(wayland_thread.mutex);
  1125. ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX);
  1126. wayland_thread.window_start_resize(p_edge, p_window);
  1127. }
  1128. void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
  1129. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  1130. MutexLock mutex_lock(wayland_thread.mutex);
  1131. if (p_shape == cursor_shape) {
  1132. return;
  1133. }
  1134. cursor_shape = p_shape;
  1135. if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
  1136. // Hidden.
  1137. return;
  1138. }
  1139. wayland_thread.cursor_set_shape(p_shape);
  1140. }
  1141. DisplayServerWayland::CursorShape DisplayServerWayland::cursor_get_shape() const {
  1142. MutexLock mutex_lock(wayland_thread.mutex);
  1143. return cursor_shape;
  1144. }
  1145. void DisplayServerWayland::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  1146. MutexLock mutex_lock(wayland_thread.mutex);
  1147. if (p_cursor.is_valid()) {
  1148. HashMap<CursorShape, CustomCursor>::Iterator cursor_c = custom_cursors.find(p_shape);
  1149. if (cursor_c) {
  1150. if (cursor_c->value.resource == p_cursor && cursor_c->value.hotspot == p_hotspot) {
  1151. // We have a cached cursor. Nice.
  1152. wayland_thread.cursor_set_shape(p_shape);
  1153. return;
  1154. }
  1155. // We're changing this cursor; we'll have to rebuild it.
  1156. custom_cursors.erase(p_shape);
  1157. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  1158. }
  1159. Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot);
  1160. ERR_FAIL_COND(image.is_null());
  1161. CustomCursor &cursor = custom_cursors[p_shape];
  1162. cursor.resource = p_cursor;
  1163. cursor.hotspot = p_hotspot;
  1164. wayland_thread.cursor_shape_set_custom_image(p_shape, image, p_hotspot);
  1165. wayland_thread.cursor_set_shape(p_shape);
  1166. } else {
  1167. // Clear cache and reset to default system cursor.
  1168. wayland_thread.cursor_shape_clear_custom_image(p_shape);
  1169. if (cursor_shape == p_shape) {
  1170. wayland_thread.cursor_set_shape(p_shape);
  1171. }
  1172. if (custom_cursors.has(p_shape)) {
  1173. custom_cursors.erase(p_shape);
  1174. }
  1175. }
  1176. }
  1177. bool DisplayServerWayland::get_swap_cancel_ok() {
  1178. return swap_cancel_ok;
  1179. }
  1180. Error DisplayServerWayland::embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) {
  1181. MutexLock mutex_lock(wayland_thread.mutex);
  1182. struct godot_embedding_compositor *ec = wayland_thread.get_embedding_compositor();
  1183. ERR_FAIL_NULL_V_MSG(ec, ERR_BUG, "Missing embedded compositor interface");
  1184. struct WaylandThread::EmbeddingCompositorState *ecs = WaylandThread::godot_embedding_compositor_get_state(ec);
  1185. ERR_FAIL_NULL_V(ecs, ERR_BUG);
  1186. if (!ecs->mapped_clients.has(p_pid)) {
  1187. return ERR_DOES_NOT_EXIST;
  1188. }
  1189. struct godot_embedded_client *embedded_client = ecs->mapped_clients[p_pid];
  1190. WaylandThread::EmbeddedClientState *client_data = (WaylandThread::EmbeddedClientState *)godot_embedded_client_get_user_data(embedded_client);
  1191. ERR_FAIL_NULL_V(client_data, ERR_BUG);
  1192. if (p_grab_focus) {
  1193. godot_embedded_client_focus_window(embedded_client);
  1194. }
  1195. if (p_visible) {
  1196. WaylandThread::WindowState *ws = wayland_thread.window_get_state(p_window);
  1197. ERR_FAIL_NULL_V(ws, ERR_BUG);
  1198. struct xdg_toplevel *toplevel = ws->xdg_toplevel;
  1199. #ifdef LIBDECOR_ENABLED
  1200. if (toplevel == nullptr && ws->libdecor_frame) {
  1201. toplevel = libdecor_frame_get_xdg_toplevel(ws->libdecor_frame);
  1202. }
  1203. #endif
  1204. ERR_FAIL_NULL_V(toplevel, ERR_CANT_CREATE);
  1205. godot_embedded_client_set_embedded_window_parent(embedded_client, toplevel);
  1206. double window_scale = WaylandThread::window_state_get_scale_factor(ws);
  1207. Rect2i scaled_rect = p_rect;
  1208. scaled_rect.position = WaylandThread::scale_vector2i(scaled_rect.position, 1 / window_scale);
  1209. scaled_rect.size = WaylandThread::scale_vector2i(scaled_rect.size, 1 / window_scale);
  1210. print_verbose(vformat("Scaling embedded rect down by %f from %s to %s.", window_scale, p_rect, scaled_rect));
  1211. godot_embedded_client_set_embedded_window_rect(embedded_client, scaled_rect.position.x, scaled_rect.position.y, scaled_rect.size.width, scaled_rect.size.height);
  1212. } else {
  1213. godot_embedded_client_set_embedded_window_parent(embedded_client, nullptr);
  1214. }
  1215. return OK;
  1216. }
  1217. Error DisplayServerWayland::request_close_embedded_process(OS::ProcessID p_pid) {
  1218. MutexLock mutex_lock(wayland_thread.mutex);
  1219. struct godot_embedding_compositor *ec = wayland_thread.get_embedding_compositor();
  1220. ERR_FAIL_NULL_V_MSG(ec, ERR_BUG, "Missing embedded compositor interface");
  1221. struct WaylandThread::EmbeddingCompositorState *ecs = WaylandThread::godot_embedding_compositor_get_state(ec);
  1222. ERR_FAIL_NULL_V(ecs, ERR_BUG);
  1223. if (!ecs->mapped_clients.has(p_pid)) {
  1224. return ERR_DOES_NOT_EXIST;
  1225. }
  1226. struct godot_embedded_client *embedded_client = ecs->mapped_clients[p_pid];
  1227. WaylandThread::EmbeddedClientState *client_data = (WaylandThread::EmbeddedClientState *)godot_embedded_client_get_user_data(embedded_client);
  1228. ERR_FAIL_NULL_V(client_data, ERR_BUG);
  1229. godot_embedded_client_embedded_window_request_close(embedded_client);
  1230. return OK;
  1231. }
  1232. Error DisplayServerWayland::remove_embedded_process(OS::ProcessID p_pid) {
  1233. return request_close_embedded_process(p_pid);
  1234. }
  1235. OS::ProcessID DisplayServerWayland::get_focused_process_id() {
  1236. MutexLock mutex_lock(wayland_thread.mutex);
  1237. OS::ProcessID embedded_pid = wayland_thread.embedded_compositor_get_focused_pid();
  1238. if (embedded_pid < 0) {
  1239. return OS::get_singleton()->get_process_id();
  1240. }
  1241. return embedded_pid;
  1242. }
  1243. int DisplayServerWayland::keyboard_get_layout_count() const {
  1244. MutexLock mutex_lock(wayland_thread.mutex);
  1245. return wayland_thread.keyboard_get_layout_count();
  1246. }
  1247. int DisplayServerWayland::keyboard_get_current_layout() const {
  1248. MutexLock mutex_lock(wayland_thread.mutex);
  1249. return wayland_thread.keyboard_get_current_layout_index();
  1250. }
  1251. void DisplayServerWayland::keyboard_set_current_layout(int p_index) {
  1252. MutexLock mutex_lock(wayland_thread.mutex);
  1253. wayland_thread.keyboard_set_current_layout_index(p_index);
  1254. }
  1255. String DisplayServerWayland::keyboard_get_layout_language(int p_index) const {
  1256. MutexLock mutex_lock(wayland_thread.mutex);
  1257. // xkbcommon exposes only the layout's name, which looks like it overlaps with
  1258. // its language.
  1259. return wayland_thread.keyboard_get_layout_name(p_index);
  1260. }
  1261. String DisplayServerWayland::keyboard_get_layout_name(int p_index) const {
  1262. MutexLock mutex_lock(wayland_thread.mutex);
  1263. return wayland_thread.keyboard_get_layout_name(p_index);
  1264. }
  1265. Key DisplayServerWayland::keyboard_get_keycode_from_physical(Key p_keycode) const {
  1266. MutexLock mutex_lock(wayland_thread.mutex);
  1267. Key key = wayland_thread.keyboard_get_key_from_physical(p_keycode);
  1268. // If not found, fallback to QWERTY.
  1269. // This should match the behavior of the event pump.
  1270. if (key == Key::NONE) {
  1271. return p_keycode;
  1272. }
  1273. if (key >= Key::A + 32 && key <= Key::Z + 32) {
  1274. key -= 'a' - 'A';
  1275. }
  1276. // Make it consistent with the keys returned by `Input`.
  1277. if (key == Key::BACKTAB) {
  1278. key = Key::TAB;
  1279. }
  1280. return key;
  1281. }
  1282. bool DisplayServerWayland::color_picker(const Callable &p_callback) {
  1283. #ifdef DBUS_ENABLED
  1284. if (!portal_desktop) {
  1285. return false;
  1286. }
  1287. MutexLock mutex_lock(wayland_thread.mutex);
  1288. WindowID window_id = MAIN_WINDOW_ID;
  1289. // TODO: Use window IDs for multiwindow support.
  1290. WaylandThread::WindowState *ws = wayland_thread.wl_surface_get_window_state(wayland_thread.window_get_wl_surface(window_id));
  1291. return portal_desktop->color_picker((ws ? ws->exported_handle : String()), p_callback);
  1292. #else
  1293. return false;
  1294. #endif
  1295. }
  1296. void DisplayServerWayland::try_suspend() {
  1297. // Due to various reasons, we manually handle display synchronization by
  1298. // waiting for a frame event (request to draw) or, if available, the actual
  1299. // window's suspend status. When a window is suspended, we can avoid drawing
  1300. // altogether, either because the compositor told us that we don't need to or
  1301. // because the pace of the frame events became unreliable.
  1302. bool frame = wayland_thread.wait_frame_suspend_ms(WAYLAND_MAX_FRAME_TIME_US / 1000);
  1303. if (!frame) {
  1304. suspend_state = SuspendState::TIMEOUT;
  1305. }
  1306. }
  1307. void DisplayServerWayland::process_events() {
  1308. wayland_thread.mutex.lock();
  1309. wayland_thread.keyboard_echo_keys();
  1310. while (wayland_thread.has_message()) {
  1311. Ref<WaylandThread::Message> msg = wayland_thread.pop_message();
  1312. // Generic check. Not actual message handling.
  1313. Ref<WaylandThread::WindowMessage> win_msg = msg;
  1314. if (win_msg.is_valid()) {
  1315. ERR_CONTINUE_MSG(win_msg->id == INVALID_WINDOW_ID, "Invalid window ID received from Wayland thread.");
  1316. if (!windows.has(win_msg->id)) {
  1317. // Window got probably deleted.
  1318. continue;
  1319. }
  1320. }
  1321. Ref<WaylandThread::WindowRectMessage> winrect_msg = msg;
  1322. if (winrect_msg.is_valid()) {
  1323. _update_window_rect(winrect_msg->rect, winrect_msg->id);
  1324. continue;
  1325. }
  1326. Ref<WaylandThread::WindowEventMessage> winev_msg = msg;
  1327. if (winev_msg.is_valid() && windows.has(winev_msg->id)) {
  1328. _send_window_event(winev_msg->event, winev_msg->id);
  1329. if (winev_msg->event == WINDOW_EVENT_FOCUS_IN) {
  1330. #ifdef ACCESSKIT_ENABLED
  1331. if (accessibility_driver) {
  1332. accessibility_driver->accessibility_set_window_focused(winev_msg->id, true);
  1333. }
  1334. #endif
  1335. if (OS::get_singleton()->get_main_loop()) {
  1336. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  1337. }
  1338. } else if (winev_msg->event == WINDOW_EVENT_FOCUS_OUT) {
  1339. #ifdef ACCESSKIT_ENABLED
  1340. if (accessibility_driver) {
  1341. accessibility_driver->accessibility_set_window_focused(winev_msg->id, false);
  1342. }
  1343. #endif
  1344. if (OS::get_singleton()->get_main_loop()) {
  1345. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  1346. }
  1347. Input::get_singleton()->release_pressed_events();
  1348. }
  1349. continue;
  1350. }
  1351. Ref<WaylandThread::InputEventMessage> inputev_msg = msg;
  1352. if (inputev_msg.is_valid()) {
  1353. Ref<InputEventMouseButton> mb = inputev_msg->event;
  1354. bool handled = false;
  1355. if (!popup_menu_list.is_empty() && mb.is_valid()) {
  1356. // Popup menu handling.
  1357. BitField<MouseButtonMask> mouse_mask = mb->get_button_mask();
  1358. if (mouse_mask != last_mouse_monitor_mask && mb->is_pressed()) {
  1359. List<WindowID>::Element *E = popup_menu_list.back();
  1360. List<WindowID>::Element *C = nullptr;
  1361. // Looking for the oldest popup to close.
  1362. while (E) {
  1363. WindowData &wd = windows[E->get()];
  1364. Point2 global_pos = mb->get_position() + window_get_position(mb->get_window_id());
  1365. if (wd.rect.has_point(global_pos)) {
  1366. break;
  1367. } else if (wd.safe_rect.has_point(global_pos)) {
  1368. break;
  1369. }
  1370. C = E;
  1371. E = E->prev();
  1372. }
  1373. if (C) {
  1374. handled = true;
  1375. _send_window_event(WINDOW_EVENT_CLOSE_REQUEST, C->get());
  1376. }
  1377. }
  1378. last_mouse_monitor_mask = mouse_mask;
  1379. }
  1380. if (!handled) {
  1381. Input::get_singleton()->parse_input_event(inputev_msg->event);
  1382. }
  1383. continue;
  1384. }
  1385. Ref<WaylandThread::DropFilesEventMessage> dropfiles_msg = msg;
  1386. if (dropfiles_msg.is_valid()) {
  1387. WindowData wd = windows[dropfiles_msg->id];
  1388. if (wd.drop_files_callback.is_valid()) {
  1389. Variant v_files = dropfiles_msg->files;
  1390. const Variant *v_args[1] = { &v_files };
  1391. Variant ret;
  1392. Callable::CallError ce;
  1393. wd.drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce);
  1394. if (ce.error != Callable::CallError::CALL_OK) {
  1395. ERR_PRINT(vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(wd.drop_files_callback, v_args, 1, ce)));
  1396. }
  1397. }
  1398. continue;
  1399. }
  1400. Ref<WaylandThread::IMECommitEventMessage> ime_commit_msg = msg;
  1401. if (ime_commit_msg.is_valid()) {
  1402. for (int i = 0; i < ime_commit_msg->text.length(); i++) {
  1403. const char32_t codepoint = ime_commit_msg->text[i];
  1404. Ref<InputEventKey> ke;
  1405. ke.instantiate();
  1406. ke->set_window_id(ime_commit_msg->id);
  1407. ke->set_pressed(true);
  1408. ke->set_echo(false);
  1409. ke->set_keycode(Key::NONE);
  1410. ke->set_physical_keycode(Key::NONE);
  1411. ke->set_key_label(Key::NONE);
  1412. ke->set_unicode(codepoint);
  1413. Input::get_singleton()->parse_input_event(ke);
  1414. }
  1415. ime_text = String();
  1416. ime_selection = Vector2i();
  1417. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  1418. continue;
  1419. }
  1420. Ref<WaylandThread::IMEUpdateEventMessage> ime_update_msg = msg;
  1421. if (ime_update_msg.is_valid()) {
  1422. if (ime_text != ime_update_msg->text || ime_selection != ime_update_msg->selection) {
  1423. ime_text = ime_update_msg->text;
  1424. ime_selection = ime_update_msg->selection;
  1425. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
  1426. }
  1427. continue;
  1428. }
  1429. }
  1430. switch (suspend_state) {
  1431. case SuspendState::NONE: {
  1432. bool emulate_vsync = false;
  1433. for (KeyValue<DisplayServer::WindowID, WindowData> &pair : windows) {
  1434. if (pair.value.emulate_vsync) {
  1435. emulate_vsync = true;
  1436. break;
  1437. }
  1438. }
  1439. if (emulate_vsync) {
  1440. // Due to the way legacy suspension works, we have to treat low processor
  1441. // usage mode very differently than the regular one.
  1442. if (OS::get_singleton()->is_in_low_processor_usage_mode()) {
  1443. // NOTE: We must avoid committing a surface if we expect a new frame, as we
  1444. // might otherwise commit some inconsistent data (e.g. buffer scale). Note
  1445. // that if a new frame is expected it's going to be committed by the renderer
  1446. // soon anyways.
  1447. if (!RenderingServer::get_singleton()->has_changed()) {
  1448. // We _can't_ commit in a different thread (such as in the frame callback
  1449. // itself) because we would risk to step on the renderer's feet, which would
  1450. // cause subtle but severe issues, such as crashes on setups with explicit
  1451. // sync. This isn't normally a problem, as the renderer commits at every
  1452. // frame (which is what we need for atomic surface updates anyways), but in
  1453. // low processor usage mode that expectation is broken. When it's on, our
  1454. // frame rate stops being constant. This also reflects in the frame
  1455. // information we use for legacy suspension. In order to avoid issues, let's
  1456. // manually commit all surfaces, so that we can get fresh frame data.
  1457. wayland_thread.commit_surfaces();
  1458. try_suspend();
  1459. }
  1460. } else {
  1461. try_suspend();
  1462. }
  1463. }
  1464. if (wayland_thread.is_suspended()) {
  1465. suspend_state = SuspendState::CAPABILITY;
  1466. }
  1467. if (suspend_state == SuspendState::TIMEOUT) {
  1468. DEBUG_LOG_WAYLAND("Suspending. Reason: timeout.");
  1469. } else if (suspend_state == SuspendState::CAPABILITY) {
  1470. DEBUG_LOG_WAYLAND("Suspending. Reason: capability.");
  1471. }
  1472. } break;
  1473. case SuspendState::TIMEOUT: {
  1474. // Certain compositors might not report the "suspended" wm_capability flag.
  1475. // Because of this we'll wake up at the next frame event, indicating the
  1476. // desire for the compositor to let us repaint.
  1477. if (wayland_thread.get_reset_frame()) {
  1478. suspend_state = SuspendState::NONE;
  1479. DEBUG_LOG_WAYLAND("Unsuspending from timeout.");
  1480. }
  1481. // Since we're not rendering, nothing is committing the windows'
  1482. // surfaces. We have to do it ourselves.
  1483. wayland_thread.commit_surfaces();
  1484. } break;
  1485. case SuspendState::CAPABILITY: {
  1486. // If we suspended by capability we can assume that it will be reset when
  1487. // the compositor wants us to repaint.
  1488. if (!wayland_thread.is_suspended()) {
  1489. suspend_state = SuspendState::NONE;
  1490. DEBUG_LOG_WAYLAND("Unsuspending from capability.");
  1491. }
  1492. } break;
  1493. }
  1494. #ifdef DBUS_ENABLED
  1495. if (portal_desktop) {
  1496. portal_desktop->process_callbacks();
  1497. }
  1498. #endif
  1499. wayland_thread.mutex.unlock();
  1500. Input::get_singleton()->flush_buffered_events();
  1501. }
  1502. void DisplayServerWayland::release_rendering_thread() {
  1503. #ifdef GLES3_ENABLED
  1504. if (egl_manager) {
  1505. egl_manager->release_current();
  1506. }
  1507. #endif
  1508. }
  1509. void DisplayServerWayland::swap_buffers() {
  1510. #ifdef GLES3_ENABLED
  1511. if (egl_manager) {
  1512. egl_manager->swap_buffers();
  1513. }
  1514. #endif
  1515. }
  1516. void DisplayServerWayland::set_icon(const Ref<Image> &p_icon) {
  1517. MutexLock mutex_lock(wayland_thread.mutex);
  1518. wayland_thread.set_icon(p_icon);
  1519. }
  1520. void DisplayServerWayland::set_context(Context p_context) {
  1521. MutexLock mutex_lock(wayland_thread.mutex);
  1522. DEBUG_LOG_WAYLAND(vformat("Setting context %d.", p_context));
  1523. context = p_context;
  1524. String app_id = _get_app_id_from_context(p_context);
  1525. wayland_thread.window_set_app_id(MAIN_WINDOW_ID, app_id);
  1526. }
  1527. bool DisplayServerWayland::is_window_transparency_available() const {
  1528. #if defined(RD_ENABLED)
  1529. if (rendering_device && !rendering_device->is_composite_alpha_supported()) {
  1530. return false;
  1531. }
  1532. #endif
  1533. return OS::get_singleton()->is_layered_allowed();
  1534. }
  1535. Vector<String> DisplayServerWayland::get_rendering_drivers_func() {
  1536. Vector<String> drivers;
  1537. #ifdef VULKAN_ENABLED
  1538. drivers.push_back("vulkan");
  1539. #endif
  1540. #ifdef GLES3_ENABLED
  1541. drivers.push_back("opengl3");
  1542. drivers.push_back("opengl3_es");
  1543. #endif
  1544. drivers.push_back("dummy");
  1545. return drivers;
  1546. }
  1547. 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, int64_t p_parent_window, Error &r_error) {
  1548. DisplayServer *ds = memnew(DisplayServerWayland(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, p_context, p_parent_window, r_error));
  1549. if (r_error != OK) {
  1550. ERR_PRINT("Can't create the Wayland display server.");
  1551. memdelete(ds);
  1552. return nullptr;
  1553. }
  1554. return ds;
  1555. }
  1556. 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, int64_t p_parent_window, Error &r_error) {
  1557. #if defined(GLES3_ENABLED) || defined(DBUS_ENABLED)
  1558. #ifdef SOWRAP_ENABLED
  1559. #ifdef DEBUG_ENABLED
  1560. int dylibloader_verbose = 1;
  1561. #else
  1562. int dylibloader_verbose = 0;
  1563. #endif // DEBUG_ENABLED
  1564. #endif // SOWRAP_ENABLED
  1565. #endif // defined(GLES3_ENABLED) || defined(DBUS_ENABLED)
  1566. r_error = ERR_UNAVAILABLE;
  1567. context = p_context;
  1568. String current_desk = OS::get_singleton()->get_environment("XDG_CURRENT_DESKTOP").to_lower();
  1569. String session_desk = OS::get_singleton()->get_environment("XDG_SESSION_DESKTOP").to_lower();
  1570. swap_cancel_ok = (current_desk.contains("kde") || session_desk.contains("kde") || current_desk.contains("lxqt") || session_desk.contains("lxqt"));
  1571. Error thread_err = wayland_thread.init();
  1572. if (thread_err != OK) {
  1573. r_error = thread_err;
  1574. ERR_FAIL_MSG("Could not initialize the Wayland thread.");
  1575. }
  1576. // Input.
  1577. Input::get_singleton()->set_event_dispatch_function(dispatch_input_events);
  1578. native_menu = memnew(NativeMenu);
  1579. #ifdef SPEECHD_ENABLED
  1580. // Init TTS
  1581. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  1582. if (tts_enabled) {
  1583. initialize_tts();
  1584. }
  1585. #endif
  1586. #ifdef ACCESSKIT_ENABLED
  1587. if (accessibility_get_mode() != DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) {
  1588. accessibility_driver = memnew(AccessibilityDriverAccessKit);
  1589. if (accessibility_driver->init() != OK) {
  1590. memdelete(accessibility_driver);
  1591. accessibility_driver = nullptr;
  1592. }
  1593. }
  1594. #endif
  1595. rendering_driver = p_rendering_driver;
  1596. bool driver_found = false;
  1597. String executable_name = OS::get_singleton()->get_executable_path().get_file();
  1598. if (rendering_driver == "dummy") {
  1599. RasterizerDummy::make_current();
  1600. driver_found = true;
  1601. }
  1602. #ifdef RD_ENABLED
  1603. #ifdef VULKAN_ENABLED
  1604. if (rendering_driver == "vulkan") {
  1605. rendering_context = memnew(RenderingContextDriverVulkanWayland);
  1606. }
  1607. #endif // VULKAN_ENABLED
  1608. if (rendering_context) {
  1609. if (rendering_context->initialize() != OK) {
  1610. memdelete(rendering_context);
  1611. rendering_context = nullptr;
  1612. #if defined(GLES3_ENABLED)
  1613. bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3");
  1614. if (fallback_to_opengl3 && rendering_driver != "opengl3") {
  1615. WARN_PRINT("Your video card drivers seem not to support the required Vulkan version, switching to OpenGL 3.");
  1616. rendering_driver = "opengl3";
  1617. OS::get_singleton()->set_current_rendering_method("gl_compatibility");
  1618. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  1619. } else
  1620. #endif // GLES3_ENABLED
  1621. {
  1622. r_error = ERR_CANT_CREATE;
  1623. if (p_rendering_driver == "vulkan") {
  1624. OS::get_singleton()->alert(
  1625. vformat("Your video card drivers seem not to support the required Vulkan version.\n\n"
  1626. "If possible, consider updating your video card drivers or using the OpenGL 3 driver.\n\n"
  1627. "You can enable the OpenGL 3 driver by starting the engine from the\n"
  1628. "command line with the command:\n\n \"%s\" --rendering-driver opengl3\n\n"
  1629. "If you recently updated your video card drivers, try rebooting.",
  1630. executable_name),
  1631. "Unable to initialize Vulkan video driver");
  1632. }
  1633. ERR_FAIL_MSG(vformat("Could not initialize %s", rendering_driver));
  1634. }
  1635. }
  1636. driver_found = true;
  1637. }
  1638. #endif // RD_ENABLED
  1639. #ifdef GLES3_ENABLED
  1640. if (rendering_driver == "opengl3" || rendering_driver == "opengl3_es") {
  1641. #ifdef SOWRAP_ENABLED
  1642. if (initialize_wayland_egl(dylibloader_verbose) != 0) {
  1643. WARN_PRINT("Can't load the Wayland EGL library.");
  1644. return;
  1645. }
  1646. #endif // SOWRAP_ENABLED
  1647. if (getenv("DRI_PRIME") == nullptr) {
  1648. int prime_idx = -1;
  1649. if (getenv("PRIMUS_DISPLAY") ||
  1650. getenv("PRIMUS_libGLd") ||
  1651. getenv("PRIMUS_libGLa") ||
  1652. getenv("PRIMUS_libGL") ||
  1653. getenv("PRIMUS_LOAD_GLOBAL") ||
  1654. getenv("BUMBLEBEE_SOCKET") ||
  1655. getenv("__NV_PRIME_RENDER_OFFLOAD")) {
  1656. print_verbose("Optirun/primusrun detected. Skipping GPU detection");
  1657. prime_idx = 0;
  1658. }
  1659. // Some tools use fake libGL libraries and have them override the real one using
  1660. // LD_LIBRARY_PATH, so we skip them. *But* Steam also sets LD_LIBRARY_PATH for its
  1661. // runtime and includes system `/lib` and `/lib64`... so ignore Steam.
  1662. if (prime_idx == -1 && getenv("LD_LIBRARY_PATH") && !getenv("STEAM_RUNTIME_LIBRARY_PATH")) {
  1663. String ld_library_path(getenv("LD_LIBRARY_PATH"));
  1664. Vector<String> libraries = ld_library_path.split(":");
  1665. for (int i = 0; i < libraries.size(); ++i) {
  1666. if (FileAccess::exists(libraries[i] + "/libGL.so.1") ||
  1667. FileAccess::exists(libraries[i] + "/libGL.so")) {
  1668. print_verbose("Custom libGL override detected. Skipping GPU detection");
  1669. prime_idx = 0;
  1670. }
  1671. }
  1672. }
  1673. if (prime_idx == -1) {
  1674. print_verbose("Detecting GPUs, set DRI_PRIME in the environment to override GPU detection logic.");
  1675. prime_idx = DetectPrimeEGL::detect_prime(EGL_PLATFORM_WAYLAND_KHR);
  1676. }
  1677. if (prime_idx) {
  1678. print_line(vformat("Found discrete GPU, setting DRI_PRIME=%d to use it.", prime_idx));
  1679. print_line("Note: Set DRI_PRIME=0 in the environment to disable Godot from using the discrete GPU.");
  1680. setenv("DRI_PRIME", itos(prime_idx).utf8().ptr(), 1);
  1681. }
  1682. }
  1683. if (rendering_driver == "opengl3") {
  1684. egl_manager = memnew(EGLManagerWayland);
  1685. if (egl_manager->initialize(wayland_thread.get_wl_display()) != OK || egl_manager->open_display(wayland_thread.get_wl_display()) != OK) {
  1686. memdelete(egl_manager);
  1687. egl_manager = nullptr;
  1688. bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_gles");
  1689. if (fallback) {
  1690. WARN_PRINT("Your video card drivers seem not to support the required OpenGL version, switching to OpenGLES.");
  1691. rendering_driver = "opengl3_es";
  1692. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  1693. } else {
  1694. r_error = ERR_UNAVAILABLE;
  1695. OS::get_singleton()->alert(
  1696. vformat("Your video card drivers seem not to support the required OpenGL 3.3 version.\n\n"
  1697. "If possible, consider updating your video card drivers or using the Vulkan driver.\n\n"
  1698. "You can enable the Vulkan driver by starting the engine from the\n"
  1699. "command line with the command:\n\n \"%s\" --rendering-driver vulkan\n\n"
  1700. "If you recently updated your video card drivers, try rebooting.",
  1701. executable_name),
  1702. "Unable to initialize OpenGL video driver");
  1703. ERR_FAIL_MSG("Could not initialize OpenGL.");
  1704. }
  1705. } else {
  1706. RasterizerGLES3::make_current(true);
  1707. driver_found = true;
  1708. }
  1709. }
  1710. if (rendering_driver == "opengl3_es") {
  1711. egl_manager = memnew(EGLManagerWaylandGLES);
  1712. if (egl_manager->initialize(wayland_thread.get_wl_display()) != OK || egl_manager->open_display(wayland_thread.get_wl_display()) != OK) {
  1713. memdelete(egl_manager);
  1714. egl_manager = nullptr;
  1715. r_error = ERR_CANT_CREATE;
  1716. OS::get_singleton()->alert(
  1717. vformat("Your video card drivers seem not to support the required OpenGL ES 3.0 version.\n\n"
  1718. "If possible, consider updating your video card drivers or using the Vulkan driver.\n\n"
  1719. "You can enable the Vulkan driver by starting the engine from the\n"
  1720. "command line with the command:\n\n \"%s\" --rendering-driver vulkan\n\n"
  1721. "If you recently updated your video card drivers, try rebooting.",
  1722. executable_name),
  1723. "Unable to initialize OpenGL ES video driver");
  1724. ERR_FAIL_MSG("Could not initialize OpenGL ES.");
  1725. }
  1726. RasterizerGLES3::make_current(false);
  1727. driver_found = true;
  1728. }
  1729. }
  1730. #endif // GLES3_ENABLED
  1731. if (!driver_found) {
  1732. r_error = ERR_UNAVAILABLE;
  1733. ERR_FAIL_MSG("Video driver not found.");
  1734. }
  1735. cursor_set_shape(CURSOR_BUSY);
  1736. WindowData &wd = windows[MAIN_WINDOW_ID];
  1737. wd.id = MAIN_WINDOW_ID;
  1738. wd.mode = p_mode;
  1739. wd.flags = p_flags;
  1740. wd.vsync_mode = p_vsync_mode;
  1741. wd.rect.size = p_resolution;
  1742. wd.title = "Godot";
  1743. #ifdef ACCESSKIT_ENABLED
  1744. if (accessibility_driver && !accessibility_driver->window_create(wd.id, nullptr)) {
  1745. if (OS::get_singleton()->is_stdout_verbose()) {
  1746. ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!");
  1747. }
  1748. memdelete(accessibility_driver);
  1749. accessibility_driver = nullptr;
  1750. }
  1751. #endif
  1752. show_window(MAIN_WINDOW_ID);
  1753. #ifdef RD_ENABLED
  1754. if (rendering_context) {
  1755. rendering_device = memnew(RenderingDevice);
  1756. if (rendering_device->initialize(rendering_context, MAIN_WINDOW_ID) != OK) {
  1757. memdelete(rendering_device);
  1758. rendering_device = nullptr;
  1759. memdelete(rendering_context);
  1760. rendering_context = nullptr;
  1761. r_error = ERR_UNAVAILABLE;
  1762. return;
  1763. }
  1764. rendering_device->screen_create(MAIN_WINDOW_ID);
  1765. RendererCompositorRD::make_current();
  1766. }
  1767. #endif // RD_ENABLED
  1768. #ifdef DBUS_ENABLED
  1769. bool dbus_ok = true;
  1770. #ifdef SOWRAP_ENABLED
  1771. if (initialize_dbus(dylibloader_verbose) != 0) {
  1772. print_verbose("Failed to load DBus library!");
  1773. dbus_ok = false;
  1774. }
  1775. #endif
  1776. if (dbus_ok) {
  1777. bool ver_ok = false;
  1778. int version_major = 0;
  1779. int version_minor = 0;
  1780. int version_rev = 0;
  1781. dbus_get_version(&version_major, &version_minor, &version_rev);
  1782. ver_ok = (version_major == 1 && version_minor >= 10) || (version_major > 1); // 1.10.0
  1783. print_verbose(vformat("DBus %d.%d.%d detected.", version_major, version_minor, version_rev));
  1784. if (!ver_ok) {
  1785. print_verbose("Unsupported DBus library version!");
  1786. dbus_ok = false;
  1787. }
  1788. }
  1789. if (dbus_ok) {
  1790. screensaver = memnew(FreeDesktopScreenSaver);
  1791. portal_desktop = memnew(FreeDesktopPortalDesktop);
  1792. atspi_monitor = memnew(FreeDesktopAtSPIMonitor);
  1793. }
  1794. #endif // DBUS_ENABLED
  1795. screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
  1796. r_error = OK;
  1797. }
  1798. DisplayServerWayland::~DisplayServerWayland() {
  1799. if (native_menu) {
  1800. memdelete(native_menu);
  1801. native_menu = nullptr;
  1802. }
  1803. // Iterating on the window map while we delete stuff from it is a bit
  1804. // uncomfortable, plus we can't even delete /all/ windows in an arbitrary order
  1805. // (due to popups).
  1806. List<WindowID> toplevels;
  1807. for (const KeyValue<WindowID, WindowData> &pair : windows) {
  1808. WindowID id = pair.key;
  1809. if (!window_get_flag(WINDOW_FLAG_POPUP_WM_HINT, id)) {
  1810. toplevels.push_back(id);
  1811. #ifdef ACCESSKIT_ENABLED
  1812. } else if (accessibility_driver) {
  1813. accessibility_driver->window_destroy(id);
  1814. #endif
  1815. }
  1816. }
  1817. for (WindowID &id : toplevels) {
  1818. delete_sub_window(id);
  1819. }
  1820. windows.clear();
  1821. wayland_thread.destroy();
  1822. // Destroy all drivers.
  1823. #ifdef RD_ENABLED
  1824. if (rendering_device) {
  1825. memdelete(rendering_device);
  1826. }
  1827. if (rendering_context) {
  1828. memdelete(rendering_context);
  1829. }
  1830. #endif
  1831. #ifdef SPEECHD_ENABLED
  1832. if (tts) {
  1833. memdelete(tts);
  1834. }
  1835. #endif
  1836. #ifdef ACCESSKIT_ENABLED
  1837. if (accessibility_driver) {
  1838. memdelete(accessibility_driver);
  1839. }
  1840. #endif
  1841. #ifdef DBUS_ENABLED
  1842. if (portal_desktop) {
  1843. memdelete(portal_desktop);
  1844. }
  1845. if (screensaver) {
  1846. memdelete(screensaver);
  1847. }
  1848. if (atspi_monitor) {
  1849. memdelete(atspi_monitor);
  1850. }
  1851. #endif
  1852. }
  1853. void DisplayServerWayland::register_wayland_driver() {
  1854. register_create_function("wayland", create_func, get_rendering_drivers_func);
  1855. }
  1856. #endif //WAYLAND_ENABLED