os_linuxbsd.cpp 38 KB

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