os_windows.h 9.9 KB

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