os_windows.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/unix/ip_unix.h"
  38. #include "drivers/wasapi/audio_driver_wasapi.h"
  39. #include "drivers/winmidi/midi_driver_winmidi.h"
  40. #include "servers/audio_server.h"
  41. #ifdef XAUDIO2_ENABLED
  42. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  43. #endif
  44. #if defined(VULKAN_ENABLED)
  45. #include "vulkan_context_win.h"
  46. #include "drivers/vulkan/rendering_device_vulkan.h"
  47. #endif
  48. #include <io.h>
  49. #include <shellapi.h>
  50. #include <stdio.h>
  51. #define WIN32_LEAN_AND_MEAN
  52. #include <dwrite.h>
  53. #include <dwrite_2.h>
  54. #include <windows.h>
  55. #include <windowsx.h>
  56. #ifdef DEBUG_ENABLED
  57. // forward error messages to OutputDebugString
  58. #define WINDOWS_DEBUG_OUTPUT_ENABLED
  59. #endif
  60. #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
  61. #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
  62. #endif
  63. template <class T>
  64. class ComAutoreleaseRef {
  65. public:
  66. T *reference = nullptr;
  67. _FORCE_INLINE_ T *operator->() { return reference; }
  68. _FORCE_INLINE_ const T *operator->() const { return reference; }
  69. _FORCE_INLINE_ T *operator*() { return reference; }
  70. _FORCE_INLINE_ const T *operator*() const { return reference; }
  71. _FORCE_INLINE_ bool is_valid() const { return reference != nullptr; }
  72. _FORCE_INLINE_ bool is_null() const { return reference == nullptr; }
  73. ComAutoreleaseRef() {}
  74. ComAutoreleaseRef(T *p_ref) {
  75. reference = p_ref;
  76. }
  77. ~ComAutoreleaseRef() {
  78. if (reference != nullptr) {
  79. reference->Release();
  80. reference = nullptr;
  81. }
  82. }
  83. };
  84. class JoypadWindows;
  85. class OS_Windows : public OS {
  86. uint64_t ticks_start = 0;
  87. uint64_t ticks_per_second = 0;
  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. String _get_default_fontname(const String &p_font_name) const;
  111. DWRITE_FONT_WEIGHT _weight_to_dw(int p_weight) const;
  112. DWRITE_FONT_STRETCH _stretch_to_dw(int p_stretch) const;
  113. // functions used by main to initialize/deinitialize the OS
  114. protected:
  115. virtual void initialize() override;
  116. virtual void set_main_loop(MainLoop *p_main_loop) override;
  117. virtual void delete_main_loop() override;
  118. virtual void finalize() override;
  119. virtual void finalize_core() override;
  120. virtual String get_stdin_string() override;
  121. String _quote_command_line_argument(const String &p_text) const;
  122. struct ProcessInfo {
  123. STARTUPINFO si;
  124. PROCESS_INFORMATION pi;
  125. };
  126. HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
  127. public:
  128. virtual void alert(const String &p_alert, const String &p_title = "ALERT!") override;
  129. virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
  130. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false, String *r_resolved_path = nullptr) override;
  131. virtual Error close_dynamic_library(void *p_library_handle) override;
  132. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false) override;
  133. virtual MainLoop *get_main_loop() const override;
  134. virtual String get_name() const override;
  135. virtual String get_distribution_name() const override;
  136. virtual String get_version() const override;
  137. virtual Vector<String> get_video_adapter_driver_info() const override;
  138. virtual void initialize_joypads() override {}
  139. virtual DateTime get_datetime(bool p_utc) const override;
  140. virtual TimeZoneInfo get_time_zone_info() const override;
  141. virtual double get_unix_time() const override;
  142. virtual Error set_cwd(const String &p_cwd) override;
  143. virtual void delay_usec(uint32_t p_usec) const override;
  144. virtual uint64_t get_ticks_usec() const override;
  145. virtual Dictionary get_memory_info() const override;
  146. 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;
  147. virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
  148. virtual Error kill(const ProcessID &p_pid) override;
  149. virtual int get_process_id() const override;
  150. virtual bool is_process_running(const ProcessID &p_pid) const override;
  151. virtual bool has_environment(const String &p_var) const override;
  152. virtual String get_environment(const String &p_var) const override;
  153. virtual void set_environment(const String &p_var, const String &p_value) const override;
  154. virtual void unset_environment(const String &p_var) const override;
  155. virtual Vector<String> get_system_fonts() const override;
  156. 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;
  157. 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;
  158. virtual String get_executable_path() const override;
  159. virtual String get_locale() const override;
  160. virtual String get_processor_name() const override;
  161. virtual uint64_t get_embedded_pck_offset() const override;
  162. virtual String get_config_path() const override;
  163. virtual String get_data_path() const override;
  164. virtual String get_cache_path() const override;
  165. virtual String get_godot_dir_name() const override;
  166. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;
  167. virtual String get_user_data_dir() const override;
  168. virtual String get_unique_id() const override;
  169. virtual Error shell_open(String p_uri) override;
  170. virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder) override;
  171. void run();
  172. virtual bool _check_internal_feature_support(const String &p_feature) override;
  173. virtual void disable_crash_handler() override;
  174. virtual bool is_disable_crash_handler() const override;
  175. virtual void initialize_debugging() override;
  176. virtual Error move_to_trash(const String &p_path) override;
  177. virtual String get_system_ca_certificates() override;
  178. void set_main_window(HWND p_main_window) { main_window = p_main_window; }
  179. HINSTANCE get_hinstance() { return hInstance; }
  180. OS_Windows(HINSTANCE _hInstance);
  181. ~OS_Windows();
  182. };
  183. #endif // OS_WINDOWS_H