| 1 |
/**************************************************************************/ |
| 2 |
/* display_server_macos.mm */ |
| 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 |
#import "display_server_macos.h" |
| 32 |
|
| 33 |
#import "godot_application.h" |
| 34 |
#import "godot_application_delegate.h" |
| 35 |
#import "godot_button_view.h" |
| 36 |
#import "godot_content_view.h" |
| 37 |
#import "godot_core_cursor.h" |
| 38 |
#import "godot_menu_delegate.h" |
| 39 |
#import "godot_menu_item.h" |
| 40 |
#import "godot_open_save_delegate.h" |
| 41 |
#import "godot_status_item.h" |
| 42 |
#import "godot_window.h" |
| 43 |
#import "godot_window_delegate.h" |
| 44 |
#import "key_mapping_macos.h" |
| 45 |
#import "os_macos.h" |
| 46 |
|
| 47 |
#ifdef TOOLS_ENABLED |
| 48 |
#import "macos_quartz_core_spi.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#include "core/config/project_settings.h" |
| 52 |
#include "core/io/file_access.h" |
| 53 |
#include "core/io/marshalls.h" |
| 54 |
#include "core/math/geometry_2d.h" |
| 55 |
#include "core/os/keyboard.h" |
| 56 |
#include "core/os/main_loop.h" |
| 57 |
#include "drivers/png/png_driver_common.h" |
| 58 |
#include "main/main.h" |
| 59 |
#include "scene/resources/image_texture.h" |
| 60 |
|
| 61 |
#ifdef TOOLS_ENABLED |
| 62 |
#import "display_server_embedded.h" |
| 63 |
#import "editor/embedded_process_macos.h" |
| 64 |
#endif |
| 65 |
|
| 66 |
#include <AppKit/AppKit.h> |
| 67 |
|
| 68 |
#include "servers/rendering/dummy/rasterizer_dummy.h" |
| 69 |
|
| 70 |
#if defined(GLES3_ENABLED) |
| 71 |
#include "drivers/gles3/rasterizer_gles3.h" |
| 72 |
#endif |
| 73 |
|
| 74 |
#if defined(RD_ENABLED) |
| 75 |
#include "servers/rendering/renderer_rd/renderer_compositor_rd.h" |
| 76 |
#endif |
| 77 |
|
| 78 |
#if defined(ACCESSKIT_ENABLED) |
| 79 |
#include "drivers/accesskit/accessibility_driver_accesskit.h" |
| 80 |
#endif |
| 81 |
|
| 82 |
#import <Carbon/Carbon.h> |
| 83 |
#import <Cocoa/Cocoa.h> |
| 84 |
#import <IOKit/IOCFPlugIn.h> |
| 85 |
#import <IOKit/IOKitLib.h> |
| 86 |
#import <IOKit/hid/IOHIDKeys.h> |
| 87 |
#import <IOKit/hid/IOHIDLib.h> |
| 88 |
|
| 89 |
DisplayServerMacOS::WindowID DisplayServerMacOS::_create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect) { |
| 90 |
const float scale = screen_get_max_scale(); |
| 91 |
|
| 92 |
WindowID id = window_id_counter; |
| 93 |
{ |
| 94 |
WindowData &wd = windows[id]; |
| 95 |
|
| 96 |
wd.window_delegate = [[GodotWindowDelegate alloc] initWithDisplayServer:this]; |
| 97 |
ERR_FAIL_NULL_V_MSG(wd.window_delegate, INVALID_WINDOW_ID, "Can't create a window delegate"); |
| 98 |
[wd.window_delegate setWindowID:id]; |
| 99 |
|
| 100 |
int rq_screen = get_screen_from_rect(p_rect); |
| 101 |
if (rq_screen < 0) { |
| 102 |
rq_screen = get_primary_screen(); // Requested window rect is outside any screen bounds. |
| 103 |
} |
| 104 |
|
| 105 |
Rect2i srect = screen_get_usable_rect(rq_screen); |
| 106 |
Point2i wpos = p_rect.position; |
| 107 |
if (srect != Rect2i()) { |
| 108 |
wpos = wpos.clamp(srect.position, srect.position + srect.size - p_rect.size / 3); |
| 109 |
} |
| 110 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 111 |
// Godot passes a positive value. |
| 112 |
wpos.y *= -1; |
| 113 |
wpos += _get_screens_origin(); |
| 114 |
wpos /= scale; |
| 115 |
|
| 116 |
// initWithContentRect uses bottom-left corner of the window’s frame as origin. |
| 117 |
wd.window_object = [[GodotWindow alloc] |
| 118 |
initWithContentRect:NSMakeRect(100, 100, MAX(1, p_rect.size.width / scale), MAX(1, p_rect.size.height / scale)) |
| 119 |
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable |
| 120 |
backing:NSBackingStoreBuffered |
| 121 |
defer:NO]; |
| 122 |
ERR_FAIL_NULL_V_MSG(wd.window_object, INVALID_WINDOW_ID, "Can't create a window"); |
| 123 |
[wd.window_object setWindowID:id]; |
| 124 |
[wd.window_object setReleasedWhenClosed:NO]; |
| 125 |
|
| 126 |
wd.window_view = [[GodotContentView alloc] init]; |
| 127 |
if (wd.window_view == nil) { |
| 128 |
windows.erase(id); |
| 129 |
ERR_FAIL_V_MSG(INVALID_WINDOW_ID, "Can't create a window view"); |
| 130 |
} |
| 131 |
[wd.window_view setWindowID:id]; |
| 132 |
[wd.window_view setWantsLayer:TRUE]; |
| 133 |
|
| 134 |
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; |
| 135 |
[wd.window_object setContentView:wd.window_view]; |
| 136 |
[wd.window_object setDelegate:wd.window_delegate]; |
| 137 |
[wd.window_object setAcceptsMouseMovedEvents:YES]; |
| 138 |
[wd.window_object setRestorable:NO]; |
| 139 |
[wd.window_object setColorSpace:[NSColorSpace sRGBColorSpace]]; |
| 140 |
|
| 141 |
#ifdef ACCESSKIT_ENABLED |
| 142 |
if (accessibility_driver && !accessibility_driver->window_create(id, (__bridge void *)wd.window_object)) { |
| 143 |
if (OS::get_singleton()->is_stdout_verbose()) { |
| 144 |
ERR_PRINT("Can't create an accessibility adapter for window, accessibility support disabled!"); |
| 145 |
} |
| 146 |
memdelete(accessibility_driver); |
| 147 |
accessibility_driver = nullptr; |
| 148 |
} |
| 149 |
#endif |
| 150 |
|
| 151 |
if ([wd.window_object respondsToSelector:@selector(setTabbingMode:)]) { |
| 152 |
[wd.window_object setTabbingMode:NSWindowTabbingModeDisallowed]; |
| 153 |
} |
| 154 |
|
| 155 |
CALayer *layer = [wd.window_view layer]; |
| 156 |
if (layer) { |
| 157 |
layer.contentsScale = scale; |
| 158 |
} |
| 159 |
|
| 160 |
NSColor *bg_color = [NSColor windowBackgroundColor]; |
| 161 |
Color _bg_color; |
| 162 |
if (_get_window_early_clear_override(_bg_color)) { |
| 163 |
bg_color = [NSColor colorWithCalibratedRed:_bg_color.r green:_bg_color.g blue:_bg_color.b alpha:1.f]; |
| 164 |
} |
| 165 |
|
| 166 |
[wd.window_object setBackgroundColor:bg_color]; |
| 167 |
if (layer) { |
| 168 |
[layer setBackgroundColor:bg_color.CGColor]; |
| 169 |
} |
| 170 |
|
| 171 |
#if defined(RD_ENABLED) |
| 172 |
if (rendering_context) { |
| 173 |
union { |
| 174 |
#ifdef VULKAN_ENABLED |
| 175 |
RenderingContextDriverVulkanMacOS::WindowPlatformData vulkan; |
| 176 |
#endif |
| 177 |
#ifdef METAL_ENABLED |
| 178 |
RenderingContextDriverMetal::WindowPlatformData metal; |
| 179 |
#endif |
| 180 |
} wpd; |
| 181 |
#ifdef VULKAN_ENABLED |
| 182 |
if (rendering_driver == "vulkan") { |
| 183 |
wpd.vulkan.layer_ptr = (CAMetalLayer *const *)&layer; |
| 184 |
} |
| 185 |
#endif |
| 186 |
#ifdef METAL_ENABLED |
| 187 |
if (rendering_driver == "metal") { |
| 188 |
wpd.metal.layer = (CAMetalLayer *)layer; |
| 189 |
} |
| 190 |
#endif |
| 191 |
Error err = rendering_context->window_create(window_id_counter, &wpd); |
| 192 |
#ifdef ACCESSKIT_ENABLED |
| 193 |
if (err != OK && accessibility_driver) { |
| 194 |
accessibility_driver->window_destroy(id); |
| 195 |
} |
| 196 |
#endif |
| 197 |
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, vformat("Can't create a %s context", rendering_driver)); |
| 198 |
|
| 199 |
rendering_context->window_set_size(window_id_counter, p_rect.size.width, p_rect.size.height); |
| 200 |
rendering_context->window_set_vsync_mode(window_id_counter, p_vsync_mode); |
| 201 |
} |
| 202 |
#endif |
| 203 |
|
| 204 |
#if defined(GLES3_ENABLED) |
| 205 |
bool gl_failed = false; |
| 206 |
if (gl_manager_legacy) { |
| 207 |
Error err = gl_manager_legacy->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height); |
| 208 |
if (err != OK) { |
| 209 |
gl_failed = true; |
| 210 |
} |
| 211 |
} |
| 212 |
if (gl_manager_angle) { |
| 213 |
Error err = gl_manager_angle->window_create(window_id_counter, nullptr, (__bridge void *)layer, p_rect.size.width, p_rect.size.height); |
| 214 |
if (err != OK) { |
| 215 |
gl_failed = true; |
| 216 |
} |
| 217 |
} |
| 218 |
if (gl_failed) { |
| 219 |
#ifdef ACCESSKIT_ENABLED |
| 220 |
if (accessibility_driver) { |
| 221 |
accessibility_driver->window_destroy(id); |
| 222 |
} |
| 223 |
#endif |
| 224 |
windows.erase(id); |
| 225 |
ERR_FAIL_V_MSG(INVALID_WINDOW_ID, "Can't create an OpenGL context."); |
| 226 |
} |
| 227 |
window_set_vsync_mode(p_vsync_mode, id); |
| 228 |
#endif |
| 229 |
[wd.window_view updateLayerDelegate]; |
| 230 |
|
| 231 |
const NSRect contentRect = [wd.window_view frame]; |
| 232 |
const NSRect windowRect = [wd.window_object frame]; |
| 233 |
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect]; |
| 234 |
Point2i offset; |
| 235 |
offset.x = (nsrect.origin.x - windowRect.origin.x); |
| 236 |
offset.y = (nsrect.origin.y + nsrect.size.height); |
| 237 |
offset.y -= (windowRect.origin.y + windowRect.size.height); |
| 238 |
[wd.window_object setFrameTopLeftPoint:NSMakePoint(wpos.x - offset.x, wpos.y - offset.y)]; |
| 239 |
} |
| 240 |
window_id_counter++; |
| 241 |
|
| 242 |
WindowData &wd = windows[id]; |
| 243 |
window_set_mode(p_mode, id); |
| 244 |
|
| 245 |
const NSRect contentRect = [wd.window_view frame]; |
| 246 |
wd.size.width = contentRect.size.width * scale; |
| 247 |
wd.size.height = contentRect.size.height * scale; |
| 248 |
|
| 249 |
CALayer *layer = [(NSView *)wd.window_view layer]; |
| 250 |
if (layer) { |
| 251 |
layer.contentsScale = scale; |
| 252 |
} |
| 253 |
|
| 254 |
#if defined(GLES3_ENABLED) |
| 255 |
if (gl_manager_legacy) { |
| 256 |
gl_manager_legacy->window_resize(id, wd.size.width, wd.size.height); |
| 257 |
} |
| 258 |
if (gl_manager_angle) { |
| 259 |
gl_manager_angle->window_resize(id, wd.size.width, wd.size.height); |
| 260 |
} |
| 261 |
#endif |
| 262 |
|
| 263 |
return id; |
| 264 |
} |
| 265 |
|
| 266 |
void DisplayServerMacOS::_update_window_style(WindowData p_wd, WindowID p_window) { |
| 267 |
bool borderless_full = false; |
| 268 |
|
| 269 |
if (p_wd.borderless) { |
| 270 |
NSRect frameRect = [p_wd.window_object frame]; |
| 271 |
NSRect screenRect = [[p_wd.window_object screen] frame]; |
| 272 |
|
| 273 |
// Check if our window covers up the screen. |
| 274 |
if (frameRect.origin.x <= screenRect.origin.x && frameRect.origin.y <= frameRect.origin.y && |
| 275 |
frameRect.size.width >= screenRect.size.width && frameRect.size.height >= screenRect.size.height) { |
| 276 |
borderless_full = true; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if (borderless_full) { |
| 281 |
// If the window covers up the screen set the level to above the main menu and hide on deactivate. |
| 282 |
[(NSWindow *)p_wd.window_object setLevel:NSMainMenuWindowLevel + 1]; |
| 283 |
[(NSWindow *)p_wd.window_object setHidesOnDeactivate:YES]; |
| 284 |
} else { |
| 285 |
// Reset these when our window is not a borderless window that covers up the screen. |
| 286 |
if (is_always_on_top_recursive(p_window) && !p_wd.fullscreen) { |
| 287 |
[(NSWindow *)p_wd.window_object setLevel:NSFloatingWindowLevel]; |
| 288 |
} else { |
| 289 |
[(NSWindow *)p_wd.window_object setLevel:NSNormalWindowLevel]; |
| 290 |
} |
| 291 |
[(NSWindow *)p_wd.window_object setHidesOnDeactivate:NO]; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
bool DisplayServerMacOS::is_always_on_top_recursive(WindowID p_window) const { |
| 296 |
ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 297 |
|
| 298 |
const WindowData &wd = windows[p_window]; |
| 299 |
if (wd.on_top) { |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 304 |
return is_always_on_top_recursive(wd.transient_parent); |
| 305 |
} |
| 306 |
|
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
void DisplayServerMacOS::set_window_per_pixel_transparency_enabled(bool p_enabled, WindowID p_window) { |
| 311 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 312 |
WindowData &wd = windows[p_window]; |
| 313 |
|
| 314 |
if (!OS::get_singleton()->is_layered_allowed()) { |
| 315 |
return; |
| 316 |
} |
| 317 |
if (p_enabled) { |
| 318 |
[wd.window_object setOpaque:NO]; |
| 319 |
if (wd.borderless) { |
| 320 |
[wd.window_object setBackgroundColor:[NSColor clearColor]]; |
| 321 |
[wd.window_object setHasShadow:NO]; |
| 322 |
} else { |
| 323 |
// Note: transparent and not borderless - set alpha to non-zero value to ensure window shadow is rendered correctly. |
| 324 |
[wd.window_object setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.004f]]; |
| 325 |
[wd.window_object setHasShadow:YES]; |
| 326 |
} |
| 327 |
CALayer *layer = [(NSView *)wd.window_view layer]; |
| 328 |
if (layer) { |
| 329 |
[layer setBackgroundColor:[NSColor clearColor].CGColor]; |
| 330 |
[layer setOpaque:NO]; |
| 331 |
} |
| 332 |
#if defined(GLES3_ENABLED) |
| 333 |
if (gl_manager_legacy) { |
| 334 |
gl_manager_legacy->window_set_per_pixel_transparency_enabled(p_window, true); |
| 335 |
} |
| 336 |
#endif |
| 337 |
} else { |
| 338 |
NSColor *bg_color = [NSColor windowBackgroundColor]; |
| 339 |
Color _bg_color; |
| 340 |
if (_get_window_early_clear_override(_bg_color)) { |
| 341 |
bg_color = [NSColor colorWithCalibratedRed:_bg_color.r green:_bg_color.g blue:_bg_color.b alpha:1.f]; |
| 342 |
} |
| 343 |
[wd.window_object setBackgroundColor:bg_color]; |
| 344 |
[wd.window_object setOpaque:YES]; |
| 345 |
if (!wd.borderless) { |
| 346 |
[wd.window_object setHasShadow:YES]; |
| 347 |
} |
| 348 |
CALayer *layer = [(NSView *)wd.window_view layer]; |
| 349 |
if (layer) { |
| 350 |
[layer setBackgroundColor:bg_color.CGColor]; |
| 351 |
[layer setOpaque:YES]; |
| 352 |
} |
| 353 |
#if defined(GLES3_ENABLED) |
| 354 |
if (gl_manager_legacy) { |
| 355 |
gl_manager_legacy->window_set_per_pixel_transparency_enabled(p_window, false); |
| 356 |
} |
| 357 |
#endif |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void DisplayServerMacOS::_update_displays_arrangement() const { |
| 362 |
origin = Point2i(); |
| 363 |
|
| 364 |
for (int i = 0; i < get_screen_count(); i++) { |
| 365 |
Point2i position = _get_native_screen_position(i); |
| 366 |
if (position.x < origin.x) { |
| 367 |
origin.x = position.x; |
| 368 |
} |
| 369 |
if (position.y > origin.y) { |
| 370 |
origin.y = position.y; |
| 371 |
} |
| 372 |
} |
| 373 |
displays_arrangement_dirty = false; |
| 374 |
} |
| 375 |
|
| 376 |
void DisplayServerMacOS::set_menu_delegate(NSMenu *p_menu) { |
| 377 |
[p_menu setDelegate:menu_delegate]; |
| 378 |
} |
| 379 |
|
| 380 |
Point2i DisplayServerMacOS::_get_screens_origin() const { |
| 381 |
// Returns the native top-left screen coordinate of the smallest rectangle |
| 382 |
// that encompasses all screens. Needed in get_screen_position(), |
| 383 |
// window_get_position, and window_set_position() |
| 384 |
// to convert between macOS native screen coordinates and the ones expected by Godot. |
| 385 |
|
| 386 |
if (displays_arrangement_dirty) { |
| 387 |
_update_displays_arrangement(); |
| 388 |
} |
| 389 |
|
| 390 |
return origin; |
| 391 |
} |
| 392 |
|
| 393 |
Point2i DisplayServerMacOS::_get_native_screen_position(int p_screen) const { |
| 394 |
NSArray *screenArray = [NSScreen screens]; |
| 395 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 396 |
NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame]; |
| 397 |
// Return the top-left corner of the screen, for macOS the y starts at the bottom. |
| 398 |
return Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * screen_get_max_scale(); |
| 399 |
} |
| 400 |
|
| 401 |
return Point2i(); |
| 402 |
} |
| 403 |
|
| 404 |
void DisplayServerMacOS::_displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info) { |
| 405 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 406 |
if (ds) { |
| 407 |
ds->displays_arrangement_dirty = true; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
DisplayServer::WindowID DisplayServerMacOS::_get_focused_window_or_popup() const { |
| 412 |
const List<WindowID>::Element *E = popup_list.back(); |
| 413 |
if (E) { |
| 414 |
return E->get(); |
| 415 |
} |
| 416 |
|
| 417 |
return last_focused_window; |
| 418 |
} |
| 419 |
|
| 420 |
void DisplayServerMacOS::mouse_enter_window(WindowID p_window) { |
| 421 |
if (window_mouseover_id != p_window) { |
| 422 |
if (window_mouseover_id != INVALID_WINDOW_ID) { |
| 423 |
send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); |
| 424 |
} |
| 425 |
window_mouseover_id = p_window; |
| 426 |
if (p_window != INVALID_WINDOW_ID) { |
| 427 |
send_window_event(windows[p_window], WINDOW_EVENT_MOUSE_ENTER); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
void DisplayServerMacOS::mouse_exit_window(WindowID p_window) { |
| 433 |
if (window_mouseover_id == p_window && p_window != INVALID_WINDOW_ID) { |
| 434 |
send_window_event(windows[p_window], WINDOW_EVENT_MOUSE_EXIT); |
| 435 |
} |
| 436 |
window_mouseover_id = INVALID_WINDOW_ID; |
| 437 |
} |
| 438 |
|
| 439 |
void DisplayServerMacOS::_dispatch_input_events(const Ref<InputEvent> &p_event) { |
| 440 |
((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event); |
| 441 |
} |
| 442 |
|
| 443 |
void DisplayServerMacOS::_dispatch_input_event(const Ref<InputEvent> &p_event) { |
| 444 |
if (!in_dispatch_input_event) { |
| 445 |
in_dispatch_input_event = true; |
| 446 |
|
| 447 |
{ |
| 448 |
List<WindowID>::Element *E = popup_list.back(); |
| 449 |
if (E && Object::cast_to<InputEventKey>(*p_event)) { |
| 450 |
// Redirect keyboard input to active popup. |
| 451 |
if (windows.has(E->get())) { |
| 452 |
Callable callable = windows[E->get()].input_event_callback; |
| 453 |
if (callable.is_valid()) { |
| 454 |
callable.call(p_event); |
| 455 |
} |
| 456 |
} |
| 457 |
in_dispatch_input_event = false; |
| 458 |
return; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
Ref<InputEventFromWindow> event_from_window = p_event; |
| 463 |
if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) { |
| 464 |
// Send to a window. |
| 465 |
if (windows.has(event_from_window->get_window_id())) { |
| 466 |
Callable callable = windows[event_from_window->get_window_id()].input_event_callback; |
| 467 |
if (callable.is_valid()) { |
| 468 |
callable.call(p_event); |
| 469 |
} |
| 470 |
} |
| 471 |
} else { |
| 472 |
// Send to all windows. Copy all pending callbacks, since callback can erase window. |
| 473 |
Vector<Callable> cbs; |
| 474 |
for (KeyValue<WindowID, WindowData> &E : windows) { |
| 475 |
Callable callable = E.value.input_event_callback; |
| 476 |
if (callable.is_valid()) { |
| 477 |
cbs.push_back(callable); |
| 478 |
} |
| 479 |
} |
| 480 |
for (const Callable &cb : cbs) { |
| 481 |
cb.call(p_event); |
| 482 |
} |
| 483 |
} |
| 484 |
in_dispatch_input_event = false; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
void DisplayServerMacOS::_push_input(const Ref<InputEvent> &p_event) { |
| 489 |
Ref<InputEvent> ev = p_event; |
| 490 |
Input::get_singleton()->parse_input_event(ev); |
| 491 |
} |
| 492 |
|
| 493 |
void DisplayServerMacOS::_process_key_events() { |
| 494 |
Ref<InputEventKey> k; |
| 495 |
for (int i = 0; i < key_event_pos; i++) { |
| 496 |
const KeyEvent &ke = key_event_buffer[i]; |
| 497 |
if (ke.raw) { |
| 498 |
// Non IME input - no composite characters, pass events as is. |
| 499 |
k.instantiate(); |
| 500 |
|
| 501 |
k->set_window_id(ke.window_id); |
| 502 |
get_key_modifier_state(ke.macos_state, k); |
| 503 |
k->set_pressed(ke.pressed); |
| 504 |
k->set_echo(ke.echo); |
| 505 |
k->set_keycode(ke.keycode); |
| 506 |
k->set_physical_keycode(ke.physical_keycode); |
| 507 |
k->set_key_label(ke.key_label); |
| 508 |
k->set_unicode(ke.unicode); |
| 509 |
k->set_location(ke.location); |
| 510 |
|
| 511 |
_push_input(k); |
| 512 |
} else { |
| 513 |
// IME input. |
| 514 |
if ((i == 0 && ke.keycode == Key::NONE) || (i > 0 && key_event_buffer[i - 1].keycode == Key::NONE)) { |
| 515 |
k.instantiate(); |
| 516 |
|
| 517 |
k->set_window_id(ke.window_id); |
| 518 |
get_key_modifier_state(ke.macos_state, k); |
| 519 |
k->set_pressed(ke.pressed); |
| 520 |
k->set_echo(ke.echo); |
| 521 |
k->set_keycode(Key::NONE); |
| 522 |
k->set_physical_keycode(Key::NONE); |
| 523 |
k->set_key_label(Key::NONE); |
| 524 |
k->set_unicode(ke.unicode); |
| 525 |
|
| 526 |
_push_input(k); |
| 527 |
} |
| 528 |
if (ke.keycode != Key::NONE) { |
| 529 |
k.instantiate(); |
| 530 |
|
| 531 |
k->set_window_id(ke.window_id); |
| 532 |
get_key_modifier_state(ke.macos_state, k); |
| 533 |
k->set_pressed(ke.pressed); |
| 534 |
k->set_echo(ke.echo); |
| 535 |
k->set_keycode(ke.keycode); |
| 536 |
k->set_physical_keycode(ke.physical_keycode); |
| 537 |
k->set_key_label(ke.key_label); |
| 538 |
k->set_location(ke.location); |
| 539 |
|
| 540 |
if (i + 1 < key_event_pos && key_event_buffer[i + 1].keycode == Key::NONE) { |
| 541 |
k->set_unicode(key_event_buffer[i + 1].unicode); |
| 542 |
} |
| 543 |
|
| 544 |
_push_input(k); |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
key_event_pos = 0; |
| 550 |
} |
| 551 |
|
| 552 |
NSImage *DisplayServerMacOS::_convert_to_nsimg(Ref<Image> &p_image) const { |
| 553 |
p_image->convert(Image::FORMAT_RGBA8); |
| 554 |
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] |
| 555 |
initWithBitmapDataPlanes:nullptr |
| 556 |
pixelsWide:p_image->get_width() |
| 557 |
pixelsHigh:p_image->get_height() |
| 558 |
bitsPerSample:8 |
| 559 |
samplesPerPixel:4 |
| 560 |
hasAlpha:YES |
| 561 |
isPlanar:NO |
| 562 |
colorSpaceName:NSDeviceRGBColorSpace |
| 563 |
bytesPerRow:int(p_image->get_width()) * 4 |
| 564 |
bitsPerPixel:32]; |
| 565 |
ERR_FAIL_NULL_V(imgrep, nil); |
| 566 |
uint8_t *pixels = [imgrep bitmapData]; |
| 567 |
|
| 568 |
int len = p_image->get_width() * p_image->get_height(); |
| 569 |
const uint8_t *r = p_image->get_data().ptr(); |
| 570 |
|
| 571 |
/* Premultiply the alpha channel */ |
| 572 |
for (int i = 0; i < len; i++) { |
| 573 |
uint8_t alpha = r[i * 4 + 3]; |
| 574 |
pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255); |
| 575 |
pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255); |
| 576 |
pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255); |
| 577 |
pixels[i * 4 + 3] = alpha; |
| 578 |
} |
| 579 |
|
| 580 |
NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(p_image->get_width(), p_image->get_height())]; |
| 581 |
ERR_FAIL_NULL_V(nsimg, nil); |
| 582 |
[nsimg addRepresentation:imgrep]; |
| 583 |
return nsimg; |
| 584 |
} |
| 585 |
|
| 586 |
NSCursor *DisplayServerMacOS::_cursor_from_selector(SEL p_selector, SEL p_fallback) { |
| 587 |
if ([NSCursor respondsToSelector:p_selector]) { |
| 588 |
id object = [NSCursor performSelector:p_selector]; |
| 589 |
if ([object isKindOfClass:[NSCursor class]]) { |
| 590 |
return object; |
| 591 |
} |
| 592 |
} |
| 593 |
if (p_fallback) { |
| 594 |
// Fallback should be a reasonable default, no need to check. |
| 595 |
return [NSCursor performSelector:p_fallback]; |
| 596 |
} |
| 597 |
return [NSCursor arrowCursor]; |
| 598 |
} |
| 599 |
|
| 600 |
void DisplayServerMacOS::menu_callback(id p_sender) { |
| 601 |
if (![p_sender representedObject]) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
GodotMenuItem *value = [p_sender representedObject]; |
| 606 |
if (value) { |
| 607 |
if (value->callback.is_valid()) { |
| 608 |
MenuCall mc; |
| 609 |
mc.tag = value->meta; |
| 610 |
mc.callback = value->callback; |
| 611 |
deferred_menu_calls.push_back(mc); |
| 612 |
// Do not run callback from here! If it is opening a new window or calling process_events, it will corrupt OS event queue and crash. |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
bool DisplayServerMacOS::has_window(WindowID p_window) const { |
| 618 |
return windows.has(p_window); |
| 619 |
} |
| 620 |
|
| 621 |
DisplayServerMacOS::WindowData &DisplayServerMacOS::get_window(WindowID p_window) { |
| 622 |
return windows[p_window]; |
| 623 |
} |
| 624 |
|
| 625 |
void DisplayServerMacOS::send_event(NSEvent *p_event) { |
| 626 |
// Special case handling of shortcuts that don't arrive at the regular keyDown handler |
| 627 |
if ([p_event type] == NSEventTypeKeyDown) { |
| 628 |
NSEventModifierFlags flags = [p_event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask; |
| 629 |
|
| 630 |
// Command-period |
| 631 |
if ((flags == NSEventModifierFlagCommand) && [p_event keyCode] == 0x2f) { |
| 632 |
Ref<InputEventKey> k; |
| 633 |
k.instantiate(); |
| 634 |
|
| 635 |
get_key_modifier_state([p_event modifierFlags], k); |
| 636 |
k->set_window_id(DisplayServerMacOS::INVALID_WINDOW_ID); |
| 637 |
k->set_pressed(true); |
| 638 |
k->set_keycode(Key::PERIOD); |
| 639 |
k->set_physical_keycode(Key::PERIOD); |
| 640 |
k->set_key_label(Key::PERIOD); |
| 641 |
k->set_echo([p_event isARepeat]); |
| 642 |
|
| 643 |
Input::get_singleton()->parse_input_event(k); |
| 644 |
return; |
| 645 |
} |
| 646 |
|
| 647 |
// Ctrl+Tab and Ctrl+Shift+Tab |
| 648 |
if (((flags == NSEventModifierFlagControl) || (flags == (NSEventModifierFlagControl | NSEventModifierFlagShift))) && [p_event keyCode] == 0x30) { |
| 649 |
Ref<InputEventKey> k; |
| 650 |
k.instantiate(); |
| 651 |
|
| 652 |
get_key_modifier_state([p_event modifierFlags], k); |
| 653 |
k->set_window_id(DisplayServerMacOS::INVALID_WINDOW_ID); |
| 654 |
k->set_pressed(true); |
| 655 |
k->set_keycode(Key::TAB); |
| 656 |
k->set_physical_keycode(Key::TAB); |
| 657 |
k->set_key_label(Key::TAB); |
| 658 |
k->set_echo([p_event isARepeat]); |
| 659 |
|
| 660 |
Input::get_singleton()->parse_input_event(k); |
| 661 |
return; |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
void DisplayServerMacOS::send_window_event(const WindowData &wd, WindowEvent p_event) { |
| 667 |
_THREAD_SAFE_METHOD_ |
| 668 |
|
| 669 |
if (wd.event_callback.is_valid()) { |
| 670 |
Variant event = int(p_event); |
| 671 |
wd.event_callback.call(event); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
void DisplayServerMacOS::release_pressed_events() { |
| 676 |
_THREAD_SAFE_METHOD_ |
| 677 |
if (Input::get_singleton()) { |
| 678 |
Input::get_singleton()->release_pressed_events(); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
void DisplayServerMacOS::sync_mouse_state() { |
| 683 |
_THREAD_SAFE_METHOD_ |
| 684 |
if (Input::get_singleton()) { |
| 685 |
Vector2i pos = Input::get_singleton()->get_mouse_position(); |
| 686 |
BitField<MouseButtonMask> in_mask = Input::get_singleton()->get_mouse_button_mask(); |
| 687 |
BitField<MouseButtonMask> ds_mask = mouse_get_button_state(); |
| 688 |
for (int btn = (int)MouseButton::LEFT; btn <= (int)MouseButton::MB_XBUTTON2; btn++) { |
| 689 |
MouseButtonMask mbm = mouse_button_to_mask(MouseButton(btn)); |
| 690 |
if (in_mask.has_flag(mbm) && !ds_mask.has_flag(mbm)) { |
| 691 |
Ref<InputEventMouseButton> mb; |
| 692 |
mb.instantiate(); |
| 693 |
mb->set_button_index(MouseButton(btn)); |
| 694 |
mb->set_pressed(false); |
| 695 |
mb->set_position(pos); |
| 696 |
mb->set_global_position(pos); |
| 697 |
mb->set_button_mask(ds_mask); |
| 698 |
Input::get_singleton()->parse_input_event(mb); |
| 699 |
} |
| 700 |
if (!in_mask.has_flag(mbm) && ds_mask.has_flag(mbm)) { |
| 701 |
Ref<InputEventMouseButton> mb; |
| 702 |
mb.instantiate(); |
| 703 |
mb->set_button_index(MouseButton(btn)); |
| 704 |
mb->set_pressed(true); |
| 705 |
mb->set_position(pos); |
| 706 |
mb->set_global_position(pos); |
| 707 |
mb->set_button_mask(ds_mask); |
| 708 |
Input::get_singleton()->parse_input_event(mb); |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
void DisplayServerMacOS::get_key_modifier_state(unsigned int p_macos_state, Ref<InputEventWithModifiers> r_state) const { |
| 715 |
r_state->set_shift_pressed((p_macos_state & NSEventModifierFlagShift)); |
| 716 |
r_state->set_ctrl_pressed((p_macos_state & NSEventModifierFlagControl)); |
| 717 |
r_state->set_alt_pressed((p_macos_state & NSEventModifierFlagOption)); |
| 718 |
r_state->set_meta_pressed((p_macos_state & NSEventModifierFlagCommand)); |
| 719 |
} |
| 720 |
|
| 721 |
void DisplayServerMacOS::update_mouse_pos(DisplayServerMacOS::WindowData &p_wd, NSPoint p_location_in_window) { |
| 722 |
const NSRect content_rect = [p_wd.window_view frame]; |
| 723 |
const float scale = screen_get_max_scale(); |
| 724 |
p_wd.mouse_pos.x = p_location_in_window.x * scale; |
| 725 |
p_wd.mouse_pos.y = (content_rect.size.height - p_location_in_window.y) * scale; |
| 726 |
Input::get_singleton()->set_mouse_position(p_wd.mouse_pos); |
| 727 |
} |
| 728 |
|
| 729 |
void DisplayServerMacOS::pop_last_key_event() { |
| 730 |
// Does not pop last key event when it is an IME key event. |
| 731 |
if (key_event_pos > 0 && key_event_buffer[key_event_pos - 1].raw) { |
| 732 |
key_event_pos--; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
void DisplayServerMacOS::push_to_key_event_buffer(const DisplayServerMacOS::KeyEvent &p_event) { |
| 737 |
if (key_event_pos >= key_event_buffer.size()) { |
| 738 |
key_event_buffer.resize(1 + key_event_pos); |
| 739 |
} |
| 740 |
key_event_buffer.write[key_event_pos++] = p_event; |
| 741 |
} |
| 742 |
|
| 743 |
void DisplayServerMacOS::update_im_text(const Point2i &p_selection, const String &p_text) { |
| 744 |
im_selection = p_selection; |
| 745 |
im_text = p_text; |
| 746 |
|
| 747 |
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 748 |
} |
| 749 |
|
| 750 |
void DisplayServerMacOS::set_last_focused_window(WindowID p_window) { |
| 751 |
last_focused_window = p_window; |
| 752 |
} |
| 753 |
|
| 754 |
void DisplayServerMacOS::set_is_resizing(bool p_is_resizing) { |
| 755 |
is_resizing = p_is_resizing; |
| 756 |
} |
| 757 |
|
| 758 |
bool DisplayServerMacOS::get_is_resizing() const { |
| 759 |
return is_resizing; |
| 760 |
} |
| 761 |
|
| 762 |
void DisplayServerMacOS::window_destroy(WindowID p_window) { |
| 763 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 764 |
|
| 765 |
#if defined(GLES3_ENABLED) |
| 766 |
if (gl_manager_legacy) { |
| 767 |
gl_manager_legacy->window_destroy(p_window); |
| 768 |
} |
| 769 |
#endif |
| 770 |
#ifdef RD_ENABLED |
| 771 |
if (rendering_device) { |
| 772 |
rendering_device->screen_free(p_window); |
| 773 |
} |
| 774 |
|
| 775 |
if (rendering_context) { |
| 776 |
rendering_context->window_destroy(p_window); |
| 777 |
} |
| 778 |
#endif |
| 779 |
#ifdef ACCESSKIT_ENABLED |
| 780 |
if (accessibility_driver) { |
| 781 |
accessibility_driver->window_destroy(p_window); |
| 782 |
} |
| 783 |
#endif |
| 784 |
windows.erase(p_window); |
| 785 |
|
| 786 |
if (last_focused_window == p_window) { |
| 787 |
last_focused_window = INVALID_WINDOW_ID; |
| 788 |
} |
| 789 |
update_presentation_mode(); |
| 790 |
} |
| 791 |
|
| 792 |
void DisplayServerMacOS::window_resize(WindowID p_window, int p_width, int p_height) { |
| 793 |
#if defined(RD_ENABLED) |
| 794 |
if (rendering_context) { |
| 795 |
rendering_context->window_set_size(p_window, p_width, p_height); |
| 796 |
} |
| 797 |
#endif |
| 798 |
#if defined(GLES3_ENABLED) |
| 799 |
if (gl_manager_legacy) { |
| 800 |
gl_manager_legacy->window_resize(p_window, p_width, p_height); |
| 801 |
} |
| 802 |
if (gl_manager_angle) { |
| 803 |
gl_manager_angle->window_resize(p_window, p_width, p_height); |
| 804 |
} |
| 805 |
#endif |
| 806 |
} |
| 807 |
|
| 808 |
bool DisplayServerMacOS::has_feature(Feature p_feature) const { |
| 809 |
switch (p_feature) { |
| 810 |
#ifndef DISABLE_DEPRECATED |
| 811 |
case FEATURE_GLOBAL_MENU: { |
| 812 |
return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)); |
| 813 |
} break; |
| 814 |
#endif |
| 815 |
case FEATURE_SUBWINDOWS: |
| 816 |
//case FEATURE_TOUCHSCREEN: |
| 817 |
case FEATURE_MOUSE: |
| 818 |
case FEATURE_MOUSE_WARP: |
| 819 |
case FEATURE_CLIPBOARD: |
| 820 |
case FEATURE_CURSOR_SHAPE: |
| 821 |
case FEATURE_CUSTOM_CURSOR_SHAPE: |
| 822 |
case FEATURE_NATIVE_DIALOG: |
| 823 |
case FEATURE_NATIVE_DIALOG_INPUT: |
| 824 |
case FEATURE_NATIVE_DIALOG_FILE: |
| 825 |
case FEATURE_NATIVE_DIALOG_FILE_EXTRA: |
| 826 |
case FEATURE_NATIVE_DIALOG_FILE_MIME: |
| 827 |
case FEATURE_IME: |
| 828 |
case FEATURE_WINDOW_TRANSPARENCY: |
| 829 |
case FEATURE_HIDPI: |
| 830 |
case FEATURE_ICON: |
| 831 |
case FEATURE_NATIVE_ICON: |
| 832 |
//case FEATURE_KEEP_SCREEN_ON: |
| 833 |
case FEATURE_SWAP_BUFFERS: |
| 834 |
case FEATURE_TEXT_TO_SPEECH: |
| 835 |
case FEATURE_EXTEND_TO_TITLE: |
| 836 |
case FEATURE_SCREEN_CAPTURE: |
| 837 |
case FEATURE_STATUS_INDICATOR: |
| 838 |
case FEATURE_NATIVE_HELP: |
| 839 |
case FEATURE_WINDOW_DRAG: |
| 840 |
case FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE: |
| 841 |
case FEATURE_EMOJI_AND_SYMBOL_PICKER: |
| 842 |
case FEATURE_WINDOW_EMBEDDING: |
| 843 |
return true; |
| 844 |
#ifdef ACCESSKIT_ENABLED |
| 845 |
case FEATURE_ACCESSIBILITY_SCREEN_READER: { |
| 846 |
return (accessibility_driver != nullptr); |
| 847 |
} break; |
| 848 |
#endif |
| 849 |
default: { |
| 850 |
} |
| 851 |
} |
| 852 |
return false; |
| 853 |
} |
| 854 |
|
| 855 |
String DisplayServerMacOS::get_name() const { |
| 856 |
return "macOS"; |
| 857 |
} |
| 858 |
|
| 859 |
void DisplayServerMacOS::help_set_search_callbacks(const Callable &p_search_callback, const Callable &p_action_callback) { |
| 860 |
help_search_callback = p_search_callback; |
| 861 |
help_action_callback = p_action_callback; |
| 862 |
} |
| 863 |
|
| 864 |
Callable DisplayServerMacOS::_help_get_search_callback() const { |
| 865 |
return help_search_callback; |
| 866 |
} |
| 867 |
|
| 868 |
Callable DisplayServerMacOS::_help_get_action_callback() const { |
| 869 |
return help_action_callback; |
| 870 |
} |
| 871 |
|
| 872 |
Error DisplayServerMacOS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) { |
| 873 |
_THREAD_SAFE_METHOD_ |
| 874 |
|
| 875 |
NSAlert *window = [[NSAlert alloc] init]; |
| 876 |
NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()]; |
| 877 |
NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()]; |
| 878 |
|
| 879 |
for (int i = 0; i < p_buttons.size(); i++) { |
| 880 |
NSString *ns_button = [NSString stringWithUTF8String:p_buttons[i].utf8().get_data()]; |
| 881 |
[window addButtonWithTitle:ns_button]; |
| 882 |
} |
| 883 |
[window setMessageText:ns_title]; |
| 884 |
[window setInformativeText:ns_description]; |
| 885 |
[window setAlertStyle:NSAlertStyleInformational]; |
| 886 |
|
| 887 |
Variant button_pressed; |
| 888 |
NSInteger ret = [window runModal]; |
| 889 |
if (ret == NSAlertFirstButtonReturn) { |
| 890 |
button_pressed = int64_t(0); |
| 891 |
} else if (ret == NSAlertSecondButtonReturn) { |
| 892 |
button_pressed = int64_t(1); |
| 893 |
} else if (ret == NSAlertThirdButtonReturn) { |
| 894 |
button_pressed = int64_t(2); |
| 895 |
} else { |
| 896 |
button_pressed = int64_t(2 + (ret - NSAlertThirdButtonReturn)); |
| 897 |
} |
| 898 |
|
| 899 |
if (p_callback.is_valid()) { |
| 900 |
Variant ret; |
| 901 |
Callable::CallError ce; |
| 902 |
const Variant *args[1] = { &button_pressed }; |
| 903 |
|
| 904 |
p_callback.callp(args, 1, ret, ce); |
| 905 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 906 |
ERR_PRINT(vformat("Failed to execute dialog callback: %s.", Variant::get_callable_error_text(p_callback, args, 1, ce))); |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
return OK; |
| 911 |
} |
| 912 |
|
| 913 |
Error DisplayServerMacOS::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) { |
| 914 |
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); |
| 915 |
} |
| 916 |
|
| 917 |
Error DisplayServerMacOS::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) { |
| 918 |
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); |
| 919 |
} |
| 920 |
|
| 921 |
Error DisplayServerMacOS::_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) { |
| 922 |
_THREAD_SAFE_METHOD_ |
| 923 |
|
| 924 |
ERR_FAIL_INDEX_V(int(p_mode), FILE_DIALOG_MODE_SAVE_MAX, FAILED); |
| 925 |
|
| 926 |
NSString *url = [NSString stringWithUTF8String:p_current_directory.utf8().get_data()]; |
| 927 |
|
| 928 |
NSWindow *nswindow = nullptr; |
| 929 |
if (windows.has(p_window_id) && !windows[p_window_id].is_popup) { |
| 930 |
nswindow = windows[p_window_id].window_object; |
| 931 |
} |
| 932 |
|
| 933 |
GodotOpenSaveDelegate *panel_delegate = [[GodotOpenSaveDelegate alloc] init]; |
| 934 |
if (p_root.length() > 0) { |
| 935 |
[panel_delegate setRootPath:p_root]; |
| 936 |
} |
| 937 |
Callable callback = p_callback; // Make a copy for async completion handler. |
| 938 |
if (p_mode == FILE_DIALOG_MODE_SAVE_FILE) { |
| 939 |
NSSavePanel *panel = [NSSavePanel savePanel]; |
| 940 |
|
| 941 |
[panel setDirectoryURL:[NSURL fileURLWithPath:url]]; |
| 942 |
[panel_delegate makeAccessoryView:panel filters:p_filters options:p_options]; |
| 943 |
[panel setExtensionHidden:YES]; |
| 944 |
[panel setCanSelectHiddenExtension:YES]; |
| 945 |
[panel setCanCreateDirectories:YES]; |
| 946 |
[panel setShowsHiddenFiles:p_show_hidden]; |
| 947 |
[panel setDelegate:panel_delegate]; |
| 948 |
if (p_filename != "") { |
| 949 |
NSString *fileurl = [NSString stringWithUTF8String:p_filename.utf8().get_data()]; |
| 950 |
[panel setNameFieldStringValue:fileurl]; |
| 951 |
} |
| 952 |
|
| 953 |
void (^completion_handler)(NSInteger ret) = ^(NSInteger ret) { |
| 954 |
if (ret == NSModalResponseOK) { |
| 955 |
// Save bookmark for folder. |
| 956 |
if (OS::get_singleton()->is_sandboxed()) { |
| 957 |
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"]; |
| 958 |
bool skip = false; |
| 959 |
for (id bookmark in bookmarks) { |
| 960 |
NSError *error = nil; |
| 961 |
BOOL isStale = NO; |
| 962 |
NSURL *exurl = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]; |
| 963 |
if (!error && !isStale && ([[exurl path] compare:[[panel directoryURL] path]] == NSOrderedSame)) { |
| 964 |
skip = true; |
| 965 |
break; |
| 966 |
} |
| 967 |
} |
| 968 |
if (!skip) { |
| 969 |
NSError *error = nil; |
| 970 |
NSData *bookmark = [[panel directoryURL] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error]; |
| 971 |
if (!error) { |
| 972 |
NSArray *new_bookmarks = [bookmarks arrayByAddingObject:bookmark]; |
| 973 |
[[NSUserDefaults standardUserDefaults] setObject:new_bookmarks forKey:@"sec_bookmarks"]; |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
// Callback. |
| 978 |
Vector<String> files; |
| 979 |
String url; |
| 980 |
url.append_utf8([[[panel URL] path] UTF8String]); |
| 981 |
files.push_back([panel_delegate validateFilename:url]); |
| 982 |
if (callback.is_valid()) { |
| 983 |
if (p_options_in_cb) { |
| 984 |
Variant v_result = true; |
| 985 |
Variant v_files = files; |
| 986 |
Variant v_index = [panel_delegate getIndex]; |
| 987 |
Variant v_opt = [panel_delegate getSelection]; |
| 988 |
Variant ret; |
| 989 |
Callable::CallError ce; |
| 990 |
const Variant *args[4] = { &v_result, &v_files, &v_index, &v_opt }; |
| 991 |
|
| 992 |
callback.callp(args, 4, ret, ce); |
| 993 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 994 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 4, ce))); |
| 995 |
} |
| 996 |
} else { |
| 997 |
Variant v_result = true; |
| 998 |
Variant v_files = files; |
| 999 |
Variant v_index = [panel_delegate getIndex]; |
| 1000 |
Variant ret; |
| 1001 |
Callable::CallError ce; |
| 1002 |
const Variant *args[3] = { &v_result, &v_files, &v_index }; |
| 1003 |
|
| 1004 |
callback.callp(args, 3, ret, ce); |
| 1005 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1006 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 3, ce))); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} else { |
| 1011 |
if (callback.is_valid()) { |
| 1012 |
if (p_options_in_cb) { |
| 1013 |
Variant v_result = false; |
| 1014 |
Variant v_files = Vector<String>(); |
| 1015 |
Variant v_index = [panel_delegate getIndex]; |
| 1016 |
Variant v_opt = [panel_delegate getSelection]; |
| 1017 |
Variant ret; |
| 1018 |
Callable::CallError ce; |
| 1019 |
const Variant *args[4] = { &v_result, &v_files, &v_index, &v_opt }; |
| 1020 |
|
| 1021 |
callback.callp(args, 4, ret, ce); |
| 1022 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1023 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 4, ce))); |
| 1024 |
} |
| 1025 |
} else { |
| 1026 |
Variant v_result = false; |
| 1027 |
Variant v_files = Vector<String>(); |
| 1028 |
Variant v_index = [panel_delegate getIndex]; |
| 1029 |
Variant ret; |
| 1030 |
Callable::CallError ce; |
| 1031 |
const Variant *args[3] = { &v_result, &v_files, &v_index }; |
| 1032 |
|
| 1033 |
callback.callp(args, 3, ret, ce); |
| 1034 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1035 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 3, ce))); |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
if (p_window_id != INVALID_WINDOW_ID) { |
| 1041 |
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(p_window_id); |
| 1042 |
} |
| 1043 |
}; |
| 1044 |
if (nswindow) { |
| 1045 |
[panel beginSheetModalForWindow:nswindow completionHandler:completion_handler]; |
| 1046 |
} else { |
| 1047 |
[panel beginWithCompletionHandler:completion_handler]; |
| 1048 |
} |
| 1049 |
} else { |
| 1050 |
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 1051 |
|
| 1052 |
[panel setDirectoryURL:[NSURL fileURLWithPath:url]]; |
| 1053 |
[panel_delegate makeAccessoryView:panel filters:p_filters options:p_options]; |
| 1054 |
[panel setExtensionHidden:YES]; |
| 1055 |
[panel setCanSelectHiddenExtension:YES]; |
| 1056 |
[panel setCanCreateDirectories:YES]; |
| 1057 |
[panel setCanChooseFiles:(p_mode != FILE_DIALOG_MODE_OPEN_DIR)]; |
| 1058 |
[panel setCanChooseDirectories:(p_mode == FILE_DIALOG_MODE_OPEN_DIR || p_mode == FILE_DIALOG_MODE_OPEN_ANY)]; |
| 1059 |
[panel setShowsHiddenFiles:p_show_hidden]; |
| 1060 |
[panel setDelegate:panel_delegate]; |
| 1061 |
if (p_filename != "") { |
| 1062 |
NSString *fileurl = [NSString stringWithUTF8String:p_filename.utf8().get_data()]; |
| 1063 |
[panel setNameFieldStringValue:fileurl]; |
| 1064 |
} |
| 1065 |
[panel setAllowsMultipleSelection:(p_mode == FILE_DIALOG_MODE_OPEN_FILES)]; |
| 1066 |
|
| 1067 |
void (^completion_handler)(NSInteger ret) = ^(NSInteger ret) { |
| 1068 |
if (ret == NSModalResponseOK) { |
| 1069 |
// Save bookmark for folder. |
| 1070 |
NSArray *urls = [(NSOpenPanel *)panel URLs]; |
| 1071 |
if (OS::get_singleton()->is_sandboxed()) { |
| 1072 |
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"]; |
| 1073 |
NSMutableArray *new_bookmarks = [bookmarks mutableCopy]; |
| 1074 |
for (NSUInteger i = 0; i != [urls count]; ++i) { |
| 1075 |
bool skip = false; |
| 1076 |
for (id bookmark in bookmarks) { |
| 1077 |
NSError *error = nil; |
| 1078 |
BOOL isStale = NO; |
| 1079 |
NSURL *exurl = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]; |
| 1080 |
if (!error && !isStale && ([[exurl path] compare:[[urls objectAtIndex:i] path]] == NSOrderedSame)) { |
| 1081 |
skip = true; |
| 1082 |
break; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
if (!skip) { |
| 1086 |
NSError *error = nil; |
| 1087 |
NSData *bookmark = [[urls objectAtIndex:i] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error]; |
| 1088 |
if (!error) { |
| 1089 |
[new_bookmarks addObject:bookmark]; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
[[NSUserDefaults standardUserDefaults] setObject:new_bookmarks forKey:@"sec_bookmarks"]; |
| 1094 |
} |
| 1095 |
// Callback. |
| 1096 |
Vector<String> files; |
| 1097 |
for (NSUInteger i = 0; i != [urls count]; ++i) { |
| 1098 |
String url; |
| 1099 |
url.append_utf8([[[urls objectAtIndex:i] path] UTF8String]); |
| 1100 |
files.push_back([panel_delegate validateFilename:url]); |
| 1101 |
} |
| 1102 |
if (callback.is_valid()) { |
| 1103 |
if (p_options_in_cb) { |
| 1104 |
Variant v_result = true; |
| 1105 |
Variant v_files = files; |
| 1106 |
Variant v_index = [panel_delegate getIndex]; |
| 1107 |
Variant v_opt = [panel_delegate getSelection]; |
| 1108 |
Variant ret; |
| 1109 |
Callable::CallError ce; |
| 1110 |
const Variant *args[4] = { &v_result, &v_files, &v_index, &v_opt }; |
| 1111 |
|
| 1112 |
callback.callp(args, 4, ret, ce); |
| 1113 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1114 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 4, ce))); |
| 1115 |
} |
| 1116 |
} else { |
| 1117 |
Variant v_result = true; |
| 1118 |
Variant v_files = files; |
| 1119 |
Variant v_index = [panel_delegate getIndex]; |
| 1120 |
Variant ret; |
| 1121 |
Callable::CallError ce; |
| 1122 |
const Variant *args[3] = { &v_result, &v_files, &v_index }; |
| 1123 |
|
| 1124 |
callback.callp(args, 3, ret, ce); |
| 1125 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1126 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 3, ce))); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} else { |
| 1131 |
if (callback.is_valid()) { |
| 1132 |
if (p_options_in_cb) { |
| 1133 |
Variant v_result = false; |
| 1134 |
Variant v_files = Vector<String>(); |
| 1135 |
Variant v_index = [panel_delegate getIndex]; |
| 1136 |
Variant v_opt = [panel_delegate getSelection]; |
| 1137 |
Variant ret; |
| 1138 |
Callable::CallError ce; |
| 1139 |
const Variant *args[4] = { &v_result, &v_files, &v_index, &v_opt }; |
| 1140 |
|
| 1141 |
callback.callp(args, 4, ret, ce); |
| 1142 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1143 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 4, ce))); |
| 1144 |
} |
| 1145 |
} else { |
| 1146 |
Variant v_result = false; |
| 1147 |
Variant v_files = Vector<String>(); |
| 1148 |
Variant v_index = [panel_delegate getIndex]; |
| 1149 |
Variant ret; |
| 1150 |
Callable::CallError ce; |
| 1151 |
const Variant *args[3] = { &v_result, &v_files, &v_index }; |
| 1152 |
|
| 1153 |
callback.callp(args, 3, ret, ce); |
| 1154 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1155 |
ERR_PRINT(vformat("Failed to execute file dialog callback: %s.", Variant::get_callable_error_text(callback, args, 3, ce))); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} |
| 1160 |
if (p_window_id != INVALID_WINDOW_ID) { |
| 1161 |
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(p_window_id); |
| 1162 |
} |
| 1163 |
}; |
| 1164 |
if (nswindow) { |
| 1165 |
[panel beginSheetModalForWindow:nswindow completionHandler:completion_handler]; |
| 1166 |
} else { |
| 1167 |
[panel beginWithCompletionHandler:completion_handler]; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
return OK; |
| 1172 |
} |
| 1173 |
|
| 1174 |
void DisplayServerMacOS::beep() const { |
| 1175 |
NSBeep(); |
| 1176 |
} |
| 1177 |
|
| 1178 |
Error DisplayServerMacOS::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) { |
| 1179 |
_THREAD_SAFE_METHOD_ |
| 1180 |
|
| 1181 |
NSAlert *window = [[NSAlert alloc] init]; |
| 1182 |
NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()]; |
| 1183 |
NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()]; |
| 1184 |
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 30)]; |
| 1185 |
|
| 1186 |
[window addButtonWithTitle:@"OK"]; |
| 1187 |
[window setMessageText:ns_title]; |
| 1188 |
[window setInformativeText:ns_description]; |
| 1189 |
[window setAlertStyle:NSAlertStyleInformational]; |
| 1190 |
|
| 1191 |
[input setStringValue:[NSString stringWithUTF8String:p_partial.utf8().get_data()]]; |
| 1192 |
[window setAccessoryView:input]; |
| 1193 |
|
| 1194 |
[window runModal]; |
| 1195 |
|
| 1196 |
String ret; |
| 1197 |
ret.append_utf8([[input stringValue] UTF8String]); |
| 1198 |
|
| 1199 |
if (p_callback.is_valid()) { |
| 1200 |
Variant v_result = ret; |
| 1201 |
Variant ret; |
| 1202 |
Callable::CallError ce; |
| 1203 |
const Variant *args[1] = { &v_result }; |
| 1204 |
|
| 1205 |
p_callback.callp(args, 1, ret, ce); |
| 1206 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 1207 |
ERR_PRINT(vformat("Failed to execute input dialog callback: %s.", Variant::get_callable_error_text(p_callback, args, 1, ce))); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
return OK; |
| 1212 |
} |
| 1213 |
|
| 1214 |
void DisplayServerMacOS::_mouse_update_mode() { |
| 1215 |
_THREAD_SAFE_METHOD_ |
| 1216 |
|
| 1217 |
MouseMode wanted_mouse_mode = mouse_mode_override_enabled |
| 1218 |
? mouse_mode_override |
| 1219 |
: mouse_mode_base; |
| 1220 |
|
| 1221 |
if (wanted_mouse_mode == mouse_mode) { |
| 1222 |
return; |
| 1223 |
} |
| 1224 |
|
| 1225 |
WindowID window_id = _get_focused_window_or_popup(); |
| 1226 |
if (!windows.has(window_id)) { |
| 1227 |
window_id = MAIN_WINDOW_ID; |
| 1228 |
} |
| 1229 |
WindowData &wd = windows[window_id]; |
| 1230 |
|
| 1231 |
bool show_cursor = (wanted_mouse_mode == MOUSE_MODE_VISIBLE || wanted_mouse_mode == MOUSE_MODE_CONFINED); |
| 1232 |
bool previously_shown = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED); |
| 1233 |
|
| 1234 |
if (show_cursor && !previously_shown) { |
| 1235 |
window_id = get_window_at_screen_position(mouse_get_position()); |
| 1236 |
mouse_enter_window(window_id); |
| 1237 |
} |
| 1238 |
|
| 1239 |
if (wanted_mouse_mode == MOUSE_MODE_CAPTURED) { |
| 1240 |
// Apple Docs state that the display parameter is not used. |
| 1241 |
// "This parameter is not used. By default, you may pass kCGDirectMainDisplay." |
| 1242 |
// https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/Quartz_Services_Ref/Reference/reference.html |
| 1243 |
if (previously_shown) { |
| 1244 |
CGDisplayHideCursor(kCGDirectMainDisplay); |
| 1245 |
} |
| 1246 |
CGAssociateMouseAndMouseCursorPosition(false); |
| 1247 |
[wd.window_object setMovable:NO]; |
| 1248 |
const NSRect contentRect = [wd.window_view frame]; |
| 1249 |
NSRect pointInWindowRect = NSMakeRect(contentRect.size.width / 2, contentRect.size.height / 2, 0, 0); |
| 1250 |
NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin; |
| 1251 |
CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y }; |
| 1252 |
CGWarpMouseCursorPosition(lMouseWarpPos); |
| 1253 |
} else if (wanted_mouse_mode == MOUSE_MODE_HIDDEN) { |
| 1254 |
if (previously_shown) { |
| 1255 |
CGDisplayHideCursor(kCGDirectMainDisplay); |
| 1256 |
} |
| 1257 |
[wd.window_object setMovable:YES]; |
| 1258 |
CGAssociateMouseAndMouseCursorPosition(true); |
| 1259 |
} else if (wanted_mouse_mode == MOUSE_MODE_CONFINED) { |
| 1260 |
CGDisplayShowCursor(kCGDirectMainDisplay); |
| 1261 |
[wd.window_object setMovable:NO]; |
| 1262 |
CGAssociateMouseAndMouseCursorPosition(false); |
| 1263 |
} else if (wanted_mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { |
| 1264 |
if (previously_shown) { |
| 1265 |
CGDisplayHideCursor(kCGDirectMainDisplay); |
| 1266 |
} |
| 1267 |
[wd.window_object setMovable:NO]; |
| 1268 |
CGAssociateMouseAndMouseCursorPosition(false); |
| 1269 |
} else { // MOUSE_MODE_VISIBLE |
| 1270 |
CGDisplayShowCursor(kCGDirectMainDisplay); |
| 1271 |
[wd.window_object setMovable:YES]; |
| 1272 |
CGAssociateMouseAndMouseCursorPosition(true); |
| 1273 |
} |
| 1274 |
|
| 1275 |
last_warp = [[NSProcessInfo processInfo] systemUptime]; |
| 1276 |
ignore_warp = true; |
| 1277 |
warp_events.clear(); |
| 1278 |
mouse_mode = wanted_mouse_mode; |
| 1279 |
|
| 1280 |
if (show_cursor) { |
| 1281 |
cursor_update_shape(); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
void DisplayServerMacOS::mouse_set_mode(MouseMode p_mode) { |
| 1286 |
ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 1287 |
if (p_mode == mouse_mode_base) { |
| 1288 |
return; |
| 1289 |
} |
| 1290 |
mouse_mode_base = p_mode; |
| 1291 |
_mouse_update_mode(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
DisplayServer::MouseMode DisplayServerMacOS::mouse_get_mode() const { |
| 1295 |
return mouse_mode; |
| 1296 |
} |
| 1297 |
|
| 1298 |
void DisplayServerMacOS::mouse_set_mode_override(MouseMode p_mode) { |
| 1299 |
ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 1300 |
if (p_mode == mouse_mode_override) { |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
mouse_mode_override = p_mode; |
| 1304 |
_mouse_update_mode(); |
| 1305 |
} |
| 1306 |
|
| 1307 |
DisplayServer::MouseMode DisplayServerMacOS::mouse_get_mode_override() const { |
| 1308 |
return mouse_mode_override; |
| 1309 |
} |
| 1310 |
|
| 1311 |
void DisplayServerMacOS::mouse_set_mode_override_enabled(bool p_override_enabled) { |
| 1312 |
if (p_override_enabled == mouse_mode_override_enabled) { |
| 1313 |
return; |
| 1314 |
} |
| 1315 |
mouse_mode_override_enabled = p_override_enabled; |
| 1316 |
_mouse_update_mode(); |
| 1317 |
} |
| 1318 |
|
| 1319 |
bool DisplayServerMacOS::mouse_is_mode_override_enabled() const { |
| 1320 |
return mouse_mode_override_enabled; |
| 1321 |
} |
| 1322 |
|
| 1323 |
bool DisplayServerMacOS::update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp) { |
| 1324 |
_THREAD_SAFE_METHOD_ |
| 1325 |
|
| 1326 |
if (ignore_warp) { |
| 1327 |
// Discard late events, before warp. |
| 1328 |
if (p_timestamp < last_warp) { |
| 1329 |
return true; |
| 1330 |
} |
| 1331 |
ignore_warp = false; |
| 1332 |
return true; |
| 1333 |
} |
| 1334 |
|
| 1335 |
if (mouse_mode == DisplayServer::MOUSE_MODE_CONFINED || mouse_mode == DisplayServer::MOUSE_MODE_CONFINED_HIDDEN) { |
| 1336 |
// Discard late events. |
| 1337 |
if (p_timestamp < last_warp) { |
| 1338 |
return true; |
| 1339 |
} |
| 1340 |
|
| 1341 |
// Warp affects next event delta, subtract previous warp deltas. |
| 1342 |
List<WarpEvent>::Element *F = warp_events.front(); |
| 1343 |
while (F) { |
| 1344 |
if (F->get().timestamp < p_timestamp) { |
| 1345 |
List<DisplayServerMacOS::WarpEvent>::Element *E = F; |
| 1346 |
r_delta.x -= E->get().delta.x; |
| 1347 |
r_delta.y -= E->get().delta.y; |
| 1348 |
F = F->next(); |
| 1349 |
warp_events.erase(E); |
| 1350 |
} else { |
| 1351 |
F = F->next(); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
// Confine mouse position to the window, and update delta. |
| 1356 |
NSRect frame = [p_wd.window_view frame]; |
| 1357 |
NSRect frameOnScreen = [[p_wd.window_view window] convertRectToScreen:frame]; |
| 1358 |
frameOnScreen.origin.y = CGDisplayBounds(CGMainDisplayID()).size.height - frameOnScreen.origin.y; |
| 1359 |
|
| 1360 |
CGEventRef ourEvent = CGEventCreate(nullptr); |
| 1361 |
NSPoint conf_pos = CGEventGetLocation(ourEvent); |
| 1362 |
CFRelease(ourEvent); |
| 1363 |
|
| 1364 |
NSPoint prev_conf_pos = conf_pos; |
| 1365 |
|
| 1366 |
conf_pos.x = CLAMP(conf_pos.x + r_delta.x, frameOnScreen.origin.x, frameOnScreen.origin.x + frameOnScreen.size.width); |
| 1367 |
conf_pos.y = CLAMP(conf_pos.y + r_delta.y, frameOnScreen.origin.y - frameOnScreen.size.height, frameOnScreen.origin.y); |
| 1368 |
r_delta.x = conf_pos.x - prev_conf_pos.x; |
| 1369 |
r_delta.y = conf_pos.y - prev_conf_pos.y; |
| 1370 |
|
| 1371 |
NSPoint wnd_point = NSMakePoint(conf_pos.x, CGDisplayBounds(CGMainDisplayID()).size.height - conf_pos.y); |
| 1372 |
r_mpos = [[p_wd.window_view window] convertPointFromScreen:wnd_point]; |
| 1373 |
|
| 1374 |
// Move mouse cursor. |
| 1375 |
CGWarpMouseCursorPosition(conf_pos); |
| 1376 |
|
| 1377 |
// Save warp data. |
| 1378 |
last_warp = [[NSProcessInfo processInfo] systemUptime]; |
| 1379 |
|
| 1380 |
DisplayServerMacOS::WarpEvent ev; |
| 1381 |
ev.timestamp = last_warp; |
| 1382 |
ev.delta = r_delta; |
| 1383 |
warp_events.push_back(ev); |
| 1384 |
} |
| 1385 |
|
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
void DisplayServerMacOS::warp_mouse(const Point2i &p_position) { |
| 1390 |
_THREAD_SAFE_METHOD_ |
| 1391 |
|
| 1392 |
if (mouse_mode != MOUSE_MODE_CAPTURED) { |
| 1393 |
WindowID window_id = _get_focused_window_or_popup(); |
| 1394 |
if (!windows.has(window_id)) { |
| 1395 |
window_id = MAIN_WINDOW_ID; |
| 1396 |
} |
| 1397 |
WindowData &wd = windows[window_id]; |
| 1398 |
|
| 1399 |
// Local point in window coords. |
| 1400 |
const NSRect contentRect = [wd.window_view frame]; |
| 1401 |
const float scale = screen_get_max_scale(); |
| 1402 |
NSRect pointInWindowRect = NSMakeRect(p_position.x / scale, contentRect.size.height - (p_position.y / scale), scale, scale); |
| 1403 |
NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin; |
| 1404 |
|
| 1405 |
// Point in screen coords. |
| 1406 |
CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y }; |
| 1407 |
|
| 1408 |
// Do the warping. |
| 1409 |
CGEventSourceRef lEventRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); |
| 1410 |
CGEventSourceSetLocalEventsSuppressionInterval(lEventRef, 0.0); |
| 1411 |
CGAssociateMouseAndMouseCursorPosition(false); |
| 1412 |
CGWarpMouseCursorPosition(lMouseWarpPos); |
| 1413 |
if (mouse_mode != MOUSE_MODE_CONFINED && mouse_mode != MOUSE_MODE_CONFINED_HIDDEN) { |
| 1414 |
CGAssociateMouseAndMouseCursorPosition(true); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
} |
| 1418 |
|
| 1419 |
Point2i DisplayServerMacOS::mouse_get_position() const { |
| 1420 |
_THREAD_SAFE_METHOD_ |
| 1421 |
|
| 1422 |
const NSPoint mouse_pos = [NSEvent mouseLocation]; |
| 1423 |
const float scale = screen_get_max_scale(); |
| 1424 |
|
| 1425 |
for (NSScreen *screen in [NSScreen screens]) { |
| 1426 |
NSRect frame = [screen frame]; |
| 1427 |
if (NSMouseInRect(mouse_pos, frame, NO)) { |
| 1428 |
Vector2i pos = Vector2i((int)mouse_pos.x, (int)mouse_pos.y); |
| 1429 |
pos *= scale; |
| 1430 |
pos -= _get_screens_origin(); |
| 1431 |
pos.y *= -1; |
| 1432 |
return pos; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
return Vector2i(); |
| 1436 |
} |
| 1437 |
|
| 1438 |
BitField<MouseButtonMask> DisplayServerMacOS::mouse_get_button_state() const { |
| 1439 |
BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 1440 |
|
| 1441 |
NSUInteger buttons = [NSEvent pressedMouseButtons]; |
| 1442 |
if (buttons & (1 << 0)) { |
| 1443 |
last_button_state.set_flag(MouseButtonMask::LEFT); |
| 1444 |
} |
| 1445 |
if (buttons & (1 << 1)) { |
| 1446 |
last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 1447 |
} |
| 1448 |
if (buttons & (1 << 2)) { |
| 1449 |
last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 1450 |
} |
| 1451 |
if (buttons & (1 << 3)) { |
| 1452 |
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 1453 |
} |
| 1454 |
if (buttons & (1 << 4)) { |
| 1455 |
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 1456 |
} |
| 1457 |
return last_button_state; |
| 1458 |
} |
| 1459 |
|
| 1460 |
int DisplayServerMacOS::get_screen_count() const { |
| 1461 |
_THREAD_SAFE_METHOD_ |
| 1462 |
|
| 1463 |
NSArray *screenArray = [NSScreen screens]; |
| 1464 |
return [screenArray count]; |
| 1465 |
} |
| 1466 |
|
| 1467 |
int DisplayServerMacOS::get_primary_screen() const { |
| 1468 |
return 0; |
| 1469 |
} |
| 1470 |
|
| 1471 |
int DisplayServerMacOS::get_keyboard_focus_screen() const { |
| 1472 |
const NSUInteger index = [[NSScreen screens] indexOfObject:[NSScreen mainScreen]]; |
| 1473 |
return (index == NSNotFound) ? get_primary_screen() : index; |
| 1474 |
} |
| 1475 |
|
| 1476 |
Point2i DisplayServerMacOS::screen_get_position(int p_screen) const { |
| 1477 |
_THREAD_SAFE_METHOD_ |
| 1478 |
|
| 1479 |
p_screen = _get_screen_index(p_screen); |
| 1480 |
int screen_count = get_screen_count(); |
| 1481 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i()); |
| 1482 |
|
| 1483 |
Point2i position = _get_native_screen_position(p_screen) - _get_screens_origin(); |
| 1484 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 1485 |
// Godot expects a positive value. |
| 1486 |
position.y *= -1; |
| 1487 |
return position; |
| 1488 |
} |
| 1489 |
|
| 1490 |
Size2i DisplayServerMacOS::screen_get_size(int p_screen) const { |
| 1491 |
_THREAD_SAFE_METHOD_ |
| 1492 |
|
| 1493 |
p_screen = _get_screen_index(p_screen); |
| 1494 |
int screen_count = get_screen_count(); |
| 1495 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i()); |
| 1496 |
|
| 1497 |
NSArray *screenArray = [NSScreen screens]; |
| 1498 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 1499 |
// Note: Use frame to get the whole screen size. |
| 1500 |
NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame]; |
| 1501 |
return Size2i(nsrect.size.width, nsrect.size.height) * screen_get_max_scale(); |
| 1502 |
} |
| 1503 |
|
| 1504 |
return Size2i(); |
| 1505 |
} |
| 1506 |
|
| 1507 |
int DisplayServerMacOS::screen_get_dpi(int p_screen) const { |
| 1508 |
_THREAD_SAFE_METHOD_ |
| 1509 |
|
| 1510 |
p_screen = _get_screen_index(p_screen); |
| 1511 |
int screen_count = get_screen_count(); |
| 1512 |
ERR_FAIL_INDEX_V(p_screen, screen_count, 72); |
| 1513 |
|
| 1514 |
NSArray *screenArray = [NSScreen screens]; |
| 1515 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 1516 |
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription]; |
| 1517 |
|
| 1518 |
const NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue]; |
| 1519 |
const CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]); |
| 1520 |
float scale = [[screenArray objectAtIndex:p_screen] backingScaleFactor]; |
| 1521 |
|
| 1522 |
float den2 = (displayPhysicalSize.width / 25.4f) * (displayPhysicalSize.width / 25.4f) + (displayPhysicalSize.height / 25.4f) * (displayPhysicalSize.height / 25.4f); |
| 1523 |
if (den2 > 0.0f) { |
| 1524 |
return std::ceil(std::sqrt(displayPixelSize.width * displayPixelSize.width + displayPixelSize.height * displayPixelSize.height) / std::sqrt(den2) * scale); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
return 72; |
| 1529 |
} |
| 1530 |
|
| 1531 |
float DisplayServerMacOS::screen_get_scale(int p_screen) const { |
| 1532 |
_THREAD_SAFE_METHOD_ |
| 1533 |
|
| 1534 |
p_screen = _get_screen_index(p_screen); |
| 1535 |
int screen_count = get_screen_count(); |
| 1536 |
ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f); |
| 1537 |
|
| 1538 |
if (OS::get_singleton()->is_hidpi_allowed()) { |
| 1539 |
NSArray<NSScreen *> *screens = NSScreen.screens; |
| 1540 |
NSUInteger index = (NSUInteger)p_screen; |
| 1541 |
if (index < screens.count) { |
| 1542 |
return std::fmax(1.0f, screens[index].backingScaleFactor); |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
return 1.f; |
| 1547 |
} |
| 1548 |
|
| 1549 |
float DisplayServerMacOS::screen_get_max_scale() const { |
| 1550 |
_THREAD_SAFE_METHOD_ |
| 1551 |
|
| 1552 |
// Note: Do not update max display scale on screen configuration change, existing editor windows can't be rescaled on the fly. |
| 1553 |
return display_max_scale; |
| 1554 |
} |
| 1555 |
|
| 1556 |
Rect2i DisplayServerMacOS::screen_get_usable_rect(int p_screen) const { |
| 1557 |
_THREAD_SAFE_METHOD_ |
| 1558 |
|
| 1559 |
p_screen = _get_screen_index(p_screen); |
| 1560 |
int screen_count = get_screen_count(); |
| 1561 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 1562 |
|
| 1563 |
NSArray *screenArray = [NSScreen screens]; |
| 1564 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 1565 |
const float scale = screen_get_max_scale(); |
| 1566 |
NSRect nsrect = [[screenArray objectAtIndex:p_screen] visibleFrame]; |
| 1567 |
|
| 1568 |
Point2i position = Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * scale - _get_screens_origin(); |
| 1569 |
position.y *= -1; |
| 1570 |
Size2i size = Size2i(nsrect.size.width, nsrect.size.height) * scale; |
| 1571 |
|
| 1572 |
return Rect2i(position, size); |
| 1573 |
} |
| 1574 |
|
| 1575 |
return Rect2i(); |
| 1576 |
} |
| 1577 |
|
| 1578 |
Color DisplayServerMacOS::screen_get_pixel(const Point2i &p_position) const { |
| 1579 |
HashSet<CGWindowID> exclude_windows; |
| 1580 |
for (HashMap<WindowID, WindowData>::ConstIterator E = windows.begin(); E; ++E) { |
| 1581 |
if (E->value.hide_from_capture) { |
| 1582 |
exclude_windows.insert([E->value.window_object windowNumber]); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
CFArrayRef on_screen_windows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); |
| 1587 |
CFMutableArrayRef capture_windows = CFArrayCreateMutableCopy(nullptr, 0, on_screen_windows); |
| 1588 |
for (long i = CFArrayGetCount(on_screen_windows) - 1; i >= 0; i--) { |
| 1589 |
CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(capture_windows, i); |
| 1590 |
if (exclude_windows.has(window)) { |
| 1591 |
CFArrayRemoveValueAtIndex(capture_windows, i); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
Point2i position = p_position - Vector2i(1, 1); |
| 1596 |
position -= screen_get_position(0); // Note: coordinates where the screen origin is in the upper-left corner of the main display and y-axis values increase downward. |
| 1597 |
position /= screen_get_max_scale(); |
| 1598 |
|
| 1599 |
Color color; |
| 1600 |
CGImageRef image = CGWindowListCreateImageFromArray(CGRectMake(position.x, position.y, 1, 1), capture_windows, kCGWindowListOptionAll); |
| 1601 |
if (image) { |
| 1602 |
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); |
| 1603 |
if (color_space) { |
| 1604 |
uint8_t img_data[4]; |
| 1605 |
CGContextRef context = CGBitmapContextCreate(img_data, 1, 1, 8, 4, color_space, (uint32_t)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); |
| 1606 |
if (context) { |
| 1607 |
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image); |
| 1608 |
color = Color(img_data[0] / 255.0f, img_data[1] / 255.0f, img_data[2] / 255.0f, img_data[3] / 255.0f); |
| 1609 |
CGContextRelease(context); |
| 1610 |
} |
| 1611 |
CGColorSpaceRelease(color_space); |
| 1612 |
} |
| 1613 |
CGImageRelease(image); |
| 1614 |
} |
| 1615 |
return color; |
| 1616 |
} |
| 1617 |
|
| 1618 |
Ref<Image> DisplayServerMacOS::screen_get_image(int p_screen) const { |
| 1619 |
p_screen = _get_screen_index(p_screen); |
| 1620 |
int screen_count = get_screen_count(); |
| 1621 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Ref<Image>()); |
| 1622 |
|
| 1623 |
HashSet<CGWindowID> exclude_windows; |
| 1624 |
for (HashMap<WindowID, WindowData>::ConstIterator E = windows.begin(); E; ++E) { |
| 1625 |
if (E->value.hide_from_capture) { |
| 1626 |
exclude_windows.insert([E->value.window_object windowNumber]); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
CFArrayRef on_screen_windows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); |
| 1631 |
CFMutableArrayRef capture_windows = CFArrayCreateMutableCopy(nullptr, 0, on_screen_windows); |
| 1632 |
for (long i = CFArrayGetCount(on_screen_windows) - 1; i >= 0; i--) { |
| 1633 |
CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(capture_windows, i); |
| 1634 |
if (exclude_windows.has(window)) { |
| 1635 |
CFArrayRemoveValueAtIndex(capture_windows, i); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
Point2i position = screen_get_position(p_screen); |
| 1640 |
position -= screen_get_position(0); // Note: coordinates where the screen origin is in the upper-left corner of the main display and y-axis values increase downward. |
| 1641 |
position /= screen_get_max_scale(); |
| 1642 |
|
| 1643 |
Size2i size = screen_get_size(p_screen); |
| 1644 |
size /= screen_get_max_scale(); |
| 1645 |
|
| 1646 |
Ref<Image> img; |
| 1647 |
CGImageRef image = CGWindowListCreateImageFromArray(CGRectMake(position.x, position.y, size.width, size.height), capture_windows, kCGWindowListOptionAll); |
| 1648 |
if (image) { |
| 1649 |
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); |
| 1650 |
if (color_space) { |
| 1651 |
NSUInteger width = CGImageGetWidth(image); |
| 1652 |
NSUInteger height = CGImageGetHeight(image); |
| 1653 |
|
| 1654 |
Vector<uint8_t> img_data; |
| 1655 |
img_data.resize(height * width * 4); |
| 1656 |
CGContextRef context = CGBitmapContextCreate(img_data.ptrw(), width, height, 8, 4 * width, color_space, (uint32_t)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); |
| 1657 |
if (context) { |
| 1658 |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); |
| 1659 |
img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data); |
| 1660 |
CGContextRelease(context); |
| 1661 |
img->resize(screen_get_size(p_screen).x, screen_get_size(p_screen).y, Image::INTERPOLATE_NEAREST); |
| 1662 |
} |
| 1663 |
CGColorSpaceRelease(color_space); |
| 1664 |
} |
| 1665 |
CGImageRelease(image); |
| 1666 |
} |
| 1667 |
return img; |
| 1668 |
} |
| 1669 |
|
| 1670 |
Ref<Image> DisplayServerMacOS::screen_get_image_rect(const Rect2i &p_rect) const { |
| 1671 |
HashSet<CGWindowID> exclude_windows; |
| 1672 |
for (HashMap<WindowID, WindowData>::ConstIterator E = windows.begin(); E; ++E) { |
| 1673 |
if (E->value.hide_from_capture) { |
| 1674 |
exclude_windows.insert([E->value.window_object windowNumber]); |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
CFArrayRef on_screen_windows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); |
| 1679 |
CFMutableArrayRef capture_windows = CFArrayCreateMutableCopy(nullptr, 0, on_screen_windows); |
| 1680 |
for (long i = CFArrayGetCount(on_screen_windows) - 1; i >= 0; i--) { |
| 1681 |
CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(capture_windows, i); |
| 1682 |
if (exclude_windows.has(window)) { |
| 1683 |
CFArrayRemoveValueAtIndex(capture_windows, i); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
Point2i position = p_rect.position; |
| 1688 |
position -= screen_get_position(0); // Note: coordinates where the screen origin is in the upper-left corner of the main display and y-axis values increase downward. |
| 1689 |
position /= screen_get_max_scale(); |
| 1690 |
|
| 1691 |
Size2i size = p_rect.size; |
| 1692 |
size /= screen_get_max_scale(); |
| 1693 |
|
| 1694 |
Ref<Image> img; |
| 1695 |
CGImageRef image = CGWindowListCreateImageFromArray(CGRectMake(position.x, position.y, size.width, size.height), capture_windows, kCGWindowListOptionAll); |
| 1696 |
if (image) { |
| 1697 |
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); |
| 1698 |
if (color_space) { |
| 1699 |
NSUInteger width = CGImageGetWidth(image); |
| 1700 |
NSUInteger height = CGImageGetHeight(image); |
| 1701 |
|
| 1702 |
Vector<uint8_t> img_data; |
| 1703 |
img_data.resize_initialized(height * width * 4); |
| 1704 |
CGContextRef context = CGBitmapContextCreate(img_data.ptrw(), width, height, 8, 4 * width, color_space, (uint32_t)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); |
| 1705 |
if (context) { |
| 1706 |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); |
| 1707 |
img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data); |
| 1708 |
CGContextRelease(context); |
| 1709 |
img->resize(p_rect.size.x, p_rect.size.y, Image::INTERPOLATE_NEAREST); |
| 1710 |
} |
| 1711 |
CGColorSpaceRelease(color_space); |
| 1712 |
} |
| 1713 |
CGImageRelease(image); |
| 1714 |
} |
| 1715 |
return img; |
| 1716 |
} |
| 1717 |
|
| 1718 |
float DisplayServerMacOS::screen_get_refresh_rate(int p_screen) const { |
| 1719 |
_THREAD_SAFE_METHOD_ |
| 1720 |
|
| 1721 |
p_screen = _get_screen_index(p_screen); |
| 1722 |
int screen_count = get_screen_count(); |
| 1723 |
ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 1724 |
|
| 1725 |
NSArray *screenArray = [NSScreen screens]; |
| 1726 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 1727 |
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription]; |
| 1728 |
const CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]); |
| 1729 |
const double displayRefreshRate = CGDisplayModeGetRefreshRate(displayMode); |
| 1730 |
return (float)displayRefreshRate; |
| 1731 |
} |
| 1732 |
ERR_PRINT("An error occurred while trying to get the screen refresh rate."); |
| 1733 |
return SCREEN_REFRESH_RATE_FALLBACK; |
| 1734 |
} |
| 1735 |
|
| 1736 |
bool DisplayServerMacOS::screen_is_kept_on() const { |
| 1737 |
return (screen_keep_on_assertion); |
| 1738 |
} |
| 1739 |
|
| 1740 |
void DisplayServerMacOS::screen_set_keep_on(bool p_enable) { |
| 1741 |
if (screen_keep_on_assertion) { |
| 1742 |
IOPMAssertionRelease(screen_keep_on_assertion); |
| 1743 |
screen_keep_on_assertion = kIOPMNullAssertionID; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (p_enable) { |
| 1747 |
String app_name_string = GLOBAL_GET("application/config/name"); |
| 1748 |
NSString *name = [NSString stringWithUTF8String:(app_name_string.is_empty() ? "Godot Engine" : app_name_string.utf8().get_data())]; |
| 1749 |
NSString *reason = @"Godot Engine running with display/window/energy_saving/keep_screen_on = true"; |
| 1750 |
IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep, (__bridge CFStringRef)name, (__bridge CFStringRef)reason, (__bridge CFStringRef)reason, nullptr, 0, nullptr, &screen_keep_on_assertion); |
| 1751 |
} |
| 1752 |
} |
| 1753 |
|
| 1754 |
Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const { |
| 1755 |
_THREAD_SAFE_METHOD_ |
| 1756 |
|
| 1757 |
Vector<int> ret; |
| 1758 |
for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 1759 |
ret.push_back(E.key); |
| 1760 |
} |
| 1761 |
return ret; |
| 1762 |
} |
| 1763 |
|
| 1764 |
DisplayServer::WindowID DisplayServerMacOS::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) { |
| 1765 |
_THREAD_SAFE_METHOD_ |
| 1766 |
|
| 1767 |
WindowID id = _create_window(p_mode, p_vsync_mode, p_rect); |
| 1768 |
for (int i = 0; i < WINDOW_FLAG_MAX; i++) { |
| 1769 |
if (p_flags & (1 << i)) { |
| 1770 |
window_set_flag(WindowFlags(i), true, id); |
| 1771 |
} |
| 1772 |
} |
| 1773 |
#ifdef RD_ENABLED |
| 1774 |
if (rendering_device) { |
| 1775 |
rendering_device->screen_create(id); |
| 1776 |
} |
| 1777 |
#endif |
| 1778 |
|
| 1779 |
window_set_exclusive(id, p_exclusive); |
| 1780 |
if (p_transient_parent != INVALID_WINDOW_ID) { |
| 1781 |
window_set_transient(id, p_transient_parent); |
| 1782 |
} |
| 1783 |
|
| 1784 |
return id; |
| 1785 |
} |
| 1786 |
|
| 1787 |
void DisplayServerMacOS::show_window(WindowID p_id) { |
| 1788 |
WindowData &wd = windows[p_id]; |
| 1789 |
|
| 1790 |
if (p_id == MAIN_WINDOW_ID) { |
| 1791 |
[GodotApp activateApplication]; |
| 1792 |
} |
| 1793 |
|
| 1794 |
popup_open(p_id); |
| 1795 |
if ([wd.window_object isMiniaturized]) { |
| 1796 |
return; |
| 1797 |
} else if (wd.no_focus) { |
| 1798 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 1799 |
WindowData &wd_parent = windows[wd.transient_parent]; |
| 1800 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[wd_parent.window_object windowNumber]]; |
| 1801 |
} else if (p_id != MAIN_WINDOW_ID) { |
| 1802 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[windows[MAIN_WINDOW_ID].window_object windowNumber]]; |
| 1803 |
} else { |
| 1804 |
[wd.window_object orderFront:nil]; |
| 1805 |
} |
| 1806 |
} else { |
| 1807 |
[wd.window_object makeKeyAndOrderFront:nil]; |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
void DisplayServerMacOS::delete_sub_window(WindowID p_id) { |
| 1812 |
_THREAD_SAFE_METHOD_ |
| 1813 |
|
| 1814 |
ERR_FAIL_COND(!windows.has(p_id)); |
| 1815 |
ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted"); |
| 1816 |
|
| 1817 |
WindowData &wd = windows[p_id]; |
| 1818 |
|
| 1819 |
[wd.window_object setContentView:nil]; |
| 1820 |
// This will cause the delegate to release the window. |
| 1821 |
[wd.window_object close]; |
| 1822 |
|
| 1823 |
mouse_enter_window(get_window_at_screen_position(mouse_get_position())); |
| 1824 |
} |
| 1825 |
|
| 1826 |
void DisplayServerMacOS::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) { |
| 1827 |
_THREAD_SAFE_METHOD_ |
| 1828 |
|
| 1829 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1830 |
WindowData &wd = windows[p_window]; |
| 1831 |
wd.rect_changed_callback = p_callable; |
| 1832 |
} |
| 1833 |
|
| 1834 |
void DisplayServerMacOS::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1835 |
_THREAD_SAFE_METHOD_ |
| 1836 |
|
| 1837 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1838 |
WindowData &wd = windows[p_window]; |
| 1839 |
wd.event_callback = p_callable; |
| 1840 |
} |
| 1841 |
|
| 1842 |
void DisplayServerMacOS::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) { |
| 1843 |
_THREAD_SAFE_METHOD_ |
| 1844 |
|
| 1845 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1846 |
WindowData &wd = windows[p_window]; |
| 1847 |
wd.input_event_callback = p_callable; |
| 1848 |
} |
| 1849 |
|
| 1850 |
void DisplayServerMacOS::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) { |
| 1851 |
_THREAD_SAFE_METHOD_ |
| 1852 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1853 |
WindowData &wd = windows[p_window]; |
| 1854 |
wd.input_text_callback = p_callable; |
| 1855 |
} |
| 1856 |
|
| 1857 |
void DisplayServerMacOS::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) { |
| 1858 |
_THREAD_SAFE_METHOD_ |
| 1859 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1860 |
WindowData &wd = windows[p_window]; |
| 1861 |
wd.drop_files_callback = p_callable; |
| 1862 |
} |
| 1863 |
|
| 1864 |
void DisplayServerMacOS::window_set_title(const String &p_title, WindowID p_window) { |
| 1865 |
_THREAD_SAFE_METHOD_ |
| 1866 |
|
| 1867 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1868 |
WindowData &wd = windows[p_window]; |
| 1869 |
|
| 1870 |
[wd.window_object setTitle:[NSString stringWithUTF8String:p_title.utf8().get_data()]]; |
| 1871 |
} |
| 1872 |
|
| 1873 |
Size2i DisplayServerMacOS::window_get_title_size(const String &p_title, WindowID p_window) const { |
| 1874 |
_THREAD_SAFE_METHOD_ |
| 1875 |
|
| 1876 |
Size2i size; |
| 1877 |
ERR_FAIL_COND_V(!windows.has(p_window), size); |
| 1878 |
|
| 1879 |
const WindowData &wd = windows[p_window]; |
| 1880 |
if (wd.fullscreen || wd.borderless) { |
| 1881 |
return size; |
| 1882 |
} |
| 1883 |
if ([wd.window_object respondsToSelector:@selector(isMiniaturized)]) { |
| 1884 |
if ([wd.window_object isMiniaturized]) { |
| 1885 |
return size; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
float scale = screen_get_max_scale(); |
| 1890 |
|
| 1891 |
if (wd.window_button_view) { |
| 1892 |
size.x = ([wd.window_button_view getOffset].x + [wd.window_button_view frame].size.width); |
| 1893 |
size.y = ([wd.window_button_view getOffset].y + [wd.window_button_view frame].size.height); |
| 1894 |
} else { |
| 1895 |
NSButton *cb = [wd.window_object standardWindowButton:NSWindowCloseButton]; |
| 1896 |
NSButton *mb = [wd.window_object standardWindowButton:NSWindowMiniaturizeButton]; |
| 1897 |
float cb_frame = NSMinX([cb frame]); |
| 1898 |
float mb_frame = NSMinX([mb frame]); |
| 1899 |
bool is_rtl = ([wd.window_object windowTitlebarLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft); |
| 1900 |
|
| 1901 |
float window_buttons_spacing = (is_rtl) ? (cb_frame - mb_frame) : (mb_frame - cb_frame); |
| 1902 |
size.x = window_buttons_spacing * 4; |
| 1903 |
size.y = [cb frame].origin.y + [cb frame].size.height; |
| 1904 |
} |
| 1905 |
|
| 1906 |
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont titleBarFontOfSize:0], NSFontAttributeName, nil]; |
| 1907 |
NSSize text_size = [[[NSAttributedString alloc] initWithString:[NSString stringWithUTF8String:p_title.utf8().get_data()] attributes:attributes] size]; |
| 1908 |
size.x += text_size.width; |
| 1909 |
size.y = MAX(size.y, text_size.height); |
| 1910 |
|
| 1911 |
return size * scale; |
| 1912 |
} |
| 1913 |
|
| 1914 |
void DisplayServerMacOS::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) { |
| 1915 |
_THREAD_SAFE_METHOD_ |
| 1916 |
|
| 1917 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1918 |
WindowData &wd = windows[p_window]; |
| 1919 |
|
| 1920 |
wd.mpath = p_region; |
| 1921 |
} |
| 1922 |
|
| 1923 |
int DisplayServerMacOS::window_get_current_screen(WindowID p_window) const { |
| 1924 |
_THREAD_SAFE_METHOD_ |
| 1925 |
ERR_FAIL_COND_V(!windows.has(p_window), INVALID_SCREEN); |
| 1926 |
const WindowData &wd = windows[p_window]; |
| 1927 |
|
| 1928 |
const NSUInteger index = [[NSScreen screens] indexOfObject:[wd.window_object screen]]; |
| 1929 |
return (index == NSNotFound) ? 0 : index; |
| 1930 |
} |
| 1931 |
|
| 1932 |
void DisplayServerMacOS::window_set_current_screen(int p_screen, WindowID p_window) { |
| 1933 |
_THREAD_SAFE_METHOD_ |
| 1934 |
|
| 1935 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1936 |
|
| 1937 |
p_screen = _get_screen_index(p_screen); |
| 1938 |
int screen_count = get_screen_count(); |
| 1939 |
ERR_FAIL_INDEX(p_screen, screen_count); |
| 1940 |
|
| 1941 |
if (window_get_current_screen(p_window) == p_screen) { |
| 1942 |
return; |
| 1943 |
} |
| 1944 |
WindowData &wd = windows[p_window]; |
| 1945 |
|
| 1946 |
bool was_fullscreen = false; |
| 1947 |
if (wd.fullscreen) { |
| 1948 |
// Temporary exit fullscreen mode to move window. |
| 1949 |
[wd.window_object toggleFullScreen:nil]; |
| 1950 |
was_fullscreen = true; |
| 1951 |
} |
| 1952 |
|
| 1953 |
bool was_maximized = false; |
| 1954 |
if (!was_fullscreen && NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) { |
| 1955 |
[wd.window_object zoom:nil]; |
| 1956 |
was_maximized = true; |
| 1957 |
} |
| 1958 |
|
| 1959 |
Rect2i srect = screen_get_usable_rect(p_screen); |
| 1960 |
Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window)); |
| 1961 |
Size2i wsize = window_get_size(p_window); |
| 1962 |
wpos += srect.position; |
| 1963 |
|
| 1964 |
wpos = wpos.clamp(srect.position, srect.position + srect.size - wsize / 3); |
| 1965 |
window_set_position(wpos, p_window); |
| 1966 |
|
| 1967 |
if (was_maximized) { |
| 1968 |
[wd.window_object zoom:nil]; |
| 1969 |
} |
| 1970 |
|
| 1971 |
if (was_fullscreen) { |
| 1972 |
// Re-enter fullscreen mode. |
| 1973 |
[wd.window_object toggleFullScreen:nil]; |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
void DisplayServerMacOS::reparent_check(WindowID p_window) { |
| 1978 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 1979 |
WindowData &wd = windows[p_window]; |
| 1980 |
NSScreen *screen = [wd.window_object screen]; |
| 1981 |
|
| 1982 |
_window_update_display_id(&wd); |
| 1983 |
|
| 1984 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 1985 |
WindowData &wd_parent = windows[wd.transient_parent]; |
| 1986 |
NSScreen *parent_screen = [wd_parent.window_object screen]; |
| 1987 |
|
| 1988 |
if (parent_screen == screen) { |
| 1989 |
if (![[wd_parent.window_object childWindows] containsObject:wd.window_object]) { |
| 1990 |
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary]; |
| 1991 |
[wd_parent.window_object addChildWindow:wd.window_object ordered:NSWindowAbove]; |
| 1992 |
} |
| 1993 |
} else { |
| 1994 |
if ([[wd_parent.window_object childWindows] containsObject:wd.window_object]) { |
| 1995 |
[wd_parent.window_object removeChildWindow:wd.window_object]; |
| 1996 |
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; |
| 1997 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 1998 |
WindowData &wd_parent = windows[wd.transient_parent]; |
| 1999 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[wd_parent.window_object windowNumber]]; |
| 2000 |
} else if (p_window != MAIN_WINDOW_ID) { |
| 2001 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[windows[MAIN_WINDOW_ID].window_object windowNumber]]; |
| 2002 |
} else { |
| 2003 |
[wd.window_object orderFront:nil]; |
| 2004 |
} |
| 2005 |
} |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
for (const WindowID &child : wd.transient_children) { |
| 2010 |
WindowData &wd_child = windows[child]; |
| 2011 |
NSScreen *child_screen = [wd_child.window_object screen]; |
| 2012 |
|
| 2013 |
if (child_screen == screen) { |
| 2014 |
if (![[wd.window_object childWindows] containsObject:wd_child.window_object]) { |
| 2015 |
if (wd_child.fullscreen) { |
| 2016 |
[wd_child.window_object toggleFullScreen:nil]; |
| 2017 |
} |
| 2018 |
[wd_child.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary]; |
| 2019 |
[wd.window_object addChildWindow:wd_child.window_object ordered:NSWindowAbove]; |
| 2020 |
} |
| 2021 |
} else { |
| 2022 |
if ([[wd.window_object childWindows] containsObject:wd_child.window_object]) { |
| 2023 |
[wd.window_object removeChildWindow:wd_child.window_object]; |
| 2024 |
[wd_child.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; |
| 2025 |
} |
| 2026 |
} |
| 2027 |
} |
| 2028 |
} |
| 2029 |
|
| 2030 |
void DisplayServerMacOS::window_set_exclusive(WindowID p_window, bool p_exclusive) { |
| 2031 |
_THREAD_SAFE_METHOD_ |
| 2032 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2033 |
WindowData &wd = windows[p_window]; |
| 2034 |
if (wd.exclusive != p_exclusive) { |
| 2035 |
wd.exclusive = p_exclusive; |
| 2036 |
reparent_check(p_window); |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
Point2i DisplayServerMacOS::window_get_position(WindowID p_window) const { |
| 2041 |
_THREAD_SAFE_METHOD_ |
| 2042 |
|
| 2043 |
ERR_FAIL_COND_V(!windows.has(p_window), Point2i()); |
| 2044 |
const WindowData &wd = windows[p_window]; |
| 2045 |
|
| 2046 |
// Use content rect position (without titlebar / window border). |
| 2047 |
const NSRect contentRect = [wd.window_view frame]; |
| 2048 |
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect]; |
| 2049 |
Point2i pos; |
| 2050 |
|
| 2051 |
// Return the position of the top-left corner, for macOS the y starts at the bottom. |
| 2052 |
const float scale = screen_get_max_scale(); |
| 2053 |
pos.x = nsrect.origin.x; |
| 2054 |
pos.y = (nsrect.origin.y + nsrect.size.height); |
| 2055 |
pos *= scale; |
| 2056 |
pos -= _get_screens_origin(); |
| 2057 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 2058 |
// Godot expects a positive value. |
| 2059 |
pos.y *= -1; |
| 2060 |
return pos; |
| 2061 |
} |
| 2062 |
|
| 2063 |
Point2i DisplayServerMacOS::window_get_position_with_decorations(WindowID p_window) const { |
| 2064 |
_THREAD_SAFE_METHOD_ |
| 2065 |
|
| 2066 |
ERR_FAIL_COND_V(!windows.has(p_window), Point2i()); |
| 2067 |
const WindowData &wd = windows[p_window]; |
| 2068 |
|
| 2069 |
const NSRect nsrect = [wd.window_object frame]; |
| 2070 |
Point2i pos; |
| 2071 |
|
| 2072 |
// Return the position of the top-left corner, for macOS the y starts at the bottom. |
| 2073 |
const float scale = screen_get_max_scale(); |
| 2074 |
pos.x = nsrect.origin.x; |
| 2075 |
pos.y = (nsrect.origin.y + nsrect.size.height); |
| 2076 |
pos *= scale; |
| 2077 |
pos -= _get_screens_origin(); |
| 2078 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 2079 |
// Godot expects a positive value. |
| 2080 |
pos.y *= -1; |
| 2081 |
return pos; |
| 2082 |
} |
| 2083 |
|
| 2084 |
void DisplayServerMacOS::window_set_position(const Point2i &p_position, WindowID p_window) { |
| 2085 |
_THREAD_SAFE_METHOD_ |
| 2086 |
|
| 2087 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2088 |
WindowData &wd = windows[p_window]; |
| 2089 |
|
| 2090 |
if (wd.fullscreen) { |
| 2091 |
return; |
| 2092 |
} |
| 2093 |
|
| 2094 |
Point2i position = p_position; |
| 2095 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 2096 |
// Godot passes a positive value. |
| 2097 |
position.y *= -1; |
| 2098 |
position += _get_screens_origin(); |
| 2099 |
position /= screen_get_max_scale(); |
| 2100 |
|
| 2101 |
// Remove titlebar / window border size. |
| 2102 |
const NSRect contentRect = [wd.window_view frame]; |
| 2103 |
const NSRect windowRect = [wd.window_object frame]; |
| 2104 |
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect]; |
| 2105 |
Point2i offset; |
| 2106 |
offset.x = (nsrect.origin.x - windowRect.origin.x); |
| 2107 |
offset.y = (nsrect.origin.y + nsrect.size.height); |
| 2108 |
offset.y -= (windowRect.origin.y + windowRect.size.height); |
| 2109 |
|
| 2110 |
[wd.window_object setFrameTopLeftPoint:NSMakePoint(position.x - offset.x, position.y - offset.y)]; |
| 2111 |
|
| 2112 |
_update_window_style(wd, p_window); |
| 2113 |
update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); |
| 2114 |
} |
| 2115 |
|
| 2116 |
void DisplayServerMacOS::window_set_transient(WindowID p_window, WindowID p_parent) { |
| 2117 |
_THREAD_SAFE_METHOD_ |
| 2118 |
ERR_FAIL_COND(p_window == p_parent); |
| 2119 |
|
| 2120 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2121 |
WindowData &wd_window = windows[p_window]; |
| 2122 |
|
| 2123 |
ERR_FAIL_COND(wd_window.transient_parent == p_parent); |
| 2124 |
|
| 2125 |
ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient."); |
| 2126 |
if (p_parent == INVALID_WINDOW_ID) { |
| 2127 |
// Remove transient. |
| 2128 |
ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID); |
| 2129 |
ERR_FAIL_COND(!windows.has(wd_window.transient_parent)); |
| 2130 |
|
| 2131 |
WindowData &wd_parent = windows[wd_window.transient_parent]; |
| 2132 |
|
| 2133 |
wd_window.transient_parent = INVALID_WINDOW_ID; |
| 2134 |
wd_parent.transient_children.erase(p_window); |
| 2135 |
if ([[wd_parent.window_object childWindows] containsObject:wd_window.window_object]) { |
| 2136 |
[wd_parent.window_object removeChildWindow:wd_window.window_object]; |
| 2137 |
} |
| 2138 |
[wd_window.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; |
| 2139 |
} else { |
| 2140 |
ERR_FAIL_COND(!windows.has(p_parent)); |
| 2141 |
ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent"); |
| 2142 |
WindowData &wd_parent = windows[p_parent]; |
| 2143 |
|
| 2144 |
wd_window.transient_parent = p_parent; |
| 2145 |
wd_parent.transient_children.insert(p_window); |
| 2146 |
reparent_check(p_window); |
| 2147 |
} |
| 2148 |
} |
| 2149 |
|
| 2150 |
void DisplayServerMacOS::window_set_max_size(const Size2i p_size, WindowID p_window) { |
| 2151 |
_THREAD_SAFE_METHOD_ |
| 2152 |
|
| 2153 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2154 |
WindowData &wd = windows[p_window]; |
| 2155 |
|
| 2156 |
if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) { |
| 2157 |
ERR_PRINT("Maximum window size can't be smaller than minimum window size!"); |
| 2158 |
return; |
| 2159 |
} |
| 2160 |
wd.max_size = p_size; |
| 2161 |
|
| 2162 |
if ((wd.max_size != Size2i()) && !wd.fullscreen) { |
| 2163 |
Size2i size = wd.max_size / screen_get_max_scale(); |
| 2164 |
[wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)]; |
| 2165 |
} else { |
| 2166 |
[wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)]; |
| 2167 |
} |
| 2168 |
} |
| 2169 |
|
| 2170 |
Size2i DisplayServerMacOS::window_get_max_size(WindowID p_window) const { |
| 2171 |
_THREAD_SAFE_METHOD_ |
| 2172 |
|
| 2173 |
ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2174 |
const WindowData &wd = windows[p_window]; |
| 2175 |
return wd.max_size; |
| 2176 |
} |
| 2177 |
|
| 2178 |
void DisplayServerMacOS::update_presentation_mode() { |
| 2179 |
bool has_fs_windows = false; |
| 2180 |
for (const KeyValue<WindowID, WindowData> &wd : windows) { |
| 2181 |
if (wd.value.fullscreen) { |
| 2182 |
if (wd.value.exclusive_fullscreen) { |
| 2183 |
return; |
| 2184 |
} else { |
| 2185 |
has_fs_windows = true; |
| 2186 |
} |
| 2187 |
} |
| 2188 |
} |
| 2189 |
if (has_fs_windows) { |
| 2190 |
[NSApp setPresentationOptions:NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock | NSApplicationPresentationFullScreen]; |
| 2191 |
} else { |
| 2192 |
[NSApp setPresentationOptions:NSApplicationPresentationDefault]; |
| 2193 |
} |
| 2194 |
} |
| 2195 |
|
| 2196 |
void DisplayServerMacOS::window_set_min_size(const Size2i p_size, WindowID p_window) { |
| 2197 |
_THREAD_SAFE_METHOD_ |
| 2198 |
|
| 2199 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2200 |
WindowData &wd = windows[p_window]; |
| 2201 |
|
| 2202 |
if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) { |
| 2203 |
ERR_PRINT("Minimum window size can't be larger than maximum window size!"); |
| 2204 |
return; |
| 2205 |
} |
| 2206 |
wd.min_size = p_size; |
| 2207 |
|
| 2208 |
if ((wd.min_size != Size2i()) && !wd.fullscreen) { |
| 2209 |
Size2i size = wd.min_size / screen_get_max_scale(); |
| 2210 |
[wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)]; |
| 2211 |
} else { |
| 2212 |
[wd.window_object setContentMinSize:NSMakeSize(0, 0)]; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
Size2i DisplayServerMacOS::window_get_min_size(WindowID p_window) const { |
| 2217 |
_THREAD_SAFE_METHOD_ |
| 2218 |
|
| 2219 |
ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2220 |
const WindowData &wd = windows[p_window]; |
| 2221 |
|
| 2222 |
return wd.min_size; |
| 2223 |
} |
| 2224 |
|
| 2225 |
void DisplayServerMacOS::window_set_size(const Size2i p_size, WindowID p_window) { |
| 2226 |
_THREAD_SAFE_METHOD_ |
| 2227 |
|
| 2228 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2229 |
WindowData &wd = windows[p_window]; |
| 2230 |
|
| 2231 |
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) { |
| 2232 |
return; |
| 2233 |
} |
| 2234 |
|
| 2235 |
Size2i size = p_size / screen_get_max_scale(); |
| 2236 |
|
| 2237 |
NSPoint top_left; |
| 2238 |
NSRect old_frame = [wd.window_object frame]; |
| 2239 |
top_left.x = old_frame.origin.x; |
| 2240 |
top_left.y = NSMaxY(old_frame); |
| 2241 |
|
| 2242 |
NSRect new_frame = NSMakeRect(0, 0, MAX(1, size.x), MAX(1, size.y)); |
| 2243 |
new_frame = [wd.window_object frameRectForContentRect:new_frame]; |
| 2244 |
|
| 2245 |
new_frame.origin.x = top_left.x; |
| 2246 |
new_frame.origin.y = top_left.y - new_frame.size.height; |
| 2247 |
|
| 2248 |
[wd.window_object setFrame:new_frame display:YES]; |
| 2249 |
|
| 2250 |
_update_window_style(wd, p_window); |
| 2251 |
} |
| 2252 |
|
| 2253 |
Size2i DisplayServerMacOS::window_get_size(WindowID p_window) const { |
| 2254 |
_THREAD_SAFE_METHOD_ |
| 2255 |
|
| 2256 |
ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2257 |
const WindowData &wd = windows[p_window]; |
| 2258 |
return wd.size; |
| 2259 |
} |
| 2260 |
|
| 2261 |
Size2i DisplayServerMacOS::window_get_size_with_decorations(WindowID p_window) const { |
| 2262 |
_THREAD_SAFE_METHOD_ |
| 2263 |
|
| 2264 |
ERR_FAIL_COND_V(!windows.has(p_window), Size2i()); |
| 2265 |
const WindowData &wd = windows[p_window]; |
| 2266 |
NSRect frame = [wd.window_object frame]; |
| 2267 |
return Size2i(frame.size.width, frame.size.height) * screen_get_max_scale(); |
| 2268 |
} |
| 2269 |
|
| 2270 |
void DisplayServerMacOS::window_set_mode(WindowMode p_mode, WindowID p_window) { |
| 2271 |
_THREAD_SAFE_METHOD_ |
| 2272 |
|
| 2273 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2274 |
WindowData &wd = windows[p_window]; |
| 2275 |
|
| 2276 |
WindowMode old_mode = window_get_mode(p_window); |
| 2277 |
if (old_mode == p_mode) { |
| 2278 |
return; // Do nothing. |
| 2279 |
} |
| 2280 |
|
| 2281 |
switch (old_mode) { |
| 2282 |
case WINDOW_MODE_WINDOWED: { |
| 2283 |
// Do nothing. |
| 2284 |
} break; |
| 2285 |
case WINDOW_MODE_MINIMIZED: { |
| 2286 |
[wd.window_object deminiaturize:nil]; |
| 2287 |
} break; |
| 2288 |
case WINDOW_MODE_EXCLUSIVE_FULLSCREEN: |
| 2289 |
case WINDOW_MODE_FULLSCREEN: { |
| 2290 |
if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN || p_mode == WINDOW_MODE_FULLSCREEN) { |
| 2291 |
if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2292 |
const NSUInteger presentationOptions = NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar; |
| 2293 |
[NSApp setPresentationOptions:presentationOptions]; |
| 2294 |
wd.exclusive_fullscreen = true; |
| 2295 |
} else { |
| 2296 |
wd.exclusive_fullscreen = false; |
| 2297 |
update_presentation_mode(); |
| 2298 |
} |
| 2299 |
return; |
| 2300 |
} |
| 2301 |
|
| 2302 |
[(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel]; |
| 2303 |
if (wd.resize_disabled) { // Restore resize disabled. |
| 2304 |
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable]; |
| 2305 |
} |
| 2306 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setEnabled:!wd.no_min_btn]; |
| 2307 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:!wd.no_max_btn]; |
| 2308 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2309 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2310 |
if (wd.min_size != Size2i()) { |
| 2311 |
Size2i size = wd.min_size / screen_get_max_scale(); |
| 2312 |
[wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)]; |
| 2313 |
} |
| 2314 |
if (wd.max_size != Size2i()) { |
| 2315 |
Size2i size = wd.max_size / screen_get_max_scale(); |
| 2316 |
[wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)]; |
| 2317 |
} |
| 2318 |
[wd.window_object toggleFullScreen:nil]; |
| 2319 |
|
| 2320 |
if (old_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2321 |
update_presentation_mode(); |
| 2322 |
} |
| 2323 |
|
| 2324 |
wd.fullscreen = false; |
| 2325 |
wd.exclusive_fullscreen = false; |
| 2326 |
} break; |
| 2327 |
case WINDOW_MODE_MAXIMIZED: { |
| 2328 |
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) { |
| 2329 |
if (wd.borderless) { |
| 2330 |
if (wd.pre_zoom_rect.size.width > 0 && wd.pre_zoom_rect.size.height > 0) { |
| 2331 |
[wd.window_object setFrame:wd.pre_zoom_rect display:NO animate:YES]; |
| 2332 |
} |
| 2333 |
} else { |
| 2334 |
[wd.window_object zoom:nil]; |
| 2335 |
} |
| 2336 |
} |
| 2337 |
} break; |
| 2338 |
} |
| 2339 |
|
| 2340 |
switch (p_mode) { |
| 2341 |
case WINDOW_MODE_WINDOWED: { |
| 2342 |
// Do nothing. |
| 2343 |
} break; |
| 2344 |
case WINDOW_MODE_MINIMIZED: { |
| 2345 |
[wd.window_object performMiniaturize:nil]; |
| 2346 |
} break; |
| 2347 |
case WINDOW_MODE_EXCLUSIVE_FULLSCREEN: |
| 2348 |
case WINDOW_MODE_FULLSCREEN: { |
| 2349 |
if (wd.resize_disabled) { // Fullscreen window should be resizable to work. |
| 2350 |
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable]; |
| 2351 |
} |
| 2352 |
[wd.window_object setContentMinSize:NSMakeSize(0, 0)]; |
| 2353 |
[wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)]; |
| 2354 |
[wd.window_object toggleFullScreen:nil]; |
| 2355 |
|
| 2356 |
wd.fullscreen = true; |
| 2357 |
if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) { |
| 2358 |
const NSUInteger presentationOptions = NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar; |
| 2359 |
[NSApp setPresentationOptions:presentationOptions]; |
| 2360 |
wd.exclusive_fullscreen = true; |
| 2361 |
} else { |
| 2362 |
wd.exclusive_fullscreen = false; |
| 2363 |
update_presentation_mode(); |
| 2364 |
} |
| 2365 |
} break; |
| 2366 |
case WINDOW_MODE_MAXIMIZED: { |
| 2367 |
if (!NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) { |
| 2368 |
wd.pre_zoom_rect = [wd.window_object frame]; |
| 2369 |
if (wd.borderless) { |
| 2370 |
[wd.window_object setFrame:[[wd.window_object screen] visibleFrame] display:NO animate:YES]; |
| 2371 |
} else { |
| 2372 |
[wd.window_object zoom:nil]; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
} break; |
| 2376 |
} |
| 2377 |
} |
| 2378 |
|
| 2379 |
DisplayServer::WindowMode DisplayServerMacOS::window_get_mode(WindowID p_window) const { |
| 2380 |
_THREAD_SAFE_METHOD_ |
| 2381 |
|
| 2382 |
ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED); |
| 2383 |
const WindowData &wd = windows[p_window]; |
| 2384 |
|
| 2385 |
if (wd.fullscreen) { // If fullscreen, it's not in another mode. |
| 2386 |
if (wd.exclusive_fullscreen) { |
| 2387 |
return WINDOW_MODE_EXCLUSIVE_FULLSCREEN; |
| 2388 |
} else { |
| 2389 |
return WINDOW_MODE_FULLSCREEN; |
| 2390 |
} |
| 2391 |
} |
| 2392 |
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) { |
| 2393 |
return WINDOW_MODE_MAXIMIZED; |
| 2394 |
} |
| 2395 |
if ([wd.window_object respondsToSelector:@selector(isMiniaturized)]) { |
| 2396 |
if ([wd.window_object isMiniaturized]) { |
| 2397 |
return WINDOW_MODE_MINIMIZED; |
| 2398 |
} |
| 2399 |
} |
| 2400 |
|
| 2401 |
// All other discarded, return windowed. |
| 2402 |
return WINDOW_MODE_WINDOWED; |
| 2403 |
} |
| 2404 |
|
| 2405 |
bool DisplayServerMacOS::window_is_maximize_allowed(WindowID p_window) const { |
| 2406 |
ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2407 |
const WindowData &wd = windows[p_window]; |
| 2408 |
|
| 2409 |
return [wd.window_object standardWindowButton:NSWindowZoomButton].enabled; |
| 2410 |
} |
| 2411 |
|
| 2412 |
bool DisplayServerMacOS::window_maximize_on_title_dbl_click() const { |
| 2413 |
NSString *value = [NSUserDefaults.standardUserDefaults stringForKey:@"AppleActionOnDoubleClick"]; |
| 2414 |
return [value isEqualToString:@"Maximize"]; |
| 2415 |
} |
| 2416 |
|
| 2417 |
bool DisplayServerMacOS::window_minimize_on_title_dbl_click() const { |
| 2418 |
NSString *value = [NSUserDefaults.standardUserDefaults stringForKey:@"AppleActionOnDoubleClick"]; |
| 2419 |
return [value isEqualToString:@"Minimize"]; |
| 2420 |
} |
| 2421 |
|
| 2422 |
void DisplayServerMacOS::window_start_drag(WindowID p_window) { |
| 2423 |
_THREAD_SAFE_METHOD_ |
| 2424 |
|
| 2425 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2426 |
WindowData &wd = windows[p_window]; |
| 2427 |
|
| 2428 |
NSEvent *event = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDown location:((NSWindow *)wd.window_object).mouseLocationOutsideOfEventStream modifierFlags:0 timestamp:[[NSProcessInfo processInfo] systemUptime] windowNumber:((NSWindow *)wd.window_object).windowNumber context:nil eventNumber:0 clickCount:1 pressure:1.0f]; |
| 2429 |
[wd.window_object performWindowDragWithEvent:event]; |
| 2430 |
} |
| 2431 |
|
| 2432 |
void DisplayServerMacOS::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) { |
| 2433 |
_THREAD_SAFE_METHOD_ |
| 2434 |
|
| 2435 |
ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX); |
| 2436 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2437 |
WindowData &wd = windows[p_window]; |
| 2438 |
|
| 2439 |
wd.edge = p_edge; |
| 2440 |
} |
| 2441 |
|
| 2442 |
void DisplayServerMacOS::window_set_window_buttons_offset(const Vector2i &p_offset, WindowID p_window) { |
| 2443 |
_THREAD_SAFE_METHOD_ |
| 2444 |
|
| 2445 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2446 |
WindowData &wd = windows[p_window]; |
| 2447 |
float scale = screen_get_max_scale(); |
| 2448 |
wd.wb_offset = p_offset / scale; |
| 2449 |
wd.wb_offset = wd.wb_offset.maxi(12); |
| 2450 |
if (wd.window_button_view) { |
| 2451 |
[wd.window_button_view setOffset:NSMakePoint(wd.wb_offset.x, wd.wb_offset.y)]; |
| 2452 |
} |
| 2453 |
} |
| 2454 |
|
| 2455 |
Vector3i DisplayServerMacOS::window_get_safe_title_margins(WindowID p_window) const { |
| 2456 |
_THREAD_SAFE_METHOD_ |
| 2457 |
|
| 2458 |
ERR_FAIL_COND_V(!windows.has(p_window), Vector3i()); |
| 2459 |
const WindowData &wd = windows[p_window]; |
| 2460 |
|
| 2461 |
if (!wd.window_button_view) { |
| 2462 |
return Vector3i(); |
| 2463 |
} |
| 2464 |
|
| 2465 |
float scale = screen_get_max_scale(); |
| 2466 |
float max_x = [wd.window_button_view getOffset].x + [wd.window_button_view frame].size.width; |
| 2467 |
float max_y = [wd.window_button_view getOffset].y + [wd.window_button_view frame].size.height; |
| 2468 |
|
| 2469 |
if ([wd.window_object windowTitlebarLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft) { |
| 2470 |
return Vector3i(0, max_x * scale, max_y * scale); |
| 2471 |
} else { |
| 2472 |
return Vector3i(max_x * scale, 0, max_y * scale); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
void DisplayServerMacOS::window_set_custom_window_buttons(WindowData &p_wd, bool p_enabled) { |
| 2477 |
if (p_wd.window_button_view) { |
| 2478 |
[p_wd.window_button_view removeFromSuperview]; |
| 2479 |
p_wd.window_button_view = nil; |
| 2480 |
} |
| 2481 |
if (p_enabled) { |
| 2482 |
float cb_frame = NSMinX([[p_wd.window_object standardWindowButton:NSWindowCloseButton] frame]); |
| 2483 |
float mb_frame = NSMinX([[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] frame]); |
| 2484 |
bool is_rtl = ([p_wd.window_object windowTitlebarLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft); |
| 2485 |
|
| 2486 |
float window_buttons_spacing = (is_rtl) ? (cb_frame - mb_frame) : (mb_frame - cb_frame); |
| 2487 |
|
| 2488 |
[p_wd.window_object setTitlebarAppearsTransparent:YES]; |
| 2489 |
[p_wd.window_object setTitleVisibility:NSWindowTitleHidden]; |
| 2490 |
[[p_wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:YES]; |
| 2491 |
[[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; |
| 2492 |
[[p_wd.window_object standardWindowButton:NSWindowCloseButton] setHidden:YES]; |
| 2493 |
|
| 2494 |
p_wd.window_button_view = [[GodotButtonView alloc] initWithFrame:NSZeroRect]; |
| 2495 |
[p_wd.window_button_view initButtons:window_buttons_spacing offset:NSMakePoint(p_wd.wb_offset.x, p_wd.wb_offset.y) rtl:is_rtl]; |
| 2496 |
[p_wd.window_view addSubview:p_wd.window_button_view]; |
| 2497 |
|
| 2498 |
[[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(p_wd.no_min_btn && p_wd.no_max_btn)]; |
| 2499 |
[[p_wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(p_wd.no_min_btn && p_wd.no_max_btn)]; |
| 2500 |
} else { |
| 2501 |
[p_wd.window_object setTitleVisibility:NSWindowTitleVisible]; |
| 2502 |
[p_wd.window_object setTitlebarAppearsTransparent:NO]; |
| 2503 |
[[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(p_wd.no_min_btn && p_wd.no_max_btn)]; |
| 2504 |
[[p_wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(p_wd.no_min_btn && p_wd.no_max_btn)]; |
| 2505 |
[[p_wd.window_object standardWindowButton:NSWindowCloseButton] setHidden:NO]; |
| 2506 |
} |
| 2507 |
} |
| 2508 |
|
| 2509 |
void DisplayServerMacOS::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) { |
| 2510 |
_THREAD_SAFE_METHOD_ |
| 2511 |
|
| 2512 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2513 |
WindowData &wd = windows[p_window]; |
| 2514 |
|
| 2515 |
switch (p_flag) { |
| 2516 |
case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 2517 |
wd.no_min_btn = p_enabled; |
| 2518 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setEnabled:!p_enabled]; |
| 2519 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2520 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2521 |
} break; |
| 2522 |
case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 2523 |
wd.no_max_btn = p_enabled; |
| 2524 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:!p_enabled]; |
| 2525 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2526 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2527 |
} break; |
| 2528 |
case WINDOW_FLAG_RESIZE_DISABLED: { |
| 2529 |
wd.resize_disabled = p_enabled; |
| 2530 |
if (wd.fullscreen) { // Fullscreen window should be resizable, style will be applied on exiting fullscreen. |
| 2531 |
return; |
| 2532 |
} |
| 2533 |
if (p_enabled) { |
| 2534 |
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable]; |
| 2535 |
} else { |
| 2536 |
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable]; |
| 2537 |
} |
| 2538 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setEnabled:!wd.no_min_btn]; |
| 2539 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:!wd.no_max_btn]; |
| 2540 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2541 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:(wd.no_min_btn && wd.no_max_btn)]; |
| 2542 |
} break; |
| 2543 |
case WINDOW_FLAG_EXTEND_TO_TITLE: { |
| 2544 |
NSRect rect = [wd.window_object frame]; |
| 2545 |
wd.extend_to_title = p_enabled; |
| 2546 |
if (p_enabled) { |
| 2547 |
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskFullSizeContentView]; |
| 2548 |
|
| 2549 |
if (!wd.fullscreen) { |
| 2550 |
window_set_custom_window_buttons(wd, true); |
| 2551 |
} |
| 2552 |
} else { |
| 2553 |
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskFullSizeContentView]; |
| 2554 |
|
| 2555 |
if (!wd.fullscreen) { |
| 2556 |
window_set_custom_window_buttons(wd, false); |
| 2557 |
} |
| 2558 |
} |
| 2559 |
[wd.window_object setFrame:rect display:YES]; |
| 2560 |
send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE); |
| 2561 |
} break; |
| 2562 |
case WINDOW_FLAG_BORDERLESS: { |
| 2563 |
if (wd.fullscreen) { |
| 2564 |
return; |
| 2565 |
} |
| 2566 |
// OrderOut prevents a lose focus bug with the window. |
| 2567 |
bool was_visible = false; |
| 2568 |
if ([wd.window_object isVisible]) { |
| 2569 |
was_visible = true; |
| 2570 |
[wd.window_object orderOut:nil]; |
| 2571 |
} |
| 2572 |
wd.borderless = p_enabled; |
| 2573 |
bool was_maximized = (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])); |
| 2574 |
if (p_enabled) { |
| 2575 |
[wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; |
| 2576 |
[wd.window_object setHasShadow:NO]; |
| 2577 |
if (wd.layered_window) { |
| 2578 |
[wd.window_object setBackgroundColor:[NSColor clearColor]]; |
| 2579 |
} |
| 2580 |
} else { |
| 2581 |
[wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)]; |
| 2582 |
[[wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setEnabled:!wd.no_min_btn]; |
| 2583 |
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:!wd.no_max_btn]; |
| 2584 |
[wd.window_object setHasShadow:YES]; |
| 2585 |
if (wd.layered_window) { |
| 2586 |
// Note: transparent and not borderless - set alpha to non-zero value to ensure window shadow is rendered correctly. |
| 2587 |
[wd.window_object setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.004f]]; |
| 2588 |
} |
| 2589 |
// Force update of the window styles. |
| 2590 |
if ([wd.window_object isVisible]) { |
| 2591 |
NSRect frameRect = [wd.window_object frame]; |
| 2592 |
[wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO]; |
| 2593 |
[wd.window_object setFrame:frameRect display:NO]; |
| 2594 |
} |
| 2595 |
} |
| 2596 |
_update_window_style(wd, p_window); |
| 2597 |
if (was_visible || [wd.window_object isVisible]) { |
| 2598 |
if ([wd.window_object isMiniaturized]) { |
| 2599 |
return; |
| 2600 |
} else if (wd.no_focus) { |
| 2601 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 2602 |
WindowData &wd_parent = windows[wd.transient_parent]; |
| 2603 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[wd_parent.window_object windowNumber]]; |
| 2604 |
} else if (p_window != MAIN_WINDOW_ID) { |
| 2605 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[windows[MAIN_WINDOW_ID].window_object windowNumber]]; |
| 2606 |
} else { |
| 2607 |
[wd.window_object orderFront:nil]; |
| 2608 |
} |
| 2609 |
} else { |
| 2610 |
[wd.window_object makeKeyAndOrderFront:nil]; |
| 2611 |
} |
| 2612 |
} |
| 2613 |
if (was_maximized) { |
| 2614 |
[wd.window_object setFrame:[[wd.window_object screen] visibleFrame] display:NO]; |
| 2615 |
} |
| 2616 |
} break; |
| 2617 |
case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 2618 |
wd.on_top = p_enabled; |
| 2619 |
if (wd.fullscreen) { |
| 2620 |
return; |
| 2621 |
} |
| 2622 |
if (p_enabled) { |
| 2623 |
[(NSWindow *)wd.window_object setLevel:NSFloatingWindowLevel]; |
| 2624 |
} else { |
| 2625 |
[(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel]; |
| 2626 |
} |
| 2627 |
} break; |
| 2628 |
case WINDOW_FLAG_TRANSPARENT: { |
| 2629 |
if (wd.fullscreen) { |
| 2630 |
return; |
| 2631 |
} |
| 2632 |
wd.layered_window = p_enabled; |
| 2633 |
set_window_per_pixel_transparency_enabled(p_enabled, p_window); |
| 2634 |
// Force update of the window styles. |
| 2635 |
if ([wd.window_object isVisible]) { |
| 2636 |
NSRect frameRect = [wd.window_object frame]; |
| 2637 |
[wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO]; |
| 2638 |
[wd.window_object setFrame:frameRect display:NO]; |
| 2639 |
} |
| 2640 |
} break; |
| 2641 |
case WINDOW_FLAG_NO_FOCUS: { |
| 2642 |
wd.no_focus = p_enabled; |
| 2643 |
|
| 2644 |
NSWindow *w = wd.window_object; |
| 2645 |
w.excludedFromWindowsMenu = wd.is_popup || wd.no_focus; |
| 2646 |
} break; |
| 2647 |
case WINDOW_FLAG_EXCLUDE_FROM_CAPTURE: { |
| 2648 |
if (p_enabled) { |
| 2649 |
[wd.window_object setSharingType:NSWindowSharingNone]; |
| 2650 |
} else { |
| 2651 |
[wd.window_object setSharingType:NSWindowSharingReadWrite]; |
| 2652 |
} |
| 2653 |
wd.hide_from_capture = p_enabled; |
| 2654 |
} break; |
| 2655 |
case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 2656 |
wd.mpass = p_enabled; |
| 2657 |
} break; |
| 2658 |
case WINDOW_FLAG_POPUP: { |
| 2659 |
ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup."); |
| 2660 |
ERR_FAIL_COND_MSG([wd.window_object isVisible] && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened."); |
| 2661 |
wd.is_popup = p_enabled; |
| 2662 |
|
| 2663 |
NSWindow *w = wd.window_object; |
| 2664 |
w.excludedFromWindowsMenu = wd.is_popup || wd.no_focus; |
| 2665 |
} break; |
| 2666 |
default: { |
| 2667 |
} |
| 2668 |
} |
| 2669 |
} |
| 2670 |
|
| 2671 |
bool DisplayServerMacOS::window_get_flag(WindowFlags p_flag, WindowID p_window) const { |
| 2672 |
_THREAD_SAFE_METHOD_ |
| 2673 |
|
| 2674 |
ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2675 |
const WindowData &wd = windows[p_window]; |
| 2676 |
|
| 2677 |
switch (p_flag) { |
| 2678 |
case WINDOW_FLAG_MAXIMIZE_DISABLED: { |
| 2679 |
return wd.no_max_btn; |
| 2680 |
} break; |
| 2681 |
case WINDOW_FLAG_MINIMIZE_DISABLED: { |
| 2682 |
return wd.no_min_btn; |
| 2683 |
} break; |
| 2684 |
case WINDOW_FLAG_RESIZE_DISABLED: { |
| 2685 |
return wd.resize_disabled; |
| 2686 |
} break; |
| 2687 |
case WINDOW_FLAG_EXTEND_TO_TITLE: { |
| 2688 |
return [wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView; |
| 2689 |
} break; |
| 2690 |
case WINDOW_FLAG_BORDERLESS: { |
| 2691 |
return [wd.window_object styleMask] == NSWindowStyleMaskBorderless; |
| 2692 |
} break; |
| 2693 |
case WINDOW_FLAG_ALWAYS_ON_TOP: { |
| 2694 |
return wd.on_top; |
| 2695 |
} break; |
| 2696 |
case WINDOW_FLAG_TRANSPARENT: { |
| 2697 |
return wd.layered_window; |
| 2698 |
} break; |
| 2699 |
case WINDOW_FLAG_NO_FOCUS: { |
| 2700 |
return wd.no_focus; |
| 2701 |
} break; |
| 2702 |
case WINDOW_FLAG_EXCLUDE_FROM_CAPTURE: { |
| 2703 |
return wd.hide_from_capture; |
| 2704 |
} break; |
| 2705 |
case WINDOW_FLAG_MOUSE_PASSTHROUGH: { |
| 2706 |
return wd.mpass; |
| 2707 |
} break; |
| 2708 |
case WINDOW_FLAG_POPUP: { |
| 2709 |
return wd.is_popup; |
| 2710 |
} break; |
| 2711 |
default: { |
| 2712 |
} |
| 2713 |
} |
| 2714 |
|
| 2715 |
return false; |
| 2716 |
} |
| 2717 |
|
| 2718 |
void DisplayServerMacOS::window_request_attention(WindowID p_window) { |
| 2719 |
// It's app global, ignore window id. |
| 2720 |
[NSApp requestUserAttention:NSCriticalRequest]; |
| 2721 |
} |
| 2722 |
|
| 2723 |
void DisplayServerMacOS::window_move_to_foreground(WindowID p_window) { |
| 2724 |
_THREAD_SAFE_METHOD_ |
| 2725 |
|
| 2726 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2727 |
const WindowData &wd = windows[p_window]; |
| 2728 |
|
| 2729 |
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; |
| 2730 |
if (wd.no_focus || wd.is_popup) { |
| 2731 |
if (wd.transient_parent != INVALID_WINDOW_ID) { |
| 2732 |
WindowData &wd_parent = windows[wd.transient_parent]; |
| 2733 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[wd_parent.window_object windowNumber]]; |
| 2734 |
} else if (p_window != MAIN_WINDOW_ID) { |
| 2735 |
[wd.window_object orderWindow:NSWindowAbove relativeTo:[windows[MAIN_WINDOW_ID].window_object windowNumber]]; |
| 2736 |
} else { |
| 2737 |
[wd.window_object orderFront:nil]; |
| 2738 |
} |
| 2739 |
} else { |
| 2740 |
[wd.window_object makeKeyAndOrderFront:nil]; |
| 2741 |
} |
| 2742 |
} |
| 2743 |
|
| 2744 |
bool DisplayServerMacOS::window_is_focused(WindowID p_window) const { |
| 2745 |
_THREAD_SAFE_METHOD_ |
| 2746 |
|
| 2747 |
ERR_FAIL_COND_V(!windows.has(p_window), false); |
| 2748 |
const WindowData &wd = windows[p_window]; |
| 2749 |
|
| 2750 |
return wd.focused; |
| 2751 |
} |
| 2752 |
|
| 2753 |
DisplayServerMacOS::WindowID DisplayServerMacOS::get_focused_window() const { |
| 2754 |
return last_focused_window; |
| 2755 |
} |
| 2756 |
|
| 2757 |
bool DisplayServerMacOS::window_can_draw(WindowID p_window) const { |
| 2758 |
return windows[p_window].is_visible; |
| 2759 |
} |
| 2760 |
|
| 2761 |
bool DisplayServerMacOS::can_any_window_draw() const { |
| 2762 |
_THREAD_SAFE_METHOD_ |
| 2763 |
|
| 2764 |
for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 2765 |
if (E.value.is_visible) { |
| 2766 |
return true; |
| 2767 |
} |
| 2768 |
} |
| 2769 |
return false; |
| 2770 |
} |
| 2771 |
|
| 2772 |
void DisplayServerMacOS::window_set_ime_active(const bool p_active, WindowID p_window) { |
| 2773 |
_THREAD_SAFE_METHOD_ |
| 2774 |
|
| 2775 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2776 |
WindowData &wd = windows[p_window]; |
| 2777 |
|
| 2778 |
wd.im_active = p_active; |
| 2779 |
|
| 2780 |
if (!p_active) { |
| 2781 |
[wd.window_view cancelComposition]; |
| 2782 |
} |
| 2783 |
} |
| 2784 |
|
| 2785 |
void DisplayServerMacOS::window_set_ime_position(const Point2i &p_pos, WindowID p_window) { |
| 2786 |
_THREAD_SAFE_METHOD_ |
| 2787 |
|
| 2788 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2789 |
WindowData &wd = windows[p_window]; |
| 2790 |
|
| 2791 |
wd.im_position = p_pos; |
| 2792 |
} |
| 2793 |
|
| 2794 |
DisplayServer::WindowID DisplayServerMacOS::get_window_at_screen_position(const Point2i &p_position) const { |
| 2795 |
Point2i position = p_position; |
| 2796 |
position.y *= -1; |
| 2797 |
position += _get_screens_origin(); |
| 2798 |
position /= screen_get_max_scale(); |
| 2799 |
|
| 2800 |
NSInteger wnum = [NSWindow windowNumberAtPoint:NSMakePoint(position.x, position.y) belowWindowWithWindowNumber:0 /*topmost*/]; |
| 2801 |
for (const KeyValue<WindowID, WindowData> &E : windows) { |
| 2802 |
if ([E.value.window_object windowNumber] == wnum) { |
| 2803 |
return E.key; |
| 2804 |
} |
| 2805 |
} |
| 2806 |
return INVALID_WINDOW_ID; |
| 2807 |
} |
| 2808 |
|
| 2809 |
int64_t DisplayServerMacOS::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const { |
| 2810 |
ERR_FAIL_COND_V(!windows.has(p_window), 0); |
| 2811 |
switch (p_handle_type) { |
| 2812 |
case DISPLAY_HANDLE: { |
| 2813 |
return 0; // Not supported. |
| 2814 |
} |
| 2815 |
case WINDOW_HANDLE: { |
| 2816 |
return (int64_t)windows[p_window].window_object; |
| 2817 |
} |
| 2818 |
case WINDOW_VIEW: { |
| 2819 |
return (int64_t)windows[p_window].window_view; |
| 2820 |
} |
| 2821 |
#ifdef GLES3_ENABLED |
| 2822 |
case OPENGL_CONTEXT: { |
| 2823 |
if (gl_manager_legacy) { |
| 2824 |
return (int64_t)gl_manager_legacy->get_context(p_window); |
| 2825 |
} |
| 2826 |
if (gl_manager_angle) { |
| 2827 |
return (int64_t)gl_manager_angle->get_context(p_window); |
| 2828 |
} |
| 2829 |
return 0; |
| 2830 |
} |
| 2831 |
case EGL_DISPLAY: { |
| 2832 |
if (gl_manager_angle) { |
| 2833 |
return (int64_t)gl_manager_angle->get_display(p_window); |
| 2834 |
} |
| 2835 |
return 0; |
| 2836 |
} |
| 2837 |
case EGL_CONFIG: { |
| 2838 |
if (gl_manager_angle) { |
| 2839 |
return (int64_t)gl_manager_angle->get_config(p_window); |
| 2840 |
} |
| 2841 |
return 0; |
| 2842 |
} |
| 2843 |
#endif |
| 2844 |
default: { |
| 2845 |
return 0; |
| 2846 |
} |
| 2847 |
} |
| 2848 |
} |
| 2849 |
|
| 2850 |
void DisplayServerMacOS::window_attach_instance_id(ObjectID p_instance, WindowID p_window) { |
| 2851 |
_THREAD_SAFE_METHOD_ |
| 2852 |
|
| 2853 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 2854 |
windows[p_window].instance_id = p_instance; |
| 2855 |
} |
| 2856 |
|
| 2857 |
ObjectID DisplayServerMacOS::window_get_attached_instance_id(WindowID p_window) const { |
| 2858 |
_THREAD_SAFE_METHOD_ |
| 2859 |
|
| 2860 |
ERR_FAIL_COND_V(!windows.has(p_window), ObjectID()); |
| 2861 |
return windows[p_window].instance_id; |
| 2862 |
} |
| 2863 |
|
| 2864 |
void DisplayServerMacOS::gl_window_make_current(DisplayServer::WindowID p_window_id) { |
| 2865 |
#if defined(GLES3_ENABLED) |
| 2866 |
if (gl_manager_legacy) { |
| 2867 |
gl_manager_legacy->window_make_current(p_window_id); |
| 2868 |
} |
| 2869 |
if (gl_manager_angle) { |
| 2870 |
gl_manager_angle->window_make_current(p_window_id); |
| 2871 |
} |
| 2872 |
#endif |
| 2873 |
} |
| 2874 |
|
| 2875 |
void DisplayServerMacOS::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) { |
| 2876 |
_THREAD_SAFE_METHOD_ |
| 2877 |
#if defined(GLES3_ENABLED) |
| 2878 |
if (gl_manager_angle) { |
| 2879 |
gl_manager_angle->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 2880 |
} |
| 2881 |
if (gl_manager_legacy) { |
| 2882 |
gl_manager_legacy->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 2883 |
} |
| 2884 |
#endif |
| 2885 |
#if defined(RD_ENABLED) |
| 2886 |
if (rendering_context) { |
| 2887 |
rendering_context->window_set_vsync_mode(p_window, p_vsync_mode); |
| 2888 |
} |
| 2889 |
#endif |
| 2890 |
} |
| 2891 |
|
| 2892 |
DisplayServer::VSyncMode DisplayServerMacOS::window_get_vsync_mode(WindowID p_window) const { |
| 2893 |
_THREAD_SAFE_METHOD_ |
| 2894 |
#if defined(GLES3_ENABLED) |
| 2895 |
if (gl_manager_angle) { |
| 2896 |
return (gl_manager_angle->is_using_vsync() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED); |
| 2897 |
} |
| 2898 |
if (gl_manager_legacy) { |
| 2899 |
return (gl_manager_legacy->is_using_vsync() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED); |
| 2900 |
} |
| 2901 |
#endif |
| 2902 |
#if defined(RD_ENABLED) |
| 2903 |
if (rendering_context) { |
| 2904 |
return rendering_context->window_get_vsync_mode(p_window); |
| 2905 |
} |
| 2906 |
#endif |
| 2907 |
return DisplayServer::VSYNC_ENABLED; |
| 2908 |
} |
| 2909 |
|
| 2910 |
int DisplayServerMacOS::accessibility_should_increase_contrast() const { |
| 2911 |
return [(GodotApplicationDelegate *)[[NSApplication sharedApplication] delegate] getHighContrast]; |
| 2912 |
} |
| 2913 |
|
| 2914 |
int DisplayServerMacOS::accessibility_should_reduce_animation() const { |
| 2915 |
return [(GodotApplicationDelegate *)[[NSApplication sharedApplication] delegate] getReduceMotion]; |
| 2916 |
} |
| 2917 |
|
| 2918 |
int DisplayServerMacOS::accessibility_should_reduce_transparency() const { |
| 2919 |
return [(GodotApplicationDelegate *)[[NSApplication sharedApplication] delegate] getReduceTransparency]; |
| 2920 |
} |
| 2921 |
|
| 2922 |
int DisplayServerMacOS::accessibility_screen_reader_active() const { |
| 2923 |
return [(GodotApplicationDelegate *)[[NSApplication sharedApplication] delegate] getVoiceOver]; |
| 2924 |
} |
| 2925 |
|
| 2926 |
Point2i DisplayServerMacOS::ime_get_selection() const { |
| 2927 |
return im_selection; |
| 2928 |
} |
| 2929 |
|
| 2930 |
String DisplayServerMacOS::ime_get_text() const { |
| 2931 |
return im_text; |
| 2932 |
} |
| 2933 |
|
| 2934 |
void DisplayServerMacOS::cursor_update_shape() { |
| 2935 |
_THREAD_SAFE_METHOD_ |
| 2936 |
|
| 2937 |
if (cursors[cursor_shape] != nullptr) { |
| 2938 |
[cursors[cursor_shape] set]; |
| 2939 |
} else { |
| 2940 |
switch (cursor_shape) { |
| 2941 |
case CURSOR_ARROW: |
| 2942 |
[[NSCursor arrowCursor] set]; |
| 2943 |
break; |
| 2944 |
case CURSOR_IBEAM: |
| 2945 |
[[NSCursor IBeamCursor] set]; |
| 2946 |
break; |
| 2947 |
case CURSOR_POINTING_HAND: |
| 2948 |
[[NSCursor pointingHandCursor] set]; |
| 2949 |
break; |
| 2950 |
case CURSOR_CROSS: |
| 2951 |
[[NSCursor crosshairCursor] set]; |
| 2952 |
break; |
| 2953 |
case CURSOR_WAIT: |
| 2954 |
[[NSCursor arrowCursor] set]; |
| 2955 |
break; |
| 2956 |
case CURSOR_BUSY: |
| 2957 |
[[NSCursor arrowCursor] set]; |
| 2958 |
break; |
| 2959 |
case CURSOR_DRAG: |
| 2960 |
[[NSCursor closedHandCursor] set]; |
| 2961 |
break; |
| 2962 |
case CURSOR_CAN_DROP: |
| 2963 |
[[NSCursor openHandCursor] set]; |
| 2964 |
break; |
| 2965 |
case CURSOR_FORBIDDEN: |
| 2966 |
[[NSCursor operationNotAllowedCursor] set]; |
| 2967 |
break; |
| 2968 |
case CURSOR_VSIZE: |
| 2969 |
[_cursor_from_selector(@selector(_windowResizeNorthSouthCursor), @selector(resizeUpDownCursor)) set]; |
| 2970 |
break; |
| 2971 |
case CURSOR_HSIZE: |
| 2972 |
[_cursor_from_selector(@selector(_windowResizeEastWestCursor), @selector(resizeLeftRightCursor)) set]; |
| 2973 |
break; |
| 2974 |
case CURSOR_BDIAGSIZE: |
| 2975 |
[_cursor_from_selector(@selector(_windowResizeNorthEastSouthWestCursor)) set]; |
| 2976 |
break; |
| 2977 |
case CURSOR_FDIAGSIZE: |
| 2978 |
[_cursor_from_selector(@selector(_windowResizeNorthWestSouthEastCursor)) set]; |
| 2979 |
break; |
| 2980 |
case CURSOR_MOVE: |
| 2981 |
[[[GodotCoreCursor alloc] initWithType:GDCoreCursorWindowMove] set]; |
| 2982 |
break; |
| 2983 |
case CURSOR_VSPLIT: |
| 2984 |
[[NSCursor resizeUpDownCursor] set]; |
| 2985 |
break; |
| 2986 |
case CURSOR_HSPLIT: |
| 2987 |
[[NSCursor resizeLeftRightCursor] set]; |
| 2988 |
break; |
| 2989 |
case CURSOR_HELP: |
| 2990 |
[_cursor_from_selector(@selector(_helpCursor)) set]; |
| 2991 |
break; |
| 2992 |
default: { |
| 2993 |
} |
| 2994 |
} |
| 2995 |
} |
| 2996 |
} |
| 2997 |
|
| 2998 |
void DisplayServerMacOS::cursor_set_shape(CursorShape p_shape) { |
| 2999 |
_THREAD_SAFE_METHOD_ |
| 3000 |
|
| 3001 |
ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3002 |
|
| 3003 |
if (cursor_shape == p_shape) { |
| 3004 |
return; |
| 3005 |
} |
| 3006 |
|
| 3007 |
cursor_shape = p_shape; |
| 3008 |
|
| 3009 |
if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) { |
| 3010 |
return; |
| 3011 |
} |
| 3012 |
|
| 3013 |
cursor_update_shape(); |
| 3014 |
} |
| 3015 |
|
| 3016 |
DisplayServerMacOS::CursorShape DisplayServerMacOS::cursor_get_shape() const { |
| 3017 |
return cursor_shape; |
| 3018 |
} |
| 3019 |
|
| 3020 |
void DisplayServerMacOS::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { |
| 3021 |
_THREAD_SAFE_METHOD_ |
| 3022 |
|
| 3023 |
ERR_FAIL_INDEX(p_shape, CURSOR_MAX); |
| 3024 |
|
| 3025 |
if (p_cursor.is_valid()) { |
| 3026 |
HashMap<CursorShape, Vector<Variant>>::Iterator cursor_c = cursors_cache.find(p_shape); |
| 3027 |
|
| 3028 |
if (cursor_c) { |
| 3029 |
if (cursor_c->value[0] == p_cursor && cursor_c->value[1] == p_hotspot) { |
| 3030 |
cursor_set_shape(p_shape); |
| 3031 |
return; |
| 3032 |
} |
| 3033 |
cursors_cache.erase(p_shape); |
| 3034 |
} |
| 3035 |
|
| 3036 |
Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot); |
| 3037 |
ERR_FAIL_COND(image.is_null()); |
| 3038 |
Vector2i texture_size = image->get_size(); |
| 3039 |
|
| 3040 |
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] |
| 3041 |
initWithBitmapDataPlanes:nullptr |
| 3042 |
pixelsWide:int(texture_size.width) |
| 3043 |
pixelsHigh:int(texture_size.height) |
| 3044 |
bitsPerSample:8 |
| 3045 |
samplesPerPixel:4 |
| 3046 |
hasAlpha:YES |
| 3047 |
isPlanar:NO |
| 3048 |
colorSpaceName:NSDeviceRGBColorSpace |
| 3049 |
bytesPerRow:int(texture_size.width) * 4 |
| 3050 |
bitsPerPixel:32]; |
| 3051 |
|
| 3052 |
ERR_FAIL_NULL(imgrep); |
| 3053 |
uint8_t *pixels = [imgrep bitmapData]; |
| 3054 |
|
| 3055 |
int len = int(texture_size.width * texture_size.height); |
| 3056 |
|
| 3057 |
for (int i = 0; i < len; i++) { |
| 3058 |
int row_index = std::floor(i / texture_size.width); |
| 3059 |
int column_index = i % int(texture_size.width); |
| 3060 |
|
| 3061 |
uint32_t color = image->get_pixel(column_index, row_index).to_argb32(); |
| 3062 |
|
| 3063 |
uint8_t alpha = (color >> 24) & 0xFF; |
| 3064 |
pixels[i * 4 + 0] = ((color >> 16) & 0xFF) * alpha / 255; |
| 3065 |
pixels[i * 4 + 1] = ((color >> 8) & 0xFF) * alpha / 255; |
| 3066 |
pixels[i * 4 + 2] = ((color) & 0xFF) * alpha / 255; |
| 3067 |
pixels[i * 4 + 3] = alpha; |
| 3068 |
} |
| 3069 |
|
| 3070 |
NSImage *nsimage = [[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)]; |
| 3071 |
[nsimage addRepresentation:imgrep]; |
| 3072 |
|
| 3073 |
NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)]; |
| 3074 |
|
| 3075 |
cursors[p_shape] = cursor; |
| 3076 |
|
| 3077 |
Vector<Variant> params; |
| 3078 |
params.push_back(p_cursor); |
| 3079 |
params.push_back(p_hotspot); |
| 3080 |
cursors_cache.insert(p_shape, params); |
| 3081 |
|
| 3082 |
if (p_shape == cursor_shape) { |
| 3083 |
if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) { |
| 3084 |
[cursor set]; |
| 3085 |
} |
| 3086 |
} |
| 3087 |
} else { |
| 3088 |
// Reset to default system cursor. |
| 3089 |
if (cursors[p_shape] != nullptr) { |
| 3090 |
cursors[p_shape] = nullptr; |
| 3091 |
} |
| 3092 |
|
| 3093 |
cursors_cache.erase(p_shape); |
| 3094 |
|
| 3095 |
cursor_update_shape(); |
| 3096 |
} |
| 3097 |
} |
| 3098 |
|
| 3099 |
bool DisplayServerMacOS::get_swap_cancel_ok() { |
| 3100 |
return false; |
| 3101 |
} |
| 3102 |
|
| 3103 |
void DisplayServerMacOS::enable_for_stealing_focus(OS::ProcessID pid) { |
| 3104 |
} |
| 3105 |
|
| 3106 |
#define GET_OR_FAIL_V(m_val, m_map, m_key, m_retval) \ |
| 3107 |
m_val = m_map.getptr(m_key); \ |
| 3108 |
if (m_val == nullptr) { \ |
| 3109 |
ERR_FAIL_V(m_retval); \ |
| 3110 |
} |
| 3111 |
|
| 3112 |
uint32_t DisplayServerMacOS::window_get_display_id(WindowID p_window) const { |
| 3113 |
const WindowData *wd; |
| 3114 |
GET_OR_FAIL_V(wd, windows, p_window, -1); |
| 3115 |
return wd->display_id; |
| 3116 |
} |
| 3117 |
|
| 3118 |
void DisplayServerMacOS::_window_update_display_id(WindowData *p_wd) { |
| 3119 |
NSScreen *screen = [p_wd->window_object screen]; |
| 3120 |
CGDirectDisplayID display_id = [[screen deviceDescription][@"NSScreenNumber"] unsignedIntValue]; |
| 3121 |
if (p_wd->display_id == display_id) { |
| 3122 |
return; |
| 3123 |
} |
| 3124 |
|
| 3125 |
p_wd->display_id = display_id; |
| 3126 |
|
| 3127 |
#ifdef TOOLS_ENABLED |
| 3128 |
// Notify any embedded processes of the new display ID, so that they can potentially update their vsync. |
| 3129 |
for (KeyValue<OS::ProcessID, EmbeddedProcessData> &E : embedded_processes) { |
| 3130 |
if (E.value.wd == p_wd) { |
| 3131 |
E.value.process->display_state_changed(); |
| 3132 |
} |
| 3133 |
} |
| 3134 |
#endif |
| 3135 |
} |
| 3136 |
|
| 3137 |
#ifdef TOOLS_ENABLED |
| 3138 |
|
| 3139 |
Error DisplayServerMacOS::embed_process_update(WindowID p_window, EmbeddedProcessMacOS *p_process) { |
| 3140 |
_THREAD_SAFE_METHOD_ |
| 3141 |
|
| 3142 |
WindowData *wd; |
| 3143 |
GET_OR_FAIL_V(wd, windows, p_window, FAILED); |
| 3144 |
|
| 3145 |
OS::ProcessID p_pid = p_process->get_embedded_pid(); |
| 3146 |
|
| 3147 |
[CATransaction begin]; |
| 3148 |
[CATransaction setDisableActions:YES]; |
| 3149 |
|
| 3150 |
EmbeddedProcessData *ed = embedded_processes.getptr(p_pid); |
| 3151 |
CGFloat scale = screen_get_max_scale(); |
| 3152 |
if (ed == nil) { |
| 3153 |
ed = &embedded_processes.insert(p_pid, EmbeddedProcessData())->value; |
| 3154 |
|
| 3155 |
ed->process = p_process; |
| 3156 |
ed->wd = wd; |
| 3157 |
|
| 3158 |
CALayerHost *host = [CALayerHost new]; |
| 3159 |
uint32_t p_context_id = p_process->get_context_id(); |
| 3160 |
host.contextId = static_cast<CAContextID>(p_context_id); |
| 3161 |
host.contentsScale = scale; |
| 3162 |
host.contentsGravity = kCAGravityCenter; |
| 3163 |
ed->layer_host = host; |
| 3164 |
[wd->window_view.layer addSublayer:host]; |
| 3165 |
} |
| 3166 |
|
| 3167 |
Rect2i p_rect = p_process->get_screen_embedded_window_rect(); |
| 3168 |
CGRect rect = CGRectMake(p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y); |
| 3169 |
rect = CGRectApplyAffineTransform(rect, CGAffineTransformInvert(CGAffineTransformMakeScale(scale, scale))); |
| 3170 |
|
| 3171 |
CGFloat height = wd->window_view.frame.size.height; |
| 3172 |
CGFloat x = rect.origin.x; |
| 3173 |
CGFloat y = (height - rect.origin.y); |
| 3174 |
ed->layer_host.position = CGPointMake(x, y); |
| 3175 |
ed->layer_host.hidden = !p_process->is_visible_in_tree(); |
| 3176 |
|
| 3177 |
[CATransaction commit]; |
| 3178 |
|
| 3179 |
return OK; |
| 3180 |
} |
| 3181 |
|
| 3182 |
Error DisplayServerMacOS::request_close_embedded_process(OS::ProcessID p_pid) { |
| 3183 |
return OK; |
| 3184 |
} |
| 3185 |
|
| 3186 |
Error DisplayServerMacOS::remove_embedded_process(OS::ProcessID p_pid) { |
| 3187 |
_THREAD_SAFE_METHOD_ |
| 3188 |
|
| 3189 |
EmbeddedProcessData *ed; |
| 3190 |
GET_OR_FAIL_V(ed, embedded_processes, p_pid, ERR_DOES_NOT_EXIST); |
| 3191 |
[ed->layer_host removeFromSuperlayer]; |
| 3192 |
embedded_processes.erase(p_pid); |
| 3193 |
|
| 3194 |
return OK; |
| 3195 |
} |
| 3196 |
|
| 3197 |
#endif |
| 3198 |
|
| 3199 |
void DisplayServerMacOS::process_events() { |
| 3200 |
_process_events(true); |
| 3201 |
} |
| 3202 |
|
| 3203 |
void DisplayServerMacOS::_process_events(bool p_pump) { |
| 3204 |
ERR_FAIL_COND(!Thread::is_main_thread()); |
| 3205 |
|
| 3206 |
if (p_pump) { |
| 3207 |
while (true) { |
| 3208 |
NSEvent *event = [NSApp |
| 3209 |
nextEventMatchingMask:NSEventMaskAny |
| 3210 |
untilDate:[NSDate distantPast] |
| 3211 |
inMode:NSDefaultRunLoopMode |
| 3212 |
dequeue:YES]; |
| 3213 |
|
| 3214 |
if (event == nil) { |
| 3215 |
break; |
| 3216 |
} |
| 3217 |
|
| 3218 |
[NSApp sendEvent:event]; |
| 3219 |
} |
| 3220 |
} |
| 3221 |
|
| 3222 |
// Process "menu_callback"s. |
| 3223 |
while (List<MenuCall>::Element *call_p = deferred_menu_calls.front()) { |
| 3224 |
MenuCall call = call_p->get(); |
| 3225 |
deferred_menu_calls.pop_front(); // Remove before call to avoid infinite loop in case callback is using `process_events` (e.g. EditorProgress). |
| 3226 |
|
| 3227 |
Variant ret; |
| 3228 |
Callable::CallError ce; |
| 3229 |
const Variant *args[1] = { &call.tag }; |
| 3230 |
|
| 3231 |
call.callback.callp(args, 1, ret, ce); |
| 3232 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 3233 |
ERR_PRINT(vformat("Failed to execute menu callback: %s.", Variant::get_callable_error_text(call.callback, args, 1, ce))); |
| 3234 |
} |
| 3235 |
} |
| 3236 |
|
| 3237 |
if (!drop_events) { |
| 3238 |
_process_key_events(); |
| 3239 |
Input::get_singleton()->flush_buffered_events(); |
| 3240 |
} |
| 3241 |
|
| 3242 |
_THREAD_SAFE_LOCK_ |
| 3243 |
|
| 3244 |
for (KeyValue<WindowID, WindowData> &E : windows) { |
| 3245 |
WindowData &wd = E.value; |
| 3246 |
if (wd.mpass) { |
| 3247 |
if (![wd.window_object ignoresMouseEvents]) { |
| 3248 |
[wd.window_object setIgnoresMouseEvents:YES]; |
| 3249 |
} |
| 3250 |
} else if (wd.mpath.size() > 0) { |
| 3251 |
update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); |
| 3252 |
if (Geometry2D::is_point_in_polygon(wd.mouse_pos, wd.mpath)) { |
| 3253 |
if ([wd.window_object ignoresMouseEvents]) { |
| 3254 |
[wd.window_object setIgnoresMouseEvents:NO]; |
| 3255 |
} |
| 3256 |
} else { |
| 3257 |
if (![wd.window_object ignoresMouseEvents]) { |
| 3258 |
[wd.window_object setIgnoresMouseEvents:YES]; |
| 3259 |
} |
| 3260 |
} |
| 3261 |
} else { |
| 3262 |
if ([wd.window_object ignoresMouseEvents]) { |
| 3263 |
[wd.window_object setIgnoresMouseEvents:NO]; |
| 3264 |
} |
| 3265 |
} |
| 3266 |
} |
| 3267 |
|
| 3268 |
_THREAD_SAFE_UNLOCK_ |
| 3269 |
} |
| 3270 |
|
| 3271 |
void DisplayServerMacOS::force_process_and_drop_events() { |
| 3272 |
ERR_FAIL_COND(!Thread::is_main_thread()); |
| 3273 |
|
| 3274 |
drop_events = true; |
| 3275 |
process_events(); |
| 3276 |
drop_events = false; |
| 3277 |
} |
| 3278 |
|
| 3279 |
void DisplayServerMacOS::release_rendering_thread() { |
| 3280 |
#if defined(GLES3_ENABLED) |
| 3281 |
if (gl_manager_angle) { |
| 3282 |
gl_manager_angle->release_current(); |
| 3283 |
} |
| 3284 |
if (gl_manager_legacy) { |
| 3285 |
gl_manager_legacy->release_current(); |
| 3286 |
} |
| 3287 |
#endif |
| 3288 |
} |
| 3289 |
|
| 3290 |
void DisplayServerMacOS::swap_buffers() { |
| 3291 |
#if defined(GLES3_ENABLED) |
| 3292 |
if (gl_manager_angle) { |
| 3293 |
gl_manager_angle->swap_buffers(); |
| 3294 |
} |
| 3295 |
if (gl_manager_legacy) { |
| 3296 |
gl_manager_legacy->swap_buffers(); |
| 3297 |
} |
| 3298 |
#endif |
| 3299 |
} |
| 3300 |
|
| 3301 |
void DisplayServerMacOS::set_native_icon(const String &p_filename) { |
| 3302 |
_THREAD_SAFE_METHOD_ |
| 3303 |
|
| 3304 |
Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::READ); |
| 3305 |
ERR_FAIL_COND(f.is_null()); |
| 3306 |
|
| 3307 |
Vector<uint8_t> data; |
| 3308 |
uint64_t len = f->get_length(); |
| 3309 |
ERR_FAIL_COND_MSG(len < 8, "Error reading icon data."); // "icns" + 32-bit length |
| 3310 |
|
| 3311 |
data.resize(len); |
| 3312 |
f->get_buffer((uint8_t *)&data.write[0], len); |
| 3313 |
|
| 3314 |
@try { |
| 3315 |
NSData *icon_data = [[NSData alloc] initWithBytes:&data.write[0] length:len]; |
| 3316 |
ERR_FAIL_NULL_MSG(icon_data, "Error reading icon data."); |
| 3317 |
|
| 3318 |
NSImage *icon = [[NSImage alloc] initWithData:icon_data]; |
| 3319 |
ERR_FAIL_NULL_MSG(icon, "Error loading icon."); |
| 3320 |
|
| 3321 |
[NSApp setApplicationIconImage:icon]; |
| 3322 |
} @catch (NSException *exception) { |
| 3323 |
ERR_FAIL_MSG("NSException: " + String::utf8([exception reason].UTF8String)); |
| 3324 |
} |
| 3325 |
} |
| 3326 |
|
| 3327 |
void DisplayServerMacOS::set_icon(const Ref<Image> &p_icon) { |
| 3328 |
_THREAD_SAFE_METHOD_ |
| 3329 |
|
| 3330 |
if (p_icon.is_valid()) { |
| 3331 |
ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0); |
| 3332 |
|
| 3333 |
Ref<Image> img = p_icon->duplicate(); |
| 3334 |
img->convert(Image::FORMAT_RGBA8); |
| 3335 |
|
| 3336 |
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc] |
| 3337 |
initWithBitmapDataPlanes:nullptr |
| 3338 |
pixelsWide:img->get_width() |
| 3339 |
pixelsHigh:img->get_height() |
| 3340 |
bitsPerSample:8 |
| 3341 |
samplesPerPixel:4 |
| 3342 |
hasAlpha:YES |
| 3343 |
isPlanar:NO |
| 3344 |
colorSpaceName:NSDeviceRGBColorSpace |
| 3345 |
bytesPerRow:img->get_width() * 4 |
| 3346 |
bitsPerPixel:32]; |
| 3347 |
ERR_FAIL_NULL(imgrep); |
| 3348 |
uint8_t *pixels = [imgrep bitmapData]; |
| 3349 |
|
| 3350 |
int len = img->get_width() * img->get_height(); |
| 3351 |
const uint8_t *r = img->get_data().ptr(); |
| 3352 |
|
| 3353 |
/* Premultiply the alpha channel */ |
| 3354 |
for (int i = 0; i < len; i++) { |
| 3355 |
uint8_t alpha = r[i * 4 + 3]; |
| 3356 |
pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255); |
| 3357 |
pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255); |
| 3358 |
pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255); |
| 3359 |
pixels[i * 4 + 3] = alpha; |
| 3360 |
} |
| 3361 |
|
| 3362 |
NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())]; |
| 3363 |
ERR_FAIL_NULL(nsimg); |
| 3364 |
|
| 3365 |
[nsimg addRepresentation:imgrep]; |
| 3366 |
[NSApp setApplicationIconImage:nsimg]; |
| 3367 |
} else { |
| 3368 |
[NSApp setApplicationIconImage:nil]; |
| 3369 |
} |
| 3370 |
} |
| 3371 |
|
| 3372 |
DisplayServer::IndicatorID DisplayServerMacOS::create_status_indicator(const Ref<Texture2D> &p_icon, const String &p_tooltip, const Callable &p_callback) { |
| 3373 |
NSImage *nsimg = nullptr; |
| 3374 |
if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { |
| 3375 |
Ref<Image> img = p_icon->get_image(); |
| 3376 |
img = img->duplicate(); |
| 3377 |
if (img->is_compressed()) { |
| 3378 |
img->decompress(); |
| 3379 |
} |
| 3380 |
nsimg = _convert_to_nsimg(img); |
| 3381 |
} |
| 3382 |
|
| 3383 |
IndicatorData idat; |
| 3384 |
|
| 3385 |
NSStatusItem *item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; |
| 3386 |
idat.item = item; |
| 3387 |
idat.delegate = [[GodotStatusItemDelegate alloc] init]; |
| 3388 |
[idat.delegate setCallback:p_callback]; |
| 3389 |
|
| 3390 |
item.button.image = nsimg; |
| 3391 |
item.button.imagePosition = NSImageOnly; |
| 3392 |
item.button.imageScaling = NSImageScaleProportionallyUpOrDown; |
| 3393 |
item.button.target = idat.delegate; |
| 3394 |
item.button.action = @selector(click:); |
| 3395 |
[item.button sendActionOn:(NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown)]; |
| 3396 |
item.button.toolTip = [NSString stringWithUTF8String:p_tooltip.utf8().get_data()]; |
| 3397 |
|
| 3398 |
IndicatorID iid = indicator_id_counter++; |
| 3399 |
indicators[iid] = idat; |
| 3400 |
|
| 3401 |
return iid; |
| 3402 |
} |
| 3403 |
|
| 3404 |
void DisplayServerMacOS::status_indicator_set_icon(IndicatorID p_id, const Ref<Texture2D> &p_icon) { |
| 3405 |
ERR_FAIL_COND(!indicators.has(p_id)); |
| 3406 |
|
| 3407 |
NSImage *nsimg = nullptr; |
| 3408 |
if (p_icon.is_valid() && p_icon->get_width() > 0 && p_icon->get_height() > 0 && p_icon->get_image().is_valid()) { |
| 3409 |
Ref<Image> img = p_icon->get_image(); |
| 3410 |
img = img->duplicate(); |
| 3411 |
if (img->is_compressed()) { |
| 3412 |
img->decompress(); |
| 3413 |
} |
| 3414 |
nsimg = _convert_to_nsimg(img); |
| 3415 |
} |
| 3416 |
|
| 3417 |
NSStatusItem *item = indicators[p_id].item; |
| 3418 |
item.button.image = nsimg; |
| 3419 |
} |
| 3420 |
|
| 3421 |
void DisplayServerMacOS::status_indicator_set_tooltip(IndicatorID p_id, const String &p_tooltip) { |
| 3422 |
ERR_FAIL_COND(!indicators.has(p_id)); |
| 3423 |
|
| 3424 |
NSStatusItem *item = indicators[p_id].item; |
| 3425 |
item.button.toolTip = [NSString stringWithUTF8String:p_tooltip.utf8().get_data()]; |
| 3426 |
} |
| 3427 |
|
| 3428 |
void DisplayServerMacOS::status_indicator_set_menu(IndicatorID p_id, const RID &p_menu_rid) { |
| 3429 |
ERR_FAIL_COND(!indicators.has(p_id)); |
| 3430 |
|
| 3431 |
NSStatusItem *item = indicators[p_id].item; |
| 3432 |
if (p_menu_rid.is_valid() && native_menu->has_menu(p_menu_rid)) { |
| 3433 |
NSMenu *menu = native_menu->get_native_menu_handle(p_menu_rid); |
| 3434 |
item.menu = menu; |
| 3435 |
} else { |
| 3436 |
item.menu = nullptr; |
| 3437 |
} |
| 3438 |
} |
| 3439 |
|
| 3440 |
void DisplayServerMacOS::status_indicator_set_callback(IndicatorID p_id, const Callable &p_callback) { |
| 3441 |
ERR_FAIL_COND(!indicators.has(p_id)); |
| 3442 |
|
| 3443 |
[indicators[p_id].delegate setCallback:p_callback]; |
| 3444 |
} |
| 3445 |
|
| 3446 |
Rect2 DisplayServerMacOS::status_indicator_get_rect(IndicatorID p_id) const { |
| 3447 |
ERR_FAIL_COND_V(!indicators.has(p_id), Rect2()); |
| 3448 |
|
| 3449 |
NSStatusItem *item = indicators[p_id].item; |
| 3450 |
NSView *v = item.button; |
| 3451 |
const NSRect contentRect = [v frame]; |
| 3452 |
const NSRect nsrect = [v.window convertRectToScreen:contentRect]; |
| 3453 |
Rect2 rect; |
| 3454 |
|
| 3455 |
// Return the position of the top-left corner, for macOS the y starts at the bottom. |
| 3456 |
const float scale = screen_get_max_scale(); |
| 3457 |
rect.size.x = nsrect.size.width; |
| 3458 |
rect.size.y = nsrect.size.height; |
| 3459 |
rect.size *= scale; |
| 3460 |
rect.position.x = nsrect.origin.x; |
| 3461 |
rect.position.y = (nsrect.origin.y + nsrect.size.height); |
| 3462 |
rect.position *= scale; |
| 3463 |
rect.position -= _get_screens_origin(); |
| 3464 |
// macOS native y-coordinate relative to _get_screens_origin() is negative, |
| 3465 |
// Godot expects a positive value. |
| 3466 |
rect.position.y *= -1; |
| 3467 |
return rect; |
| 3468 |
} |
| 3469 |
|
| 3470 |
void DisplayServerMacOS::delete_status_indicator(IndicatorID p_id) { |
| 3471 |
ERR_FAIL_COND(!indicators.has(p_id)); |
| 3472 |
|
| 3473 |
[[NSStatusBar systemStatusBar] removeStatusItem:indicators[p_id].item]; |
| 3474 |
indicators.erase(p_id); |
| 3475 |
} |
| 3476 |
|
| 3477 |
bool DisplayServerMacOS::is_window_transparency_available() const { |
| 3478 |
#if defined(RD_ENABLED) |
| 3479 |
if (rendering_device && !rendering_device->is_composite_alpha_supported()) { |
| 3480 |
return false; |
| 3481 |
} |
| 3482 |
#endif |
| 3483 |
return OS::get_singleton()->is_layered_allowed(); |
| 3484 |
} |
| 3485 |
|
| 3486 |
DisplayServer *DisplayServerMacOS::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) { |
| 3487 |
DisplayServer *ds = memnew(DisplayServerMacOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error)); |
| 3488 |
if (r_error != OK) { |
| 3489 |
if (p_rendering_driver == "vulkan") { |
| 3490 |
String executable_command; |
| 3491 |
if (OS::get_singleton()->get_bundle_resource_dir() == OS::get_singleton()->get_executable_path().get_base_dir()) { |
| 3492 |
executable_command = vformat("\"%s\" --rendering-driver opengl3", OS::get_singleton()->get_executable_path()); |
| 3493 |
} else { |
| 3494 |
executable_command = vformat("open \"%s\" --args --rendering-driver opengl3", OS::get_singleton()->get_bundle_resource_dir().path_join("../..").simplify_path()); |
| 3495 |
} |
| 3496 |
OS::get_singleton()->alert( |
| 3497 |
vformat("Your video card drivers seem not to support the required Vulkan version.\n\n" |
| 3498 |
"If possible, consider updating your macOS version or using the OpenGL 3 driver.\n\n" |
| 3499 |
"You can enable the OpenGL 3 driver by starting the engine from the\n" |
| 3500 |
"command line with the command:\n\n %s", |
| 3501 |
executable_command), |
| 3502 |
"Unable to initialize Vulkan video driver"); |
| 3503 |
} else { |
| 3504 |
OS::get_singleton()->alert( |
| 3505 |
"Your video card drivers seem not to support the required OpenGL 3.3 version.\n\n" |
| 3506 |
"If possible, consider updating your macOS version.", |
| 3507 |
"Unable to initialize OpenGL video driver"); |
| 3508 |
} |
| 3509 |
} |
| 3510 |
return ds; |
| 3511 |
} |
| 3512 |
|
| 3513 |
Vector<String> DisplayServerMacOS::get_rendering_drivers_func() { |
| 3514 |
Vector<String> drivers; |
| 3515 |
|
| 3516 |
#if defined(VULKAN_ENABLED) |
| 3517 |
drivers.push_back("vulkan"); |
| 3518 |
#endif |
| 3519 |
#if defined(METAL_ENABLED) |
| 3520 |
drivers.push_back("metal"); |
| 3521 |
#endif |
| 3522 |
#if defined(GLES3_ENABLED) |
| 3523 |
drivers.push_back("opengl3"); |
| 3524 |
drivers.push_back("opengl3_angle"); |
| 3525 |
#endif |
| 3526 |
drivers.push_back("dummy"); |
| 3527 |
|
| 3528 |
return drivers; |
| 3529 |
} |
| 3530 |
|
| 3531 |
void DisplayServerMacOS::register_macos_driver() { |
| 3532 |
register_create_function("macos", create_func, get_rendering_drivers_func); |
| 3533 |
} |
| 3534 |
|
| 3535 |
DisplayServer::WindowID DisplayServerMacOS::window_get_active_popup() const { |
| 3536 |
const List<WindowID>::Element *E = popup_list.back(); |
| 3537 |
if (E) { |
| 3538 |
return E->get(); |
| 3539 |
} else { |
| 3540 |
return INVALID_WINDOW_ID; |
| 3541 |
} |
| 3542 |
} |
| 3543 |
|
| 3544 |
void DisplayServerMacOS::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) { |
| 3545 |
_THREAD_SAFE_METHOD_ |
| 3546 |
|
| 3547 |
ERR_FAIL_COND(!windows.has(p_window)); |
| 3548 |
WindowData &wd = windows[p_window]; |
| 3549 |
wd.parent_safe_rect = p_rect; |
| 3550 |
} |
| 3551 |
|
| 3552 |
Rect2i DisplayServerMacOS::window_get_popup_safe_rect(WindowID p_window) const { |
| 3553 |
_THREAD_SAFE_METHOD_ |
| 3554 |
|
| 3555 |
ERR_FAIL_COND_V(!windows.has(p_window), Rect2i()); |
| 3556 |
const WindowData &wd = windows[p_window]; |
| 3557 |
return wd.parent_safe_rect; |
| 3558 |
} |
| 3559 |
|
| 3560 |
void DisplayServerMacOS::popup_open(WindowID p_window) { |
| 3561 |
_THREAD_SAFE_METHOD_ |
| 3562 |
|
| 3563 |
bool has_popup_ancestor = false; |
| 3564 |
WindowID transient_root = p_window; |
| 3565 |
while (true) { |
| 3566 |
WindowID parent = windows[transient_root].transient_parent; |
| 3567 |
if (parent == INVALID_WINDOW_ID) { |
| 3568 |
break; |
| 3569 |
} else { |
| 3570 |
transient_root = parent; |
| 3571 |
if (windows[parent].is_popup) { |
| 3572 |
has_popup_ancestor = true; |
| 3573 |
break; |
| 3574 |
} |
| 3575 |
} |
| 3576 |
} |
| 3577 |
|
| 3578 |
// Detect tooltips and other similar popups that shouldn't block input to their parent. |
| 3579 |
bool ignores_input = window_get_flag(WINDOW_FLAG_NO_FOCUS, p_window) && window_get_flag(WINDOW_FLAG_MOUSE_PASSTHROUGH, p_window); |
| 3580 |
|
| 3581 |
WindowData &wd = windows[p_window]; |
| 3582 |
if (wd.is_popup || (has_popup_ancestor && !ignores_input)) { |
| 3583 |
bool was_empty = popup_list.is_empty(); |
| 3584 |
// Find current popup parent, or root popup if new window is not transient. |
| 3585 |
List<WindowID>::Element *C = nullptr; |
| 3586 |
List<WindowID>::Element *E = popup_list.back(); |
| 3587 |
while (E) { |
| 3588 |
if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) { |
| 3589 |
C = E; |
| 3590 |
E = E->prev(); |
| 3591 |
} else { |
| 3592 |
break; |
| 3593 |
} |
| 3594 |
} |
| 3595 |
if (C) { |
| 3596 |
send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); |
| 3597 |
} |
| 3598 |
|
| 3599 |
if (was_empty && popup_list.is_empty()) { |
| 3600 |
// Inform OS that popup was opened, to close other native popups. |
| 3601 |
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.beginMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"]; |
| 3602 |
} |
| 3603 |
time_since_popup = OS::get_singleton()->get_ticks_msec(); |
| 3604 |
popup_list.push_back(p_window); |
| 3605 |
} |
| 3606 |
} |
| 3607 |
|
| 3608 |
void DisplayServerMacOS::popup_close(WindowID p_window) { |
| 3609 |
_THREAD_SAFE_METHOD_ |
| 3610 |
|
| 3611 |
bool was_empty = popup_list.is_empty(); |
| 3612 |
List<WindowID>::Element *E = popup_list.find(p_window); |
| 3613 |
while (E) { |
| 3614 |
List<WindowID>::Element *F = E->next(); |
| 3615 |
WindowID win_id = E->get(); |
| 3616 |
popup_list.erase(E); |
| 3617 |
|
| 3618 |
if (win_id != p_window) { |
| 3619 |
// Only request close on related windows, not this window. We are already processing it. |
| 3620 |
send_window_event(windows[win_id], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); |
| 3621 |
} |
| 3622 |
E = F; |
| 3623 |
} |
| 3624 |
if (!was_empty && popup_list.is_empty()) { |
| 3625 |
// Inform OS that all popups are closed. |
| 3626 |
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"]; |
| 3627 |
} |
| 3628 |
} |
| 3629 |
|
| 3630 |
bool DisplayServerMacOS::mouse_process_popups(bool p_close) { |
| 3631 |
_THREAD_SAFE_METHOD_ |
| 3632 |
|
| 3633 |
bool was_empty = popup_list.is_empty(); |
| 3634 |
bool closed = false; |
| 3635 |
if (p_close) { |
| 3636 |
// Close all popups. |
| 3637 |
List<WindowID>::Element *E = popup_list.front(); |
| 3638 |
if (E) { |
| 3639 |
send_window_event(windows[E->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); |
| 3640 |
closed = true; |
| 3641 |
} |
| 3642 |
if (!was_empty) { |
| 3643 |
// Inform OS that all popups are closed. |
| 3644 |
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"]; |
| 3645 |
} |
| 3646 |
} else { |
| 3647 |
uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup; |
| 3648 |
if (delta < 250) { |
| 3649 |
return false; |
| 3650 |
} |
| 3651 |
|
| 3652 |
Point2i pos = mouse_get_position(); |
| 3653 |
List<WindowID>::Element *C = nullptr; |
| 3654 |
List<WindowID>::Element *E = popup_list.back(); |
| 3655 |
// Find top popup to close. |
| 3656 |
while (E) { |
| 3657 |
// Popup window area. |
| 3658 |
Rect2i win_rect = Rect2i(window_get_position_with_decorations(E->get()), window_get_size_with_decorations(E->get())); |
| 3659 |
// Area of the parent window, which responsible for opening sub-menu. |
| 3660 |
Rect2i safe_rect = window_get_popup_safe_rect(E->get()); |
| 3661 |
if (win_rect.has_point(pos)) { |
| 3662 |
break; |
| 3663 |
} else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) { |
| 3664 |
break; |
| 3665 |
} else { |
| 3666 |
C = E; |
| 3667 |
E = E->prev(); |
| 3668 |
} |
| 3669 |
} |
| 3670 |
if (C) { |
| 3671 |
send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); |
| 3672 |
closed = true; |
| 3673 |
} |
| 3674 |
if (!was_empty && popup_list.is_empty()) { |
| 3675 |
// Inform OS that all popups are closed. |
| 3676 |
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"]; |
| 3677 |
} |
| 3678 |
} |
| 3679 |
return closed; |
| 3680 |
} |
| 3681 |
|
| 3682 |
DisplayServerMacOS::DisplayServerMacOS(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) { |
| 3683 |
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events); |
| 3684 |
|
| 3685 |
r_error = OK; |
| 3686 |
|
| 3687 |
memset(cursors, 0, sizeof(cursors)); |
| 3688 |
|
| 3689 |
event_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); |
| 3690 |
ERR_FAIL_COND(!event_source); |
| 3691 |
|
| 3692 |
CGEventSourceSetLocalEventsSuppressionInterval(event_source, 0.0); |
| 3693 |
|
| 3694 |
int screen_count = get_screen_count(); |
| 3695 |
for (int i = 0; i < screen_count; i++) { |
| 3696 |
display_max_scale = std::fmax(display_max_scale, screen_get_scale(i)); |
| 3697 |
} |
| 3698 |
|
| 3699 |
// Register to be notified on displays arrangement changes. |
| 3700 |
CGDisplayRegisterReconfigurationCallback(_displays_arrangement_changed, nullptr); |
| 3701 |
|
| 3702 |
native_menu = memnew(NativeMenuMacOS); |
| 3703 |
|
| 3704 |
#ifdef ACCESSKIT_ENABLED |
| 3705 |
if (accessibility_get_mode() != DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) { |
| 3706 |
accessibility_driver = memnew(AccessibilityDriverAccessKit); |
| 3707 |
if (accessibility_driver->init() != OK) { |
| 3708 |
memdelete(accessibility_driver); |
| 3709 |
accessibility_driver = nullptr; |
| 3710 |
} |
| 3711 |
} |
| 3712 |
#endif |
| 3713 |
|
| 3714 |
NSMenuItem *menu_item; |
| 3715 |
NSString *title; |
| 3716 |
|
| 3717 |
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; |
| 3718 |
if (nsappname == nil) { |
| 3719 |
nsappname = [[NSProcessInfo processInfo] processName]; |
| 3720 |
} |
| 3721 |
|
| 3722 |
menu_delegate = [[GodotMenuDelegate alloc] init]; |
| 3723 |
|
| 3724 |
// Setup Dock menu. |
| 3725 |
NSMenu *dock_menu = [[NSMenu alloc] initWithTitle:@"_dock"]; |
| 3726 |
[dock_menu setAutoenablesItems:NO]; |
| 3727 |
[dock_menu setDelegate:menu_delegate]; |
| 3728 |
|
| 3729 |
// Setup Apple menu. |
| 3730 |
NSMenu *application_menu = [[NSMenu alloc] initWithTitle:@""]; |
| 3731 |
title = [NSString stringWithFormat:NSLocalizedString(@"About %@", nil), nsappname]; |
| 3732 |
[application_menu addItemWithTitle:title action:@selector(showAbout:) keyEquivalent:@""]; |
| 3733 |
[application_menu setAutoenablesItems:NO]; |
| 3734 |
[application_menu setDelegate:menu_delegate]; |
| 3735 |
|
| 3736 |
[application_menu addItem:[NSMenuItem separatorItem]]; |
| 3737 |
|
| 3738 |
menu_item = [application_menu addItemWithTitle:@"_start_" action:nil keyEquivalent:@""]; |
| 3739 |
menu_item.hidden = YES; |
| 3740 |
menu_item.tag = MENU_TAG_START; |
| 3741 |
menu_item = [application_menu addItemWithTitle:@"_end_" action:nil keyEquivalent:@""]; |
| 3742 |
menu_item.hidden = YES; |
| 3743 |
menu_item.tag = MENU_TAG_END; |
| 3744 |
|
| 3745 |
NSMenu *services = [[NSMenu alloc] initWithTitle:@""]; |
| 3746 |
menu_item = [application_menu addItemWithTitle:NSLocalizedString(@"Services", nil) action:nil keyEquivalent:@""]; |
| 3747 |
[application_menu setSubmenu:services forItem:menu_item]; |
| 3748 |
[NSApp setServicesMenu:services]; |
| 3749 |
|
| 3750 |
[application_menu addItem:[NSMenuItem separatorItem]]; |
| 3751 |
|
| 3752 |
title = [NSString stringWithFormat:NSLocalizedString(@"Hide %@", nil), nsappname]; |
| 3753 |
[application_menu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; |
| 3754 |
|
| 3755 |
menu_item = [application_menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil) action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; |
| 3756 |
[menu_item setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)]; |
| 3757 |
|
| 3758 |
[application_menu addItemWithTitle:NSLocalizedString(@"Show all", nil) action:@selector(unhideAllApplications:) keyEquivalent:@""]; |
| 3759 |
|
| 3760 |
[application_menu addItem:[NSMenuItem separatorItem]]; |
| 3761 |
|
| 3762 |
title = [NSString stringWithFormat:NSLocalizedString(@"Quit %@", nil), nsappname]; |
| 3763 |
[application_menu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; |
| 3764 |
|
| 3765 |
NSMenu *window_menu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Window", nil)]; |
| 3766 |
[window_menu addItemWithTitle:NSLocalizedString(@"Minimize", nil) action:@selector(performMiniaturize:) keyEquivalent:@"m"]; |
| 3767 |
[window_menu addItemWithTitle:NSLocalizedString(@"Zoom", nil) action:@selector(performZoom:) keyEquivalent:@""]; |
| 3768 |
[window_menu addItem:[NSMenuItem separatorItem]]; |
| 3769 |
[window_menu addItemWithTitle:NSLocalizedString(@"Bring All to Front", nil) action:@selector(bringAllToFront:) keyEquivalent:@""]; |
| 3770 |
[window_menu addItem:[NSMenuItem separatorItem]]; |
| 3771 |
menu_item = [window_menu addItemWithTitle:@"_start_" action:nil keyEquivalent:@""]; |
| 3772 |
menu_item.hidden = YES; |
| 3773 |
menu_item.tag = MENU_TAG_START; |
| 3774 |
menu_item = [window_menu addItemWithTitle:@"_end_" action:nil keyEquivalent:@""]; |
| 3775 |
menu_item.hidden = YES; |
| 3776 |
menu_item.tag = MENU_TAG_END; |
| 3777 |
|
| 3778 |
NSMenu *help_menu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Help", nil)]; |
| 3779 |
menu_item = [help_menu addItemWithTitle:@"_start_" action:nil keyEquivalent:@""]; |
| 3780 |
menu_item.hidden = YES; |
| 3781 |
menu_item.tag = MENU_TAG_START; |
| 3782 |
menu_item = [help_menu addItemWithTitle:@"_end_" action:nil keyEquivalent:@""]; |
| 3783 |
menu_item.hidden = YES; |
| 3784 |
menu_item.tag = MENU_TAG_END; |
| 3785 |
|
| 3786 |
[NSApp setWindowsMenu:window_menu]; |
| 3787 |
[NSApp setHelpMenu:help_menu]; |
| 3788 |
|
| 3789 |
// Add items to the menu bar. |
| 3790 |
NSMenu *main_menu = [NSApp mainMenu]; |
| 3791 |
menu_item = [main_menu addItemWithTitle:@"" action:nil keyEquivalent:@""]; |
| 3792 |
[main_menu setSubmenu:application_menu forItem:menu_item]; |
| 3793 |
|
| 3794 |
menu_item = [main_menu addItemWithTitle:NSLocalizedString(@"Window", nil) action:nil keyEquivalent:@""]; |
| 3795 |
[main_menu setSubmenu:window_menu forItem:menu_item]; |
| 3796 |
|
| 3797 |
menu_item = [main_menu addItemWithTitle:NSLocalizedString(@"Help", nil) action:nil keyEquivalent:@""]; |
| 3798 |
[main_menu setSubmenu:help_menu forItem:menu_item]; |
| 3799 |
|
| 3800 |
[main_menu setAutoenablesItems:NO]; |
| 3801 |
|
| 3802 |
native_menu->_register_system_menus(main_menu, application_menu, window_menu, help_menu, dock_menu); |
| 3803 |
|
| 3804 |
//!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 3805 |
//TODO - do Vulkan and OpenGL support checks, driver selection and fallback |
| 3806 |
rendering_driver = p_rendering_driver; |
| 3807 |
|
| 3808 |
#if defined(RD_ENABLED) |
| 3809 |
#if defined(VULKAN_ENABLED) |
| 3810 |
#if defined(__x86_64__) |
| 3811 |
bool fallback_to_vulkan = GLOBAL_GET("rendering/rendering_device/fallback_to_vulkan"); |
| 3812 |
if (!fallback_to_vulkan) { |
| 3813 |
WARN_PRINT("Metal is not supported on Intel Macs, switching to Vulkan."); |
| 3814 |
} |
| 3815 |
// Metal rendering driver not available on Intel. |
| 3816 |
if (rendering_driver == "metal") { |
| 3817 |
rendering_driver = "vulkan"; |
| 3818 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 3819 |
} |
| 3820 |
#endif |
| 3821 |
if (rendering_driver == "vulkan") { |
| 3822 |
rendering_context = memnew(RenderingContextDriverVulkanMacOS); |
| 3823 |
} |
| 3824 |
#endif |
| 3825 |
#if defined(METAL_ENABLED) |
| 3826 |
if (rendering_driver == "metal") { |
| 3827 |
rendering_context = memnew(RenderingContextDriverMetal); |
| 3828 |
} |
| 3829 |
#endif |
| 3830 |
|
| 3831 |
if (rendering_context) { |
| 3832 |
if (rendering_context->initialize() != OK) { |
| 3833 |
memdelete(rendering_context); |
| 3834 |
rendering_context = nullptr; |
| 3835 |
#if defined(GLES3_ENABLED) |
| 3836 |
bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3"); |
| 3837 |
if (fallback_to_opengl3 && rendering_driver != "opengl3") { |
| 3838 |
WARN_PRINT("Your device does not seem to support MoltenVK or Metal, switching to OpenGL 3."); |
| 3839 |
rendering_driver = "opengl3"; |
| 3840 |
OS::get_singleton()->set_current_rendering_method("gl_compatibility"); |
| 3841 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 3842 |
} else |
| 3843 |
#endif |
| 3844 |
{ |
| 3845 |
r_error = ERR_CANT_CREATE; |
| 3846 |
ERR_FAIL_MSG("Could not initialize " + rendering_driver); |
| 3847 |
} |
| 3848 |
} |
| 3849 |
} |
| 3850 |
#endif |
| 3851 |
|
| 3852 |
#if defined(GLES3_ENABLED) |
| 3853 |
if (rendering_driver == "opengl3" && OS::get_singleton()->get_processor_name().contains("Virtual")) { |
| 3854 |
WARN_PRINT("Virtual Machine detected, switching to ANGLE."); |
| 3855 |
rendering_driver = "opengl3_angle"; |
| 3856 |
} |
| 3857 |
if (rendering_driver == "opengl3_angle") { |
| 3858 |
gl_manager_angle = memnew(GLManagerANGLE_MacOS); |
| 3859 |
if (gl_manager_angle->initialize() != OK || gl_manager_angle->open_display(nullptr) != OK) { |
| 3860 |
memdelete(gl_manager_angle); |
| 3861 |
gl_manager_angle = nullptr; |
| 3862 |
if (OS::get_singleton()->get_processor_name().contains("Virtual")) { |
| 3863 |
r_error = ERR_UNAVAILABLE; |
| 3864 |
ERR_FAIL_MSG("Could not initialize ANGLE OpenGL."); |
| 3865 |
} |
| 3866 |
bool fallback = GLOBAL_GET("rendering/gl_compatibility/fallback_to_native"); |
| 3867 |
if (fallback) { |
| 3868 |
#ifdef EGL_STATIC |
| 3869 |
WARN_PRINT("Your video card drivers seem not to support GLES3 / ANGLE, switching to native OpenGL."); |
| 3870 |
#else |
| 3871 |
WARN_PRINT("Your video card drivers seem not to support GLES3 / ANGLE or ANGLE dynamic libraries (libEGL.dylib and libGLESv2.dylib) are missing, switching to native OpenGL."); |
| 3872 |
#endif |
| 3873 |
rendering_driver = "opengl3"; |
| 3874 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 3875 |
} else { |
| 3876 |
r_error = ERR_UNAVAILABLE; |
| 3877 |
ERR_FAIL_MSG("Could not initialize ANGLE OpenGL."); |
| 3878 |
} |
| 3879 |
} |
| 3880 |
} |
| 3881 |
|
| 3882 |
if (rendering_driver == "opengl3") { |
| 3883 |
gl_manager_legacy = memnew(GLManagerLegacy_MacOS); |
| 3884 |
if (gl_manager_legacy->initialize() != OK) { |
| 3885 |
memdelete(gl_manager_legacy); |
| 3886 |
gl_manager_legacy = nullptr; |
| 3887 |
r_error = ERR_UNAVAILABLE; |
| 3888 |
ERR_FAIL_MSG("Could not initialize native OpenGL."); |
| 3889 |
} |
| 3890 |
} |
| 3891 |
#endif |
| 3892 |
|
| 3893 |
Point2i window_position; |
| 3894 |
if (p_position != nullptr) { |
| 3895 |
window_position = *p_position; |
| 3896 |
} else { |
| 3897 |
if (p_screen == SCREEN_OF_MAIN_WINDOW) { |
| 3898 |
p_screen = SCREEN_PRIMARY; |
| 3899 |
} |
| 3900 |
Rect2i scr_rect = screen_get_usable_rect(p_screen); |
| 3901 |
window_position = scr_rect.position + (scr_rect.size - p_resolution) / 2; |
| 3902 |
} |
| 3903 |
|
| 3904 |
WindowID main_window = _create_window(p_mode, p_vsync_mode, Rect2i(window_position, p_resolution)); |
| 3905 |
ERR_FAIL_COND(main_window == INVALID_WINDOW_ID); |
| 3906 |
for (int i = 0; i < WINDOW_FLAG_MAX; i++) { |
| 3907 |
if (p_flags & (1 << i)) { |
| 3908 |
window_set_flag(WindowFlags(i), true, main_window); |
| 3909 |
} |
| 3910 |
} |
| 3911 |
force_process_and_drop_events(); |
| 3912 |
|
| 3913 |
if (rendering_driver == "dummy") { |
| 3914 |
RasterizerDummy::make_current(); |
| 3915 |
} |
| 3916 |
|
| 3917 |
#if defined(GLES3_ENABLED) |
| 3918 |
if (rendering_driver == "opengl3") { |
| 3919 |
RasterizerGLES3::make_current(true); |
| 3920 |
} |
| 3921 |
if (rendering_driver == "opengl3_angle") { |
| 3922 |
RasterizerGLES3::make_current(false); |
| 3923 |
} |
| 3924 |
#endif |
| 3925 |
#if defined(RD_ENABLED) |
| 3926 |
if (rendering_context) { |
| 3927 |
rendering_device = memnew(RenderingDevice); |
| 3928 |
rendering_device->initialize(rendering_context, MAIN_WINDOW_ID); |
| 3929 |
rendering_device->screen_create(MAIN_WINDOW_ID); |
| 3930 |
|
| 3931 |
RendererCompositorRD::make_current(); |
| 3932 |
} |
| 3933 |
#endif |
| 3934 |
|
| 3935 |
screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on")); |
| 3936 |
} |
| 3937 |
|
| 3938 |
DisplayServerMacOS::~DisplayServerMacOS() { |
| 3939 |
if (screen_keep_on_assertion) { |
| 3940 |
IOPMAssertionRelease(screen_keep_on_assertion); |
| 3941 |
screen_keep_on_assertion = kIOPMNullAssertionID; |
| 3942 |
} |
| 3943 |
|
| 3944 |
// Destroy all status indicators. |
| 3945 |
for (HashMap<IndicatorID, IndicatorData>::Iterator E = indicators.begin(); E; ++E) { |
| 3946 |
[[NSStatusBar systemStatusBar] removeStatusItem:E->value.item]; |
| 3947 |
} |
| 3948 |
|
| 3949 |
// Destroy native menu. |
| 3950 |
if (native_menu) { |
| 3951 |
memdelete(native_menu); |
| 3952 |
native_menu = nullptr; |
| 3953 |
} |
| 3954 |
|
| 3955 |
// Destroy all windows. |
| 3956 |
for (HashMap<WindowID, WindowData>::Iterator E = windows.begin(); E;) { |
| 3957 |
HashMap<WindowID, WindowData>::Iterator F = E; |
| 3958 |
++E; |
| 3959 |
[F->value.window_object setContentView:nil]; |
| 3960 |
[F->value.window_object close]; |
| 3961 |
} |
| 3962 |
|
| 3963 |
// Destroy drivers. |
| 3964 |
#if defined(GLES3_ENABLED) |
| 3965 |
if (gl_manager_legacy) { |
| 3966 |
memdelete(gl_manager_legacy); |
| 3967 |
gl_manager_legacy = nullptr; |
| 3968 |
} |
| 3969 |
if (gl_manager_angle) { |
| 3970 |
memdelete(gl_manager_angle); |
| 3971 |
gl_manager_angle = nullptr; |
| 3972 |
} |
| 3973 |
#endif |
| 3974 |
#if defined(RD_ENABLED) |
| 3975 |
if (rendering_device) { |
| 3976 |
memdelete(rendering_device); |
| 3977 |
rendering_device = nullptr; |
| 3978 |
} |
| 3979 |
|
| 3980 |
if (rendering_context) { |
| 3981 |
memdelete(rendering_context); |
| 3982 |
rendering_context = nullptr; |
| 3983 |
} |
| 3984 |
#endif |
| 3985 |
#ifdef ACCESSKIT_ENABLED |
| 3986 |
if (accessibility_driver) { |
| 3987 |
memdelete(accessibility_driver); |
| 3988 |
} |
| 3989 |
#endif |
| 3990 |
CGDisplayRemoveReconfigurationCallback(_displays_arrangement_changed, nullptr); |
| 3991 |
|
| 3992 |
cursors_cache.clear(); |
| 3993 |
} |