Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/os/os.h
21026 views
1
/**************************************************************************/
2
/* os.h */
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
#pragma once
32
33
#include "core/config/engine.h"
34
#include "core/io/logger.h"
35
#include "core/io/remote_filesystem_client.h"
36
#include "core/os/time_enums.h"
37
#include "core/string/ustring.h"
38
#include "core/templates/list.h"
39
#include "core/templates/vector.h"
40
41
#include <cstdlib>
42
43
class MainLoop;
44
45
class OS {
46
public:
47
typedef void (*ImeCallback)(void *p_inp, const String &p_text, Point2 p_selection);
48
typedef bool (*HasServerFeatureCallback)(const String &p_feature);
49
50
enum RenderThreadMode {
51
RENDER_THREAD_UNSAFE,
52
RENDER_THREAD_SAFE,
53
RENDER_SEPARATE_THREAD,
54
};
55
56
enum StdHandleType {
57
STD_HANDLE_INVALID,
58
STD_HANDLE_CONSOLE,
59
STD_HANDLE_FILE,
60
STD_HANDLE_PIPE,
61
STD_HANDLE_UNKNOWN,
62
};
63
64
enum RenderingSource {
65
RENDERING_SOURCE_DEFAULT,
66
RENDERING_SOURCE_PROJECT_SETTING,
67
RENDERING_SOURCE_COMMANDLINE,
68
RENDERING_SOURCE_FALLBACK
69
};
70
71
private:
72
static OS *singleton;
73
static uint64_t target_ticks;
74
String _execpath;
75
List<String> _cmdline;
76
List<String> _user_args;
77
bool _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0.
78
bool low_processor_usage_mode = false;
79
int low_processor_usage_mode_sleep_usec = 10000;
80
bool _delta_smoothing_enabled = false;
81
bool _verbose_stdout = false;
82
bool _debug_stdout = false;
83
String _local_clipboard;
84
// Assume success by default, all failure cases need to set EXIT_FAILURE explicitly.
85
int _exit_code = EXIT_SUCCESS;
86
bool _allow_hidpi = false;
87
bool _allow_layered = false;
88
bool _stdout_enabled = true;
89
bool _stderr_enabled = true;
90
bool _writing_movie = false;
91
bool _in_editor = false;
92
bool _embedded_in_editor = false;
93
94
CompositeLogger *_logger = nullptr;
95
96
bool restart_on_exit = false;
97
List<String> restart_commandline;
98
99
String _current_rendering_driver_name;
100
RenderingSource _current_rendering_driver_name_source = RENDERING_SOURCE_DEFAULT;
101
String _current_rendering_method;
102
RenderingSource _current_rendering_method_source = RENDERING_SOURCE_DEFAULT;
103
bool _is_gles_over_gl = false;
104
105
RemoteFilesystemClient default_rfs;
106
107
// For tracking benchmark data
108
bool use_benchmark = false;
109
String benchmark_file;
110
HashMap<Pair<String, String>, uint64_t> benchmark_marks_from;
111
HashMap<Pair<String, String>, double> benchmark_marks_final;
112
113
protected:
114
void _set_logger(CompositeLogger *p_logger);
115
116
friend class Main;
117
// Needed by tests to setup command-line args.
118
friend int test_main(int argc, char *argv[]);
119
120
HasServerFeatureCallback has_server_feature_callback = nullptr;
121
bool _separate_thread_render = false;
122
bool _silent_crash_handler = false;
123
124
// Functions used by Main to initialize/deinitialize the OS.
125
126
virtual void initialize() = 0;
127
virtual void initialize_joypads() = 0;
128
129
virtual void set_main_loop(MainLoop *p_main_loop) = 0;
130
virtual void delete_main_loop() = 0;
131
132
virtual void finalize() = 0;
133
virtual void finalize_core() = 0;
134
135
virtual void set_cmdline(const char *p_execpath, const List<String> &p_args, const List<String> &p_user_args);
136
137
virtual bool _check_internal_feature_support(const String &p_feature) = 0;
138
139
public:
140
typedef int64_t ProcessID;
141
142
static OS *get_singleton();
143
144
static bool prefer_meta_over_ctrl();
145
146
void set_current_rendering_driver_name(const String &p_driver_name, RenderingSource p_source) {
147
_current_rendering_driver_name = p_driver_name;
148
_current_rendering_driver_name_source = p_source;
149
}
150
void set_current_rendering_method(const String &p_name, RenderingSource p_source) {
151
_current_rendering_method = p_name;
152
_current_rendering_method_source = p_source;
153
}
154
void set_gles_over_gl(bool p_enabled) { _is_gles_over_gl = p_enabled; }
155
156
String get_current_rendering_driver_name() const { return _current_rendering_driver_name; }
157
String get_current_rendering_method() const { return _current_rendering_method; }
158
RenderingSource get_current_rendering_driver_name_source() const { return _current_rendering_driver_name_source; }
159
RenderingSource get_current_rendering_method_source() const { return _current_rendering_method_source; }
160
bool get_gles_over_gl() const { return _is_gles_over_gl; }
161
162
virtual Vector<String> get_video_adapter_driver_info() const = 0;
163
virtual bool get_user_prefers_integrated_gpu() const { return false; }
164
165
void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, Logger::ErrorType p_type = Logger::ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {});
166
void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
167
void print_rich(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
168
void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
169
170
virtual String get_stdin_string(int64_t p_buffer_size = 1024) = 0;
171
virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) = 0;
172
173
virtual StdHandleType get_stdin_type() const { return STD_HANDLE_UNKNOWN; }
174
virtual StdHandleType get_stdout_type() const { return STD_HANDLE_UNKNOWN; }
175
virtual StdHandleType get_stderr_type() const { return STD_HANDLE_UNKNOWN; }
176
177
virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) = 0; // Should return cryptographically-safe random bytes.
178
virtual String get_system_ca_certificates() { return ""; } // Concatenated certificates in PEM format.
179
180
virtual PackedStringArray get_connected_midi_inputs();
181
virtual void open_midi_inputs();
182
virtual void close_midi_inputs();
183
184
virtual Rect2 calculate_boot_screen_rect(const Size2 &p_window_size, const Size2 &p_imgrect_size) const;
185
186
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
187
188
struct GDExtensionData {
189
bool also_set_library_path = false;
190
String *r_resolved_path = nullptr;
191
bool generate_temp_files = false;
192
PackedStringArray *library_dependencies = nullptr;
193
};
194
195
virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) { return ERR_UNAVAILABLE; }
196
virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; }
197
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) { return ERR_UNAVAILABLE; }
198
199
virtual void set_low_processor_usage_mode(bool p_enabled);
200
virtual bool is_in_low_processor_usage_mode() const;
201
virtual void set_low_processor_usage_mode_sleep_usec(int p_usec);
202
virtual int get_low_processor_usage_mode_sleep_usec() const;
203
204
void set_delta_smoothing(bool p_enabled);
205
bool is_delta_smoothing_enabled() const;
206
207
virtual Vector<String> get_system_fonts() const { return Vector<String>(); }
208
virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return String(); }
209
virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); }
210
virtual String get_executable_path() const;
211
virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) = 0;
212
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) { return Dictionary(); }
213
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) = 0;
214
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); }
215
virtual Error open_with_program(const String &p_program_path, const List<String> &p_paths) { return create_process(p_program_path, p_paths); }
216
virtual Error kill(const ProcessID &p_pid) = 0;
217
virtual int get_process_id() const;
218
virtual bool is_process_running(const ProcessID &p_pid) const = 0;
219
virtual int get_process_exit_code(const ProcessID &p_pid) const = 0;
220
virtual void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0) {}
221
222
virtual Error shell_open(const String &p_uri);
223
virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder = true);
224
virtual Error set_cwd(const String &p_cwd);
225
virtual String get_cwd() const;
226
227
virtual bool has_environment(const String &p_var) const = 0;
228
virtual String get_environment(const String &p_var) const = 0;
229
virtual void set_environment(const String &p_var, const String &p_value) const = 0;
230
virtual void unset_environment(const String &p_var) const = 0;
231
virtual void load_shell_environment() const {}
232
233
virtual String get_name() const = 0;
234
virtual String get_identifier() const;
235
virtual String get_distribution_name() const = 0;
236
virtual String get_version() const = 0;
237
virtual String get_version_alias() const { return get_version(); }
238
virtual List<String> get_cmdline_args() const { return _cmdline; }
239
virtual List<String> get_cmdline_user_args() const { return _user_args; }
240
virtual List<String> get_cmdline_platform_args() const { return List<String>(); }
241
virtual String get_model_name() const;
242
243
bool is_layered_allowed() const { return _allow_layered; }
244
bool is_hidpi_allowed() const { return _allow_hidpi; }
245
246
void ensure_user_data_dir();
247
248
// NOTE: MainLoop is forward-declared in OS and should be included to use this.
249
virtual MainLoop *get_main_loop() const = 0;
250
251
virtual void yield();
252
253
struct DateTime {
254
int64_t year;
255
Month month;
256
uint8_t day;
257
Weekday weekday;
258
uint8_t hour;
259
uint8_t minute;
260
uint8_t second;
261
bool dst;
262
};
263
264
struct TimeZoneInfo {
265
int bias;
266
String name;
267
};
268
269
virtual DateTime get_datetime(bool utc = false) const = 0;
270
virtual TimeZoneInfo get_time_zone_info() const = 0;
271
virtual double get_unix_time() const;
272
273
virtual void delay_usec(uint32_t p_usec) const = 0;
274
virtual void add_frame_delay(bool p_can_draw, bool p_wake_for_events);
275
virtual uint64_t get_frame_delay(bool p_can_draw) const;
276
277
virtual uint64_t get_ticks_usec() const = 0;
278
uint64_t get_ticks_msec() const;
279
280
virtual bool is_userfs_persistent() const { return true; }
281
282
bool is_stdout_verbose() const;
283
bool is_stdout_debug_enabled() const;
284
285
bool is_stdout_enabled() const;
286
bool is_stderr_enabled() const;
287
void set_stdout_enabled(bool p_enabled);
288
void set_stderr_enabled(bool p_enabled);
289
290
virtual void set_crash_handler_silent() { _silent_crash_handler = true; }
291
virtual bool is_crash_handler_silent() { return _silent_crash_handler; }
292
293
virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const;
294
virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const;
295
296
virtual void disable_crash_handler() {}
297
virtual bool is_disable_crash_handler() const { return false; }
298
virtual void initialize_debugging() {}
299
300
virtual uint64_t get_static_memory_usage() const;
301
virtual uint64_t get_static_memory_peak_usage() const;
302
virtual Dictionary get_memory_info() const;
303
304
bool is_separate_thread_rendering_enabled() const { return _separate_thread_render; }
305
306
virtual String get_locale() const;
307
String get_locale_language() const;
308
309
virtual uint64_t get_embedded_pck_offset() const;
310
311
String get_safe_dir_name(const String &p_dir_name, bool p_allow_paths = false) const;
312
virtual String get_godot_dir_name() const;
313
314
virtual String get_data_path() const;
315
virtual String get_config_path() const;
316
virtual String get_cache_path() const;
317
virtual String get_temp_path() const;
318
virtual String get_bundle_resource_dir() const;
319
virtual String get_bundle_icon_path() const;
320
virtual String get_bundle_icon_name() const;
321
322
virtual String get_user_data_dir(const String &p_user_dir) const;
323
virtual String get_user_data_dir() const;
324
virtual String get_resource_dir() const;
325
326
enum SystemDir {
327
SYSTEM_DIR_DESKTOP,
328
SYSTEM_DIR_DCIM,
329
SYSTEM_DIR_DOCUMENTS,
330
SYSTEM_DIR_DOWNLOADS,
331
SYSTEM_DIR_MOVIES,
332
SYSTEM_DIR_MUSIC,
333
SYSTEM_DIR_PICTURES,
334
SYSTEM_DIR_RINGTONES,
335
};
336
337
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
338
339
virtual Error move_to_trash(const String &p_path) { return FAILED; }
340
341
void create_lock_file();
342
void remove_lock_file();
343
344
virtual int get_exit_code() const;
345
// `set_exit_code` should only be used from `SceneTree` (or from a similar
346
// level, e.g. from the `Main::start` if leaving without creating a `SceneTree`).
347
// For other components, `SceneTree.quit()` should be used instead.
348
virtual void set_exit_code(int p_code);
349
350
virtual int get_processor_count() const;
351
virtual String get_processor_name() const;
352
virtual int get_default_thread_pool_size() const { return get_processor_count(); }
353
354
virtual String get_unique_id() const;
355
356
bool has_feature(const String &p_feature);
357
358
virtual bool is_sandboxed() const;
359
360
void set_has_server_feature_callback(HasServerFeatureCallback p_callback);
361
362
void set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments);
363
bool is_restart_on_exit_set() const;
364
List<String> get_restart_on_exit_arguments() const;
365
366
virtual bool request_permission(const String &p_name) { return true; }
367
virtual bool request_permissions() { return true; }
368
virtual Vector<String> get_granted_permissions() const { return Vector<String>(); }
369
virtual void revoke_granted_permissions() {}
370
371
// For recording / measuring benchmark data. Only enabled with tools
372
void set_use_benchmark(bool p_use_benchmark);
373
bool is_use_benchmark_set();
374
void set_benchmark_file(const String &p_benchmark_file);
375
String get_benchmark_file();
376
virtual void benchmark_begin_measure(const String &p_context, const String &p_what);
377
virtual void benchmark_end_measure(const String &p_context, const String &p_what);
378
virtual void benchmark_dump();
379
380
virtual Error setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path);
381
382
void add_logger(Logger *p_logger);
383
384
enum PreferredTextureFormat {
385
PREFERRED_TEXTURE_FORMAT_S3TC_BPTC,
386
PREFERRED_TEXTURE_FORMAT_ETC2_ASTC
387
};
388
389
virtual PreferredTextureFormat get_preferred_texture_format() const;
390
391
// Load GDExtensions specific to this platform.
392
// This is invoked by the GDExtensionManager after loading GDExtensions specified by the project.
393
virtual void load_platform_gdextensions() const {}
394
395
#ifdef TOOLS_ENABLED
396
// Tests OpenGL context and Rendering Device simultaneous creation. This function is expected to crash on some NVIDIA drivers.
397
virtual bool _test_create_rendering_device_and_gl(const String &p_display_driver) const { return true; }
398
virtual bool _test_create_rendering_device(const String &p_display_driver) const { return true; }
399
#endif
400
401
OS();
402
virtual ~OS();
403
};
404
405