os_linuxbsd.cpp 37 KB

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