os_linuxbsd.cpp 41 KB

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