os_linuxbsd.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. return !err_code ? ok : FAILED;
  500. }
  501. bool OS_LinuxBSD::_check_internal_feature_support(const String &p_feature) {
  502. #ifdef FONTCONFIG_ENABLED
  503. if (p_feature == "system_fonts") {
  504. return font_config_initialized;
  505. }
  506. #endif
  507. #ifndef __linux__
  508. // `bsd` includes **all** BSD, not only "other BSD" (see `get_name()`).
  509. if (p_feature == "bsd") {
  510. return true;
  511. }
  512. #endif
  513. if (p_feature == "pc") {
  514. return true;
  515. }
  516. // Match against the specific OS (`linux`, `freebsd`, `netbsd`, `openbsd`).
  517. if (p_feature == get_name().to_lower()) {
  518. return true;
  519. }
  520. return false;
  521. }
  522. uint64_t OS_LinuxBSD::get_embedded_pck_offset() const {
  523. Ref<FileAccess> f = FileAccess::open(get_executable_path(), FileAccess::READ);
  524. if (f.is_null()) {
  525. return 0;
  526. }
  527. // Read and check ELF magic number.
  528. {
  529. uint32_t magic = f->get_32();
  530. if (magic != 0x464c457f) { // 0x7F + "ELF"
  531. return 0;
  532. }
  533. }
  534. // Read program architecture bits from class field.
  535. int bits = f->get_8() * 32;
  536. // Get info about the section header table.
  537. int64_t section_table_pos;
  538. int64_t section_header_size;
  539. if (bits == 32) {
  540. section_header_size = 40;
  541. f->seek(0x20);
  542. section_table_pos = f->get_32();
  543. f->seek(0x30);
  544. } else { // 64
  545. section_header_size = 64;
  546. f->seek(0x28);
  547. section_table_pos = f->get_64();
  548. f->seek(0x3c);
  549. }
  550. int num_sections = f->get_16();
  551. int string_section_idx = f->get_16();
  552. // Load the strings table.
  553. uint8_t *strings;
  554. {
  555. // Jump to the strings section header.
  556. f->seek(section_table_pos + string_section_idx * section_header_size);
  557. // Read strings data size and offset.
  558. int64_t string_data_pos;
  559. int64_t string_data_size;
  560. if (bits == 32) {
  561. f->seek(f->get_position() + 0x10);
  562. string_data_pos = f->get_32();
  563. string_data_size = f->get_32();
  564. } else { // 64
  565. f->seek(f->get_position() + 0x18);
  566. string_data_pos = f->get_64();
  567. string_data_size = f->get_64();
  568. }
  569. // Read strings data.
  570. f->seek(string_data_pos);
  571. strings = (uint8_t *)memalloc(string_data_size);
  572. if (!strings) {
  573. return 0;
  574. }
  575. f->get_buffer(strings, string_data_size);
  576. }
  577. // Search for the "pck" section.
  578. int64_t off = 0;
  579. for (int i = 0; i < num_sections; ++i) {
  580. int64_t section_header_pos = section_table_pos + i * section_header_size;
  581. f->seek(section_header_pos);
  582. uint32_t name_offset = f->get_32();
  583. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  584. if (bits == 32) {
  585. f->seek(section_header_pos + 0x10);
  586. off = f->get_32();
  587. } else { // 64
  588. f->seek(section_header_pos + 0x18);
  589. off = f->get_64();
  590. }
  591. break;
  592. }
  593. }
  594. memfree(strings);
  595. return off;
  596. }
  597. Vector<String> OS_LinuxBSD::get_system_fonts() const {
  598. #ifdef FONTCONFIG_ENABLED
  599. if (!font_config_initialized) {
  600. ERR_FAIL_V_MSG(Vector<String>(), "Unable to load fontconfig, system font support is disabled.");
  601. }
  602. HashSet<String> font_names;
  603. Vector<String> ret;
  604. static const char *allowed_formats[] = { "TrueType", "CFF" };
  605. for (size_t i = 0; i < sizeof(allowed_formats) / sizeof(const char *); i++) {
  606. FcPattern *pattern = FcPatternCreate();
  607. ERR_CONTINUE(!pattern);
  608. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  609. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  610. FcFontSet *font_set = FcFontList(config, pattern, object_set);
  611. if (font_set) {
  612. for (int j = 0; j < font_set->nfont; j++) {
  613. char *family_name = nullptr;
  614. if (FcPatternGetString(font_set->fonts[j], FC_FAMILY, 0, reinterpret_cast<FcChar8 **>(&family_name)) == FcResultMatch) {
  615. if (family_name) {
  616. font_names.insert(String::utf8(family_name));
  617. }
  618. }
  619. }
  620. FcFontSetDestroy(font_set);
  621. }
  622. FcPatternDestroy(pattern);
  623. }
  624. for (const String &E : font_names) {
  625. ret.push_back(E);
  626. }
  627. return ret;
  628. #else
  629. ERR_FAIL_V_MSG(Vector<String>(), "Godot was compiled without fontconfig, system font support is disabled.");
  630. #endif
  631. }
  632. #ifdef FONTCONFIG_ENABLED
  633. int OS_LinuxBSD::_weight_to_fc(int p_weight) const {
  634. if (p_weight < 150) {
  635. return FC_WEIGHT_THIN;
  636. } else if (p_weight < 250) {
  637. return FC_WEIGHT_EXTRALIGHT;
  638. } else if (p_weight < 325) {
  639. return FC_WEIGHT_LIGHT;
  640. } else if (p_weight < 375) {
  641. return FC_WEIGHT_DEMILIGHT;
  642. } else if (p_weight < 390) {
  643. return FC_WEIGHT_BOOK;
  644. } else if (p_weight < 450) {
  645. return FC_WEIGHT_REGULAR;
  646. } else if (p_weight < 550) {
  647. return FC_WEIGHT_MEDIUM;
  648. } else if (p_weight < 650) {
  649. return FC_WEIGHT_DEMIBOLD;
  650. } else if (p_weight < 750) {
  651. return FC_WEIGHT_BOLD;
  652. } else if (p_weight < 850) {
  653. return FC_WEIGHT_EXTRABOLD;
  654. } else if (p_weight < 925) {
  655. return FC_WEIGHT_BLACK;
  656. } else {
  657. return FC_WEIGHT_EXTRABLACK;
  658. }
  659. }
  660. int OS_LinuxBSD::_stretch_to_fc(int p_stretch) const {
  661. if (p_stretch < 56) {
  662. return FC_WIDTH_ULTRACONDENSED;
  663. } else if (p_stretch < 69) {
  664. return FC_WIDTH_EXTRACONDENSED;
  665. } else if (p_stretch < 81) {
  666. return FC_WIDTH_CONDENSED;
  667. } else if (p_stretch < 93) {
  668. return FC_WIDTH_SEMICONDENSED;
  669. } else if (p_stretch < 106) {
  670. return FC_WIDTH_NORMAL;
  671. } else if (p_stretch < 137) {
  672. return FC_WIDTH_SEMIEXPANDED;
  673. } else if (p_stretch < 144) {
  674. return FC_WIDTH_EXPANDED;
  675. } else if (p_stretch < 162) {
  676. return FC_WIDTH_EXTRAEXPANDED;
  677. } else {
  678. return FC_WIDTH_ULTRAEXPANDED;
  679. }
  680. }
  681. #endif // FONTCONFIG_ENABLED
  682. 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 {
  683. #ifdef FONTCONFIG_ENABLED
  684. if (!font_config_initialized) {
  685. ERR_FAIL_V_MSG(Vector<String>(), "Unable to load fontconfig, system font support is disabled.");
  686. }
  687. Vector<String> ret;
  688. static const char *allowed_formats[] = { "TrueType", "CFF" };
  689. for (size_t i = 0; i < std_size(allowed_formats); i++) {
  690. FcPattern *pattern = FcPatternCreate();
  691. if (pattern) {
  692. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  693. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  694. FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8 *>(p_font_name.utf8().get_data()));
  695. FcPatternAddInteger(pattern, FC_WEIGHT, _weight_to_fc(p_weight));
  696. FcPatternAddInteger(pattern, FC_WIDTH, _stretch_to_fc(p_stretch));
  697. FcPatternAddInteger(pattern, FC_SLANT, p_italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  698. FcCharSet *char_set = FcCharSetCreate();
  699. for (int j = 0; j < p_text.size(); j++) {
  700. FcCharSetAddChar(char_set, p_text[j]);
  701. }
  702. FcPatternAddCharSet(pattern, FC_CHARSET, char_set);
  703. FcLangSet *lang_set = FcLangSetCreate();
  704. FcLangSetAdd(lang_set, reinterpret_cast<const FcChar8 *>(p_locale.utf8().get_data()));
  705. FcPatternAddLangSet(pattern, FC_LANG, lang_set);
  706. FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
  707. FcDefaultSubstitute(pattern);
  708. FcResult result;
  709. FcPattern *match = FcFontMatch(nullptr, pattern, &result);
  710. if (match) {
  711. char *file_name = nullptr;
  712. if (FcPatternGetString(match, FC_FILE, 0, reinterpret_cast<FcChar8 **>(&file_name)) == FcResultMatch) {
  713. if (file_name) {
  714. ret.push_back(String::utf8(file_name));
  715. }
  716. }
  717. FcPatternDestroy(match);
  718. }
  719. FcPatternDestroy(pattern);
  720. FcCharSetDestroy(char_set);
  721. FcLangSetDestroy(lang_set);
  722. }
  723. }
  724. return ret;
  725. #else
  726. ERR_FAIL_V_MSG(Vector<String>(), "Godot was compiled without fontconfig, system font support is disabled.");
  727. #endif
  728. }
  729. String OS_LinuxBSD::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  730. #ifdef FONTCONFIG_ENABLED
  731. if (!font_config_initialized) {
  732. ERR_FAIL_V_MSG(String(), "Unable to load fontconfig, system font support is disabled.");
  733. }
  734. static const char *allowed_formats[] = { "TrueType", "CFF" };
  735. for (size_t i = 0; i < sizeof(allowed_formats) / sizeof(const char *); i++) {
  736. FcPattern *pattern = FcPatternCreate();
  737. if (pattern) {
  738. 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");
  739. FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
  740. FcPatternAddString(pattern, FC_FONTFORMAT, reinterpret_cast<const FcChar8 *>(allowed_formats[i]));
  741. FcPatternAddString(pattern, FC_FAMILY, reinterpret_cast<const FcChar8 *>(p_font_name.utf8().get_data()));
  742. FcPatternAddInteger(pattern, FC_WEIGHT, _weight_to_fc(p_weight));
  743. FcPatternAddInteger(pattern, FC_WIDTH, _stretch_to_fc(p_stretch));
  744. FcPatternAddInteger(pattern, FC_SLANT, p_italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  745. FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
  746. FcDefaultSubstitute(pattern);
  747. FcResult result;
  748. FcPattern *match = FcFontMatch(nullptr, pattern, &result);
  749. if (match) {
  750. if (!allow_substitutes) {
  751. char *family_name = nullptr;
  752. if (FcPatternGetString(match, FC_FAMILY, 0, reinterpret_cast<FcChar8 **>(&family_name)) == FcResultMatch) {
  753. if (family_name && String::utf8(family_name).to_lower() != p_font_name.to_lower()) {
  754. FcPatternDestroy(match);
  755. FcPatternDestroy(pattern);
  756. continue;
  757. }
  758. }
  759. }
  760. char *file_name = nullptr;
  761. if (FcPatternGetString(match, FC_FILE, 0, reinterpret_cast<FcChar8 **>(&file_name)) == FcResultMatch) {
  762. if (file_name) {
  763. String ret = String::utf8(file_name);
  764. FcPatternDestroy(match);
  765. FcPatternDestroy(pattern);
  766. return ret;
  767. }
  768. }
  769. FcPatternDestroy(match);
  770. }
  771. FcPatternDestroy(pattern);
  772. }
  773. }
  774. return String();
  775. #else
  776. ERR_FAIL_V_MSG(String(), "Godot was compiled without fontconfig, system font support is disabled.");
  777. #endif
  778. }
  779. String OS_LinuxBSD::get_config_path() const {
  780. if (has_environment("XDG_CONFIG_HOME")) {
  781. if (get_environment("XDG_CONFIG_HOME").is_absolute_path()) {
  782. return get_environment("XDG_CONFIG_HOME");
  783. } else {
  784. 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.");
  785. return has_environment("HOME") ? get_environment("HOME").path_join(".config") : ".";
  786. }
  787. } else if (has_environment("HOME")) {
  788. return get_environment("HOME").path_join(".config");
  789. } else {
  790. return ".";
  791. }
  792. }
  793. String OS_LinuxBSD::get_data_path() const {
  794. if (has_environment("XDG_DATA_HOME")) {
  795. if (get_environment("XDG_DATA_HOME").is_absolute_path()) {
  796. return get_environment("XDG_DATA_HOME");
  797. } else {
  798. 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.");
  799. return has_environment("HOME") ? get_environment("HOME").path_join(".local/share") : get_config_path();
  800. }
  801. } else if (has_environment("HOME")) {
  802. return get_environment("HOME").path_join(".local/share");
  803. } else {
  804. return get_config_path();
  805. }
  806. }
  807. String OS_LinuxBSD::get_cache_path() const {
  808. if (has_environment("XDG_CACHE_HOME")) {
  809. if (get_environment("XDG_CACHE_HOME").is_absolute_path()) {
  810. return get_environment("XDG_CACHE_HOME");
  811. } else {
  812. 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.");
  813. return has_environment("HOME") ? get_environment("HOME").path_join(".cache") : get_config_path();
  814. }
  815. } else if (has_environment("HOME")) {
  816. return get_environment("HOME").path_join(".cache");
  817. } else {
  818. return get_config_path();
  819. }
  820. }
  821. String OS_LinuxBSD::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  822. if (p_dir == SYSTEM_DIR_DESKTOP && !system_dir_desktop_cache.is_empty()) {
  823. return system_dir_desktop_cache;
  824. }
  825. String xdgparam;
  826. switch (p_dir) {
  827. case SYSTEM_DIR_DESKTOP: {
  828. xdgparam = "DESKTOP";
  829. } break;
  830. case SYSTEM_DIR_DCIM: {
  831. xdgparam = "PICTURES";
  832. } break;
  833. case SYSTEM_DIR_DOCUMENTS: {
  834. xdgparam = "DOCUMENTS";
  835. } break;
  836. case SYSTEM_DIR_DOWNLOADS: {
  837. xdgparam = "DOWNLOAD";
  838. } break;
  839. case SYSTEM_DIR_MOVIES: {
  840. xdgparam = "VIDEOS";
  841. } break;
  842. case SYSTEM_DIR_MUSIC: {
  843. xdgparam = "MUSIC";
  844. } break;
  845. case SYSTEM_DIR_PICTURES: {
  846. xdgparam = "PICTURES";
  847. } break;
  848. case SYSTEM_DIR_RINGTONES: {
  849. xdgparam = "MUSIC";
  850. } break;
  851. }
  852. String pipe;
  853. List<String> arg;
  854. arg.push_back(xdgparam);
  855. Error err = const_cast<OS_LinuxBSD *>(this)->execute("xdg-user-dir", arg, &pipe);
  856. if (err != OK) {
  857. return ".";
  858. }
  859. return pipe.strip_edges();
  860. }
  861. void OS_LinuxBSD::run() {
  862. if (!main_loop) {
  863. return;
  864. }
  865. main_loop->initialize();
  866. //uint64_t last_ticks=get_ticks_usec();
  867. //int frames=0;
  868. //uint64_t frame=0;
  869. while (true) {
  870. GodotProfileFrameMark;
  871. GodotProfileZone("OS_LinuxBSD::run");
  872. DisplayServer::get_singleton()->process_events(); // get rid of pending events
  873. #ifdef SDL_ENABLED
  874. if (joypad_sdl) {
  875. joypad_sdl->process_events();
  876. }
  877. #endif
  878. if (Main::iteration()) {
  879. break;
  880. }
  881. }
  882. main_loop->finalize();
  883. }
  884. void OS_LinuxBSD::disable_crash_handler() {
  885. crash_handler.disable();
  886. }
  887. bool OS_LinuxBSD::is_disable_crash_handler() const {
  888. return crash_handler.is_disabled();
  889. }
  890. static String get_mountpoint(const String &p_path) {
  891. struct stat s;
  892. if (stat(p_path.utf8().get_data(), &s)) {
  893. return "";
  894. }
  895. #if __has_include(<mntent.h>)
  896. dev_t dev = s.st_dev;
  897. FILE *fd = setmntent("/proc/mounts", "r");
  898. if (!fd) {
  899. return "";
  900. }
  901. struct mntent mnt;
  902. char buf[1024];
  903. size_t buflen = 1024;
  904. while (getmntent_r(fd, &mnt, buf, buflen)) {
  905. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  906. endmntent(fd);
  907. return String(mnt.mnt_dir);
  908. }
  909. }
  910. endmntent(fd);
  911. #endif
  912. return "";
  913. }
  914. Error OS_LinuxBSD::move_to_trash(const String &p_path) {
  915. // We try multiple methods, until we find one that works.
  916. // So we only return on success until we exhausted possibilities.
  917. String path = p_path.rstrip("/"); // Strip trailing slash when path points to a directory.
  918. int err_code;
  919. List<String> args;
  920. args.push_back(path);
  921. args.push_front("trash"); // The command is `gio trash <file_name>` so we add it before the path.
  922. Error result = execute("gio", args, nullptr, &err_code); // For GNOME based machines.
  923. if (result == OK && err_code == 0) { // Success.
  924. return OK;
  925. }
  926. args.pop_front();
  927. args.push_front("move");
  928. args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`.
  929. result = execute("kioclient5", args, nullptr, &err_code); // For KDE based machines.
  930. if (result == OK && err_code == 0) {
  931. return OK;
  932. }
  933. args.pop_front();
  934. args.pop_back();
  935. result = execute("gvfs-trash", args, nullptr, &err_code); // For older Linux machines.
  936. if (result == OK && err_code == 0) {
  937. return OK;
  938. }
  939. // If the commands `kioclient5`, `gio` or `gvfs-trash` don't work on the system we do it manually.
  940. String trash_path = "";
  941. String mnt = get_mountpoint(path);
  942. // If there is a directory "[Mountpoint]/.Trash-[UID], use it as the trash can.
  943. if (!mnt.is_empty()) {
  944. String mountpoint_trash_path(mnt + "/.Trash-" + itos(getuid()));
  945. struct stat s;
  946. if (!stat(mountpoint_trash_path.utf8().get_data(), &s)) {
  947. trash_path = mountpoint_trash_path;
  948. }
  949. }
  950. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash" as the trash can.
  951. if (trash_path.is_empty()) {
  952. char *dhome = getenv("XDG_DATA_HOME");
  953. if (dhome) {
  954. trash_path = String::utf8(dhome) + "/Trash";
  955. }
  956. }
  957. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash" as the trash can.
  958. if (trash_path.is_empty()) {
  959. char *home = getenv("HOME");
  960. if (home) {
  961. trash_path = String::utf8(home) + "/.local/share/Trash";
  962. }
  963. }
  964. // Issue an error if none of the previous locations is appropriate for the trash can.
  965. ERR_FAIL_COND_V_MSG(trash_path.is_empty(), FAILED, "Could not determine the trash can location");
  966. // Create needed directories for decided trash can location.
  967. {
  968. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  969. Error err = dir_access->make_dir_recursive(trash_path);
  970. // Issue an error if trash can is not created properly.
  971. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"");
  972. err = dir_access->make_dir_recursive(trash_path + "/files");
  973. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "/files\"");
  974. err = dir_access->make_dir_recursive(trash_path + "/info");
  975. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "/info\"");
  976. }
  977. // The trash can is successfully created, now we check that we don't exceed our file name length limit.
  978. // If the file name is too long trim it so we can add the identifying number and ".trashinfo".
  979. // Assumes that the file name length limit is 255 characters.
  980. String file_name = path.get_file();
  981. if (file_name.length() > 240) {
  982. file_name = file_name.substr(0, file_name.length() - 15);
  983. }
  984. String dest_path = trash_path + "/files/" + file_name;
  985. struct stat buff;
  986. int id_number = 0;
  987. String fn = file_name;
  988. // Checks if a resource with the same name already exist in the trash can,
  989. // if there is, add an identifying number to our resource's name.
  990. while (stat(dest_path.utf8().get_data(), &buff) == 0) {
  991. id_number++;
  992. // Added a limit to check for identically named files already on the trash can
  993. // if there are too many it could make the editor unresponsive.
  994. ERR_FAIL_COND_V_MSG(id_number > 99, FAILED, "Too many identically named resources already in the trash can.");
  995. fn = file_name + "." + itos(id_number);
  996. dest_path = trash_path + "/files/" + fn;
  997. }
  998. file_name = fn;
  999. String renamed_path = path.get_base_dir() + "/" + file_name;
  1000. // Generates the .trashinfo file
  1001. OS::DateTime dt = OS::get_singleton()->get_datetime(false);
  1002. String timestamp = vformat("%04d-%02d-%02dT%02d:%02d:", dt.year, (int)dt.month, dt.day, dt.hour, dt.minute);
  1003. timestamp = vformat("%s%02d", timestamp, dt.second); // vformat only supports up to 6 arguments.
  1004. String trash_info = "[Trash Info]\nPath=" + path.uri_encode() + "\nDeletionDate=" + timestamp + "\n";
  1005. {
  1006. Error err;
  1007. {
  1008. Ref<FileAccess> file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err);
  1009. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file: \"" + trash_path + "/info/" + file_name + ".trashinfo\"");
  1010. file->store_string(trash_info);
  1011. }
  1012. // Rename our resource before moving it to the trash can.
  1013. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1014. err = dir_access->rename(path, renamed_path);
  1015. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + path + "\" to \"" + renamed_path + "\"");
  1016. }
  1017. // Move the given resource to the trash can.
  1018. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  1019. List<String> mv_args;
  1020. mv_args.push_back(renamed_path);
  1021. mv_args.push_back(trash_path + "/files");
  1022. {
  1023. int retval;
  1024. Error err = execute("mv", mv_args, nullptr, &retval);
  1025. // Issue an error if "mv" failed to move the given resource to the trash can.
  1026. if (err != OK || retval != 0) {
  1027. ERR_PRINT("move_to_trash: Could not move the resource \"" + path + "\" to the trash can \"" + trash_path + "/files\"");
  1028. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1029. err = dir_access->rename(renamed_path, path);
  1030. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not rename \"" + renamed_path + "\" back to its original name: \"" + path + "\"");
  1031. return FAILED;
  1032. }
  1033. }
  1034. return OK;
  1035. }
  1036. String OS_LinuxBSD::get_system_ca_certificates() {
  1037. String certfile;
  1038. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1039. // Compile time preferred certificates path.
  1040. if (!String(_SYSTEM_CERTS_PATH).is_empty() && da->file_exists(_SYSTEM_CERTS_PATH)) {
  1041. certfile = _SYSTEM_CERTS_PATH;
  1042. } else if (da->file_exists("/etc/ssl/certs/ca-certificates.crt")) {
  1043. // Debian/Ubuntu
  1044. certfile = "/etc/ssl/certs/ca-certificates.crt";
  1045. } else if (da->file_exists("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem")) {
  1046. // Fedora
  1047. certfile = "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem";
  1048. } else if (da->file_exists("/etc/ca-certificates/extracted/tls-ca-bundle.pem")) {
  1049. // Arch Linux
  1050. certfile = "/etc/ca-certificates/extracted/tls-ca-bundle.pem";
  1051. } else if (da->file_exists("/var/lib/ca-certificates/ca-bundle.pem")) {
  1052. // openSUSE
  1053. certfile = "/var/lib/ca-certificates/ca-bundle.pem";
  1054. } else if (da->file_exists("/etc/ssl/cert.pem")) {
  1055. // FreeBSD/OpenBSD
  1056. certfile = "/etc/ssl/cert.pem";
  1057. }
  1058. if (certfile.is_empty()) {
  1059. return "";
  1060. }
  1061. Ref<FileAccess> f = FileAccess::open(certfile, FileAccess::READ);
  1062. ERR_FAIL_COND_V_MSG(f.is_null(), "", vformat("Failed to open system CA certificates file: '%s'", certfile));
  1063. return f->get_as_text();
  1064. }
  1065. #ifdef TOOLS_ENABLED
  1066. bool OS_LinuxBSD::_test_create_rendering_device(const String &p_display_driver) const {
  1067. // Tests Rendering Device creation.
  1068. bool ok = false;
  1069. #if defined(RD_ENABLED)
  1070. Error err;
  1071. RenderingContextDriver *rcd = nullptr;
  1072. #if defined(VULKAN_ENABLED)
  1073. #ifdef X11_ENABLED
  1074. if (p_display_driver == "x11" || p_display_driver.is_empty()) {
  1075. rcd = memnew(RenderingContextDriverVulkanX11);
  1076. }
  1077. #endif
  1078. #ifdef WAYLAND_ENABLED
  1079. if (p_display_driver == "wayland") {
  1080. rcd = memnew(RenderingContextDriverVulkanWayland);
  1081. }
  1082. #endif
  1083. #endif
  1084. if (rcd != nullptr) {
  1085. err = rcd->initialize();
  1086. if (err == OK) {
  1087. RenderingDevice *rd = memnew(RenderingDevice);
  1088. err = rd->initialize(rcd);
  1089. memdelete(rd);
  1090. rd = nullptr;
  1091. if (err == OK) {
  1092. ok = true;
  1093. }
  1094. }
  1095. memdelete(rcd);
  1096. rcd = nullptr;
  1097. }
  1098. #endif
  1099. return ok;
  1100. }
  1101. bool OS_LinuxBSD::_test_create_rendering_device_and_gl(const String &p_display_driver) const {
  1102. // Tests OpenGL context and Rendering Device simultaneous creation. This function is expected to crash on some drivers.
  1103. #ifdef GLES3_ENABLED
  1104. #ifdef X11_ENABLED
  1105. if (p_display_driver == "x11" || p_display_driver.is_empty()) {
  1106. #ifdef SOWRAP_ENABLED
  1107. if (initialize_xlib(0) != 0) {
  1108. return false;
  1109. }
  1110. #endif
  1111. DetectPrimeX11::create_context();
  1112. }
  1113. #endif
  1114. #ifdef WAYLAND_ENABLED
  1115. if (p_display_driver == "wayland") {
  1116. #ifdef SOWRAP_ENABLED
  1117. if (initialize_wayland_egl(0) != 0) {
  1118. return false;
  1119. }
  1120. #endif
  1121. DetectPrimeEGL::create_context(EGL_PLATFORM_WAYLAND_KHR);
  1122. }
  1123. #endif
  1124. RasterizerGLES3::make_current(true);
  1125. #endif
  1126. return _test_create_rendering_device(p_display_driver);
  1127. }
  1128. #endif
  1129. OS_LinuxBSD::OS_LinuxBSD() {
  1130. main_loop = nullptr;
  1131. #ifdef PULSEAUDIO_ENABLED
  1132. AudioDriverManager::add_driver(&driver_pulseaudio);
  1133. #endif
  1134. #ifdef ALSA_ENABLED
  1135. AudioDriverManager::add_driver(&driver_alsa);
  1136. #endif
  1137. #ifdef X11_ENABLED
  1138. DisplayServerX11::register_x11_driver();
  1139. #endif
  1140. #ifdef WAYLAND_ENABLED
  1141. DisplayServerWayland::register_wayland_driver();
  1142. #endif
  1143. #ifdef FONTCONFIG_ENABLED
  1144. #ifdef SOWRAP_ENABLED
  1145. #ifdef DEBUG_ENABLED
  1146. int dylibloader_verbose = 1;
  1147. #else
  1148. int dylibloader_verbose = 0;
  1149. #endif
  1150. font_config_initialized = (initialize_fontconfig(dylibloader_verbose) == 0);
  1151. #else
  1152. font_config_initialized = true;
  1153. #endif
  1154. if (font_config_initialized) {
  1155. bool ver_ok = false;
  1156. int version = FcGetVersion();
  1157. ver_ok = ((version / 100 / 100) == 2 && (version / 100 % 100) >= 11) || ((version / 100 / 100) > 2); // 2.11.0
  1158. print_verbose(vformat("FontConfig %d.%d.%d detected.", version / 100 / 100, version / 100 % 100, version % 100));
  1159. if (!ver_ok) {
  1160. font_config_initialized = false;
  1161. }
  1162. }
  1163. if (font_config_initialized) {
  1164. config = FcInitLoadConfigAndFonts();
  1165. if (!config) {
  1166. font_config_initialized = false;
  1167. }
  1168. object_set = FcObjectSetBuild(FC_FAMILY, FC_FILE, nullptr);
  1169. if (!object_set) {
  1170. font_config_initialized = false;
  1171. }
  1172. }
  1173. #endif // FONTCONFIG_ENABLED
  1174. }
  1175. OS_LinuxBSD::~OS_LinuxBSD() {
  1176. #ifdef FONTCONFIG_ENABLED
  1177. if (object_set) {
  1178. FcObjectSetDestroy(object_set);
  1179. }
  1180. if (config) {
  1181. FcConfigDestroy(config);
  1182. }
  1183. #endif // FONTCONFIG_ENABLED
  1184. }