os_linuxbsd.cpp 38 KB

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