os_linuxbsd.cpp 37 KB

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