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