Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/os_windows.h
10277 views
1
/**************************************************************************/
2
/* os_windows.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 "crash_handler_windows.h"
34
#include "key_mapping_windows.h"
35
36
#include "core/config/project_settings.h"
37
#include "core/input/input.h"
38
#include "core/os/os.h"
39
#include "drivers/wasapi/audio_driver_wasapi.h"
40
#include "drivers/winmidi/midi_driver_winmidi.h"
41
#include "servers/audio_server.h"
42
43
#ifdef XAUDIO2_ENABLED
44
#include "drivers/xaudio2/audio_driver_xaudio2.h"
45
#endif
46
47
#if defined(RD_ENABLED)
48
#include "servers/rendering/rendering_device.h"
49
#endif
50
51
#include <io.h>
52
#include <shellapi.h>
53
#include <cstdio>
54
55
#define WIN32_LEAN_AND_MEAN
56
#include <dwrite.h>
57
#include <dwrite_2.h>
58
#include <windows.h>
59
#include <windowsx.h>
60
61
#ifdef DEBUG_ENABLED
62
// forward error messages to OutputDebugString
63
#define WINDOWS_DEBUG_OUTPUT_ENABLED
64
#endif
65
66
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
67
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
68
#endif
69
70
#ifndef SAFE_RELEASE // when Windows Media Device M? is not present
71
#define SAFE_RELEASE(x) \
72
if (x != nullptr) { \
73
x->Release(); \
74
x = nullptr; \
75
}
76
#endif
77
78
template <typename T>
79
class ComAutoreleaseRef {
80
public:
81
T *reference = nullptr;
82
83
_FORCE_INLINE_ T *operator->() { return reference; }
84
_FORCE_INLINE_ const T *operator->() const { return reference; }
85
_FORCE_INLINE_ T *operator*() { return reference; }
86
_FORCE_INLINE_ const T *operator*() const { return reference; }
87
_FORCE_INLINE_ bool is_valid() const { return reference != nullptr; }
88
_FORCE_INLINE_ bool is_null() const { return reference == nullptr; }
89
ComAutoreleaseRef() {}
90
ComAutoreleaseRef(T *p_ref) {
91
reference = p_ref;
92
}
93
~ComAutoreleaseRef() {
94
if (reference != nullptr) {
95
reference->Release();
96
reference = nullptr;
97
}
98
}
99
};
100
101
class OS_Windows : public OS {
102
uint64_t target_ticks = 0;
103
uint64_t ticks_start = 0;
104
uint64_t ticks_per_second = 0;
105
uint64_t delay_resolution = 1000;
106
107
HINSTANCE hInstance;
108
MainLoop *main_loop = nullptr;
109
110
#ifdef WASAPI_ENABLED
111
AudioDriverWASAPI driver_wasapi;
112
#endif
113
#ifdef XAUDIO2_ENABLED
114
AudioDriverXAudio2 driver_xaudio2;
115
#endif
116
#ifdef WINMIDI_ENABLED
117
MIDIDriverWinMidi driver_midi;
118
#endif
119
120
CrashHandler crash_handler;
121
122
#ifdef WINDOWS_DEBUG_OUTPUT_ENABLED
123
ErrorHandlerList error_handlers;
124
#endif
125
126
HWND main_window;
127
128
IDWriteFactory *dwrite_factory = nullptr;
129
IDWriteFactory2 *dwrite_factory2 = nullptr;
130
IDWriteFontCollection *font_collection = nullptr;
131
IDWriteFontFallback *system_font_fallback = nullptr;
132
133
bool dwrite_init = false;
134
bool dwrite2_init = false;
135
136
HashMap<void *, String> temp_libraries;
137
138
void _remove_temp_library(void *p_library_handle);
139
String _get_default_fontname(const String &p_font_name) const;
140
DWRITE_FONT_WEIGHT _weight_to_dw(int p_weight) const;
141
DWRITE_FONT_STRETCH _stretch_to_dw(int p_stretch) const;
142
143
bool is_using_con_wrapper() const;
144
145
HashMap<String, int> encodings;
146
void _init_encodings();
147
148
// functions used by main to initialize/deinitialize the OS
149
protected:
150
virtual void initialize() override;
151
152
virtual void set_main_loop(MainLoop *p_main_loop) override;
153
virtual void delete_main_loop() override;
154
155
virtual void finalize() override;
156
virtual void finalize_core() override;
157
158
virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
159
virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
160
virtual StdHandleType get_stdin_type() const override;
161
virtual StdHandleType get_stdout_type() const override;
162
virtual StdHandleType get_stderr_type() const override;
163
164
String _quote_command_line_argument(const String &p_text) const;
165
166
struct ProcessInfo {
167
STARTUPINFOEX si;
168
PROCESS_INFORMATION pi;
169
mutable bool is_running = true;
170
mutable int exit_code = -1;
171
};
172
HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
173
Mutex process_map_mutex;
174
175
public:
176
virtual void alert(const String &p_alert, const String &p_title = "ALERT!") override;
177
178
virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
179
180
virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
181
virtual Error close_dynamic_library(void *p_library_handle) override;
182
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
183
184
virtual MainLoop *get_main_loop() const override;
185
186
virtual String get_name() const override;
187
virtual String get_distribution_name() const override;
188
virtual String get_version() const override;
189
virtual String get_version_alias() const override;
190
191
virtual Vector<String> get_video_adapter_driver_info() const override;
192
virtual bool get_user_prefers_integrated_gpu() const override;
193
194
virtual void initialize_joypads() override {}
195
196
virtual DateTime get_datetime(bool p_utc) const override;
197
virtual TimeZoneInfo get_time_zone_info() const override;
198
virtual double get_unix_time() const override;
199
200
virtual Error set_cwd(const String &p_cwd) override;
201
202
virtual void add_frame_delay(bool p_can_draw, bool p_wake_for_events) override;
203
virtual void delay_usec(uint32_t p_usec) const override;
204
virtual uint64_t get_ticks_usec() const override;
205
206
virtual Dictionary get_memory_info() const override;
207
208
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) override;
209
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) override;
210
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
211
virtual Error kill(const ProcessID &p_pid) override;
212
virtual int get_process_id() const override;
213
virtual bool is_process_running(const ProcessID &p_pid) const override;
214
virtual int get_process_exit_code(const ProcessID &p_pid) const override;
215
216
virtual bool has_environment(const String &p_var) const override;
217
virtual String get_environment(const String &p_var) const override;
218
virtual void set_environment(const String &p_var, const String &p_value) const override;
219
virtual void unset_environment(const String &p_var) const override;
220
221
virtual Vector<String> get_system_fonts() const override;
222
virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const override;
223
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 override;
224
225
virtual String get_executable_path() const override;
226
227
virtual String get_locale() const override;
228
229
virtual String get_processor_name() const override;
230
231
virtual String get_model_name() const override;
232
233
virtual uint64_t get_embedded_pck_offset() const override;
234
235
virtual String get_config_path() const override;
236
virtual String get_data_path() const override;
237
virtual String get_cache_path() const override;
238
virtual String get_temp_path() const override;
239
virtual String get_godot_dir_name() const override;
240
241
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;
242
virtual String get_user_data_dir(const String &p_user_dir) const override;
243
244
virtual String get_unique_id() const override;
245
246
virtual Error shell_open(const String &p_uri) override;
247
virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder) override;
248
249
void run();
250
251
virtual bool _check_internal_feature_support(const String &p_feature) override;
252
253
virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const override;
254
virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const override;
255
256
virtual void disable_crash_handler() override;
257
virtual bool is_disable_crash_handler() const override;
258
virtual void initialize_debugging() override;
259
260
virtual Error move_to_trash(const String &p_path) override;
261
262
virtual String get_system_ca_certificates() override;
263
264
void set_main_window(HWND p_main_window) { main_window = p_main_window; }
265
266
#ifdef TOOLS_ENABLED
267
virtual bool _test_create_rendering_device_and_gl(const String &p_display_driver) const override;
268
virtual bool _test_create_rendering_device(const String &p_display_driver) const override;
269
#endif
270
271
HINSTANCE get_hinstance() { return hInstance; }
272
OS_Windows(HINSTANCE _hInstance);
273
~OS_Windows();
274
};
275
276