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