Debian Codegraph @

platform/windows/os_windows.h

raw symbols SCIP
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/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 Vector<String> _get_video_adapter_driver_info_reg(const String &p_name) const;
149 Vector<String> _get_video_adapter_driver_info_wmi(const String &p_name) const;
150
151 // functions used by main to initialize/deinitialize the OS
152 protected:
153 virtual void initialize() override;
154
155 virtual void set_main_loop(MainLoop *p_main_loop) override;
156 virtual void delete_main_loop() override;
157
158 virtual void finalize() override;
159 virtual void finalize_core() override;
160
161 virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
162 virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
163 virtual StdHandleType get_stdin_type() const override;
164 virtual StdHandleType get_stdout_type() const override;
165 virtual StdHandleType get_stderr_type() const override;
166
167 String _quote_command_line_argument(const String &p_text) const;
168
169 struct ProcessInfo {
170 STARTUPINFOEX si;
171 PROCESS_INFORMATION pi;
172 mutable bool is_running = true;
173 mutable int exit_code = -1;
174 };
175 HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
176 Mutex process_map_mutex;
177
178 public:
179 virtual void alert(const String &p_alert, const String &p_title = "ALERT!") override;
180
181 virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
182
183 virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
184 virtual Error close_dynamic_library(void *p_library_handle) override;
185 virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
186
187 virtual MainLoop *get_main_loop() const override;
188
189 virtual String get_name() const override;
190 virtual String get_distribution_name() const override;
191 virtual String get_version() const override;
192 virtual String get_version_alias() const override;
193
194 virtual Vector<String> get_video_adapter_driver_info() const override;
195 virtual bool get_user_prefers_integrated_gpu() const override;
196
197 virtual void initialize_joypads() override {}
198
199 virtual DateTime get_datetime(bool p_utc) const override;
200 virtual TimeZoneInfo get_time_zone_info() const override;
201 virtual double get_unix_time() const override;
202
203 virtual Error set_cwd(const String &p_cwd) override;
204 virtual String get_cwd() const override;
205
206 virtual void add_frame_delay(bool p_can_draw, bool p_wake_for_events) override;
207 virtual void delay_usec(uint32_t p_usec) const override;
208 virtual uint64_t get_ticks_usec() const override;
209
210 virtual Dictionary get_memory_info() const override;
211
212 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;
213 virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) override;
214 virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
215 virtual Error kill(const ProcessID &p_pid) override;
216 virtual int get_process_id() const override;
217 virtual bool is_process_running(const ProcessID &p_pid) const override;
218 virtual int get_process_exit_code(const ProcessID &p_pid) const override;
219
220 virtual bool has_environment(const String &p_var) const override;
221 virtual String get_environment(const String &p_var) const override;
222 virtual void set_environment(const String &p_var, const String &p_value) const override;
223 virtual void unset_environment(const String &p_var) const override;
224
225 virtual Vector<String> get_system_fonts() const override;
226 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;
227 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;
228
229 virtual String get_executable_path() const override;
230
231 virtual String get_locale() const override;
232
233 virtual String get_processor_name() const override;
234
235 virtual String get_model_name() const override;
236
237 virtual uint64_t get_embedded_pck_offset() const override;
238
239 virtual String get_config_path() const override;
240 virtual String get_data_path() const override;
241 virtual String get_cache_path() const override;
242 virtual String get_temp_path() const override;
243 virtual String get_godot_dir_name() const override;
244
245 virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;
246 virtual String get_user_data_dir(const String &p_user_dir) const override;
247
248 virtual String get_unique_id() const override;
249
250 virtual Error shell_open(const String &p_uri) override;
251 virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder) override;
252
253 void run();
254
255 virtual bool _check_internal_feature_support(const String &p_feature) override;
256
257 virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const override;
258 virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const override;
259
260 virtual void disable_crash_handler() override;
261 virtual bool is_disable_crash_handler() const override;
262 virtual void initialize_debugging() override;
263
264 virtual Error move_to_trash(const String &p_path) override;
265
266 virtual String get_system_ca_certificates() override;
267
268 void set_main_window(HWND p_main_window) { main_window = p_main_window; }
269
270 #ifdef TOOLS_ENABLED
271 virtual bool _test_create_rendering_device_and_gl(const String &p_display_driver) const override;
272 virtual bool _test_create_rendering_device(const String &p_display_driver) const override;
273 #endif
274
275 HINSTANCE get_hinstance() { return hInstance; }
276 OS_Windows(HINSTANCE _hInstance);
277 ~OS_Windows();
278 };