os_linuxbsd.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /**************************************************************************/
  2. /* os_linuxbsd.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 "os_linuxbsd.h"
  31. #include "core/io/certs_compressed.gen.h"
  32. #include "core/io/dir_access.h"
  33. #ifdef SDL_ENABLED
  34. #include "drivers/sdl/joypad_sdl.h"
  35. #endif
  36. #include "main/main.h"
  37. #include "servers/display_server.h"
  38. #include "servers/rendering_server.h"
  39. #ifdef X11_ENABLED
  40. #include "x11/detect_prime_x11.h"
  41. #include "x11/display_server_x11.h"
  42. #endif
  43. #ifdef WAYLAND_ENABLED
  44. #include "wayland/detect_prime_egl.h"
  45. #include "wayland/display_server_wayland.h"
  46. #endif
  47. #include "modules/modules_enabled.gen.h" // For regex.
  48. #ifdef MODULE_REGEX_ENABLED
  49. #include "modules/regex/regex.h"
  50. #endif
  51. #if defined(RD_ENABLED)
  52. #include "servers/rendering/rendering_device.h"
  53. #endif
  54. #if defined(VULKAN_ENABLED)
  55. #ifdef X11_ENABLED
  56. #include "x11/rendering_context_driver_vulkan_x11.h"
  57. #endif
  58. #ifdef WAYLAND_ENABLED
  59. #include "wayland/rendering_context_driver_vulkan_wayland.h"
  60. #endif
  61. #endif
  62. #if defined(GLES3_ENABLED)
  63. #include "drivers/gles3/rasterizer_gles3.h"
  64. #endif
  65. #include <dlfcn.h>
  66. #include <sys/stat.h>
  67. #include <sys/types.h>
  68. #include <sys/utsname.h>
  69. #include <unistd.h>
  70. #include <cstdio>
  71. #include <cstdlib>
  72. #if __has_include(<mntent.h>)
  73. #include <mntent.h>
  74. #endif
  75. #if defined(__FreeBSD__)
  76. #include <sys/sysctl.h>
  77. #endif
  78. void OS_LinuxBSD::alert(const String &p_alert, const String &p_title) {
  79. const char *message_programs[] = { "zenity", "kdialog", "Xdialog", "xmessage" };
  80. String path = get_environment("PATH");
  81. Vector<String> path_elems = path.split(":", false);
  82. String program;
  83. for (int i = 0; i < path_elems.size(); i++) {
  84. for (uint64_t k = 0; k < std::size(message_programs); k++) {
  85. String tested_path = path_elems[i].path_join(message_programs[k]);
  86. if (FileAccess::exists(tested_path)) {
  87. program = tested_path;
  88. break;
  89. }
  90. }
  91. if (program.length()) {
  92. break;
  93. }
  94. }
  95. List<String> args;
  96. if (program.ends_with("zenity")) {
  97. args.push_back("--warning");
  98. args.push_back("--width");
  99. args.push_back("500");
  100. args.push_back("--title");
  101. args.push_back(p_title);
  102. args.push_back("--text");
  103. args.push_back(p_alert);
  104. }
  105. if (program.ends_with("kdialog")) {
  106. // `--sorry` uses the same icon as `--warning` in Zenity.
  107. // As of KDialog 22.12.1, its `--warning` options are only available for yes/no questions.
  108. args.push_back("--sorry");
  109. args.push_back(p_alert);
  110. args.push_back("--title");
  111. args.push_back(p_title);
  112. }
  113. if (program.ends_with("Xdialog")) {
  114. args.push_back("--title");
  115. args.push_back(p_title);
  116. args.push_back("--msgbox");
  117. args.push_back(p_alert);
  118. args.push_back("0");
  119. args.push_back("0");
  120. }
  121. if (program.ends_with("xmessage")) {
  122. args.push_back("-center");
  123. args.push_back("-title");
  124. args.push_back(p_title);
  125. args.push_back(p_alert);
  126. }
  127. if (program.length()) {
  128. execute(program, args);
  129. } else {
  130. print_line(p_alert);
  131. }
  132. }
  133. void OS_LinuxBSD::initialize() {
  134. crash_handler.initialize();
  135. OS_Unix::initialize_core();
  136. system_dir_desktop_cache = get_system_dir(SYSTEM_DIR_DESKTOP);
  137. }
  138. void OS_LinuxBSD::initialize_joypads() {
  139. #ifdef SDL_ENABLED
  140. joypad_sdl = memnew(JoypadSDL());
  141. if (joypad_sdl->initialize() != OK) {
  142. ERR_PRINT("Couldn't initialize SDL joypad input driver.");
  143. memdelete(joypad_sdl);
  144. joypad_sdl = nullptr;
  145. }
  146. #endif
  147. }
  148. String OS_LinuxBSD::get_unique_id() const {
  149. static String machine_id;
  150. if (machine_id.is_empty()) {
  151. #if defined(__FreeBSD__)
  152. const int mib[2] = { CTL_KERN, KERN_HOSTUUID };
  153. char buf[4096];
  154. memset(buf, 0, sizeof(buf));
  155. size_t len = sizeof(buf) - 1;
  156. if (sysctl(mib, 2, buf, &len, 0x0, 0) != -1) {
  157. machine_id = String::utf8(buf).remove_char('-');
  158. }
  159. #else
  160. Ref<FileAccess> f = FileAccess::open("/etc/machine-id", FileAccess::READ);
  161. if (f.is_valid()) {
  162. while (machine_id.is_empty() && !f->eof_reached()) {
  163. machine_id = f->get_line().strip_edges();
  164. }
  165. }
  166. #endif
  167. }
  168. return machine_id;
  169. }
  170. String OS_LinuxBSD::get_processor_name() const {
  171. #if defined(__FreeBSD__)
  172. const int mib[2] = { CTL_HW, HW_MODEL };
  173. char buf[4096];
  174. memset(buf, 0, sizeof(buf));
  175. size_t len = sizeof(buf) - 1;
  176. if (sysctl(mib, 2, buf, &len, 0x0, 0) != -1) {
  177. return String::utf8(buf);
  178. }
  179. #else
  180. Ref<FileAccess> f = FileAccess::open("/proc/cpuinfo", FileAccess::READ);
  181. ERR_FAIL_COND_V_MSG(f.is_null(), "", String("Couldn't open `/proc/cpuinfo` to get the CPU model name. Returning an empty string."));
  182. while (!f->eof_reached()) {
  183. const String line = f->get_line();
  184. if (line.to_lower().contains("model name")) {
  185. return line.get_slicec(':', 1).strip_edges();
  186. }
  187. }
  188. #endif
  189. ERR_FAIL_V_MSG("", String("Couldn't get the CPU model. Returning an empty string."));
  190. }
  191. bool OS_LinuxBSD::is_sandboxed() const {
  192. // This function is derived from SDL:
  193. // https://github.com/libsdl-org/SDL/blob/main/src/core/linux/SDL_sandbox.c#L28-L45
  194. if (access("/.flatpak-info", F_OK) == 0) {
  195. return true;
  196. }
  197. // For Snap, we check multiple variables because they might be set for
  198. // unrelated reasons. This is the same thing WebKitGTK does.
  199. if (has_environment("SNAP") && has_environment("SNAP_NAME") && has_environment("SNAP_REVISION")) {
  200. return true;
  201. }
  202. if (access("/run/host/container-manager", F_OK) == 0) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. void OS_LinuxBSD::finalize() {
  208. if (main_loop) {
  209. memdelete(main_loop);
  210. }
  211. main_loop = nullptr;
  212. #ifdef ALSAMIDI_ENABLED
  213. driver_alsamidi.close();
  214. #endif
  215. #ifdef SDL_ENABLED
  216. if (joypad_sdl) {
  217. memdelete(joypad_sdl);
  218. }
  219. #endif
  220. }
  221. MainLoop *OS_LinuxBSD::get_main_loop() const {
  222. return main_loop;
  223. }
  224. void OS_LinuxBSD::delete_main_loop() {
  225. if (main_loop) {
  226. memdelete(main_loop);
  227. }
  228. main_loop = nullptr;
  229. }
  230. void OS_LinuxBSD::set_main_loop(MainLoop *p_main_loop) {
  231. main_loop = p_main_loop;
  232. }
  233. String OS_LinuxBSD::get_identifier() const {
  234. return "linuxbsd";
  235. }
  236. String OS_LinuxBSD::get_name() const {
  237. #ifdef __linux__
  238. return "Linux";
  239. #elif defined(__FreeBSD__)
  240. return "FreeBSD";
  241. #elif defined(__NetBSD__)
  242. return "NetBSD";
  243. #elif defined(__OpenBSD__)
  244. return "OpenBSD";
  245. #else
  246. return "BSD";
  247. #endif
  248. }
  249. String OS_LinuxBSD::get_systemd_os_release_info_value(const String &key) const {
  250. Ref<FileAccess> f = FileAccess::open("/etc/os-release", FileAccess::READ);
  251. if (f.is_valid()) {
  252. while (!f->eof_reached()) {
  253. const String line = f->get_line();
  254. if (line.contains(key)) {
  255. String value = line.get_slicec('=', 1).strip_edges();
  256. value = value.trim_prefix("\"");
  257. return value.trim_suffix("\"");
  258. }
  259. }
  260. }
  261. return "";
  262. }
  263. String OS_LinuxBSD::get_distribution_name() const {
  264. static String distribution_name = get_systemd_os_release_info_value("NAME"); // returns a value for systemd users, otherwise an empty string.
  265. if (!distribution_name.is_empty()) {
  266. return distribution_name;
  267. }
  268. struct utsname uts; // returns a decent value for BSD family.
  269. uname(&uts);
  270. distribution_name = uts.sysname;
  271. return distribution_name;
  272. }
  273. String OS_LinuxBSD::get_version() const {
  274. static String release_version = get_systemd_os_release_info_value("VERSION"); // returns a value for systemd users, otherwise an empty string.
  275. if (!release_version.is_empty()) {
  276. return release_version;
  277. }
  278. struct utsname uts; // returns a decent value for BSD family.
  279. uname(&uts);
  280. release_version = uts.version;
  281. return release_version;
  282. }
  283. Vector<String> OS_LinuxBSD::get_video_adapter_driver_info() const {
  284. if (RenderingServer::get_singleton() == nullptr) {
  285. return Vector<String>();
  286. }
  287. static Vector<String> info;
  288. if (!info.is_empty()) {
  289. return info;
  290. }
  291. const String rendering_device_name = RenderingServer::get_singleton()->get_video_adapter_name(); // e.g. `NVIDIA GeForce GTX 970`
  292. const String rendering_device_vendor = RenderingServer::get_singleton()->get_video_adapter_vendor(); // e.g. `NVIDIA`
  293. const String card_name = rendering_device_name.trim_prefix(rendering_device_vendor).strip_edges(); // -> `GeForce GTX 970`
  294. String vendor_device_id_mappings;
  295. List<String> lspci_args;
  296. lspci_args.push_back("-n");
  297. Error err = const_cast<OS_LinuxBSD *>(this)->execute("lspci", lspci_args, &vendor_device_id_mappings);
  298. if (err != OK || vendor_device_id_mappings.is_empty()) {
  299. return Vector<String>();
  300. }
  301. // Usually found under "VGA", but for example NVIDIA mobile/laptop adapters are often listed under "3D" and some AMD adapters are under "Display".
  302. const String dc_vga = "0300"; // VGA compatible controller
  303. const String dc_display = "0302"; // Display controller
  304. const String dc_3d = "0380"; // 3D controller
  305. // splitting results by device class allows prioritizing, if multiple devices are found.
  306. Vector<String> class_vga_device_candidates;
  307. Vector<String> class_display_device_candidates;
  308. Vector<String> class_3d_device_candidates;
  309. #ifdef MODULE_REGEX_ENABLED
  310. RegEx regex_id_format = RegEx();
  311. regex_id_format.compile("^[a-f0-9]{4}:[a-f0-9]{4}$"); // e.g. `10de:13c2`; IDs are always in hexadecimal
  312. #endif
  313. Vector<String> value_lines = vendor_device_id_mappings.split("\n", false); // example: `02:00.0 0300: 10de:13c2 (rev a1)`
  314. for (const String &line : value_lines) {
  315. Vector<String> columns = line.split(" ", false);
  316. if (columns.size() < 3) {
  317. continue;
  318. }
  319. String device_class = columns[1].trim_suffix(":");
  320. const String &vendor_device_id_mapping = columns[2];
  321. #ifdef MODULE_REGEX_ENABLED
  322. if (regex_id_format.search(vendor_device_id_mapping).is_null()) {
  323. continue;
  324. }
  325. #endif
  326. if (device_class == dc_vga) {
  327. class_vga_device_candidates.push_back(vendor_device_id_mapping);
  328. } else if (device_class == dc_display) {
  329. class_display_device_candidates.push_back(vendor_device_id_mapping);
  330. } else if (device_class == dc_3d) {
  331. class_3d_device_candidates.push_back(vendor_device_id_mapping);
  332. }
  333. }
  334. // Check results against currently used device (`card_name`), in case the user has multiple graphics cards.
  335. const String device_lit = "Device"; // line of interest
  336. class_vga_device_candidates = OS_LinuxBSD::lspci_device_filter(class_vga_device_candidates, dc_vga, device_lit, card_name);
  337. class_display_device_candidates = OS_LinuxBSD::lspci_device_filter(class_display_device_candidates, dc_display, device_lit, card_name);
  338. class_3d_device_candidates = OS_LinuxBSD::lspci_device_filter(class_3d_device_candidates, dc_3d, device_lit, card_name);
  339. // Get driver names and filter out invalid ones, because some adapters are dummys used only for passthrough.
  340. // And they have no indicator besides certain driver names.
  341. const String kernel_lit = "Kernel driver in use"; // line of interest
  342. const String dummys = "vfio"; // for e.g. pci passthrough dummy kernel driver `vfio-pci`
  343. Vector<String> class_vga_device_drivers = OS_LinuxBSD::lspci_get_device_value(class_vga_device_candidates, kernel_lit, dummys);
  344. Vector<String> class_display_device_drivers = OS_LinuxBSD::lspci_get_device_value(class_display_device_candidates, kernel_lit, dummys);
  345. Vector<String> class_3d_device_drivers = OS_LinuxBSD::lspci_get_device_value(class_3d_device_candidates, kernel_lit, dummys);
  346. String driver_name;
  347. String driver_version;
  348. // Use first valid value:
  349. for (const String &driver : class_3d_device_drivers) {
  350. driver_name = driver;
  351. break;
  352. }
  353. if (driver_name.is_empty()) {
  354. for (const String &driver : class_display_device_drivers) {
  355. driver_name = driver;
  356. break;
  357. }
  358. }
  359. if (driver_name.is_empty()) {
  360. for (const String &driver : class_vga_device_drivers) {
  361. driver_name = driver;
  362. break;
  363. }
  364. }
  365. info.push_back(driver_name);
  366. String modinfo;
  367. List<String> modinfo_args;
  368. modinfo_args.push_back(driver_name);
  369. err = const_cast<OS_LinuxBSD *>(this)->execute("modinfo", modinfo_args, &modinfo);
  370. if (err != OK || modinfo.is_empty()) {
  371. info.push_back(""); // So that this method always either returns an empty array, or an array of length 2.
  372. return info;
  373. }
  374. Vector<String> lines = modinfo.split("\n", false);
  375. for (const String &line : lines) {
  376. Vector<String> columns = line.split(":", false, 1);
  377. if (columns.size() < 2) {
  378. continue;
  379. }
  380. if (columns[0].strip_edges() == "version") {
  381. driver_version = columns[1].strip_edges(); // example value: `510.85.02` on Linux/BSD
  382. break;
  383. }
  384. }
  385. info.push_back(driver_version);
  386. return info;
  387. }
  388. Vector<String> OS_LinuxBSD::lspci_device_filter(Vector<String> vendor_device_id_mapping, String class_suffix, String check_column, String whitelist) const {
  389. // NOTE: whitelist can be changed to `Vector<String>`, if the need arises.
  390. const String sep = ":";
  391. Vector<String> devices;
  392. for (const String &mapping : vendor_device_id_mapping) {
  393. String device;
  394. List<String> d_args;
  395. d_args.push_back("-d");
  396. d_args.push_back(mapping + sep + class_suffix);
  397. d_args.push_back("-vmm");
  398. Error err = const_cast<OS_LinuxBSD *>(this)->execute("lspci", d_args, &device); // e.g. `lspci -d 10de:13c2:0300 -vmm`
  399. if (err != OK) {
  400. return Vector<String>();
  401. } else if (device.is_empty()) {
  402. continue;
  403. }
  404. Vector<String> device_lines = device.split("\n", false);
  405. for (const String &line : device_lines) {
  406. Vector<String> columns = line.split(":", false, 1);
  407. if (columns.size() < 2) {
  408. continue;
  409. }
  410. if (columns[0].strip_edges() == check_column) {
  411. // for `column[0] == "Device"` this may contain `GM204 [GeForce GTX 970]`
  412. bool is_valid = true;
  413. if (!whitelist.is_empty()) {
  414. is_valid = columns[1].strip_edges().contains(whitelist);
  415. }
  416. if (is_valid) {
  417. devices.push_back(mapping);
  418. }
  419. break;
  420. }
  421. }
  422. }
  423. return devices;
  424. }
  425. Vector<String> OS_LinuxBSD::lspci_get_device_value(Vector<String> vendor_device_id_mapping, String check_column, String blacklist) const {
  426. // NOTE: blacklist can be changed to `Vector<String>`, if the need arises.
  427. const String sep = ":";
  428. Vector<String> values;
  429. for (const String &mapping : vendor_device_id_mapping) {
  430. String device;
  431. List<String> d_args;
  432. d_args.push_back("-d");
  433. d_args.push_back(mapping);
  434. d_args.push_back("-k");
  435. Error err = const_cast<OS_LinuxBSD *>(this)->execute("lspci", d_args, &device); // e.g. `lspci -d 10de:13c2 -k`
  436. if (err != OK) {
  437. return Vector<String>();
  438. } else if (device.is_empty()) {
  439. continue;
  440. }
  441. Vector<String> device_lines = device.split("\n", false);
  442. for (const String &line : device_lines) {
  443. Vector<String> columns = line.split(":", false, 1);
  444. if (columns.size() < 2) {
  445. continue;
  446. }
  447. if (columns[0].strip_edges() == check_column) {
  448. // for `column[0] == "Kernel driver in use"` this may contain `nvidia`
  449. bool is_valid = true;
  450. const String value = columns[1].strip_edges();
  451. if (!blacklist.is_empty()) {
  452. is_valid = !value.contains(blacklist);
  453. }
  454. if (is_valid) {
  455. values.push_back(value);
  456. }
  457. break;
  458. }
  459. }
  460. }
  461. return values;
  462. }
  463. Error OS_LinuxBSD::shell_open(const String &p_uri) {
  464. Error ok;
  465. int err_code;
  466. List<String> args;
  467. args.push_back(p_uri);
  468. // Agnostic
  469. ok = execute("xdg-open", args, nullptr, &err_code);
  470. if (ok == OK && !err_code) {
  471. return OK;
  472. } else if (err_code == 2) {
  473. return ERR_FILE_NOT_FOUND;
  474. }
  475. // GNOME
  476. args.push_front("open"); // The command is `gio open`, so we need to add it to args
  477. ok = execute("gio", args, nullptr, &err_code);
  478. if (ok == OK && !err_code) {
  479. return OK;
  480. } else if (err_code == 2) {
  481. return ERR_FILE_NOT_FOUND;
  482. }
  483. args.pop_front();
  484. ok = execute("gvfs-open", args, nullptr, &err_code);
  485. if (ok == OK && !err_code) {
  486. return OK;
  487. } else if (err_code == 2) {
  488. return ERR_FILE_NOT_FOUND;
  489. }
  490. // KDE
  491. ok = execute("kde-open5", args, nullptr, &err_code);
  492. if (ok == OK && !err_code) {
  493. return OK;
  494. }
  495. ok = execute("kde-open", args, nullptr, &err_code);
  496. return !err_code ? ok : FAILED;
  497. }
  498. bool OS_LinuxBSD::_check_internal_feature_support(const String &p_feature) {
  499. #ifdef FONTCONFIG_ENABLED
  500. if (p_feature == "system_fonts") {
  501. return font_config_initialized;
  502. }
  503. #endif
  504. #ifndef __linux__
  505. // `bsd` includes **all** BSD, not only "other BSD" (see `get_name()`).
  506. if (p_feature == "bsd") {
  507. return true;
  508. }
  509. #endif
  510. if (p_feature == "pc") {
  511. return true;
  512. }
  513. // Match against the specific OS (`linux`, `freebsd`, `netbsd`, `openbsd`).
  514. if (p_feature == get_name().to_lower()) {
  515. return true;
  516. }
  517. return false;
  518. }
  519. uint64_t OS_LinuxBSD::get_embedded_pck_offset() const {
  520. Ref<FileAccess> f = FileAccess::open(get_executable_path(), FileAccess::READ);
  521. if (f.is_null()) {
  522. return 0;
  523. }
  524. // Read and check ELF magic number.
  525. {
  526. uint32_t magic = f->get_32();
  527. if (magic != 0x464c457f) { // 0x7F + "ELF"
  528. return 0;
  529. }
  530. }
  531. // Read program architecture bits from class field.
  532. int bits = f->get_8() * 32;
  533. // Get info about the section header table.
  534. int64_t section_table_pos;
  535. int64_t section_header_size;
  536. if (bits == 32) {
  537. section_header_size = 40;
  538. f->seek(0x20);
  539. section_table_pos = f->get_32();
  540. f->seek(0x30);
  541. } else { // 64
  542. section_header_size = 64;
  543. f->seek(0x28);
  544. section_table_pos = f->get_64();
  545. f->seek(0x3c);
  546. }
  547. int num_sections = f->get_16();
  548. int string_section_idx = f->get_16();
  549. // Load the strings table.
  550. uint8_t *strings;
  551. {
  552. // Jump to the strings section header.
  553. f->seek(section_table_pos + string_section_idx * section_header_size);
  554. // Read strings data size and offset.
  555. int64_t string_data_pos;
  556. int64_t string_data_size;
  557. if (bits == 32) {
  558. f->seek(f->get_position() + 0x10);
  559. string_data_pos = f->get_32();
  560. string_data_size = f->get_32();
  561. } else { // 64
  562. f->seek(f->get_position() + 0x18);
  563. string_data_pos = f->get_64();
  564. string_data_size = f->get_64();
  565. }
  566. // Read strings data.
  567. f->seek(string_data_pos);
  568. strings = (uint8_t *)memalloc(string_data_size);
  569. if (!strings) {
  570. return 0;
  571. }
  572. f->get_buffer(strings, string_data_size);
  573. }
  574. // Search for the "pck" section.
  575. int64_t off = 0;
  576. for (int i = 0; i < num_sections; ++i) {
  577. int64_t section_header_pos = section_table_pos + i * section_header_size;
  578. f->seek(section_header_pos);
  579. uint32_t name_offset = f->get_32();
  580. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  581. if (bits == 32) {
  582. f->seek(section_header_pos + 0x10);
  583. off = f->get_32();
  584. } else { // 64
  585. f->seek(section_header_pos + 0x18);
  586. off = f->get_64();
  587. }
  588. break;
  589. }
  590. }
  591. memfree(strings);
  592. return off;
  593. }
  594. Vector<String> OS_LinuxBSD::get_system_fonts() const {
  595. #ifdef FONTCONFIG_ENABLED
  596. if (!font_config_initialized) {
  597. ERR_FAIL_V_MSG(Vector<String>(), "Unable to load fontconfig, system font support is disabled.");
  598. }
  599. HashSet<String> font_names;
  600. Vector<String> ret;
  601. static const char *allowed_formats[] = { "TrueType", "CFF" };
  602. for (size_t i = 0; i < sizeof(allowed_formats) / sizeof(const char *); i++) {
  603. FcPattern *pattern = FcPatternCreate();
  604. ERR_CONTINUE(!pattern);
  605. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  606. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  607. FcFontSet *font_set = FcFontList(config, pattern, object_set);
  608. if (font_set) {
  609. for (int j = 0; j < font_set->nfont; j++) {
  610. char *family_name = nullptr;
  611. if (FcPatternGetString(font_set->fonts[j], FC_FAMILY, 0, reinterpret_cast<FcChar8 **>(&family_name)) == FcResultMatch) {
  612. if (family_name) {
  613. font_names.insert(String::utf8(family_name));
  614. }
  615. }
  616. }
  617. FcFontSetDestroy(font_set);
  618. }
  619. FcPatternDestroy(pattern);
  620. }
  621. for (const String &E : font_names) {
  622. ret.push_back(E);
  623. }
  624. return ret;
  625. #else
  626. ERR_FAIL_V_MSG(Vector<String>(), "Godot was compiled without fontconfig, system font support is disabled.");
  627. #endif
  628. }
  629. #ifdef FONTCONFIG_ENABLED
  630. int OS_LinuxBSD::_weight_to_fc(int p_weight) const {
  631. if (p_weight < 150) {
  632. return FC_WEIGHT_THIN;
  633. } else if (p_weight < 250) {
  634. return FC_WEIGHT_EXTRALIGHT;
  635. } else if (p_weight < 325) {
  636. return FC_WEIGHT_LIGHT;
  637. } else if (p_weight < 375) {
  638. return FC_WEIGHT_DEMILIGHT;
  639. } else if (p_weight < 390) {
  640. return FC_WEIGHT_BOOK;
  641. } else if (p_weight < 450) {
  642. return FC_WEIGHT_REGULAR;
  643. } else if (p_weight < 550) {
  644. return FC_WEIGHT_MEDIUM;
  645. } else if (p_weight < 650) {
  646. return FC_WEIGHT_DEMIBOLD;
  647. } else if (p_weight < 750) {
  648. return FC_WEIGHT_BOLD;
  649. } else if (p_weight < 850) {
  650. return FC_WEIGHT_EXTRABOLD;
  651. } else if (p_weight < 925) {
  652. return FC_WEIGHT_BLACK;
  653. } else {
  654. return FC_WEIGHT_EXTRABLACK;
  655. }
  656. }
  657. int OS_LinuxBSD::_stretch_to_fc(int p_stretch) const {
  658. if (p_stretch < 56) {
  659. return FC_WIDTH_ULTRACONDENSED;
  660. } else if (p_stretch < 69) {
  661. return FC_WIDTH_EXTRACONDENSED;
  662. } else if (p_stretch < 81) {
  663. return FC_WIDTH_CONDENSED;
  664. } else if (p_stretch < 93) {
  665. return FC_WIDTH_SEMICONDENSED;
  666. } else if (p_stretch < 106) {
  667. return FC_WIDTH_NORMAL;
  668. } else if (p_stretch < 137) {
  669. return FC_WIDTH_SEMIEXPANDED;
  670. } else if (p_stretch < 144) {
  671. return FC_WIDTH_EXPANDED;
  672. } else if (p_stretch < 162) {
  673. return FC_WIDTH_EXTRAEXPANDED;
  674. } else {
  675. return FC_WIDTH_ULTRAEXPANDED;
  676. }
  677. }
  678. #endif // FONTCONFIG_ENABLED
  679. Vector<String> OS_LinuxBSD::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
  680. #ifdef FONTCONFIG_ENABLED
  681. if (!font_config_initialized) {
  682. ERR_FAIL_V_MSG(Vector<String>(), "Unable to load fontconfig, system font support is disabled.");
  683. }
  684. Vector<String> ret;
  685. static const char *allowed_formats[] = { "TrueType", "CFF" };
  686. for (size_t i = 0; i < std::size(allowed_formats); i++) {
  687. FcPattern *pattern = FcPatternCreate();
  688. if (pattern) {
  689. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  690. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  691. FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8 *>(p_font_name.utf8().get_data()));
  692. FcPatternAddInteger(pattern, FC_WEIGHT, _weight_to_fc(p_weight));
  693. FcPatternAddInteger(pattern, FC_WIDTH, _stretch_to_fc(p_stretch));
  694. FcPatternAddInteger(pattern, FC_SLANT, p_italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  695. FcCharSet *char_set = FcCharSetCreate();
  696. for (int j = 0; j < p_text.size(); j++) {
  697. FcCharSetAddChar(char_set, p_text[j]);
  698. }
  699. FcPatternAddCharSet(pattern, FC_CHARSET, char_set);
  700. FcLangSet *lang_set = FcLangSetCreate();
  701. FcLangSetAdd(lang_set, reinterpret_cast<const FcChar8 *>(p_locale.utf8().get_data()));
  702. FcPatternAddLangSet(pattern, FC_LANG, lang_set);
  703. FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
  704. FcDefaultSubstitute(pattern);
  705. FcResult result;
  706. FcPattern *match = FcFontMatch(nullptr, pattern, &result);
  707. if (match) {
  708. char *file_name = nullptr;
  709. if (FcPatternGetString(match, FC_FILE, 0, reinterpret_cast<FcChar8 **>(&file_name)) == FcResultMatch) {
  710. if (file_name) {
  711. ret.push_back(String::utf8(file_name));
  712. }
  713. }
  714. FcPatternDestroy(match);
  715. }
  716. FcPatternDestroy(pattern);
  717. FcCharSetDestroy(char_set);
  718. FcLangSetDestroy(lang_set);
  719. }
  720. }
  721. return ret;
  722. #else
  723. ERR_FAIL_V_MSG(Vector<String>(), "Godot was compiled without fontconfig, system font support is disabled.");
  724. #endif
  725. }
  726. String OS_LinuxBSD::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  727. #ifdef FONTCONFIG_ENABLED
  728. if (!font_config_initialized) {
  729. ERR_FAIL_V_MSG(String(), "Unable to load fontconfig, system font support is disabled.");
  730. }
  731. static const char *allowed_formats[] = { "TrueType", "CFF" };
  732. for (size_t i = 0; i < sizeof(allowed_formats) / sizeof(const char *); i++) {
  733. FcPattern *pattern = FcPatternCreate();
  734. if (pattern) {
  735. bool allow_substitutes = (p_font_name.to_lower() == "sans-serif") || (p_font_name.to_lower() == "serif") || (p_font_name.to_lower() == "monospace") || (p_font_name.to_lower() == "cursive") || (p_font_name.to_lower() == "fantasy");
  736. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  737. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  738. FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8 *>(p_font_name.utf8().get_data()));
  739. FcPatternAddInteger(pattern, FC_WEIGHT, _weight_to_fc(p_weight));
  740. FcPatternAddInteger(pattern, FC_WIDTH, _stretch_to_fc(p_stretch));
  741. FcPatternAddInteger(pattern, FC_SLANT, p_italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  742. FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
  743. FcDefaultSubstitute(pattern);
  744. FcResult result;
  745. FcPattern *match = FcFontMatch(nullptr, pattern, &result);
  746. if (match) {
  747. if (!allow_substitutes) {
  748. char *family_name = nullptr;
  749. if (FcPatternGetString(match, FC_FAMILY, 0, reinterpret_cast<FcChar8 **>(&family_name)) == FcResultMatch) {
  750. if (family_name && String::utf8(family_name).to_lower() != p_font_name.to_lower()) {
  751. FcPatternDestroy(match);
  752. FcPatternDestroy(pattern);
  753. continue;
  754. }
  755. }
  756. }
  757. char *file_name = nullptr;
  758. if (FcPatternGetString(match, FC_FILE, 0, reinterpret_cast<FcChar8 **>(&file_name)) == FcResultMatch) {
  759. if (file_name) {
  760. String ret = String::utf8(file_name);
  761. FcPatternDestroy(match);
  762. FcPatternDestroy(pattern);
  763. return ret;
  764. }
  765. }
  766. FcPatternDestroy(match);
  767. }
  768. FcPatternDestroy(pattern);
  769. }
  770. }
  771. return String();
  772. #else
  773. ERR_FAIL_V_MSG(String(), "Godot was compiled without fontconfig, system font support is disabled.");
  774. #endif
  775. }
  776. String OS_LinuxBSD::get_config_path() const {
  777. if (has_environment("XDG_CONFIG_HOME")) {
  778. if (get_environment("XDG_CONFIG_HOME").is_absolute_path()) {
  779. return get_environment("XDG_CONFIG_HOME");
  780. } else {
  781. WARN_PRINT_ONCE("`XDG_CONFIG_HOME` is a relative path. Ignoring its value and falling back to `$HOME/.config` or `.` per the XDG Base Directory specification.");
  782. return has_environment("HOME") ? get_environment("HOME").path_join(".config") : ".";
  783. }
  784. } else if (has_environment("HOME")) {
  785. return get_environment("HOME").path_join(".config");
  786. } else {
  787. return ".";
  788. }
  789. }
  790. String OS_LinuxBSD::get_data_path() const {
  791. if (has_environment("XDG_DATA_HOME")) {
  792. if (get_environment("XDG_DATA_HOME").is_absolute_path()) {
  793. return get_environment("XDG_DATA_HOME");
  794. } else {
  795. WARN_PRINT_ONCE("`XDG_DATA_HOME` is a relative path. Ignoring its value and falling back to `$HOME/.local/share` or `get_config_path()` per the XDG Base Directory specification.");
  796. return has_environment("HOME") ? get_environment("HOME").path_join(".local/share") : get_config_path();
  797. }
  798. } else if (has_environment("HOME")) {
  799. return get_environment("HOME").path_join(".local/share");
  800. } else {
  801. return get_config_path();
  802. }
  803. }
  804. String OS_LinuxBSD::get_cache_path() const {
  805. if (has_environment("XDG_CACHE_HOME")) {
  806. if (get_environment("XDG_CACHE_HOME").is_absolute_path()) {
  807. return get_environment("XDG_CACHE_HOME");
  808. } else {
  809. WARN_PRINT_ONCE("`XDG_CACHE_HOME` is a relative path. Ignoring its value and falling back to `$HOME/.cache` or `get_config_path()` per the XDG Base Directory specification.");
  810. return has_environment("HOME") ? get_environment("HOME").path_join(".cache") : get_config_path();
  811. }
  812. } else if (has_environment("HOME")) {
  813. return get_environment("HOME").path_join(".cache");
  814. } else {
  815. return get_config_path();
  816. }
  817. }
  818. String OS_LinuxBSD::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  819. if (p_dir == SYSTEM_DIR_DESKTOP && !system_dir_desktop_cache.is_empty()) {
  820. return system_dir_desktop_cache;
  821. }
  822. String xdgparam;
  823. switch (p_dir) {
  824. case SYSTEM_DIR_DESKTOP: {
  825. xdgparam = "DESKTOP";
  826. } break;
  827. case SYSTEM_DIR_DCIM: {
  828. xdgparam = "PICTURES";
  829. } break;
  830. case SYSTEM_DIR_DOCUMENTS: {
  831. xdgparam = "DOCUMENTS";
  832. } break;
  833. case SYSTEM_DIR_DOWNLOADS: {
  834. xdgparam = "DOWNLOAD";
  835. } break;
  836. case SYSTEM_DIR_MOVIES: {
  837. xdgparam = "VIDEOS";
  838. } break;
  839. case SYSTEM_DIR_MUSIC: {
  840. xdgparam = "MUSIC";
  841. } break;
  842. case SYSTEM_DIR_PICTURES: {
  843. xdgparam = "PICTURES";
  844. } break;
  845. case SYSTEM_DIR_RINGTONES: {
  846. xdgparam = "MUSIC";
  847. } break;
  848. }
  849. String pipe;
  850. List<String> arg;
  851. arg.push_back(xdgparam);
  852. Error err = const_cast<OS_LinuxBSD *>(this)->execute("xdg-user-dir", arg, &pipe);
  853. if (err != OK) {
  854. return ".";
  855. }
  856. return pipe.strip_edges();
  857. }
  858. void OS_LinuxBSD::run() {
  859. if (!main_loop) {
  860. return;
  861. }
  862. main_loop->initialize();
  863. //uint64_t last_ticks=get_ticks_usec();
  864. //int frames=0;
  865. //uint64_t frame=0;
  866. while (true) {
  867. DisplayServer::get_singleton()->process_events(); // get rid of pending events
  868. #ifdef SDL_ENABLED
  869. if (joypad_sdl) {
  870. joypad_sdl->process_events();
  871. }
  872. #endif
  873. if (Main::iteration()) {
  874. break;
  875. }
  876. }
  877. main_loop->finalize();
  878. }
  879. void OS_LinuxBSD::disable_crash_handler() {
  880. crash_handler.disable();
  881. }
  882. bool OS_LinuxBSD::is_disable_crash_handler() const {
  883. return crash_handler.is_disabled();
  884. }
  885. static String get_mountpoint(const String &p_path) {
  886. struct stat s;
  887. if (stat(p_path.utf8().get_data(), &s)) {
  888. return "";
  889. }
  890. #if __has_include(<mntent.h>)
  891. dev_t dev = s.st_dev;
  892. FILE *fd = setmntent("/proc/mounts", "r");
  893. if (!fd) {
  894. return "";
  895. }
  896. struct mntent mnt;
  897. char buf[1024];
  898. size_t buflen = 1024;
  899. while (getmntent_r(fd, &mnt, buf, buflen)) {
  900. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  901. endmntent(fd);
  902. return String(mnt.mnt_dir);
  903. }
  904. }
  905. endmntent(fd);
  906. #endif
  907. return "";
  908. }
  909. Error OS_LinuxBSD::move_to_trash(const String &p_path) {
  910. // We try multiple methods, until we find one that works.
  911. // So we only return on success until we exhausted possibilities.
  912. String path = p_path.rstrip("/"); // Strip trailing slash when path points to a directory.
  913. int err_code;
  914. List<String> args;
  915. args.push_back(path);
  916. args.push_front("trash"); // The command is `gio trash <file_name>` so we add it before the path.
  917. Error result = execute("gio", args, nullptr, &err_code); // For GNOME based machines.
  918. if (result == OK && err_code == 0) { // Success.
  919. return OK;
  920. }
  921. args.pop_front();
  922. args.push_front("move");
  923. args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`.
  924. result = execute("kioclient5", args, nullptr, &err_code); // For KDE based machines.
  925. if (result == OK && err_code == 0) {
  926. return OK;
  927. }
  928. args.pop_front();
  929. args.pop_back();
  930. result = execute("gvfs-trash", args, nullptr, &err_code); // For older Linux machines.
  931. if (result == OK && err_code == 0) {
  932. return OK;
  933. }
  934. // If the commands `kioclient5`, `gio` or `gvfs-trash` don't work on the system we do it manually.
  935. String trash_path = "";
  936. String mnt = get_mountpoint(path);
  937. // If there is a directory "[Mountpoint]/.Trash-[UID], use it as the trash can.
  938. if (!mnt.is_empty()) {
  939. String mountpoint_trash_path(mnt + "/.Trash-" + itos(getuid()));
  940. struct stat s;
  941. if (!stat(mountpoint_trash_path.utf8().get_data(), &s)) {
  942. trash_path = mountpoint_trash_path;
  943. }
  944. }
  945. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash" as the trash can.
  946. if (trash_path.is_empty()) {
  947. char *dhome = getenv("XDG_DATA_HOME");
  948. if (dhome) {
  949. trash_path = String::utf8(dhome) + "/Trash";
  950. }
  951. }
  952. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash" as the trash can.
  953. if (trash_path.is_empty()) {
  954. char *home = getenv("HOME");
  955. if (home) {
  956. trash_path = String::utf8(home) + "/.local/share/Trash";
  957. }
  958. }
  959. // Issue an error if none of the previous locations is appropriate for the trash can.
  960. ERR_FAIL_COND_V_MSG(trash_path.is_empty(), FAILED, "Could not determine the trash can location");
  961. // Create needed directories for decided trash can location.
  962. {
  963. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  964. Error err = dir_access->make_dir_recursive(trash_path);
  965. // Issue an error if trash can is not created properly.
  966. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"");
  967. err = dir_access->make_dir_recursive(trash_path + "/files");
  968. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "/files\"");
  969. err = dir_access->make_dir_recursive(trash_path + "/info");
  970. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "/info\"");
  971. }
  972. // The trash can is successfully created, now we check that we don't exceed our file name length limit.
  973. // If the file name is too long trim it so we can add the identifying number and ".trashinfo".
  974. // Assumes that the file name length limit is 255 characters.
  975. String file_name = path.get_file();
  976. if (file_name.length() > 240) {
  977. file_name = file_name.substr(0, file_name.length() - 15);
  978. }
  979. String dest_path = trash_path + "/files/" + file_name;
  980. struct stat buff;
  981. int id_number = 0;
  982. String fn = file_name;
  983. // Checks if a resource with the same name already exist in the trash can,
  984. // if there is, add an identifying number to our resource's name.
  985. while (stat(dest_path.utf8().get_data(), &buff) == 0) {
  986. id_number++;
  987. // Added a limit to check for identically named files already on the trash can
  988. // if there are too many it could make the editor unresponsive.
  989. ERR_FAIL_COND_V_MSG(id_number > 99, FAILED, "Too many identically named resources already in the trash can.");
  990. fn = file_name + "." + itos(id_number);
  991. dest_path = trash_path + "/files/" + fn;
  992. }
  993. file_name = fn;
  994. String renamed_path = path.get_base_dir() + "/" + file_name;
  995. // Generates the .trashinfo file
  996. OS::DateTime dt = OS::get_singleton()->get_datetime(false);
  997. String timestamp = vformat("%04d-%02d-%02dT%02d:%02d:", dt.year, (int)dt.month, dt.day, dt.hour, dt.minute);
  998. timestamp = vformat("%s%02d", timestamp, dt.second); // vformat only supports up to 6 arguments.
  999. String trash_info = "[Trash Info]\nPath=" + path.uri_encode() + "\nDeletionDate=" + timestamp + "\n";
  1000. {
  1001. Error err;
  1002. {
  1003. Ref<FileAccess> file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err);
  1004. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file: \"" + trash_path + "/info/" + file_name + ".trashinfo\"");
  1005. file->store_string(trash_info);
  1006. }
  1007. // Rename our resource before moving it to the trash can.
  1008. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1009. err = dir_access->rename(path, renamed_path);
  1010. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + path + "\" to \"" + renamed_path + "\"");
  1011. }
  1012. // Move the given resource to the trash can.
  1013. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  1014. List<String> mv_args;
  1015. mv_args.push_back(renamed_path);
  1016. mv_args.push_back(trash_path + "/files");
  1017. {
  1018. int retval;
  1019. Error err = execute("mv", mv_args, nullptr, &retval);
  1020. // Issue an error if "mv" failed to move the given resource to the trash can.
  1021. if (err != OK || retval != 0) {
  1022. ERR_PRINT("move_to_trash: Could not move the resource \"" + path + "\" to the trash can \"" + trash_path + "/files\"");
  1023. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1024. err = dir_access->rename(renamed_path, path);
  1025. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not rename \"" + renamed_path + "\" back to its original name: \"" + path + "\"");
  1026. return FAILED;
  1027. }
  1028. }
  1029. return OK;
  1030. }
  1031. String OS_LinuxBSD::get_system_ca_certificates() {
  1032. String certfile;
  1033. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1034. // Compile time preferred certificates path.
  1035. if (!String(_SYSTEM_CERTS_PATH).is_empty() && da->file_exists(_SYSTEM_CERTS_PATH)) {
  1036. certfile = _SYSTEM_CERTS_PATH;
  1037. } else if (da->file_exists("/etc/ssl/certs/ca-certificates.crt")) {
  1038. // Debian/Ubuntu
  1039. certfile = "/etc/ssl/certs/ca-certificates.crt";
  1040. } else if (da->file_exists("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem")) {
  1041. // Fedora
  1042. certfile = "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem";
  1043. } else if (da->file_exists("/etc/ca-certificates/extracted/tls-ca-bundle.pem")) {
  1044. // Arch Linux
  1045. certfile = "/etc/ca-certificates/extracted/tls-ca-bundle.pem";
  1046. } else if (da->file_exists("/var/lib/ca-certificates/ca-bundle.pem")) {
  1047. // openSUSE
  1048. certfile = "/var/lib/ca-certificates/ca-bundle.pem";
  1049. } else if (da->file_exists("/etc/ssl/cert.pem")) {
  1050. // FreeBSD/OpenBSD
  1051. certfile = "/etc/ssl/cert.pem";
  1052. }
  1053. if (certfile.is_empty()) {
  1054. return "";
  1055. }
  1056. Ref<FileAccess> f = FileAccess::open(certfile, FileAccess::READ);
  1057. ERR_FAIL_COND_V_MSG(f.is_null(), "", vformat("Failed to open system CA certificates file: '%s'", certfile));
  1058. return f->get_as_text();
  1059. }
  1060. #ifdef TOOLS_ENABLED
  1061. bool OS_LinuxBSD::_test_create_rendering_device(const String &p_display_driver) const {
  1062. // Tests Rendering Device creation.
  1063. bool ok = false;
  1064. #if defined(RD_ENABLED)
  1065. Error err;
  1066. RenderingContextDriver *rcd = nullptr;
  1067. #if defined(VULKAN_ENABLED)
  1068. #ifdef X11_ENABLED
  1069. if (p_display_driver == "x11" || p_display_driver.is_empty()) {
  1070. rcd = memnew(RenderingContextDriverVulkanX11);
  1071. }
  1072. #endif
  1073. #ifdef WAYLAND_ENABLED
  1074. if (p_display_driver == "wayland") {
  1075. rcd = memnew(RenderingContextDriverVulkanWayland);
  1076. }
  1077. #endif
  1078. #endif
  1079. if (rcd != nullptr) {
  1080. err = rcd->initialize();
  1081. if (err == OK) {
  1082. RenderingDevice *rd = memnew(RenderingDevice);
  1083. err = rd->initialize(rcd);
  1084. memdelete(rd);
  1085. rd = nullptr;
  1086. if (err == OK) {
  1087. ok = true;
  1088. }
  1089. }
  1090. memdelete(rcd);
  1091. rcd = nullptr;
  1092. }
  1093. #endif
  1094. return ok;
  1095. }
  1096. bool OS_LinuxBSD::_test_create_rendering_device_and_gl(const String &p_display_driver) const {
  1097. // Tests OpenGL context and Rendering Device simultaneous creation. This function is expected to crash on some drivers.
  1098. #ifdef GLES3_ENABLED
  1099. #ifdef X11_ENABLED
  1100. if (p_display_driver == "x11" || p_display_driver.is_empty()) {
  1101. #ifdef SOWRAP_ENABLED
  1102. if (initialize_xlib(0) != 0) {
  1103. return false;
  1104. }
  1105. #endif
  1106. DetectPrimeX11::create_context();
  1107. }
  1108. #endif
  1109. #ifdef WAYLAND_ENABLED
  1110. if (p_display_driver == "wayland") {
  1111. #ifdef SOWRAP_ENABLED
  1112. if (initialize_wayland_egl(0) != 0) {
  1113. return false;
  1114. }
  1115. #endif
  1116. DetectPrimeEGL::create_context(EGL_PLATFORM_WAYLAND_KHR);
  1117. }
  1118. #endif
  1119. RasterizerGLES3::make_current(true);
  1120. #endif
  1121. return _test_create_rendering_device(p_display_driver);
  1122. }
  1123. #endif
  1124. OS_LinuxBSD::OS_LinuxBSD() {
  1125. main_loop = nullptr;
  1126. #ifdef PULSEAUDIO_ENABLED
  1127. AudioDriverManager::add_driver(&driver_pulseaudio);
  1128. #endif
  1129. #ifdef ALSA_ENABLED
  1130. AudioDriverManager::add_driver(&driver_alsa);
  1131. #endif
  1132. #ifdef X11_ENABLED
  1133. DisplayServerX11::register_x11_driver();
  1134. #endif
  1135. #ifdef WAYLAND_ENABLED
  1136. DisplayServerWayland::register_wayland_driver();
  1137. #endif
  1138. #ifdef FONTCONFIG_ENABLED
  1139. #ifdef SOWRAP_ENABLED
  1140. #ifdef DEBUG_ENABLED
  1141. int dylibloader_verbose = 1;
  1142. #else
  1143. int dylibloader_verbose = 0;
  1144. #endif
  1145. font_config_initialized = (initialize_fontconfig(dylibloader_verbose) == 0);
  1146. #else
  1147. font_config_initialized = true;
  1148. #endif
  1149. if (font_config_initialized) {
  1150. bool ver_ok = false;
  1151. int version = FcGetVersion();
  1152. ver_ok = ((version / 100 / 100) == 2 && (version / 100 % 100) >= 11) || ((version / 100 / 100) > 2); // 2.11.0
  1153. print_verbose(vformat("FontConfig %d.%d.%d detected.", version / 100 / 100, version / 100 % 100, version % 100));
  1154. if (!ver_ok) {
  1155. font_config_initialized = false;
  1156. }
  1157. }
  1158. if (font_config_initialized) {
  1159. config = FcInitLoadConfigAndFonts();
  1160. if (!config) {
  1161. font_config_initialized = false;
  1162. }
  1163. object_set = FcObjectSetBuild(FC_FAMILY, FC_FILE, nullptr);
  1164. if (!object_set) {
  1165. font_config_initialized = false;
  1166. }
  1167. }
  1168. #endif // FONTCONFIG_ENABLED
  1169. }
  1170. OS_LinuxBSD::~OS_LinuxBSD() {
  1171. #ifdef FONTCONFIG_ENABLED
  1172. if (object_set) {
  1173. FcObjectSetDestroy(object_set);
  1174. }
  1175. if (config) {
  1176. FcConfigDestroy(config);
  1177. }
  1178. #endif // FONTCONFIG_ENABLED
  1179. }