os_linuxbsd.cpp 38 KB

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