os_linuxbsd.cpp 37 KB

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