os_linuxbsd.cpp 41 KB

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