os_linuxbsd.cpp 41 KB

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