| 1 | /**************************************************************************/ |
| 2 | /* display_server_windows.cpp */ |
| 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 | #include "display_server_windows.h" |
| 32 | |
| 33 | #include "drop_target_windows.h" |
| 34 | #include "os_windows.h" |
| 35 | #include "scene/main/window.h" |
| 36 | #include "wgl_detect_version.h" |
| 37 | |
| 38 | #include "core/config/project_settings.h" |
| 39 | #include "core/io/file_access.h" |
| 40 | #include "core/io/marshalls.h" |
| 41 | #include "core/io/xml_parser.h" |
| 42 | #include "core/os/main_loop.h" |
| 43 | #include "core/version.h" |
| 44 | #include "drivers/png/png_driver_common.h" |
| 45 | #include "main/main.h" |
| 46 | #include "scene/resources/texture.h" |
| 47 | |
| 48 | #ifdef SDL_ENABLED |
| 49 | #include "drivers/sdl/joypad_sdl.h" |
| 50 | #endif |
| 51 | |
| 52 | #include "servers/rendering/dummy/rasterizer_dummy.h" |
| 53 | |
| 54 | #if defined(VULKAN_ENABLED) |
| 55 | #include "rendering_context_driver_vulkan_windows.h" |
| 56 | #endif |
| 57 | #if defined(D3D12_ENABLED) |
| 58 | #include "drivers/d3d12/rendering_context_driver_d3d12.h" |
| 59 | #endif |
| 60 | #if defined(GLES3_ENABLED) |
| 61 | #include "drivers/gles3/rasterizer_gles3.h" |
| 62 | #endif |
| 63 | |
| 64 | #if defined(ACCESSKIT_ENABLED) |
| 65 | #include "drivers/accesskit/accessibility_driver_accesskit.h" |
| 66 | #endif |
| 67 | |
| 68 | #include <avrt.h> |
| 69 | #include <dwmapi.h> |
| 70 | #include <propkey.h> |
| 71 | #include <propvarutil.h> |
| 72 | #include <shellapi.h> |
| 73 | #include <shellscalingapi.h> |
| 74 | #include <shlwapi.h> |
| 75 | #include <shobjidl.h> |
| 76 | #include <wbemcli.h> |
| 77 | |
| 78 | #ifndef DWMWA_USE_IMMERSIVE_DARK_MODE |
| 79 | #define DWMWA_USE_IMMERSIVE_DARK_MODE 20 |
| 80 | #endif |
| 81 | |
| 82 | #ifndef DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 |
| 83 | #define DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 19 |
| 84 | #endif |
| 85 | |
| 86 | #ifndef DWMWA_WINDOW_CORNER_PREFERENCE |
| 87 | #define DWMWA_WINDOW_CORNER_PREFERENCE 33 |
| 88 | #endif |
| 89 | |
| 90 | #ifndef DWMWCP_DEFAULT |
| 91 | #define DWMWCP_DEFAULT 0 |
| 92 | #endif |
| 93 | |
| 94 | #ifndef DWMWCP_DONOTROUND |
| 95 | #define DWMWCP_DONOTROUND 1 |
| 96 | #endif |
| 97 | |
| 98 | #define WM_INDICATOR_CALLBACK_MESSAGE (WM_USER + 1) |
| 99 | |
| 100 | static String format_error_message(DWORD id) { |
| 101 | LPWSTR messageBuffer = nullptr; |
| 102 | size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 103 | nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr); |
| 104 | |
| 105 | String msg = "Error " + itos(id) + ": " + String::utf16((const char16_t *)messageBuffer, size); |
| 106 | |
| 107 | LocalFree(messageBuffer); |
| 108 | |
| 109 | return msg; |
| 110 | } |
| 111 | |
| 112 | static void track_mouse_leave_event(HWND hWnd) { |
| 113 | TRACKMOUSEEVENT tme; |
| 114 | tme.cbSize = sizeof(TRACKMOUSEEVENT); |
| 115 | tme.dwFlags = TME_LEAVE; |
| 116 | tme.hwndTrack = hWnd; |
| 117 | tme.dwHoverTime = HOVER_DEFAULT; |
| 118 | TrackMouseEvent(&tme); |
| 119 | } |
| 120 | |
| 121 | bool DisplayServerWindows::has_feature(Feature p_feature) const { |
| 122 | switch (p_feature) { |
| 123 | #ifndef DISABLE_DEPRECATED |
| 124 | case FEATURE_GLOBAL_MENU: { |
| 125 | return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)); |
| 126 | } break; |
| 127 | #endif |
| 128 | case FEATURE_SUBWINDOWS: |
| 129 | case FEATURE_TOUCHSCREEN: |
| 130 | case FEATURE_MOUSE: |
| 131 | case FEATURE_MOUSE_WARP: |
| 132 | case FEATURE_CLIPBOARD: |
| 133 | case FEATURE_CURSOR_SHAPE: |
| 134 | case FEATURE_CUSTOM_CURSOR_SHAPE: |
| 135 | case FEATURE_IME: |
| 136 | case FEATURE_WINDOW_TRANSPARENCY: |
| 137 | case FEATURE_HIDPI: |
| 138 | case FEATURE_ICON: |
| 139 | case FEATURE_NATIVE_ICON: |
| 140 | case FEATURE_NATIVE_DIALOG: |
| 141 | case FEATURE_NATIVE_DIALOG_INPUT: |
| 142 | case FEATURE_NATIVE_DIALOG_FILE: |
| 143 | case FEATURE_NATIVE_DIALOG_FILE_EXTRA: |
| 144 | //case FEATURE_NATIVE_DIALOG_FILE_MIME: |
| 145 | case FEATURE_SWAP_BUFFERS: |
| 146 | case FEATURE_KEEP_SCREEN_ON: |
| 147 | case FEATURE_TEXT_TO_SPEECH: |
| 148 | case FEATURE_SCREEN_CAPTURE: |
| 149 | case FEATURE_STATUS_INDICATOR: |
| 150 | case FEATURE_WINDOW_EMBEDDING: |
| 151 | case FEATURE_WINDOW_DRAG: |
| 152 | return true; |
| 153 | case FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE: |
| 154 | return (os_ver.dwBuildNumber >= 19041); // Fully supported on Windows 10 Vibranium R1 (2004)+ only, captured as black rect on older versions. |
| 155 | case FEATURE_EMOJI_AND_SYMBOL_PICKER: |
| 156 | return (os_ver.dwBuildNumber >= 17134); // Windows 10 Redstone 4 (1803)+ only. |
| 157 | #ifdef ACCESSKIT_ENABLED |
| 158 | case FEATURE_ACCESSIBILITY_SCREEN_READER: { |
| 159 | return (accessibility_driver != nullptr); |
| 160 | } break; |
| 161 | #endif |
| 162 | default: |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | String DisplayServerWindows::get_name() const { |
| 168 | return "Windows"; |
| 169 | } |
| 170 | |
| 171 | Vector2i _logical_to_physical(const Vector2i &p_point) { |
| 172 | POINT p1; |
| 173 | p1.x = p_point.x; |
| 174 | p1.y = p_point.y; |
| 175 | LogicalToPhysicalPointForPerMonitorDPI(nullptr, &p1); |
| 176 | return Vector2i(p1.x, p1.y); |
| 177 | } |
| 178 | |
| 179 | Vector2i DisplayServerWindows::_get_screen_expand_offset(int p_screen) const { |
| 180 | Vector2i p1 = _logical_to_physical(screen_get_position(p_screen)); |
| 181 | Vector2i p2 = _logical_to_physical(screen_get_position(p_screen) + screen_get_size(p_screen)); |
| 182 | |
| 183 | int screen_down = -1; |
| 184 | int screen_right = -1; |
| 185 | for (int i = 0; i < get_screen_count(); i++) { |
| 186 | if (i == p_screen) { |
| 187 | continue; |
| 188 | } |
| 189 | Vector2i sp1 = _logical_to_physical(screen_get_position(i)); |
| 190 | Vector2i sp2 = _logical_to_physical(screen_get_position(i) + screen_get_size(i)); |
| 191 | if (sp1.y >= p2.y - 5 && sp1.y <= p2.y + 5 && sp1.x <= p2.x && p1.x <= sp2.x) { |
| 192 | screen_down = i; |
| 193 | } |
| 194 | if (sp1.x >= p2.x - 5 && sp1.x <= p2.x + 5 && sp1.y <= p2.y && p1.y <= sp2.y) { |
| 195 | screen_right = i; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (screen_down == -1) { |
| 200 | return Vector2i(0, 2); |
| 201 | } else if (screen_right == -1) { |
| 202 | return Vector2i(2, 0); |
| 203 | } else { |
| 204 | int diff_d = screen_get_refresh_rate(p_screen) - screen_get_refresh_rate(screen_down); |
| 205 | int diff_r = screen_get_refresh_rate(p_screen) - screen_get_refresh_rate(screen_right); |
| 206 | if (diff_d < diff_r) { |
| 207 | return Vector2i(0, 2); |
| 208 | } else { |
| 209 | return Vector2i(2, 0); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void DisplayServerWindows::_set_mouse_mode_impl(MouseMode p_mode) { |
| 215 | if (p_mode == MOUSE_MODE_HIDDEN || p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED_HIDDEN) { |
| 216 | // Hide cursor before moving. |
| 217 | if (hCursor == nullptr) { |
| 218 | hCursor = SetCursor(nullptr); |
| 219 | } else { |
| 220 | SetCursor(nullptr); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (windows.has(MAIN_WINDOW_ID) && (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN)) { |
| 225 | // Mouse is grabbed (captured or confined). |
| 226 | WindowID window_id = _get_focused_window_or_popup(); |
| 227 | if (!windows.has(window_id)) { |
| 228 | window_id = MAIN_WINDOW_ID; |
| 229 | } |
| 230 | |
| 231 | WindowData &wd = windows[window_id]; |
| 232 | |
| 233 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(window_get_current_screen(window_id)) : Vector2i(); |
| 234 | |
| 235 | RECT clipRect; |
| 236 | GetClientRect(wd.hWnd, &clipRect); |
| 237 | clipRect.right -= off.x; |
| 238 | clipRect.bottom -= off.y; |
| 239 | ClientToScreen(wd.hWnd, (POINT *)&clipRect.left); |
| 240 | ClientToScreen(wd.hWnd, (POINT *)&clipRect.right); |
| 241 | ClipCursor(&clipRect); |
| 242 | if (p_mode == MOUSE_MODE_CAPTURED) { |
| 243 | center = window_get_size() / 2; |
| 244 | POINT pos = { (int)center.x, (int)center.y }; |
| 245 | ClientToScreen(wd.hWnd, &pos); |
| 246 | SetCursorPos(pos.x, pos.y); |
| 247 | SetCapture(wd.hWnd); |
| 248 | |
| 249 | _register_raw_input_devices(window_id); |
| 250 | } |
| 251 | } else { |
| 252 | // Mouse is free to move around (not captured or confined). |
| 253 | // When the user is moving a window, it's important to not ReleaseCapture because it will cause |
| 254 | // the window movement to stop and if the user tries to move the Windows when it's not activated, |
| 255 | // it will prevent the window movement. It's probably impossible to move the Window while it's captured anyway. |
| 256 | if (!_has_moving_window()) { |
| 257 | ReleaseCapture(); |
| 258 | } |
| 259 | ClipCursor(nullptr); |
| 260 | |
| 261 | _register_raw_input_devices(INVALID_WINDOW_ID); |
| 262 | } |
| 263 | |
| 264 | if (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED) { |
| 265 | // Show cursor. |
| 266 | CursorShape c = cursor_shape; |
| 267 | cursor_shape = CURSOR_MAX; |
| 268 | cursor_set_shape(c); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | DisplayServer::WindowID DisplayServerWindows::_get_focused_window_or_popup() const { |
| 273 | const List<WindowID>::Element *E = popup_list.back(); |
| 274 | if (E) { |
| 275 | return E->get(); |
| 276 | } |
| 277 | |
| 278 | return last_focused_window; |
| 279 | } |
| 280 | |
| 281 | bool DisplayServerWindows::_has_moving_window() const { |
| 282 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 283 | if (E.value.move_timer_id) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | void DisplayServerWindows::_register_raw_input_devices(WindowID p_target_window) { |
| 291 | use_raw_input = true; |
| 292 | |
| 293 | RAWINPUTDEVICE rid[2] = {}; |
| 294 | rid[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC |
| 295 | rid[0].usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE |
| 296 | rid[0].dwFlags = 0; |
| 297 | |
| 298 | rid[1].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC |
| 299 | rid[1].usUsage = 0x06; // HID_USAGE_GENERIC_KEYBOARD |
| 300 | rid[1].dwFlags = 0; |
| 301 | |
| 302 | if (p_target_window != INVALID_WINDOW_ID && windows.has(p_target_window)) { |
| 303 | // Follow the defined window |
| 304 | rid[0].hwndTarget = windows[p_target_window].hWnd; |
| 305 | rid[1].hwndTarget = windows[p_target_window].hWnd; |
| 306 | } else { |
| 307 | // Follow the keyboard focus |
| 308 | rid[0].hwndTarget = nullptr; |
| 309 | rid[1].hwndTarget = nullptr; |
| 310 | } |
| 311 | |
| 312 | if (RegisterRawInputDevices(rid, 2, sizeof(rid[0])) == FALSE) { |
| 313 | // Registration failed. |
| 314 | use_raw_input = false; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void DisplayServerWindows::initialize_tts() const { |
| 319 | const_cast<DisplayServerWindows *>(this)->tts = memnew(TTS_Windows); |
| 320 | } |
| 321 | |
| 322 | bool DisplayServerWindows::tts_is_speaking() const { |
| 323 | if (unlikely(!tts)) { |
| 324 | initialize_tts(); |
| 325 | } |
| 326 | ERR_FAIL_NULL_V(tts, false); |
| 327 | return tts->is_speaking(); |
| 328 | } |
| 329 | |
| 330 | bool DisplayServerWindows::tts_is_paused() const { |
| 331 | if (unlikely(!tts)) { |
| 332 | initialize_tts(); |
| 333 | } |
| 334 | ERR_FAIL_NULL_V(tts, false); |
| 335 | return tts->is_paused(); |
| 336 | } |
| 337 | |
| 338 | TypedArray<Dictionary> DisplayServerWindows::tts_get_voices() const { |
| 339 | if (unlikely(!tts)) { |
| 340 | initialize_tts(); |
| 341 | } |
| 342 | ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>()); |
| 343 | return tts->get_voices(); |
| 344 | } |
| 345 | |
| 346 | void DisplayServerWindows::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) { |
| 347 | if (unlikely(!tts)) { |
| 348 | initialize_tts(); |
| 349 | } |
| 350 | ERR_FAIL_NULL(tts); |
| 351 | tts->speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt); |
| 352 | } |
| 353 | |
| 354 | void DisplayServerWindows::tts_pause() { |
| 355 | if (unlikely(!tts)) { |
| 356 | initialize_tts(); |
| 357 | } |
| 358 | ERR_FAIL_NULL(tts); |
| 359 | tts->pause(); |
| 360 | } |
| 361 | |
| 362 | void DisplayServerWindows::tts_resume() { |
| 363 | if (unlikely(!tts)) { |
| 364 | initialize_tts(); |
| 365 | } |
| 366 | ERR_FAIL_NULL(tts); |
| 367 | tts->resume(); |
| 368 | } |
| 369 | |
| 370 | void DisplayServerWindows::tts_stop() { |
| 371 | if (unlikely(!tts)) { |
| 372 | initialize_tts(); |
| 373 | } |
| 374 | ERR_FAIL_NULL(tts); |
| 375 | tts->stop(); |
| 376 | } |
| 377 | |
| 378 | Error DisplayServerWindows::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback, WindowID p_window_id) { |
| 379 | return _file_dialog_with_options_show(p_title, p_current_directory, String(), p_filename, p_show_hidden, p_mode, p_filters, TypedArray<Dictionary>(), p_callback, false, p_window_id); |
| 380 | } |
| 381 | |
| 382 | Error DisplayServerWindows::file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, WindowID p_window_id) { |
| 383 | return _file_dialog_with_options_show(p_title, p_current_directory, p_root, p_filename, p_show_hidden, p_mode, p_filters, p_options, p_callback, true, p_window_id); |
| 384 | } |
| 385 | |
| 386 | GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wnon-virtual-dtor") // Silence warning due to a COM API weirdness. |
| 387 | |
| 388 | class FileDialogEventHandler : public IFileDialogEvents, public IFileDialogControlEvents { |
| 389 | LONG ref_count = 1; |
| 390 | int ctl_id = 1; |
| 391 | |
| 392 | HashMap<int, String> ctls; |
| 393 | Dictionary selected; |
| 394 | String root; |
| 395 | |
| 396 | public: |
| 397 | // IUnknown methods |
| 398 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppv) { |
| 399 | static const QITAB qit[] = { |
| 400 | #ifdef __MINGW32__ |
| 401 | { &__uuidof(IFileDialogEvents), static_cast<decltype(qit[0].dwOffset)>(OFFSETOFCLASS(IFileDialogEvents, FileDialogEventHandler)) }, |
| 402 | { &__uuidof(IFileDialogControlEvents), static_cast<decltype(qit[0].dwOffset)>(OFFSETOFCLASS(IFileDialogControlEvents, FileDialogEventHandler)) }, |
| 403 | #else |
| 404 | QITABENT(FileDialogEventHandler, IFileDialogEvents), |
| 405 | QITABENT(FileDialogEventHandler, IFileDialogControlEvents), |
| 406 | #endif |
| 407 | { nullptr, 0 }, |
| 408 | }; |
| 409 | return QISearch(this, qit, riid, ppv); |
| 410 | } |
| 411 | |
| 412 | ULONG STDMETHODCALLTYPE AddRef() { |
| 413 | return InterlockedIncrement(&ref_count); |
| 414 | } |
| 415 | |
| 416 | ULONG STDMETHODCALLTYPE Release() { |
| 417 | long ref = InterlockedDecrement(&ref_count); |
| 418 | if (!ref) { |
| 419 | delete this; |
| 420 | } |
| 421 | return ref; |
| 422 | } |
| 423 | |
| 424 | // IFileDialogEvents methods |
| 425 | HRESULT STDMETHODCALLTYPE OnFileOk(IFileDialog *) { return S_OK; } |
| 426 | HRESULT STDMETHODCALLTYPE OnFolderChange(IFileDialog *) { return S_OK; } |
| 427 | |
| 428 | HRESULT STDMETHODCALLTYPE OnFolderChanging(IFileDialog *p_pfd, IShellItem *p_item) { |
| 429 | if (root.is_empty()) { |
| 430 | return S_OK; |
| 431 | } |
| 432 | |
| 433 | LPWSTR lpw_path = nullptr; |
| 434 | p_item->GetDisplayName(SIGDN_FILESYSPATH, &lpw_path); |
| 435 | if (!lpw_path) { |
| 436 | return S_FALSE; |
| 437 | } |
| 438 | String path = String::utf16((const char16_t *)lpw_path).replace_char('\\', '/').trim_prefix(R"(\\?\)").simplify_path(); |
| 439 | if (!path.begins_with(root.simplify_path())) { |
| 440 | return S_FALSE; |
| 441 | } |
| 442 | return S_OK; |
| 443 | } |
| 444 | |
| 445 | HRESULT STDMETHODCALLTYPE OnHelp(IFileDialog *) { return S_OK; } |
| 446 | HRESULT STDMETHODCALLTYPE OnSelectionChange(IFileDialog *) { return S_OK; } |
| 447 | HRESULT STDMETHODCALLTYPE OnShareViolation(IFileDialog *, IShellItem *, FDE_SHAREVIOLATION_RESPONSE *) { return S_OK; } |
| 448 | HRESULT STDMETHODCALLTYPE OnTypeChange(IFileDialog *pfd) { return S_OK; } |
| 449 | HRESULT STDMETHODCALLTYPE OnOverwrite(IFileDialog *, IShellItem *, FDE_OVERWRITE_RESPONSE *) { return S_OK; } |
| 450 | |
| 451 | // IFileDialogControlEvents methods |
| 452 | HRESULT STDMETHODCALLTYPE OnItemSelected(IFileDialogCustomize *p_pfdc, DWORD p_ctl_id, DWORD p_item_idx) { |
| 453 | if (ctls.has(p_ctl_id)) { |
| 454 | selected[ctls[p_ctl_id]] = (int)p_item_idx; |
| 455 | } |
| 456 | return S_OK; |
| 457 | } |
| 458 | |
| 459 | HRESULT STDMETHODCALLTYPE OnButtonClicked(IFileDialogCustomize *, DWORD) { return S_OK; } |
| 460 | HRESULT STDMETHODCALLTYPE OnCheckButtonToggled(IFileDialogCustomize *p_pfdc, DWORD p_ctl_id, BOOL p_checked) { |
| 461 | if (ctls.has(p_ctl_id)) { |
| 462 | selected[ctls[p_ctl_id]] = (bool)p_checked; |
| 463 | } |
| 464 | return S_OK; |
| 465 | } |
| 466 | HRESULT STDMETHODCALLTYPE OnControlActivating(IFileDialogCustomize *, DWORD) { return S_OK; } |
| 467 | |
| 468 | Dictionary get_selected() { |
| 469 | return selected; |
| 470 | } |
| 471 | |
| 472 | void set_root(const String &p_root) { |
| 473 | root = p_root; |
| 474 | } |
| 475 | |
| 476 | void add_option(IFileDialogCustomize *p_pfdc, const String &p_name, const Vector<String> &p_options, int p_default) { |
| 477 | int gid = ctl_id++; |
| 478 | int cid = ctl_id++; |
| 479 | |
| 480 | if (p_options.is_empty()) { |
| 481 | // Add check box. |
| 482 | p_pfdc->StartVisualGroup(gid, L""); |
| 483 | p_pfdc->AddCheckButton(cid, (LPCWSTR)p_name.utf16().get_data(), p_default); |
| 484 | p_pfdc->SetControlState(cid, CDCS_VISIBLE | CDCS_ENABLED); |
| 485 | p_pfdc->EndVisualGroup(); |
| 486 | selected[p_name] = (bool)p_default; |
| 487 | } else { |
| 488 | // Add combo box. |
| 489 | p_pfdc->StartVisualGroup(gid, (LPCWSTR)p_name.utf16().get_data()); |
| 490 | p_pfdc->AddComboBox(cid); |
| 491 | p_pfdc->SetControlState(cid, CDCS_VISIBLE | CDCS_ENABLED); |
| 492 | for (int i = 0; i < p_options.size(); i++) { |
| 493 | p_pfdc->AddControlItem(cid, i, (LPCWSTR)p_options[i].utf16().get_data()); |
| 494 | } |
| 495 | p_pfdc->SetSelectedControlItem(cid, p_default); |
| 496 | p_pfdc->EndVisualGroup(); |
| 497 | selected[p_name] = p_default; |
| 498 | } |
| 499 | ctls[cid] = p_name; |
| 500 | } |
| 501 | |
| 502 | virtual ~FileDialogEventHandler() {} |
| 503 | }; |
| 504 | |
| 505 | GODOT_GCC_WARNING_POP |
| 506 | |
| 507 | LRESULT CALLBACK WndProcFileDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 508 | DisplayServerWindows *ds_win = static_cast<DisplayServerWindows *>(DisplayServer::get_singleton()); |
| 509 | if (ds_win) { |
| 510 | return ds_win->WndProcFileDialog(hWnd, uMsg, wParam, lParam); |
| 511 | } else { |
| 512 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | LRESULT DisplayServerWindows::WndProcFileDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 517 | MutexLock lock(file_dialog_mutex); |
| 518 | if (file_dialog_wnd.has(hWnd)) { |
| 519 | if (file_dialog_wnd[hWnd]->close_requested.is_set()) { |
| 520 | IPropertyStore *prop_store; |
| 521 | HRESULT hr = SHGetPropertyStoreForWindow(hWnd, IID_IPropertyStore, (void **)&prop_store); |
| 522 | if (hr == S_OK) { |
| 523 | PROPVARIANT val; |
| 524 | PropVariantInit(&val); |
| 525 | prop_store->SetValue(PKEY_AppUserModel_ID, val); |
| 526 | prop_store->Release(); |
| 527 | } |
| 528 | DestroyWindow(hWnd); |
| 529 | file_dialog_wnd.erase(hWnd); |
| 530 | } |
| 531 | } |
| 532 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 533 | } |
| 534 | |
| 535 | void DisplayServerWindows::_thread_fd_monitor(void *p_ud) { |
| 536 | DisplayServerWindows *ds = static_cast<DisplayServerWindows *>(get_singleton()); |
| 537 | FileDialogData *fd = (FileDialogData *)p_ud; |
| 538 | |
| 539 | if (fd->mode < 0 && fd->mode >= DisplayServer::FILE_DIALOG_MODE_SAVE_MAX) { |
| 540 | fd->finished.set(); |
| 541 | return; |
| 542 | } |
| 543 | CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); |
| 544 | |
| 545 | int64_t x = fd->wrect.position.x; |
| 546 | int64_t y = fd->wrect.position.y; |
| 547 | int64_t w = fd->wrect.size.x; |
| 548 | int64_t h = fd->wrect.size.y; |
| 549 | |
| 550 | WNDCLASSW wc = {}; |
| 551 | wc.lpfnWndProc = (WNDPROC)::WndProcFileDialog; |
| 552 | wc.hInstance = GetModuleHandle(nullptr); |
| 553 | wc.lpszClassName = L"Engine File Dialog"; |
| 554 | RegisterClassW(&wc); |
| 555 | |
| 556 | HWND hwnd_dialog = CreateWindowExW(WS_EX_APPWINDOW, L"Engine File Dialog", L"", WS_OVERLAPPEDWINDOW, x, y, w, h, nullptr, nullptr, GetModuleHandle(nullptr), nullptr); |
| 557 | if (hwnd_dialog) { |
| 558 | { |
| 559 | MutexLock lock(ds->file_dialog_mutex); |
| 560 | ds->file_dialog_wnd[hwnd_dialog] = fd; |
| 561 | } |
| 562 | |
| 563 | HICON mainwindow_icon = (HICON)SendMessage(fd->hwnd_owner, WM_GETICON, ICON_SMALL, 0); |
| 564 | if (mainwindow_icon) { |
| 565 | SendMessage(hwnd_dialog, WM_SETICON, ICON_SMALL, (LPARAM)mainwindow_icon); |
| 566 | } |
| 567 | mainwindow_icon = (HICON)SendMessage(fd->hwnd_owner, WM_GETICON, ICON_BIG, 0); |
| 568 | if (mainwindow_icon) { |
| 569 | SendMessage(hwnd_dialog, WM_SETICON, ICON_BIG, (LPARAM)mainwindow_icon); |
| 570 | } |
| 571 | IPropertyStore *prop_store; |
| 572 | HRESULT hr = SHGetPropertyStoreForWindow(hwnd_dialog, IID_IPropertyStore, (void **)&prop_store); |
| 573 | if (hr == S_OK) { |
| 574 | PROPVARIANT val; |
| 575 | InitPropVariantFromString((PCWSTR)fd->appid.utf16().get_data(), &val); |
| 576 | prop_store->SetValue(PKEY_AppUserModel_ID, val); |
| 577 | prop_store->Release(); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | SetCurrentProcessExplicitAppUserModelID((PCWSTR)fd->appid.utf16().get_data()); |
| 582 | |
| 583 | Vector<Char16String> filter_names; |
| 584 | Vector<Char16String> filter_exts; |
| 585 | for (const String &E : fd->filters) { |
| 586 | Vector<String> tokens = E.split(";"); |
| 587 | if (tokens.size() >= 1) { |
| 588 | String flt = tokens[0].strip_edges(); |
| 589 | int filter_slice_count = flt.get_slice_count(","); |
| 590 | Vector<String> exts; |
| 591 | for (int j = 0; j < filter_slice_count; j++) { |
| 592 | String str = (flt.get_slicec(',', j).strip_edges()); |
| 593 | if (!str.is_empty()) { |
| 594 | exts.push_back(str); |
| 595 | } |
| 596 | } |
| 597 | if (!exts.is_empty()) { |
| 598 | String str = String(";").join(exts); |
| 599 | filter_exts.push_back(str.utf16()); |
| 600 | if (tokens.size() >= 2) { |
| 601 | filter_names.push_back(tokens[1].strip_edges().utf16()); |
| 602 | } else { |
| 603 | filter_names.push_back(str.utf16()); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | if (filter_names.is_empty()) { |
| 609 | filter_exts.push_back(String("*.*").utf16()); |
| 610 | filter_names.push_back((RTR("All Files") + " (*.*)").utf16()); |
| 611 | } |
| 612 | |
| 613 | Vector<COMDLG_FILTERSPEC> filters; |
| 614 | for (int i = 0; i < filter_names.size(); i++) { |
| 615 | filters.push_back({ (LPCWSTR)filter_names[i].ptr(), (LPCWSTR)filter_exts[i].ptr() }); |
| 616 | } |
| 617 | |
| 618 | HRESULT hr = S_OK; |
| 619 | IFileDialog *pfd = nullptr; |
| 620 | if (fd->mode == DisplayServer::FILE_DIALOG_MODE_SAVE_FILE) { |
| 621 | hr = CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_INPROC_SERVER, IID_IFileSaveDialog, (void **)&pfd); |
| 622 | } else { |
| 623 | hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_IFileOpenDialog, (void **)&pfd); |
| 624 | } |
| 625 | if (SUCCEEDED(hr)) { |
| 626 | IFileDialogEvents *pfde = nullptr; |
| 627 | FileDialogEventHandler *event_handler = new FileDialogEventHandler(); |
| 628 | hr = event_handler->QueryInterface(IID_PPV_ARGS(&pfde)); |
| 629 | |
| 630 | DWORD cookie = 0; |
| 631 | hr = pfd->Advise(pfde, &cookie); |
| 632 | |
| 633 | IFileDialogCustomize *pfdc = nullptr; |
| 634 | hr = pfd->QueryInterface(IID_PPV_ARGS(&pfdc)); |
| 635 | |
| 636 | for (int i = 0; i < fd->options.size(); i++) { |
| 637 | const Dictionary &item = fd->options[i]; |
| 638 | if (!item.has("name") || !item.has("values") || !item.has("default")) { |
| 639 | continue; |
| 640 | } |
| 641 | event_handler->add_option(pfdc, item["name"], item["values"], item["default"]); |
| 642 | } |
| 643 | event_handler->set_root(fd->root); |
| 644 | |
| 645 | pfdc->Release(); |
| 646 | |
| 647 | DWORD flags; |
| 648 | pfd->GetOptions(&flags); |
| 649 | if (fd->mode == DisplayServer::FILE_DIALOG_MODE_OPEN_FILES) { |
| 650 | flags |= FOS_ALLOWMULTISELECT; |
| 651 | } |
| 652 | if (fd->mode == DisplayServer::FILE_DIALOG_MODE_OPEN_DIR) { |
| 653 | flags |= FOS_PICKFOLDERS; |
| 654 | } |
| 655 | if (fd->show_hidden) { |
| 656 | flags |= FOS_FORCESHOWHIDDEN; |
| 657 | } |
| 658 | pfd->SetOptions(flags | FOS_FORCEFILESYSTEM); |
| 659 | pfd->SetTitle((LPCWSTR)fd->title.utf16().get_data()); |
| 660 | |
| 661 | String dir = ProjectSettings::get_singleton()->globalize_path(fd->current_directory); |
| 662 | if (dir == ".") { |
| 663 | dir = OS::get_singleton()->get_executable_path().get_base_dir(); |
| 664 | } |
| 665 | if (dir.is_relative_path() || dir == ".") { |
| 666 | Char16String current_dir_name; |
| 667 | size_t str_len = GetCurrentDirectoryW(0, nullptr); |
| 668 | current_dir_name.resize_uninitialized(str_len + 1); |
| 669 | GetCurrentDirectoryW(current_dir_name.size(), (LPWSTR)current_dir_name.ptrw()); |
| 670 | if (dir == ".") { |
| 671 | dir = String::utf16((const char16_t *)current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace_char('\\', '/'); |
| 672 | } else { |
| 673 | dir = String::utf16((const char16_t *)current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace_char('\\', '/').path_join(dir); |
| 674 | } |
| 675 | } |
| 676 | dir = dir.simplify_path(); |
| 677 | dir = dir.trim_prefix(R"(\\?\)").replace_char('/', '\\'); |
| 678 | |
| 679 | IShellItem *shellitem = nullptr; |
| 680 | hr = SHCreateItemFromParsingName((LPCWSTR)dir.utf16().ptr(), nullptr, IID_IShellItem, (void **)&shellitem); |
| 681 | if (SUCCEEDED(hr)) { |
| 682 | pfd->SetDefaultFolder(shellitem); |
| 683 | pfd->SetFolder(shellitem); |
| 684 | } |
| 685 | |
| 686 | pfd->SetFileName((LPCWSTR)fd->filename.utf16().get_data()); |
| 687 | pfd->SetFileTypes(filters.size(), filters.ptr()); |
| 688 | pfd->SetFileTypeIndex(0); |
| 689 | |
| 690 | hr = pfd->Show(hwnd_dialog); |
| 691 | pfd->Unadvise(cookie); |
| 692 | |
| 693 | Dictionary options = event_handler->get_selected(); |
| 694 | |
| 695 | pfde->Release(); |
| 696 | event_handler->Release(); |
| 697 | |
| 698 | UINT index = 0; |
| 699 | pfd->GetFileTypeIndex(&index); |
| 700 | if (index > 0) { |
| 701 | index = index - 1; |
| 702 | } |
| 703 | |
| 704 | if (SUCCEEDED(hr)) { |
| 705 | Vector<String> file_names; |
| 706 | |
| 707 | if (fd->mode == DisplayServer::FILE_DIALOG_MODE_OPEN_FILES) { |
| 708 | IShellItemArray *results; |
| 709 | hr = static_cast<IFileOpenDialog *>(pfd)->GetResults(&results); |
| 710 | if (SUCCEEDED(hr)) { |
| 711 | DWORD count = 0; |
| 712 | results->GetCount(&count); |
| 713 | for (DWORD i = 0; i < count; i++) { |
| 714 | IShellItem *result; |
| 715 | results->GetItemAt(i, &result); |
| 716 | |
| 717 | PWSTR file_path = nullptr; |
| 718 | hr = result->GetDisplayName(SIGDN_FILESYSPATH, &file_path); |
| 719 | if (SUCCEEDED(hr)) { |
| 720 | file_names.push_back(String::utf16((const char16_t *)file_path).replace_char('\\', '/').trim_prefix(R"(\\?\)")); |
| 721 | CoTaskMemFree(file_path); |
| 722 | } |
| 723 | result->Release(); |
| 724 | } |
| 725 | results->Release(); |
| 726 | } |
| 727 | } else { |
| 728 | IShellItem *result; |
| 729 | hr = pfd->GetResult(&result); |
| 730 | if (SUCCEEDED(hr)) { |
| 731 | PWSTR file_path = nullptr; |
| 732 | hr = result->GetDisplayName(SIGDN_FILESYSPATH, &file_path); |
| 733 | if (SUCCEEDED(hr)) { |
| 734 | file_names.push_back(String::utf16((const char16_t *)file_path).replace_char('\\', '/').trim_prefix(R"(\\?\)")); |
| 735 | CoTaskMemFree(file_path); |
| 736 | } |
| 737 | result->Release(); |
| 738 | } |
| 739 | } |
| 740 | if (fd->callback.is_valid()) { |
| 741 | MutexLock lock(ds->file_dialog_mutex); |
| 742 | FileDialogCallback cb; |
| 743 | cb.callback = fd->callback; |
| 744 | cb.status = true; |
| 745 | cb.files = file_names; |
| 746 | cb.index = index; |
| 747 | cb.options = options; |
| 748 | cb.opt_in_cb = fd->options_in_cb; |
| 749 | ds->pending_cbs.push_back(cb); |
| 750 | } |
| 751 | } else { |
| 752 | if (fd->callback.is_valid()) { |
| 753 | MutexLock lock(ds->file_dialog_mutex); |
| 754 | FileDialogCallback cb; |
| 755 | cb.callback = fd->callback; |
| 756 | cb.status = false; |
| 757 | cb.files = Vector<String>(); |
| 758 | cb.index = index; |
| 759 | cb.options = options; |
| 760 | cb.opt_in_cb = fd->options_in_cb; |
| 761 | ds->pending_cbs.push_back(cb); |
| 762 | } |
| 763 | } |
| 764 | pfd->Release(); |
| 765 | } else { |
| 766 | if (fd->callback.is_valid()) { |
| 767 | MutexLock lock(ds->file_dialog_mutex); |
| 768 | FileDialogCallback cb; |
| 769 | cb.callback = fd->callback; |
| 770 | cb.status = false; |
| 771 | cb.files = Vector<String>(); |
| 772 | cb.index = 0; |
| 773 | cb.options = Dictionary(); |
| 774 | cb.opt_in_cb = fd->options_in_cb; |
| 775 | ds->pending_cbs.push_back(cb); |
| 776 | } |
| 777 | } |
| 778 | { |
| 779 | MutexLock lock(ds->file_dialog_mutex); |
| 780 | if (hwnd_dialog && ds->file_dialog_wnd.has(hwnd_dialog)) { |
| 781 | IPropertyStore *prop_store; |
| 782 | hr = SHGetPropertyStoreForWindow(hwnd_dialog, IID_IPropertyStore, (void **)&prop_store); |
| 783 | if (hr == S_OK) { |
| 784 | PROPVARIANT val; |
| 785 | PropVariantInit(&val); |
| 786 | prop_store->SetValue(PKEY_AppUserModel_ID, val); |
| 787 | prop_store->Release(); |
| 788 | } |
| 789 | DestroyWindow(hwnd_dialog); |
| 790 | ds->file_dialog_wnd.erase(hwnd_dialog); |
| 791 | } |
| 792 | } |
| 793 | UnregisterClassW(L"Engine File Dialog", GetModuleHandle(nullptr)); |
| 794 | CoUninitialize(); |
| 795 | |
| 796 | fd->finished.set(); |
| 797 | |
| 798 | if (fd->window_id != INVALID_WINDOW_ID) { |
| 799 | callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(fd->window_id); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | Error DisplayServerWindows::_file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, bool p_options_in_cb, WindowID p_window_id) { |
| 804 | _THREAD_SAFE_METHOD_ |
| 805 | |
| 806 | ERR_FAIL_INDEX_V(int(p_mode), FILE_DIALOG_MODE_SAVE_MAX, FAILED); |
| 807 | |
| 808 | String appname; |
| 809 | if (Engine::get_singleton()->is_editor_hint()) { |
| 810 | appname = "Godot.GodotEditor." + String(GODOT_VERSION_BRANCH); |
| 811 | } else { |
| 812 | String name = GLOBAL_GET("application/config/name"); |
| 813 | String version = GLOBAL_GET("application/config/version"); |
| 814 | if (version.is_empty()) { |
| 815 | version = "0"; |
| 816 | } |
| 817 | String clean_app_name = name.to_pascal_case(); |
| 818 | for (int i = 0; i < clean_app_name.length(); i++) { |
| 819 | if (!is_ascii_alphanumeric_char(clean_app_name[i]) && clean_app_name[i] != '_' && clean_app_name[i] != '.') { |
| 820 | clean_app_name[i] = '_'; |
| 821 | } |
| 822 | } |
| 823 | clean_app_name = clean_app_name.substr(0, 120 - version.length()).trim_suffix("."); |
| 824 | appname = "Godot." + clean_app_name + "." + version; |
| 825 | } |
| 826 | |
| 827 | FileDialogData *fd = memnew(FileDialogData); |
| 828 | if (windows.has(p_window_id) && !windows[p_window_id].is_popup) { |
| 829 | fd->hwnd_owner = windows[p_window_id].hWnd; |
| 830 | RECT crect; |
| 831 | GetWindowRect(fd->hwnd_owner, &crect); |
| 832 | fd->wrect = Rect2i(crect.left, crect.top, crect.right - crect.left, crect.bottom - crect.top); |
| 833 | } else { |
| 834 | fd->hwnd_owner = nullptr; |
| 835 | fd->wrect = Rect2i(CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT); |
| 836 | } |
| 837 | fd->appid = appname; |
| 838 | fd->title = p_title; |
| 839 | fd->current_directory = p_current_directory; |
| 840 | fd->root = p_root; |
| 841 | fd->filename = p_filename; |
| 842 | fd->show_hidden = p_show_hidden; |
| 843 | fd->mode = p_mode; |
| 844 | fd->window_id = p_window_id; |
| 845 | fd->filters = p_filters; |
| 846 | fd->options = p_options; |
| 847 | fd->callback = p_callback; |
| 848 | fd->options_in_cb = p_options_in_cb; |
| 849 | fd->finished.clear(); |
| 850 | fd->close_requested.clear(); |
| 851 | |
| 852 | fd->listener_thread.start(DisplayServerWindows::_thread_fd_monitor, fd); |
| 853 | |
| 854 | file_dialogs.push_back(fd); |
| 855 | |
| 856 | return OK; |
| 857 | } |
| 858 | |
| 859 | void DisplayServerWindows::process_file_dialog_callbacks() { |
| 860 | MutexLock lock(file_dialog_mutex); |
| 861 | while (!pending_cbs.is_empty()) { |
| 862 | FileDialogCallback cb = pending_cbs.front()->get(); |
| 863 | pending_cbs.pop_front(); |
| 864 | |
| 865 | if (cb.opt_in_cb) { |
| 866 | Variant ret; |
| 867 | Callable::CallError ce; |
| 868 | const Variant *args[4] = { &cb.status, &cb.files, &cb.index, &cb.options }; |
| 869 | |
| 870 | cb.callback.callp(args, 4, ret, ce); |
| 871 | if (ce.error != Callable::CallError::CALL_OK) { |
| 872 | ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(cb.callback, args, 4, ce))); |
| 873 | } |
| 874 | } else { |
| 875 | Variant ret; |
| 876 | Callable::CallError ce; |
| 877 | const Variant *args[3] = { &cb.status, &cb.files, &cb.index }; |
| 878 | |
| 879 | cb.callback.callp(args, 3, ret, ce); |
| 880 | if (ce.error != Callable::CallError::CALL_OK) { |
| 881 | ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(cb.callback, args, 3, ce))); |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void DisplayServerWindows::beep() const { |
| 888 | MessageBeep(MB_OK); |
| 889 | } |
| 890 | |
| 891 | void DisplayServerWindows::_mouse_update_mode() { |
| 892 | _THREAD_SAFE_METHOD_ |
| 893 | |
| 894 | MouseMode wanted_mouse_mode = mouse_mode_override_enabled |
| 895 | ? mouse_mode_override |
| 896 | : mouse_mode_base; |
| 897 | |
| 898 | if (mouse_mode == wanted_mouse_mode) { |
| 899 | // Already in the same mode; do nothing. |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | mouse_mode = wanted_mouse_mode; |
| 904 | |
| 905 | _set_mouse_mode_impl(wanted_mouse_mode); |
| 906 | } |
| 907 | |
| 908 | void DisplayServerWindows::mouse_set_mode(MouseMode p_mode) { |
| 909 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 910 | if (p_mode == mouse_mode_base) { |
| 911 | return; |
| 912 | } |
| 913 | mouse_mode_base = p_mode; |
| 914 | _mouse_update_mode(); |
| 915 | } |
| 916 | |
| 917 | DisplayServer::MouseMode DisplayServerWindows::mouse_get_mode() const { |
| 918 | return mouse_mode; |
| 919 | } |
| 920 | |
| 921 | void DisplayServerWindows::mouse_set_mode_override(MouseMode p_mode) { |
| 922 | ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 923 | if (p_mode == mouse_mode_override) { |
| 924 | return; |
| 925 | } |
| 926 | mouse_mode_override = p_mode; |
| 927 | _mouse_update_mode(); |
| 928 | } |
| 929 | |
| 930 | DisplayServer::MouseMode DisplayServerWindows::mouse_get_mode_override() const { |
| 931 | return mouse_mode_override; |
| 932 | } |
| 933 | |
| 934 | void DisplayServerWindows::mouse_set_mode_override_enabled(bool p_override_enabled) { |
| 935 | if (p_override_enabled == mouse_mode_override_enabled) { |
| 936 | return; |
| 937 | } |
| 938 | mouse_mode_override_enabled = p_override_enabled; |
| 939 | _mouse_update_mode(); |
| 940 | } |
| 941 | |
| 942 | bool DisplayServerWindows::mouse_is_mode_override_enabled() const { |
| 943 | return mouse_mode_override_enabled; |
| 944 | } |
| 945 | |
| 946 | void DisplayServerWindows::warp_mouse(const Point2i &p_position) { |
| 947 | _THREAD_SAFE_METHOD_ |
| 948 | |
| 949 | WindowID window_id = _get_focused_window_or_popup(); |
| 950 | |
| 951 | if (!windows.has(window_id)) { |
| 952 | return; // No focused window? |
| 953 | } |
| 954 | |
| 955 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 956 | old_x = p_position.x; |
| 957 | old_y = p_position.y; |
| 958 | } else { |
| 959 | POINT p; |
| 960 | p.x = p_position.x; |
| 961 | p.y = p_position.y; |
| 962 | ClientToScreen(windows[window_id].hWnd, &p); |
| 963 | |
| 964 | SetCursorPos(p.x, p.y); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | Point2i DisplayServerWindows::mouse_get_position() const { |
| 969 | POINT p; |
| 970 | GetCursorPos(&p); |
| 971 | return Point2i(p.x, p.y) - _get_screens_origin(); |
| 972 | } |
| 973 | |
| 974 | BitField<MouseButtonMask> DisplayServerWindows::mouse_get_button_state() const { |
| 975 | BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 976 | |
| 977 | if (GetKeyState(VK_LBUTTON) & (1 << 15)) { |
| 978 | last_button_state.set_flag(MouseButtonMask::LEFT); |
| 979 | } |
| 980 | if (GetKeyState(VK_RBUTTON) & (1 << 15)) { |
| 981 | last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 982 | } |
| 983 | if (GetKeyState(VK_MBUTTON) & (1 << 15)) { |
| 984 | last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 985 | } |
| 986 | if (GetKeyState(VK_XBUTTON1) & (1 << 15)) { |
| 987 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 988 | } |
| 989 | if (GetKeyState(VK_XBUTTON2) & (1 << 15)) { |
| 990 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 991 | } |
| 992 | |
| 993 | return last_button_state; |
| 994 | } |
| 995 | |
| 996 | void DisplayServerWindows::clipboard_set(const String &p_text) { |
| 997 | _THREAD_SAFE_METHOD_ |
| 998 | |
| 999 | if (!windows.has(MAIN_WINDOW_ID)) { |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | // Convert LF line endings to CRLF in clipboard content. |
| 1004 | // Otherwise, line endings won't be visible when pasted in other software. |
| 1005 | String text = p_text.replace("\r\n", "\n").replace("\n", "\r\n"); // Avoid \r\r\n. |
| 1006 | |
| 1007 | if (!OpenClipboard(windows[MAIN_WINDOW_ID].hWnd)) { |
| 1008 | ERR_FAIL_MSG("Unable to open clipboard."); |
| 1009 | } |
| 1010 | EmptyClipboard(); |
| 1011 | |
| 1012 | Char16String utf16 = text.utf16(); |
| 1013 | HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (utf16.length() + 1) * sizeof(WCHAR)); |
| 1014 | ERR_FAIL_NULL_MSG(mem, "Unable to allocate memory for clipboard contents."); |
| 1015 | |
| 1016 | LPWSTR lptstrCopy = (LPWSTR)GlobalLock(mem); |
| 1017 | memcpy(lptstrCopy, utf16.get_data(), (utf16.length() + 1) * sizeof(WCHAR)); |
| 1018 | GlobalUnlock(mem); |
| 1019 | |
| 1020 | SetClipboardData(CF_UNICODETEXT, mem); |
| 1021 | |
| 1022 | // Set the CF_TEXT version (not needed?). |
| 1023 | CharString utf8 = text.utf8(); |
| 1024 | mem = GlobalAlloc(GMEM_MOVEABLE, utf8.length() + 1); |
| 1025 | ERR_FAIL_NULL_MSG(mem, "Unable to allocate memory for clipboard contents."); |
| 1026 | |
| 1027 | LPTSTR ptr = (LPTSTR)GlobalLock(mem); |
| 1028 | memcpy(ptr, utf8.get_data(), utf8.length()); |
| 1029 | ptr[utf8.length()] = 0; |
| 1030 | GlobalUnlock(mem); |
| 1031 | |
| 1032 | SetClipboardData(CF_TEXT, mem); |
| 1033 | |
| 1034 | CloseClipboard(); |
| 1035 | } |
| 1036 | |
| 1037 | String DisplayServerWindows::clipboard_get() const { |
| 1038 | _THREAD_SAFE_METHOD_ |
| 1039 | |
| 1040 | if (!windows.has(MAIN_WINDOW_ID)) { |
| 1041 | return String(); |
| 1042 | } |
| 1043 | |
| 1044 | String ret; |
| 1045 | if (!OpenClipboard(windows[MAIN_WINDOW_ID].hWnd)) { |
| 1046 | ERR_FAIL_V_MSG("", "Unable to open clipboard."); |
| 1047 | } |
| 1048 | |
| 1049 | if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { |
| 1050 | HGLOBAL mem = GetClipboardData(CF_UNICODETEXT); |
| 1051 | if (mem != nullptr) { |
| 1052 | LPWSTR ptr = (LPWSTR)GlobalLock(mem); |
| 1053 | if (ptr != nullptr) { |
| 1054 | ret = String::utf16((const char16_t *)ptr); |
| 1055 | GlobalUnlock(mem); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | } else if (IsClipboardFormatAvailable(CF_TEXT)) { |
| 1060 | HGLOBAL mem = GetClipboardData(CF_UNICODETEXT); |
| 1061 | if (mem != nullptr) { |
| 1062 | LPTSTR ptr = (LPTSTR)GlobalLock(mem); |
| 1063 | if (ptr != nullptr) { |
| 1064 | ret.append_utf8((const char *)ptr); |
| 1065 | GlobalUnlock(mem); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | CloseClipboard(); |
| 1071 | |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
| 1075 | Ref<Image> DisplayServerWindows::clipboard_get_image() const { |
| 1076 | Ref<Image> image; |
| 1077 | if (!windows.has(last_focused_window)) { |
| 1078 | return image; // No focused window? |
| 1079 | } |
| 1080 | if (!OpenClipboard(windows[last_focused_window].hWnd)) { |
| 1081 | ERR_FAIL_V_MSG(image, "Unable to open clipboard."); |
| 1082 | } |
| 1083 | UINT png_format = RegisterClipboardFormatA("PNG"); |
| 1084 | if (png_format && IsClipboardFormatAvailable(png_format)) { |
| 1085 | HANDLE png_handle = GetClipboardData(png_format); |
| 1086 | if (png_handle) { |
| 1087 | size_t png_size = GlobalSize(png_handle); |
| 1088 | uint8_t *png_data = (uint8_t *)GlobalLock(png_handle); |
| 1089 | image.instantiate(); |
| 1090 | |
| 1091 | PNGDriverCommon::png_to_image(png_data, png_size, false, image); |
| 1092 | |
| 1093 | GlobalUnlock(png_handle); |
| 1094 | } |
| 1095 | } else if (IsClipboardFormatAvailable(CF_DIB)) { |
| 1096 | HGLOBAL mem = GetClipboardData(CF_DIB); |
| 1097 | if (mem != nullptr) { |
| 1098 | BITMAPINFO *ptr = static_cast<BITMAPINFO *>(GlobalLock(mem)); |
| 1099 | |
| 1100 | if (ptr != nullptr) { |
| 1101 | BITMAPINFOHEADER *info = &ptr->bmiHeader; |
| 1102 | void *dib_bits = (void *)(ptr->bmiColors); |
| 1103 | |
| 1104 | // Draw DIB image to temporary DC surface and read it back as BGRA8. |
| 1105 | HDC dc = GetDC(nullptr); |
| 1106 | if (dc) { |
| 1107 | HDC hdc = CreateCompatibleDC(dc); |
| 1108 | if (hdc) { |
| 1109 | HBITMAP hbm = CreateCompatibleBitmap(dc, info->biWidth, std::abs(info->biHeight)); |
| 1110 | if (hbm) { |
| 1111 | SelectObject(hdc, hbm); |
| 1112 | SetDIBitsToDevice(hdc, 0, 0, info->biWidth, std::abs(info->biHeight), 0, 0, 0, std::abs(info->biHeight), dib_bits, ptr, DIB_RGB_COLORS); |
| 1113 | |
| 1114 | BITMAPINFO bmp_info = {}; |
| 1115 | bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader); |
| 1116 | bmp_info.bmiHeader.biWidth = info->biWidth; |
| 1117 | bmp_info.bmiHeader.biHeight = -std::abs(info->biHeight); |
| 1118 | bmp_info.bmiHeader.biPlanes = 1; |
| 1119 | bmp_info.bmiHeader.biBitCount = 32; |
| 1120 | bmp_info.bmiHeader.biCompression = BI_RGB; |
| 1121 | |
| 1122 | Vector<uint8_t> img_data; |
| 1123 | img_data.resize(info->biWidth * std::abs(info->biHeight) * 4); |
| 1124 | GetDIBits(hdc, hbm, 0, std::abs(info->biHeight), img_data.ptrw(), &bmp_info, DIB_RGB_COLORS); |
| 1125 | |
| 1126 | uint8_t *wr = (uint8_t *)img_data.ptrw(); |
| 1127 | for (int i = 0; i < info->biWidth * std::abs(info->biHeight); i++) { |
| 1128 | SWAP(wr[i * 4 + 0], wr[i * 4 + 2]); // Swap B and R. |
| 1129 | if (info->biBitCount != 32) { |
| 1130 | wr[i * 4 + 3] = 255; // Set A to solid if it's not in the source image. |
| 1131 | } |
| 1132 | } |
| 1133 | image = Image::create_from_data(info->biWidth, std::abs(info->biHeight), false, Image::Format::FORMAT_RGBA8, img_data); |
| 1134 | |
| 1135 | DeleteObject(hbm); |
| 1136 | } |
| 1137 | DeleteDC(hdc); |
| 1138 | } |
| 1139 | ReleaseDC(nullptr, dc); |
| 1140 | } |
| 1141 | GlobalUnlock(mem); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | CloseClipboard(); |
| 1146 | |
| 1147 | return image; |
| 1148 | } |
| 1149 | |
| 1150 | bool DisplayServerWindows::clipboard_has() const { |
| 1151 | return (IsClipboardFormatAvailable(CF_TEXT) || |
| 1152 | IsClipboardFormatAvailable(CF_UNICODETEXT) || |
| 1153 | IsClipboardFormatAvailable(CF_OEMTEXT)); |
| 1154 | } |
| 1155 | |
| 1156 | bool DisplayServerWindows::clipboard_has_image() const { |
| 1157 | UINT png_format = RegisterClipboardFormatA("PNG"); |
| 1158 | return ((png_format && IsClipboardFormatAvailable(png_format)) || IsClipboardFormatAvailable(CF_DIB)); |
| 1159 | } |
| 1160 | |
| 1161 | typedef struct { |
| 1162 | int count; |
| 1163 | int screen; |
| 1164 | HMONITOR monitor; |
| 1165 | } EnumScreenData; |
| 1166 | |
| 1167 | static BOOL CALLBACK _MonitorEnumProcPrim(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1168 | EnumScreenData *data = (EnumScreenData *)dwData; |
| 1169 | if ((lprcMonitor->left == 0) && (lprcMonitor->top == 0)) { |
| 1170 | data->screen = data->count; |
| 1171 | return FALSE; |
| 1172 | } |
| 1173 | |
| 1174 | data->count++; |
| 1175 | return TRUE; |
| 1176 | } |
| 1177 | |
| 1178 | static BOOL CALLBACK _MonitorEnumProcScreen(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1179 | EnumScreenData *data = (EnumScreenData *)dwData; |
| 1180 | if (data->monitor == hMonitor) { |
| 1181 | data->screen = data->count; |
| 1182 | } |
| 1183 | |
| 1184 | data->count++; |
| 1185 | return TRUE; |
| 1186 | } |
| 1187 | |
| 1188 | static BOOL CALLBACK _MonitorEnumProcCount(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1189 | int *data = (int *)dwData; |
| 1190 | (*data)++; |
| 1191 | return TRUE; |
| 1192 | } |
| 1193 | |
| 1194 | int DisplayServerWindows::get_screen_count() const { |
| 1195 | _THREAD_SAFE_METHOD_ |
| 1196 | |
| 1197 | int data = 0; |
| 1198 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcCount, (LPARAM)&data); |
| 1199 | return data; |
| 1200 | } |
| 1201 | |
| 1202 | int DisplayServerWindows::get_primary_screen() const { |
| 1203 | EnumScreenData data = { 0, 0, nullptr }; |
| 1204 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcPrim, (LPARAM)&data); |
| 1205 | return data.screen; |
| 1206 | } |
| 1207 | |
| 1208 | int DisplayServerWindows::get_keyboard_focus_screen() const { |
| 1209 | HWND hwnd = GetForegroundWindow(); |
| 1210 | if (hwnd) { |
| 1211 | EnumScreenData data = { 0, 0, MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) }; |
| 1212 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcScreen, (LPARAM)&data); |
| 1213 | return data.screen; |
| 1214 | } else { |
| 1215 | return get_primary_screen(); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | typedef struct { |
| 1220 | int count; |
| 1221 | int screen; |
| 1222 | Point2 pos; |
| 1223 | } EnumPosData; |
| 1224 | |
| 1225 | static BOOL CALLBACK _MonitorEnumProcPos(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1226 | EnumPosData *data = (EnumPosData *)dwData; |
| 1227 | if (data->count == data->screen) { |
| 1228 | data->pos.x = lprcMonitor->left; |
| 1229 | data->pos.y = lprcMonitor->top; |
| 1230 | } |
| 1231 | |
| 1232 | data->count++; |
| 1233 | return TRUE; |
| 1234 | } |
| 1235 | |
| 1236 | static BOOL CALLBACK _MonitorEnumProcOrigin(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1237 | EnumPosData *data = (EnumPosData *)dwData; |
| 1238 | data->pos = data->pos.min(Point2(lprcMonitor->left, lprcMonitor->top)); |
| 1239 | |
| 1240 | return TRUE; |
| 1241 | } |
| 1242 | |
| 1243 | Point2i DisplayServerWindows::_get_screens_origin() const { |
| 1244 | _THREAD_SAFE_METHOD_ |
| 1245 | |
| 1246 | EnumPosData data = { 0, 0, Point2() }; |
| 1247 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcOrigin, (LPARAM)&data); |
| 1248 | return data.pos; |
| 1249 | } |
| 1250 | |
| 1251 | Point2i DisplayServerWindows::screen_get_position(int p_screen) const { |
| 1252 | _THREAD_SAFE_METHOD_ |
| 1253 | |
| 1254 | p_screen = _get_screen_index(p_screen); |
| 1255 | int screen_count = get_screen_count(); |
| 1256 | ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i()); |
| 1257 | |
| 1258 | EnumPosData data = { 0, p_screen, Point2() }; |
| 1259 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcPos, (LPARAM)&data); |
| 1260 | return data.pos - _get_screens_origin(); |
| 1261 | } |
| 1262 | |
| 1263 | typedef struct { |
| 1264 | int count; |
| 1265 | int screen; |
| 1266 | Size2 size; |
| 1267 | } EnumSizeData; |
| 1268 | |
| 1269 | typedef struct { |
| 1270 | int count; |
| 1271 | int screen; |
| 1272 | Rect2i rect; |
| 1273 | } EnumRectData; |
| 1274 | |
| 1275 | typedef struct { |
| 1276 | Vector<DISPLAYCONFIG_PATH_INFO> paths; |
| 1277 | Vector<DISPLAYCONFIG_MODE_INFO> modes; |
| 1278 | int count; |
| 1279 | int screen; |
| 1280 | float rate; |
| 1281 | } EnumRefreshRateData; |
| 1282 | |
| 1283 | static BOOL CALLBACK _MonitorEnumProcSize(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1284 | EnumSizeData *data = (EnumSizeData *)dwData; |
| 1285 | if (data->count == data->screen) { |
| 1286 | data->size.x = lprcMonitor->right - lprcMonitor->left; |
| 1287 | data->size.y = lprcMonitor->bottom - lprcMonitor->top; |
| 1288 | } |
| 1289 | |
| 1290 | data->count++; |
| 1291 | return TRUE; |
| 1292 | } |
| 1293 | |
| 1294 | Size2i DisplayServerWindows::screen_get_size(int p_screen) const { |
| 1295 | _THREAD_SAFE_METHOD_ |
| 1296 | |
| 1297 | p_screen = _get_screen_index(p_screen); |
| 1298 | int screen_count = get_screen_count(); |
| 1299 | ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i()); |
| 1300 | |
| 1301 | EnumSizeData data = { 0, p_screen, Size2() }; |
| 1302 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcSize, (LPARAM)&data); |
| 1303 | return data.size; |
| 1304 | } |
| 1305 | |
| 1306 | static BOOL CALLBACK _MonitorEnumProcUsableSize(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1307 | EnumRectData *data = (EnumRectData *)dwData; |
| 1308 | if (data->count == data->screen) { |
| 1309 | MONITORINFO minfo; |
| 1310 | memset(&minfo, 0, sizeof(MONITORINFO)); |
| 1311 | minfo.cbSize = sizeof(MONITORINFO); |
| 1312 | GetMonitorInfoA(hMonitor, &minfo); |
| 1313 | |
| 1314 | data->rect.position.x = minfo.rcWork.left; |
| 1315 | data->rect.position.y = minfo.rcWork.top; |
| 1316 | data->rect.size.x = minfo.rcWork.right - minfo.rcWork.left; |
| 1317 | data->rect.size.y = minfo.rcWork.bottom - minfo.rcWork.top; |
| 1318 | } |
| 1319 | |
| 1320 | data->count++; |
| 1321 | return TRUE; |
| 1322 | } |
| 1323 | |
| 1324 | static BOOL CALLBACK _MonitorEnumProcRefreshRate(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1325 | EnumRefreshRateData *data = (EnumRefreshRateData *)dwData; |
| 1326 | if (data->count == data->screen) { |
| 1327 | MONITORINFOEXW minfo; |
| 1328 | memset(&minfo, 0, sizeof(minfo)); |
| 1329 | minfo.cbSize = sizeof(minfo); |
| 1330 | GetMonitorInfoW(hMonitor, &minfo); |
| 1331 | |
| 1332 | bool found = false; |
| 1333 | for (const DISPLAYCONFIG_PATH_INFO &path : data->paths) { |
| 1334 | DISPLAYCONFIG_SOURCE_DEVICE_NAME source_name; |
| 1335 | memset(&source_name, 0, sizeof(source_name)); |
| 1336 | source_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME; |
| 1337 | source_name.header.size = sizeof(source_name); |
| 1338 | source_name.header.adapterId = path.sourceInfo.adapterId; |
| 1339 | source_name.header.id = path.sourceInfo.id; |
| 1340 | if (DisplayConfigGetDeviceInfo(&source_name.header) == ERROR_SUCCESS) { |
| 1341 | if (wcscmp(minfo.szDevice, source_name.viewGdiDeviceName) == 0 && path.targetInfo.refreshRate.Numerator != 0 && path.targetInfo.refreshRate.Denominator != 0) { |
| 1342 | data->rate = (double)path.targetInfo.refreshRate.Numerator / (double)path.targetInfo.refreshRate.Denominator; |
| 1343 | found = true; |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | if (!found) { |
| 1349 | DEVMODEW dm; |
| 1350 | memset(&dm, 0, sizeof(dm)); |
| 1351 | dm.dmSize = sizeof(dm); |
| 1352 | EnumDisplaySettingsW(minfo.szDevice, ENUM_CURRENT_SETTINGS, &dm); |
| 1353 | |
| 1354 | data->rate = dm.dmDisplayFrequency; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | data->count++; |
| 1359 | return TRUE; |
| 1360 | } |
| 1361 | |
| 1362 | Rect2i DisplayServerWindows::screen_get_usable_rect(int p_screen) const { |
| 1363 | _THREAD_SAFE_METHOD_ |
| 1364 | |
| 1365 | p_screen = _get_screen_index(p_screen); |
| 1366 | int screen_count = get_screen_count(); |
| 1367 | ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 1368 | |
| 1369 | EnumRectData data = { 0, p_screen, Rect2i() }; |
| 1370 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcUsableSize, (LPARAM)&data); |
| 1371 | data.rect.position -= _get_screens_origin(); |
| 1372 | return data.rect; |
| 1373 | } |
| 1374 | |
| 1375 | typedef struct { |
| 1376 | int current_index; |
| 1377 | int screen; |
| 1378 | int dpi; |
| 1379 | } EnumDpiData; |
| 1380 | |
| 1381 | static int QueryDpiForMonitor(HMONITOR hmon) { |
| 1382 | int dpiX = 96, dpiY = 96; |
| 1383 | |
| 1384 | UINT x = 0, y = 0; |
| 1385 | if (hmon) { |
| 1386 | HRESULT hr = GetDpiForMonitor(hmon, MDT_DEFAULT, &x, &y); |
| 1387 | if (SUCCEEDED(hr) && (x > 0) && (y > 0)) { |
| 1388 | dpiX = (int)x; |
| 1389 | dpiY = (int)y; |
| 1390 | } |
| 1391 | } else { |
| 1392 | static int overallX = 0, overallY = 0; |
| 1393 | if (overallX <= 0 || overallY <= 0) { |
| 1394 | HDC hdc = GetDC(nullptr); |
| 1395 | if (hdc) { |
| 1396 | overallX = GetDeviceCaps(hdc, LOGPIXELSX); |
| 1397 | overallY = GetDeviceCaps(hdc, LOGPIXELSY); |
| 1398 | ReleaseDC(nullptr, hdc); |
| 1399 | } |
| 1400 | } |
| 1401 | if (overallX > 0 && overallY > 0) { |
| 1402 | dpiX = overallX; |
| 1403 | dpiY = overallY; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | return (dpiX + dpiY) / 2; |
| 1408 | } |
| 1409 | |
| 1410 | static BOOL CALLBACK _MonitorEnumProcDpi(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { |
| 1411 | EnumDpiData *data = (EnumDpiData *)dwData; |
| 1412 | |
| 1413 | data->current_index++; |
| 1414 | |
| 1415 | if (data->current_index == data->screen) { |
| 1416 | data->dpi = QueryDpiForMonitor(hMonitor); |
| 1417 | return FALSE; |
| 1418 | } |
| 1419 | return TRUE; |
| 1420 | } |
| 1421 | |
| 1422 | int DisplayServerWindows::screen_get_dpi(int p_screen) const { |
| 1423 | _THREAD_SAFE_METHOD_ |
| 1424 | |
| 1425 | p_screen = _get_screen_index(p_screen); |
| 1426 | |
| 1427 | EnumDpiData data = { -1, p_screen, 96 }; |
| 1428 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcDpi, (LPARAM)&data); |
| 1429 | |
| 1430 | ERR_FAIL_COND_V_MSG(data.current_index < p_screen, 96, vformat("Screen index %d out of range [0, %d].", p_screen, data.current_index)); |
| 1431 | return data.dpi; |
| 1432 | } |
| 1433 | |
| 1434 | Color DisplayServerWindows::screen_get_pixel(const Point2i &p_position) const { |
| 1435 | Point2i pos = p_position + _get_screens_origin(); |
| 1436 | |
| 1437 | POINT p; |
| 1438 | p.x = pos.x; |
| 1439 | p.y = pos.y; |
| 1440 | LogicalToPhysicalPointForPerMonitorDPI(nullptr, &p); |
| 1441 | |
| 1442 | HDC dc = GetDC(nullptr); |
| 1443 | if (dc) { |
| 1444 | COLORREF col = GetPixel(dc, p.x, p.y); |
| 1445 | if (col != CLR_INVALID) { |
| 1446 | ReleaseDC(nullptr, dc); |
| 1447 | return Color(float(col & 0x000000FF) / 255.0f, float((col & 0x0000FF00) >> 8) / 255.0f, float((col & 0x00FF0000) >> 16) / 255.0f, 1.0f); |
| 1448 | } |
| 1449 | ReleaseDC(nullptr, dc); |
| 1450 | } |
| 1451 | |
| 1452 | return Color(); |
| 1453 | } |
| 1454 | |
| 1455 | Ref<Image> DisplayServerWindows::screen_get_image(int p_screen) const { |
| 1456 | p_screen = _get_screen_index(p_screen); |
| 1457 | int screen_count = get_screen_count(); |
| 1458 | ERR_FAIL_INDEX_V(p_screen, screen_count, Ref<Image>()); |
| 1459 | |
| 1460 | Point2i pos = screen_get_position(p_screen) + _get_screens_origin(); |
| 1461 | Size2i size = screen_get_size(p_screen); |
| 1462 | |
| 1463 | POINT p1; |
| 1464 | p1.x = pos.x; |
| 1465 | p1.y = pos.y; |
| 1466 | |
| 1467 | POINT p2; |
| 1468 | p2.x = pos.x + size.x; |
| 1469 | p2.y = pos.y + size.y; |
| 1470 | LogicalToPhysicalPointForPerMonitorDPI(nullptr, &p1); |
| 1471 | LogicalToPhysicalPointForPerMonitorDPI(nullptr, &p2); |
| 1472 | |
| 1473 | Ref<Image> img; |
| 1474 | HDC dc = GetDC(nullptr); |
| 1475 | if (dc) { |
| 1476 | HDC hdc = CreateCompatibleDC(dc); |
| 1477 | int width = p2.x - p1.x; |
| 1478 | int height = p2.y - p1.y; |
| 1479 | if (hdc) { |
| 1480 | HBITMAP hbm = CreateCompatibleBitmap(dc, width, height); |
| 1481 | if (hbm) { |
| 1482 | SelectObject(hdc, hbm); |
| 1483 | BitBlt(hdc, 0, 0, width, height, dc, p1.x, p1.y, SRCCOPY); |
| 1484 | |
| 1485 | BITMAPINFO bmp_info = {}; |
| 1486 | bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader); |
| 1487 | bmp_info.bmiHeader.biWidth = width; |
| 1488 | bmp_info.bmiHeader.biHeight = -height; |
| 1489 | bmp_info.bmiHeader.biPlanes = 1; |
| 1490 | bmp_info.bmiHeader.biBitCount = 32; |
| 1491 | bmp_info.bmiHeader.biCompression = BI_RGB; |
| 1492 | |
| 1493 | Vector<uint8_t> img_data; |
| 1494 | img_data.resize(width * height * 4); |
| 1495 | GetDIBits(hdc, hbm, 0, height, img_data.ptrw(), &bmp_info, DIB_RGB_COLORS); |
| 1496 | |
| 1497 | uint8_t *wr = (uint8_t *)img_data.ptrw(); |
| 1498 | for (int i = 0; i < width * height; i++) { |
| 1499 | SWAP(wr[i * 4 + 0], wr[i * 4 + 2]); // Swap B and R. |
| 1500 | } |
| 1501 | img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data); |
| 1502 | |
| 1503 | DeleteObject(hbm); |
| 1504 | } |
| 1505 | DeleteDC(hdc); |
| 1506 | } |
| 1507 | ReleaseDC(nullptr, dc); |
| 1508 | } |
| 1509 | |
| 1510 | return img; |
| 1511 | } |
| 1512 | |
| 1513 | Ref<Image> DisplayServerWindows::screen_get_image_rect(const Rect2i &p_rect) const { |
| 1514 | Point2i pos = p_rect.position + _get_screens_origin(); |
| 1515 | Size2i size = p_rect.size; |
| 1516 | |
| 1517 | POINT p1; |
| 1518 | p1.x = pos.x; |
| 1519 | p1.y = pos.y; |
| 1520 | |
| 1521 | POINT p2; |
| 1522 | p2.x = pos.x + size.x; |
| 1523 | p2.y = pos.y + size.y; |
| 1524 | LogicalToPhysicalPointForPerMonitorDPI(0, &p1); |
| 1525 | LogicalToPhysicalPointForPerMonitorDPI(0, &p2); |
| 1526 | |
| 1527 | Ref<Image> img; |
| 1528 | HDC dc = GetDC(0); |
| 1529 | if (dc) { |
| 1530 | HDC hdc = CreateCompatibleDC(dc); |
| 1531 | int width = p2.x - p1.x; |
| 1532 | int height = p2.y - p1.y; |
| 1533 | if (hdc) { |
| 1534 | HBITMAP hbm = CreateCompatibleBitmap(dc, width, height); |
| 1535 | if (hbm) { |
| 1536 | SelectObject(hdc, hbm); |
| 1537 | BitBlt(hdc, 0, 0, width, height, dc, p1.x, p1.y, SRCCOPY); |
| 1538 | |
| 1539 | BITMAPINFO bmp_info = {}; |
| 1540 | bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader); |
| 1541 | bmp_info.bmiHeader.biWidth = width; |
| 1542 | bmp_info.bmiHeader.biHeight = -height; |
| 1543 | bmp_info.bmiHeader.biPlanes = 1; |
| 1544 | bmp_info.bmiHeader.biBitCount = 32; |
| 1545 | bmp_info.bmiHeader.biCompression = BI_RGB; |
| 1546 | |
| 1547 | Vector<uint8_t> img_data; |
| 1548 | img_data.resize(width * height * 4); |
| 1549 | GetDIBits(hdc, hbm, 0, height, img_data.ptrw(), &bmp_info, DIB_RGB_COLORS); |
| 1550 | |
| 1551 | uint8_t *wr = (uint8_t *)img_data.ptrw(); |
| 1552 | for (int i = 0; i < width * height; i++) { |
| 1553 | SWAP(wr[i * 4 + 0], wr[i * 4 + 2]); // Swap B and R. |
| 1554 | } |
| 1555 | img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data); |
| 1556 | |
| 1557 | DeleteObject(hbm); |
| 1558 | } |
| 1559 | DeleteDC(hdc); |
| 1560 | } |
| 1561 | ReleaseDC(NULL, dc); |
| 1562 | } |
| 1563 | |
| 1564 | return img; |
| 1565 | } |
| 1566 | |
| 1567 | float DisplayServerWindows::screen_get_refresh_rate(int p_screen) const { |
| 1568 | _THREAD_SAFE_METHOD_ |
| 1569 | |
| 1570 | p_screen = _get_screen_index(p_screen); |
| 1571 | int screen_count = get_screen_count(); |
| 1572 | ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 1573 | |
| 1574 | EnumRefreshRateData data = { Vector<DISPLAYCONFIG_PATH_INFO>(), Vector<DISPLAYCONFIG_MODE_INFO>(), 0, p_screen, SCREEN_REFRESH_RATE_FALLBACK }; |
| 1575 | |
| 1576 | uint32_t path_count = 0; |
| 1577 | uint32_t mode_count = 0; |
| 1578 | if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &path_count, &mode_count) == ERROR_SUCCESS) { |
| 1579 | data.paths.resize(path_count); |
| 1580 | data.modes.resize(mode_count); |
| 1581 | if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &path_count, data.paths.ptrw(), &mode_count, data.modes.ptrw(), nullptr) != ERROR_SUCCESS) { |
| 1582 | data.paths.clear(); |
| 1583 | data.modes.clear(); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcRefreshRate, (LPARAM)&data); |
| 1588 | return data.rate; |
| 1589 | } |
| 1590 | |
| 1591 | void DisplayServerWindows::screen_set_keep_on(bool p_enable) { |
| 1592 | if (keep_screen_on == p_enable) { |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | if (p_enable) { |
| 1597 | const String reason = "Godot Engine running with display/window/energy_saving/keep_screen_on = true"; |
| 1598 | Char16String reason_utf16 = reason.utf16(); |
| 1599 | REASON_CONTEXT context; |
| 1600 | context.Version = POWER_REQUEST_CONTEXT_VERSION; |
| 1601 | context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; |
| 1602 | context.Reason.SimpleReasonString = (LPWSTR)(reason_utf16.ptrw()); |
| 1603 | power_request = PowerCreateRequest(&context); |
| 1604 | if (power_request == INVALID_HANDLE_VALUE) { |
| 1605 | print_error("Failed to enable screen_keep_on."); |
| 1606 | return; |
| 1607 | } |
| 1608 | if (PowerSetRequest(power_request, POWER_REQUEST_TYPE::PowerRequestSystemRequired) == 0) { |
| 1609 | print_error("Failed to request system sleep override."); |
| 1610 | return; |
| 1611 | } |
| 1612 | if (PowerSetRequest(power_request, POWER_REQUEST_TYPE::PowerRequestDisplayRequired) == 0) { |
| 1613 | print_error("Failed to request display timeout override."); |
| 1614 | return; |
| 1615 | } |
| 1616 | } else { |
| 1617 | PowerClearRequest(power_request, POWER_REQUEST_TYPE::PowerRequestSystemRequired); |
| 1618 | PowerClearRequest(power_request, POWER_REQUEST_TYPE::PowerRequestDisplayRequired); |
| 1619 | CloseHandle(power_request); |
| 1620 | power_request = nullptr; |
| 1621 | } |
| 1622 | |
| 1623 | keep_screen_on = p_enable; |
| 1624 | } |
| 1625 | |
| 1626 | bool DisplayServerWindows::screen_is_kept_on() const { |
| 1627 | return keep_screen_on; |
| 1628 | } |
| 1629 | |
| 1630 | Vector<DisplayServer::WindowID> DisplayServerWindows::get_window_list() const { |
| 1631 | _THREAD_SAFE_METHOD_ |
| 1632 | |
| 1633 | Vector<DisplayServer::WindowID> ret; |
| 1634 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 1635 | ret.push_back(E.key); |
| 1636 | } |
| 1637 | return ret; |
| 1638 | } |
| 1639 | |
| 1640 | DisplayServer::WindowID DisplayServerWindows::get_window_at_screen_position(const Point2i &p_position) const { |
| 1641 | Point2i offset = _get_screens_origin(); |
| 1642 | POINT p; |
| 1643 | p.x = p_position.x + offset.x; |
| 1644 | p.y = p_position.y + offset.y; |
| 1645 | HWND hwnd = WindowFromPoint(p); |
| 1646 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 1647 | if (E.value.hWnd == hwnd) { |
| 1648 | return E.key; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | return INVALID_WINDOW_ID; |
| 1653 | } |
| 1654 | |
| 1655 | DisplayServer::WindowID DisplayServerWindows::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent) { |
| 1656 | _THREAD_SAFE_METHOD_ |
| 1657 | |
| 1658 | bool no_redirection_bitmap = false; |
| 1659 | #ifdef DCOMP_ENABLED |
| 1660 | no_redirection_bitmap = OS::get_singleton()->is_layered_allowed() && rendering_driver == "d3d12"; |
| 1661 | #endif |
| 1662 | |
| 1663 | WindowID window_id = window_id_counter; |
| 1664 | Error err = _create_window(window_id, p_mode, p_flags, p_rect, p_exclusive, p_transient_parent, NULL, no_redirection_bitmap); |
| 1665 | ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Failed to create sub window."); |
| 1666 | ++window_id_counter; |
| 1667 | |
| 1668 | #ifdef RD_ENABLED |
| 1669 | if (rendering_context != nullptr) { |
| 1670 | _create_rendering_context_window(window_id, rendering_driver); |
| 1671 | } |
| 1672 | #endif |
| 1673 | #ifdef GLES3_ENABLED |
| 1674 | _create_gl_window(window_id); |
| 1675 | #endif |
| 1676 | |
| 1677 | window_set_vsync_mode(p_vsync_mode, window_id); |
| 1678 | |
| 1679 | WindowData &wd = windows[window_id]; |
| 1680 | |
| 1681 | if (p_flags & WINDOW_FLAG_RESIZE_DISABLED_BIT) { |
| 1682 | wd.resizable = false; |
| 1683 | } |
| 1684 | if (p_flags & WINDOW_FLAG_MINIMIZE_DISABLED_BIT) { |
| 1685 | wd.no_min_btn = true; |
| 1686 | } |
| 1687 | if (p_flags & WINDOW_FLAG_MAXIMIZE_DISABLED_BIT) { |
| 1688 | wd.no_max_btn = true; |
| 1689 | } |
| 1690 | if (p_flags & WINDOW_FLAG_BORDERLESS_BIT) { |
| 1691 | wd.borderless = true; |
| 1692 | } |
| 1693 | if (p_flags & WINDOW_FLAG_ALWAYS_ON_TOP_BIT && p_mode != WINDOW_MODE_FULLSCREEN && p_mode != WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 1694 | wd.always_on_top = true; |
| 1695 | } |
| 1696 | if (p_flags & WINDOW_FLAG_SHARP_CORNERS_BIT) { |
| 1697 | wd.sharp_corners = true; |
| 1698 | } |
| 1699 | if (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) { |
| 1700 | wd.no_focus = true; |
| 1701 | } |
| 1702 | if (p_flags & WINDOW_FLAG_MOUSE_PASSTHROUGH_BIT) { |
| 1703 | wd.mpass = true; |
| 1704 | } |
| 1705 | if (p_flags & WINDOW_FLAG_EXCLUDE_FROM_CAPTURE_BIT) { |
| 1706 | wd.hide_from_capture = true; |
| 1707 | if (os_ver.dwBuildNumber >= 19041) { |
| 1708 | SetWindowDisplayAffinity(wd.hWnd, WDA_EXCLUDEFROMCAPTURE); |
| 1709 | } else { |
| 1710 | SetWindowDisplayAffinity(wd.hWnd, WDA_MONITOR); |
| 1711 | } |
| 1712 | } |
| 1713 | if (p_flags & WINDOW_FLAG_POPUP_BIT) { |
| 1714 | wd.is_popup = true; |
| 1715 | } |
| 1716 | if (p_flags & WINDOW_FLAG_TRANSPARENT_BIT) { |
| 1717 | if (OS::get_singleton()->is_layered_allowed()) { |
| 1718 | DWM_BLURBEHIND bb; |
| 1719 | ZeroMemory(&bb, sizeof(bb)); |
| 1720 | HRGN hRgn = CreateRectRgn(0, 0, -1, -1); |
| 1721 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; |
| 1722 | bb.hRgnBlur = hRgn; |
| 1723 | bb.fEnable = TRUE; |
| 1724 | DwmEnableBlurBehindWindow(wd.hWnd, &bb); |
| 1725 | } |
| 1726 | |
| 1727 | wd.layered_window = true; |
| 1728 | } |
| 1729 | |
| 1730 | // Inherit icons from MAIN_WINDOW for all sub windows. |
| 1731 | HICON mainwindow_icon = (HICON)SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_GETICON, ICON_SMALL, 0); |
| 1732 | if (mainwindow_icon) { |
| 1733 | SendMessage(windows[window_id].hWnd, WM_SETICON, ICON_SMALL, (LPARAM)mainwindow_icon); |
| 1734 | } |
| 1735 | mainwindow_icon = (HICON)SendMessage(windows[MAIN_WINDOW_ID].hWnd, WM_GETICON, ICON_BIG, 0); |
| 1736 | if (mainwindow_icon) { |
| 1737 | SendMessage(windows[window_id].hWnd, WM_SETICON, ICON_BIG, (LPARAM)mainwindow_icon); |
| 1738 | } |
| 1739 | #ifdef RD_ENABLED |
| 1740 | if (rendering_device) { |
| 1741 | rendering_device->screen_create(window_id); |
| 1742 | } |
| 1743 | #endif |
| 1744 | return window_id; |
| 1745 | } |
| 1746 | |
| 1747 | bool DisplayServerWindows::_is_always_on_top_recursive(WindowID p_window) const { |
| 1748 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 1749 | |
| 1750 | const WindowData &wd = windows[p_window]; |
| 1751 | if (wd.always_on_top) { |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 1756 | return _is_always_on_top_recursive(wd.transient_parent); |
| 1757 | } |
| 1758 | |
| 1759 | return false; |
| 1760 | } |
| 1761 | |
| 1762 | void DisplayServerWindows::show_window(WindowID p_id) { |
| 1763 | ERR_FAIL_COND(!windows.has(p_id)); |
| 1764 | |
| 1765 | WindowData &wd = windows[p_id]; |
| 1766 | popup_open(p_id); |
| 1767 | |
| 1768 | if (p_id != MAIN_WINDOW_ID) { |
| 1769 | _update_window_style(p_id); |
| 1770 | } |
| 1771 | wd.initialized = true; |
| 1772 | |
| 1773 | if (wd.maximized) { |
| 1774 | ShowWindow(wd.hWnd, SW_SHOWMAXIMIZED); |
| 1775 | SetForegroundWindow(wd.hWnd); // Slightly higher priority. |
| 1776 | SetFocus(wd.hWnd); // Set keyboard focus. |
| 1777 | } else if (wd.minimized) { |
| 1778 | ShowWindow(wd.hWnd, SW_SHOWMINIMIZED); |
| 1779 | } else if (wd.no_focus) { |
| 1780 | // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow |
| 1781 | ShowWindow(wd.hWnd, SW_SHOWNA); |
| 1782 | } else if (wd.is_popup) { |
| 1783 | ShowWindow(wd.hWnd, SW_SHOWNA); |
| 1784 | SetFocus(wd.hWnd); // Set keyboard focus. |
| 1785 | } else { |
| 1786 | ShowWindow(wd.hWnd, SW_SHOW); |
| 1787 | SetForegroundWindow(wd.hWnd); // Slightly higher priority. |
| 1788 | SetFocus(wd.hWnd); // Set keyboard focus. |
| 1789 | } |
| 1790 | if (_is_always_on_top_recursive(p_id)) { |
| 1791 | SetWindowPos(wd.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | ((wd.no_focus || wd.is_popup) ? SWP_NOACTIVATE : 0)); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | void DisplayServerWindows::delete_sub_window(WindowID p_window) { |
| 1796 | _THREAD_SAFE_METHOD_ |
| 1797 | |
| 1798 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1799 | ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window cannot be deleted."); |
| 1800 | |
| 1801 | popup_close(p_window); |
| 1802 | |
| 1803 | WindowData &wd = windows[p_window]; |
| 1804 | |
| 1805 | while (wd.transient_children.size()) { |
| 1806 | window_set_transient(*wd.transient_children.begin(), INVALID_WINDOW_ID); |
| 1807 | } |
| 1808 | |
| 1809 | if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 1810 | window_set_transient(p_window, INVALID_WINDOW_ID); |
| 1811 | } |
| 1812 | |
| 1813 | #ifdef RD_ENABLED |
| 1814 | if (rendering_device) { |
| 1815 | rendering_device->screen_free(p_window); |
| 1816 | } |
| 1817 | |
| 1818 | if (rendering_context) { |
| 1819 | rendering_context->window_destroy(p_window); |
| 1820 | } |
| 1821 | #endif |
| 1822 | #ifdef GLES3_ENABLED |
| 1823 | if (gl_manager_angle) { |
| 1824 | gl_manager_angle->window_destroy(p_window); |
| 1825 | } |
| 1826 | if (gl_manager_native) { |
| 1827 | gl_manager_native->window_destroy(p_window); |
| 1828 | } |
| 1829 | #endif |
| 1830 | |
| 1831 | _destroy_window(p_window); |
| 1832 | |
| 1833 | if (last_focused_window == p_window) { |
| 1834 | last_focused_window = INVALID_WINDOW_ID; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | void DisplayServerWindows::gl_window_make_current(DisplayServer::WindowID p_window_id) { |
| 1839 | #if defined(GLES3_ENABLED) |
| 1840 | if (gl_manager_angle) { |
| 1841 | gl_manager_angle->window_make_current(p_window_id); |
| 1842 | } |
| 1843 | if (gl_manager_native) { |
| 1844 | gl_manager_native->window_make_current(p_window_id); |
| 1845 | } |
| 1846 | #endif |
| 1847 | } |
| 1848 | |
| 1849 | int64_t DisplayServerWindows::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const { |
| 1850 | ERR_FAIL_COND_V(!windows.has(p_window), 0); |
| 1851 | switch (p_handle_type) { |
| 1852 | case DISPLAY_HANDLE: { |
| 1853 | return 0; // Not supported. |
| 1854 | } |
| 1855 | case WINDOW_HANDLE: { |
| 1856 | return (int64_t)windows[p_window].hWnd; |
| 1857 | } |
| 1858 | #if defined(GLES3_ENABLED) |
| 1859 | case WINDOW_VIEW: { |
| 1860 | if (gl_manager_native) { |
| 1861 | return (int64_t)gl_manager_native->get_hdc(p_window); |
| 1862 | } else { |
| 1863 | return (int64_t)GetDC(windows[p_window].hWnd); |
| 1864 | } |
| 1865 | } |
| 1866 | case OPENGL_CONTEXT: { |
| 1867 | if (gl_manager_native) { |
| 1868 | return (int64_t)gl_manager_native->get_hglrc(p_window); |
| 1869 | } |
| 1870 | if (gl_manager_angle) { |
| 1871 | return (int64_t)gl_manager_angle->get_context(p_window); |
| 1872 | } |
| 1873 | return 0; |
| 1874 | } |
| 1875 | case EGL_DISPLAY: { |
| 1876 | if (gl_manager_angle) { |
| 1877 | return (int64_t)gl_manager_angle->get_display(p_window); |
| 1878 | } |
| 1879 | return 0; |
| 1880 | } |
| 1881 | case EGL_CONFIG: { |
| 1882 | if (gl_manager_angle) { |
| 1883 | return (int64_t)gl_manager_angle->get_config(p_window); |
| 1884 | } |
| 1885 | return 0; |
| 1886 | } |
| 1887 | #endif |
| 1888 | default: { |
| 1889 | return 0; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | void DisplayServerWindows::window_attach_instance_id(ObjectID p_instance, WindowID p_window) { |
| 1895 | _THREAD_SAFE_METHOD_ |
| 1896 | |
| 1897 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1898 | windows[p_window].instance_id = p_instance; |
| 1899 | } |
| 1900 | |
| 1901 | ObjectID DisplayServerWindows::window_get_attached_instance_id(WindowID p_window) const { |
| 1902 | _THREAD_SAFE_METHOD_ |
| 1903 | |
| 1904 | ERR_FAIL_COND_V(!windows.has(p_window), ObjectID()); |
| 1905 | return windows[p_window].instance_id; |
| 1906 | } |
| 1907 | |
| 1908 | void DisplayServerWindows::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) { |
| 1909 | _THREAD_SAFE_METHOD_ |
| 1910 | |
| 1911 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1912 | windows[p_window].rect_changed_callback = p_callable; |
| 1913 | } |
| 1914 | |
| 1915 | void DisplayServerWindows::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1916 | _THREAD_SAFE_METHOD_ |
| 1917 | |
| 1918 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1919 | windows[p_window].event_callback = p_callable; |
| 1920 | } |
| 1921 | |
| 1922 | void DisplayServerWindows::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1923 | _THREAD_SAFE_METHOD_ |
| 1924 | |
| 1925 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1926 | windows[p_window].input_event_callback = p_callable; |
| 1927 | } |
| 1928 | |
| 1929 | void DisplayServerWindows::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) { |
| 1930 | _THREAD_SAFE_METHOD_ |
| 1931 | |
| 1932 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1933 | windows[p_window].input_text_callback = p_callable; |
| 1934 | } |
| 1935 | |
| 1936 | void DisplayServerWindows::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) { |
| 1937 | _THREAD_SAFE_METHOD_ |
| 1938 | |
| 1939 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1940 | WindowData &window_data = windows[p_window]; |
| 1941 | |
| 1942 | window_data.drop_files_callback = p_callable; |
| 1943 | |
| 1944 | if (window_data.drop_target == nullptr) { |
| 1945 | window_data.drop_target = memnew(DropTargetWindows(&window_data)); |
| 1946 | ERR_FAIL_COND(RegisterDragDrop(window_data.hWnd, window_data.drop_target) != S_OK); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | void DisplayServerWindows::window_set_title(const String &p_title, WindowID p_window) { |
| 1951 | _THREAD_SAFE_METHOD_ |
| 1952 | |
| 1953 | ERR_FAIL_COND(!windows.has(p_window)); |
| 1954 | SetWindowTextW(windows[p_window].hWnd, (LPCWSTR)(p_title.utf16().get_data())); |
| 1955 | } |
| 1956 | |
| 1957 | Size2i DisplayServerWindows::window_get_title_size(const String &p_title, WindowID p_window) const { |
| 1958 | _THREAD_SAFE_METHOD_ |
| 1959 | |
| 1960 | Size2i size; |
| 1961 | ERR_FAIL_COND_V(!windows.has(p_window), size); |
| 1962 | |
| 1963 | const WindowData &wd = windows[p_window]; |
| 1964 | if (wd.fullscreen || wd.minimized || wd.borderless) { |
| 1965 | return size; |
| 1966 | } |
| 1967 | |
| 1968 | HDC hdc = GetDCEx(wd.hWnd, nullptr, DCX_WINDOW); |
| 1969 | if (hdc) { |
| 1970 | Char16String s = p_title.utf16(); |
| 1971 | SIZE text_size; |
| 1972 | if (GetTextExtentPoint32W(hdc, (LPCWSTR)(s.get_data()), s.length(), &text_size)) { |
| 1973 | size.x = text_size.cx; |
| 1974 | size.y = text_size.cy; |
| 1975 | } |
| 1976 | |
| 1977 | ReleaseDC(wd.hWnd, hdc); |
| 1978 | } |
| 1979 | RECT rect; |
| 1980 | if (DwmGetWindowAttribute(wd.hWnd, DWMWA_CAPTION_BUTTON_BOUNDS, &rect, sizeof(RECT)) == S_OK) { |
| 1981 | if (rect.right - rect.left > 0) { |
| 1982 | ClientToScreen(wd.hWnd, (POINT *)&rect.left); |
| 1983 | ClientToScreen(wd.hWnd, (POINT *)&rect.right); |
| 1984 | |
| 1985 | PhysicalToLogicalPointForPerMonitorDPI(nullptr, (POINT *)&rect.left); |
| 1986 | PhysicalToLogicalPointForPerMonitorDPI(nullptr, (POINT *)&rect.right); |
| 1987 | |
| 1988 | size.x += (rect.right - rect.left); |
| 1989 | size.y = MAX(size.y, rect.bottom - rect.top); |
| 1990 | } |
| 1991 | } |
| 1992 | if (icon_big) { |
| 1993 | size.x += 32; |
| 1994 | } else { |
| 1995 | size.x += 16; |
| 1996 | } |
| 1997 | return size; |
| 1998 | } |
| 1999 | |
| 2000 | void DisplayServerWindows::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) { |
| 2001 | _THREAD_SAFE_METHOD_ |
| 2002 | |
| 2003 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2004 | windows[p_window].mpath = p_region; |
| 2005 | _update_window_mouse_passthrough(p_window); |
| 2006 | } |
| 2007 | |
| 2008 | void DisplayServerWindows::_update_window_mouse_passthrough(WindowID p_window) { |
| 2009 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2010 | |
| 2011 | const WindowData &wd = windows[p_window]; |
| 2012 | bool clip_pixel = (wd.multiwindow_fs || (wd.borderless && wd.maximized)); |
| 2013 | bool pass_set = (wd.mpath.size() > 0); |
| 2014 | if (!clip_pixel && !pass_set) { |
| 2015 | SetWindowRgn(wd.hWnd, nullptr, TRUE); |
| 2016 | } else { |
| 2017 | HRGN region = nullptr; |
| 2018 | if (pass_set) { |
| 2019 | Vector<POINT> points; |
| 2020 | points.resize(wd.mpath.size()); |
| 2021 | POINT *points_ptr = points.ptrw(); |
| 2022 | for (int i = 0; i < wd.mpath.size(); i++) { |
| 2023 | if (wd.borderless) { |
| 2024 | points_ptr[i].x = wd.mpath[i].x; |
| 2025 | points_ptr[i].y = wd.mpath[i].y; |
| 2026 | } else { |
| 2027 | points_ptr[i].x = wd.mpath[i].x + GetSystemMetrics(SM_CXSIZEFRAME); |
| 2028 | points_ptr[i].y = wd.mpath[i].y + GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION); |
| 2029 | } |
| 2030 | } |
| 2031 | region = CreatePolygonRgn(points.ptr(), points.size(), ALTERNATE); |
| 2032 | } else { |
| 2033 | region = CreateRectRgn(0, 0, wd.width, wd.height); |
| 2034 | } |
| 2035 | if (clip_pixel) { |
| 2036 | HRGN region_clip = CreateRectRgn(0, 0, wd.width, wd.height); |
| 2037 | CombineRgn(region, region, region_clip, RGN_AND); |
| 2038 | DeleteObject(region_clip); |
| 2039 | } |
| 2040 | SetWindowRgn(wd.hWnd, region, FALSE); |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | int DisplayServerWindows::window_get_current_screen(WindowID p_window) const { |
| 2045 | _THREAD_SAFE_METHOD_ |
| 2046 | |
| 2047 | ERR_FAIL_COND_V(!windows.has(p_window), INVALID_SCREEN); |
| 2048 | |
| 2049 | EnumScreenData data = { 0, 0, MonitorFromWindow(windows[p_window].hWnd, MONITOR_DEFAULTTONEAREST) }; |
| 2050 | EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcScreen, (LPARAM)&data); |
| 2051 | return data.screen; |
| 2052 | } |
| 2053 | |
| 2054 | void DisplayServerWindows::window_set_current_screen(int p_screen, WindowID p_window) { |
| 2055 | _THREAD_SAFE_METHOD_ |
| 2056 | |
| 2057 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2058 | |
| 2059 | p_screen = _get_screen_index(p_screen); |
| 2060 | int screen_count = get_screen_count(); |
| 2061 | ERR_FAIL_INDEX(p_screen, screen_count); |
| 2062 | |
| 2063 | if (window_get_current_screen(p_window) == p_screen) { |
| 2064 | return; |
| 2065 | } |
| 2066 | const WindowData &wd = windows[p_window]; |
| 2067 | |
| 2068 | if (wd.parent_hwnd) { |
| 2069 | print_line("Embedded window can't be moved to another screen."); |
| 2070 | return; |
| 2071 | } |
| 2072 | if (wd.fullscreen) { |
| 2073 | Point2 pos = screen_get_position(p_screen) + _get_screens_origin(); |
| 2074 | Size2 size = screen_get_size(p_screen); |
| 2075 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(p_screen) : Vector2i(); |
| 2076 | |
| 2077 | MoveWindow(wd.hWnd, pos.x, pos.y, size.width + off.x, size.height + off.y, TRUE); |
| 2078 | } else if (wd.maximized) { |
| 2079 | Point2 pos = screen_get_position(p_screen) + _get_screens_origin(); |
| 2080 | Size2 size = screen_get_size(p_screen); |
| 2081 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(p_screen) : Vector2i(); |
| 2082 | |
| 2083 | ShowWindow(wd.hWnd, SW_RESTORE); |
| 2084 | MoveWindow(wd.hWnd, pos.x, pos.y, size.width + off.x, size.height + off.y, TRUE); |
| 2085 | ShowWindow(wd.hWnd, SW_MAXIMIZE); |
| 2086 | } else { |
| 2087 | Rect2i srect = screen_get_usable_rect(p_screen); |
| 2088 | Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window)); |
| 2089 | Size2i wsize = window_get_size(p_window); |
| 2090 | wpos += srect.position; |
| 2091 | |
| 2092 | wpos = wpos.clamp(srect.position, srect.position + srect.size - wsize / 3); |
| 2093 | window_set_position(wpos, p_window); |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | Point2i DisplayServerWindows::window_get_position(WindowID p_window) const { |
| 2098 | _THREAD_SAFE_METHOD_ |
| 2099 | |
| 2100 | ERR_FAIL_COND_V(!windows.has(p_window), Point2i()); |
| 2101 | const WindowData &wd = windows[p_window]; |
| 2102 | |
| 2103 | if (wd.minimized) { |
| 2104 | return wd.last_pos; |
| 2105 | } |
| 2106 | |
| 2107 | POINT point; |
| 2108 | point.x = 0; |
| 2109 | point.y = 0; |
| 2110 | |
| 2111 | ClientToScreen(wd.hWnd, &point); |
| 2112 | |
| 2113 | return Point2i(point.x, point.y) - _get_screens_origin(); |
| 2114 | } |
| 2115 | |
| 2116 | Point2i DisplayServerWindows::window_get_position_with_decorations(WindowID p_window) const { |
| 2117 | _THREAD_SAFE_METHOD_ |
| 2118 | |
| 2119 | ERR_FAIL_COND_V(!windows.has(p_window), Point2i()); |
| 2120 | const WindowData &wd = windows[p_window]; |
| 2121 | |
| 2122 | if (wd.minimized) { |
| 2123 | return wd.last_pos; |
| 2124 | } |
| 2125 | |
| 2126 | RECT r; |
| 2127 | if (GetWindowRect(wd.hWnd, &r)) { |
| 2128 | return Point2i(r.left, r.top) - _get_screens_origin(); |
| 2129 | } |
| 2130 | |
| 2131 | return Point2i(); |
| 2132 | } |
| 2133 | |
| 2134 | void DisplayServerWindows::_update_real_mouse_position(WindowID p_window) { |
| 2135 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2136 | |
| 2137 | POINT mouse_pos; |
| 2138 | if (GetCursorPos(&mouse_pos) && ScreenToClient(windows[p_window].hWnd, &mouse_pos)) { |
| 2139 | if (mouse_pos.x > 0 && mouse_pos.y > 0 && mouse_pos.x <= windows[p_window].width && mouse_pos.y <= windows[p_window].height) { |
| 2140 | old_x = mouse_pos.x; |
| 2141 | old_y = mouse_pos.y; |
| 2142 | old_invalid = false; |
| 2143 | Input::get_singleton()->set_mouse_position(Point2i(mouse_pos.x, mouse_pos.y)); |
| 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | void DisplayServerWindows::window_set_position(const Point2i &p_position, WindowID p_window) { |
| 2149 | _THREAD_SAFE_METHOD_ |
| 2150 | |
| 2151 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2152 | WindowData &wd = windows[p_window]; |
| 2153 | |
| 2154 | if (wd.parent_hwnd) { |
| 2155 | print_line("Embedded window can't be moved."); |
| 2156 | return; |
| 2157 | } |
| 2158 | |
| 2159 | if (wd.fullscreen || wd.maximized) { |
| 2160 | return; |
| 2161 | } |
| 2162 | |
| 2163 | Point2i offset = _get_screens_origin(); |
| 2164 | |
| 2165 | RECT rc; |
| 2166 | rc.left = p_position.x + offset.x; |
| 2167 | rc.right = p_position.x + wd.width + offset.x; |
| 2168 | rc.bottom = p_position.y + wd.height + offset.y; |
| 2169 | rc.top = p_position.y + offset.y; |
| 2170 | |
| 2171 | const DWORD style = GetWindowLongPtr(wd.hWnd, GWL_STYLE); |
| 2172 | const DWORD exStyle = GetWindowLongPtr(wd.hWnd, GWL_EXSTYLE); |
| 2173 | |
| 2174 | AdjustWindowRectEx(&rc, style, false, exStyle); |
| 2175 | MoveWindow(wd.hWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE); |
| 2176 | |
| 2177 | wd.last_pos = p_position; |
| 2178 | _update_real_mouse_position(p_window); |
| 2179 | } |
| 2180 | |
| 2181 | void DisplayServerWindows::window_set_exclusive(WindowID p_window, bool p_exclusive) { |
| 2182 | _THREAD_SAFE_METHOD_ |
| 2183 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2184 | WindowData &wd = windows[p_window]; |
| 2185 | if (wd.exclusive != p_exclusive) { |
| 2186 | wd.exclusive = p_exclusive; |
| 2187 | if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 2188 | if (wd.exclusive) { |
| 2189 | WindowData &wd_parent = windows[wd.transient_parent]; |
| 2190 | SetWindowLongPtr(wd.hWnd, GWLP_HWNDPARENT, (LONG_PTR)wd_parent.hWnd); |
| 2191 | } else { |
| 2192 | SetWindowLongPtr(wd.hWnd, GWLP_HWNDPARENT, (LONG_PTR) nullptr); |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | void DisplayServerWindows::window_set_transient(WindowID p_window, WindowID p_parent) { |
| 2199 | _THREAD_SAFE_METHOD_ |
| 2200 | |
| 2201 | ERR_FAIL_COND(p_window == p_parent); |
| 2202 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2203 | |
| 2204 | WindowData &wd_window = windows[p_window]; |
| 2205 | |
| 2206 | ERR_FAIL_COND(wd_window.transient_parent == p_parent); |
| 2207 | ERR_FAIL_COND_MSG(wd_window.always_on_top, "Windows with the 'on top' can't become transient."); |
| 2208 | |
| 2209 | if (p_parent == INVALID_WINDOW_ID) { |
| 2210 | // Remove transient. |
| 2211 | |
| 2212 | ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID); |
| 2213 | ERR_FAIL_COND(!windows.has(wd_window.transient_parent)); |
| 2214 | |
| 2215 | WindowData &wd_parent = windows[wd_window.transient_parent]; |
| 2216 | |
| 2217 | wd_window.transient_parent = INVALID_WINDOW_ID; |
| 2218 | wd_parent.transient_children.erase(p_window); |
| 2219 | |
| 2220 | if (wd_window.exclusive) { |
| 2221 | SetWindowLongPtr(wd_window.hWnd, GWLP_HWNDPARENT, (LONG_PTR) nullptr); |
| 2222 | } |
| 2223 | } else { |
| 2224 | ERR_FAIL_COND(!windows.has(p_parent)); |
| 2225 | ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent"); |
| 2226 | WindowData &wd_parent = windows[p_parent]; |
| 2227 | |
| 2228 | wd_window.transient_parent = p_parent; |
| 2229 | wd_parent.transient_children.insert(p_window); |
| 2230 | |
| 2231 | if (wd_window.exclusive) { |
| 2232 | SetWindowLongPtr(wd_window.hWnd, GWLP_HWNDPARENT, (LONG_PTR)wd_parent.hWnd); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | void DisplayServerWindows::window_set_max_size(const Size2i p_size, WindowID p_window) { |
| 2238 | _THREAD_SAFE_METHOD_ |
| 2239 | |
| 2240 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2241 | WindowData &wd = windows[p_window]; |
| 2242 | |
| 2243 | if (wd.parent_hwnd) { |
| 2244 | print_line("Embedded windows can't have a maximum size."); |
| 2245 | return; |
| 2246 | } |
| 2247 | |
| 2248 | if ((p_size != Size2()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) { |
| 2249 | ERR_PRINT("Maximum window size can't be smaller than minimum window size!"); |
| 2250 | return; |
| 2251 | } |
| 2252 | wd.max_size = p_size; |
| 2253 | } |
| 2254 | |
| 2255 | Size2i DisplayServerWindows::window_get_max_size(WindowID p_window) const { |
| 2256 | _THREAD_SAFE_METHOD_ |
| 2257 | |
| 2258 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2259 | const WindowData &wd = windows[p_window]; |
| 2260 | return wd.max_size; |
| 2261 | } |
| 2262 | |
| 2263 | void DisplayServerWindows::window_set_min_size(const Size2i p_size, WindowID p_window) { |
| 2264 | _THREAD_SAFE_METHOD_ |
| 2265 | |
| 2266 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2267 | WindowData &wd = windows[p_window]; |
| 2268 | |
| 2269 | if (wd.parent_hwnd) { |
| 2270 | print_line("Embedded windows can't have a minimum size."); |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | if ((p_size != Size2()) && (wd.max_size != Size2()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) { |
| 2275 | ERR_PRINT("Minimum window size can't be larger than maximum window size!"); |
| 2276 | return; |
| 2277 | } |
| 2278 | wd.min_size = p_size; |
| 2279 | } |
| 2280 | |
| 2281 | Size2i DisplayServerWindows::window_get_min_size(WindowID p_window) const { |
| 2282 | _THREAD_SAFE_METHOD_ |
| 2283 | |
| 2284 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2285 | const WindowData &wd = windows[p_window]; |
| 2286 | return wd.min_size; |
| 2287 | } |
| 2288 | |
| 2289 | void DisplayServerWindows::window_set_size(const Size2i p_size, WindowID p_window) { |
| 2290 | _THREAD_SAFE_METHOD_ |
| 2291 | |
| 2292 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2293 | WindowData &wd = windows[p_window]; |
| 2294 | |
| 2295 | if (wd.parent_hwnd) { |
| 2296 | print_line("Embedded window can't be resized."); |
| 2297 | return; |
| 2298 | } |
| 2299 | |
| 2300 | if (wd.fullscreen || wd.maximized) { |
| 2301 | return; |
| 2302 | } |
| 2303 | |
| 2304 | int w = p_size.width; |
| 2305 | int h = p_size.height; |
| 2306 | RECT rect; |
| 2307 | GetWindowRect(wd.hWnd, &rect); |
| 2308 | |
| 2309 | if (!wd.borderless) { |
| 2310 | RECT crect; |
| 2311 | GetClientRect(wd.hWnd, &crect); |
| 2312 | |
| 2313 | w += (rect.right - rect.left) - (crect.right - crect.left); |
| 2314 | h += (rect.bottom - rect.top) - (crect.bottom - crect.top); |
| 2315 | } |
| 2316 | |
| 2317 | MoveWindow(wd.hWnd, rect.left, rect.top, w, h, TRUE); |
| 2318 | } |
| 2319 | |
| 2320 | Size2i DisplayServerWindows::window_get_size(WindowID p_window) const { |
| 2321 | _THREAD_SAFE_METHOD_ |
| 2322 | |
| 2323 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2324 | const WindowData &wd = windows[p_window]; |
| 2325 | |
| 2326 | // GetClientRect() returns a zero rect for a minimized window, so we need to get the size in another way. |
| 2327 | if (wd.minimized) { |
| 2328 | return Size2(wd.width, wd.height); |
| 2329 | } |
| 2330 | |
| 2331 | RECT r; |
| 2332 | if (GetClientRect(wd.hWnd, &r)) { // Retrieves area inside of window border, including decoration. |
| 2333 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(window_get_current_screen(p_window)) : Vector2i(); |
| 2334 | return Size2(r.right - r.left - off.x, r.bottom - r.top - off.y); |
| 2335 | } |
| 2336 | return Size2(); |
| 2337 | } |
| 2338 | |
| 2339 | Size2i DisplayServerWindows::window_get_size_with_decorations(WindowID p_window) const { |
| 2340 | _THREAD_SAFE_METHOD_ |
| 2341 | |
| 2342 | ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2343 | const WindowData &wd = windows[p_window]; |
| 2344 | |
| 2345 | // GetWindowRect() returns a zero rect for a minimized window, so we need to get the size in another way. |
| 2346 | if (wd.minimized) { |
| 2347 | return Size2(wd.width_with_decorations, wd.height_with_decorations); |
| 2348 | } |
| 2349 | |
| 2350 | RECT r; |
| 2351 | if (GetWindowRect(wd.hWnd, &r)) { // Retrieves area inside of window border, including decoration. |
| 2352 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(window_get_current_screen(p_window)) : Vector2i(); |
| 2353 | return Size2(r.right - r.left - off.x, r.bottom - r.top - off.y); |
| 2354 | } |
| 2355 | return Size2(); |
| 2356 | } |
| 2357 | |
| 2358 | void DisplayServerWindows::_get_window_style(bool p_main_window, bool p_initialized, bool p_fullscreen, bool p_multiwindow_fs, bool p_borderless, bool p_resizable, bool p_no_min_btn, bool p_no_max_btn, bool p_minimized, bool p_maximized, bool p_maximized_fs, bool p_no_activate_focus, bool p_embed_child, bool p_no_redirection_bitmap, DWORD &r_style, DWORD &r_style_ex) { |
| 2359 | // Windows docs for window styles: |
| 2360 | // https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles |
| 2361 | // https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles |
| 2362 | |
| 2363 | r_style = 0; |
| 2364 | r_style_ex = WS_EX_WINDOWEDGE; |
| 2365 | if (p_main_window) { |
| 2366 | // When embedded, we don't want the window to have WS_EX_APPWINDOW because it will |
| 2367 | // show the embedded process in the taskbar and Alt-Tab. |
| 2368 | if (!p_embed_child) { |
| 2369 | r_style_ex |= WS_EX_APPWINDOW; |
| 2370 | } |
| 2371 | if (p_initialized) { |
| 2372 | r_style |= WS_VISIBLE; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | if (p_embed_child) { |
| 2377 | r_style |= WS_POPUP; |
| 2378 | } else if (p_fullscreen || p_borderless) { |
| 2379 | r_style |= WS_POPUP; // p_borderless was WS_EX_TOOLWINDOW in the past. |
| 2380 | if (p_minimized) { |
| 2381 | r_style |= WS_MINIMIZE; |
| 2382 | } else if (p_maximized) { |
| 2383 | r_style |= WS_MAXIMIZE; |
| 2384 | } |
| 2385 | if (!p_fullscreen) { |
| 2386 | r_style |= WS_SYSMENU; |
| 2387 | if (!p_no_min_btn) { |
| 2388 | r_style |= WS_MINIMIZEBOX; |
| 2389 | } |
| 2390 | if (!p_no_max_btn) { |
| 2391 | r_style |= WS_MAXIMIZEBOX; |
| 2392 | } |
| 2393 | } |
| 2394 | } else { |
| 2395 | if (p_resizable) { |
| 2396 | if (p_minimized) { |
| 2397 | r_style = WS_OVERLAPPEDWINDOW | WS_MINIMIZE; |
| 2398 | } else if (p_maximized) { |
| 2399 | r_style = WS_OVERLAPPEDWINDOW | WS_MAXIMIZE; |
| 2400 | } else { |
| 2401 | r_style = WS_OVERLAPPEDWINDOW; |
| 2402 | } |
| 2403 | if (p_no_min_btn) { |
| 2404 | r_style &= ~WS_MINIMIZEBOX; |
| 2405 | } |
| 2406 | if (p_no_max_btn) { |
| 2407 | r_style &= ~WS_MAXIMIZEBOX; |
| 2408 | } |
| 2409 | } else { |
| 2410 | if (p_minimized) { |
| 2411 | r_style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZE; |
| 2412 | } else { |
| 2413 | r_style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU; |
| 2414 | } |
| 2415 | if (!p_no_min_btn) { |
| 2416 | r_style |= WS_MINIMIZEBOX; |
| 2417 | } |
| 2418 | if (!p_no_max_btn) { |
| 2419 | r_style |= WS_MAXIMIZEBOX; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | if (p_no_activate_focus && !p_embed_child) { |
| 2425 | r_style_ex |= WS_EX_TOPMOST | WS_EX_NOACTIVATE; |
| 2426 | } |
| 2427 | |
| 2428 | if (!p_borderless && !p_no_activate_focus && p_initialized) { |
| 2429 | r_style |= WS_VISIBLE; |
| 2430 | } |
| 2431 | |
| 2432 | r_style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS; |
| 2433 | r_style_ex |= WS_EX_ACCEPTFILES; |
| 2434 | |
| 2435 | if (p_no_redirection_bitmap) { |
| 2436 | r_style_ex |= WS_EX_NOREDIRECTIONBITMAP; |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | void DisplayServerWindows::_update_window_style(WindowID p_window, bool p_repaint) { |
| 2441 | _THREAD_SAFE_METHOD_ |
| 2442 | |
| 2443 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2444 | WindowData &wd = windows[p_window]; |
| 2445 | |
| 2446 | DWORD style = 0; |
| 2447 | DWORD style_ex = 0; |
| 2448 | |
| 2449 | _get_window_style(p_window == MAIN_WINDOW_ID, wd.initialized, wd.fullscreen, wd.multiwindow_fs, wd.borderless, wd.resizable, wd.no_min_btn, wd.no_max_btn, wd.minimized, wd.maximized, wd.maximized_fs, wd.no_focus || wd.is_popup, wd.parent_hwnd, wd.no_redirection_bitmap, style, style_ex); |
| 2450 | |
| 2451 | SetWindowLongPtr(wd.hWnd, GWL_STYLE, style); |
| 2452 | SetWindowLongPtr(wd.hWnd, GWL_EXSTYLE, style_ex); |
| 2453 | |
| 2454 | if (icon_big && !icon_small) { |
| 2455 | SendMessage(wd.hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon_big); |
| 2456 | SendMessage(wd.hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon_big); |
| 2457 | } else { |
| 2458 | if (icon_big) { |
| 2459 | SendMessage(wd.hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon_big); |
| 2460 | } |
| 2461 | if (icon_small) { |
| 2462 | SendMessage(wd.hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon_small); |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | SetWindowPos(wd.hWnd, _is_always_on_top_recursive(p_window) ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | ((wd.no_focus || wd.is_popup) ? SWP_NOACTIVATE : 0)); |
| 2467 | |
| 2468 | if (p_repaint) { |
| 2469 | RECT rect; |
| 2470 | GetWindowRect(wd.hWnd, &rect); |
| 2471 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(window_get_current_screen(p_window)) : Vector2i(); |
| 2472 | MoveWindow(wd.hWnd, rect.left, rect.top, rect.right - rect.left + off.x, rect.bottom - rect.top - off.y, TRUE); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | void DisplayServerWindows::window_set_mode(WindowMode p_mode, WindowID p_window) { |
| 2477 | _THREAD_SAFE_METHOD_ |
| 2478 | |
| 2479 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2480 | WindowData &wd = windows[p_window]; |
| 2481 | |
| 2482 | if (p_mode != WINDOW_MODE_WINDOWED && wd.parent_hwnd) { |
| 2483 | print_line("Embedded window only supports Windowed mode."); |
| 2484 | return; |
| 2485 | } |
| 2486 | |
| 2487 | bool was_fullscreen = wd.fullscreen; |
| 2488 | wd.was_fullscreen_pre_min = false; |
| 2489 | |
| 2490 | if (p_mode == WINDOW_MODE_MAXIMIZED && wd.borderless) { |
| 2491 | int cs = window_get_current_screen(p_window); |
| 2492 | Rect2i full = Rect2i(screen_get_position(cs), screen_get_size(cs)); |
| 2493 | Rect2i usable = screen_get_usable_rect(cs); |
| 2494 | if (full == usable) { |
| 2495 | p_mode = WINDOW_MODE_FULLSCREEN; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | if (wd.fullscreen && p_mode != WINDOW_MODE_FULLSCREEN && p_mode != WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2500 | RECT rect; |
| 2501 | |
| 2502 | wd.fullscreen = false; |
| 2503 | wd.multiwindow_fs = false; |
| 2504 | |
| 2505 | // Restore previous maximized state. |
| 2506 | wd.maximized = wd.was_maximized_pre_fs; |
| 2507 | |
| 2508 | _update_window_style(p_window, false); |
| 2509 | |
| 2510 | // Restore window rect after exiting fullscreen. |
| 2511 | if (wd.pre_fs_valid) { |
| 2512 | rect = wd.pre_fs_rect; |
| 2513 | } else { |
| 2514 | rect.left = 0; |
| 2515 | rect.right = wd.width; |
| 2516 | rect.top = 0; |
| 2517 | rect.bottom = wd.height; |
| 2518 | } |
| 2519 | |
| 2520 | ShowWindow(wd.hWnd, SW_RESTORE); |
| 2521 | MoveWindow(wd.hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE); |
| 2522 | |
| 2523 | if (restore_mouse_trails > 1) { |
| 2524 | SystemParametersInfoA(SPI_SETMOUSETRAILS, restore_mouse_trails, nullptr, 0); |
| 2525 | restore_mouse_trails = 0; |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | if ((wd.maximized || wd.was_maximized_pre_fs) && wd.borderless && p_mode != WINDOW_MODE_MINIMIZED && p_mode != WINDOW_MODE_FULLSCREEN && p_mode != WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2530 | RECT rect; |
| 2531 | if (wd.pre_fs_valid) { |
| 2532 | rect = wd.pre_fs_rect; |
| 2533 | } else { |
| 2534 | rect.left = 0; |
| 2535 | rect.right = wd.width; |
| 2536 | rect.top = 0; |
| 2537 | rect.bottom = wd.height; |
| 2538 | } |
| 2539 | |
| 2540 | ShowWindow(wd.hWnd, SW_RESTORE); |
| 2541 | MoveWindow(wd.hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE); |
| 2542 | } |
| 2543 | |
| 2544 | if (p_mode == WINDOW_MODE_WINDOWED) { |
| 2545 | ShowWindow(wd.hWnd, SW_NORMAL); |
| 2546 | wd.maximized = false; |
| 2547 | wd.minimized = false; |
| 2548 | } |
| 2549 | |
| 2550 | if (p_mode == WINDOW_MODE_MAXIMIZED && !wd.borderless) { |
| 2551 | ShowWindow(wd.hWnd, SW_MAXIMIZE); |
| 2552 | wd.maximized = true; |
| 2553 | wd.minimized = false; |
| 2554 | } |
| 2555 | |
| 2556 | if (p_mode == WINDOW_MODE_MAXIMIZED && wd.borderless) { |
| 2557 | if (!was_fullscreen && !(wd.maximized && wd.borderless)) { |
| 2558 | // Save non-fullscreen rect before entering fullscreen. |
| 2559 | GetWindowRect(wd.hWnd, &wd.pre_fs_rect); |
| 2560 | wd.pre_fs_valid = true; |
| 2561 | } |
| 2562 | ShowWindow(wd.hWnd, SW_NORMAL); |
| 2563 | wd.maximized = true; |
| 2564 | wd.minimized = false; |
| 2565 | |
| 2566 | int cs = window_get_current_screen(p_window); |
| 2567 | Rect2i usable = screen_get_usable_rect(cs); |
| 2568 | Point2 pos = usable.position + _get_screens_origin(); |
| 2569 | Size2 size = usable.size; |
| 2570 | MoveWindow(wd.hWnd, pos.x, pos.y, size.width, size.height, TRUE); |
| 2571 | } |
| 2572 | |
| 2573 | if (p_mode == WINDOW_MODE_MINIMIZED) { |
| 2574 | ShowWindow(wd.hWnd, SW_MINIMIZE); |
| 2575 | wd.maximized = false; |
| 2576 | wd.minimized = true; |
| 2577 | wd.was_fullscreen_pre_min = was_fullscreen; |
| 2578 | } |
| 2579 | |
| 2580 | if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2581 | wd.multiwindow_fs = false; |
| 2582 | } else if (p_mode == WINDOW_MODE_FULLSCREEN) { |
| 2583 | wd.multiwindow_fs = true; |
| 2584 | } |
| 2585 | _update_window_style(p_window, false); |
| 2586 | |
| 2587 | if ((p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) && !wd.fullscreen) { |
| 2588 | if (wd.minimized || wd.maximized) { |
| 2589 | ShowWindow(wd.hWnd, SW_RESTORE); |
| 2590 | } |
| 2591 | |
| 2592 | // Save previous maximized stare. |
| 2593 | wd.was_maximized_pre_fs = wd.maximized; |
| 2594 | |
| 2595 | if (!was_fullscreen && !(wd.maximized && wd.borderless)) { |
| 2596 | // Save non-fullscreen rect before entering fullscreen. |
| 2597 | GetWindowRect(wd.hWnd, &wd.pre_fs_rect); |
| 2598 | wd.pre_fs_valid = true; |
| 2599 | } |
| 2600 | |
| 2601 | int cs = window_get_current_screen(p_window); |
| 2602 | Point2 pos = screen_get_position(cs) + _get_screens_origin(); |
| 2603 | Size2 size = screen_get_size(cs); |
| 2604 | |
| 2605 | wd.fullscreen = true; |
| 2606 | wd.maximized = false; |
| 2607 | wd.minimized = false; |
| 2608 | |
| 2609 | _update_window_style(p_window, false); |
| 2610 | |
| 2611 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(cs) : Vector2i(); |
| 2612 | MoveWindow(wd.hWnd, pos.x, pos.y, size.width + off.x, size.height + off.y, TRUE); |
| 2613 | |
| 2614 | // If the user has mouse trails enabled in windows, then sometimes the cursor disappears in fullscreen mode. |
| 2615 | // Save number of trails so we can restore when exiting, then turn off mouse trails |
| 2616 | SystemParametersInfoA(SPI_GETMOUSETRAILS, 0, &restore_mouse_trails, 0); |
| 2617 | if (restore_mouse_trails > 1) { |
| 2618 | SystemParametersInfoA(SPI_SETMOUSETRAILS, 0, nullptr, 0); |
| 2619 | } |
| 2620 | } |
| 2621 | _update_window_mouse_passthrough(p_window); |
| 2622 | } |
| 2623 | |
| 2624 | DisplayServer::WindowMode DisplayServerWindows::window_get_mode(WindowID p_window) const { |
| 2625 | _THREAD_SAFE_METHOD_ |
| 2626 | |
| 2627 | ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED); |
| 2628 | const WindowData &wd = windows[p_window]; |
| 2629 | |
| 2630 | if (wd.fullscreen) { |
| 2631 | if (wd.multiwindow_fs) { |
| 2632 | return WINDOW_MODE_FULLSCREEN; |
| 2633 | } else { |
| 2634 | return WINDOW_MODE_EXCLUSIVE_FULLSCREEN; |
| 2635 | } |
| 2636 | } else if (wd.minimized) { |
| 2637 | return WINDOW_MODE_MINIMIZED; |
| 2638 | } else if (wd.maximized) { |
| 2639 | return WINDOW_MODE_MAXIMIZED; |
| 2640 | } else { |
| 2641 | return WINDOW_MODE_WINDOWED; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | bool DisplayServerWindows::window_is_maximize_allowed(WindowID p_window) const { |
| 2646 | _THREAD_SAFE_METHOD_ |
| 2647 | |
| 2648 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2649 | const WindowData &wd = windows[p_window]; |
| 2650 | |
| 2651 | const DWORD style = GetWindowLongPtr(wd.hWnd, GWL_STYLE); |
| 2652 | return (style & WS_MAXIMIZEBOX) == WS_MAXIMIZEBOX; |
| 2653 | } |
| 2654 | |
| 2655 | void DisplayServerWindows::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) { |
| 2656 | _THREAD_SAFE_METHOD_ |
| 2657 | |
| 2658 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2659 | WindowData &wd = windows[p_window]; |
| 2660 | switch (p_flag) { |
| 2661 | case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 2662 | wd.no_min_btn = p_enabled; |
| 2663 | _update_window_style(p_window); |
| 2664 | } break; |
| 2665 | case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 2666 | wd.no_max_btn = p_enabled; |
| 2667 | _update_window_style(p_window); |
| 2668 | } break; |
| 2669 | case WINDOW_FLAG_RESIZE_DISABLED: { |
| 2670 | if (p_enabled && wd.parent_hwnd) { |
| 2671 | print_line("Embedded window resize can't be disabled."); |
| 2672 | return; |
| 2673 | } |
| 2674 | wd.resizable = !p_enabled; |
| 2675 | _update_window_style(p_window); |
| 2676 | } break; |
| 2677 | case WINDOW_FLAG_BORDERLESS: { |
| 2678 | wd.borderless = p_enabled; |
| 2679 | if (wd.fullscreen) { |
| 2680 | return; |
| 2681 | } |
| 2682 | _update_window_mouse_passthrough(p_window); |
| 2683 | _update_window_style(p_window); |
| 2684 | ShowWindow(wd.hWnd, (wd.no_focus || wd.is_popup) ? SW_SHOWNOACTIVATE : SW_SHOW); // Show the window. |
| 2685 | } break; |
| 2686 | case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 2687 | ERR_FAIL_COND_MSG(wd.transient_parent != INVALID_WINDOW_ID && p_enabled, "Transient windows can't become on top."); |
| 2688 | if (p_enabled && wd.parent_hwnd) { |
| 2689 | print_line("Embedded window can't become on top."); |
| 2690 | return; |
| 2691 | } |
| 2692 | wd.always_on_top = p_enabled; |
| 2693 | _update_window_style(p_window); |
| 2694 | } break; |
| 2695 | case WINDOW_FLAG_SHARP_CORNERS: { |
| 2696 | wd.sharp_corners = p_enabled; |
| 2697 | DWORD value = wd.sharp_corners ? DWMWCP_DONOTROUND : DWMWCP_DEFAULT; |
| 2698 | ::DwmSetWindowAttribute(wd.hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, &value, sizeof(value)); |
| 2699 | _update_window_style(p_window); |
| 2700 | } break; |
| 2701 | case WINDOW_FLAG_TRANSPARENT: { |
| 2702 | if (p_enabled) { |
| 2703 | // Enable per-pixel alpha. |
| 2704 | if (OS::get_singleton()->is_layered_allowed()) { |
| 2705 | DWM_BLURBEHIND bb; |
| 2706 | ZeroMemory(&bb, sizeof(bb)); |
| 2707 | HRGN hRgn = CreateRectRgn(0, 0, -1, -1); |
| 2708 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; |
| 2709 | bb.hRgnBlur = hRgn; |
| 2710 | bb.fEnable = TRUE; |
| 2711 | DwmEnableBlurBehindWindow(wd.hWnd, &bb); |
| 2712 | } |
| 2713 | wd.layered_window = true; |
| 2714 | } else { |
| 2715 | // Disable per-pixel alpha. |
| 2716 | wd.layered_window = false; |
| 2717 | if (OS::get_singleton()->is_layered_allowed()) { |
| 2718 | DWM_BLURBEHIND bb; |
| 2719 | ZeroMemory(&bb, sizeof(bb)); |
| 2720 | HRGN hRgn = CreateRectRgn(0, 0, -1, -1); |
| 2721 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; |
| 2722 | bb.hRgnBlur = hRgn; |
| 2723 | bb.fEnable = FALSE; |
| 2724 | DwmEnableBlurBehindWindow(wd.hWnd, &bb); |
| 2725 | } |
| 2726 | } |
| 2727 | } break; |
| 2728 | case WINDOW_FLAG_NO_FOCUS: { |
| 2729 | wd.no_focus = p_enabled; |
| 2730 | _update_window_style(p_window); |
| 2731 | } break; |
| 2732 | case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 2733 | wd.mpass = p_enabled; |
| 2734 | } break; |
| 2735 | case WINDOW_FLAG_EXCLUDE_FROM_CAPTURE: { |
| 2736 | wd.hide_from_capture = p_enabled; |
| 2737 | if (p_enabled) { |
| 2738 | if (os_ver.dwBuildNumber >= 19041) { |
| 2739 | SetWindowDisplayAffinity(wd.hWnd, WDA_EXCLUDEFROMCAPTURE); |
| 2740 | } else { |
| 2741 | SetWindowDisplayAffinity(wd.hWnd, WDA_MONITOR); |
| 2742 | } |
| 2743 | } else { |
| 2744 | SetWindowDisplayAffinity(wd.hWnd, WDA_NONE); |
| 2745 | } |
| 2746 | } break; |
| 2747 | case WINDOW_FLAG_POPUP: { |
| 2748 | ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup."); |
| 2749 | ERR_FAIL_COND_MSG(IsWindowVisible(wd.hWnd) && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened."); |
| 2750 | if (p_enabled && wd.parent_hwnd) { |
| 2751 | print_line("Embedded window can't be popup."); |
| 2752 | return; |
| 2753 | } |
| 2754 | wd.is_popup = p_enabled; |
| 2755 | } break; |
| 2756 | default: |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | bool DisplayServerWindows::window_get_flag(WindowFlags p_flag, WindowID p_window) const { |
| 2762 | _THREAD_SAFE_METHOD_ |
| 2763 | |
| 2764 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2765 | const WindowData &wd = windows[p_window]; |
| 2766 | switch (p_flag) { |
| 2767 | case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 2768 | return wd.no_max_btn; |
| 2769 | } break; |
| 2770 | case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 2771 | return wd.no_min_btn; |
| 2772 | } break; |
| 2773 | case WINDOW_FLAG_RESIZE_DISABLED: { |
| 2774 | return !wd.resizable; |
| 2775 | } break; |
| 2776 | case WINDOW_FLAG_BORDERLESS: { |
| 2777 | return wd.borderless; |
| 2778 | } break; |
| 2779 | case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 2780 | return wd.always_on_top; |
| 2781 | } break; |
| 2782 | case WINDOW_FLAG_SHARP_CORNERS: { |
| 2783 | return wd.sharp_corners; |
| 2784 | } break; |
| 2785 | case WINDOW_FLAG_TRANSPARENT: { |
| 2786 | return wd.layered_window; |
| 2787 | } break; |
| 2788 | case WINDOW_FLAG_NO_FOCUS: { |
| 2789 | return wd.no_focus; |
| 2790 | } break; |
| 2791 | case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 2792 | return wd.mpass; |
| 2793 | } break; |
| 2794 | case WINDOW_FLAG_EXCLUDE_FROM_CAPTURE: { |
| 2795 | return wd.hide_from_capture; |
| 2796 | } break; |
| 2797 | case WINDOW_FLAG_POPUP: { |
| 2798 | return wd.is_popup; |
| 2799 | } break; |
| 2800 | default: |
| 2801 | break; |
| 2802 | } |
| 2803 | |
| 2804 | return false; |
| 2805 | } |
| 2806 | |
| 2807 | void DisplayServerWindows::window_request_attention(WindowID p_window) { |
| 2808 | _THREAD_SAFE_METHOD_ |
| 2809 | |
| 2810 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2811 | const WindowData &wd = windows[p_window]; |
| 2812 | |
| 2813 | FLASHWINFO info; |
| 2814 | info.cbSize = sizeof(FLASHWINFO); |
| 2815 | info.hwnd = wd.hWnd; |
| 2816 | info.dwFlags = FLASHW_ALL; |
| 2817 | info.dwTimeout = 0; |
| 2818 | info.uCount = 2; |
| 2819 | FlashWindowEx(&info); |
| 2820 | } |
| 2821 | |
| 2822 | void DisplayServerWindows::window_move_to_foreground(WindowID p_window) { |
| 2823 | _THREAD_SAFE_METHOD_ |
| 2824 | |
| 2825 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2826 | WindowData &wd = windows[p_window]; |
| 2827 | |
| 2828 | if (!wd.no_focus && !wd.is_popup) { |
| 2829 | SetForegroundWindow(wd.hWnd); |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | bool DisplayServerWindows::window_is_focused(WindowID p_window) const { |
| 2834 | _THREAD_SAFE_METHOD_ |
| 2835 | |
| 2836 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2837 | const WindowData &wd = windows[p_window]; |
| 2838 | |
| 2839 | return wd.window_focused; |
| 2840 | } |
| 2841 | |
| 2842 | DisplayServerWindows::WindowID DisplayServerWindows::get_focused_window() const { |
| 2843 | return last_focused_window; |
| 2844 | } |
| 2845 | |
| 2846 | bool DisplayServerWindows::window_can_draw(WindowID p_window) const { |
| 2847 | _THREAD_SAFE_METHOD_ |
| 2848 | |
| 2849 | ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2850 | const WindowData &wd = windows[p_window]; |
| 2851 | return !wd.minimized; |
| 2852 | } |
| 2853 | |
| 2854 | bool DisplayServerWindows::can_any_window_draw() const { |
| 2855 | _THREAD_SAFE_METHOD_ |
| 2856 | |
| 2857 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 2858 | if (!E.value.minimized) { |
| 2859 | return true; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | return false; |
| 2864 | } |
| 2865 | |
| 2866 | int DisplayServerWindows::accessibility_should_increase_contrast() const { |
| 2867 | HIGHCONTRASTA hc; |
| 2868 | hc.cbSize = sizeof(HIGHCONTRAST); |
| 2869 | if (!SystemParametersInfoA(SPI_GETHIGHCONTRAST, sizeof(HIGHCONTRAST), &hc, 0)) { |
| 2870 | return -1; |
| 2871 | } |
| 2872 | return (hc.dwFlags & HCF_HIGHCONTRASTON); |
| 2873 | } |
| 2874 | |
| 2875 | int DisplayServerWindows::accessibility_should_reduce_animation() const { |
| 2876 | BOOL anim_enabled = false; // Note: this should be BOOL (WinAPI), not bool (C++), since SystemParametersInfoA expect variable with specific size. |
| 2877 | if (!SystemParametersInfoA(SPI_GETCLIENTAREAANIMATION, 0, &anim_enabled, 0)) { |
| 2878 | return -1; |
| 2879 | } |
| 2880 | return (!anim_enabled); |
| 2881 | } |
| 2882 | |
| 2883 | int DisplayServerWindows::accessibility_should_reduce_transparency() const { |
| 2884 | BOOL tr_enabled = false; // Note: this should be BOOL (WinAPI), not bool (C++), since SystemParametersInfoA expect variable with specific size. |
| 2885 | if (!SystemParametersInfoA(SPI_GETDISABLEOVERLAPPEDCONTENT, 0, &tr_enabled, 0)) { |
| 2886 | return -1; |
| 2887 | } |
| 2888 | return tr_enabled; |
| 2889 | } |
| 2890 | |
| 2891 | int DisplayServerWindows::accessibility_screen_reader_active() const { |
| 2892 | BOOL sr_enabled = false; // Note: this should be BOOL (WinAPI), not bool (C++), since SystemParametersInfoA expect variable with specific size. |
| 2893 | if (SystemParametersInfoA(SPI_GETSCREENREADER, 0, &sr_enabled, 0) && sr_enabled) { |
| 2894 | return true; |
| 2895 | } |
| 2896 | |
| 2897 | static const WCHAR *narrator_mutex_name = L"NarratorRunning"; |
| 2898 | HANDLE narrator_mutex = OpenMutexW(MUTEX_ALL_ACCESS, false, narrator_mutex_name); |
| 2899 | if (narrator_mutex) { |
| 2900 | CloseHandle(narrator_mutex); |
| 2901 | return true; |
| 2902 | } |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | Vector2i DisplayServerWindows::ime_get_selection() const { |
| 2907 | _THREAD_SAFE_METHOD_ |
| 2908 | |
| 2909 | DisplayServer::WindowID window_id = _get_focused_window_or_popup(); |
| 2910 | const WindowData &wd = windows[window_id]; |
| 2911 | if (!wd.ime_active) { |
| 2912 | return Vector2i(); |
| 2913 | } |
| 2914 | |
| 2915 | int cursor = ImmGetCompositionStringW(wd.im_himc, GCS_CURSORPOS, nullptr, 0); |
| 2916 | |
| 2917 | int32_t length = ImmGetCompositionStringW(wd.im_himc, GCS_COMPSTR, nullptr, 0); |
| 2918 | wchar_t *string = reinterpret_cast<wchar_t *>(memalloc(length)); |
| 2919 | ImmGetCompositionStringW(wd.im_himc, GCS_COMPSTR, string, length); |
| 2920 | |
| 2921 | int32_t utf32_cursor = 0; |
| 2922 | for (int32_t i = 0; i < length / int32_t(sizeof(wchar_t)); i++) { |
| 2923 | if ((string[i] & 0xfffffc00) == 0xd800) { |
| 2924 | i++; |
| 2925 | } |
| 2926 | if (i < cursor) { |
| 2927 | utf32_cursor++; |
| 2928 | } else { |
| 2929 | break; |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | memdelete(string); |
| 2934 | |
| 2935 | return Vector2i(utf32_cursor, 0); |
| 2936 | } |
| 2937 | |
| 2938 | String DisplayServerWindows::ime_get_text() const { |
| 2939 | _THREAD_SAFE_METHOD_ |
| 2940 | |
| 2941 | DisplayServer::WindowID window_id = _get_focused_window_or_popup(); |
| 2942 | const WindowData &wd = windows[window_id]; |
| 2943 | if (!wd.ime_active) { |
| 2944 | return String(); |
| 2945 | } |
| 2946 | |
| 2947 | String ret; |
| 2948 | int32_t length = ImmGetCompositionStringW(wd.im_himc, GCS_COMPSTR, nullptr, 0); |
| 2949 | wchar_t *string = reinterpret_cast<wchar_t *>(memalloc(length)); |
| 2950 | ImmGetCompositionStringW(wd.im_himc, GCS_COMPSTR, string, length); |
| 2951 | ret.append_utf16((char16_t *)string, length / sizeof(wchar_t)); |
| 2952 | |
| 2953 | memdelete(string); |
| 2954 | |
| 2955 | return ret; |
| 2956 | } |
| 2957 | |
| 2958 | void DisplayServerWindows::window_set_ime_active(const bool p_active, WindowID p_window) { |
| 2959 | _THREAD_SAFE_METHOD_ |
| 2960 | |
| 2961 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2962 | WindowData &wd = windows[p_window]; |
| 2963 | |
| 2964 | if (p_active) { |
| 2965 | wd.ime_active = true; |
| 2966 | ImmAssociateContext(wd.hWnd, wd.im_himc); |
| 2967 | CreateCaret(wd.hWnd, nullptr, 1, 1); |
| 2968 | window_set_ime_position(wd.im_position, p_window); |
| 2969 | } else { |
| 2970 | ImmAssociateContext(wd.hWnd, (HIMC) nullptr); |
| 2971 | DestroyCaret(); |
| 2972 | wd.ime_active = false; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | void DisplayServerWindows::window_set_ime_position(const Point2i &p_pos, WindowID p_window) { |
| 2977 | _THREAD_SAFE_METHOD_ |
| 2978 | |
| 2979 | ERR_FAIL_COND(!windows.has(p_window)); |
| 2980 | WindowData &wd = windows[p_window]; |
| 2981 | |
| 2982 | wd.im_position = p_pos; |
| 2983 | |
| 2984 | HIMC himc = ImmGetContext(wd.hWnd); |
| 2985 | if (himc == (HIMC) nullptr) { |
| 2986 | return; |
| 2987 | } |
| 2988 | |
| 2989 | COMPOSITIONFORM cps; |
| 2990 | cps.dwStyle = CFS_POINT; |
| 2991 | cps.ptCurrentPos.x = wd.im_position.x; |
| 2992 | cps.ptCurrentPos.y = wd.im_position.y; |
| 2993 | ImmSetCompositionWindow(himc, &cps); |
| 2994 | ImmReleaseContext(wd.hWnd, himc); |
| 2995 | } |
| 2996 | |
| 2997 | void DisplayServerWindows::cursor_set_shape(CursorShape p_shape) { |
| 2998 | _THREAD_SAFE_METHOD_ |
| 2999 | |
| 3000 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3001 | |
| 3002 | if (cursor_shape == p_shape) { |
| 3003 | return; |
| 3004 | } |
| 3005 | |
| 3006 | if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) { |
| 3007 | cursor_shape = p_shape; |
| 3008 | return; |
| 3009 | } |
| 3010 | |
| 3011 | static const LPCTSTR win_cursors[CURSOR_MAX] = { |
| 3012 | IDC_ARROW, |
| 3013 | IDC_IBEAM, |
| 3014 | IDC_HAND, // Finger. |
| 3015 | IDC_CROSS, |
| 3016 | IDC_WAIT, |
| 3017 | IDC_APPSTARTING, |
| 3018 | IDC_SIZEALL, |
| 3019 | IDC_ARROW, |
| 3020 | IDC_NO, |
| 3021 | IDC_SIZENS, |
| 3022 | IDC_SIZEWE, |
| 3023 | IDC_SIZENESW, |
| 3024 | IDC_SIZENWSE, |
| 3025 | IDC_SIZEALL, |
| 3026 | IDC_SIZENS, |
| 3027 | IDC_SIZEWE, |
| 3028 | IDC_HELP |
| 3029 | }; |
| 3030 | |
| 3031 | if (cursors_cache.has(p_shape)) { |
| 3032 | SetCursor(cursors[p_shape]); |
| 3033 | } else { |
| 3034 | SetCursor(LoadCursor(hInstance, win_cursors[p_shape])); |
| 3035 | } |
| 3036 | |
| 3037 | cursor_shape = p_shape; |
| 3038 | } |
| 3039 | |
| 3040 | DisplayServer::CursorShape DisplayServerWindows::cursor_get_shape() const { |
| 3041 | return cursor_shape; |
| 3042 | } |
| 3043 | |
| 3044 | void DisplayServerWindows::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { |
| 3045 | _THREAD_SAFE_METHOD_ |
| 3046 | |
| 3047 | ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3048 | |
| 3049 | if (p_cursor.is_valid()) { |
| 3050 | RBMap<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape); |
| 3051 | |
| 3052 | if (cursor_c) { |
| 3053 | if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) { |
| 3054 | cursor_set_shape(p_shape); |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | cursors_cache.erase(p_shape); |
| 3059 | } |
| 3060 | |
| 3061 | Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot); |
| 3062 | ERR_FAIL_COND(image.is_null()); |
| 3063 | Vector2i texture_size = image->get_size(); |
| 3064 | |
| 3065 | UINT image_size = texture_size.width * texture_size.height; |
| 3066 | |
| 3067 | // Create the BITMAP with alpha channel. |
| 3068 | COLORREF *buffer = nullptr; |
| 3069 | |
| 3070 | BITMAPV5HEADER bi; |
| 3071 | ZeroMemory(&bi, sizeof(bi)); |
| 3072 | bi.bV5Size = sizeof(bi); |
| 3073 | bi.bV5Width = texture_size.width; |
| 3074 | bi.bV5Height = -texture_size.height; |
| 3075 | bi.bV5Planes = 1; |
| 3076 | bi.bV5BitCount = 32; |
| 3077 | bi.bV5Compression = BI_BITFIELDS; |
| 3078 | bi.bV5RedMask = 0x00ff0000; |
| 3079 | bi.bV5GreenMask = 0x0000ff00; |
| 3080 | bi.bV5BlueMask = 0x000000ff; |
| 3081 | bi.bV5AlphaMask = 0xff000000; |
| 3082 | |
| 3083 | HDC dc = GetDC(nullptr); |
| 3084 | HBITMAP bitmap = CreateDIBSection(dc, reinterpret_cast<BITMAPINFO *>(&bi), DIB_RGB_COLORS, reinterpret_cast<void **>(&buffer), nullptr, 0); |
| 3085 | HBITMAP mask = CreateBitmap(texture_size.width, texture_size.height, 1, 1, nullptr); |
| 3086 | |
| 3087 | bool fully_transparent = true; |
| 3088 | for (UINT index = 0; index < image_size; index++) { |
| 3089 | int row_index = std::floor(index / texture_size.width); |
| 3090 | int column_index = index % int(texture_size.width); |
| 3091 | |
| 3092 | const Color &c = image->get_pixel(column_index, row_index); |
| 3093 | fully_transparent = fully_transparent && (c.a == 0.f); |
| 3094 | |
| 3095 | *(buffer + index) = c.to_argb32(); |
| 3096 | } |
| 3097 | |
| 3098 | // Finally, create the icon. |
| 3099 | if (cursors[p_shape]) { |
| 3100 | DestroyIcon(cursors[p_shape]); |
| 3101 | } |
| 3102 | |
| 3103 | if (fully_transparent) { |
| 3104 | cursors[p_shape] = nullptr; |
| 3105 | } else { |
| 3106 | ICONINFO iconinfo; |
| 3107 | iconinfo.fIcon = FALSE; |
| 3108 | iconinfo.xHotspot = p_hotspot.x; |
| 3109 | iconinfo.yHotspot = p_hotspot.y; |
| 3110 | iconinfo.hbmMask = mask; |
| 3111 | iconinfo.hbmColor = bitmap; |
| 3112 | cursors[p_shape] = CreateIconIndirect(&iconinfo); |
| 3113 | } |
| 3114 | |
| 3115 | Vector<Variant> params; |
| 3116 | params.push_back(p_cursor); |
| 3117 | params.push_back(p_hotspot); |
| 3118 | cursors_cache.insert(p_shape, params); |
| 3119 | |
| 3120 | if (p_shape == cursor_shape) { |
| 3121 | if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) { |
| 3122 | SetCursor(cursors[p_shape]); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | DeleteObject(mask); |
| 3127 | DeleteObject(bitmap); |
| 3128 | ReleaseDC(nullptr, dc); |
| 3129 | } else { |
| 3130 | // Reset to default system cursor. |
| 3131 | if (cursors[p_shape]) { |
| 3132 | DestroyIcon(cursors[p_shape]); |
| 3133 | } |
| 3134 | cursors[p_shape] = nullptr; |
| 3135 | |
| 3136 | cursors_cache.erase(p_shape); |
| 3137 | |
| 3138 | CursorShape c = cursor_shape; |
| 3139 | cursor_shape = CURSOR_MAX; |
| 3140 | cursor_set_shape(c); |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | bool DisplayServerWindows::get_swap_cancel_ok() { |
| 3145 | return true; |
| 3146 | } |
| 3147 | |
| 3148 | void DisplayServerWindows::enable_for_stealing_focus(OS::ProcessID pid) { |
| 3149 | _THREAD_SAFE_METHOD_ |
| 3150 | |
| 3151 | AllowSetForegroundWindow(pid); |
| 3152 | } |
| 3153 | |
| 3154 | struct WindowEnumData { |
| 3155 | DWORD process_id; |
| 3156 | HWND parent_hWnd; |
| 3157 | HWND hWnd; |
| 3158 | }; |
| 3159 | |
| 3160 | static BOOL CALLBACK _enum_proc_find_window_from_process_id_callback(HWND hWnd, LPARAM lParam) { |
| 3161 | WindowEnumData &ed = *(WindowEnumData *)lParam; |
| 3162 | DWORD process_id = 0x0; |
| 3163 | |
| 3164 | GetWindowThreadProcessId(hWnd, &process_id); |
| 3165 | if (ed.process_id == process_id) { |
| 3166 | if (GetParent(hWnd) != ed.parent_hWnd) { |
| 3167 | return TRUE; |
| 3168 | } |
| 3169 | |
| 3170 | // Found it. |
| 3171 | ed.hWnd = hWnd; |
| 3172 | SetLastError(ERROR_SUCCESS); |
| 3173 | return FALSE; |
| 3174 | } |
| 3175 | // Continue enumeration. |
| 3176 | return TRUE; |
| 3177 | } |
| 3178 | |
| 3179 | HWND DisplayServerWindows::_find_window_from_process_id(OS::ProcessID p_pid, HWND p_current_hwnd) { |
| 3180 | DWORD pid = p_pid; |
| 3181 | WindowEnumData ed = { pid, p_current_hwnd, NULL }; |
| 3182 | |
| 3183 | // First, check our own child, maybe it's already embedded. |
| 3184 | if (!EnumChildWindows(p_current_hwnd, _enum_proc_find_window_from_process_id_callback, (LPARAM)&ed) && (GetLastError() == ERROR_SUCCESS)) { |
| 3185 | if (ed.hWnd) { |
| 3186 | return ed.hWnd; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | // Then check all the opened windows on the computer. |
| 3191 | if (!EnumWindows(_enum_proc_find_window_from_process_id_callback, (LPARAM)&ed) && (GetLastError() == ERROR_SUCCESS)) { |
| 3192 | return ed.hWnd; |
| 3193 | } |
| 3194 | |
| 3195 | return NULL; |
| 3196 | } |
| 3197 | |
| 3198 | Error DisplayServerWindows::embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) { |
| 3199 | _THREAD_SAFE_METHOD_ |
| 3200 | |
| 3201 | ERR_FAIL_COND_V(!windows.has(p_window), FAILED); |
| 3202 | |
| 3203 | const WindowData &wd = windows[p_window]; |
| 3204 | |
| 3205 | EmbeddedProcessData *ep = nullptr; |
| 3206 | if (embedded_processes.has(p_pid)) { |
| 3207 | ep = embedded_processes.get(p_pid); |
| 3208 | } else { |
| 3209 | // New process, trying to find the window. |
| 3210 | HWND handle_to_embed = _find_window_from_process_id(p_pid, wd.hWnd); |
| 3211 | if (!handle_to_embed) { |
| 3212 | return ERR_DOES_NOT_EXIST; |
| 3213 | } |
| 3214 | |
| 3215 | const DWORD style = GetWindowLongPtr(handle_to_embed, GWL_STYLE); |
| 3216 | |
| 3217 | ep = memnew(EmbeddedProcessData); |
| 3218 | ep->window_handle = handle_to_embed; |
| 3219 | ep->parent_window_handle = wd.hWnd; |
| 3220 | ep->is_visible = (style & WS_VISIBLE) == WS_VISIBLE; |
| 3221 | |
| 3222 | embedded_processes.insert(p_pid, ep); |
| 3223 | } |
| 3224 | |
| 3225 | if (p_rect.size.x <= 100 || p_rect.size.y <= 100) { |
| 3226 | p_visible = false; |
| 3227 | } |
| 3228 | |
| 3229 | // In Godot, the window position is offset by the screen's origin coordinates. |
| 3230 | // We need to adjust for this when a screen is positioned in the negative space |
| 3231 | // (e.g., a screen to the left of the main screen). |
| 3232 | const Rect2i adjusted_rect = Rect2i(p_rect.position + _get_screens_origin(), p_rect.size); |
| 3233 | |
| 3234 | // Use HWND_BOTTOM to prevent reordering of the embedded window over another popup. |
| 3235 | SetWindowPos(ep->window_handle, HWND_BOTTOM, adjusted_rect.position.x, adjusted_rect.position.y, adjusted_rect.size.x, adjusted_rect.size.y, SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS); |
| 3236 | |
| 3237 | if (ep->is_visible != p_visible) { |
| 3238 | if (p_visible) { |
| 3239 | ShowWindow(ep->window_handle, SW_SHOWNA); |
| 3240 | } else { |
| 3241 | ShowWindow(ep->window_handle, SW_HIDE); |
| 3242 | } |
| 3243 | ep->is_visible = p_visible; |
| 3244 | } |
| 3245 | |
| 3246 | if (p_grab_focus) { |
| 3247 | SetForegroundWindow(ep->window_handle); |
| 3248 | SetFocus(ep->window_handle); |
| 3249 | } |
| 3250 | |
| 3251 | return OK; |
| 3252 | } |
| 3253 | |
| 3254 | Error DisplayServerWindows::request_close_embedded_process(OS::ProcessID p_pid) { |
| 3255 | _THREAD_SAFE_METHOD_ |
| 3256 | |
| 3257 | if (!embedded_processes.has(p_pid)) { |
| 3258 | return ERR_DOES_NOT_EXIST; |
| 3259 | } |
| 3260 | |
| 3261 | EmbeddedProcessData *ep = embedded_processes.get(p_pid); |
| 3262 | |
| 3263 | // Send a close message to gracefully close the process. |
| 3264 | PostMessage(ep->window_handle, WM_CLOSE, 0, 0); |
| 3265 | |
| 3266 | return OK; |
| 3267 | } |
| 3268 | |
| 3269 | Error DisplayServerWindows::remove_embedded_process(OS::ProcessID p_pid) { |
| 3270 | _THREAD_SAFE_METHOD_ |
| 3271 | |
| 3272 | if (!embedded_processes.has(p_pid)) { |
| 3273 | return ERR_DOES_NOT_EXIST; |
| 3274 | } |
| 3275 | |
| 3276 | EmbeddedProcessData *ep = embedded_processes.get(p_pid); |
| 3277 | |
| 3278 | request_close_embedded_process(p_pid); |
| 3279 | |
| 3280 | // This is a workaround to ensure the parent window correctly regains focus after the |
| 3281 | // embedded window is closed. When the embedded window is closed while it has focus, |
| 3282 | // the parent window (the editor) does not become active. It appears focused but is not truly activated. |
| 3283 | // Opening a new window and closing it forces Windows to set the focus and activation correctly. |
| 3284 | DWORD style = WS_POPUP | WS_VISIBLE; |
| 3285 | DWORD style_ex = WS_EX_TOPMOST; |
| 3286 | |
| 3287 | WNDCLASSW wcTemp = {}; |
| 3288 | wcTemp.lpfnWndProc = DefWindowProcW; |
| 3289 | wcTemp.hInstance = GetModuleHandle(nullptr); |
| 3290 | wcTemp.lpszClassName = L"Engine temp window"; |
| 3291 | RegisterClassW(&wcTemp); |
| 3292 | |
| 3293 | HWND hWnd = CreateWindowExW( |
| 3294 | style_ex, |
| 3295 | L"Engine temp window", L"", |
| 3296 | style, |
| 3297 | 0, |
| 3298 | 0, |
| 3299 | 1, |
| 3300 | 1, |
| 3301 | ep->parent_window_handle, |
| 3302 | nullptr, |
| 3303 | GetModuleHandle(nullptr), |
| 3304 | nullptr); |
| 3305 | |
| 3306 | SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE); |
| 3307 | |
| 3308 | DestroyWindow(hWnd); |
| 3309 | UnregisterClassW(L"Engine temp window", GetModuleHandle(nullptr)); |
| 3310 | |
| 3311 | SetForegroundWindow(ep->parent_window_handle); |
| 3312 | |
| 3313 | embedded_processes.erase(p_pid); |
| 3314 | memdelete(ep); |
| 3315 | |
| 3316 | return OK; |
| 3317 | } |
| 3318 | |
| 3319 | OS::ProcessID DisplayServerWindows::get_focused_process_id() { |
| 3320 | HWND hwnd = GetForegroundWindow(); |
| 3321 | if (!hwnd) { |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
| 3325 | // Get the process ID of the window. |
| 3326 | DWORD processID; |
| 3327 | GetWindowThreadProcessId(hwnd, &processID); |
| 3328 | |
| 3329 | return processID; |
| 3330 | } |
| 3331 | |
| 3332 | static HRESULT CALLBACK win32_task_dialog_callback(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LONG_PTR lpRefData) { |
| 3333 | if (msg == TDN_CREATED) { |
| 3334 | // To match the input text dialog. |
| 3335 | SendMessageW(hwnd, WM_SETICON, ICON_BIG, 0); |
| 3336 | SendMessageW(hwnd, WM_SETICON, ICON_SMALL, 0); |
| 3337 | } |
| 3338 | |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
| 3342 | Error DisplayServerWindows::dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) { |
| 3343 | _THREAD_SAFE_METHOD_ |
| 3344 | |
| 3345 | TASKDIALOGCONFIG config; |
| 3346 | ZeroMemory(&config, sizeof(TASKDIALOGCONFIG)); |
| 3347 | config.cbSize = sizeof(TASKDIALOGCONFIG); |
| 3348 | |
| 3349 | Char16String title = p_title.utf16(); |
| 3350 | Char16String message = p_description.utf16(); |
| 3351 | LocalVector<Char16String> buttons; |
| 3352 | for (String s : p_buttons) { |
| 3353 | buttons.push_back(s.utf16()); |
| 3354 | } |
| 3355 | |
| 3356 | WindowID window_id = _get_focused_window_or_popup(); |
| 3357 | if (!windows.has(window_id)) { |
| 3358 | window_id = MAIN_WINDOW_ID; |
| 3359 | } |
| 3360 | |
| 3361 | config.pszWindowTitle = (LPCWSTR)(title.get_data()); |
| 3362 | config.pszContent = (LPCWSTR)(message.get_data()); |
| 3363 | config.hwndParent = windows[window_id].hWnd; |
| 3364 | |
| 3365 | const int button_count = buttons.size(); |
| 3366 | config.cButtons = button_count; |
| 3367 | |
| 3368 | // No dynamic stack array size :( |
| 3369 | TASKDIALOG_BUTTON *tbuttons = button_count != 0 ? (TASKDIALOG_BUTTON *)alloca(sizeof(TASKDIALOG_BUTTON) * button_count) : nullptr; |
| 3370 | if (tbuttons) { |
| 3371 | for (int i = 0; i < button_count; i++) { |
| 3372 | tbuttons[i].nButtonID = i + 100; |
| 3373 | tbuttons[i].pszButtonText = (LPCWSTR)(buttons[i].get_data()); |
| 3374 | } |
| 3375 | } |
| 3376 | config.pButtons = tbuttons; |
| 3377 | config.pfCallback = win32_task_dialog_callback; |
| 3378 | |
| 3379 | Error result = FAILED; |
| 3380 | HMODULE comctl = LoadLibraryW(L"comctl32.dll"); |
| 3381 | if (comctl) { |
| 3382 | typedef HRESULT(WINAPI * TaskDialogIndirectPtr)(const TASKDIALOGCONFIG *pTaskConfig, int *pnButton, int *pnRadioButton, BOOL *pfVerificationFlagChecked); |
| 3383 | |
| 3384 | TaskDialogIndirectPtr task_dialog_indirect = (TaskDialogIndirectPtr)(void *)GetProcAddress(comctl, "TaskDialogIndirect"); |
| 3385 | int button_pressed; |
| 3386 | |
| 3387 | if (task_dialog_indirect && SUCCEEDED(task_dialog_indirect(&config, &button_pressed, nullptr, nullptr))) { |
| 3388 | if (p_callback.is_valid()) { |
| 3389 | Variant button = button_pressed - 100; |
| 3390 | const Variant *args[1] = { &button }; |
| 3391 | Variant ret; |
| 3392 | Callable::CallError ce; |
| 3393 | p_callback.callp(args, 1, ret, ce); |
| 3394 | if (ce.error != Callable::CallError::CALL_OK) { |
| 3395 | ERR_PRINT(vformat("Failed to execute dialog callback: %s.", Variant::get_callable_error_text(p_callback, args, 1, ce))); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | result = OK; |
| 3400 | } |
| 3401 | FreeLibrary(comctl); |
| 3402 | } else { |
| 3403 | ERR_PRINT("Unable to create native dialog."); |
| 3404 | } |
| 3405 | |
| 3406 | return result; |
| 3407 | } |
| 3408 | |
| 3409 | struct Win32InputTextDialogInit { |
| 3410 | const char16_t *title; |
| 3411 | const char16_t *description; |
| 3412 | const char16_t *partial; |
| 3413 | const Callable &callback; |
| 3414 | }; |
| 3415 | |
| 3416 | static int scale_with_dpi(int p_pos, int p_dpi) { |
| 3417 | return IsProcessDPIAware() ? (p_pos * p_dpi / 96) : p_pos; |
| 3418 | } |
| 3419 | |
| 3420 | static INT_PTR input_text_dialog_init(HWND hWnd, UINT code, WPARAM wParam, LPARAM lParam) { |
| 3421 | Win32InputTextDialogInit init = *(Win32InputTextDialogInit *)lParam; |
| 3422 | SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)&init.callback); // Set dialog callback. |
| 3423 | |
| 3424 | SetWindowTextW(hWnd, (LPCWSTR)init.title); |
| 3425 | |
| 3426 | const int dpi = DisplayServerWindows::get_singleton()->screen_get_dpi(); |
| 3427 | |
| 3428 | const int margin = scale_with_dpi(7, dpi); |
| 3429 | const SIZE dlg_size = { scale_with_dpi(300, dpi), scale_with_dpi(50, dpi) }; |
| 3430 | |
| 3431 | int str_len = lstrlenW((LPCWSTR)init.description); |
| 3432 | SIZE str_size = { dlg_size.cx, 0 }; |
| 3433 | if (str_len > 0) { |
| 3434 | HDC hdc = GetDC(nullptr); |
| 3435 | RECT trect = { margin, margin, margin + dlg_size.cx, margin + dlg_size.cy }; |
| 3436 | SelectObject(hdc, (HFONT)SendMessageW(hWnd, WM_GETFONT, 0, 0)); |
| 3437 | |
| 3438 | // `+ margin` adds some space between the static text and the edit field. |
| 3439 | // Don't scale this with DPI because DPI is already handled by DrawText. |
| 3440 | str_size.cy = DrawTextW(hdc, (LPCWSTR)init.description, str_len, &trect, DT_LEFT | DT_WORDBREAK | DT_CALCRECT) + margin; |
| 3441 | |
| 3442 | ReleaseDC(nullptr, hdc); |
| 3443 | } |
| 3444 | |
| 3445 | RECT crect, wrect; |
| 3446 | GetClientRect(hWnd, &crect); |
| 3447 | GetWindowRect(hWnd, &wrect); |
| 3448 | int sw = GetSystemMetrics(SM_CXSCREEN); |
| 3449 | int sh = GetSystemMetrics(SM_CYSCREEN); |
| 3450 | int new_width = dlg_size.cx + margin * 2 + wrect.right - wrect.left - crect.right; |
| 3451 | int new_height = dlg_size.cy + margin * 2 + wrect.bottom - wrect.top - crect.bottom + str_size.cy; |
| 3452 | |
| 3453 | MoveWindow(hWnd, (sw - new_width) / 2, (sh - new_height) / 2, new_width, new_height, true); |
| 3454 | |
| 3455 | HWND ok_button = GetDlgItem(hWnd, 1); |
| 3456 | MoveWindow(ok_button, |
| 3457 | dlg_size.cx + margin - scale_with_dpi(65, dpi), |
| 3458 | dlg_size.cy + str_size.cy + margin - scale_with_dpi(20, dpi), |
| 3459 | scale_with_dpi(65, dpi), scale_with_dpi(20, dpi), true); |
| 3460 | |
| 3461 | HWND description = GetDlgItem(hWnd, 3); |
| 3462 | MoveWindow(description, margin, margin, dlg_size.cx, str_size.cy, true); |
| 3463 | SetWindowTextW(description, (LPCWSTR)init.description); |
| 3464 | |
| 3465 | HWND text_edit = GetDlgItem(hWnd, 2); |
| 3466 | MoveWindow(text_edit, margin, str_size.cy + margin, dlg_size.cx, scale_with_dpi(20, dpi), true); |
| 3467 | SetWindowTextW(text_edit, (LPCWSTR)init.partial); |
| 3468 | |
| 3469 | return TRUE; |
| 3470 | } |
| 3471 | |
| 3472 | static INT_PTR input_text_dialog_cmd_proc(HWND hWnd, UINT code, WPARAM wParam, LPARAM lParam) { |
| 3473 | if (LOWORD(wParam) == 1) { |
| 3474 | HWND text_edit = GetDlgItem(hWnd, 2); |
| 3475 | ERR_FAIL_NULL_V(text_edit, false); |
| 3476 | |
| 3477 | Char16String text; |
| 3478 | text.resize_uninitialized(GetWindowTextLengthW(text_edit) + 1); |
| 3479 | GetWindowTextW(text_edit, (LPWSTR)text.get_data(), text.size()); |
| 3480 | |
| 3481 | const Callable *callback = (const Callable *)GetWindowLongPtrW(hWnd, GWLP_USERDATA); |
| 3482 | if (callback && callback->is_valid()) { |
| 3483 | Variant v_result = String((const wchar_t *)text.get_data()); |
| 3484 | Variant ret; |
| 3485 | Callable::CallError ce; |
| 3486 | const Variant *args[1] = { &v_result }; |
| 3487 | |
| 3488 | callback->callp(args, 1, ret, ce); |
| 3489 | if (ce.error != Callable::CallError::CALL_OK) { |
| 3490 | ERR_PRINT(vformat("Failed to execute input dialog callback: %s.", Variant::get_callable_error_text(*callback, args, 1, ce))); |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | return EndDialog(hWnd, 0); |
| 3495 | } |
| 3496 | |
| 3497 | return false; |
| 3498 | } |
| 3499 | |
| 3500 | static INT_PTR CALLBACK input_text_dialog_proc(HWND hWnd, UINT code, WPARAM wParam, LPARAM lParam) { |
| 3501 | switch (code) { |
| 3502 | case WM_INITDIALOG: |
| 3503 | return input_text_dialog_init(hWnd, code, wParam, lParam); |
| 3504 | |
| 3505 | case WM_COMMAND: |
| 3506 | return input_text_dialog_cmd_proc(hWnd, code, wParam, lParam); |
| 3507 | |
| 3508 | default: |
| 3509 | return FALSE; |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | Error DisplayServerWindows::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) { |
| 3514 | #pragma pack(push, 1) |
| 3515 | |
| 3516 | // NOTE: Use default/placeholder coordinates here. Windows uses its own coordinate system |
| 3517 | // specifically for dialogs which relies on font sizes instead of pixels. |
| 3518 | const struct { |
| 3519 | WORD dlgVer; // must be 1 |
| 3520 | WORD signature; // must be 0xFFFF |
| 3521 | DWORD helpID; |
| 3522 | DWORD exStyle; |
| 3523 | DWORD style; |
| 3524 | WORD cDlgItems; |
| 3525 | short x; |
| 3526 | short y; |
| 3527 | short cx; |
| 3528 | short cy; |
| 3529 | WCHAR menu[1]; // must be 0 |
| 3530 | WCHAR windowClass[7]; // must be "#32770" -- the default window class for dialogs |
| 3531 | WCHAR title[1]; // must be 0 |
| 3532 | WORD pointsize; |
| 3533 | WORD weight; |
| 3534 | BYTE italic; |
| 3535 | BYTE charset; |
| 3536 | WCHAR font[13]; // must be "MS Shell Dlg" |
| 3537 | } template_base = { |
| 3538 | 1, 0xFFFF, 0, 0, |
| 3539 | DS_SYSMODAL | DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION, |
| 3540 | 3, 0, 0, 20, 20, L"", L"#32770", L"", 8, FW_NORMAL, 0, DEFAULT_CHARSET, L"MS Shell Dlg" |
| 3541 | }; |
| 3542 | |
| 3543 | const struct { |
| 3544 | DWORD helpID; |
| 3545 | DWORD exStyle; |
| 3546 | DWORD style; |
| 3547 | short x; |
| 3548 | short y; |
| 3549 | short cx; |
| 3550 | short cy; |
| 3551 | DWORD id; |
| 3552 | WCHAR windowClass[7]; // must be "Button" |
| 3553 | WCHAR title[3]; // must be "OK" |
| 3554 | WORD extraCount; |
| 3555 | } ok_button = { |
| 3556 | 0, 0, WS_VISIBLE | BS_DEFPUSHBUTTON, 0, 0, 50, 14, 1, WC_BUTTONW, L"OK", 0 |
| 3557 | }; |
| 3558 | const struct { |
| 3559 | DWORD helpID; |
| 3560 | DWORD exStyle; |
| 3561 | DWORD style; |
| 3562 | short x; |
| 3563 | short y; |
| 3564 | short cx; |
| 3565 | short cy; |
| 3566 | DWORD id; |
| 3567 | WCHAR windowClass[5]; // must be "Edit" |
| 3568 | WCHAR title[1]; // must be 0 |
| 3569 | WORD extraCount; |
| 3570 | } text_field = { |
| 3571 | 0, 0, WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, 0, 0, 250, 14, 2, WC_EDITW, L"", 0 |
| 3572 | }; |
| 3573 | const struct { |
| 3574 | DWORD helpID; |
| 3575 | DWORD exStyle; |
| 3576 | DWORD style; |
| 3577 | short x; |
| 3578 | short y; |
| 3579 | short cx; |
| 3580 | short cy; |
| 3581 | DWORD id; |
| 3582 | WCHAR windowClass[7]; // must be "Static" |
| 3583 | WCHAR title[1]; // must be 0 |
| 3584 | WORD extraCount; |
| 3585 | } static_text = { |
| 3586 | 0, 0, WS_VISIBLE, 0, 0, 250, 14, 3, WC_STATICW, L"", 0 |
| 3587 | }; |
| 3588 | |
| 3589 | #pragma pack(pop) |
| 3590 | |
| 3591 | // Dialog template |
| 3592 | const size_t data_size = sizeof(template_base) + (sizeof(template_base) % 4) + |
| 3593 | sizeof(ok_button) + (sizeof(ok_button) % 4) + |
| 3594 | sizeof(text_field) + (sizeof(text_field) % 4) + |
| 3595 | sizeof(static_text) + (sizeof(static_text) % 4); |
| 3596 | |
| 3597 | void *data_template = memalloc(data_size); |
| 3598 | ERR_FAIL_NULL_V_MSG(data_template, FAILED, "Unable to allocate memory for the dialog template."); |
| 3599 | ZeroMemory(data_template, data_size); |
| 3600 | |
| 3601 | char *current_block = (char *)data_template; |
| 3602 | CopyMemory(current_block, &template_base, sizeof(template_base)); |
| 3603 | current_block += sizeof(template_base) + (sizeof(template_base) % 4); |
| 3604 | CopyMemory(current_block, &ok_button, sizeof(ok_button)); |
| 3605 | current_block += sizeof(ok_button) + (sizeof(ok_button) % 4); |
| 3606 | CopyMemory(current_block, &text_field, sizeof(text_field)); |
| 3607 | current_block += sizeof(text_field) + (sizeof(text_field) % 4); |
| 3608 | CopyMemory(current_block, &static_text, sizeof(static_text)); |
| 3609 | |
| 3610 | Char16String title16 = p_title.utf16(); |
| 3611 | Char16String description16 = p_description.utf16(); |
| 3612 | Char16String partial16 = p_partial.utf16(); |
| 3613 | |
| 3614 | Win32InputTextDialogInit init = { |
| 3615 | title16.get_data(), description16.get_data(), partial16.get_data(), p_callback |
| 3616 | }; |
| 3617 | |
| 3618 | // No modal dialogs for specific windows? Assume main window here. |
| 3619 | INT_PTR ret = DialogBoxIndirectParamW(hInstance, (LPDLGTEMPLATEW)data_template, nullptr, (DLGPROC)input_text_dialog_proc, (LPARAM)(&init)); |
| 3620 | |
| 3621 | Error result = ret != -1 ? OK : FAILED; |
| 3622 | memfree(data_template); |
| 3623 | |
| 3624 | if (result == FAILED) { |
| 3625 | ERR_PRINT("Unable to create native dialog."); |
| 3626 | } |
| 3627 | return result; |
| 3628 | } |
| 3629 | |
| 3630 | int DisplayServerWindows::keyboard_get_layout_count() const { |
| 3631 | return GetKeyboardLayoutList(0, nullptr); |
| 3632 | } |
| 3633 | |
| 3634 | int DisplayServerWindows::keyboard_get_current_layout() const { |
| 3635 | HKL cur_layout = GetKeyboardLayout(0); |
| 3636 | |
| 3637 | int layout_count = GetKeyboardLayoutList(0, nullptr); |
| 3638 | HKL *layouts = (HKL *)memalloc(layout_count * sizeof(HKL)); |
| 3639 | GetKeyboardLayoutList(layout_count, layouts); |
| 3640 | |
| 3641 | for (int i = 0; i < layout_count; i++) { |
| 3642 | if (cur_layout == layouts[i]) { |
| 3643 | memfree(layouts); |
| 3644 | return i; |
| 3645 | } |
| 3646 | } |
| 3647 | memfree(layouts); |
| 3648 | return -1; |
| 3649 | } |
| 3650 | |
| 3651 | void DisplayServerWindows::keyboard_set_current_layout(int p_index) { |
| 3652 | int layout_count = GetKeyboardLayoutList(0, nullptr); |
| 3653 | |
| 3654 | ERR_FAIL_INDEX(p_index, layout_count); |
| 3655 | |
| 3656 | HKL *layouts = (HKL *)memalloc(layout_count * sizeof(HKL)); |
| 3657 | GetKeyboardLayoutList(layout_count, layouts); |
| 3658 | ActivateKeyboardLayout(layouts[p_index], KLF_SETFORPROCESS); |
| 3659 | memfree(layouts); |
| 3660 | } |
| 3661 | |
| 3662 | String DisplayServerWindows::keyboard_get_layout_language(int p_index) const { |
| 3663 | int layout_count = GetKeyboardLayoutList(0, nullptr); |
| 3664 | |
| 3665 | ERR_FAIL_INDEX_V(p_index, layout_count, ""); |
| 3666 | |
| 3667 | HKL *layouts = (HKL *)memalloc(layout_count * sizeof(HKL)); |
| 3668 | GetKeyboardLayoutList(layout_count, layouts); |
| 3669 | |
| 3670 | WCHAR buf[LOCALE_NAME_MAX_LENGTH]; |
| 3671 | memset(buf, 0, LOCALE_NAME_MAX_LENGTH * sizeof(WCHAR)); |
| 3672 | LCIDToLocaleName(MAKELCID(LOWORD(layouts[p_index]), SORT_DEFAULT), buf, LOCALE_NAME_MAX_LENGTH, 0); |
| 3673 | |
| 3674 | memfree(layouts); |
| 3675 | |
| 3676 | return String::utf16((const char16_t *)buf).substr(0, 2); |
| 3677 | } |
| 3678 | |
| 3679 | Key DisplayServerWindows::keyboard_get_keycode_from_physical(Key p_keycode) const { |
| 3680 | Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK; |
| 3681 | Key keycode_no_mod = (Key)(p_keycode & KeyModifierMask::CODE_MASK); |
| 3682 | |
| 3683 | if (keycode_no_mod == Key::PRINT || |
| 3684 | keycode_no_mod == Key::KP_ADD || |
| 3685 | keycode_no_mod == Key::KP_5 || |
| 3686 | (keycode_no_mod >= Key::KEY_0 && keycode_no_mod <= Key::KEY_9)) { |
| 3687 | return p_keycode; |
| 3688 | } |
| 3689 | |
| 3690 | unsigned int scancode = KeyMappingWindows::get_scancode(keycode_no_mod); |
| 3691 | if (scancode == 0) { |
| 3692 | return p_keycode; |
| 3693 | } |
| 3694 | |
| 3695 | HKL current_layout = GetKeyboardLayout(0); |
| 3696 | UINT vk = MapVirtualKeyEx(scancode, MAPVK_VSC_TO_VK, current_layout); |
| 3697 | if (vk == 0) { |
| 3698 | return p_keycode; |
| 3699 | } |
| 3700 | |
| 3701 | UINT char_code = MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, current_layout) & 0x7FFF; |
| 3702 | // Unlike a similar Linux/BSD check which matches full Latin-1 range, |
| 3703 | // we limit these to ASCII to fix some layouts, including Arabic ones |
| 3704 | if (char_code >= 32 && char_code <= 127) { |
| 3705 | // Godot uses 'braces' instead of 'brackets' |
| 3706 | if (char_code == (unsigned int)Key::BRACKETLEFT || char_code == (unsigned int)Key::BRACKETRIGHT) { |
| 3707 | char_code += 32; |
| 3708 | } |
| 3709 | return (Key)(char_code | (unsigned int)modifiers); |
| 3710 | } |
| 3711 | |
| 3712 | return (Key)(KeyMappingWindows::get_keysym(vk) | modifiers); |
| 3713 | } |
| 3714 | |
| 3715 | Key DisplayServerWindows::keyboard_get_label_from_physical(Key p_keycode) const { |
| 3716 | Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK; |
| 3717 | Key keycode_no_mod = (Key)(p_keycode & KeyModifierMask::CODE_MASK); |
| 3718 | |
| 3719 | if (keycode_no_mod == Key::PRINT || |
| 3720 | keycode_no_mod == Key::KP_ADD || |
| 3721 | keycode_no_mod == Key::KP_5 || |
| 3722 | (keycode_no_mod >= Key::KEY_0 && keycode_no_mod <= Key::KEY_9)) { |
| 3723 | return p_keycode; |
| 3724 | } |
| 3725 | |
| 3726 | unsigned int scancode = KeyMappingWindows::get_scancode(keycode_no_mod); |
| 3727 | if (scancode == 0) { |
| 3728 | return p_keycode; |
| 3729 | } |
| 3730 | |
| 3731 | Key keycode = KeyMappingWindows::get_keysym(MapVirtualKey(scancode, MAPVK_VSC_TO_VK)); |
| 3732 | |
| 3733 | HKL current_layout = GetKeyboardLayout(0); |
| 3734 | static BYTE keyboard_state[256]; |
| 3735 | memset(keyboard_state, 0, 256); |
| 3736 | wchar_t chars[256] = {}; |
| 3737 | UINT extended_code = MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX); |
| 3738 | if (ToUnicodeEx(extended_code, scancode, keyboard_state, chars, 255, 4, current_layout) > 0) { |
| 3739 | String keysym = String::utf16((char16_t *)chars, 255); |
| 3740 | if (!keysym.is_empty()) { |
| 3741 | return fix_key_label(keysym[0], keycode) | modifiers; |
| 3742 | } |
| 3743 | } |
| 3744 | return p_keycode; |
| 3745 | } |
| 3746 | |
| 3747 | void DisplayServerWindows::show_emoji_and_symbol_picker() const { |
| 3748 | // Send Win + Period shortcut, there's no non-WinRT public API. |
| 3749 | |
| 3750 | INPUT input[4] = {}; |
| 3751 | input[0].type = INPUT_KEYBOARD; // Win down. |
| 3752 | input[0].ki.wVk = VK_LWIN; |
| 3753 | |
| 3754 | input[1].type = INPUT_KEYBOARD; // Period down. |
| 3755 | input[1].ki.wVk = VK_OEM_PERIOD; |
| 3756 | |
| 3757 | input[2].type = INPUT_KEYBOARD; // Win up. |
| 3758 | input[2].ki.wVk = VK_LWIN; |
| 3759 | input[2].ki.dwFlags = KEYEVENTF_KEYUP; |
| 3760 | |
| 3761 | input[3].type = INPUT_KEYBOARD; // Period up. |
| 3762 | input[3].ki.wVk = VK_OEM_PERIOD; |
| 3763 | input[3].ki.dwFlags = KEYEVENTF_KEYUP; |
| 3764 | |
| 3765 | SendInput(4, input, sizeof(INPUT)); |
| 3766 | } |
| 3767 | |
| 3768 | String DisplayServerWindows::_get_keyboard_layout_display_name(const String &p_klid) const { |
| 3769 | String ret; |
| 3770 | HKEY key; |
| 3771 | if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", &key) != ERROR_SUCCESS) { |
| 3772 | return String(); |
| 3773 | } |
| 3774 | |
| 3775 | WCHAR buffer[MAX_PATH] = {}; |
| 3776 | DWORD buffer_size = MAX_PATH; |
| 3777 | if (RegGetValueW(key, (LPCWSTR)p_klid.utf16().get_data(), L"Layout Display Name", RRF_RT_REG_SZ, nullptr, buffer, &buffer_size) == ERROR_SUCCESS) { |
| 3778 | if (SHLoadIndirectString(buffer, buffer, buffer_size, nullptr) == S_OK) { |
| 3779 | ret = String::utf16((const char16_t *)buffer, buffer_size); |
| 3780 | } |
| 3781 | } else { |
| 3782 | if (RegGetValueW(key, (LPCWSTR)p_klid.utf16().get_data(), L"Layout Text", RRF_RT_REG_SZ, nullptr, buffer, &buffer_size) == ERROR_SUCCESS) { |
| 3783 | ret = String::utf16((const char16_t *)buffer, buffer_size); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | RegCloseKey(key); |
| 3788 | return ret; |
| 3789 | } |
| 3790 | |
| 3791 | String DisplayServerWindows::_get_klid(HKL p_hkl) const { |
| 3792 | String ret; |
| 3793 | |
| 3794 | WORD device = HIWORD(p_hkl); |
| 3795 | if ((device & 0xf000) == 0xf000) { |
| 3796 | WORD layout_id = device & 0x0fff; |
| 3797 | |
| 3798 | HKEY key; |
| 3799 | if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", &key) != ERROR_SUCCESS) { |
| 3800 | return String(); |
| 3801 | } |
| 3802 | |
| 3803 | DWORD index = 0; |
| 3804 | wchar_t klid_buffer[KL_NAMELENGTH]; |
| 3805 | DWORD klid_buffer_size = KL_NAMELENGTH; |
| 3806 | while (RegEnumKeyExW(key, index, klid_buffer, &klid_buffer_size, nullptr, nullptr, nullptr, nullptr) == ERROR_SUCCESS) { |
| 3807 | wchar_t layout_id_buf[MAX_PATH] = {}; |
| 3808 | DWORD layout_id_size = MAX_PATH; |
| 3809 | if (RegGetValueW(key, klid_buffer, L"Layout Id", RRF_RT_REG_SZ, nullptr, layout_id_buf, &layout_id_size) == ERROR_SUCCESS) { |
| 3810 | if (layout_id == String::utf16((char16_t *)layout_id_buf, layout_id_size).hex_to_int()) { |
| 3811 | ret = String::utf16((const char16_t *)klid_buffer, klid_buffer_size).lpad(8, "0"); |
| 3812 | break; |
| 3813 | } |
| 3814 | } |
| 3815 | klid_buffer_size = KL_NAMELENGTH; |
| 3816 | ++index; |
| 3817 | } |
| 3818 | |
| 3819 | RegCloseKey(key); |
| 3820 | } else { |
| 3821 | if (device == 0) { |
| 3822 | device = LOWORD(p_hkl); |
| 3823 | } |
| 3824 | ret = (String::num_uint64((uint64_t)device, 16, false)).lpad(8, "0"); |
| 3825 | } |
| 3826 | |
| 3827 | return ret; |
| 3828 | } |
| 3829 | |
| 3830 | String DisplayServerWindows::keyboard_get_layout_name(int p_index) const { |
| 3831 | int layout_count = GetKeyboardLayoutList(0, nullptr); |
| 3832 | |
| 3833 | ERR_FAIL_INDEX_V(p_index, layout_count, ""); |
| 3834 | |
| 3835 | HKL *layouts = (HKL *)memalloc(layout_count * sizeof(HKL)); |
| 3836 | GetKeyboardLayoutList(layout_count, layouts); |
| 3837 | |
| 3838 | String ret = _get_keyboard_layout_display_name(_get_klid(layouts[p_index])); // Try reading full name from Windows registry, fallback to locale name if failed (e.g. on Wine). |
| 3839 | if (ret.is_empty()) { |
| 3840 | WCHAR buf[LOCALE_NAME_MAX_LENGTH]; |
| 3841 | memset(buf, 0, LOCALE_NAME_MAX_LENGTH * sizeof(WCHAR)); |
| 3842 | LCIDToLocaleName(MAKELCID(LOWORD(layouts[p_index]), SORT_DEFAULT), buf, LOCALE_NAME_MAX_LENGTH, 0); |
| 3843 | |
| 3844 | WCHAR name[1024]; |
| 3845 | memset(name, 0, 1024 * sizeof(WCHAR)); |
| 3846 | GetLocaleInfoEx(buf, LOCALE_SLOCALIZEDDISPLAYNAME, (LPWSTR)&name, 1024); |
| 3847 | |
| 3848 | ret = String::utf16((const char16_t *)name); |
| 3849 | } |
| 3850 | memfree(layouts); |
| 3851 | |
| 3852 | return ret; |
| 3853 | } |
| 3854 | |
| 3855 | void DisplayServerWindows::process_events() { |
| 3856 | ERR_FAIL_COND(!Thread::is_main_thread()); |
| 3857 | |
| 3858 | if (!drop_events) { |
| 3859 | #ifdef SDL_ENABLED |
| 3860 | if (joypad_sdl) { |
| 3861 | joypad_sdl->process_events(); |
| 3862 | } |
| 3863 | #endif |
| 3864 | } |
| 3865 | |
| 3866 | _THREAD_SAFE_LOCK_ |
| 3867 | MSG msg = {}; |
| 3868 | while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { |
| 3869 | TranslateMessage(&msg); |
| 3870 | DispatchMessageW(&msg); |
| 3871 | } |
| 3872 | _THREAD_SAFE_UNLOCK_ |
| 3873 | |
| 3874 | if (tts) { |
| 3875 | tts->process_events(); |
| 3876 | } |
| 3877 | |
| 3878 | if (!drop_events) { |
| 3879 | _process_key_events(); |
| 3880 | Input::get_singleton()->flush_buffered_events(); |
| 3881 | } |
| 3882 | |
| 3883 | LocalVector<List<FileDialogData *>::Element *> to_remove; |
| 3884 | for (List<FileDialogData *>::Element *E = file_dialogs.front(); E; E = E->next()) { |
| 3885 | FileDialogData *fd = E->get(); |
| 3886 | if (fd->finished.is_set()) { |
| 3887 | if (fd->listener_thread.is_started()) { |
| 3888 | fd->listener_thread.wait_to_finish(); |
| 3889 | } |
| 3890 | to_remove.push_back(E); |
| 3891 | } |
| 3892 | } |
| 3893 | for (List<FileDialogData *>::Element *E : to_remove) { |
| 3894 | memdelete(E->get()); |
| 3895 | E->erase(); |
| 3896 | } |
| 3897 | process_file_dialog_callbacks(); |
| 3898 | } |
| 3899 | |
| 3900 | void DisplayServerWindows::force_process_and_drop_events() { |
| 3901 | ERR_FAIL_COND(!Thread::is_main_thread()); |
| 3902 | |
| 3903 | drop_events = true; |
| 3904 | process_events(); |
| 3905 | drop_events = false; |
| 3906 | } |
| 3907 | |
| 3908 | void DisplayServerWindows::release_rendering_thread() { |
| 3909 | #if defined(GLES3_ENABLED) |
| 3910 | if (gl_manager_angle) { |
| 3911 | gl_manager_angle->release_current(); |
| 3912 | } |
| 3913 | if (gl_manager_native) { |
| 3914 | gl_manager_native->release_current(); |
| 3915 | } |
| 3916 | #endif |
| 3917 | } |
| 3918 | |
| 3919 | void DisplayServerWindows::swap_buffers() { |
| 3920 | #if defined(GLES3_ENABLED) |
| 3921 | if (gl_manager_angle) { |
| 3922 | gl_manager_angle->swap_buffers(); |
| 3923 | } |
| 3924 | if (gl_manager_native) { |
| 3925 | gl_manager_native->swap_buffers(); |
| 3926 | } |
| 3927 | #endif |
| 3928 | } |
| 3929 | |
| 3930 | void DisplayServerWindows::set_native_icon(const String &p_filename) { |
| 3931 | _THREAD_SAFE_METHOD_ |
| 3932 | |
| 3933 | if (icon_big) { |
| 3934 | DestroyIcon(icon_big); |
| 3935 | icon_buffer_big.clear(); |
| 3936 | icon_big = nullptr; |
| 3937 | } |
| 3938 | if (icon_small) { |
| 3939 | DestroyIcon(icon_small); |
| 3940 | icon_buffer_small.clear(); |
| 3941 | icon_small = nullptr; |
| 3942 | } |
| 3943 | |
| 3944 | Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::READ); |
| 3945 | ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file with icon '" + p_filename + "'."); |
| 3946 | |
| 3947 | ICONDIR *icon_dir = (ICONDIR *)memalloc(sizeof(ICONDIR)); |
| 3948 | int pos = 0; |
| 3949 | |
| 3950 | icon_dir->idReserved = f->get_32(); |
| 3951 | pos += sizeof(WORD); |
| 3952 | f->seek(pos); |
| 3953 | |
| 3954 | icon_dir->idType = f->get_32(); |
| 3955 | pos += sizeof(WORD); |
| 3956 | f->seek(pos); |
| 3957 | |
| 3958 | ERR_FAIL_COND_MSG(icon_dir->idType != 1, "Invalid icon file format!"); |
| 3959 | |
| 3960 | icon_dir->idCount = f->get_32(); |
| 3961 | pos += sizeof(WORD); |
| 3962 | f->seek(pos); |
| 3963 | |
| 3964 | icon_dir = (ICONDIR *)memrealloc(icon_dir, sizeof(ICONDIR) - sizeof(ICONDIRENTRY) + icon_dir->idCount * sizeof(ICONDIRENTRY)); |
| 3965 | f->get_buffer((uint8_t *)&icon_dir->idEntries[0], icon_dir->idCount * sizeof(ICONDIRENTRY)); |
| 3966 | |
| 3967 | int small_icon_index = -1; // Select 16x16 with largest color count. |
| 3968 | int small_icon_cc = 0; |
| 3969 | int big_icon_index = -1; // Select largest. |
| 3970 | int big_icon_width = 16; |
| 3971 | int big_icon_cc = 0; |
| 3972 | |
| 3973 | for (int i = 0; i < icon_dir->idCount; i++) { |
| 3974 | int colors = (icon_dir->idEntries[i].bColorCount == 0) ? 32768 : icon_dir->idEntries[i].bColorCount; |
| 3975 | int width = (icon_dir->idEntries[i].bWidth == 0) ? 256 : icon_dir->idEntries[i].bWidth; |
| 3976 | if (width == 16) { |
| 3977 | if (colors >= small_icon_cc) { |
| 3978 | small_icon_index = i; |
| 3979 | small_icon_cc = colors; |
| 3980 | } |
| 3981 | } |
| 3982 | if (width >= big_icon_width) { |
| 3983 | if (colors >= big_icon_cc) { |
| 3984 | big_icon_index = i; |
| 3985 | big_icon_width = width; |
| 3986 | big_icon_cc = colors; |
| 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | ERR_FAIL_COND_MSG(big_icon_index == -1, "No valid icons found!"); |
| 3992 | |
| 3993 | if (small_icon_index == -1) { |
| 3994 | WARN_PRINT("No small icon found, reusing " + itos(big_icon_width) + "x" + itos(big_icon_width) + " @" + itos(big_icon_cc) + " icon!"); |
| 3995 | small_icon_index = big_icon_index; |
| 3996 | small_icon_cc = big_icon_cc; |
| 3997 | } |
| 3998 | |
| 3999 | // Read the big icon. |
| 4000 | DWORD bytecount_big = icon_dir->idEntries[big_icon_index].dwBytesInRes; |
| 4001 | icon_buffer_big.resize(bytecount_big); |
| 4002 | pos = icon_dir->idEntries[big_icon_index].dwImageOffset; |
| 4003 | f->seek(pos); |
| 4004 | f->get_buffer((uint8_t *)&icon_buffer_big.write[0], bytecount_big); |
| 4005 | icon_big = CreateIconFromResourceEx((PBYTE)&icon_buffer_big.write[0], bytecount_big, TRUE, 0x00030000, 0, 0, LR_DEFAULTSIZE); |
| 4006 | ERR_FAIL_NULL_MSG(icon_big, "Could not create " + itos(big_icon_width) + "x" + itos(big_icon_width) + " @" + itos(big_icon_cc) + " icon, error: " + format_error_message(GetLastError()) + "."); |
| 4007 | |
| 4008 | // Read the small icon. |
| 4009 | DWORD bytecount_small = icon_dir->idEntries[small_icon_index].dwBytesInRes; |
| 4010 | icon_buffer_small.resize(bytecount_small); |
| 4011 | pos = icon_dir->idEntries[small_icon_index].dwImageOffset; |
| 4012 | f->seek(pos); |
| 4013 | f->get_buffer((uint8_t *)&icon_buffer_small.write[0], bytecount_small); |
| 4014 | icon_small = CreateIconFromResourceEx((PBYTE)&icon_buffer_small.write[0], bytecount_small, TRUE, 0x00030000, 0, 0, LR_DEFAULTSIZE); |
| 4015 | ERR_FAIL_NULL_MSG(icon_small, "Could not create 16x16 @" + itos(small_icon_cc) + " icon, error: " + format_error_message(GetLastError()) + "."); |
| 4016 | |
| 4017 | // Online tradition says to be sure last error is cleared and set the small icon first. |
| 4018 | int err = 0; |
| 4019 | SetLastError(err); |
| 4020 | |
| 4021 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4022 | SendMessage(E.value.hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon_small); |
| 4023 | SendMessage(E.value.hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon_big); |
| 4024 | } |
| 4025 | memdelete(icon_dir); |
| 4026 | } |
| 4027 | |
| 4028 | void DisplayServerWindows::set_icon(const Ref<Image> &p_icon) { |
| 4029 | _THREAD_SAFE_METHOD_ |
| 4030 | |
| 4031 | if (icon_big) { |
| 4032 | DestroyIcon(icon_big); |
| 4033 | icon_buffer_big.clear(); |
| 4034 | icon_big = nullptr; |
| 4035 | } |
| 4036 | if (icon_small) { |
| 4037 | DestroyIcon(icon_small); |
| 4038 | icon_buffer_small.clear(); |
| 4039 | icon_small = nullptr; |
| 4040 | } |
| 4041 | |
| 4042 | if (p_icon.is_valid()) { |
| 4043 | ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0); |
| 4044 | |
| 4045 | Ref<Image> img = p_icon->duplicate(); |
| 4046 | img->convert(Image::FORMAT_RGBA8); |
| 4047 | |
| 4048 | int w = img->get_width(); |
| 4049 | int h = img->get_height(); |
| 4050 | |
| 4051 | // Create temporary bitmap buffer. |
| 4052 | int icon_len = 40 + h * w * 4; |
| 4053 | icon_buffer_big.resize(icon_len); |
| 4054 | BYTE *icon_bmp = icon_buffer_big.ptrw(); |
| 4055 | |
| 4056 | encode_uint32(40, &icon_bmp[0]); |
| 4057 | encode_uint32(w, &icon_bmp[4]); |
| 4058 | encode_uint32(h * 2, &icon_bmp[8]); |
| 4059 | encode_uint16(1, &icon_bmp[12]); |
| 4060 | encode_uint16(32, &icon_bmp[14]); |
| 4061 | encode_uint32(BI_RGB, &icon_bmp[16]); |
| 4062 | encode_uint32(w * h * 4, &icon_bmp[20]); |
| 4063 | encode_uint32(0, &icon_bmp[24]); |
| 4064 | encode_uint32(0, &icon_bmp[28]); |
| 4065 | encode_uint32(0, &icon_bmp[32]); |
| 4066 | encode_uint32(0, &icon_bmp[36]); |
| 4067 | |
| 4068 | uint8_t *wr = &icon_bmp[40]; |
| 4069 | const uint8_t *r = img->get_data().ptr(); |
| 4070 | |
| 4071 | for (int i = 0; i < h; i++) { |
| 4072 | for (int j = 0; j < w; j++) { |
| 4073 | const uint8_t *rpx = &r[((h - i - 1) * w + j) * 4]; |
| 4074 | uint8_t *wpx = &wr[(i * w + j) * 4]; |
| 4075 | wpx[0] = rpx[2]; |
| 4076 | wpx[1] = rpx[1]; |
| 4077 | wpx[2] = rpx[0]; |
| 4078 | wpx[3] = rpx[3]; |
| 4079 | } |
| 4080 | } |
| 4081 | icon_big = CreateIconFromResourceEx(icon_bmp, icon_len, TRUE, 0x00030000, 0, 0, LR_DEFAULTSIZE); |
| 4082 | ERR_FAIL_NULL(icon_big); |
| 4083 | |
| 4084 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4085 | SendMessage(E.value.hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon_big); |
| 4086 | SendMessage(E.value.hWnd, WM_SETICON, ICON_BIG, (LPARAM)icon_big); |
| 4087 | } |
| 4088 | } else { |
| 4089 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4090 | SendMessage(E.value.hWnd, WM_SETICON, ICON_SMALL, 0); |
| 4091 | SendMessage(E.value.hWnd, WM_SETICON, ICON_BIG, 0); |
| 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | DisplayServer::IndicatorID DisplayServerWindows::create_status_indicator(const Ref<Texture2D> &p_icon, const String &p_tooltip, const Callable &p_callback) { |
| 4097 | IndicatorData idat; |
| 4098 | if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { |
| 4099 | Ref<Image> img = p_icon->get_image(); |
| 4100 | img = img->duplicate(); |
| 4101 | if (img->is_compressed()) { |
| 4102 | img->decompress(); |
| 4103 | } |
| 4104 | img->convert(Image::FORMAT_RGBA8); |
| 4105 | |
| 4106 | int w = img->get_width(); |
| 4107 | int h = img->get_height(); |
| 4108 | |
| 4109 | // Create temporary bitmap buffer. |
| 4110 | int icon_len = 40 + h * w * 4; |
| 4111 | idat.icon_buffer.resize(icon_len); |
| 4112 | BYTE *icon_bmp = idat.icon_buffer.ptrw(); |
| 4113 | |
| 4114 | encode_uint32(40, &icon_bmp[0]); |
| 4115 | encode_uint32(w, &icon_bmp[4]); |
| 4116 | encode_uint32(h * 2, &icon_bmp[8]); |
| 4117 | encode_uint16(1, &icon_bmp[12]); |
| 4118 | encode_uint16(32, &icon_bmp[14]); |
| 4119 | encode_uint32(BI_RGB, &icon_bmp[16]); |
| 4120 | encode_uint32(w * h * 4, &icon_bmp[20]); |
| 4121 | encode_uint32(0, &icon_bmp[24]); |
| 4122 | encode_uint32(0, &icon_bmp[28]); |
| 4123 | encode_uint32(0, &icon_bmp[32]); |
| 4124 | encode_uint32(0, &icon_bmp[36]); |
| 4125 | |
| 4126 | uint8_t *wr = &icon_bmp[40]; |
| 4127 | const uint8_t *r = img->get_data().ptr(); |
| 4128 | |
| 4129 | for (int i = 0; i < h; i++) { |
| 4130 | for (int j = 0; j < w; j++) { |
| 4131 | const uint8_t *rpx = &r[((h - i - 1) * w + j) * 4]; |
| 4132 | uint8_t *wpx = &wr[(i * w + j) * 4]; |
| 4133 | wpx[0] = rpx[2]; |
| 4134 | wpx[1] = rpx[1]; |
| 4135 | wpx[2] = rpx[0]; |
| 4136 | wpx[3] = rpx[3]; |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | idat.icon = CreateIconFromResourceEx(icon_bmp, icon_len, TRUE, 0x00030000, 0, 0, LR_DEFAULTSIZE); |
| 4141 | } |
| 4142 | |
| 4143 | idat.callback = p_callback; |
| 4144 | |
| 4145 | NOTIFYICONDATAW ndat; |
| 4146 | ZeroMemory(&ndat, sizeof(NOTIFYICONDATAW)); |
| 4147 | ndat.cbSize = sizeof(NOTIFYICONDATAW); |
| 4148 | ndat.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4149 | ndat.uID = indicator_id_counter; |
| 4150 | ndat.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; |
| 4151 | ndat.uCallbackMessage = WM_INDICATOR_CALLBACK_MESSAGE; |
| 4152 | ndat.hIcon = idat.icon; |
| 4153 | memcpy(ndat.szTip, p_tooltip.utf16().get_data(), MIN(p_tooltip.utf16().length(), 127) * sizeof(WCHAR)); |
| 4154 | ndat.uVersion = NOTIFYICON_VERSION; |
| 4155 | |
| 4156 | Shell_NotifyIconW(NIM_ADD, &ndat); |
| 4157 | Shell_NotifyIconW(NIM_SETVERSION, &ndat); |
| 4158 | |
| 4159 | IndicatorID iid = indicator_id_counter++; |
| 4160 | indicators[iid] = idat; |
| 4161 | |
| 4162 | return iid; |
| 4163 | } |
| 4164 | |
| 4165 | void DisplayServerWindows::status_indicator_set_icon(IndicatorID p_id, const Ref<Texture2D> &p_icon) { |
| 4166 | ERR_FAIL_COND(!indicators.has(p_id)); |
| 4167 | |
| 4168 | IndicatorData &idat = indicators[p_id]; |
| 4169 | |
| 4170 | if (idat.icon) { |
| 4171 | DestroyIcon(idat.icon); |
| 4172 | idat.icon_buffer.clear(); |
| 4173 | idat.icon = nullptr; |
| 4174 | } |
| 4175 | |
| 4176 | if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { |
| 4177 | Ref<Image> img = p_icon->get_image(); |
| 4178 | img = img->duplicate(); |
| 4179 | if (img->is_compressed()) { |
| 4180 | img->decompress(); |
| 4181 | } |
| 4182 | img->convert(Image::FORMAT_RGBA8); |
| 4183 | |
| 4184 | int w = img->get_width(); |
| 4185 | int h = img->get_height(); |
| 4186 | |
| 4187 | // Create temporary bitmap buffer. |
| 4188 | int icon_len = 40 + h * w * 4; |
| 4189 | idat.icon_buffer.resize(icon_len); |
| 4190 | BYTE *icon_bmp = idat.icon_buffer.ptrw(); |
| 4191 | |
| 4192 | encode_uint32(40, &icon_bmp[0]); |
| 4193 | encode_uint32(w, &icon_bmp[4]); |
| 4194 | encode_uint32(h * 2, &icon_bmp[8]); |
| 4195 | encode_uint16(1, &icon_bmp[12]); |
| 4196 | encode_uint16(32, &icon_bmp[14]); |
| 4197 | encode_uint32(BI_RGB, &icon_bmp[16]); |
| 4198 | encode_uint32(w * h * 4, &icon_bmp[20]); |
| 4199 | encode_uint32(0, &icon_bmp[24]); |
| 4200 | encode_uint32(0, &icon_bmp[28]); |
| 4201 | encode_uint32(0, &icon_bmp[32]); |
| 4202 | encode_uint32(0, &icon_bmp[36]); |
| 4203 | |
| 4204 | uint8_t *wr = &icon_bmp[40]; |
| 4205 | const uint8_t *r = img->get_data().ptr(); |
| 4206 | |
| 4207 | for (int i = 0; i < h; i++) { |
| 4208 | for (int j = 0; j < w; j++) { |
| 4209 | const uint8_t *rpx = &r[((h - i - 1) * w + j) * 4]; |
| 4210 | uint8_t *wpx = &wr[(i * w + j) * 4]; |
| 4211 | wpx[0] = rpx[2]; |
| 4212 | wpx[1] = rpx[1]; |
| 4213 | wpx[2] = rpx[0]; |
| 4214 | wpx[3] = rpx[3]; |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | idat.icon = CreateIconFromResourceEx(icon_bmp, icon_len, TRUE, 0x00030000, 0, 0, LR_DEFAULTSIZE); |
| 4219 | } |
| 4220 | |
| 4221 | NOTIFYICONDATAW ndat; |
| 4222 | ZeroMemory(&ndat, sizeof(NOTIFYICONDATAW)); |
| 4223 | ndat.cbSize = sizeof(NOTIFYICONDATAW); |
| 4224 | ndat.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4225 | ndat.uID = p_id; |
| 4226 | ndat.uFlags = NIF_ICON; |
| 4227 | ndat.hIcon = idat.icon; |
| 4228 | ndat.uVersion = NOTIFYICON_VERSION; |
| 4229 | |
| 4230 | Shell_NotifyIconW(NIM_MODIFY, &ndat); |
| 4231 | } |
| 4232 | |
| 4233 | void DisplayServerWindows::status_indicator_set_tooltip(IndicatorID p_id, const String &p_tooltip) { |
| 4234 | ERR_FAIL_COND(!indicators.has(p_id)); |
| 4235 | |
| 4236 | NOTIFYICONDATAW ndat; |
| 4237 | ZeroMemory(&ndat, sizeof(NOTIFYICONDATAW)); |
| 4238 | ndat.cbSize = sizeof(NOTIFYICONDATAW); |
| 4239 | ndat.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4240 | ndat.uID = p_id; |
| 4241 | ndat.uFlags = NIF_TIP; |
| 4242 | memcpy(ndat.szTip, p_tooltip.utf16().get_data(), MIN(p_tooltip.utf16().length(), 127) * sizeof(WCHAR)); |
| 4243 | ndat.uVersion = NOTIFYICON_VERSION; |
| 4244 | |
| 4245 | Shell_NotifyIconW(NIM_MODIFY, &ndat); |
| 4246 | } |
| 4247 | |
| 4248 | void DisplayServerWindows::status_indicator_set_menu(IndicatorID p_id, const RID &p_menu_rid) { |
| 4249 | ERR_FAIL_COND(!indicators.has(p_id)); |
| 4250 | |
| 4251 | indicators[p_id].menu_rid = p_menu_rid; |
| 4252 | } |
| 4253 | |
| 4254 | void DisplayServerWindows::status_indicator_set_callback(IndicatorID p_id, const Callable &p_callback) { |
| 4255 | ERR_FAIL_COND(!indicators.has(p_id)); |
| 4256 | |
| 4257 | indicators[p_id].callback = p_callback; |
| 4258 | } |
| 4259 | |
| 4260 | Rect2 DisplayServerWindows::status_indicator_get_rect(IndicatorID p_id) const { |
| 4261 | ERR_FAIL_COND_V(!indicators.has(p_id), Rect2()); |
| 4262 | |
| 4263 | NOTIFYICONIDENTIFIER nid; |
| 4264 | ZeroMemory(&nid, sizeof(NOTIFYICONIDENTIFIER)); |
| 4265 | nid.cbSize = sizeof(NOTIFYICONIDENTIFIER); |
| 4266 | nid.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4267 | nid.uID = p_id; |
| 4268 | nid.guidItem = GUID_NULL; |
| 4269 | |
| 4270 | RECT rect; |
| 4271 | if (Shell_NotifyIconGetRect(&nid, &rect) != S_OK) { |
| 4272 | return Rect2(); |
| 4273 | } |
| 4274 | Rect2 ind_rect = Rect2(Point2(rect.left, rect.top) - _get_screens_origin(), Size2(rect.right - rect.left, rect.bottom - rect.top)); |
| 4275 | for (int i = 0; i < get_screen_count(); i++) { |
| 4276 | Rect2 screen_rect = Rect2(screen_get_position(i), screen_get_size(i)); |
| 4277 | if (screen_rect.encloses(ind_rect)) { |
| 4278 | return ind_rect; |
| 4279 | } |
| 4280 | } |
| 4281 | return Rect2(); |
| 4282 | } |
| 4283 | |
| 4284 | void DisplayServerWindows::delete_status_indicator(IndicatorID p_id) { |
| 4285 | ERR_FAIL_COND(!indicators.has(p_id)); |
| 4286 | |
| 4287 | IndicatorData &idat = indicators[p_id]; |
| 4288 | if (idat.icon) { |
| 4289 | DestroyIcon(idat.icon); |
| 4290 | idat.icon_buffer.clear(); |
| 4291 | idat.icon = nullptr; |
| 4292 | } |
| 4293 | |
| 4294 | NOTIFYICONDATAW ndat; |
| 4295 | ZeroMemory(&ndat, sizeof(NOTIFYICONDATAW)); |
| 4296 | ndat.cbSize = sizeof(NOTIFYICONDATAW); |
| 4297 | ndat.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4298 | ndat.uID = p_id; |
| 4299 | ndat.uVersion = NOTIFYICON_VERSION; |
| 4300 | |
| 4301 | Shell_NotifyIconW(NIM_DELETE, &ndat); |
| 4302 | indicators.erase(p_id); |
| 4303 | } |
| 4304 | |
| 4305 | void DisplayServerWindows::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) { |
| 4306 | _THREAD_SAFE_METHOD_ |
| 4307 | #if defined(RD_ENABLED) |
| 4308 | if (rendering_context) { |
| 4309 | rendering_context->window_set_vsync_mode(p_window, p_vsync_mode); |
| 4310 | } |
| 4311 | #endif |
| 4312 | |
| 4313 | #if defined(GLES3_ENABLED) |
| 4314 | if (gl_manager_native) { |
| 4315 | gl_manager_native->set_use_vsync(p_window, p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 4316 | } |
| 4317 | if (gl_manager_angle) { |
| 4318 | gl_manager_angle->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 4319 | } |
| 4320 | #endif |
| 4321 | } |
| 4322 | |
| 4323 | DisplayServer::VSyncMode DisplayServerWindows::window_get_vsync_mode(WindowID p_window) const { |
| 4324 | _THREAD_SAFE_METHOD_ |
| 4325 | #if defined(RD_ENABLED) |
| 4326 | if (rendering_context) { |
| 4327 | return rendering_context->window_get_vsync_mode(p_window); |
| 4328 | } |
| 4329 | #endif |
| 4330 | |
| 4331 | #if defined(GLES3_ENABLED) |
| 4332 | if (gl_manager_native) { |
| 4333 | return gl_manager_native->is_using_vsync(p_window) ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED; |
| 4334 | } |
| 4335 | if (gl_manager_angle) { |
| 4336 | return gl_manager_angle->is_using_vsync() ? DisplayServer::VSYNC_ENABLED : DisplayServer::VSYNC_DISABLED; |
| 4337 | } |
| 4338 | #endif |
| 4339 | return DisplayServer::VSYNC_ENABLED; |
| 4340 | } |
| 4341 | |
| 4342 | void DisplayServerWindows::window_start_drag(WindowID p_window) { |
| 4343 | _THREAD_SAFE_METHOD_ |
| 4344 | |
| 4345 | ERR_FAIL_COND(!windows.has(p_window)); |
| 4346 | WindowData &wd = windows[p_window]; |
| 4347 | |
| 4348 | if (wd.parent_hwnd) { |
| 4349 | return; // Embedded window. |
| 4350 | } |
| 4351 | |
| 4352 | ReleaseCapture(); |
| 4353 | |
| 4354 | POINT coords; |
| 4355 | GetCursorPos(&coords); |
| 4356 | ScreenToClient(wd.hWnd, &coords); |
| 4357 | |
| 4358 | SendMessage(wd.hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, MAKELPARAM(coords.x, coords.y)); |
| 4359 | |
| 4360 | for (int btn = (int)MouseButton::LEFT; btn <= (int)MouseButton::MB_XBUTTON2; btn++) { |
| 4361 | if (Input::get_singleton()->is_mouse_button_pressed(MouseButton(btn))) { |
| 4362 | Ref<InputEventMouseButton> mb; |
| 4363 | mb.instantiate(); |
| 4364 | mb->set_window_id(p_window); |
| 4365 | mb->set_pressed(false); |
| 4366 | mb->set_button_index(MouseButton::LEFT); |
| 4367 | mb->set_position(Vector2(coords.x, coords.y)); |
| 4368 | mb->set_global_position(mb->get_position()); |
| 4369 | Input::get_singleton()->parse_input_event(mb); |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | void DisplayServerWindows::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) { |
| 4375 | _THREAD_SAFE_METHOD_ |
| 4376 | |
| 4377 | ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX); |
| 4378 | ERR_FAIL_COND(!windows.has(p_window)); |
| 4379 | WindowData &wd = windows[p_window]; |
| 4380 | |
| 4381 | if (wd.parent_hwnd) { |
| 4382 | return; // Embedded window. |
| 4383 | } |
| 4384 | |
| 4385 | ReleaseCapture(); |
| 4386 | |
| 4387 | POINT coords; |
| 4388 | GetCursorPos(&coords); |
| 4389 | ScreenToClient(wd.hWnd, &coords); |
| 4390 | |
| 4391 | DWORD op = 0; |
| 4392 | switch (p_edge) { |
| 4393 | case DisplayServer::WINDOW_EDGE_TOP_LEFT: { |
| 4394 | op = WMSZ_TOPLEFT; |
| 4395 | } break; |
| 4396 | case DisplayServer::WINDOW_EDGE_TOP: { |
| 4397 | op = WMSZ_TOP; |
| 4398 | } break; |
| 4399 | case DisplayServer::WINDOW_EDGE_TOP_RIGHT: { |
| 4400 | op = WMSZ_TOPRIGHT; |
| 4401 | } break; |
| 4402 | case DisplayServer::WINDOW_EDGE_LEFT: { |
| 4403 | op = WMSZ_LEFT; |
| 4404 | } break; |
| 4405 | case DisplayServer::WINDOW_EDGE_RIGHT: { |
| 4406 | op = WMSZ_RIGHT; |
| 4407 | } break; |
| 4408 | case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: { |
| 4409 | op = WMSZ_BOTTOMLEFT; |
| 4410 | } break; |
| 4411 | case DisplayServer::WINDOW_EDGE_BOTTOM: { |
| 4412 | op = WMSZ_BOTTOM; |
| 4413 | } break; |
| 4414 | case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: { |
| 4415 | op = WMSZ_BOTTOMRIGHT; |
| 4416 | } break; |
| 4417 | default: |
| 4418 | break; |
| 4419 | } |
| 4420 | |
| 4421 | SendMessage(wd.hWnd, WM_SYSCOMMAND, SC_SIZE | op, MAKELPARAM(coords.x, coords.y)); |
| 4422 | |
| 4423 | for (int btn = (int)MouseButton::LEFT; btn <= (int)MouseButton::MB_XBUTTON2; btn++) { |
| 4424 | if (Input::get_singleton()->is_mouse_button_pressed(MouseButton(btn))) { |
| 4425 | Ref<InputEventMouseButton> mb; |
| 4426 | mb.instantiate(); |
| 4427 | mb->set_window_id(p_window); |
| 4428 | mb->set_pressed(false); |
| 4429 | mb->set_button_index(MouseButton::LEFT); |
| 4430 | mb->set_position(Vector2(coords.x, coords.y)); |
| 4431 | mb->set_global_position(mb->get_position()); |
| 4432 | Input::get_singleton()->parse_input_event(mb); |
| 4433 | } |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | void DisplayServerWindows::set_context(Context p_context) { |
| 4438 | } |
| 4439 | |
| 4440 | bool DisplayServerWindows::is_window_transparency_available() const { |
| 4441 | #if defined(RD_ENABLED) |
| 4442 | if (rendering_device && !rendering_device->is_composite_alpha_supported()) { |
| 4443 | return false; |
| 4444 | } |
| 4445 | #endif |
| 4446 | return OS::get_singleton()->is_layered_allowed(); |
| 4447 | } |
| 4448 | |
| 4449 | #define MI_WP_SIGNATURE 0xFF515700 |
| 4450 | #define SIGNATURE_MASK 0xFFFFFF00 |
| 4451 | // Keeping the name suggested by Microsoft, but this macro really answers: |
| 4452 | // Is this mouse event emulated from touch or pen input? |
| 4453 | #define IsPenEvent(dw) (((dw) & SIGNATURE_MASK) == MI_WP_SIGNATURE) |
| 4454 | // This one tells whether the event comes from touchscreen (and not from pen). |
| 4455 | #define IsTouchEvent(dw) (IsPenEvent(dw) && ((dw) & 0x80)) |
| 4456 | |
| 4457 | void DisplayServerWindows::_touch_event(WindowID p_window, bool p_pressed, float p_x, float p_y, int idx) { |
| 4458 | if (touch_state.has(idx) == p_pressed) { |
| 4459 | return; |
| 4460 | } |
| 4461 | |
| 4462 | if (p_pressed) { |
| 4463 | touch_state.insert(idx, Vector2(p_x, p_y)); |
| 4464 | } else { |
| 4465 | touch_state.erase(idx); |
| 4466 | } |
| 4467 | |
| 4468 | Ref<InputEventScreenTouch> event; |
| 4469 | event.instantiate(); |
| 4470 | event->set_index(idx); |
| 4471 | event->set_window_id(p_window); |
| 4472 | event->set_pressed(p_pressed); |
| 4473 | event->set_position(Vector2(p_x, p_y)); |
| 4474 | |
| 4475 | Input::get_singleton()->parse_input_event(event); |
| 4476 | } |
| 4477 | |
| 4478 | void DisplayServerWindows::_drag_event(WindowID p_window, float p_x, float p_y, int idx) { |
| 4479 | RBMap<int, Vector2>::Element *curr = touch_state.find(idx); |
| 4480 | if (!curr) { |
| 4481 | return; |
| 4482 | } |
| 4483 | |
| 4484 | if (curr->get() == Vector2(p_x, p_y)) { |
| 4485 | return; |
| 4486 | } |
| 4487 | |
| 4488 | Ref<InputEventScreenDrag> event; |
| 4489 | event.instantiate(); |
| 4490 | event->set_window_id(p_window); |
| 4491 | event->set_index(idx); |
| 4492 | event->set_position(Vector2(p_x, p_y)); |
| 4493 | event->set_relative(Vector2(p_x, p_y) - curr->get()); |
| 4494 | event->set_relative_screen_position(event->get_relative()); |
| 4495 | |
| 4496 | Input::get_singleton()->parse_input_event(event); |
| 4497 | |
| 4498 | curr->get() = Vector2(p_x, p_y); |
| 4499 | } |
| 4500 | |
| 4501 | void DisplayServerWindows::_send_window_event(const WindowData &wd, WindowEvent p_event) { |
| 4502 | if (wd.event_callback.is_valid()) { |
| 4503 | Variant event = int(p_event); |
| 4504 | wd.event_callback.call(event); |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | void DisplayServerWindows::_dispatch_input_events(const Ref<InputEvent> &p_event) { |
| 4509 | static_cast<DisplayServerWindows *>(get_singleton())->_dispatch_input_event(p_event); |
| 4510 | } |
| 4511 | |
| 4512 | void DisplayServerWindows::_dispatch_input_event(const Ref<InputEvent> &p_event) { |
| 4513 | if (in_dispatch_input_event) { |
| 4514 | return; |
| 4515 | } |
| 4516 | in_dispatch_input_event = true; |
| 4517 | |
| 4518 | { |
| 4519 | List<WindowID>::Element *E = popup_list.back(); |
| 4520 | if (E && Object::cast_to<InputEventKey>(*p_event)) { |
| 4521 | // Redirect keyboard input to active popup. |
| 4522 | if (windows.has(E->get())) { |
| 4523 | Callable callable = windows[E->get()].input_event_callback; |
| 4524 | if (callable.is_valid()) { |
| 4525 | callable.call(p_event); |
| 4526 | } |
| 4527 | } |
| 4528 | in_dispatch_input_event = false; |
| 4529 | return; |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | Ref<InputEventFromWindow> event_from_window = p_event; |
| 4534 | if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) { |
| 4535 | // Send to a single window. |
| 4536 | if (windows.has(event_from_window->get_window_id())) { |
| 4537 | Callable callable = windows[event_from_window->get_window_id()].input_event_callback; |
| 4538 | if (callable.is_valid()) { |
| 4539 | callable.call(p_event); |
| 4540 | } |
| 4541 | } |
| 4542 | } else { |
| 4543 | // Send to all windows. Copy all pending callbacks, since callback can erase window. |
| 4544 | Vector<Callable> cbs; |
| 4545 | for (KeyValue<WindowID, WindowData> &E : windows) { |
| 4546 | Callable callable = E.value.input_event_callback; |
| 4547 | if (callable.is_valid()) { |
| 4548 | cbs.push_back(callable); |
| 4549 | } |
| 4550 | } |
| 4551 | for (const Callable &cb : cbs) { |
| 4552 | cb.call(p_event); |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | in_dispatch_input_event = false; |
| 4557 | } |
| 4558 | |
| 4559 | LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) { |
| 4560 | DisplayServerWindows *ds_win = static_cast<DisplayServerWindows *>(DisplayServer::get_singleton()); |
| 4561 | if (ds_win) { |
| 4562 | return ds_win->MouseProc(code, wParam, lParam); |
| 4563 | } else { |
| 4564 | return ::CallNextHookEx(nullptr, code, wParam, lParam); |
| 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | DisplayServer::WindowID DisplayServerWindows::window_get_active_popup() const { |
| 4569 | const List<WindowID>::Element *E = popup_list.back(); |
| 4570 | if (E) { |
| 4571 | return E->get(); |
| 4572 | } else { |
| 4573 | return INVALID_WINDOW_ID; |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | void DisplayServerWindows::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) { |
| 4578 | _THREAD_SAFE_METHOD_ |
| 4579 | |
| 4580 | ERR_FAIL_COND(!windows.has(p_window)); |
| 4581 | WindowData &wd = windows[p_window]; |
| 4582 | wd.parent_safe_rect = p_rect; |
| 4583 | } |
| 4584 | |
| 4585 | Rect2i DisplayServerWindows::window_get_popup_safe_rect(WindowID p_window) const { |
| 4586 | _THREAD_SAFE_METHOD_ |
| 4587 | |
| 4588 | ERR_FAIL_COND_V(!windows.has(p_window), Rect2i()); |
| 4589 | const WindowData &wd = windows[p_window]; |
| 4590 | return wd.parent_safe_rect; |
| 4591 | } |
| 4592 | |
| 4593 | void DisplayServerWindows::popup_open(WindowID p_window) { |
| 4594 | _THREAD_SAFE_METHOD_ |
| 4595 | |
| 4596 | bool has_popup_ancestor = false; |
| 4597 | WindowID transient_root = p_window; |
| 4598 | while (true) { |
| 4599 | WindowID parent = windows[transient_root].transient_parent; |
| 4600 | if (parent == INVALID_WINDOW_ID) { |
| 4601 | break; |
| 4602 | } else { |
| 4603 | transient_root = parent; |
| 4604 | if (windows[parent].is_popup) { |
| 4605 | has_popup_ancestor = true; |
| 4606 | break; |
| 4607 | } |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | // Detect tooltips and other similar popups that shouldn't block input to their parent. |
| 4612 | bool ignores_input = window_get_flag(WINDOW_FLAG_NO_FOCUS, p_window) && window_get_flag(WINDOW_FLAG_MOUSE_PASSTHROUGH, p_window); |
| 4613 | |
| 4614 | WindowData &wd = windows[p_window]; |
| 4615 | if (wd.is_popup || (has_popup_ancestor && !ignores_input)) { |
| 4616 | // Find current popup parent, or root popup if new window is not transient. |
| 4617 | List<WindowID>::Element *C = nullptr; |
| 4618 | List<WindowID>::Element *E = popup_list.back(); |
| 4619 | while (E) { |
| 4620 | if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) { |
| 4621 | C = E; |
| 4622 | E = E->prev(); |
| 4623 | } else { |
| 4624 | break; |
| 4625 | } |
| 4626 | } |
| 4627 | if (C) { |
| 4628 | _send_window_event(windows[C->get()], DisplayServerWindows::WINDOW_EVENT_CLOSE_REQUEST); |
| 4629 | } |
| 4630 | |
| 4631 | time_since_popup = OS::get_singleton()->get_ticks_msec(); |
| 4632 | popup_list.push_back(p_window); |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | void DisplayServerWindows::popup_close(WindowID p_window) { |
| 4637 | _THREAD_SAFE_METHOD_ |
| 4638 | |
| 4639 | List<WindowID>::Element *E = popup_list.find(p_window); |
| 4640 | while (E) { |
| 4641 | List<WindowID>::Element *F = E->next(); |
| 4642 | WindowID win_id = E->get(); |
| 4643 | popup_list.erase(E); |
| 4644 | |
| 4645 | if (win_id != p_window) { |
| 4646 | // Only request close on related windows, not this window. We are already processing it. |
| 4647 | _send_window_event(windows[win_id], DisplayServerWindows::WINDOW_EVENT_CLOSE_REQUEST); |
| 4648 | } |
| 4649 | E = F; |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | BitField<DisplayServerWindows::WinKeyModifierMask> DisplayServerWindows::_get_mods() const { |
| 4654 | BitField<WinKeyModifierMask> mask = {}; |
| 4655 | static unsigned char keyboard_state[256]; |
| 4656 | if (GetKeyboardState((PBYTE)&keyboard_state)) { |
| 4657 | if ((keyboard_state[VK_LSHIFT] & 0x80) || (keyboard_state[VK_RSHIFT] & 0x80)) { |
| 4658 | mask.set_flag(WinKeyModifierMask::SHIFT); |
| 4659 | } |
| 4660 | if ((keyboard_state[VK_LCONTROL] & 0x80) || (keyboard_state[VK_RCONTROL] & 0x80)) { |
| 4661 | mask.set_flag(WinKeyModifierMask::CTRL); |
| 4662 | } |
| 4663 | if ((keyboard_state[VK_LMENU] & 0x80) || (keyboard_state[VK_RMENU] & 0x80)) { |
| 4664 | mask.set_flag(WinKeyModifierMask::ALT); |
| 4665 | } |
| 4666 | if ((keyboard_state[VK_RMENU] & 0x80)) { |
| 4667 | mask.set_flag(WinKeyModifierMask::ALT_GR); |
| 4668 | } |
| 4669 | if ((keyboard_state[VK_LWIN] & 0x80) || (keyboard_state[VK_RWIN] & 0x80)) { |
| 4670 | mask.set_flag(WinKeyModifierMask::META); |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | return mask; |
| 4675 | } |
| 4676 | |
| 4677 | LRESULT DisplayServerWindows::MouseProc(int code, WPARAM wParam, LPARAM lParam) { |
| 4678 | _THREAD_SAFE_METHOD_ |
| 4679 | |
| 4680 | uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup; |
| 4681 | if (delta > 250) { |
| 4682 | switch (wParam) { |
| 4683 | case WM_NCLBUTTONDOWN: |
| 4684 | case WM_NCRBUTTONDOWN: |
| 4685 | case WM_NCMBUTTONDOWN: |
| 4686 | case WM_LBUTTONDOWN: |
| 4687 | case WM_RBUTTONDOWN: |
| 4688 | case WM_MBUTTONDOWN: { |
| 4689 | MOUSEHOOKSTRUCT *ms = (MOUSEHOOKSTRUCT *)lParam; |
| 4690 | Point2i pos = Point2i(ms->pt.x, ms->pt.y) - _get_screens_origin(); |
| 4691 | List<WindowID>::Element *C = nullptr; |
| 4692 | List<WindowID>::Element *E = popup_list.back(); |
| 4693 | // Find top popup to close. |
| 4694 | while (E) { |
| 4695 | // Popup window area. |
| 4696 | Rect2i win_rect = Rect2i(window_get_position_with_decorations(E->get()), window_get_size_with_decorations(E->get())); |
| 4697 | // Area of the parent window, which responsible for opening sub-menu. |
| 4698 | Rect2i safe_rect = window_get_popup_safe_rect(E->get()); |
| 4699 | if (win_rect.has_point(pos)) { |
| 4700 | break; |
| 4701 | } else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) { |
| 4702 | break; |
| 4703 | } else { |
| 4704 | C = E; |
| 4705 | E = E->prev(); |
| 4706 | } |
| 4707 | } |
| 4708 | if (C) { |
| 4709 | _send_window_event(windows[C->get()], DisplayServerWindows::WINDOW_EVENT_CLOSE_REQUEST); |
| 4710 | return 1; |
| 4711 | } |
| 4712 | } break; |
| 4713 | } |
| 4714 | } |
| 4715 | return ::CallNextHookEx(mouse_monitor, code, wParam, lParam); |
| 4716 | } |
| 4717 | |
| 4718 | // Handle a single window message received while CreateWindowEx is still on the stack and our data |
| 4719 | // structures are not fully initialized. |
| 4720 | LRESULT DisplayServerWindows::_handle_early_window_message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 4721 | switch (uMsg) { |
| 4722 | case WM_GETMINMAXINFO: { |
| 4723 | // We receive this during CreateWindowEx and we haven't initialized the window |
| 4724 | // struct, so let Windows figure out the maximized size. |
| 4725 | // Silently forward to user/default. |
| 4726 | } break; |
| 4727 | case WM_NCCREATE: { |
| 4728 | // We tunnel an unowned pointer to our window context (WindowData) through the |
| 4729 | // first possible message (WM_NCCREATE) to fix up our window context collection. |
| 4730 | CREATESTRUCTW *pCreate = (CREATESTRUCTW *)lParam; |
| 4731 | WindowData *pWindowData = reinterpret_cast<WindowData *>(pCreate->lpCreateParams); |
| 4732 | |
| 4733 | // Fix this up so we can recognize the remaining messages. |
| 4734 | pWindowData->hWnd = hWnd; |
| 4735 | |
| 4736 | #ifdef ACCESSKIT_ENABLED |
| 4737 | if (accessibility_driver && !accessibility_driver->window_create(pWindowData->id, (void *)hWnd)) { |
| 4738 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 4739 | ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!"); |
| 4740 | } |
| 4741 | memdelete(accessibility_driver); |
| 4742 | accessibility_driver = nullptr; |
| 4743 | } |
| 4744 | #endif |
| 4745 | } break; |
| 4746 | default: { |
| 4747 | // Additional messages during window creation should happen after we fixed |
| 4748 | // up the data structures on WM_NCCREATE, but this might change in the future, |
| 4749 | // so report an error here and then we can implement them. |
| 4750 | ERR_PRINT_ONCE(vformat("Unexpected window message 0x%x received for window we cannot recognize in our collection; sequence error.", uMsg)); |
| 4751 | } break; |
| 4752 | } |
| 4753 | |
| 4754 | if (user_proc) { |
| 4755 | return CallWindowProcW(user_proc, hWnd, uMsg, wParam, lParam); |
| 4756 | } |
| 4757 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 4758 | } |
| 4759 | |
| 4760 | // The window procedure for our window class "Engine", used to handle processing of window-related system messages/events. |
| 4761 | // See: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-procedures |
| 4762 | LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 4763 | if (drop_events) { |
| 4764 | if (user_proc) { |
| 4765 | return CallWindowProcW(user_proc, hWnd, uMsg, wParam, lParam); |
| 4766 | } else { |
| 4767 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | WindowID window_id = INVALID_WINDOW_ID; |
| 4772 | bool window_created = false; |
| 4773 | |
| 4774 | // Check whether window exists |
| 4775 | // FIXME this is O(n), where n is the set of currently open windows and subwindows |
| 4776 | // we should have a secondary map from HWND to WindowID or even WindowData* alias, if we want to eliminate all the map lookups below |
| 4777 | for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 4778 | if (E.value.hWnd == hWnd) { |
| 4779 | window_id = E.key; |
| 4780 | window_created = true; |
| 4781 | break; |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | // WARNING: We get called with events before the window is registered in our collection |
| 4786 | // specifically, even the call to CreateWindowEx already calls here while still on the stack, |
| 4787 | // so there is no way to store the window handle in our collection before we get here. |
| 4788 | if (!window_created) { |
| 4789 | // don't let code below operate on incompletely initialized window objects or missing window_id |
| 4790 | return _handle_early_window_message(hWnd, uMsg, wParam, lParam); |
| 4791 | } |
| 4792 | |
| 4793 | // Process window messages. |
| 4794 | switch (uMsg) { |
| 4795 | case WM_GETOBJECT: { |
| 4796 | get_object_received = true; |
| 4797 | } break; |
| 4798 | case WM_MENUCOMMAND: { |
| 4799 | native_menu->_menu_activate(HMENU(lParam), (int)wParam); |
| 4800 | } break; |
| 4801 | case WM_CREATE: { |
| 4802 | { |
| 4803 | DWORD value = windows[window_id].sharp_corners ? DWMWCP_DONOTROUND : DWMWCP_DEFAULT; |
| 4804 | ::DwmSetWindowAttribute(windows[window_id].hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, &value, sizeof(value)); |
| 4805 | } |
| 4806 | if (is_dark_mode_supported() && dark_title_available) { |
| 4807 | BOOL value = is_dark_mode(); |
| 4808 | |
| 4809 | ::DwmSetWindowAttribute(windows[window_id].hWnd, use_legacy_dark_mode_before_20H1 ? DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 : DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value)); |
| 4810 | SendMessageW(windows[window_id].hWnd, WM_PAINT, 0, 0); |
| 4811 | } |
| 4812 | } break; |
| 4813 | case WM_NCHITTEST: { |
| 4814 | if (windows[window_id].mpass) { |
| 4815 | return HTTRANSPARENT; |
| 4816 | } |
| 4817 | } break; |
| 4818 | case WM_MOUSEACTIVATE: { |
| 4819 | if (windows[window_id].no_focus || windows[window_id].is_popup) { |
| 4820 | return MA_NOACTIVATE; // Do not activate, but process mouse messages. |
| 4821 | } |
| 4822 | // When embedded, the window is a child of the parent and is not activated |
| 4823 | // by default because it lacks native controls. |
| 4824 | if (windows[window_id].parent_hwnd) { |
| 4825 | SetFocus(windows[window_id].hWnd); |
| 4826 | return MA_ACTIVATE; |
| 4827 | } |
| 4828 | } break; |
| 4829 | case WM_ACTIVATEAPP: { |
| 4830 | bool new_app_focused = (bool)wParam; |
| 4831 | if (new_app_focused == app_focused) { |
| 4832 | break; |
| 4833 | } |
| 4834 | app_focused = new_app_focused; |
| 4835 | if (OS::get_singleton()->get_main_loop()) { |
| 4836 | OS::get_singleton()->get_main_loop()->notification(app_focused ? MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN : MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT); |
| 4837 | } |
| 4838 | } break; |
| 4839 | case WM_ACTIVATE: { |
| 4840 | // Activation can happen just after the window has been created, even before the callbacks are set. |
| 4841 | // Therefore, it's safer to defer the delivery of the event. |
| 4842 | // It's important to set an nIDEvent different from the SetTimer for move_timer_id because |
| 4843 | // if the same nIDEvent is passed, the timer is replaced and the same timer_id is returned. |
| 4844 | // The problem with the timer is that the window cannot be resized or the buttons cannot be used correctly |
| 4845 | // if the window is not activated first. This happens because the code in the activation process runs |
| 4846 | // after the mouse click is handled. To address this, the timer is now used only during the window creation, |
| 4847 | // and only as part of the activation process. We don't want 'Input::release_pressed_events()' |
| 4848 | // to be called immediately in '_process_activate_event' when the window is not yet activated, |
| 4849 | // as it would reset the currently pressed keys when hiding a window, which is incorrect behavior. |
| 4850 | windows[window_id].activate_state = GET_WM_ACTIVATE_STATE(wParam, lParam); |
| 4851 | if (windows[window_id].first_activation_done && (windows[window_id].activate_state == WA_ACTIVE || windows[window_id].activate_state == WA_CLICKACTIVE)) { |
| 4852 | _process_activate_event(window_id); |
| 4853 | } else { |
| 4854 | windows[window_id].activate_timer_id = SetTimer(windows[window_id].hWnd, DisplayServerWindows::TIMER_ID_WINDOW_ACTIVATION, USER_TIMER_MINIMUM, (TIMERPROC) nullptr); |
| 4855 | } |
| 4856 | return 0; |
| 4857 | } break; |
| 4858 | case WM_GETMINMAXINFO: { |
| 4859 | if (windows[window_id].resizable && !windows[window_id].fullscreen) { |
| 4860 | // Size of window decorations. |
| 4861 | Size2 decor = window_get_size_with_decorations(window_id) - window_get_size(window_id); |
| 4862 | |
| 4863 | MINMAXINFO *min_max_info = (MINMAXINFO *)lParam; |
| 4864 | if (windows[window_id].min_size != Size2()) { |
| 4865 | min_max_info->ptMinTrackSize.x = windows[window_id].min_size.x + decor.x; |
| 4866 | min_max_info->ptMinTrackSize.y = windows[window_id].min_size.y + decor.y; |
| 4867 | } |
| 4868 | if (windows[window_id].max_size != Size2()) { |
| 4869 | min_max_info->ptMaxTrackSize.x = windows[window_id].max_size.x + decor.x; |
| 4870 | min_max_info->ptMaxTrackSize.y = windows[window_id].max_size.y + decor.y; |
| 4871 | } |
| 4872 | if (windows[window_id].borderless) { |
| 4873 | Rect2i screen_rect = screen_get_usable_rect(window_get_current_screen(window_id)); |
| 4874 | |
| 4875 | // Set the size of (borderless) maximized mode to exclude taskbar (or any other panel) if present. |
| 4876 | min_max_info->ptMaxPosition.x = screen_rect.position.x; |
| 4877 | min_max_info->ptMaxPosition.y = screen_rect.position.y; |
| 4878 | min_max_info->ptMaxSize.x = screen_rect.size.x; |
| 4879 | min_max_info->ptMaxSize.y = screen_rect.size.y; |
| 4880 | } |
| 4881 | return 0; |
| 4882 | } |
| 4883 | } break; |
| 4884 | case WM_ERASEBKGND: { |
| 4885 | Color early_color; |
| 4886 | if (!_get_window_early_clear_override(early_color)) { |
| 4887 | break; |
| 4888 | } |
| 4889 | bool must_recreate_brush = !window_bkg_brush || window_bkg_brush_color != early_color.to_argb32(); |
| 4890 | if (must_recreate_brush) { |
| 4891 | if (window_bkg_brush) { |
| 4892 | DeleteObject(window_bkg_brush); |
| 4893 | } |
| 4894 | window_bkg_brush = CreateSolidBrush(RGB(early_color.get_r8(), early_color.get_g8(), early_color.get_b8())); |
| 4895 | } |
| 4896 | HDC hdc = (HDC)wParam; |
| 4897 | RECT rect = {}; |
| 4898 | if (GetUpdateRect(hWnd, &rect, true)) { |
| 4899 | FillRect(hdc, &rect, window_bkg_brush); |
| 4900 | } |
| 4901 | return 1; |
| 4902 | } break; |
| 4903 | case WM_PAINT: { |
| 4904 | Main::force_redraw(); |
| 4905 | } break; |
| 4906 | case WM_SETTINGCHANGE: |
| 4907 | case WM_SYSCOLORCHANGE: { |
| 4908 | if (lParam && CompareStringOrdinal(reinterpret_cast<LPCWCH>(lParam), -1, L"ImmersiveColorSet", -1, true) == CSTR_EQUAL) { |
| 4909 | if (is_dark_mode_supported() && dark_title_available) { |
| 4910 | BOOL value = is_dark_mode(); |
| 4911 | ::DwmSetWindowAttribute(windows[window_id].hWnd, use_legacy_dark_mode_before_20H1 ? DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 : DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value)); |
| 4912 | } |
| 4913 | } |
| 4914 | if (system_theme_changed.is_valid()) { |
| 4915 | Variant ret; |
| 4916 | Callable::CallError ce; |
| 4917 | system_theme_changed.callp(nullptr, 0, ret, ce); |
| 4918 | if (ce.error != Callable::CallError::CALL_OK) { |
| 4919 | ERR_PRINT(vformat("Failed to execute system theme changed callback: %s.", Variant::get_callable_error_text(system_theme_changed, nullptr, 0, ce))); |
| 4920 | } |
| 4921 | } |
| 4922 | } break; |
| 4923 | case WM_THEMECHANGED: { |
| 4924 | if (is_dark_mode_supported() && dark_title_available) { |
| 4925 | BOOL value = is_dark_mode(); |
| 4926 | ::DwmSetWindowAttribute(windows[window_id].hWnd, use_legacy_dark_mode_before_20H1 ? DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 : DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value)); |
| 4927 | } |
| 4928 | } break; |
| 4929 | case WM_SYSCOMMAND: // Intercept system commands. |
| 4930 | { |
| 4931 | switch (wParam) // Check system calls. |
| 4932 | { |
| 4933 | case SC_SCREENSAVE: // Screensaver trying to start? |
| 4934 | case SC_MONITORPOWER: // Monitor trying to enter powersave? |
| 4935 | return 0; // Prevent from happening. |
| 4936 | case SC_KEYMENU: |
| 4937 | Engine *engine = Engine::get_singleton(); |
| 4938 | if (((lParam >> 16) <= 0) && !engine->is_project_manager_hint() && !engine->is_editor_hint() && !GLOBAL_GET_CACHED(bool, "application/run/enable_alt_space_menu")) { |
| 4939 | return 0; |
| 4940 | } |
| 4941 | if (!_get_mods().has_flag(WinKeyModifierMask::ALT) || !(GetAsyncKeyState(VK_SPACE) & (1 << 15))) { |
| 4942 | return 0; |
| 4943 | } |
| 4944 | SendMessage(windows[window_id].hWnd, WM_SYSKEYUP, VK_SPACE, 0); |
| 4945 | SendMessage(windows[window_id].hWnd, WM_SYSKEYUP, VK_MENU, 0); |
| 4946 | } |
| 4947 | } break; |
| 4948 | case WM_INDICATOR_CALLBACK_MESSAGE: { |
| 4949 | if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN || lParam == WM_MBUTTONDOWN || lParam == WM_XBUTTONDOWN) { |
| 4950 | IndicatorID iid = (IndicatorID)wParam; |
| 4951 | MouseButton mb = MouseButton::LEFT; |
| 4952 | if (lParam == WM_RBUTTONDOWN) { |
| 4953 | mb = MouseButton::RIGHT; |
| 4954 | } else if (lParam == WM_MBUTTONDOWN) { |
| 4955 | mb = MouseButton::MIDDLE; |
| 4956 | } else if (lParam == WM_XBUTTONDOWN) { |
| 4957 | mb = MouseButton::MB_XBUTTON1; |
| 4958 | } |
| 4959 | if (indicators.has(iid)) { |
| 4960 | if (lParam == WM_RBUTTONDOWN && indicators[iid].menu_rid.is_valid() && native_menu->has_menu(indicators[iid].menu_rid)) { |
| 4961 | NOTIFYICONIDENTIFIER nid; |
| 4962 | ZeroMemory(&nid, sizeof(NOTIFYICONIDENTIFIER)); |
| 4963 | nid.cbSize = sizeof(NOTIFYICONIDENTIFIER); |
| 4964 | nid.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 4965 | nid.uID = iid; |
| 4966 | nid.guidItem = GUID_NULL; |
| 4967 | |
| 4968 | RECT rect; |
| 4969 | if (Shell_NotifyIconGetRect(&nid, &rect) == S_OK) { |
| 4970 | native_menu->popup(indicators[iid].menu_rid, Vector2i((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2)); |
| 4971 | } |
| 4972 | } else if (indicators[iid].callback.is_valid()) { |
| 4973 | Variant v_button = mb; |
| 4974 | Variant v_pos = mouse_get_position(); |
| 4975 | const Variant *v_args[2] = { &v_button, &v_pos }; |
| 4976 | Variant ret; |
| 4977 | Callable::CallError ce; |
| 4978 | indicators[iid].callback.callp((const Variant **)&v_args, 2, ret, ce); |
| 4979 | if (ce.error != Callable::CallError::CALL_OK) { |
| 4980 | ERR_PRINT(vformat("Failed to execute status indicator callback: %s.", Variant::get_callable_error_text(indicators[iid].callback, v_args, 2, ce))); |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | return 0; |
| 4985 | } |
| 4986 | } break; |
| 4987 | case WM_CLOSE: { |
| 4988 | if (windows[window_id].activate_timer_id) { |
| 4989 | KillTimer(windows[window_id].hWnd, windows[window_id].activate_timer_id); |
| 4990 | windows[window_id].activate_timer_id = 0; |
| 4991 | } |
| 4992 | _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST); |
| 4993 | return 0; |
| 4994 | } |
| 4995 | case WM_MOUSELEAVE: { |
| 4996 | if (window_mouseover_id == window_id) { |
| 4997 | old_invalid = true; |
| 4998 | window_mouseover_id = INVALID_WINDOW_ID; |
| 4999 | |
| 5000 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT); |
| 5001 | } else if (window_mouseover_id != INVALID_WINDOW_ID && windows.has(window_mouseover_id)) { |
| 5002 | // This is reached during drag and drop, after dropping in a different window. |
| 5003 | // Once-off notification, must call again. |
| 5004 | track_mouse_leave_event(windows[window_mouseover_id].hWnd); |
| 5005 | } |
| 5006 | |
| 5007 | } break; |
| 5008 | case WM_INPUT: { |
| 5009 | if (!use_raw_input) { |
| 5010 | break; |
| 5011 | } |
| 5012 | |
| 5013 | UINT dwSize; |
| 5014 | |
| 5015 | GetRawInputData((HRAWINPUT)lParam, RID_INPUT, nullptr, &dwSize, sizeof(RAWINPUTHEADER)); |
| 5016 | LPBYTE lpb = new BYTE[dwSize]; |
| 5017 | if (lpb == nullptr) { |
| 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize) { |
| 5022 | OutputDebugString(TEXT("GetRawInputData does not return correct size !\n")); |
| 5023 | } |
| 5024 | |
| 5025 | RAWINPUT *raw = (RAWINPUT *)lpb; |
| 5026 | |
| 5027 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5028 | if (raw->header.dwType == RIM_TYPEKEYBOARD) { |
| 5029 | if (raw->data.keyboard.VKey == VK_SHIFT) { |
| 5030 | // If multiple Shifts are held down at the same time, |
| 5031 | // Windows natively only sends a KEYUP for the last one to be released. |
| 5032 | if (raw->data.keyboard.Flags & RI_KEY_BREAK) { |
| 5033 | // Make sure to check the latest key state since |
| 5034 | // we're in the middle of the message queue. |
| 5035 | if (GetAsyncKeyState(VK_SHIFT) < 0) { |
| 5036 | // A Shift is released, but another Shift is still held |
| 5037 | ERR_BREAK(key_event_pos >= KEY_EVENT_BUFFER_SIZE); |
| 5038 | |
| 5039 | KeyEvent ke; |
| 5040 | ke.shift = false; |
| 5041 | ke.altgr = mods.has_flag(WinKeyModifierMask::ALT_GR); |
| 5042 | ke.alt = mods.has_flag(WinKeyModifierMask::ALT); |
| 5043 | ke.control = mods.has_flag(WinKeyModifierMask::CTRL); |
| 5044 | ke.meta = mods.has_flag(WinKeyModifierMask::META); |
| 5045 | ke.uMsg = WM_KEYUP; |
| 5046 | ke.window_id = window_id; |
| 5047 | |
| 5048 | ke.wParam = VK_SHIFT; |
| 5049 | // data.keyboard.MakeCode -> 0x2A - left shift, 0x36 - right shift. |
| 5050 | // Bit 30 -> key was previously down, bit 31 -> key is being released. |
| 5051 | ke.lParam = raw->data.keyboard.MakeCode << 16 | 1 << 30 | 1 << 31; |
| 5052 | key_event_buffer[key_event_pos++] = ke; |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | } else if (mouse_mode == MOUSE_MODE_CAPTURED && raw->header.dwType == RIM_TYPEMOUSE) { |
| 5057 | Ref<InputEventMouseMotion> mm; |
| 5058 | mm.instantiate(); |
| 5059 | |
| 5060 | mm->set_window_id(window_id); |
| 5061 | mm->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5062 | mm->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5063 | mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5064 | mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5065 | |
| 5066 | mm->set_pressure((raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) ? 1.0f : 0.0f); |
| 5067 | |
| 5068 | mm->set_button_mask(mouse_get_button_state()); |
| 5069 | |
| 5070 | Point2i c(windows[window_id].width / 2, windows[window_id].height / 2); |
| 5071 | |
| 5072 | // Centering just so it works as before. |
| 5073 | POINT pos = { (int)c.x, (int)c.y }; |
| 5074 | ClientToScreen(windows[window_id].hWnd, &pos); |
| 5075 | SetCursorPos(pos.x, pos.y); |
| 5076 | |
| 5077 | mm->set_position(c); |
| 5078 | mm->set_global_position(c); |
| 5079 | mm->set_velocity(Vector2(0, 0)); |
| 5080 | mm->set_screen_velocity(Vector2(0, 0)); |
| 5081 | |
| 5082 | if (raw->data.mouse.usFlags == MOUSE_MOVE_RELATIVE) { |
| 5083 | mm->set_relative(Vector2(raw->data.mouse.lLastX, raw->data.mouse.lLastY)); |
| 5084 | |
| 5085 | } else if (raw->data.mouse.usFlags == MOUSE_MOVE_ABSOLUTE) { |
| 5086 | int nScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN); |
| 5087 | int nScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN); |
| 5088 | int nScreenLeft = GetSystemMetrics(SM_XVIRTUALSCREEN); |
| 5089 | int nScreenTop = GetSystemMetrics(SM_YVIRTUALSCREEN); |
| 5090 | |
| 5091 | Vector2 abs_pos( |
| 5092 | (double(raw->data.mouse.lLastX) - 65536.0 / (nScreenWidth)) * nScreenWidth / 65536.0 + nScreenLeft, |
| 5093 | (double(raw->data.mouse.lLastY) - 65536.0 / (nScreenHeight)) * nScreenHeight / 65536.0 + nScreenTop); |
| 5094 | |
| 5095 | POINT coords; // Client coords. |
| 5096 | coords.x = abs_pos.x; |
| 5097 | coords.y = abs_pos.y; |
| 5098 | |
| 5099 | ScreenToClient(hWnd, &coords); |
| 5100 | |
| 5101 | mm->set_relative(Vector2(coords.x - old_x, coords.y - old_y)); |
| 5102 | old_x = coords.x; |
| 5103 | old_y = coords.y; |
| 5104 | } |
| 5105 | mm->set_relative_screen_position(mm->get_relative()); |
| 5106 | |
| 5107 | if ((windows[window_id].window_focused || windows[window_id].is_popup) && mm->get_relative() != Vector2()) { |
| 5108 | Input::get_singleton()->parse_input_event(mm); |
| 5109 | } |
| 5110 | } |
| 5111 | delete[] lpb; |
| 5112 | } break; |
| 5113 | case WT_CSRCHANGE: |
| 5114 | case WT_PROXIMITY: { |
| 5115 | if ((tablet_get_current_driver() == "wintab") && wintab_available && windows[window_id].wtctx) { |
| 5116 | AXIS pressure; |
| 5117 | if (wintab_WTInfo(WTI_DEVICES + windows[window_id].wtlc.lcDevice, DVC_NPRESSURE, &pressure)) { |
| 5118 | windows[window_id].min_pressure = int(pressure.axMin); |
| 5119 | windows[window_id].max_pressure = int(pressure.axMax); |
| 5120 | } |
| 5121 | AXIS orientation[3]; |
| 5122 | if (wintab_WTInfo(WTI_DEVICES + windows[window_id].wtlc.lcDevice, DVC_ORIENTATION, &orientation)) { |
| 5123 | windows[window_id].tilt_supported = orientation[0].axResolution && orientation[1].axResolution; |
| 5124 | } |
| 5125 | return 0; |
| 5126 | } |
| 5127 | } break; |
| 5128 | case WT_PACKET: { |
| 5129 | if ((tablet_get_current_driver() == "wintab") && wintab_available && windows[window_id].wtctx) { |
| 5130 | PACKET packet; |
| 5131 | if (wintab_WTPacket(windows[window_id].wtctx, wParam, &packet)) { |
| 5132 | POINT coords; |
| 5133 | GetCursorPos(&coords); |
| 5134 | ScreenToClient(windows[window_id].hWnd, &coords); |
| 5135 | |
| 5136 | windows[window_id].last_pressure_update = 0; |
| 5137 | |
| 5138 | float pressure = float(packet.pkNormalPressure - windows[window_id].min_pressure) / float(windows[window_id].max_pressure - windows[window_id].min_pressure); |
| 5139 | double azim = (packet.pkOrientation.orAzimuth / 10.0f) * (Math::PI / 180); |
| 5140 | double alt = Math::tan((Math::abs(packet.pkOrientation.orAltitude / 10.0f)) * (Math::PI / 180)); |
| 5141 | bool inverted = packet.pkStatus & TPS_INVERT; |
| 5142 | |
| 5143 | Vector2 tilt = (windows[window_id].tilt_supported) ? Vector2(Math::atan(Math::sin(azim) / alt), Math::atan(Math::cos(azim) / alt)) : Vector2(); |
| 5144 | |
| 5145 | // Nothing changed, ignore event. |
| 5146 | if (!old_invalid && coords.x == old_x && coords.y == old_y && windows[window_id].last_pressure == pressure && windows[window_id].last_tilt == tilt && windows[window_id].last_pen_inverted == inverted) { |
| 5147 | break; |
| 5148 | } |
| 5149 | |
| 5150 | windows[window_id].last_pressure = pressure; |
| 5151 | windows[window_id].last_tilt = tilt; |
| 5152 | windows[window_id].last_pen_inverted = inverted; |
| 5153 | |
| 5154 | // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. |
| 5155 | if (!windows[window_id].window_focused && mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5156 | break; |
| 5157 | } |
| 5158 | |
| 5159 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5160 | Ref<InputEventMouseMotion> mm; |
| 5161 | mm.instantiate(); |
| 5162 | mm->set_window_id(window_id); |
| 5163 | mm->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5164 | mm->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5165 | mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5166 | mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5167 | |
| 5168 | mm->set_pressure(windows[window_id].last_pressure); |
| 5169 | mm->set_tilt(windows[window_id].last_tilt); |
| 5170 | mm->set_pen_inverted(windows[window_id].last_pen_inverted); |
| 5171 | |
| 5172 | mm->set_button_mask(mouse_get_button_state()); |
| 5173 | |
| 5174 | mm->set_position(Vector2(coords.x, coords.y)); |
| 5175 | mm->set_global_position(Vector2(coords.x, coords.y)); |
| 5176 | |
| 5177 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5178 | Point2i c(windows[window_id].width / 2, windows[window_id].height / 2); |
| 5179 | old_x = c.x; |
| 5180 | old_y = c.y; |
| 5181 | |
| 5182 | if (mm->get_position() == c) { |
| 5183 | center = c; |
| 5184 | return 0; |
| 5185 | } |
| 5186 | |
| 5187 | Point2i ncenter = mm->get_position(); |
| 5188 | center = ncenter; |
| 5189 | POINT pos = { (int)c.x, (int)c.y }; |
| 5190 | ClientToScreen(windows[window_id].hWnd, &pos); |
| 5191 | SetCursorPos(pos.x, pos.y); |
| 5192 | } |
| 5193 | |
| 5194 | mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 5195 | mm->set_screen_velocity(mm->get_velocity()); |
| 5196 | |
| 5197 | if (old_invalid) { |
| 5198 | old_x = mm->get_position().x; |
| 5199 | old_y = mm->get_position().y; |
| 5200 | old_invalid = false; |
| 5201 | } |
| 5202 | |
| 5203 | mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y))); |
| 5204 | mm->set_relative_screen_position(mm->get_relative()); |
| 5205 | old_x = mm->get_position().x; |
| 5206 | old_y = mm->get_position().y; |
| 5207 | |
| 5208 | if (windows[window_id].window_focused || window_get_active_popup() == window_id) { |
| 5209 | Input::get_singleton()->parse_input_event(mm); |
| 5210 | } |
| 5211 | } |
| 5212 | return 0; |
| 5213 | } |
| 5214 | } break; |
| 5215 | case WM_POINTERENTER: { |
| 5216 | if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) { |
| 5217 | break; |
| 5218 | } |
| 5219 | |
| 5220 | if (tablet_get_current_driver() != "winink") { |
| 5221 | break; |
| 5222 | } |
| 5223 | |
| 5224 | uint32_t pointer_id = LOWORD(wParam); |
| 5225 | POINTER_INPUT_TYPE pointer_type = PT_POINTER; |
| 5226 | if (!GetPointerType(pointer_id, &pointer_type)) { |
| 5227 | break; |
| 5228 | } |
| 5229 | |
| 5230 | if (pointer_type != PT_PEN) { |
| 5231 | break; |
| 5232 | } |
| 5233 | |
| 5234 | pointer_button[GET_POINTERID_WPARAM(wParam)] = MouseButton::NONE; |
| 5235 | windows[window_id].block_mm = true; |
| 5236 | return 0; |
| 5237 | } break; |
| 5238 | case WM_POINTERLEAVE: { |
| 5239 | pointer_button[GET_POINTERID_WPARAM(wParam)] = MouseButton::NONE; |
| 5240 | windows[window_id].block_mm = false; |
| 5241 | return 0; |
| 5242 | } break; |
| 5243 | case WM_POINTERDOWN: |
| 5244 | case WM_POINTERUP: { |
| 5245 | if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) { |
| 5246 | break; |
| 5247 | } |
| 5248 | |
| 5249 | if (tablet_get_current_driver() != "winink") { |
| 5250 | break; |
| 5251 | } |
| 5252 | |
| 5253 | uint32_t pointer_id = LOWORD(wParam); |
| 5254 | POINTER_INPUT_TYPE pointer_type = PT_POINTER; |
| 5255 | if (!GetPointerType(pointer_id, &pointer_type)) { |
| 5256 | break; |
| 5257 | } |
| 5258 | |
| 5259 | if (pointer_type != PT_PEN) { |
| 5260 | break; |
| 5261 | } |
| 5262 | |
| 5263 | Ref<InputEventMouseButton> mb; |
| 5264 | mb.instantiate(); |
| 5265 | mb->set_window_id(window_id); |
| 5266 | |
| 5267 | BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 5268 | if (IS_POINTER_FIRSTBUTTON_WPARAM(wParam)) { |
| 5269 | last_button_state.set_flag(MouseButtonMask::LEFT); |
| 5270 | mb->set_button_index(MouseButton::LEFT); |
| 5271 | } |
| 5272 | if (IS_POINTER_SECONDBUTTON_WPARAM(wParam)) { |
| 5273 | last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 5274 | mb->set_button_index(MouseButton::RIGHT); |
| 5275 | } |
| 5276 | if (IS_POINTER_THIRDBUTTON_WPARAM(wParam)) { |
| 5277 | last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 5278 | mb->set_button_index(MouseButton::MIDDLE); |
| 5279 | } |
| 5280 | if (IS_POINTER_FOURTHBUTTON_WPARAM(wParam)) { |
| 5281 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 5282 | mb->set_button_index(MouseButton::MB_XBUTTON1); |
| 5283 | } |
| 5284 | if (IS_POINTER_FIFTHBUTTON_WPARAM(wParam)) { |
| 5285 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 5286 | mb->set_button_index(MouseButton::MB_XBUTTON2); |
| 5287 | } |
| 5288 | mb->set_button_mask(last_button_state); |
| 5289 | |
| 5290 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5291 | mb->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5292 | mb->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5293 | mb->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5294 | mb->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5295 | |
| 5296 | POINT coords; // Client coords. |
| 5297 | coords.x = GET_X_LPARAM(lParam); |
| 5298 | coords.y = GET_Y_LPARAM(lParam); |
| 5299 | |
| 5300 | // Note: Handle popup closing here, since mouse event is not emulated and hook will not be called. |
| 5301 | uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup; |
| 5302 | if (delta > 250) { |
| 5303 | Point2i pos = Point2i(coords.x, coords.y) - _get_screens_origin(); |
| 5304 | List<WindowID>::Element *C = nullptr; |
| 5305 | List<WindowID>::Element *E = popup_list.back(); |
| 5306 | // Find top popup to close. |
| 5307 | while (E) { |
| 5308 | // Popup window area. |
| 5309 | Rect2i win_rect = Rect2i(window_get_position_with_decorations(E->get()), window_get_size_with_decorations(E->get())); |
| 5310 | // Area of the parent window, which responsible for opening sub-menu. |
| 5311 | Rect2i safe_rect = window_get_popup_safe_rect(E->get()); |
| 5312 | if (win_rect.has_point(pos)) { |
| 5313 | break; |
| 5314 | } else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) { |
| 5315 | break; |
| 5316 | } else { |
| 5317 | C = E; |
| 5318 | E = E->prev(); |
| 5319 | } |
| 5320 | } |
| 5321 | if (C) { |
| 5322 | _send_window_event(windows[C->get()], DisplayServerWindows::WINDOW_EVENT_CLOSE_REQUEST); |
| 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | int64_t pen_id = GET_POINTERID_WPARAM(wParam); |
| 5327 | if (uMsg == WM_POINTERDOWN) { |
| 5328 | mb->set_pressed(true); |
| 5329 | if (pointer_down_time.has(pen_id) && (pointer_prev_button[pen_id] == mb->get_button_index()) && (Math::abs(coords.y - pointer_last_pos[pen_id].y) < GetSystemMetrics(SM_CYDOUBLECLK)) && GetMessageTime() - pointer_down_time[pen_id] < (LONG)GetDoubleClickTime()) { |
| 5330 | mb->set_double_click(true); |
| 5331 | pointer_down_time[pen_id] = 0; |
| 5332 | } else { |
| 5333 | pointer_down_time[pen_id] = GetMessageTime(); |
| 5334 | pointer_prev_button[pen_id] = mb->get_button_index(); |
| 5335 | pointer_last_pos[pen_id] = Vector2(coords.x, coords.y); |
| 5336 | } |
| 5337 | pointer_button[pen_id] = mb->get_button_index(); |
| 5338 | } else { |
| 5339 | if (!pointer_button.has(pen_id)) { |
| 5340 | return 0; |
| 5341 | } |
| 5342 | mb->set_pressed(false); |
| 5343 | mb->set_button_index(pointer_button[pen_id]); |
| 5344 | pointer_button[pen_id] = MouseButton::NONE; |
| 5345 | } |
| 5346 | |
| 5347 | ScreenToClient(windows[window_id].hWnd, &coords); |
| 5348 | |
| 5349 | mb->set_position(Vector2(coords.x, coords.y)); |
| 5350 | mb->set_global_position(Vector2(coords.x, coords.y)); |
| 5351 | |
| 5352 | Input::get_singleton()->parse_input_event(mb); |
| 5353 | |
| 5354 | return 0; |
| 5355 | } break; |
| 5356 | case WM_POINTERUPDATE: { |
| 5357 | if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) { |
| 5358 | break; |
| 5359 | } |
| 5360 | |
| 5361 | if (tablet_get_current_driver() != "winink") { |
| 5362 | break; |
| 5363 | } |
| 5364 | |
| 5365 | uint32_t pointer_id = LOWORD(wParam); |
| 5366 | POINTER_INPUT_TYPE pointer_type = PT_POINTER; |
| 5367 | if (!GetPointerType(pointer_id, &pointer_type)) { |
| 5368 | break; |
| 5369 | } |
| 5370 | |
| 5371 | if (pointer_type != PT_PEN) { |
| 5372 | break; |
| 5373 | } |
| 5374 | |
| 5375 | POINTER_PEN_INFO pen_info; |
| 5376 | if (!GetPointerPenInfo(pointer_id, &pen_info)) { |
| 5377 | break; |
| 5378 | } |
| 5379 | |
| 5380 | if (Input::get_singleton()->is_emulating_mouse_from_touch()) { |
| 5381 | // Universal translation enabled; ignore OS translation. |
| 5382 | LPARAM extra = GetMessageExtraInfo(); |
| 5383 | if (IsTouchEvent(extra)) { |
| 5384 | break; |
| 5385 | } |
| 5386 | } |
| 5387 | |
| 5388 | if (window_mouseover_id != window_id) { |
| 5389 | // Mouse enter. |
| 5390 | |
| 5391 | if (mouse_mode != MOUSE_MODE_CAPTURED) { |
| 5392 | if (window_mouseover_id != INVALID_WINDOW_ID && windows.has(window_mouseover_id)) { |
| 5393 | // Leave previous window. |
| 5394 | _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); |
| 5395 | } |
| 5396 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER); |
| 5397 | } |
| 5398 | |
| 5399 | CursorShape c = cursor_shape; |
| 5400 | cursor_shape = CURSOR_MAX; |
| 5401 | cursor_set_shape(c); |
| 5402 | window_mouseover_id = window_id; |
| 5403 | |
| 5404 | // Once-off notification, must call again. |
| 5405 | track_mouse_leave_event(hWnd); |
| 5406 | } |
| 5407 | |
| 5408 | // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. |
| 5409 | if (!windows[window_id].window_focused && mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5410 | break; |
| 5411 | } |
| 5412 | |
| 5413 | Ref<InputEventMouseMotion> mm; |
| 5414 | mm.instantiate(); |
| 5415 | |
| 5416 | mm->set_window_id(window_id); |
| 5417 | if (pen_info.penMask & PEN_MASK_PRESSURE) { |
| 5418 | mm->set_pressure((float)pen_info.pressure / 1024); |
| 5419 | } else { |
| 5420 | mm->set_pressure((HIWORD(wParam) & POINTER_MESSAGE_FLAG_FIRSTBUTTON) ? 1.0f : 0.0f); |
| 5421 | } |
| 5422 | if ((pen_info.penMask & PEN_MASK_TILT_X) && (pen_info.penMask & PEN_MASK_TILT_Y)) { |
| 5423 | mm->set_tilt(Vector2((float)pen_info.tiltX / 90, (float)pen_info.tiltY / 90)); |
| 5424 | } |
| 5425 | mm->set_pen_inverted(pen_info.penFlags & (PEN_FLAG_INVERTED | PEN_FLAG_ERASER)); |
| 5426 | |
| 5427 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5428 | mm->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5429 | mm->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5430 | mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5431 | mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5432 | |
| 5433 | BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 5434 | if (IS_POINTER_FIRSTBUTTON_WPARAM(wParam)) { |
| 5435 | last_button_state.set_flag(MouseButtonMask::LEFT); |
| 5436 | } |
| 5437 | if (IS_POINTER_SECONDBUTTON_WPARAM(wParam)) { |
| 5438 | last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 5439 | } |
| 5440 | if (IS_POINTER_THIRDBUTTON_WPARAM(wParam)) { |
| 5441 | last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 5442 | } |
| 5443 | if (IS_POINTER_FOURTHBUTTON_WPARAM(wParam)) { |
| 5444 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 5445 | } |
| 5446 | if (IS_POINTER_FIFTHBUTTON_WPARAM(wParam)) { |
| 5447 | last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 5448 | } |
| 5449 | mm->set_button_mask(last_button_state); |
| 5450 | |
| 5451 | POINT coords; // Client coords. |
| 5452 | coords.x = GET_X_LPARAM(lParam); |
| 5453 | coords.y = GET_Y_LPARAM(lParam); |
| 5454 | |
| 5455 | ScreenToClient(windows[window_id].hWnd, &coords); |
| 5456 | |
| 5457 | mm->set_position(Vector2(coords.x, coords.y)); |
| 5458 | mm->set_global_position(Vector2(coords.x, coords.y)); |
| 5459 | |
| 5460 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5461 | Point2i c(windows[window_id].width / 2, windows[window_id].height / 2); |
| 5462 | old_x = c.x; |
| 5463 | old_y = c.y; |
| 5464 | |
| 5465 | if (mm->get_position() == c) { |
| 5466 | center = c; |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
| 5470 | Point2i ncenter = mm->get_position(); |
| 5471 | center = ncenter; |
| 5472 | POINT pos = { (int)c.x, (int)c.y }; |
| 5473 | ClientToScreen(hWnd, &pos); |
| 5474 | SetCursorPos(pos.x, pos.y); |
| 5475 | } |
| 5476 | |
| 5477 | mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 5478 | mm->set_screen_velocity(mm->get_velocity()); |
| 5479 | |
| 5480 | if (old_invalid) { |
| 5481 | old_x = mm->get_position().x; |
| 5482 | old_y = mm->get_position().y; |
| 5483 | old_invalid = false; |
| 5484 | } |
| 5485 | |
| 5486 | mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y))); |
| 5487 | mm->set_relative_screen_position(mm->get_relative()); |
| 5488 | old_x = mm->get_position().x; |
| 5489 | old_y = mm->get_position().y; |
| 5490 | if (windows[window_id].window_focused || window_get_active_popup() == window_id) { |
| 5491 | Input::get_singleton()->parse_input_event(mm); |
| 5492 | } |
| 5493 | |
| 5494 | return 0; // Pointer event handled return 0 to avoid duplicate WM_MOUSEMOVE event. |
| 5495 | } break; |
| 5496 | case WM_MOUSEMOVE: { |
| 5497 | if (windows[window_id].block_mm) { |
| 5498 | break; |
| 5499 | } |
| 5500 | |
| 5501 | if (mouse_mode == MOUSE_MODE_CAPTURED && use_raw_input) { |
| 5502 | break; |
| 5503 | } |
| 5504 | |
| 5505 | if (Input::get_singleton()->is_emulating_mouse_from_touch()) { |
| 5506 | // Universal translation enabled; ignore OS translation. |
| 5507 | LPARAM extra = GetMessageExtraInfo(); |
| 5508 | if (IsTouchEvent(extra)) { |
| 5509 | break; |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | DisplayServer::WindowID over_id = get_window_at_screen_position(mouse_get_position()); |
| 5514 | if (windows.has(over_id) && !Rect2(window_get_position(over_id), Point2(windows[over_id].width, windows[over_id].height)).has_point(mouse_get_position())) { |
| 5515 | // Don't consider the windowborder as part of the window. |
| 5516 | over_id = INVALID_WINDOW_ID; |
| 5517 | } |
| 5518 | if (window_mouseover_id != over_id) { |
| 5519 | // Mouse enter. |
| 5520 | |
| 5521 | if (mouse_mode != MOUSE_MODE_CAPTURED) { |
| 5522 | if (window_mouseover_id != INVALID_WINDOW_ID && windows.has(window_mouseover_id)) { |
| 5523 | // Leave previous window. |
| 5524 | _send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); |
| 5525 | } |
| 5526 | |
| 5527 | if (over_id != INVALID_WINDOW_ID && windows.has(over_id)) { |
| 5528 | _send_window_event(windows[over_id], WINDOW_EVENT_MOUSE_ENTER); |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | CursorShape c = cursor_shape; |
| 5533 | cursor_shape = CURSOR_MAX; |
| 5534 | cursor_set_shape(c); |
| 5535 | window_mouseover_id = over_id; |
| 5536 | |
| 5537 | // Once-off notification, must call again. |
| 5538 | track_mouse_leave_event(hWnd); |
| 5539 | } |
| 5540 | |
| 5541 | // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. |
| 5542 | if (!windows[window_id].window_focused && mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5543 | break; |
| 5544 | } |
| 5545 | |
| 5546 | DisplayServer::WindowID receiving_window_id = window_id; |
| 5547 | if (!windows[window_id].no_focus) { |
| 5548 | receiving_window_id = _get_focused_window_or_popup(); |
| 5549 | if (receiving_window_id == INVALID_WINDOW_ID) { |
| 5550 | receiving_window_id = window_id; |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5555 | Ref<InputEventMouseMotion> mm; |
| 5556 | mm.instantiate(); |
| 5557 | mm->set_window_id(receiving_window_id); |
| 5558 | mm->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5559 | mm->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5560 | mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5561 | mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5562 | |
| 5563 | if ((tablet_get_current_driver() == "wintab") && wintab_available && windows[window_id].wtctx) { |
| 5564 | // Note: WinTab sends both WT_PACKET and WM_xBUTTONDOWN/UP/MOUSEMOVE events, use mouse 1/0 pressure only when last_pressure was not updated recently. |
| 5565 | if (windows[window_id].last_pressure_update < 10) { |
| 5566 | windows[window_id].last_pressure_update++; |
| 5567 | } else { |
| 5568 | windows[window_id].last_tilt = Vector2(); |
| 5569 | windows[window_id].last_pressure = (wParam & MK_LBUTTON) ? 1.0f : 0.0f; |
| 5570 | windows[window_id].last_pen_inverted = false; |
| 5571 | } |
| 5572 | } else { |
| 5573 | windows[window_id].last_tilt = Vector2(); |
| 5574 | windows[window_id].last_pressure = (wParam & MK_LBUTTON) ? 1.0f : 0.0f; |
| 5575 | windows[window_id].last_pen_inverted = false; |
| 5576 | } |
| 5577 | |
| 5578 | mm->set_pressure(windows[window_id].last_pressure); |
| 5579 | mm->set_tilt(windows[window_id].last_tilt); |
| 5580 | mm->set_pen_inverted(windows[window_id].last_pen_inverted); |
| 5581 | |
| 5582 | mm->set_button_mask(mouse_get_button_state()); |
| 5583 | |
| 5584 | mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); |
| 5585 | mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); |
| 5586 | |
| 5587 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5588 | Point2i c(windows[window_id].width / 2, windows[window_id].height / 2); |
| 5589 | old_x = c.x; |
| 5590 | old_y = c.y; |
| 5591 | |
| 5592 | if (mm->get_position() == c) { |
| 5593 | center = c; |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
| 5597 | Point2i ncenter = mm->get_position(); |
| 5598 | center = ncenter; |
| 5599 | POINT pos = { (int)c.x, (int)c.y }; |
| 5600 | ClientToScreen(windows[window_id].hWnd, &pos); |
| 5601 | SetCursorPos(pos.x, pos.y); |
| 5602 | } |
| 5603 | |
| 5604 | mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 5605 | mm->set_screen_velocity(mm->get_velocity()); |
| 5606 | |
| 5607 | if (old_invalid) { |
| 5608 | old_x = mm->get_position().x; |
| 5609 | old_y = mm->get_position().y; |
| 5610 | old_invalid = false; |
| 5611 | } |
| 5612 | |
| 5613 | mm->set_relative(Vector2(mm->get_position() - Vector2(old_x, old_y))); |
| 5614 | mm->set_relative_screen_position(mm->get_relative()); |
| 5615 | old_x = mm->get_position().x; |
| 5616 | old_y = mm->get_position().y; |
| 5617 | |
| 5618 | if (receiving_window_id != window_id) { |
| 5619 | // Adjust event position relative to window distance when event is sent to a different window. |
| 5620 | mm->set_position(mm->get_position() - window_get_position(receiving_window_id) + window_get_position(window_id)); |
| 5621 | mm->set_global_position(mm->get_position()); |
| 5622 | } |
| 5623 | |
| 5624 | Input::get_singleton()->parse_input_event(mm); |
| 5625 | |
| 5626 | } break; |
| 5627 | case WM_LBUTTONDOWN: |
| 5628 | case WM_LBUTTONUP: |
| 5629 | if (Input::get_singleton()->is_emulating_mouse_from_touch()) { |
| 5630 | // Universal translation enabled; ignore OS translations for left button. |
| 5631 | LPARAM extra = GetMessageExtraInfo(); |
| 5632 | if (IsTouchEvent(extra)) { |
| 5633 | break; |
| 5634 | } |
| 5635 | } |
| 5636 | [[fallthrough]]; |
| 5637 | case WM_MBUTTONDOWN: |
| 5638 | case WM_MBUTTONUP: |
| 5639 | case WM_RBUTTONDOWN: |
| 5640 | case WM_RBUTTONUP: |
| 5641 | case WM_MOUSEWHEEL: |
| 5642 | case WM_MOUSEHWHEEL: |
| 5643 | case WM_LBUTTONDBLCLK: |
| 5644 | case WM_MBUTTONDBLCLK: |
| 5645 | case WM_RBUTTONDBLCLK: |
| 5646 | case WM_XBUTTONDBLCLK: |
| 5647 | case WM_XBUTTONDOWN: |
| 5648 | case WM_XBUTTONUP: { |
| 5649 | Ref<InputEventMouseButton> mb; |
| 5650 | mb.instantiate(); |
| 5651 | mb->set_window_id(window_id); |
| 5652 | |
| 5653 | switch (uMsg) { |
| 5654 | case WM_LBUTTONDOWN: { |
| 5655 | mb->set_pressed(true); |
| 5656 | mb->set_button_index(MouseButton::LEFT); |
| 5657 | } break; |
| 5658 | case WM_LBUTTONUP: { |
| 5659 | mb->set_pressed(false); |
| 5660 | mb->set_button_index(MouseButton::LEFT); |
| 5661 | } break; |
| 5662 | case WM_MBUTTONDOWN: { |
| 5663 | mb->set_pressed(true); |
| 5664 | mb->set_button_index(MouseButton::MIDDLE); |
| 5665 | } break; |
| 5666 | case WM_MBUTTONUP: { |
| 5667 | mb->set_pressed(false); |
| 5668 | mb->set_button_index(MouseButton::MIDDLE); |
| 5669 | } break; |
| 5670 | case WM_RBUTTONDOWN: { |
| 5671 | mb->set_pressed(true); |
| 5672 | mb->set_button_index(MouseButton::RIGHT); |
| 5673 | } break; |
| 5674 | case WM_RBUTTONUP: { |
| 5675 | mb->set_pressed(false); |
| 5676 | mb->set_button_index(MouseButton::RIGHT); |
| 5677 | } break; |
| 5678 | case WM_LBUTTONDBLCLK: { |
| 5679 | mb->set_pressed(true); |
| 5680 | mb->set_button_index(MouseButton::LEFT); |
| 5681 | mb->set_double_click(true); |
| 5682 | } break; |
| 5683 | case WM_RBUTTONDBLCLK: { |
| 5684 | mb->set_pressed(true); |
| 5685 | mb->set_button_index(MouseButton::RIGHT); |
| 5686 | mb->set_double_click(true); |
| 5687 | } break; |
| 5688 | case WM_MBUTTONDBLCLK: { |
| 5689 | mb->set_pressed(true); |
| 5690 | mb->set_button_index(MouseButton::MIDDLE); |
| 5691 | mb->set_double_click(true); |
| 5692 | } break; |
| 5693 | case WM_MOUSEWHEEL: { |
| 5694 | mb->set_pressed(true); |
| 5695 | int motion = (short)HIWORD(wParam); |
| 5696 | if (!motion) { |
| 5697 | return 0; |
| 5698 | } |
| 5699 | |
| 5700 | if (motion > 0) { |
| 5701 | mb->set_button_index(MouseButton::WHEEL_UP); |
| 5702 | } else { |
| 5703 | mb->set_button_index(MouseButton::WHEEL_DOWN); |
| 5704 | } |
| 5705 | mb->set_factor(std::fabs((double)motion / (double)WHEEL_DELTA)); |
| 5706 | } break; |
| 5707 | case WM_MOUSEHWHEEL: { |
| 5708 | mb->set_pressed(true); |
| 5709 | int motion = (short)HIWORD(wParam); |
| 5710 | if (!motion) { |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
| 5714 | if (motion < 0) { |
| 5715 | mb->set_button_index(MouseButton::WHEEL_LEFT); |
| 5716 | } else { |
| 5717 | mb->set_button_index(MouseButton::WHEEL_RIGHT); |
| 5718 | } |
| 5719 | mb->set_factor(std::fabs((double)motion / (double)WHEEL_DELTA)); |
| 5720 | } break; |
| 5721 | case WM_XBUTTONDOWN: { |
| 5722 | mb->set_pressed(true); |
| 5723 | if (HIWORD(wParam) == XBUTTON1) { |
| 5724 | mb->set_button_index(MouseButton::MB_XBUTTON1); |
| 5725 | } else { |
| 5726 | mb->set_button_index(MouseButton::MB_XBUTTON2); |
| 5727 | } |
| 5728 | } break; |
| 5729 | case WM_XBUTTONUP: { |
| 5730 | mb->set_pressed(false); |
| 5731 | if (HIWORD(wParam) == XBUTTON1) { |
| 5732 | mb->set_button_index(MouseButton::MB_XBUTTON1); |
| 5733 | } else { |
| 5734 | mb->set_button_index(MouseButton::MB_XBUTTON2); |
| 5735 | } |
| 5736 | } break; |
| 5737 | case WM_XBUTTONDBLCLK: { |
| 5738 | mb->set_pressed(true); |
| 5739 | if (HIWORD(wParam) == XBUTTON1) { |
| 5740 | mb->set_button_index(MouseButton::MB_XBUTTON1); |
| 5741 | } else { |
| 5742 | mb->set_button_index(MouseButton::MB_XBUTTON2); |
| 5743 | } |
| 5744 | mb->set_double_click(true); |
| 5745 | } break; |
| 5746 | default: { |
| 5747 | return 0; |
| 5748 | } |
| 5749 | } |
| 5750 | |
| 5751 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 5752 | mb->set_ctrl_pressed(mods.has_flag(WinKeyModifierMask::CTRL)); |
| 5753 | mb->set_shift_pressed(mods.has_flag(WinKeyModifierMask::SHIFT)); |
| 5754 | mb->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); |
| 5755 | mb->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); |
| 5756 | |
| 5757 | if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) { |
| 5758 | MouseButtonMask mask = mouse_button_to_mask(mb->get_button_index()); |
| 5759 | BitField<MouseButtonMask> scroll_mask = mouse_get_button_state(); |
| 5760 | scroll_mask.set_flag(mask); |
| 5761 | mb->set_button_mask(scroll_mask); |
| 5762 | } else { |
| 5763 | mb->set_button_mask(mouse_get_button_state()); |
| 5764 | } |
| 5765 | |
| 5766 | mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); |
| 5767 | |
| 5768 | if (mouse_mode == MOUSE_MODE_CAPTURED && !use_raw_input) { |
| 5769 | mb->set_position(Vector2(old_x, old_y)); |
| 5770 | } |
| 5771 | |
| 5772 | if (uMsg != WM_MOUSEWHEEL && uMsg != WM_MOUSEHWHEEL) { |
| 5773 | if (mb->is_pressed()) { |
| 5774 | if (++pressrc > 0 && mouse_mode != MOUSE_MODE_CAPTURED) { |
| 5775 | SetCapture(hWnd); |
| 5776 | } |
| 5777 | } else { |
| 5778 | if (--pressrc <= 0 || mouse_get_button_state().is_empty()) { |
| 5779 | if (mouse_mode != MOUSE_MODE_CAPTURED) { |
| 5780 | ReleaseCapture(); |
| 5781 | } |
| 5782 | pressrc = 0; |
| 5783 | } |
| 5784 | } |
| 5785 | } else { |
| 5786 | // For reasons unknown to humanity, wheel comes in screen coordinates. |
| 5787 | POINT coords; |
| 5788 | coords.x = mb->get_position().x; |
| 5789 | coords.y = mb->get_position().y; |
| 5790 | |
| 5791 | ScreenToClient(hWnd, &coords); |
| 5792 | |
| 5793 | mb->set_position(Vector2(coords.x, coords.y)); |
| 5794 | } |
| 5795 | |
| 5796 | mb->set_global_position(mb->get_position()); |
| 5797 | |
| 5798 | Input::get_singleton()->parse_input_event(mb); |
| 5799 | if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) { |
| 5800 | // Send release for mouse wheel. |
| 5801 | Ref<InputEventMouseButton> mbd = mb->duplicate(); |
| 5802 | mbd->set_window_id(window_id); |
| 5803 | mbd->set_button_mask(mouse_get_button_state()); |
| 5804 | mbd->set_pressed(false); |
| 5805 | Input::get_singleton()->parse_input_event(mbd); |
| 5806 | } |
| 5807 | |
| 5808 | // Propagate the button up event to the window on which the button down |
| 5809 | // event was triggered. This is needed for drag & drop to work between windows, |
| 5810 | // because the engine expects events to keep being processed |
| 5811 | // on the same window dragging started. |
| 5812 | if (mb->is_pressed()) { |
| 5813 | last_mouse_button_down_window = window_id; |
| 5814 | } else if (last_mouse_button_down_window != INVALID_WINDOW_ID) { |
| 5815 | mb->set_window_id(last_mouse_button_down_window); |
| 5816 | last_mouse_button_down_window = INVALID_WINDOW_ID; |
| 5817 | } |
| 5818 | } break; |
| 5819 | |
| 5820 | case WM_WINDOWPOSCHANGED: { |
| 5821 | WindowData &window = windows[window_id]; |
| 5822 | |
| 5823 | Vector2i off = (window.multiwindow_fs || (!window.fullscreen && window.borderless && window.maximized)) ? _get_screen_expand_offset(window_get_current_screen(window_id)) : Vector2i(); |
| 5824 | Rect2i window_client_rect; |
| 5825 | Rect2i window_rect; |
| 5826 | { |
| 5827 | RECT rect; |
| 5828 | GetClientRect(hWnd, &rect); |
| 5829 | ClientToScreen(hWnd, (POINT *)&rect.left); |
| 5830 | ClientToScreen(hWnd, (POINT *)&rect.right); |
| 5831 | window_client_rect = Rect2i(rect.left, rect.top, rect.right - rect.left - off.x, rect.bottom - rect.top - off.y); |
| 5832 | window_client_rect.position -= _get_screens_origin(); |
| 5833 | |
| 5834 | RECT wrect; |
| 5835 | GetWindowRect(hWnd, &wrect); |
| 5836 | window_rect = Rect2i(wrect.left, wrect.top, wrect.right - wrect.left - off.x, wrect.bottom - wrect.top - off.y); |
| 5837 | window_rect.position -= _get_screens_origin(); |
| 5838 | } |
| 5839 | |
| 5840 | WINDOWPOS *window_pos_params = (WINDOWPOS *)lParam; |
| 5841 | |
| 5842 | bool rect_changed = false; |
| 5843 | if (!(window_pos_params->flags & SWP_NOSIZE) || window_pos_params->flags & SWP_FRAMECHANGED) { |
| 5844 | int screen_id = window_get_current_screen(window_id); |
| 5845 | Size2i screen_size = screen_get_size(screen_id); |
| 5846 | Point2i screen_position = screen_get_position(screen_id); |
| 5847 | Rect2i usable = screen_get_usable_rect(screen_id); |
| 5848 | |
| 5849 | window.maximized = false; |
| 5850 | window.minimized = false; |
| 5851 | window.fullscreen = false; |
| 5852 | |
| 5853 | if (IsIconic(hWnd)) { |
| 5854 | window.minimized = true; |
| 5855 | } else if (IsZoomed(hWnd)) { |
| 5856 | window.maximized = true; |
| 5857 | |
| 5858 | // If maximized_window_size == screen_size add 1px border to prevent switching to exclusive_fs. |
| 5859 | if (!window.maximized_fs && window.borderless && window_rect.position == screen_position && window_rect.size == screen_size) { |
| 5860 | // Window (borderless) was just maximized and the covers the entire screen. |
| 5861 | window.maximized_fs = true; |
| 5862 | _update_window_style(window_id, false); |
| 5863 | } |
| 5864 | if (window.borderless && (screen_size != usable.size || screen_position != usable.position)) { |
| 5865 | Point2 pos = usable.position + _get_screens_origin(); |
| 5866 | Size2 size = usable.size; |
| 5867 | MoveWindow(window.hWnd, pos.x, pos.y, size.width, size.height, TRUE); |
| 5868 | } |
| 5869 | } else if (window_rect.position == screen_position && window_rect.size == screen_size) { |
| 5870 | window.fullscreen = true; |
| 5871 | } else if (window.borderless && usable.position == window_rect.position && usable.size == window_rect.size) { |
| 5872 | window.maximized = true; |
| 5873 | } |
| 5874 | |
| 5875 | if (window.maximized_fs && !window.maximized) { |
| 5876 | // Window (maximized and covering fullscreen) was just non-maximized. |
| 5877 | window.maximized_fs = false; |
| 5878 | _update_window_style(window_id, false); |
| 5879 | } |
| 5880 | |
| 5881 | if (!window.minimized) { |
| 5882 | window.width = window_client_rect.size.width; |
| 5883 | window.height = window_client_rect.size.height; |
| 5884 | window.width_with_decorations = window_rect.size.width; |
| 5885 | window.height_with_decorations = window_rect.size.height; |
| 5886 | |
| 5887 | rect_changed = true; |
| 5888 | } |
| 5889 | #if defined(RD_ENABLED) |
| 5890 | if (window.create_completed && rendering_context && window.rendering_context_window_created) { |
| 5891 | // Note: Trigger resize event to update swapchains when window is minimized/restored, even if size is not changed. |
| 5892 | rendering_context->window_set_size(window_id, window.width + off.x, window.height + off.y); |
| 5893 | } |
| 5894 | #endif |
| 5895 | #if defined(GLES3_ENABLED) |
| 5896 | if (window.create_completed && gl_manager_native && window.gl_native_window_created) { |
| 5897 | gl_manager_native->window_resize(window_id, window.width + off.x, window.height + off.y); |
| 5898 | } |
| 5899 | if (window.create_completed && gl_manager_angle && window.gl_angle_window_created) { |
| 5900 | gl_manager_angle->window_resize(window_id, window.width + off.x, window.height + off.y); |
| 5901 | } |
| 5902 | #endif |
| 5903 | } |
| 5904 | |
| 5905 | if (!window.minimized && (!(window_pos_params->flags & SWP_NOMOVE) || window_pos_params->flags & SWP_FRAMECHANGED)) { |
| 5906 | window.last_pos = window_client_rect.position; |
| 5907 | rect_changed = true; |
| 5908 | } |
| 5909 | |
| 5910 | if (rect_changed) { |
| 5911 | if (window.rect_changed_callback.is_valid()) { |
| 5912 | window.rect_changed_callback.call(Rect2i(window.last_pos.x, window.last_pos.y, window.width, window.height)); |
| 5913 | } |
| 5914 | |
| 5915 | // Update cursor clip region after window rect has changed. |
| 5916 | if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { |
| 5917 | RECT crect; |
| 5918 | GetClientRect(window.hWnd, &crect); |
| 5919 | crect.right -= off.x; |
| 5920 | crect.bottom -= off.y; |
| 5921 | ClientToScreen(window.hWnd, (POINT *)&crect.left); |
| 5922 | ClientToScreen(window.hWnd, (POINT *)&crect.right); |
| 5923 | ClipCursor(&crect); |
| 5924 | } |
| 5925 | |
| 5926 | if (!window.minimized && window.was_fullscreen_pre_min) { |
| 5927 | // Restore fullscreen mode if window was in fullscreen before it was minimized. |
| 5928 | int cs = window_get_current_screen(window_id); |
| 5929 | Point2 pos = screen_get_position(cs) + _get_screens_origin(); |
| 5930 | Size2 size = screen_get_size(cs); |
| 5931 | |
| 5932 | window.was_fullscreen_pre_min = false; |
| 5933 | window.fullscreen = true; |
| 5934 | window.maximized = false; |
| 5935 | window.minimized = false; |
| 5936 | |
| 5937 | _update_window_style(window_id, false); |
| 5938 | |
| 5939 | MoveWindow(window.hWnd, pos.x, pos.y, size.width, size.height, TRUE); |
| 5940 | } |
| 5941 | } else { |
| 5942 | if (window.parent_hwnd) { |
| 5943 | // WM_WINDOWPOSCHANGED is sent when the parent changes. |
| 5944 | // If we are supposed to have a parent and now we don't, it's likely |
| 5945 | // because the parent was closed. We will close our window as well. |
| 5946 | // This prevents an embedded game from staying alive when the editor is closed or crashes. |
| 5947 | if (!GetParent(window.hWnd)) { |
| 5948 | SendMessage(window.hWnd, WM_CLOSE, 0, 0); |
| 5949 | } |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | // Return here to prevent WM_MOVE and WM_SIZE from being sent |
| 5954 | // See: https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-windowposchanged#remarks |
| 5955 | return 0; |
| 5956 | } break; |
| 5957 | |
| 5958 | case WM_ENTERSIZEMOVE: { |
| 5959 | Input::get_singleton()->release_pressed_events(); |
| 5960 | windows[window_id].move_timer_id = SetTimer(windows[window_id].hWnd, DisplayServerWindows::TIMER_ID_MOVE_REDRAW, USER_TIMER_MINIMUM, (TIMERPROC) nullptr); |
| 5961 | } break; |
| 5962 | case WM_EXITSIZEMOVE: { |
| 5963 | KillTimer(windows[window_id].hWnd, windows[window_id].move_timer_id); |
| 5964 | windows[window_id].move_timer_id = 0; |
| 5965 | // Reset the correct mouse mode because we couldn't call ReleaseCapture in |
| 5966 | // _set_mouse_mode_impl while in _process_activate_event (because the user was moving a window). |
| 5967 | _set_mouse_mode_impl(mouse_mode); |
| 5968 | } break; |
| 5969 | case WM_TIMER: { |
| 5970 | if (wParam == windows[window_id].move_timer_id) { |
| 5971 | _THREAD_SAFE_UNLOCK_ |
| 5972 | _process_key_events(); |
| 5973 | if (!Main::is_iterating()) { |
| 5974 | Main::iteration(); |
| 5975 | } |
| 5976 | _THREAD_SAFE_LOCK_ |
| 5977 | } else if (wParam == windows[window_id].activate_timer_id) { |
| 5978 | _process_activate_event(window_id); |
| 5979 | KillTimer(windows[window_id].hWnd, windows[window_id].activate_timer_id); |
| 5980 | windows[window_id].activate_timer_id = 0; |
| 5981 | windows[window_id].first_activation_done = true; |
| 5982 | } |
| 5983 | } break; |
| 5984 | case WM_SYSKEYUP: |
| 5985 | case WM_KEYUP: |
| 5986 | case WM_SYSKEYDOWN: |
| 5987 | case WM_KEYDOWN: { |
| 5988 | if (windows[window_id].ime_suppress_next_keyup && (uMsg == WM_KEYUP || uMsg == WM_SYSKEYUP)) { |
| 5989 | windows[window_id].ime_suppress_next_keyup = false; |
| 5990 | break; |
| 5991 | } |
| 5992 | if (windows[window_id].ime_in_progress) { |
| 5993 | break; |
| 5994 | } |
| 5995 | |
| 5996 | if (mouse_mode == MOUSE_MODE_CAPTURED) { |
| 5997 | // When SetCapture is used, ALT+F4 hotkey is ignored by Windows, so handle it ourselves |
| 5998 | if (wParam == VK_F4 && _get_mods().has_flag(WinKeyModifierMask::ALT) && (uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYDOWN)) { |
| 5999 | _send_window_event(windows[window_id], WINDOW_EVENT_CLOSE_REQUEST); |
| 6000 | } |
| 6001 | } |
| 6002 | [[fallthrough]]; |
| 6003 | } |
| 6004 | case WM_CHAR: { |
| 6005 | ERR_BREAK(key_event_pos >= KEY_EVENT_BUFFER_SIZE); |
| 6006 | const BitField<WinKeyModifierMask> &mods = _get_mods(); |
| 6007 | |
| 6008 | KeyEvent ke; |
| 6009 | ke.shift = mods.has_flag(WinKeyModifierMask::SHIFT); |
| 6010 | ke.alt = mods.has_flag(WinKeyModifierMask::ALT); |
| 6011 | ke.altgr = mods.has_flag(WinKeyModifierMask::ALT_GR); |
| 6012 | ke.control = mods.has_flag(WinKeyModifierMask::CTRL); |
| 6013 | ke.meta = mods.has_flag(WinKeyModifierMask::META); |
| 6014 | ke.uMsg = uMsg; |
| 6015 | ke.window_id = window_id; |
| 6016 | |
| 6017 | if (ke.uMsg == WM_SYSKEYDOWN) { |
| 6018 | ke.uMsg = WM_KEYDOWN; |
| 6019 | } |
| 6020 | if (ke.uMsg == WM_SYSKEYUP) { |
| 6021 | ke.uMsg = WM_KEYUP; |
| 6022 | } |
| 6023 | |
| 6024 | ke.wParam = wParam; |
| 6025 | ke.lParam = lParam; |
| 6026 | key_event_buffer[key_event_pos++] = ke; |
| 6027 | |
| 6028 | } break; |
| 6029 | case WM_IME_COMPOSITION: { |
| 6030 | CANDIDATEFORM cf; |
| 6031 | cf.dwIndex = 0; |
| 6032 | |
| 6033 | cf.dwStyle = CFS_CANDIDATEPOS; |
| 6034 | cf.ptCurrentPos.x = windows[window_id].im_position.x; |
| 6035 | cf.ptCurrentPos.y = windows[window_id].im_position.y; |
| 6036 | ImmSetCandidateWindow(windows[window_id].im_himc, &cf); |
| 6037 | |
| 6038 | cf.dwStyle = CFS_EXCLUDE; |
| 6039 | cf.rcArea.left = windows[window_id].im_position.x; |
| 6040 | cf.rcArea.right = windows[window_id].im_position.x; |
| 6041 | cf.rcArea.top = windows[window_id].im_position.y; |
| 6042 | cf.rcArea.bottom = windows[window_id].im_position.y; |
| 6043 | ImmSetCandidateWindow(windows[window_id].im_himc, &cf); |
| 6044 | |
| 6045 | if (windows[window_id].ime_active) { |
| 6046 | SetCaretPos(windows[window_id].im_position.x, windows[window_id].im_position.y); |
| 6047 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 6048 | } |
| 6049 | } break; |
| 6050 | case WM_INPUTLANGCHANGEREQUEST: { |
| 6051 | // FIXME: Do something? |
| 6052 | } break; |
| 6053 | case WM_IME_STARTCOMPOSITION: { |
| 6054 | if (windows[window_id].ime_active) { |
| 6055 | windows[window_id].ime_in_progress = true; |
| 6056 | if (key_event_pos > 0) { |
| 6057 | key_event_pos--; |
| 6058 | } |
| 6059 | } |
| 6060 | return 0; |
| 6061 | } break; |
| 6062 | case WM_IME_ENDCOMPOSITION: { |
| 6063 | if (windows[window_id].ime_active) { |
| 6064 | windows[window_id].ime_in_progress = false; |
| 6065 | windows[window_id].ime_suppress_next_keyup = true; |
| 6066 | } |
| 6067 | return 0; |
| 6068 | } break; |
| 6069 | case WM_IME_NOTIFY: { |
| 6070 | return 0; |
| 6071 | } break; |
| 6072 | case WM_TOUCH: { |
| 6073 | BOOL bHandled = FALSE; |
| 6074 | UINT cInputs = LOWORD(wParam); |
| 6075 | PTOUCHINPUT pInputs = memnew_arr(TOUCHINPUT, cInputs); |
| 6076 | if (pInputs) { |
| 6077 | if (GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT))) { |
| 6078 | for (UINT i = 0; i < cInputs; i++) { |
| 6079 | TOUCHINPUT ti = pInputs[i]; |
| 6080 | POINT touch_pos = { |
| 6081 | TOUCH_COORD_TO_PIXEL(ti.x), |
| 6082 | TOUCH_COORD_TO_PIXEL(ti.y), |
| 6083 | }; |
| 6084 | ScreenToClient(hWnd, &touch_pos); |
| 6085 | // Do something with each touch input entry. |
| 6086 | if (ti.dwFlags & TOUCHEVENTF_MOVE) { |
| 6087 | _drag_event(window_id, touch_pos.x, touch_pos.y, ti.dwID); |
| 6088 | } else if (ti.dwFlags & (TOUCHEVENTF_UP | TOUCHEVENTF_DOWN)) { |
| 6089 | _touch_event(window_id, ti.dwFlags & TOUCHEVENTF_DOWN, touch_pos.x, touch_pos.y, ti.dwID); |
| 6090 | } |
| 6091 | } |
| 6092 | bHandled = TRUE; |
| 6093 | } else { |
| 6094 | // TODO: Handle the error here. |
| 6095 | } |
| 6096 | memdelete_arr(pInputs); |
| 6097 | } else { |
| 6098 | // TODO: Handle the error here, probably out of memory. |
| 6099 | } |
| 6100 | if (bHandled) { |
| 6101 | CloseTouchInputHandle((HTOUCHINPUT)lParam); |
| 6102 | return 0; |
| 6103 | } |
| 6104 | |
| 6105 | } break; |
| 6106 | case WM_DESTROY: { |
| 6107 | #ifdef ACCESSKIT_ENABLED |
| 6108 | if (accessibility_driver) { |
| 6109 | accessibility_driver->window_destroy(window_id); |
| 6110 | } |
| 6111 | #endif |
| 6112 | Input::get_singleton()->flush_buffered_events(); |
| 6113 | if (window_mouseover_id == window_id) { |
| 6114 | window_mouseover_id = INVALID_WINDOW_ID; |
| 6115 | _send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_EXIT); |
| 6116 | } |
| 6117 | } break; |
| 6118 | case WM_SETCURSOR: { |
| 6119 | if (LOWORD(lParam) == HTCLIENT) { |
| 6120 | if (windows[window_id].window_focused && (mouse_mode == MOUSE_MODE_HIDDEN || mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN)) { |
| 6121 | // Hide the cursor. |
| 6122 | if (hCursor == nullptr) { |
| 6123 | hCursor = SetCursor(nullptr); |
| 6124 | } else { |
| 6125 | SetCursor(nullptr); |
| 6126 | } |
| 6127 | } else { |
| 6128 | if (hCursor != nullptr) { |
| 6129 | CursorShape c = cursor_shape; |
| 6130 | cursor_shape = CURSOR_MAX; |
| 6131 | cursor_set_shape(c); |
| 6132 | hCursor = nullptr; |
| 6133 | } |
| 6134 | } |
| 6135 | } |
| 6136 | } break; |
| 6137 | default: { |
| 6138 | if (user_proc) { |
| 6139 | return CallWindowProcW(user_proc, hWnd, uMsg, wParam, lParam); |
| 6140 | } |
| 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 6145 | } |
| 6146 | |
| 6147 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 6148 | DisplayServerWindows *ds_win = static_cast<DisplayServerWindows *>(DisplayServer::get_singleton()); |
| 6149 | if (ds_win) { |
| 6150 | return ds_win->WndProc(hWnd, uMsg, wParam, lParam); |
| 6151 | } else { |
| 6152 | return DefWindowProcW(hWnd, uMsg, wParam, lParam); |
| 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | void DisplayServerWindows::_process_activate_event(WindowID p_window_id) { |
| 6157 | WindowData &wd = windows[p_window_id]; |
| 6158 | if (wd.activate_state == WA_ACTIVE || wd.activate_state == WA_CLICKACTIVE) { |
| 6159 | last_focused_window = p_window_id; |
| 6160 | _set_mouse_mode_impl(mouse_mode); |
| 6161 | if (!IsIconic(wd.hWnd)) { |
| 6162 | SetFocus(wd.hWnd); |
| 6163 | } |
| 6164 | wd.window_focused = true; |
| 6165 | #ifdef ACCESSKIT_ENABLED |
| 6166 | if (accessibility_driver) { |
| 6167 | accessibility_driver->accessibility_set_window_focused(p_window_id, true); |
| 6168 | } |
| 6169 | #endif |
| 6170 | _send_window_event(wd, WINDOW_EVENT_FOCUS_IN); |
| 6171 | } else { // WM_INACTIVE. |
| 6172 | Input::get_singleton()->release_pressed_events(); |
| 6173 | track_mouse_leave_event(wd.hWnd); |
| 6174 | // Release capture unconditionally because it can be set due to dragging, in addition to captured mode. |
| 6175 | // When the user is moving a window, it's important to not ReleaseCapture because it will cause |
| 6176 | // the window movement to stop and if the user tries to move the Windows when it's not activated, |
| 6177 | // it will prevent the window movement. If we are here and a window is moving, it's because we had multiple |
| 6178 | // opened windows in the editor and we are definitively not in a middle of dragging. |
| 6179 | if (!_has_moving_window()) { |
| 6180 | ReleaseCapture(); |
| 6181 | } |
| 6182 | wd.window_focused = false; |
| 6183 | #ifdef ACCESSKIT_ENABLED |
| 6184 | if (accessibility_driver) { |
| 6185 | accessibility_driver->accessibility_set_window_focused(p_window_id, false); |
| 6186 | } |
| 6187 | #endif |
| 6188 | _send_window_event(wd, WINDOW_EVENT_FOCUS_OUT); |
| 6189 | } |
| 6190 | |
| 6191 | if ((tablet_get_current_driver() == "wintab") && wintab_available && wd.wtctx) { |
| 6192 | wintab_WTEnable(wd.wtctx, wd.activate_state); |
| 6193 | } |
| 6194 | } |
| 6195 | |
| 6196 | void DisplayServerWindows::_process_key_events() { |
| 6197 | for (int i = 0; i < key_event_pos; i++) { |
| 6198 | KeyEvent &ke = key_event_buffer[i]; |
| 6199 | switch (ke.uMsg) { |
| 6200 | case WM_CHAR: { |
| 6201 | // Extended keys should only be processed as WM_KEYDOWN message. |
| 6202 | if (!KeyMappingWindows::is_extended_key(ke.wParam) && ((i == 0 && ke.uMsg == WM_CHAR) || (i > 0 && key_event_buffer[i - 1].uMsg == WM_CHAR))) { |
| 6203 | static char32_t prev_wc = 0; |
| 6204 | char32_t unicode = ke.wParam; |
| 6205 | if ((unicode & 0xfffffc00) == 0xd800) { |
| 6206 | if (prev_wc != 0) { |
| 6207 | ERR_PRINT("invalid utf16 surrogate input"); |
| 6208 | } |
| 6209 | prev_wc = unicode; |
| 6210 | break; // Skip surrogate. |
| 6211 | } else if ((unicode & 0xfffffc00) == 0xdc00) { |
| 6212 | if (prev_wc == 0) { |
| 6213 | ERR_PRINT("invalid utf16 surrogate input"); |
| 6214 | break; // Skip invalid surrogate. |
| 6215 | } |
| 6216 | unicode = (prev_wc << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000); |
| 6217 | prev_wc = 0; |
| 6218 | } else { |
| 6219 | prev_wc = 0; |
| 6220 | } |
| 6221 | Ref<InputEventKey> k; |
| 6222 | k.instantiate(); |
| 6223 | |
| 6224 | UINT vk = MapVirtualKey((ke.lParam >> 16) & 0xFF, MAPVK_VSC_TO_VK); |
| 6225 | bool is_oem = (vk >= 0xB8) && (vk <= 0xE6); |
| 6226 | Key keycode = KeyMappingWindows::get_keysym(vk); |
| 6227 | Key key_label = keycode; |
| 6228 | Key physical_keycode = KeyMappingWindows::get_scansym((ke.lParam >> 16) & 0xFF, ke.lParam & (1 << 24)); |
| 6229 | |
| 6230 | static BYTE keyboard_state[256]; |
| 6231 | memset(keyboard_state, 0, 256); |
| 6232 | wchar_t chars[256] = {}; |
| 6233 | UINT extended_code = MapVirtualKey((ke.lParam >> 16) & 0xFF, MAPVK_VSC_TO_VK_EX); |
| 6234 | if (!(ke.lParam & (1 << 24)) && ToUnicodeEx(extended_code, (ke.lParam >> 16) & 0xFF, keyboard_state, chars, 255, 4, GetKeyboardLayout(0)) > 0) { |
| 6235 | String keysym = String::utf16((char16_t *)chars, 255); |
| 6236 | if (!keysym.is_empty()) { |
| 6237 | char32_t unicode_value = keysym[0]; |
| 6238 | // For printable ASCII characters (0x20-0x7E), override the original keycode with the character value. |
| 6239 | if (is_oem && Key::SPACE <= (Key)unicode_value && (Key)unicode_value <= Key::ASCIITILDE) { |
| 6240 | keycode = fix_keycode(unicode_value, (Key)unicode_value); |
| 6241 | } |
| 6242 | key_label = fix_key_label(unicode_value, keycode); |
| 6243 | } |
| 6244 | } |
| 6245 | |
| 6246 | k->set_window_id(ke.window_id); |
| 6247 | if (keycode != Key::SHIFT) { |
| 6248 | k->set_shift_pressed(ke.shift); |
| 6249 | } |
| 6250 | if (keycode != Key::ALT) { |
| 6251 | k->set_alt_pressed(ke.alt); |
| 6252 | } |
| 6253 | if (keycode != Key::CTRL) { |
| 6254 | k->set_ctrl_pressed(ke.control); |
| 6255 | } |
| 6256 | if (keycode != Key::META) { |
| 6257 | k->set_meta_pressed(ke.meta); |
| 6258 | } |
| 6259 | k->set_pressed(true); |
| 6260 | k->set_keycode(keycode); |
| 6261 | k->set_physical_keycode(physical_keycode); |
| 6262 | k->set_key_label(key_label); |
| 6263 | k->set_unicode(fix_unicode(unicode)); |
| 6264 | if (k->get_unicode() && ke.altgr && windows[ke.window_id].ime_active) { |
| 6265 | k->set_alt_pressed(false); |
| 6266 | k->set_ctrl_pressed(false); |
| 6267 | } |
| 6268 | |
| 6269 | Input::get_singleton()->parse_input_event(k); |
| 6270 | } else { |
| 6271 | // Do nothing. |
| 6272 | } |
| 6273 | } break; |
| 6274 | case WM_KEYUP: |
| 6275 | case WM_KEYDOWN: { |
| 6276 | Ref<InputEventKey> k; |
| 6277 | k.instantiate(); |
| 6278 | |
| 6279 | k->set_window_id(ke.window_id); |
| 6280 | k->set_pressed(ke.uMsg == WM_KEYDOWN); |
| 6281 | |
| 6282 | bool is_oem = (ke.wParam >= 0xB8) && (ke.wParam <= 0xE6); |
| 6283 | Key keycode = KeyMappingWindows::get_keysym(ke.wParam); |
| 6284 | if ((ke.lParam & (1 << 24)) && (ke.wParam == VK_RETURN)) { |
| 6285 | // Special case for Numpad Enter key. |
| 6286 | keycode = Key::KP_ENTER; |
| 6287 | } |
| 6288 | Key key_label = keycode; |
| 6289 | Key physical_keycode = KeyMappingWindows::get_scansym((ke.lParam >> 16) & 0xFF, ke.lParam & (1 << 24)); |
| 6290 | KeyLocation location = KeyMappingWindows::get_location((ke.lParam >> 16) & 0xFF, ke.lParam & (1 << 24)); |
| 6291 | |
| 6292 | static BYTE keyboard_state[256]; |
| 6293 | memset(keyboard_state, 0, 256); |
| 6294 | wchar_t chars[256] = {}; |
| 6295 | UINT extended_code = MapVirtualKey((ke.lParam >> 16) & 0xFF, MAPVK_VSC_TO_VK_EX); |
| 6296 | if (!(ke.lParam & (1 << 24)) && ToUnicodeEx(extended_code, (ke.lParam >> 16) & 0xFF, keyboard_state, chars, 255, 4, GetKeyboardLayout(0)) > 0) { |
| 6297 | String keysym = String::utf16((char16_t *)chars, 255); |
| 6298 | if (!keysym.is_empty()) { |
| 6299 | char32_t unicode_value = keysym[0]; |
| 6300 | // For printable ASCII characters (0x20-0x7E), override the original keycode with the character value. |
| 6301 | if (is_oem && Key::SPACE <= (Key)unicode_value && (Key)unicode_value <= Key::ASCIITILDE) { |
| 6302 | keycode = fix_keycode(unicode_value, (Key)unicode_value); |
| 6303 | } |
| 6304 | key_label = fix_key_label(unicode_value, keycode); |
| 6305 | } |
| 6306 | } |
| 6307 | |
| 6308 | if (keycode != Key::SHIFT) { |
| 6309 | k->set_shift_pressed(ke.shift); |
| 6310 | } |
| 6311 | if (keycode != Key::ALT) { |
| 6312 | k->set_alt_pressed(ke.alt); |
| 6313 | } |
| 6314 | if (keycode != Key::CTRL) { |
| 6315 | k->set_ctrl_pressed(ke.control); |
| 6316 | } |
| 6317 | if (keycode != Key::META) { |
| 6318 | k->set_meta_pressed(ke.meta); |
| 6319 | } |
| 6320 | k->set_keycode(keycode); |
| 6321 | k->set_physical_keycode(physical_keycode); |
| 6322 | k->set_location(location); |
| 6323 | k->set_key_label(key_label); |
| 6324 | |
| 6325 | if (i + 1 < key_event_pos && key_event_buffer[i + 1].uMsg == WM_CHAR) { |
| 6326 | char32_t unicode = key_event_buffer[i + 1].wParam; |
| 6327 | static char32_t prev_wck = 0; |
| 6328 | if ((unicode & 0xfffffc00) == 0xd800) { |
| 6329 | if (prev_wck != 0) { |
| 6330 | ERR_PRINT("invalid utf16 surrogate input"); |
| 6331 | } |
| 6332 | prev_wck = unicode; |
| 6333 | break; // Skip surrogate. |
| 6334 | } else if ((unicode & 0xfffffc00) == 0xdc00) { |
| 6335 | if (prev_wck == 0) { |
| 6336 | ERR_PRINT("invalid utf16 surrogate input"); |
| 6337 | break; // Skip invalid surrogate. |
| 6338 | } |
| 6339 | unicode = (prev_wck << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000); |
| 6340 | prev_wck = 0; |
| 6341 | } else { |
| 6342 | prev_wck = 0; |
| 6343 | } |
| 6344 | k->set_unicode(fix_unicode(unicode)); |
| 6345 | } |
| 6346 | if (k->get_unicode() && ke.altgr && windows[ke.window_id].ime_active) { |
| 6347 | k->set_alt_pressed(false); |
| 6348 | k->set_ctrl_pressed(false); |
| 6349 | } |
| 6350 | |
| 6351 | k->set_echo((ke.uMsg == WM_KEYDOWN && (ke.lParam & (1 << 30)))); |
| 6352 | |
| 6353 | Input::get_singleton()->parse_input_event(k); |
| 6354 | |
| 6355 | } break; |
| 6356 | } |
| 6357 | } |
| 6358 | |
| 6359 | key_event_pos = 0; |
| 6360 | } |
| 6361 | |
| 6362 | void DisplayServerWindows::_update_tablet_ctx(const String &p_old_driver, const String &p_new_driver) { |
| 6363 | for (KeyValue<WindowID, WindowData> &E : windows) { |
| 6364 | WindowData &wd = E.value; |
| 6365 | wd.block_mm = false; |
| 6366 | if ((p_old_driver == "wintab") && wintab_available && wd.wtctx) { |
| 6367 | wintab_WTEnable(wd.wtctx, false); |
| 6368 | wintab_WTClose(wd.wtctx); |
| 6369 | wd.wtctx = nullptr; |
| 6370 | } |
| 6371 | if ((p_new_driver == "wintab") && wintab_available) { |
| 6372 | wintab_WTInfo(WTI_DEFSYSCTX, 0, &wd.wtlc); |
| 6373 | wd.wtlc.lcOptions |= CXO_MESSAGES; |
| 6374 | wd.wtlc.lcPktData = PK_STATUS | PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE | PK_ORIENTATION; |
| 6375 | wd.wtlc.lcMoveMask = PK_STATUS | PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE; |
| 6376 | wd.wtlc.lcPktMode = 0; |
| 6377 | wd.wtlc.lcOutOrgX = 0; |
| 6378 | wd.wtlc.lcOutExtX = wd.wtlc.lcInExtX; |
| 6379 | wd.wtlc.lcOutOrgY = 0; |
| 6380 | wd.wtlc.lcOutExtY = -wd.wtlc.lcInExtY; |
| 6381 | wd.wtctx = wintab_WTOpen(wd.hWnd, &wd.wtlc, false); |
| 6382 | if (wd.wtctx) { |
| 6383 | wintab_WTEnable(wd.wtctx, true); |
| 6384 | AXIS pressure; |
| 6385 | if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_NPRESSURE, &pressure)) { |
| 6386 | wd.min_pressure = int(pressure.axMin); |
| 6387 | wd.max_pressure = int(pressure.axMax); |
| 6388 | } |
| 6389 | AXIS orientation[3]; |
| 6390 | if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_ORIENTATION, &orientation)) { |
| 6391 | wd.tilt_supported = orientation[0].axResolution && orientation[1].axResolution; |
| 6392 | } |
| 6393 | wintab_WTEnable(wd.wtctx, true); |
| 6394 | } else { |
| 6395 | print_verbose("WinTab context creation failed."); |
| 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | |
| 6401 | Error DisplayServerWindows::_create_window(WindowID p_window_id, WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent, HWND p_parent_hwnd, bool p_no_redirection_bitmap) { |
| 6402 | DWORD dwExStyle; |
| 6403 | DWORD dwStyle; |
| 6404 | |
| 6405 | _get_window_style(p_window_id == MAIN_WINDOW_ID, false, (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN), p_mode != WINDOW_MODE_EXCLUSIVE_FULLSCREEN, p_flags & WINDOW_FLAG_BORDERLESS_BIT, !(p_flags & WINDOW_FLAG_RESIZE_DISABLED_BIT), p_flags & WINDOW_FLAG_MINIMIZE_DISABLED_BIT, p_flags & WINDOW_FLAG_MAXIMIZE_DISABLED_BIT, p_mode == WINDOW_MODE_MINIMIZED, p_mode == WINDOW_MODE_MAXIMIZED, false, (p_flags & WINDOW_FLAG_NO_FOCUS_BIT) | (p_flags & WINDOW_FLAG_POPUP_BIT), p_parent_hwnd, p_no_redirection_bitmap, dwStyle, dwExStyle); |
| 6406 | |
| 6407 | int rq_screen = get_screen_from_rect(p_rect); |
| 6408 | if (rq_screen < 0) { |
| 6409 | rq_screen = get_primary_screen(); // Requested window rect is outside any screen bounds. |
| 6410 | } |
| 6411 | Rect2i usable_rect = screen_get_usable_rect(rq_screen); |
| 6412 | |
| 6413 | Point2i offset = _get_screens_origin(); |
| 6414 | |
| 6415 | RECT WindowRect; |
| 6416 | |
| 6417 | Vector2i off = (p_mode == WINDOW_MODE_FULLSCREEN || ((p_flags & WINDOW_FLAG_BORDERLESS_BIT) && p_mode == WINDOW_MODE_MAXIMIZED)) ? _get_screen_expand_offset(rq_screen) : Vector2i(); |
| 6418 | |
| 6419 | WindowRect.left = p_rect.position.x; |
| 6420 | WindowRect.right = p_rect.position.x + p_rect.size.x + off.x; |
| 6421 | WindowRect.top = p_rect.position.y; |
| 6422 | WindowRect.bottom = p_rect.position.y + p_rect.size.y + off.y; |
| 6423 | |
| 6424 | if (!p_parent_hwnd) { |
| 6425 | if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 6426 | Rect2i screen_rect = Rect2i(screen_get_position(rq_screen), screen_get_size(rq_screen)); |
| 6427 | |
| 6428 | WindowRect.left = screen_rect.position.x; |
| 6429 | WindowRect.right = screen_rect.position.x + screen_rect.size.x + off.x; |
| 6430 | WindowRect.top = screen_rect.position.y; |
| 6431 | WindowRect.bottom = screen_rect.position.y + screen_rect.size.y + off.y; |
| 6432 | } else { |
| 6433 | Rect2i srect = screen_get_usable_rect(rq_screen); |
| 6434 | Point2i wpos = p_rect.position; |
| 6435 | if (srect != Rect2i()) { |
| 6436 | wpos = wpos.clamp(srect.position, srect.position + srect.size - p_rect.size / 3); |
| 6437 | } |
| 6438 | |
| 6439 | WindowRect.left = wpos.x; |
| 6440 | WindowRect.right = wpos.x + p_rect.size.x + off.x; |
| 6441 | WindowRect.top = wpos.y; |
| 6442 | WindowRect.bottom = wpos.y + p_rect.size.y + off.y; |
| 6443 | } |
| 6444 | } |
| 6445 | |
| 6446 | WindowRect.left += offset.x; |
| 6447 | WindowRect.right += offset.x; |
| 6448 | WindowRect.top += offset.y; |
| 6449 | WindowRect.bottom += offset.y; |
| 6450 | |
| 6451 | if (p_mode != WINDOW_MODE_FULLSCREEN && p_mode != WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 6452 | AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); |
| 6453 | } |
| 6454 | |
| 6455 | WindowID id = p_window_id; |
| 6456 | { |
| 6457 | WindowData *wd_transient_parent = nullptr; |
| 6458 | HWND owner_hwnd = nullptr; |
| 6459 | if (p_parent_hwnd) { |
| 6460 | owner_hwnd = p_parent_hwnd; |
| 6461 | } else if (p_transient_parent != INVALID_WINDOW_ID) { |
| 6462 | if (!windows.has(p_transient_parent)) { |
| 6463 | ERR_PRINT("Condition \"!windows.has(p_transient_parent)\" is true."); |
| 6464 | p_transient_parent = INVALID_WINDOW_ID; |
| 6465 | } else { |
| 6466 | wd_transient_parent = &windows[p_transient_parent]; |
| 6467 | if (p_exclusive) { |
| 6468 | owner_hwnd = wd_transient_parent->hWnd; |
| 6469 | } |
| 6470 | } |
| 6471 | } |
| 6472 | |
| 6473 | WindowData &wd = windows[id]; |
| 6474 | |
| 6475 | wd.id = id; |
| 6476 | wd.hWnd = CreateWindowExW( |
| 6477 | dwExStyle, |
| 6478 | L"Engine", L"", |
| 6479 | dwStyle, |
| 6480 | WindowRect.left, |
| 6481 | WindowRect.top, |
| 6482 | WindowRect.right - WindowRect.left, |
| 6483 | WindowRect.bottom - WindowRect.top, |
| 6484 | owner_hwnd, |
| 6485 | nullptr, |
| 6486 | hInstance, |
| 6487 | // tunnel the WindowData we need to handle creation message |
| 6488 | // lifetime is ensured because we are still on the stack when this is |
| 6489 | // processed in the window proc |
| 6490 | reinterpret_cast<void *>(&wd)); |
| 6491 | if (!wd.hWnd) { |
| 6492 | MessageBoxW(nullptr, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION); |
| 6493 | windows.erase(id); |
| 6494 | ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Failed to create Windows OS window."); |
| 6495 | } |
| 6496 | |
| 6497 | wd.parent_hwnd = p_parent_hwnd; |
| 6498 | |
| 6499 | // Detach the input queue from the parent window. |
| 6500 | // This prevents the embedded window from waiting on the main window's input queue, |
| 6501 | // causing lags input lags when resizing or moving the main window. |
| 6502 | if (p_parent_hwnd) { |
| 6503 | DWORD mainThreadId = GetWindowThreadProcessId(owner_hwnd, nullptr); |
| 6504 | DWORD embeddedThreadId = GetCurrentThreadId(); |
| 6505 | AttachThreadInput(embeddedThreadId, mainThreadId, FALSE); |
| 6506 | } |
| 6507 | |
| 6508 | if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 6509 | wd.fullscreen = true; |
| 6510 | if (p_mode == WINDOW_MODE_FULLSCREEN) { |
| 6511 | wd.multiwindow_fs = true; |
| 6512 | } |
| 6513 | } |
| 6514 | |
| 6515 | if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 6516 | // Save initial non-fullscreen rect. |
| 6517 | Rect2i srect = screen_get_usable_rect(rq_screen); |
| 6518 | Point2i wpos = p_rect.position; |
| 6519 | if (srect != Rect2i()) { |
| 6520 | wpos = wpos.clamp(srect.position, srect.position + srect.size - p_rect.size / 3); |
| 6521 | } |
| 6522 | |
| 6523 | wd.pre_fs_rect.left = wpos.x + offset.x; |
| 6524 | wd.pre_fs_rect.right = wpos.x + p_rect.size.x + offset.x; |
| 6525 | wd.pre_fs_rect.top = wpos.y + offset.y; |
| 6526 | wd.pre_fs_rect.bottom = wpos.y + p_rect.size.y + offset.y; |
| 6527 | wd.pre_fs_valid = true; |
| 6528 | } |
| 6529 | |
| 6530 | wd.exclusive = p_exclusive; |
| 6531 | if (wd_transient_parent) { |
| 6532 | wd.transient_parent = p_transient_parent; |
| 6533 | wd_transient_parent->transient_children.insert(id); |
| 6534 | } |
| 6535 | |
| 6536 | wd.sharp_corners = p_flags & WINDOW_FLAG_SHARP_CORNERS_BIT; |
| 6537 | { |
| 6538 | DWORD value = wd.sharp_corners ? DWMWCP_DONOTROUND : DWMWCP_DEFAULT; |
| 6539 | ::DwmSetWindowAttribute(wd.hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, &value, sizeof(value)); |
| 6540 | } |
| 6541 | |
| 6542 | if (is_dark_mode_supported() && dark_title_available) { |
| 6543 | BOOL value = is_dark_mode(); |
| 6544 | ::DwmSetWindowAttribute(wd.hWnd, use_legacy_dark_mode_before_20H1 ? DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 : DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value)); |
| 6545 | } |
| 6546 | |
| 6547 | RegisterTouchWindow(wd.hWnd, 0); |
| 6548 | DragAcceptFiles(wd.hWnd, true); |
| 6549 | |
| 6550 | if ((tablet_get_current_driver() == "wintab") && wintab_available) { |
| 6551 | wintab_WTInfo(WTI_DEFSYSCTX, 0, &wd.wtlc); |
| 6552 | wd.wtlc.lcOptions |= CXO_MESSAGES; |
| 6553 | wd.wtlc.lcPktData = PK_STATUS | PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE | PK_ORIENTATION; |
| 6554 | wd.wtlc.lcMoveMask = PK_STATUS | PK_NORMAL_PRESSURE | PK_TANGENT_PRESSURE; |
| 6555 | wd.wtlc.lcPktMode = 0; |
| 6556 | wd.wtlc.lcOutOrgX = 0; |
| 6557 | wd.wtlc.lcOutExtX = wd.wtlc.lcInExtX; |
| 6558 | wd.wtlc.lcOutOrgY = 0; |
| 6559 | wd.wtlc.lcOutExtY = -wd.wtlc.lcInExtY; |
| 6560 | wd.wtctx = wintab_WTOpen(wd.hWnd, &wd.wtlc, false); |
| 6561 | if (wd.wtctx) { |
| 6562 | wintab_WTEnable(wd.wtctx, true); |
| 6563 | AXIS pressure; |
| 6564 | if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_NPRESSURE, &pressure)) { |
| 6565 | wd.min_pressure = int(pressure.axMin); |
| 6566 | wd.max_pressure = int(pressure.axMax); |
| 6567 | } |
| 6568 | AXIS orientation[3]; |
| 6569 | if (wintab_WTInfo(WTI_DEVICES + wd.wtlc.lcDevice, DVC_ORIENTATION, &orientation)) { |
| 6570 | wd.tilt_supported = orientation[0].axResolution && orientation[1].axResolution; |
| 6571 | } |
| 6572 | } else { |
| 6573 | print_verbose("WinTab context creation failed."); |
| 6574 | } |
| 6575 | } else { |
| 6576 | wd.wtctx = nullptr; |
| 6577 | } |
| 6578 | |
| 6579 | if (p_mode == WINDOW_MODE_MAXIMIZED) { |
| 6580 | wd.maximized = true; |
| 6581 | wd.minimized = false; |
| 6582 | } |
| 6583 | |
| 6584 | if (p_mode == WINDOW_MODE_MINIMIZED) { |
| 6585 | wd.maximized = false; |
| 6586 | wd.minimized = true; |
| 6587 | } |
| 6588 | |
| 6589 | wd.last_pressure = 0; |
| 6590 | wd.last_pressure_update = 0; |
| 6591 | wd.last_tilt = Vector2(); |
| 6592 | |
| 6593 | IPropertyStore *prop_store; |
| 6594 | HRESULT hr = SHGetPropertyStoreForWindow(wd.hWnd, IID_IPropertyStore, (void **)&prop_store); |
| 6595 | if (hr == S_OK) { |
| 6596 | PROPVARIANT val; |
| 6597 | String appname; |
| 6598 | if (Engine::get_singleton()->is_editor_hint()) { |
| 6599 | appname = "Godot.GodotEditor." + String(GODOT_VERSION_FULL_CONFIG); |
| 6600 | } else { |
| 6601 | String name = GLOBAL_GET("application/config/name"); |
| 6602 | String version = GLOBAL_GET("application/config/version"); |
| 6603 | if (version.is_empty()) { |
| 6604 | version = "0"; |
| 6605 | } |
| 6606 | String clean_app_name = name.to_pascal_case(); |
| 6607 | for (int i = 0; i < clean_app_name.length(); i++) { |
| 6608 | if (!is_ascii_alphanumeric_char(clean_app_name[i]) && clean_app_name[i] != '_' && clean_app_name[i] != '.') { |
| 6609 | clean_app_name[i] = '_'; |
| 6610 | } |
| 6611 | } |
| 6612 | clean_app_name = clean_app_name.substr(0, 120 - version.length()).trim_suffix("."); |
| 6613 | appname = "Godot." + clean_app_name + "." + version; |
| 6614 | } |
| 6615 | InitPropVariantFromString((PCWSTR)appname.utf16().get_data(), &val); |
| 6616 | prop_store->SetValue(PKEY_AppUserModel_ID, val); |
| 6617 | prop_store->Release(); |
| 6618 | } |
| 6619 | |
| 6620 | // IME. |
| 6621 | wd.im_himc = ImmGetContext(wd.hWnd); |
| 6622 | ImmAssociateContext(wd.hWnd, (HIMC) nullptr); |
| 6623 | |
| 6624 | wd.im_position = Vector2(); |
| 6625 | |
| 6626 | if (p_mode == WINDOW_MODE_FULLSCREEN || p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN || p_mode == WINDOW_MODE_MAXIMIZED) { |
| 6627 | RECT r; |
| 6628 | GetClientRect(wd.hWnd, &r); |
| 6629 | ClientToScreen(wd.hWnd, (POINT *)&r.left); |
| 6630 | ClientToScreen(wd.hWnd, (POINT *)&r.right); |
| 6631 | wd.last_pos = Point2i(r.left, r.top) - _get_screens_origin(); |
| 6632 | wd.width = r.right - r.left - off.x; |
| 6633 | wd.height = r.bottom - r.top - off.y; |
| 6634 | } else { |
| 6635 | wd.last_pos = p_rect.position; |
| 6636 | wd.width = p_rect.size.width; |
| 6637 | wd.height = p_rect.size.height; |
| 6638 | } |
| 6639 | |
| 6640 | wd.no_redirection_bitmap = p_no_redirection_bitmap; |
| 6641 | |
| 6642 | wd.create_completed = true; |
| 6643 | // Set size of maximized borderless window (by default it covers the entire screen). |
| 6644 | if (!p_parent_hwnd && p_mode == WINDOW_MODE_MAXIMIZED && (p_flags & WINDOW_FLAG_BORDERLESS_BIT)) { |
| 6645 | SetWindowPos(wd.hWnd, HWND_TOP, usable_rect.position.x - off.x, usable_rect.position.y - off.y, usable_rect.size.width + off.x, usable_rect.size.height + off.y, SWP_NOZORDER | SWP_NOACTIVATE); |
| 6646 | } |
| 6647 | _update_window_mouse_passthrough(id); |
| 6648 | } |
| 6649 | |
| 6650 | return OK; |
| 6651 | } |
| 6652 | |
| 6653 | void DisplayServerWindows::_destroy_window(WindowID p_window_id) { |
| 6654 | WindowData &wd = windows[p_window_id]; |
| 6655 | |
| 6656 | IPropertyStore *prop_store; |
| 6657 | HRESULT hr = SHGetPropertyStoreForWindow(wd.hWnd, IID_IPropertyStore, (void **)&prop_store); |
| 6658 | if (hr == S_OK) { |
| 6659 | PROPVARIANT val; |
| 6660 | PropVariantInit(&val); |
| 6661 | prop_store->SetValue(PKEY_AppUserModel_ID, val); |
| 6662 | prop_store->Release(); |
| 6663 | } |
| 6664 | |
| 6665 | if ((tablet_get_current_driver() == "wintab") && wintab_available && wd.wtctx) { |
| 6666 | wintab_WTClose(wd.wtctx); |
| 6667 | wd.wtctx = nullptr; |
| 6668 | } |
| 6669 | |
| 6670 | if (wd.drop_target != nullptr) { |
| 6671 | RevokeDragDrop(wd.hWnd); |
| 6672 | wd.drop_target->Release(); |
| 6673 | } |
| 6674 | |
| 6675 | DestroyWindow(wd.hWnd); |
| 6676 | windows.erase(p_window_id); |
| 6677 | } |
| 6678 | |
| 6679 | #ifdef RD_ENABLED |
| 6680 | Error DisplayServerWindows::_create_rendering_context_window(WindowID p_window_id, const String &p_rendering_driver) { |
| 6681 | DEV_ASSERT(rendering_context != nullptr); |
| 6682 | |
| 6683 | WindowData &wd = windows[p_window_id]; |
| 6684 | |
| 6685 | union { |
| 6686 | #ifdef VULKAN_ENABLED |
| 6687 | RenderingContextDriverVulkanWindows::WindowPlatformData vulkan; |
| 6688 | #endif |
| 6689 | #ifdef D3D12_ENABLED |
| 6690 | RenderingContextDriverD3D12::WindowPlatformData d3d12; |
| 6691 | #endif |
| 6692 | } wpd; |
| 6693 | #ifdef VULKAN_ENABLED |
| 6694 | if (p_rendering_driver == "vulkan") { |
| 6695 | wpd.vulkan.window = wd.hWnd; |
| 6696 | wpd.vulkan.instance = hInstance; |
| 6697 | } |
| 6698 | #endif |
| 6699 | #ifdef D3D12_ENABLED |
| 6700 | if (p_rendering_driver == "d3d12") { |
| 6701 | wpd.d3d12.window = wd.hWnd; |
| 6702 | } |
| 6703 | #endif |
| 6704 | |
| 6705 | Error err = rendering_context->window_create(p_window_id, &wpd); |
| 6706 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to create %s window.", p_rendering_driver)); |
| 6707 | |
| 6708 | Vector2i off = (wd.multiwindow_fs || (!wd.fullscreen && wd.borderless && wd.maximized)) ? _get_screen_expand_offset(window_get_current_screen(p_window_id)) : Vector2i(); |
| 6709 | rendering_context->window_set_size(p_window_id, wd.width + off.x, wd.height + off.y); |
| 6710 | wd.rendering_context_window_created = true; |
| 6711 | |
| 6712 | return OK; |
| 6713 | } |
| 6714 | |
| 6715 | void DisplayServerWindows::_destroy_rendering_context_window(WindowID p_window_id) { |
| 6716 | DEV_ASSERT(rendering_context != nullptr); |
| 6717 | |
| 6718 | WindowData &wd = windows[p_window_id]; |
| 6719 | DEV_ASSERT(wd.rendering_context_window_created); |
| 6720 | |
| 6721 | rendering_context->window_destroy(p_window_id); |
| 6722 | wd.rendering_context_window_created = false; |
| 6723 | } |
| 6724 | #endif |
| 6725 | |
| 6726 | #ifdef GLES3_ENABLED |
| 6727 | Error DisplayServerWindows::_create_gl_window(WindowID p_window_id) { |
| 6728 | if (gl_manager_native) { |
| 6729 | WindowData &wd = windows[p_window_id]; |
| 6730 | |
| 6731 | Error err = gl_manager_native->window_create(p_window_id, wd.hWnd, hInstance, wd.width, wd.height); |
| 6732 | ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to create native OpenGL window."); |
| 6733 | |
| 6734 | wd.gl_native_window_created = true; |
| 6735 | } |
| 6736 | |
| 6737 | if (gl_manager_angle) { |
| 6738 | WindowData &wd = windows[p_window_id]; |
| 6739 | |
| 6740 | Error err = gl_manager_angle->window_create(p_window_id, nullptr, wd.hWnd, wd.width, wd.height); |
| 6741 | ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to create ANGLE OpenGL window."); |
| 6742 | |
| 6743 | wd.gl_angle_window_created = true; |
| 6744 | } |
| 6745 | |
| 6746 | return OK; |
| 6747 | } |
| 6748 | #endif |
| 6749 | |
| 6750 | BitField<DisplayServerWindows::DriverID> DisplayServerWindows::tested_drivers = 0; |
| 6751 | |
| 6752 | // WinTab API. |
| 6753 | bool DisplayServerWindows::wintab_available = false; |
| 6754 | WTOpenPtr DisplayServerWindows::wintab_WTOpen = nullptr; |
| 6755 | WTClosePtr DisplayServerWindows::wintab_WTClose = nullptr; |
| 6756 | WTInfoPtr DisplayServerWindows::wintab_WTInfo = nullptr; |
| 6757 | WTPacketPtr DisplayServerWindows::wintab_WTPacket = nullptr; |
| 6758 | WTEnablePtr DisplayServerWindows::wintab_WTEnable = nullptr; |
| 6759 | |
| 6760 | // UXTheme API. |
| 6761 | bool DisplayServerWindows::dark_title_available = false; |
| 6762 | bool DisplayServerWindows::use_legacy_dark_mode_before_20H1 = false; |
| 6763 | bool DisplayServerWindows::ux_theme_available = false; |
| 6764 | ShouldAppsUseDarkModePtr DisplayServerWindows::ShouldAppsUseDarkMode = nullptr; |
| 6765 | GetImmersiveColorFromColorSetExPtr DisplayServerWindows::GetImmersiveColorFromColorSetEx = nullptr; |
| 6766 | GetImmersiveColorTypeFromNamePtr DisplayServerWindows::GetImmersiveColorTypeFromName = nullptr; |
| 6767 | GetImmersiveUserColorSetPreferencePtr DisplayServerWindows::GetImmersiveUserColorSetPreference = nullptr; |
| 6768 | |
| 6769 | Vector2i _get_device_ids_reg(const String &p_device_name) { |
| 6770 | Vector2i out; |
| 6771 | |
| 6772 | String subkey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}"; |
| 6773 | HKEY hkey = nullptr; |
| 6774 | LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)subkey.utf16().get_data(), 0, KEY_READ, &hkey); |
| 6775 | if (result != ERROR_SUCCESS) { |
| 6776 | return Vector2i(); |
| 6777 | } |
| 6778 | |
| 6779 | DWORD subkeys = 0; |
| 6780 | result = RegQueryInfoKeyW(hkey, nullptr, nullptr, nullptr, &subkeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); |
| 6781 | if (result != ERROR_SUCCESS) { |
| 6782 | RegCloseKey(hkey); |
| 6783 | return Vector2i(); |
| 6784 | } |
| 6785 | for (DWORD i = 0; i < subkeys; i++) { |
| 6786 | WCHAR key_name[MAX_PATH] = L""; |
| 6787 | DWORD key_name_size = MAX_PATH; |
| 6788 | result = RegEnumKeyExW(hkey, i, key_name, &key_name_size, nullptr, nullptr, nullptr, nullptr); |
| 6789 | if (result != ERROR_SUCCESS) { |
| 6790 | continue; |
| 6791 | } |
| 6792 | String id = String::utf16((const char16_t *)key_name, key_name_size); |
| 6793 | if (!id.is_empty()) { |
| 6794 | HKEY sub_hkey = nullptr; |
| 6795 | result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)(subkey + "\\" + id).utf16().get_data(), 0, KEY_QUERY_VALUE, &sub_hkey); |
| 6796 | if (result != ERROR_SUCCESS) { |
| 6797 | continue; |
| 6798 | } |
| 6799 | |
| 6800 | WCHAR buffer[4096]; |
| 6801 | DWORD buffer_len = 4096; |
| 6802 | DWORD vtype = REG_SZ; |
| 6803 | if (RegQueryValueExW(sub_hkey, L"DriverDesc", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) != ERROR_SUCCESS || buffer_len == 0) { |
| 6804 | buffer_len = 4096; |
| 6805 | if (RegQueryValueExW(sub_hkey, L"HardwareInformation.AdapterString", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) != ERROR_SUCCESS || buffer_len == 0) { |
| 6806 | RegCloseKey(sub_hkey); |
| 6807 | continue; |
| 6808 | } |
| 6809 | } |
| 6810 | |
| 6811 | String driver_name = String::utf16((const char16_t *)buffer, buffer_len).strip_edges(); |
| 6812 | if (driver_name == p_device_name) { |
| 6813 | String driver_id; |
| 6814 | |
| 6815 | buffer_len = 4096; |
| 6816 | if (RegQueryValueExW(sub_hkey, L"MatchingDeviceId", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS && buffer_len != 0) { |
| 6817 | driver_id = String::utf16((const char16_t *)buffer, buffer_len).strip_edges(); |
| 6818 | |
| 6819 | Vector<String> id_parts = driver_id.to_lower().split("&"); |
| 6820 | for (const String &id_part : id_parts) { |
| 6821 | int ven_off = id_part.find("ven_"); |
| 6822 | if (ven_off >= 0) { |
| 6823 | out.x = id_part.substr(ven_off + 4).hex_to_int(); |
| 6824 | } |
| 6825 | int dev_off = id_part.find("dev_"); |
| 6826 | if (dev_off >= 0) { |
| 6827 | out.y = id_part.substr(dev_off + 4).hex_to_int(); |
| 6828 | } |
| 6829 | } |
| 6830 | |
| 6831 | RegCloseKey(sub_hkey); |
| 6832 | break; |
| 6833 | } |
| 6834 | } |
| 6835 | RegCloseKey(sub_hkey); |
| 6836 | } |
| 6837 | } |
| 6838 | RegCloseKey(hkey); |
| 6839 | return out; |
| 6840 | } |
| 6841 | |
| 6842 | Vector2i _get_device_ids_wmi(const String &p_device_name) { |
| 6843 | if (p_device_name.is_empty()) { |
| 6844 | return Vector2i(); |
| 6845 | } |
| 6846 | |
| 6847 | REFCLSID clsid = CLSID_WbemLocator; // Unmarshaler CLSID |
| 6848 | REFIID uuid = IID_IWbemLocator; // Interface UUID |
| 6849 | IWbemLocator *wbemLocator = nullptr; // to get the services |
| 6850 | IWbemServices *wbemServices = nullptr; // to get the class |
| 6851 | IEnumWbemClassObject *iter = nullptr; |
| 6852 | IWbemClassObject *pnpSDriverObject[1]; // contains driver name, version, etc. |
| 6853 | |
| 6854 | HRESULT hr = CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, uuid, (LPVOID *)&wbemLocator); |
| 6855 | if (hr != S_OK) { |
| 6856 | return Vector2i(); |
| 6857 | } |
| 6858 | BSTR resource_name = SysAllocString(L"root\\CIMV2"); |
| 6859 | hr = wbemLocator->ConnectServer(resource_name, nullptr, nullptr, nullptr, 0, nullptr, nullptr, &wbemServices); |
| 6860 | SysFreeString(resource_name); |
| 6861 | |
| 6862 | SAFE_RELEASE(wbemLocator) // from now on, use `wbemServices` |
| 6863 | if (hr != S_OK) { |
| 6864 | SAFE_RELEASE(wbemServices) |
| 6865 | return Vector2i(); |
| 6866 | } |
| 6867 | |
| 6868 | Vector2i ids; |
| 6869 | |
| 6870 | const String gpu_device_class_query = vformat("SELECT * FROM Win32_PnPSignedDriver WHERE DeviceName = \"%s\"", p_device_name); |
| 6871 | BSTR query = SysAllocString((const WCHAR *)gpu_device_class_query.utf16().get_data()); |
| 6872 | BSTR query_lang = SysAllocString(L"WQL"); |
| 6873 | hr = wbemServices->ExecQuery(query_lang, query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, nullptr, &iter); |
| 6874 | SysFreeString(query_lang); |
| 6875 | SysFreeString(query); |
| 6876 | if (hr == S_OK) { |
| 6877 | ULONG resultCount; |
| 6878 | hr = iter->Next(5000, 1, pnpSDriverObject, &resultCount); // Get exactly 1. Wait max 5 seconds. |
| 6879 | |
| 6880 | if (hr == S_OK && resultCount > 0) { |
| 6881 | VARIANT did; |
| 6882 | VariantInit(&did); |
| 6883 | BSTR object_name = SysAllocString(L"DeviceID"); |
| 6884 | hr = pnpSDriverObject[0]->Get(object_name, 0, &did, nullptr, nullptr); |
| 6885 | SysFreeString(object_name); |
| 6886 | if (hr == S_OK) { |
| 6887 | String device_id = String(V_BSTR(&did)); |
| 6888 | ids.x = device_id.get_slicec('&', 0).lstrip("PCI\\VEN_").hex_to_int(); |
| 6889 | ids.y = device_id.get_slicec('&', 1).lstrip("DEV_").hex_to_int(); |
| 6890 | } |
| 6891 | |
| 6892 | for (ULONG i = 0; i < resultCount; i++) { |
| 6893 | SAFE_RELEASE(pnpSDriverObject[i]) |
| 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | SAFE_RELEASE(wbemServices) |
| 6899 | SAFE_RELEASE(iter) |
| 6900 | |
| 6901 | return ids; |
| 6902 | } |
| 6903 | |
| 6904 | Vector2i _get_device_ids(const String &p_device_name) { |
| 6905 | Vector2i out = _get_device_ids_reg(p_device_name); |
| 6906 | if (out == Vector2i()) { |
| 6907 | out = _get_device_ids_wmi(p_device_name); |
| 6908 | } |
| 6909 | return out; |
| 6910 | } |
| 6911 | |
| 6912 | bool DisplayServerWindows::is_dark_mode_supported() const { |
| 6913 | return ux_theme_available; |
| 6914 | } |
| 6915 | |
| 6916 | bool DisplayServerWindows::is_dark_mode() const { |
| 6917 | return ux_theme_available && ShouldAppsUseDarkMode(); |
| 6918 | } |
| 6919 | |
| 6920 | Color DisplayServerWindows::get_accent_color() const { |
| 6921 | if (!ux_theme_available) { |
| 6922 | return Color(0, 0, 0, 0); |
| 6923 | } |
| 6924 | |
| 6925 | int argb = GetImmersiveColorFromColorSetEx((UINT)GetImmersiveUserColorSetPreference(false, false), GetImmersiveColorTypeFromName(L"ImmersiveSystemAccent"), false, 0); |
| 6926 | return Color((argb & 0xFF) / 255.f, ((argb & 0xFF00) >> 8) / 255.f, ((argb & 0xFF0000) >> 16) / 255.f, ((argb & 0xFF000000) >> 24) / 255.f); |
| 6927 | } |
| 6928 | |
| 6929 | Color DisplayServerWindows::get_base_color() const { |
| 6930 | if (!ux_theme_available) { |
| 6931 | return Color(0, 0, 0, 0); |
| 6932 | } |
| 6933 | |
| 6934 | int argb = GetImmersiveColorFromColorSetEx((UINT)GetImmersiveUserColorSetPreference(false, false), GetImmersiveColorTypeFromName(ShouldAppsUseDarkMode() ? L"ImmersiveDarkChromeMediumLow" : L"ImmersiveLightChromeMediumLow"), false, 0); |
| 6935 | return Color((argb & 0xFF) / 255.f, ((argb & 0xFF00) >> 8) / 255.f, ((argb & 0xFF0000) >> 16) / 255.f, ((argb & 0xFF000000) >> 24) / 255.f); |
| 6936 | } |
| 6937 | |
| 6938 | void DisplayServerWindows::set_system_theme_change_callback(const Callable &p_callable) { |
| 6939 | system_theme_changed = p_callable; |
| 6940 | } |
| 6941 | |
| 6942 | int DisplayServerWindows::tablet_get_driver_count() const { |
| 6943 | return tablet_drivers.size(); |
| 6944 | } |
| 6945 | |
| 6946 | String DisplayServerWindows::tablet_get_driver_name(int p_driver) const { |
| 6947 | if (p_driver < 0 || p_driver >= tablet_drivers.size()) { |
| 6948 | return ""; |
| 6949 | } else { |
| 6950 | return tablet_drivers[p_driver]; |
| 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | String DisplayServerWindows::tablet_get_current_driver() const { |
| 6955 | return tablet_driver; |
| 6956 | } |
| 6957 | |
| 6958 | void DisplayServerWindows::tablet_set_current_driver(const String &p_driver) { |
| 6959 | if (tablet_get_driver_count() == 0) { |
| 6960 | return; |
| 6961 | } |
| 6962 | |
| 6963 | String driver = p_driver; |
| 6964 | if (driver == "auto") { |
| 6965 | if (!winink_disabled) { |
| 6966 | driver = "winink"; |
| 6967 | } else if (wintab_available) { |
| 6968 | driver = "wintab"; |
| 6969 | } else { |
| 6970 | driver = "dummy"; |
| 6971 | } |
| 6972 | } |
| 6973 | |
| 6974 | bool found = false; |
| 6975 | for (int i = 0; i < tablet_get_driver_count(); i++) { |
| 6976 | if (driver == tablet_get_driver_name(i)) { |
| 6977 | found = true; |
| 6978 | } |
| 6979 | } |
| 6980 | if (found) { |
| 6981 | _update_tablet_ctx(tablet_driver, driver); |
| 6982 | tablet_driver = driver; |
| 6983 | } else { |
| 6984 | ERR_PRINT("Unknown tablet driver " + p_driver + "."); |
| 6985 | } |
| 6986 | } |
| 6987 | |
| 6988 | DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 6989 | KeyMappingWindows::initialize(); |
| 6990 | |
| 6991 | tested_drivers.clear(); |
| 6992 | |
| 6993 | drop_events = false; |
| 6994 | key_event_pos = 0; |
| 6995 | |
| 6996 | hInstance = static_cast<OS_Windows *>(OS::get_singleton())->get_hinstance(); |
| 6997 | |
| 6998 | pressrc = 0; |
| 6999 | old_invalid = true; |
| 7000 | mouse_mode = MOUSE_MODE_VISIBLE; |
| 7001 | |
| 7002 | rendering_driver = p_rendering_driver; |
| 7003 | |
| 7004 | // Init TTS |
| 7005 | bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech"); |
| 7006 | if (tts_enabled) { |
| 7007 | initialize_tts(); |
| 7008 | } |
| 7009 | native_menu = memnew(NativeMenuWindows); |
| 7010 | |
| 7011 | #ifdef ACCESSKIT_ENABLED |
| 7012 | if (accessibility_get_mode() != DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) { |
| 7013 | accessibility_driver = memnew(AccessibilityDriverAccessKit); |
| 7014 | if (accessibility_driver->init() != OK) { |
| 7015 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 7016 | ERR_PRINT("Can't create an accessibility driver, accessibility support disabled!"); |
| 7017 | } |
| 7018 | memdelete(accessibility_driver); |
| 7019 | accessibility_driver = nullptr; |
| 7020 | } |
| 7021 | } |
| 7022 | #endif |
| 7023 | |
| 7024 | // Enforce default keep screen on value. |
| 7025 | screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on")); |
| 7026 | |
| 7027 | // Load Windows version info. |
| 7028 | ZeroMemory(&os_ver, sizeof(OSVERSIONINFOW)); |
| 7029 | os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); |
| 7030 | |
| 7031 | HMODULE nt_lib = LoadLibraryW(L"ntdll.dll"); |
| 7032 | bool is_wine = false; |
| 7033 | if (nt_lib) { |
| 7034 | WineGetVersionPtr wine_get_version = (WineGetVersionPtr)(void *)GetProcAddress(nt_lib, "wine_get_version"); // Do not read Windows build number under Wine, it can be set to arbitrary value. |
| 7035 | if (wine_get_version) { |
| 7036 | is_wine = true; |
| 7037 | } else { |
| 7038 | RtlGetVersionPtr RtlGetVersion = (RtlGetVersionPtr)(void *)GetProcAddress(nt_lib, "RtlGetVersion"); |
| 7039 | if (RtlGetVersion) { |
| 7040 | RtlGetVersion(&os_ver); |
| 7041 | } |
| 7042 | } |
| 7043 | FreeLibrary(nt_lib); |
| 7044 | } |
| 7045 | |
| 7046 | // Load UXTheme. |
| 7047 | if (os_ver.dwBuildNumber >= 10240) { // Not available on Wine, use only if real Windows 10/11 detected. |
| 7048 | HMODULE ux_theme_lib = LoadLibraryW(L"uxtheme.dll"); |
| 7049 | if (ux_theme_lib) { |
| 7050 | ShouldAppsUseDarkMode = (ShouldAppsUseDarkModePtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(132)); |
| 7051 | GetImmersiveColorFromColorSetEx = (GetImmersiveColorFromColorSetExPtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(95)); |
| 7052 | GetImmersiveColorTypeFromName = (GetImmersiveColorTypeFromNamePtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(96)); |
| 7053 | GetImmersiveUserColorSetPreference = (GetImmersiveUserColorSetPreferencePtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(98)); |
| 7054 | if (os_ver.dwBuildNumber >= 17763) { // Windows 10 Redstone 5 (1809)+ only. |
| 7055 | AllowDarkModeForAppPtr AllowDarkModeForApp = nullptr; |
| 7056 | SetPreferredAppModePtr SetPreferredAppMode = nullptr; |
| 7057 | FlushMenuThemesPtr FlushMenuThemes = nullptr; |
| 7058 | if (os_ver.dwBuildNumber < 18362) { // Windows 10 Redstone 5 (1809) and 19H1 (1903) only. |
| 7059 | AllowDarkModeForApp = (AllowDarkModeForAppPtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(135)); |
| 7060 | } else { // Windows 10 19H2 (1909)+ only. |
| 7061 | SetPreferredAppMode = (SetPreferredAppModePtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(135)); |
| 7062 | FlushMenuThemes = (FlushMenuThemesPtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(136)); |
| 7063 | } |
| 7064 | RefreshImmersiveColorPolicyStatePtr RefreshImmersiveColorPolicyState = (RefreshImmersiveColorPolicyStatePtr)(void *)GetProcAddress(ux_theme_lib, MAKEINTRESOURCEA(104)); |
| 7065 | if (ShouldAppsUseDarkMode) { |
| 7066 | bool dark_mode = ShouldAppsUseDarkMode(); |
| 7067 | if (SetPreferredAppMode) { |
| 7068 | SetPreferredAppMode(dark_mode ? APPMODE_ALLOWDARK : APPMODE_DEFAULT); |
| 7069 | } else if (AllowDarkModeForApp) { |
| 7070 | AllowDarkModeForApp(dark_mode); |
| 7071 | } |
| 7072 | if (RefreshImmersiveColorPolicyState) { |
| 7073 | RefreshImmersiveColorPolicyState(); |
| 7074 | } |
| 7075 | if (FlushMenuThemes) { |
| 7076 | FlushMenuThemes(); |
| 7077 | } |
| 7078 | } |
| 7079 | } |
| 7080 | |
| 7081 | ux_theme_available = ShouldAppsUseDarkMode && GetImmersiveColorFromColorSetEx && GetImmersiveColorTypeFromName && GetImmersiveUserColorSetPreference; |
| 7082 | if (os_ver.dwBuildNumber >= 18363) { |
| 7083 | dark_title_available = true; |
| 7084 | if (os_ver.dwBuildNumber < 19041) { |
| 7085 | use_legacy_dark_mode_before_20H1 = true; |
| 7086 | } |
| 7087 | } |
| 7088 | } |
| 7089 | } |
| 7090 | |
| 7091 | tablet_drivers.push_back("auto"); |
| 7092 | tablet_drivers.push_back("winink"); |
| 7093 | |
| 7094 | // Note: Wacom WinTab driver API for pen input, for devices incompatible with Windows Ink. |
| 7095 | HMODULE wintab_lib = LoadLibraryW(L"wintab32.dll"); |
| 7096 | if (wintab_lib) { |
| 7097 | wintab_WTOpen = (WTOpenPtr)(void *)GetProcAddress(wintab_lib, "WTOpenW"); |
| 7098 | wintab_WTClose = (WTClosePtr)(void *)GetProcAddress(wintab_lib, "WTClose"); |
| 7099 | wintab_WTInfo = (WTInfoPtr)(void *)GetProcAddress(wintab_lib, "WTInfoW"); |
| 7100 | wintab_WTPacket = (WTPacketPtr)(void *)GetProcAddress(wintab_lib, "WTPacket"); |
| 7101 | wintab_WTEnable = (WTEnablePtr)(void *)GetProcAddress(wintab_lib, "WTEnable"); |
| 7102 | |
| 7103 | wintab_available = wintab_WTOpen && wintab_WTClose && wintab_WTInfo && wintab_WTPacket && wintab_WTEnable; |
| 7104 | } |
| 7105 | |
| 7106 | if (wintab_available) { |
| 7107 | tablet_drivers.push_back("wintab"); |
| 7108 | } |
| 7109 | |
| 7110 | tablet_drivers.push_back("dummy"); |
| 7111 | |
| 7112 | String wacom_cfg = OS::get_singleton()->get_config_path().path_join("WTablet").path_join("Wacom_Tablet.dat"); |
| 7113 | if (FileAccess::exists(wacom_cfg)) { |
| 7114 | Ref<XMLParser> parser; |
| 7115 | parser.instantiate(); |
| 7116 | if (parser->open(wacom_cfg) == OK) { |
| 7117 | while (parser->read() == OK) { |
| 7118 | if (parser->get_node_type() != XMLParser::NODE_ELEMENT) { |
| 7119 | continue; |
| 7120 | } |
| 7121 | if (parser->get_node_name() == "WinUseInk") { |
| 7122 | parser->read(); |
| 7123 | if (parser->get_node_type() == XMLParser::NODE_TEXT) { |
| 7124 | winink_disabled = (parser->get_node_data().to_lower().strip_edges() != "true"); |
| 7125 | print_verbose(vformat("Wacom tablet config found at \"%s\", Windows Ink support is %s.", wacom_cfg, winink_disabled ? "disabled" : "enabled")); |
| 7126 | break; |
| 7127 | } |
| 7128 | } |
| 7129 | } |
| 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | if (OS::get_singleton()->is_hidpi_allowed()) { |
| 7134 | SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE); |
| 7135 | } |
| 7136 | |
| 7137 | HMODULE comctl32 = LoadLibraryW(L"comctl32.dll"); |
| 7138 | if (comctl32) { |
| 7139 | typedef BOOL(WINAPI * InitCommonControlsExPtr)(_In_ const INITCOMMONCONTROLSEX *picce); |
| 7140 | InitCommonControlsExPtr init_common_controls_ex = (InitCommonControlsExPtr)(void *)GetProcAddress(comctl32, "InitCommonControlsEx"); |
| 7141 | |
| 7142 | // Fails if the incorrect version was loaded. Probably not a big enough deal to print an error about. |
| 7143 | if (init_common_controls_ex) { |
| 7144 | INITCOMMONCONTROLSEX icc = {}; |
| 7145 | icc.dwICC = ICC_STANDARD_CLASSES; |
| 7146 | icc.dwSize = sizeof(INITCOMMONCONTROLSEX); |
| 7147 | if (!init_common_controls_ex(&icc)) { |
| 7148 | WARN_PRINT("Unable to initialize Windows common controls. Native dialogs may not work properly."); |
| 7149 | } |
| 7150 | } |
| 7151 | FreeLibrary(comctl32); |
| 7152 | } |
| 7153 | |
| 7154 | OleInitialize(nullptr); |
| 7155 | |
| 7156 | HICON default_icon = LoadIconW(GetModuleHandle(nullptr), L"GODOT_ICON"); |
| 7157 | if (default_icon == nullptr) { |
| 7158 | default_icon = LoadIcon(nullptr, IDI_WINLOGO); |
| 7159 | } |
| 7160 | |
| 7161 | memset(&wc, 0, sizeof(WNDCLASSEXW)); |
| 7162 | wc.cbSize = sizeof(WNDCLASSEXW); |
| 7163 | wc.style = CS_OWNDC | CS_DBLCLKS; |
| 7164 | wc.lpfnWndProc = (WNDPROC)::WndProc; |
| 7165 | wc.cbClsExtra = 0; |
| 7166 | wc.cbWndExtra = 0; |
| 7167 | wc.hInstance = hInstance ? hInstance : GetModuleHandle(nullptr); |
| 7168 | wc.hIcon = default_icon; |
| 7169 | wc.hCursor = nullptr; |
| 7170 | wc.hbrBackground = nullptr; |
| 7171 | wc.lpszMenuName = nullptr; |
| 7172 | wc.lpszClassName = L"Engine"; |
| 7173 | |
| 7174 | if (!RegisterClassExW(&wc)) { |
| 7175 | r_error = ERR_UNAVAILABLE; |
| 7176 | return; |
| 7177 | } |
| 7178 | |
| 7179 | _register_raw_input_devices(INVALID_WINDOW_ID); |
| 7180 | |
| 7181 | String appname; |
| 7182 | if (Engine::get_singleton()->is_editor_hint()) { |
| 7183 | appname = "Godot.GodotEditor." + String(GODOT_VERSION_FULL_CONFIG); |
| 7184 | } else { |
| 7185 | String name = GLOBAL_GET("application/config/name"); |
| 7186 | String version = GLOBAL_GET("application/config/version"); |
| 7187 | if (version.is_empty()) { |
| 7188 | version = "0"; |
| 7189 | } |
| 7190 | String clean_app_name = name.to_pascal_case(); |
| 7191 | for (int i = 0; i < clean_app_name.length(); i++) { |
| 7192 | if (!is_ascii_alphanumeric_char(clean_app_name[i]) && clean_app_name[i] != '_' && clean_app_name[i] != '.') { |
| 7193 | clean_app_name[i] = '_'; |
| 7194 | } |
| 7195 | } |
| 7196 | clean_app_name = clean_app_name.substr(0, 120 - version.length()).trim_suffix("."); |
| 7197 | appname = "Godot." + clean_app_name + "." + version; |
| 7198 | |
| 7199 | #ifndef TOOLS_ENABLED |
| 7200 | // Set for exported projects only. |
| 7201 | HKEY key; |
| 7202 | if (RegOpenKeyW(HKEY_CURRENT_USER_LOCAL_SETTINGS, L"Software\\Microsoft\\Windows\\Shell\\MuiCache", &key) == ERROR_SUCCESS) { |
| 7203 | Char16String cs_name = name.utf16(); |
| 7204 | String value_name = OS::get_singleton()->get_executable_path().replace_char('/', '\\') + ".FriendlyAppName"; |
| 7205 | RegSetValueExW(key, (LPCWSTR)value_name.utf16().get_data(), 0, REG_SZ, (const BYTE *)cs_name.get_data(), cs_name.size() * sizeof(WCHAR)); |
| 7206 | RegCloseKey(key); |
| 7207 | } |
| 7208 | #endif |
| 7209 | } |
| 7210 | SetCurrentProcessExplicitAppUserModelID((PCWSTR)appname.utf16().get_data()); |
| 7211 | |
| 7212 | mouse_monitor = SetWindowsHookEx(WH_MOUSE, ::MouseProc, nullptr, GetCurrentThreadId()); |
| 7213 | |
| 7214 | Point2i window_position; |
| 7215 | if (p_position != nullptr) { |
| 7216 | window_position = *p_position; |
| 7217 | } else { |
| 7218 | if (p_screen == SCREEN_OF_MAIN_WINDOW) { |
| 7219 | p_screen = SCREEN_PRIMARY; |
| 7220 | } |
| 7221 | Rect2i scr_rect = screen_get_usable_rect(p_screen); |
| 7222 | window_position = scr_rect.position + (scr_rect.size - p_resolution) / 2; |
| 7223 | } |
| 7224 | |
| 7225 | HWND parent_hwnd = NULL; |
| 7226 | if (p_parent_window) { |
| 7227 | // Parented window. |
| 7228 | parent_hwnd = (HWND)p_parent_window; |
| 7229 | } |
| 7230 | |
| 7231 | // Init context and rendering device. |
| 7232 | if (rendering_driver == "dummy") { |
| 7233 | RasterizerDummy::make_current(); |
| 7234 | } |
| 7235 | |
| 7236 | #ifdef RD_ENABLED |
| 7237 | bool fallback_to_vulkan = GLOBAL_GET("rendering/rendering_device/fallback_to_vulkan"); |
| 7238 | bool fallback_to_d3d12 = GLOBAL_GET("rendering/rendering_device/fallback_to_d3d12"); |
| 7239 | |
| 7240 | #ifndef VULKAN_ENABLED |
| 7241 | fallback_to_d3d12 = true; // Always enable fallback if engine was built w/o other driver support. |
| 7242 | #endif |
| 7243 | #ifndef D3D12_ENABLED |
| 7244 | fallback_to_vulkan = true; // Always enable fallback if engine was built w/o other driver support. |
| 7245 | #endif |
| 7246 | |
| 7247 | String rendering_drivers[2]; |
| 7248 | uint32_t rendering_driver_count = 0; |
| 7249 | |
| 7250 | if (rendering_driver == "d3d12") { |
| 7251 | rendering_drivers[rendering_driver_count++] = rendering_driver; |
| 7252 | if (fallback_to_vulkan) { |
| 7253 | rendering_drivers[rendering_driver_count++] = "vulkan"; |
| 7254 | } |
| 7255 | } else if (rendering_driver == "vulkan") { |
| 7256 | rendering_drivers[rendering_driver_count++] = rendering_driver; |
| 7257 | if (fallback_to_d3d12) { |
| 7258 | rendering_drivers[rendering_driver_count++] = "d3d12"; |
| 7259 | } |
| 7260 | } |
| 7261 | |
| 7262 | bool main_window_created = false; |
| 7263 | bool cur_no_redirection_bitmap_value = false; |
| 7264 | |
| 7265 | for (uint32_t i = 0; i < rendering_driver_count; i++) { |
| 7266 | const String &tested_rendering_driver = rendering_drivers[i]; |
| 7267 | |
| 7268 | #ifdef VULKAN_ENABLED |
| 7269 | if (tested_rendering_driver == "vulkan") { |
| 7270 | rendering_context = memnew(RenderingContextDriverVulkanWindows); |
| 7271 | tested_drivers.set_flag(DRIVER_ID_RD_VULKAN); |
| 7272 | } |
| 7273 | #endif |
| 7274 | #ifdef D3D12_ENABLED |
| 7275 | if (tested_rendering_driver == "d3d12") { |
| 7276 | rendering_context = memnew(RenderingContextDriverD3D12); |
| 7277 | tested_drivers.set_flag(DRIVER_ID_RD_D3D12); |
| 7278 | } |
| 7279 | #endif |
| 7280 | if (rendering_context != nullptr) { |
| 7281 | if (rendering_context->initialize() == OK) { |
| 7282 | // The window needs to be recreated when this value differs, because it cannot be added or removed after creation. |
| 7283 | #ifdef DCOMP_ENABLED |
| 7284 | bool new_no_redirection_bitmap_value = OS::get_singleton()->is_layered_allowed() && tested_rendering_driver == "d3d12"; |
| 7285 | #else |
| 7286 | bool new_no_redirection_bitmap_value = false; |
| 7287 | #endif |
| 7288 | if (cur_no_redirection_bitmap_value != new_no_redirection_bitmap_value) { |
| 7289 | if (main_window_created) { |
| 7290 | _destroy_window(MAIN_WINDOW_ID); |
| 7291 | main_window_created = false; |
| 7292 | } |
| 7293 | cur_no_redirection_bitmap_value = new_no_redirection_bitmap_value; |
| 7294 | } |
| 7295 | |
| 7296 | if (!main_window_created) { |
| 7297 | if (_create_window(MAIN_WINDOW_ID, p_mode, p_flags, Rect2i(window_position, p_resolution), false, INVALID_WINDOW_ID, parent_hwnd, cur_no_redirection_bitmap_value) != OK) { |
| 7298 | r_error = ERR_UNAVAILABLE; |
| 7299 | ERR_FAIL_MSG("Failed to create main window."); |
| 7300 | } |
| 7301 | main_window_created = true; |
| 7302 | } |
| 7303 | |
| 7304 | if (_create_rendering_context_window(MAIN_WINDOW_ID, tested_rendering_driver) == OK) { |
| 7305 | rendering_device = memnew(RenderingDevice); |
| 7306 | if (rendering_device->initialize(rendering_context, MAIN_WINDOW_ID) == OK) { |
| 7307 | #ifdef VULKAN_ENABLED |
| 7308 | if (rendering_driver == "vulkan" && tested_rendering_driver == "d3d12") { |
| 7309 | WARN_PRINT("Your video card drivers seem not to support Vulkan, switching to Direct3D 12."); |
| 7310 | } |
| 7311 | #endif |
| 7312 | #ifdef D3D12_ENABLED |
| 7313 | if (rendering_driver == "d3d12" && tested_rendering_driver == "vulkan") { |
| 7314 | WARN_PRINT("Your video card drivers seem not to support Direct3D 12, switching to Vulkan."); |
| 7315 | } |
| 7316 | #endif |
| 7317 | rendering_driver = tested_rendering_driver; |
| 7318 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7319 | |
| 7320 | break; |
| 7321 | } |
| 7322 | |
| 7323 | memdelete(rendering_device); |
| 7324 | rendering_device = nullptr; |
| 7325 | |
| 7326 | _destroy_rendering_context_window(MAIN_WINDOW_ID); |
| 7327 | } |
| 7328 | } |
| 7329 | |
| 7330 | memdelete(rendering_context); |
| 7331 | rendering_context = nullptr; |
| 7332 | } |
| 7333 | } |
| 7334 | |
| 7335 | bool rendering_driver_failed = rendering_driver_count != 0 && rendering_context == nullptr; |
| 7336 | |
| 7337 | #ifdef GLES3_ENABLED |
| 7338 | if (rendering_driver_failed) { |
| 7339 | bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3"); |
| 7340 | if (fallback_to_opengl3) { |
| 7341 | tested_drivers.set_flag(DRIVER_ID_COMPAT_OPENGL3); |
| 7342 | WARN_PRINT("Your video card drivers seem not to support Direct3D 12 or Vulkan, switching to OpenGL 3."); |
| 7343 | rendering_driver = "opengl3"; |
| 7344 | OS::get_singleton()->set_current_rendering_method("gl_compatibility"); |
| 7345 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7346 | rendering_driver_failed = false; |
| 7347 | } |
| 7348 | } |
| 7349 | #endif |
| 7350 | |
| 7351 | if (rendering_driver_failed) { |
| 7352 | r_error = ERR_UNAVAILABLE; |
| 7353 | return; |
| 7354 | } |
| 7355 | #endif |
| 7356 | |
| 7357 | #if defined(GLES3_ENABLED) |
| 7358 | |
| 7359 | bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_angle"); |
| 7360 | bool show_warning = true; |
| 7361 | |
| 7362 | if (rendering_driver == "opengl3") { |
| 7363 | // There's no native OpenGL drivers on Windows for ARM, always enable fallback. |
| 7364 | #if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64) |
| 7365 | fallback = true; |
| 7366 | show_warning = false; |
| 7367 | #else |
| 7368 | typedef BOOL(WINAPI * IsWow64Process2Ptr)(HANDLE, USHORT *, USHORT *); |
| 7369 | |
| 7370 | IsWow64Process2Ptr IsWow64Process2 = (IsWow64Process2Ptr)(void *)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process2"); |
| 7371 | if (IsWow64Process2) { |
| 7372 | USHORT process_arch = 0; |
| 7373 | USHORT machine_arch = 0; |
| 7374 | if (!IsWow64Process2(GetCurrentProcess(), &process_arch, &machine_arch)) { |
| 7375 | machine_arch = 0; |
| 7376 | } |
| 7377 | if (machine_arch == 0xAA64) { |
| 7378 | fallback = true; |
| 7379 | show_warning = false; |
| 7380 | } |
| 7381 | } |
| 7382 | #endif |
| 7383 | } |
| 7384 | |
| 7385 | bool gl_supported = true; |
| 7386 | if (fallback && !is_wine && (rendering_driver == "opengl3")) { |
| 7387 | Dictionary gl_info = detect_wgl(); |
| 7388 | |
| 7389 | bool force_angle = false; |
| 7390 | gl_supported = gl_info["version"].operator int() >= 30003; |
| 7391 | |
| 7392 | Vector2i device_id = Vector2i(-1, -1); |
| 7393 | Array device_list = GLOBAL_GET("rendering/gl_compatibility/force_angle_on_devices"); |
| 7394 | for (int i = 0; i < device_list.size(); i++) { |
| 7395 | const Dictionary &device = device_list[i]; |
| 7396 | if (device.has("vendor") && device.has("name")) { |
| 7397 | const String &vendor = device["vendor"]; |
| 7398 | const String &name = device["name"]; |
| 7399 | if (gl_info["vendor"].operator String().containsn(vendor) && (name == "*" || gl_info["name"].operator String().containsn(name))) { |
| 7400 | // Check vendor/device names. |
| 7401 | force_angle = true; |
| 7402 | break; |
| 7403 | } else if (vendor.begins_with("0x") && name.begins_with("0x")) { |
| 7404 | if (device_id == Vector2i(-1, -1)) { |
| 7405 | // Load device IDs. |
| 7406 | device_id = _get_device_ids(gl_info["name"]); |
| 7407 | } |
| 7408 | if (device_id.x == vendor.lstrip("0x").hex_to_int() && device_id.y == name.lstrip("0x").hex_to_int()) { |
| 7409 | // Check vendor/device IDs. |
| 7410 | force_angle = true; |
| 7411 | break; |
| 7412 | } |
| 7413 | } |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | if (force_angle || (gl_info["version"].operator int() < 30003)) { |
| 7418 | tested_drivers.set_flag(DRIVER_ID_COMPAT_OPENGL3); |
| 7419 | if (show_warning) { |
| 7420 | if (gl_info["version"].operator int() < 30003) { |
| 7421 | WARN_PRINT("Your video card drivers seem not to support the required OpenGL 3.3 version, switching to ANGLE."); |
| 7422 | } else { |
| 7423 | WARN_PRINT("Your video card drivers are known to have low quality OpenGL 3.3 support, switching to ANGLE."); |
| 7424 | } |
| 7425 | } |
| 7426 | rendering_driver = "opengl3_angle"; |
| 7427 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | if (rendering_driver == "opengl3_angle") { |
| 7432 | gl_manager_angle = memnew(GLManagerANGLE_Windows); |
| 7433 | tested_drivers.set_flag(DRIVER_ID_COMPAT_ANGLE_D3D11); |
| 7434 | |
| 7435 | if (gl_manager_angle->initialize() != OK) { |
| 7436 | memdelete(gl_manager_angle); |
| 7437 | gl_manager_angle = nullptr; |
| 7438 | bool fallback_to_native = GLOBAL_GET("rendering/gl_compatibility/fallback_to_native"); |
| 7439 | if (fallback_to_native && gl_supported) { |
| 7440 | #ifdef EGL_STATIC |
| 7441 | WARN_PRINT("Your video card drivers seem not to support GLES3 / ANGLE, switching to native OpenGL."); |
| 7442 | #else |
| 7443 | WARN_PRINT("Your video card drivers seem not to support GLES3 / ANGLE or ANGLE dynamic libraries (libEGL.dll and libGLESv2.dll) are missing, switching to native OpenGL."); |
| 7444 | #endif |
| 7445 | rendering_driver = "opengl3"; |
| 7446 | OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 7447 | } else { |
| 7448 | r_error = ERR_UNAVAILABLE; |
| 7449 | ERR_FAIL_MSG("Could not initialize ANGLE OpenGL."); |
| 7450 | } |
| 7451 | } |
| 7452 | } |
| 7453 | if (rendering_driver == "opengl3") { |
| 7454 | gl_manager_native = memnew(GLManagerNative_Windows); |
| 7455 | tested_drivers.set_flag(DRIVER_ID_COMPAT_OPENGL3); |
| 7456 | |
| 7457 | if (gl_manager_native->initialize() != OK) { |
| 7458 | memdelete(gl_manager_native); |
| 7459 | gl_manager_native = nullptr; |
| 7460 | r_error = ERR_UNAVAILABLE; |
| 7461 | ERR_FAIL_MSG("Could not initialize native OpenGL."); |
| 7462 | } |
| 7463 | } |
| 7464 | #endif |
| 7465 | |
| 7466 | bool should_create_main_window = true; |
| 7467 | bool no_redirection_bitmap = false; |
| 7468 | |
| 7469 | #ifdef RD_ENABLED |
| 7470 | #ifdef DCOMP_ENABLED |
| 7471 | no_redirection_bitmap = OS::get_singleton()->is_layered_allowed() && rendering_driver == "d3d12"; |
| 7472 | #endif |
| 7473 | |
| 7474 | // The window may still need to be recreated when all RD backends fail and it falls back to OpenGL. |
| 7475 | if (main_window_created) { |
| 7476 | if (no_redirection_bitmap != cur_no_redirection_bitmap_value) { |
| 7477 | DEV_ASSERT(rendering_context == nullptr); |
| 7478 | _destroy_window(MAIN_WINDOW_ID); |
| 7479 | } else { |
| 7480 | should_create_main_window = false; |
| 7481 | } |
| 7482 | } |
| 7483 | #endif |
| 7484 | |
| 7485 | if (should_create_main_window) { |
| 7486 | if (_create_window(MAIN_WINDOW_ID, p_mode, p_flags, Rect2i(window_position, p_resolution), false, INVALID_WINDOW_ID, parent_hwnd, no_redirection_bitmap) != OK) { |
| 7487 | r_error = ERR_UNAVAILABLE; |
| 7488 | ERR_FAIL_MSG("Failed to create main window."); |
| 7489 | } |
| 7490 | } |
| 7491 | ++window_id_counter; |
| 7492 | |
| 7493 | #ifdef GLES3_ENABLED |
| 7494 | if (rendering_driver == "opengl3") { |
| 7495 | if (_create_gl_window(MAIN_WINDOW_ID) != OK) { |
| 7496 | memdelete(gl_manager_native); |
| 7497 | gl_manager_native = nullptr; |
| 7498 | windows.erase(MAIN_WINDOW_ID); |
| 7499 | r_error = ERR_UNAVAILABLE; |
| 7500 | return; |
| 7501 | } |
| 7502 | RasterizerGLES3::make_current(true); |
| 7503 | } |
| 7504 | if (rendering_driver == "opengl3_angle") { |
| 7505 | if (_create_gl_window(MAIN_WINDOW_ID) != OK) { |
| 7506 | memdelete(gl_manager_angle); |
| 7507 | gl_manager_angle = nullptr; |
| 7508 | windows.erase(MAIN_WINDOW_ID); |
| 7509 | r_error = ERR_UNAVAILABLE; |
| 7510 | return; |
| 7511 | } |
| 7512 | RasterizerGLES3::make_current(false); |
| 7513 | } |
| 7514 | #endif |
| 7515 | |
| 7516 | window_set_vsync_mode(p_vsync_mode, MAIN_WINDOW_ID); |
| 7517 | |
| 7518 | #ifdef SDL_ENABLED |
| 7519 | joypad_sdl = memnew(JoypadSDL); |
| 7520 | if (joypad_sdl->initialize() != OK) { |
| 7521 | ERR_PRINT("Couldn't initialize SDL joypad input driver."); |
| 7522 | memdelete(joypad_sdl); |
| 7523 | joypad_sdl = nullptr; |
| 7524 | } |
| 7525 | #endif |
| 7526 | |
| 7527 | for (int i = 0; i < WINDOW_FLAG_MAX; i++) { |
| 7528 | if (p_flags & (1 << i)) { |
| 7529 | window_set_flag(WindowFlags(i), true, MAIN_WINDOW_ID); |
| 7530 | } |
| 7531 | } |
| 7532 | |
| 7533 | windows[MAIN_WINDOW_ID].initialized = true; |
| 7534 | |
| 7535 | #ifdef ACCESSKIT_ENABLED |
| 7536 | if (accessibility_screen_reader_active()) { |
| 7537 | _THREAD_SAFE_LOCK_ |
| 7538 | uint64_t time_wait = OS::get_singleton()->get_ticks_msec(); |
| 7539 | while (true) { |
| 7540 | MSG msg = {}; |
| 7541 | while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { |
| 7542 | TranslateMessage(&msg); |
| 7543 | DispatchMessageW(&msg); |
| 7544 | } |
| 7545 | |
| 7546 | uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_wait; |
| 7547 | if (delta > 500 || get_object_received) { |
| 7548 | break; |
| 7549 | } |
| 7550 | } |
| 7551 | _THREAD_SAFE_UNLOCK_ |
| 7552 | } |
| 7553 | #endif |
| 7554 | |
| 7555 | #if defined(RD_ENABLED) |
| 7556 | if (rendering_context) { |
| 7557 | DEV_ASSERT(rendering_device != nullptr); |
| 7558 | |
| 7559 | rendering_device->screen_create(MAIN_WINDOW_ID); |
| 7560 | |
| 7561 | RendererCompositorRD::make_current(); |
| 7562 | } |
| 7563 | #endif |
| 7564 | |
| 7565 | if (!Engine::get_singleton()->is_editor_hint() && !OS::get_singleton()->is_in_low_processor_usage_mode()) { |
| 7566 | // Increase priority for projects that are not in low-processor mode (typically games) |
| 7567 | // to reduce the risk of frame stuttering. |
| 7568 | // This is not done for the editor to prevent importers or resource bakers |
| 7569 | // from making the system unresponsive. |
| 7570 | SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); |
| 7571 | DWORD index = 0; |
| 7572 | HANDLE handle = AvSetMmThreadCharacteristicsW(L"Games", &index); |
| 7573 | if (handle) { |
| 7574 | AvSetMmThreadPriority(handle, AVRT_PRIORITY_CRITICAL); |
| 7575 | } |
| 7576 | |
| 7577 | // This is needed to make sure that background work does not starve the main thread. |
| 7578 | // This is only setting the priority of this thread, not the whole process. |
| 7579 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
| 7580 | } |
| 7581 | |
| 7582 | cursor_shape = CURSOR_ARROW; |
| 7583 | |
| 7584 | _update_real_mouse_position(MAIN_WINDOW_ID); |
| 7585 | |
| 7586 | r_error = OK; |
| 7587 | |
| 7588 | static_cast<OS_Windows *>(OS::get_singleton())->set_main_window(windows[MAIN_WINDOW_ID].hWnd); |
| 7589 | Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events); |
| 7590 | } |
| 7591 | |
| 7592 | Vector<String> DisplayServerWindows::get_rendering_drivers_func() { |
| 7593 | Vector<String> drivers; |
| 7594 | |
| 7595 | #ifdef VULKAN_ENABLED |
| 7596 | drivers.push_back("vulkan"); |
| 7597 | #endif |
| 7598 | #ifdef D3D12_ENABLED |
| 7599 | drivers.push_back("d3d12"); |
| 7600 | #endif |
| 7601 | #ifdef GLES3_ENABLED |
| 7602 | drivers.push_back("opengl3"); |
| 7603 | drivers.push_back("opengl3_angle"); |
| 7604 | #endif |
| 7605 | drivers.push_back("dummy"); |
| 7606 | |
| 7607 | return drivers; |
| 7608 | } |
| 7609 | |
| 7610 | DisplayServer *DisplayServerWindows::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 7611 | DisplayServer *ds = memnew(DisplayServerWindows(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error)); |
| 7612 | if (r_error != OK) { |
| 7613 | if (tested_drivers == 0) { |
| 7614 | OS::get_singleton()->alert("Failed to register the window class.", "Unable to initialize DisplayServer"); |
| 7615 | } else if (tested_drivers.has_flag(DRIVER_ID_RD_VULKAN) || tested_drivers.has_flag(DRIVER_ID_RD_D3D12)) { |
| 7616 | Vector<String> drivers; |
| 7617 | if (tested_drivers.has_flag(DRIVER_ID_RD_VULKAN)) { |
| 7618 | drivers.push_back("Vulkan"); |
| 7619 | } |
| 7620 | if (tested_drivers.has_flag(DRIVER_ID_RD_D3D12)) { |
| 7621 | drivers.push_back("Direct3D 12"); |
| 7622 | } |
| 7623 | String executable_name = OS::get_singleton()->get_executable_path().get_file(); |
| 7624 | OS::get_singleton()->alert( |
| 7625 | vformat("Your video card drivers seem not to support the required %s version.\n\n" |
| 7626 | "If possible, consider updating your video card drivers or using the OpenGL 3 driver.\n\n" |
| 7627 | "You can enable the OpenGL 3 driver by starting the engine from the\n" |
| 7628 | "command line with the command:\n\n \"%s\" --rendering-driver opengl3\n\n" |
| 7629 | "If you have recently updated your video card drivers, try rebooting.", |
| 7630 | String(" or ").join(drivers), |
| 7631 | executable_name), |
| 7632 | "Unable to initialize video driver"); |
| 7633 | } else { |
| 7634 | Vector<String> drivers; |
| 7635 | if (tested_drivers.has_flag(DRIVER_ID_COMPAT_OPENGL3)) { |
| 7636 | drivers.push_back("OpenGL 3.3"); |
| 7637 | } |
| 7638 | if (tested_drivers.has_flag(DRIVER_ID_COMPAT_ANGLE_D3D11)) { |
| 7639 | drivers.push_back("Direct3D 11"); |
| 7640 | } |
| 7641 | OS::get_singleton()->alert( |
| 7642 | vformat( |
| 7643 | "Your video card drivers seem not to support the required %s version.\n\n" |
| 7644 | "If possible, consider updating your video card drivers.\n\n" |
| 7645 | "If you have recently updated your video card drivers, try rebooting.", |
| 7646 | String(" or ").join(drivers)), |
| 7647 | "Unable to initialize video driver"); |
| 7648 | } |
| 7649 | } |
| 7650 | return ds; |
| 7651 | } |
| 7652 | |
| 7653 | void DisplayServerWindows::register_windows_driver() { |
| 7654 | register_create_function("windows", create_func, get_rendering_drivers_func); |
| 7655 | } |
| 7656 | |
| 7657 | DisplayServerWindows::~DisplayServerWindows() { |
| 7658 | LocalVector<List<FileDialogData *>::Element *> to_remove; |
| 7659 | for (List<FileDialogData *>::Element *E = file_dialogs.front(); E; E = E->next()) { |
| 7660 | FileDialogData *fd = E->get(); |
| 7661 | if (fd->listener_thread.is_started()) { |
| 7662 | fd->close_requested.set(); |
| 7663 | fd->listener_thread.wait_to_finish(); |
| 7664 | } |
| 7665 | to_remove.push_back(E); |
| 7666 | } |
| 7667 | for (List<FileDialogData *>::Element *E : to_remove) { |
| 7668 | memdelete(E->get()); |
| 7669 | E->erase(); |
| 7670 | } |
| 7671 | |
| 7672 | #ifdef SDL_ENABLED |
| 7673 | if (joypad_sdl) { |
| 7674 | memdelete(joypad_sdl); |
| 7675 | } |
| 7676 | #endif |
| 7677 | touch_state.clear(); |
| 7678 | |
| 7679 | cursors_cache.clear(); |
| 7680 | |
| 7681 | // Destroy all status indicators. |
| 7682 | for (HashMap<IndicatorID, IndicatorData>::Iterator E = indicators.begin(); E; ++E) { |
| 7683 | NOTIFYICONDATAW ndat; |
| 7684 | ZeroMemory(&ndat, sizeof(NOTIFYICONDATAW)); |
| 7685 | ndat.cbSize = sizeof(NOTIFYICONDATAW); |
| 7686 | ndat.hWnd = windows[MAIN_WINDOW_ID].hWnd; |
| 7687 | ndat.uID = E->key; |
| 7688 | ndat.uVersion = NOTIFYICON_VERSION; |
| 7689 | |
| 7690 | if (E->value.icon) { |
| 7691 | DestroyIcon(E->value.icon); |
| 7692 | E->value.icon_buffer.clear(); |
| 7693 | E->value.icon = nullptr; |
| 7694 | } |
| 7695 | |
| 7696 | Shell_NotifyIconW(NIM_DELETE, &ndat); |
| 7697 | } |
| 7698 | |
| 7699 | if (mouse_monitor) { |
| 7700 | UnhookWindowsHookEx(mouse_monitor); |
| 7701 | } |
| 7702 | |
| 7703 | if (user_proc) { |
| 7704 | SetWindowLongPtr(windows[MAIN_WINDOW_ID].hWnd, GWLP_WNDPROC, (LONG_PTR)user_proc); |
| 7705 | } |
| 7706 | |
| 7707 | // Close power request handle. |
| 7708 | screen_set_keep_on(false); |
| 7709 | |
| 7710 | if (native_menu) { |
| 7711 | memdelete(native_menu); |
| 7712 | native_menu = nullptr; |
| 7713 | } |
| 7714 | |
| 7715 | #ifdef GLES3_ENABLED |
| 7716 | // destroy windows .. NYI? |
| 7717 | // FIXME wglDeleteContext is never called |
| 7718 | #endif |
| 7719 | |
| 7720 | if (windows.has(MAIN_WINDOW_ID)) { |
| 7721 | #ifdef RD_ENABLED |
| 7722 | if (rendering_device) { |
| 7723 | rendering_device->screen_free(MAIN_WINDOW_ID); |
| 7724 | } |
| 7725 | |
| 7726 | if (rendering_context) { |
| 7727 | rendering_context->window_destroy(MAIN_WINDOW_ID); |
| 7728 | } |
| 7729 | #endif |
| 7730 | _destroy_window(MAIN_WINDOW_ID); |
| 7731 | } |
| 7732 | |
| 7733 | #ifdef RD_ENABLED |
| 7734 | if (rendering_device) { |
| 7735 | memdelete(rendering_device); |
| 7736 | rendering_device = nullptr; |
| 7737 | } |
| 7738 | |
| 7739 | if (rendering_context) { |
| 7740 | memdelete(rendering_context); |
| 7741 | rendering_context = nullptr; |
| 7742 | } |
| 7743 | #endif |
| 7744 | |
| 7745 | if (icon_big) { |
| 7746 | DestroyIcon(icon_big); |
| 7747 | icon_buffer_big.clear(); |
| 7748 | icon_big = nullptr; |
| 7749 | } |
| 7750 | if (icon_small) { |
| 7751 | DestroyIcon(icon_small); |
| 7752 | icon_buffer_small.clear(); |
| 7753 | icon_small = nullptr; |
| 7754 | } |
| 7755 | |
| 7756 | if (restore_mouse_trails > 1) { |
| 7757 | SystemParametersInfoA(SPI_SETMOUSETRAILS, restore_mouse_trails, nullptr, 0); |
| 7758 | } |
| 7759 | #ifdef GLES3_ENABLED |
| 7760 | if (gl_manager_angle) { |
| 7761 | memdelete(gl_manager_angle); |
| 7762 | gl_manager_angle = nullptr; |
| 7763 | } |
| 7764 | if (gl_manager_native) { |
| 7765 | memdelete(gl_manager_native); |
| 7766 | gl_manager_native = nullptr; |
| 7767 | } |
| 7768 | #endif |
| 7769 | #ifdef ACCESSKIT_ENABLED |
| 7770 | if (accessibility_driver) { |
| 7771 | memdelete(accessibility_driver); |
| 7772 | } |
| 7773 | #endif |
| 7774 | if (tts) { |
| 7775 | memdelete(tts); |
| 7776 | } |
| 7777 | |
| 7778 | OleUninitialize(); |
| 7779 | } |