os_linuxbsd.cpp 40 KB

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