os_linuxbsd.cpp 38 KB

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