os_windows.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #pragma once
  31. #include "crash_handler_windows.h"
  32. #include "key_mapping_windows.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/input/input.h"
  35. #include "core/os/os.h"
  36. #include "drivers/wasapi/audio_driver_wasapi.h"
  37. #include "drivers/winmidi/midi_driver_winmidi.h"
  38. #include "servers/audio_server.h"
  39. #ifdef XAUDIO2_ENABLED
  40. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  41. #endif
  42. #if defined(RD_ENABLED)
  43. #include "servers/rendering/rendering_device.h"
  44. #endif
  45. #include <io.h>
  46. #include <shellapi.h>
  47. #include <cstdio>
  48. #define WIN32_LEAN_AND_MEAN
  49. #include <dwrite.h>
  50. #include <dwrite_2.h>
  51. #include <windows.h>
  52. #include <windowsx.h>
  53. #ifdef DEBUG_ENABLED
  54. // forward error messages to OutputDebugString
  55. #define WINDOWS_DEBUG_OUTPUT_ENABLED
  56. #endif
  57. #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
  58. #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
  59. #endif
  60. template <typename T>
  61. class ComAutoreleaseRef {
  62. public:
  63. T *reference = nullptr;
  64. _FORCE_INLINE_ T *operator->() { return reference; }
  65. _FORCE_INLINE_ const T *operator->() const { return reference; }
  66. _FORCE_INLINE_ T *operator*() { return reference; }
  67. _FORCE_INLINE_ const T *operator*() const { return reference; }
  68. _FORCE_INLINE_ bool is_valid() const { return reference != nullptr; }
  69. _FORCE_INLINE_ bool is_null() const { return reference == nullptr; }
  70. ComAutoreleaseRef() {}
  71. ComAutoreleaseRef(T *p_ref) {
  72. reference = p_ref;
  73. }
  74. ~ComAutoreleaseRef() {
  75. if (reference != nullptr) {
  76. reference->Release();
  77. reference = nullptr;
  78. }
  79. }
  80. };
  81. class JoypadWindows;
  82. class OS_Windows : public OS {
  83. uint64_t target_ticks = 0;
  84. uint64_t ticks_start = 0;
  85. uint64_t ticks_per_second = 0;
  86. uint64_t delay_resolution = 1000;
  87. HINSTANCE hInstance;
  88. MainLoop *main_loop = nullptr;
  89. #ifdef WASAPI_ENABLED
  90. AudioDriverWASAPI driver_wasapi;
  91. #endif
  92. #ifdef XAUDIO2_ENABLED
  93. AudioDriverXAudio2 driver_xaudio2;
  94. #endif
  95. #ifdef WINMIDI_ENABLED
  96. MIDIDriverWinMidi driver_midi;
  97. #endif
  98. CrashHandler crash_handler;
  99. #ifdef WINDOWS_DEBUG_OUTPUT_ENABLED
  100. ErrorHandlerList error_handlers;
  101. #endif
  102. HWND main_window;
  103. IDWriteFactory *dwrite_factory = nullptr;
  104. IDWriteFactory2 *dwrite_factory2 = nullptr;
  105. IDWriteFontCollection *font_collection = nullptr;
  106. IDWriteFontFallback *system_font_fallback = nullptr;
  107. bool dwrite_init = false;
  108. bool dwrite2_init = false;
  109. HashMap<void *, String> temp_libraries;
  110. void _remove_temp_library(void *p_library_handle);
  111. String _get_default_fontname(const String &p_font_name) const;
  112. DWRITE_FONT_WEIGHT _weight_to_dw(int p_weight) const;
  113. DWRITE_FONT_STRETCH _stretch_to_dw(int p_stretch) const;
  114. bool is_using_con_wrapper() const;
  115. HashMap<String, int> encodings;
  116. void _init_encodings();
  117. // functions used by main to initialize/deinitialize the OS
  118. protected:
  119. virtual void initialize() override;
  120. virtual void set_main_loop(MainLoop *p_main_loop) override;
  121. virtual void delete_main_loop() override;
  122. virtual void finalize() override;
  123. virtual void finalize_core() override;
  124. virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
  125. virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
  126. virtual StdHandleType get_stdin_type() const override;
  127. virtual StdHandleType get_stdout_type() const override;
  128. virtual StdHandleType get_stderr_type() const override;
  129. String _quote_command_line_argument(const String &p_text) const;
  130. struct ProcessInfo {
  131. STARTUPINFOEX si;
  132. PROCESS_INFORMATION pi;
  133. mutable bool is_running = true;
  134. mutable int exit_code = -1;
  135. };
  136. HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
  137. Mutex process_map_mutex;
  138. public:
  139. virtual void alert(const String &p_alert, const String &p_title = "ALERT!") override;
  140. virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
  141. virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
  142. virtual Error close_dynamic_library(void *p_library_handle) override;
  143. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
  144. virtual MainLoop *get_main_loop() const override;
  145. virtual String get_name() const override;
  146. virtual String get_distribution_name() const override;
  147. virtual String get_version() const override;
  148. virtual String get_version_alias() const override;
  149. virtual Vector<String> get_video_adapter_driver_info() const override;
  150. virtual bool get_user_prefers_integrated_gpu() const override;
  151. virtual void initialize_joypads() override {}
  152. virtual DateTime get_datetime(bool p_utc) const override;
  153. virtual TimeZoneInfo get_time_zone_info() const override;
  154. virtual double get_unix_time() const override;
  155. virtual Error set_cwd(const String &p_cwd) override;
  156. virtual void add_frame_delay(bool p_can_draw) override;
  157. virtual void delay_usec(uint32_t p_usec) const override;
  158. virtual uint64_t get_ticks_usec() const override;
  159. virtual Dictionary get_memory_info() const override;
  160. 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;
  161. virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) override;
  162. virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
  163. virtual Error kill(const ProcessID &p_pid) override;
  164. virtual int get_process_id() const override;
  165. virtual bool is_process_running(const ProcessID &p_pid) const override;
  166. virtual int get_process_exit_code(const ProcessID &p_pid) const override;
  167. virtual bool has_environment(const String &p_var) const override;
  168. virtual String get_environment(const String &p_var) const override;
  169. virtual void set_environment(const String &p_var, const String &p_value) const override;
  170. virtual void unset_environment(const String &p_var) const override;
  171. virtual Vector<String> get_system_fonts() const override;
  172. 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;
  173. 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;
  174. virtual String get_executable_path() const override;
  175. virtual String get_locale() const override;
  176. virtual String get_processor_name() const override;
  177. virtual String get_model_name() const override;
  178. virtual uint64_t get_embedded_pck_offset() const override;
  179. virtual String get_config_path() const override;
  180. virtual String get_data_path() const override;
  181. virtual String get_cache_path() const override;
  182. virtual String get_temp_path() const override;
  183. virtual String get_godot_dir_name() const override;
  184. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;
  185. virtual String get_user_data_dir(const String &p_user_dir) const override;
  186. virtual String get_unique_id() const override;
  187. virtual Error shell_open(const String &p_uri) override;
  188. virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder) override;
  189. void run();
  190. virtual bool _check_internal_feature_support(const String &p_feature) override;
  191. virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const override;
  192. virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const override;
  193. virtual void disable_crash_handler() override;
  194. virtual bool is_disable_crash_handler() const override;
  195. virtual void initialize_debugging() override;
  196. virtual Error move_to_trash(const String &p_path) override;
  197. virtual String get_system_ca_certificates() override;
  198. void set_main_window(HWND p_main_window) { main_window = p_main_window; }
  199. virtual bool _test_create_rendering_device_and_gl(const String &p_display_driver) const override;
  200. virtual bool _test_create_rendering_device(const String &p_display_driver) const override;
  201. HINSTANCE get_hinstance() { return hInstance; }
  202. OS_Windows(HINSTANCE _hInstance);
  203. ~OS_Windows();
  204. };