2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* display_server_macos.mm */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
#include "display_server_macos.h"
2020-03-07 16:02:54 +00:00
2022-09-20 08:12:49 +00:00
#include "godot_button_view.h"
2022-01-26 09:01:27 +00:00
#include "godot_content_view.h"
2022-08-31 08:11:52 +00:00
#include "godot_menu_delegate.h"
2022-01-26 09:01:27 +00:00
#include "godot_menu_item.h"
#include "godot_window.h"
#include "godot_window_delegate.h"
2022-07-20 06:28:22 +00:00
#include "key_mapping_macos.h"
#include "os_macos.h"
#include "tts_macos.h"
2021-11-04 12:33:37 +00:00
2022-08-04 07:38:26 +00:00
#include "core/config/project_settings.h"
2020-03-07 16:02:54 +00:00
#include "core/io/marshalls.h"
2020-06-29 09:31:36 +00:00
#include "core/math/geometry_2d.h"
2020-03-07 16:02:54 +00:00
#include "core/os/keyboard.h"
2022-08-02 12:05:37 +00:00
#include "drivers/png/png_driver_common.h"
2020-03-07 16:02:54 +00:00
#include "main/main.h"
2023-07-11 20:29:09 +00:00
#include "scene/resources/atlas_texture.h"
#include "scene/resources/image_texture.h"
2020-03-07 16:02:54 +00:00
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2021-11-02 10:27:38 +00:00
#include "drivers/gles3/rasterizer_gles3.h"
2020-03-07 16:02:54 +00:00
#endif
#if defined(VULKAN_ENABLED)
2020-12-04 18:26:24 +00:00
#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
2020-04-26 18:17:10 +00:00
#endif
2023-06-08 12:51:32 +00:00
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#import <IOKit/IOCFPlugIn.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/hid/IOHIDKeys.h>
#import <IOKit/hid/IOHIDLib.h>
2022-07-20 06:28:22 +00:00
const NSMenu *DisplayServerMacOS::_get_menu_root(const String &p_menu_root) const {
2022-01-26 09:01:27 +00:00
const NSMenu *menu = nullptr;
2022-08-01 09:28:16 +00:00
if (p_menu_root == "" || p_menu_root.to_lower() == "_main") {
2022-01-26 09:01:27 +00:00
// Main menu.
menu = [NSApp mainMenu];
} else if (p_menu_root.to_lower() == "_dock") {
// macOS dock menu.
menu = dock_menu;
} else {
// Submenu.
if (submenu.has(p_menu_root)) {
menu = submenu[p_menu_root];
2020-03-07 16:02:54 +00:00
}
}
2022-01-26 09:01:27 +00:00
if (menu == apple_menu) {
// Do not allow to change Apple menu.
return nullptr;
2020-03-30 13:18:29 +00:00
}
2022-01-26 09:01:27 +00:00
return menu;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
NSMenu *DisplayServerMacOS::_get_menu_root(const String &p_menu_root) {
2022-01-26 09:01:27 +00:00
NSMenu *menu = nullptr;
2022-08-01 09:28:16 +00:00
if (p_menu_root == "" || p_menu_root.to_lower() == "_main") {
2022-01-26 09:01:27 +00:00
// Main menu.
menu = [NSApp mainMenu];
} else if (p_menu_root.to_lower() == "_dock") {
// macOS dock menu.
menu = dock_menu;
} else {
// Submenu.
if (!submenu.has(p_menu_root)) {
NSMenu *n_menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:p_menu_root.utf8().get_data()]];
2022-03-22 09:26:36 +00:00
[n_menu setAutoenablesItems:NO];
2022-08-31 08:11:52 +00:00
[n_menu setDelegate:menu_delegate];
2022-01-26 09:01:27 +00:00
submenu[p_menu_root] = n_menu;
}
menu = submenu[p_menu_root];
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
if (menu == apple_menu) {
// Do not allow to change Apple menu.
return nullptr;
2020-03-30 13:18:29 +00:00
}
2022-01-26 09:01:27 +00:00
return menu;
2020-03-07 16:02:54 +00:00
}
2023-01-04 22:00:02 +00:00
DisplayServerMacOS::WindowID DisplayServerMacOS::_create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect) {
2022-01-26 09:01:27 +00:00
WindowID id;
const float scale = screen_get_max_scale();
{
WindowData wd;
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
wd.window_delegate = [[GodotWindowDelegate alloc] init];
ERR_FAIL_COND_V_MSG(wd.window_delegate == nil, INVALID_WINDOW_ID, "Can't create a window delegate");
[wd.window_delegate setWindowID:window_id_counter];
2020-03-07 16:02:54 +00:00
2023-01-04 22:00:02 +00:00
int rq_screen = get_screen_from_rect(p_rect);
if (rq_screen < 0) {
rq_screen = get_primary_screen(); // Requested window rect is outside any screen bounds.
2022-12-08 18:34:15 +00:00
}
2023-01-04 22:00:02 +00:00
Rect2i srect = screen_get_usable_rect(rq_screen);
Point2i wpos = p_rect.position;
if (srect != Rect2i()) {
wpos.x = CLAMP(wpos.x, srect.position.x, srect.position.x + srect.size.width - p_rect.size.width / 3);
wpos.y = CLAMP(wpos.y, srect.position.y, srect.position.y + srect.size.height - p_rect.size.height / 3);
}
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2022-01-26 09:01:27 +00:00
// Godot passes a positive value.
2022-12-08 18:34:15 +00:00
wpos.y *= -1;
wpos += _get_screens_origin();
2023-01-04 22:00:02 +00:00
wpos /= scale;
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// initWithContentRect uses bottom-left corner of the window’ s frame as origin.
wd.window_object = [[GodotWindow alloc]
2023-01-04 22:00:02 +00:00
initWithContentRect:NSMakeRect(100, 100, p_rect.size.width / scale, p_rect.size.height / scale)
2022-01-26 09:01:27 +00:00
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered
defer:NO];
ERR_FAIL_COND_V_MSG(wd.window_object == nil, INVALID_WINDOW_ID, "Can't create a window");
2022-02-08 11:22:03 +00:00
[wd.window_object setWindowID:window_id_counter];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
wd.window_view = [[GodotContentView alloc] init];
ERR_FAIL_COND_V_MSG(wd.window_view == nil, INVALID_WINDOW_ID, "Can't create a window view");
[wd.window_view setWindowID:window_id_counter];
[wd.window_view setWantsLayer:TRUE];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
[wd.window_object setContentView:wd.window_view];
[wd.window_object setDelegate:wd.window_delegate];
[wd.window_object setAcceptsMouseMovedEvents:YES];
[wd.window_object setRestorable:NO];
[wd.window_object setColorSpace:[NSColorSpace sRGBColorSpace]];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if ([wd.window_object respondsToSelector:@selector(setTabbingMode:)]) {
[wd.window_object setTabbingMode:NSWindowTabbingModeDisallowed];
}
2020-03-07 16:02:54 +00:00
2022-05-10 18:02:26 +00:00
CALayer *layer = [(NSView *)wd.window_view layer];
2020-07-03 10:06:03 +00:00
if (layer) {
layer.contentsScale = scale;
2020-03-07 16:02:54 +00:00
}
2020-07-03 10:06:03 +00:00
2023-01-12 22:02:07 +00:00
NSColor *bg_color = [NSColor windowBackgroundColor];
Color _bg_color;
if (_get_window_early_clear_override(_bg_color)) {
bg_color = [NSColor colorWithCalibratedRed:_bg_color.r green:_bg_color.g blue:_bg_color.b alpha:1.f];
}
[wd.window_object setBackgroundColor:bg_color];
if (layer) {
[layer setBackgroundColor:bg_color.CGColor];
}
2022-01-26 09:01:27 +00:00
#if defined(VULKAN_ENABLED)
if (context_vulkan) {
Error err = context_vulkan->window_create(window_id_counter, p_vsync_mode, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan context");
}
#endif
#if defined(GLES3_ENABLED)
if (gl_manager) {
Error err = gl_manager->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL context");
}
2022-11-15 22:14:21 +00:00
window_set_vsync_mode(p_vsync_mode, window_id_counter);
2022-01-26 09:01:27 +00:00
#endif
2022-06-10 11:02:51 +00:00
[wd.window_view updateLayerDelegate];
2023-01-04 22:00:02 +00:00
const NSRect contentRect = [wd.window_view frame];
const NSRect windowRect = [wd.window_object frame];
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect];
Point2i offset;
offset.x = (nsrect.origin.x - windowRect.origin.x);
offset.y = (nsrect.origin.y + nsrect.size.height);
offset.y -= (windowRect.origin.y + windowRect.size.height);
[wd.window_object setFrameTopLeftPoint:NSMakePoint(wpos.x - offset.x, wpos.y - offset.y)];
2022-01-26 09:01:27 +00:00
id = window_id_counter++;
windows[id] = wd;
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
WindowData &wd = windows[id];
window_set_mode(p_mode, id);
2020-03-07 16:02:54 +00:00
2020-07-03 10:06:03 +00:00
const NSRect contentRect = [wd.window_view frame];
wd.size.width = contentRect.size.width * scale;
wd.size.height = contentRect.size.height * scale;
2022-05-10 18:02:26 +00:00
CALayer *layer = [(NSView *)wd.window_view layer];
2020-07-03 10:06:03 +00:00
if (layer) {
layer.contentsScale = scale;
}
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2022-01-26 09:01:27 +00:00
if (gl_manager) {
gl_manager->window_resize(id, wd.size.width, wd.size.height);
2020-03-07 16:02:54 +00:00
}
#endif
#if defined(VULKAN_ENABLED)
2022-01-26 09:01:27 +00:00
if (context_vulkan) {
context_vulkan->window_resize(id, wd.size.width, wd.size.height);
2020-03-07 16:02:54 +00:00
}
#endif
2022-01-26 09:01:27 +00:00
return id;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_update_window_style(WindowData p_wd) {
2022-01-26 09:01:27 +00:00
bool borderless_full = false;
2020-03-30 13:18:29 +00:00
2022-01-26 09:01:27 +00:00
if (p_wd.borderless) {
NSRect frameRect = [p_wd.window_object frame];
NSRect screenRect = [[p_wd.window_object screen] frame];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Check if our window covers up the screen.
if (frameRect.origin.x <= screenRect.origin.x && frameRect.origin.y <= frameRect.origin.y &&
frameRect.size.width >= screenRect.size.width && frameRect.size.height >= screenRect.size.height) {
borderless_full = true;
}
2020-03-30 13:18:29 +00:00
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (borderless_full) {
// If the window covers up the screen set the level to above the main menu and hide on deactivate.
2022-05-10 18:02:26 +00:00
[(NSWindow *)p_wd.window_object setLevel:NSMainMenuWindowLevel + 1];
[(NSWindow *)p_wd.window_object setHidesOnDeactivate:YES];
2021-08-08 19:27:57 +00:00
} else {
2022-01-26 09:01:27 +00:00
// Reset these when our window is not a borderless window that covers up the screen.
if (p_wd.on_top && !p_wd.fullscreen) {
2022-05-10 18:02:26 +00:00
[(NSWindow *)p_wd.window_object setLevel:NSFloatingWindowLevel];
2022-01-26 09:01:27 +00:00
} else {
2022-05-10 18:02:26 +00:00
[(NSWindow *)p_wd.window_object setLevel:NSNormalWindowLevel];
2022-01-26 09:01:27 +00:00
}
2022-05-10 18:02:26 +00:00
[(NSWindow *)p_wd.window_object setHidesOnDeactivate:NO];
2020-03-30 13:18:29 +00:00
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_set_window_per_pixel_transparency_enabled(bool p_enabled, WindowID p_window) {
2022-01-26 09:01:27 +00:00
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (!OS::get_singleton()->is_layered_allowed()) {
2020-03-30 13:18:29 +00:00
return;
}
2022-01-26 09:01:27 +00:00
if (wd.layered_window != p_enabled) {
if (p_enabled) {
[wd.window_object setBackgroundColor:[NSColor clearColor]];
[wd.window_object setOpaque:NO];
[wd.window_object setHasShadow:NO];
2022-05-10 18:02:26 +00:00
CALayer *layer = [(NSView *)wd.window_view layer];
2022-01-26 09:01:27 +00:00
if (layer) {
[layer setBackgroundColor:[NSColor clearColor].CGColor];
[layer setOpaque:NO];
}
2021-11-02 10:27:38 +00:00
#if defined(GLES3_ENABLED)
2022-01-26 09:01:27 +00:00
if (gl_manager) {
gl_manager->window_set_per_pixel_transparency_enabled(p_window, true);
}
2020-03-07 16:02:54 +00:00
#endif
2022-01-26 09:01:27 +00:00
wd.layered_window = true;
} else {
2023-01-12 22:02:07 +00:00
NSColor *bg_color = [NSColor windowBackgroundColor];
Color _bg_color;
if (_get_window_early_clear_override(_bg_color)) {
bg_color = [NSColor colorWithCalibratedRed:_bg_color.r green:_bg_color.g blue:_bg_color.b alpha:1.f];
}
[wd.window_object setBackgroundColor:bg_color];
2022-01-26 09:01:27 +00:00
[wd.window_object setOpaque:YES];
[wd.window_object setHasShadow:YES];
2022-05-10 18:02:26 +00:00
CALayer *layer = [(NSView *)wd.window_view layer];
2022-01-26 09:01:27 +00:00
if (layer) {
2023-01-12 22:02:07 +00:00
[layer setBackgroundColor:bg_color.CGColor];
2022-01-26 09:01:27 +00:00
[layer setOpaque:YES];
}
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2022-01-26 09:01:27 +00:00
if (gl_manager) {
gl_manager->window_set_per_pixel_transparency_enabled(p_window, false);
}
2020-03-07 16:02:54 +00:00
#endif
2022-01-26 09:01:27 +00:00
wd.layered_window = false;
}
NSRect frameRect = [wd.window_object frame];
[wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:YES];
[wd.window_object setFrame:frameRect display:YES];
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_update_displays_arrangement() {
2022-01-26 09:01:27 +00:00
origin = Point2i();
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
for (int i = 0; i < get_screen_count(); i++) {
Point2i position = _get_native_screen_position(i);
if (position.x < origin.x) {
origin.x = position.x;
}
if (position.y > origin.y) {
origin.y = position.y;
}
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
displays_arrangement_dirty = false;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::_get_screens_origin() const {
2022-01-26 09:01:27 +00:00
// Returns the native top-left screen coordinate of the smallest rectangle
// that encompasses all screens. Needed in get_screen_position(),
// window_get_position, and window_set_position()
2023-03-12 16:48:52 +00:00
// to convert between macOS native screen coordinates and the ones expected by Godot.
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (displays_arrangement_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_displays_arrangement();
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
return origin;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::_get_native_screen_position(int p_screen) const {
2022-01-26 09:01:27 +00:00
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
2023-03-12 16:48:52 +00:00
// Return the top-left corner of the screen, for macOS the y starts at the bottom.
2022-01-26 09:01:27 +00:00
return Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * screen_get_max_scale();
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
return Point2i();
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info) {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-01-26 09:01:27 +00:00
if (ds) {
ds->displays_arrangement_dirty = true;
}
2020-03-07 16:02:54 +00:00
}
2022-09-07 17:31:57 +00:00
DisplayServer::WindowID DisplayServerMacOS::_get_focused_window_or_popup() const {
const List<WindowID>::Element *E = popup_list.back();
if (E) {
return E->get();
}
return last_focused_window;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_dispatch_input_events(const Ref<InputEvent> &p_event) {
((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event);
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_dispatch_input_event(const Ref<InputEvent> &p_event) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
if (!in_dispatch_input_event) {
in_dispatch_input_event = true;
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
Variant ev = p_event;
Variant *evp = &ev;
2020-03-07 16:02:54 +00:00
Variant ret;
Callable::CallError ce;
2022-02-24 09:21:23 +00:00
{
2022-04-13 06:58:38 +00:00
List<WindowID>::Element *E = popup_list.back();
2022-02-24 09:21:23 +00:00
if (E && Object::cast_to<InputEventKey>(*p_event)) {
// Redirect keyboard input to active popup.
if (windows.has(E->get())) {
Callable callable = windows[E->get()].input_event_callback;
if (callable.is_valid()) {
2022-07-28 20:56:41 +00:00
callable.callp((const Variant **)&evp, 1, ret, ce);
2022-02-24 09:21:23 +00:00
}
}
in_dispatch_input_event = false;
return;
}
}
2022-01-26 09:01:27 +00:00
Ref<InputEventFromWindow> event_from_window = p_event;
if (event_from_window.is_valid() && event_from_window->get_window_id() != INVALID_WINDOW_ID) {
// Send to a window.
if (windows.has(event_from_window->get_window_id())) {
Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
2022-02-24 09:21:23 +00:00
if (callable.is_valid()) {
2022-07-28 20:56:41 +00:00
callable.callp((const Variant **)&evp, 1, ret, ce);
2022-01-26 09:01:27 +00:00
}
}
} else {
// Send to all windows.
2022-02-24 09:21:23 +00:00
for (KeyValue<WindowID, WindowData> &E : windows) {
Callable callable = E.value.input_event_callback;
if (callable.is_valid()) {
2022-07-28 20:56:41 +00:00
callable.callp((const Variant **)&evp, 1, ret, ce);
2022-01-26 09:01:27 +00:00
}
}
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
in_dispatch_input_event = false;
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_push_input(const Ref<InputEvent> &p_event) {
2022-01-26 09:01:27 +00:00
Ref<InputEvent> ev = p_event;
Input::get_singleton()->parse_input_event(ev);
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_process_key_events() {
2022-01-26 09:01:27 +00:00
Ref<InputEventKey> k;
for (int i = 0; i < key_event_pos; i++) {
const KeyEvent &ke = key_event_buffer[i];
if (ke.raw) {
// Non IME input - no composite characters, pass events as is.
k.instantiate();
2020-07-02 12:55:53 +00:00
2022-01-26 09:01:27 +00:00
k->set_window_id(ke.window_id);
2022-07-20 06:28:22 +00:00
get_key_modifier_state(ke.macos_state, k);
2022-01-26 09:01:27 +00:00
k->set_pressed(ke.pressed);
k->set_echo(ke.echo);
k->set_keycode(ke.keycode);
2022-12-10 23:21:22 +00:00
k->set_physical_keycode(ke.physical_keycode);
k->set_key_label(ke.key_label);
2022-01-26 09:01:27 +00:00
k->set_unicode(ke.unicode);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
_push_input(k);
} else {
// IME input.
if ((i == 0 && ke.keycode == Key::NONE) || (i > 0 && key_event_buffer[i - 1].keycode == Key::NONE)) {
k.instantiate();
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
k->set_window_id(ke.window_id);
2022-07-20 06:28:22 +00:00
get_key_modifier_state(ke.macos_state, k);
2022-01-26 09:01:27 +00:00
k->set_pressed(ke.pressed);
k->set_echo(ke.echo);
k->set_keycode(Key::NONE);
k->set_physical_keycode(Key::NONE);
2022-12-10 23:21:22 +00:00
k->set_key_label(Key::NONE);
2022-01-26 09:01:27 +00:00
k->set_unicode(ke.unicode);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
_push_input(k);
}
if (ke.keycode != Key::NONE) {
k.instantiate();
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
k->set_window_id(ke.window_id);
2022-07-20 06:28:22 +00:00
get_key_modifier_state(ke.macos_state, k);
2022-01-26 09:01:27 +00:00
k->set_pressed(ke.pressed);
k->set_echo(ke.echo);
k->set_keycode(ke.keycode);
2022-12-10 23:21:22 +00:00
k->set_physical_keycode(ke.physical_keycode);
k->set_key_label(ke.key_label);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (i + 1 < key_event_pos && key_event_buffer[i + 1].keycode == Key::NONE) {
k->set_unicode(key_event_buffer[i + 1].unicode);
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
_push_input(k);
}
}
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
key_event_pos = 0;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_update_keyboard_layouts() {
2022-01-26 09:01:27 +00:00
kbd_layouts.clear();
current_layout = 0;
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
TISInputSourceRef cur_source = TISCopyCurrentKeyboardInputSource();
NSString *cur_name = (__bridge NSString *)TISGetInputSourceProperty(cur_source, kTISPropertyLocalizedName);
CFRelease(cur_source);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Enum IME layouts.
NSDictionary *filter_ime = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardInputMode };
NSArray *list_ime = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_ime, false);
for (NSUInteger i = 0; i < [list_ime count]; i++) {
LayoutInfo ly;
NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyLocalizedName);
ly.name.parse_utf8([name UTF8String]);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
NSArray *langs = (__bridge NSArray *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyInputSourceLanguages);
ly.code.parse_utf8([(NSString *)[langs objectAtIndex:0] UTF8String]);
kbd_layouts.push_back(ly);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if ([name isEqualToString:cur_name]) {
current_layout = kbd_layouts.size() - 1;
}
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Enum plain keyboard layouts.
NSDictionary *filter_kbd = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardLayout };
NSArray *list_kbd = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_kbd, false);
for (NSUInteger i = 0; i < [list_kbd count]; i++) {
LayoutInfo ly;
NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyLocalizedName);
ly.name.parse_utf8([name UTF8String]);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
NSArray *langs = (__bridge NSArray *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyInputSourceLanguages);
ly.code.parse_utf8([(NSString *)[langs objectAtIndex:0] UTF8String]);
kbd_layouts.push_back(ly);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if ([name isEqualToString:cur_name]) {
current_layout = kbd_layouts.size() - 1;
}
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
keyboard_layout_dirty = false;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::_keyboard_layout_changed(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef user_info) {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-01-26 09:01:27 +00:00
if (ds) {
ds->keyboard_layout_dirty = true;
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
NSImage *DisplayServerMacOS::_convert_to_nsimg(Ref<Image> &p_image) const {
2022-03-22 09:26:36 +00:00
p_image->convert(Image::FORMAT_RGBA8);
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:p_image->get_width()
pixelsHigh:p_image->get_height()
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:int(p_image->get_width()) * 4
bitsPerPixel:32];
ERR_FAIL_COND_V(imgrep == nil, nil);
uint8_t *pixels = [imgrep bitmapData];
int len = p_image->get_width() * p_image->get_height();
const uint8_t *r = p_image->get_data().ptr();
/* Premultiply the alpha channel */
for (int i = 0; i < len; i++) {
uint8_t alpha = r[i * 4 + 3];
pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255);
pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255);
pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255);
pixels[i * 4 + 3] = alpha;
}
NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(p_image->get_width(), p_image->get_height())];
ERR_FAIL_COND_V(nsimg == nil, nil);
[nsimg addRepresentation:imgrep];
return nsimg;
}
2022-07-20 06:28:22 +00:00
NSCursor *DisplayServerMacOS::_cursor_from_selector(SEL p_selector, SEL p_fallback) {
2022-01-26 09:01:27 +00:00
if ([NSCursor respondsToSelector:p_selector]) {
id object = [NSCursor performSelector:p_selector];
if ([object isKindOfClass:[NSCursor class]]) {
return object;
2020-03-07 16:02:54 +00:00
}
}
2022-01-26 09:01:27 +00:00
if (p_fallback) {
// Fallback should be a reasonable default, no need to check.
return [NSCursor performSelector:p_fallback];
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
return [NSCursor arrowCursor];
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
NSMenu *DisplayServerMacOS::get_dock_menu() const {
2022-01-26 09:01:27 +00:00
return dock_menu;
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::menu_callback(id p_sender) {
2022-01-26 09:01:27 +00:00
if (![p_sender representedObject]) {
return;
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
GodotMenuItem *value = [p_sender representedObject];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (value) {
2022-03-22 09:26:36 +00:00
if (value->max_states > 0) {
value->state++;
if (value->state >= value->max_states) {
value->state = 0;
}
}
if (value->checkable_type == CHECKABLE_TYPE_CHECK_BOX) {
2022-01-26 09:01:27 +00:00
if ([p_sender state] == NSControlStateValueOff) {
[p_sender setState:NSControlStateValueOn];
2020-03-07 16:02:54 +00:00
} else {
2022-01-26 09:01:27 +00:00
[p_sender setState:NSControlStateValueOff];
2020-03-07 16:02:54 +00:00
}
}
2022-01-26 09:01:27 +00:00
if (value->callback != Callable()) {
2022-09-24 20:57:00 +00:00
MenuCall mc;
mc.tag = value->meta;
mc.callback = value->callback;
deferred_menu_calls.push_back(mc);
// 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.
2022-01-26 09:01:27 +00:00
}
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::has_window(WindowID p_window) const {
2022-01-26 09:01:27 +00:00
return windows.has(p_window);
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
DisplayServerMacOS::WindowData &DisplayServerMacOS::get_window(WindowID p_window) {
2022-01-26 09:01:27 +00:00
return windows[p_window];
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::send_event(NSEvent *p_event) {
2022-01-26 09:01:27 +00:00
// Special case handling of command-period, which is traditionally a special
// shortcut in macOS and doesn't arrive at our regular keyDown handler.
if ([p_event type] == NSEventTypeKeyDown) {
2022-10-03 17:44:33 +00:00
if ((([p_event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagCommand) && [p_event keyCode] == 0x2f) {
2022-01-26 09:01:27 +00:00
Ref<InputEventKey> k;
k.instantiate();
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
get_key_modifier_state([p_event modifierFlags], k);
2022-07-20 06:28:22 +00:00
k->set_window_id(DisplayServerMacOS::INVALID_WINDOW_ID);
2022-01-26 09:01:27 +00:00
k->set_pressed(true);
k->set_keycode(Key::PERIOD);
k->set_physical_keycode(Key::PERIOD);
2022-12-10 23:21:22 +00:00
k->set_key_label(Key::PERIOD);
2022-01-26 09:01:27 +00:00
k->set_echo([p_event isARepeat]);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
Input::get_singleton()->parse_input_event(k);
2020-03-07 16:02:54 +00:00
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::send_window_event(const WindowData &wd, WindowEvent p_event) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (!wd.event_callback.is_null()) {
Variant event = int(p_event);
Variant *eventp = &event;
Variant ret;
Callable::CallError ce;
2022-07-28 20:56:41 +00:00
wd.event_callback.callp((const Variant **)&eventp, 1, ret, ce);
2022-01-26 09:01:27 +00:00
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::release_pressed_events() {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
if (Input::get_singleton()) {
Input::get_singleton()->release_pressed_events();
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::get_key_modifier_state(unsigned int p_macos_state, Ref<InputEventWithModifiers> r_state) const {
r_state->set_shift_pressed((p_macos_state & NSEventModifierFlagShift));
r_state->set_ctrl_pressed((p_macos_state & NSEventModifierFlagControl));
r_state->set_alt_pressed((p_macos_state & NSEventModifierFlagOption));
r_state->set_meta_pressed((p_macos_state & NSEventModifierFlagCommand));
2022-01-26 09:01:27 +00:00
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::update_mouse_pos(DisplayServerMacOS::WindowData &p_wd, NSPoint p_location_in_window) {
2022-01-26 09:01:27 +00:00
const NSRect content_rect = [p_wd.window_view frame];
const float scale = screen_get_max_scale();
p_wd.mouse_pos.x = p_location_in_window.x * scale;
p_wd.mouse_pos.y = (content_rect.size.height - p_location_in_window.y) * scale;
Input::get_singleton()->set_mouse_position(p_wd.mouse_pos);
}
2020-10-13 15:58:55 +00:00
2022-12-10 23:21:22 +00:00
void DisplayServerMacOS::pop_last_key_event() {
if (key_event_pos > 0) {
key_event_pos--;
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::push_to_key_event_buffer(const DisplayServerMacOS::KeyEvent &p_event) {
2022-01-26 09:01:27 +00:00
if (key_event_pos >= key_event_buffer.size()) {
key_event_buffer.resize(1 + key_event_pos);
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
key_event_buffer.write[key_event_pos++] = p_event;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::update_im_text(const Point2i &p_selection, const String &p_text) {
2022-01-26 09:01:27 +00:00
im_selection = p_selection;
im_text = p_text;
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE);
2020-03-07 16:02:54 +00:00
}
2020-05-14 12:29:06 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::set_last_focused_window(WindowID p_window) {
2022-01-26 09:01:27 +00:00
last_focused_window = p_window;
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::set_is_resizing(bool p_is_resizing) {
2022-03-04 08:25:39 +00:00
is_resizing = p_is_resizing;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::get_is_resizing() const {
2022-03-04 08:25:39 +00:00
return is_resizing;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_update(WindowID p_window) {
2022-01-26 09:01:27 +00:00
#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->window_update(p_window);
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
#endif
2020-07-03 10:06:03 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_destroy(WindowID p_window) {
2022-01-26 09:01:27 +00:00
#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->window_destroy(p_window);
}
#endif
#ifdef VULKAN_ENABLED
if (context_vulkan) {
context_vulkan->window_destroy(p_window);
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
#endif
windows.erase(p_window);
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_resize(WindowID p_window, int p_width, int p_height) {
2022-01-26 09:01:27 +00:00
#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->window_resize(p_window, p_width, p_height);
}
#endif
#if defined(VULKAN_ENABLED)
if (context_vulkan) {
context_vulkan->window_resize(p_window, p_width, p_height);
}
#endif
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::has_feature(Feature p_feature) const {
2020-03-07 16:02:54 +00:00
switch (p_feature) {
case FEATURE_GLOBAL_MENU:
case FEATURE_SUBWINDOWS:
//case FEATURE_TOUCHSCREEN:
case FEATURE_MOUSE:
case FEATURE_MOUSE_WARP:
case FEATURE_CLIPBOARD:
case FEATURE_CURSOR_SHAPE:
case FEATURE_CUSTOM_CURSOR_SHAPE:
case FEATURE_NATIVE_DIALOG:
case FEATURE_IME:
case FEATURE_WINDOW_TRANSPARENCY:
case FEATURE_HIDPI:
case FEATURE_ICON:
case FEATURE_NATIVE_ICON:
2020-07-03 10:06:03 +00:00
//case FEATURE_KEEP_SCREEN_ON:
2020-03-07 16:02:54 +00:00
case FEATURE_SWAP_BUFFERS:
2021-11-04 12:33:37 +00:00
case FEATURE_TEXT_TO_SPEECH:
2022-08-23 10:40:48 +00:00
case FEATURE_EXTEND_TO_TITLE:
2023-02-27 18:36:13 +00:00
case FEATURE_SCREEN_CAPTURE:
2020-03-07 16:02:54 +00:00
return true;
default: {
}
}
return false;
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::get_name() const {
return "macOS";
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
bool DisplayServerMacOS::_has_help_menu() const {
if ([NSApp helpMenu]) {
return true;
} else {
NSMenu *menu = [NSApp mainMenu];
const NSMenuItem *menu_item = [menu itemAtIndex:[menu numberOfItems] - 1];
if (menu_item) {
String menu_name = String::utf8([[menu_item title] UTF8String]);
if (menu_name == "Help" || menu_name == RTR("Help")) {
return true;
}
}
return false;
}
}
2022-03-22 09:26:36 +00:00
2022-08-01 09:28:16 +00:00
NSMenuItem *DisplayServerMacOS::_menu_add_item(const String &p_menu_root, const String &p_label, Key p_accel, int p_index, int *r_out) {
2022-03-22 09:26:36 +00:00
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-07-20 06:28:22 +00:00
String keycode = KeyMappingMacOS::keycode_get_native_string(p_accel & KeyModifierMask::CODE_MASK);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item;
2022-08-01 09:28:16 +00:00
int item_count = ((menu == [NSApp mainMenu]) && _has_help_menu()) ? [menu numberOfItems] - 1 : [menu numberOfItems];
if ((menu == [NSApp mainMenu]) && (p_label == "Help" || p_label == RTR("Help"))) {
p_index = [menu numberOfItems];
} else if (p_index < 0) {
p_index = item_count;
2022-03-22 09:26:36 +00:00
} else {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_index++;
}
p_index = CLAMP(p_index, 0, item_count);
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
menu_item = [menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()] atIndex:p_index];
*r_out = (menu == [NSApp mainMenu]) ? p_index - 1 : p_index;
return menu_item;
}
return nullptr;
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2022-08-01 09:28:16 +00:00
_THREAD_SAFE_METHOD_
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-03-22 09:26:36 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2022-03-22 09:26:36 +00:00
obj->meta = p_tag;
obj->checkable_type = CHECKABLE_TYPE_NONE;
obj->max_states = 0;
obj->state = 0;
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2022-03-22 09:26:36 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2022-03-22 09:26:36 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-03-22 09:26:36 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2022-03-22 09:26:36 +00:00
obj->meta = p_tag;
obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
obj->max_states = 0;
obj->state = 0;
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2022-03-22 09:26:36 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2022-03-22 09:26:36 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_icon_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-03-22 09:26:36 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2022-03-22 09:26:36 +00:00
obj->meta = p_tag;
obj->checkable_type = CHECKABLE_TYPE_NONE;
obj->max_states = 0;
obj->state = 0;
if (p_icon.is_valid()) {
obj->img = p_icon->get_image();
obj->img = obj->img->duplicate();
if (obj->img->is_compressed()) {
obj->img->decompress();
}
obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
[menu_item setImage:_convert_to_nsimg(obj->img)];
}
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2022-03-22 09:26:36 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2022-03-22 09:26:36 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_icon_check_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-03-22 09:26:36 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2022-03-22 09:26:36 +00:00
obj->meta = p_tag;
obj->checkable_type = CHECKABLE_TYPE_CHECK_BOX;
obj->max_states = 0;
obj->state = 0;
if (p_icon.is_valid()) {
obj->img = p_icon->get_image();
obj->img = obj->img->duplicate();
if (obj->img->is_compressed()) {
obj->img->decompress();
}
obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
[menu_item setImage:_convert_to_nsimg(obj->img)];
}
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2022-03-22 09:26:36 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2022-03-22 09:26:36 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_radio_check_item(const String &p_menu_root, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-03-22 09:26:36 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2022-03-22 09:26:36 +00:00
obj->meta = p_tag;
obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
obj->max_states = 0;
obj->state = 0;
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2022-03-22 09:26:36 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2022-03-22 09:26:36 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_icon_radio_check_item(const String &p_menu_root, const Ref<Texture2D> &p_icon, const String &p_label, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
2020-03-07 16:02:54 +00:00
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2020-03-07 16:02:54 +00:00
obj->meta = p_tag;
2022-03-22 09:26:36 +00:00
obj->checkable_type = CHECKABLE_TYPE_RADIO_BUTTON;
obj->max_states = 0;
obj->state = 0;
if (p_icon.is_valid()) {
obj->img = p_icon->get_image();
obj->img = obj->img->duplicate();
if (obj->img->is_compressed()) {
obj->img->decompress();
}
obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
[menu_item setImage:_convert_to_nsimg(obj->img)];
}
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2020-03-07 16:02:54 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2020-03-07 16:02:54 +00:00
}
2022-08-31 08:11:52 +00:00
int DisplayServerMacOS::global_menu_add_multistate_item(const String &p_menu_root, const String &p_label, int p_max_states, int p_default_state, const Callable &p_callback, const Callable &p_key_callback, const Variant &p_tag, Key p_accel, int p_index) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-08-01 09:28:16 +00:00
int out = -1;
2022-08-31 08:11:52 +00:00
NSMenuItem *menu_item = _menu_add_item(p_menu_root, p_label, p_accel, p_index, &out);
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
2020-03-07 16:02:54 +00:00
obj->callback = p_callback;
2022-08-31 08:11:52 +00:00
obj->key_callback = p_key_callback;
2020-03-07 16:02:54 +00:00
obj->meta = p_tag;
2022-03-22 09:26:36 +00:00
obj->checkable_type = CHECKABLE_TYPE_NONE;
obj->max_states = p_max_states;
obj->state = p_default_state;
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_accel)];
2020-03-07 16:02:54 +00:00
[menu_item setRepresentedObject:obj];
}
2022-08-01 09:28:16 +00:00
return out;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
int DisplayServerMacOS::global_menu_add_submenu_item(const String &p_menu_root, const String &p_label, const String &p_submenu, int p_index) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
NSMenu *sub_menu = _get_menu_root(p_submenu);
2022-08-01 09:28:16 +00:00
int out = -1;
2020-03-07 16:02:54 +00:00
if (menu && sub_menu) {
if (sub_menu == menu) {
ERR_PRINT("Can't set submenu to self!");
2022-08-01 09:28:16 +00:00
return -1;
2020-03-07 16:02:54 +00:00
}
if ([sub_menu supermenu]) {
ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
2022-08-01 09:28:16 +00:00
return -1;
2020-03-07 16:02:54 +00:00
}
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item;
2022-08-01 09:28:16 +00:00
int item_count = ((menu == [NSApp mainMenu]) && _has_help_menu()) ? [menu numberOfItems] - 1 : [menu numberOfItems];
if ((menu == [NSApp mainMenu]) && (p_label == "Help" || p_label == RTR("Help"))) {
p_index = [menu numberOfItems];
} else if (p_index < 0) {
p_index = item_count;
2022-03-22 09:26:36 +00:00
} else {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_index++;
}
p_index = CLAMP(p_index, 0, item_count);
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
menu_item = [menu insertItemWithTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()] action:nil keyEquivalent:@"" atIndex:p_index];
out = (menu == [NSApp mainMenu]) ? p_index - 1 : p_index;
GodotMenuItem *obj = [[GodotMenuItem alloc] init];
obj->callback = Callable();
obj->checkable_type = CHECKABLE_TYPE_NONE;
obj->max_states = 0;
obj->state = 0;
[menu_item setRepresentedObject:obj];
2022-03-22 09:26:36 +00:00
[sub_menu setTitle:[NSString stringWithUTF8String:p_label.utf8().get_data()]];
2020-03-07 16:02:54 +00:00
[menu setSubmenu:sub_menu forItem:menu_item];
}
2022-08-01 09:28:16 +00:00
return out;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
int DisplayServerMacOS::global_menu_add_separator(const String &p_menu_root, int p_index) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Do not add separators into main menu.
return -1;
}
if (p_index < 0) {
p_index = [menu numberOfItems];
2022-03-22 09:26:36 +00:00
} else {
2022-08-01 09:28:16 +00:00
p_index = CLAMP(p_index, 0, [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
[menu insertItem:[NSMenuItem separatorItem] atIndex:p_index];
return p_index;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
return -1;
2022-03-22 09:26:36 +00:00
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::global_menu_get_item_index_from_text(const String &p_menu_root, const String &p_text) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
return [menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]] - 1;
} else {
return [menu indexOfItemWithTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
}
2020-03-07 16:02:54 +00:00
}
2022-03-22 09:26:36 +00:00
return -1;
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::global_menu_get_item_index_from_tag(const String &p_menu_root, const Variant &p_tag) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
for (NSInteger i = (menu == [NSApp mainMenu]) ? 1 : 0; i < [menu numberOfItems]; i++) {
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:i];
if (menu_item) {
const GodotMenuItem *obj = [menu_item representedObject];
if (obj && obj->meta == p_tag) {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
return i - 1;
} else {
return i;
}
2022-03-22 09:26:36 +00:00
}
}
}
}
return -1;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::global_menu_is_item_checked(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, false);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
return ([menu_item state] == NSControlStateValueOn);
}
}
return false;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::global_menu_is_item_checkable(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, false);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2020-03-07 16:02:54 +00:00
if (obj) {
2022-03-22 09:26:36 +00:00
return obj->checkable_type == CHECKABLE_TYPE_CHECK_BOX;
}
}
}
return false;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::global_menu_is_item_radio_checkable(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, false);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
if (obj) {
return obj->checkable_type == CHECKABLE_TYPE_RADIO_BUTTON;
2020-03-07 16:02:54 +00:00
}
}
}
return false;
}
2022-07-20 06:28:22 +00:00
Callable DisplayServerMacOS::global_menu_get_item_callback(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, Callable());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Callable());
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2020-03-07 16:02:54 +00:00
if (obj) {
return obj->callback;
}
}
}
return Callable();
}
2022-08-31 08:11:52 +00:00
Callable DisplayServerMacOS::global_menu_get_item_key_callback(const String &p_menu_root, int p_idx) const {
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
ERR_FAIL_COND_V(p_idx < 0, Callable());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Callable());
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
if (obj) {
return obj->key_callback;
}
}
}
return Callable();
}
2022-07-20 06:28:22 +00:00
Variant DisplayServerMacOS::global_menu_get_item_tag(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, Variant());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Variant());
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2020-03-07 16:02:54 +00:00
if (obj) {
return obj->meta;
}
}
}
return Variant();
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::global_menu_get_item_text(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, String());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-03-22 09:26:36 +00:00
return String::utf8([[menu_item title] UTF8String]);
2020-03-07 16:02:54 +00:00
}
}
return String();
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::global_menu_get_item_submenu(const String &p_menu_root, int p_idx) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, String());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
2020-03-07 16:02:54 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
const NSMenu *sub_menu = [menu_item submenu];
if (sub_menu) {
2022-05-13 13:04:37 +00:00
for (const KeyValue<String, NSMenu *> &E : submenu) {
if (E.value == sub_menu) {
return E.key;
2022-02-16 12:56:32 +00:00
}
2020-03-07 16:02:54 +00:00
}
}
}
}
return String();
}
2022-07-20 06:28:22 +00:00
Key DisplayServerMacOS::global_menu_get_item_accelerator(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, Key::NONE);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Key::NONE);
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
String ret = String::utf8([[menu_item keyEquivalent] UTF8String]);
Key keycode = find_keycode(ret);
NSUInteger mask = [menu_item keyEquivalentModifierMask];
if (mask & NSEventModifierFlagControl) {
keycode |= KeyModifierMask::CTRL;
}
if (mask & NSEventModifierFlagOption) {
keycode |= KeyModifierMask::ALT;
}
if (mask & NSEventModifierFlagShift) {
keycode |= KeyModifierMask::SHIFT;
}
if (mask & NSEventModifierFlagCommand) {
keycode |= KeyModifierMask::META;
}
if (mask & NSEventModifierFlagNumericPad) {
keycode |= KeyModifierMask::KPAD;
}
return keycode;
}
}
return Key::NONE;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::global_menu_is_item_disabled(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, false);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], false);
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
return ![menu_item isEnabled];
}
}
return false;
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::global_menu_get_item_tooltip(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, String());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], String());
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
return String::utf8([[menu_item toolTip] UTF8String]);
}
}
return String();
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::global_menu_get_item_state(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
if (obj) {
return obj->state;
}
}
}
return 0;
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::global_menu_get_item_max_states(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
if (obj) {
return obj->max_states;
}
}
}
return 0;
}
2022-07-20 06:28:22 +00:00
Ref<Texture2D> DisplayServerMacOS::global_menu_get_item_icon(const String &p_menu_root, int p_idx) const {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], Ref<Texture2D>());
2022-03-22 09:26:36 +00:00
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
if (obj) {
if (obj->img.is_valid()) {
2022-05-03 23:49:20 +00:00
return ImageTexture::create_from_image(obj->img);
2022-03-22 09:26:36 +00:00
}
}
}
}
return Ref<Texture2D>();
}
2022-08-01 09:28:16 +00:00
int DisplayServerMacOS::global_menu_get_item_indentation_level(const String &p_menu_root, int p_idx) const {
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
ERR_FAIL_COND_V(p_idx < 0, 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND_V(p_idx >= [menu numberOfItems], 0);
const NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
return [menu_item indentationLevel];
}
}
return 0;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_checked(const String &p_menu_root, int p_idx, bool p_checked) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
if (p_checked) {
[menu_item setState:NSControlStateValueOn];
} else {
[menu_item setState:NSControlStateValueOff];
}
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_checkable(const String &p_menu_root, int p_idx, bool p_checkable) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
2022-03-22 09:26:36 +00:00
obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_CHECK_BOX : CHECKABLE_TYPE_NONE;
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_radio_checkable(const String &p_menu_root, int p_idx, bool p_checkable) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
2022-03-22 09:26:36 +00:00
obj->checkable_type = (p_checkable) ? CHECKABLE_TYPE_RADIO_BUTTON : CHECKABLE_TYPE_NONE;
2020-03-07 16:02:54 +00:00
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_callback(const String &p_menu_root, int p_idx, const Callable &p_callback) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
2020-03-07 16:02:54 +00:00
obj->callback = p_callback;
}
}
}
2022-08-31 08:11:52 +00:00
void DisplayServerMacOS::global_menu_set_item_key_callback(const String &p_menu_root, int p_idx, const Callable &p_key_callback) {
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
ERR_FAIL_COND(!obj);
obj->key_callback = p_key_callback;
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_tag(const String &p_menu_root, int p_idx, const Variant &p_tag) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-01-26 09:01:27 +00:00
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
2020-03-07 16:02:54 +00:00
obj->meta = p_tag;
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_text(const String &p_menu_root, int p_idx, const String &p_text) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
[menu_item setTitle:[NSString stringWithUTF8String:p_text.utf8().get_data()]];
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_submenu(const String &p_menu_root, int p_idx, const String &p_submenu) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
NSMenu *sub_menu = _get_menu_root(p_submenu);
if (menu && sub_menu) {
if (sub_menu == menu) {
ERR_PRINT("Can't set submenu to self!");
return;
}
if ([sub_menu supermenu]) {
ERR_PRINT("Can't set submenu to menu that is already a submenu of some other menu!");
return;
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
[menu setSubmenu:sub_menu forItem:menu_item];
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_accelerator(const String &p_menu_root, int p_idx, Key p_keycode) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
2022-07-20 06:28:22 +00:00
[menu_item setKeyEquivalentModifierMask:KeyMappingMacOS::keycode_get_native_mask(p_keycode)];
String keycode = KeyMappingMacOS::keycode_get_native_string(p_keycode & KeyModifierMask::CODE_MASK);
2022-03-22 09:26:36 +00:00
[menu_item setKeyEquivalent:[NSString stringWithUTF8String:keycode.utf8().get_data()]];
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_disabled(const String &p_menu_root, int p_idx, bool p_disabled) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
[menu_item setEnabled:(!p_disabled)];
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_tooltip(const String &p_menu_root, int p_idx, const String &p_tooltip) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
[menu_item setToolTip:[NSString stringWithUTF8String:p_tooltip.utf8().get_data()]];
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_state(const String &p_menu_root, int p_idx, int p_state) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
obj->state = p_state;
2022-03-22 09:26:36 +00:00
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_max_states(const String &p_menu_root, int p_idx, int p_max_states) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
obj->max_states = p_max_states;
2022-03-22 09:26:36 +00:00
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_set_item_icon(const String &p_menu_root, int p_idx, const Ref<Texture2D> &p_icon) {
2022-03-22 09:26:36 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2022-03-22 09:26:36 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2022-03-22 09:26:36 +00:00
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
GodotMenuItem *obj = [menu_item representedObject];
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(!obj);
2022-03-22 09:26:36 +00:00
if (p_icon.is_valid()) {
obj->img = p_icon->get_image();
obj->img = obj->img->duplicate();
if (obj->img->is_compressed()) {
obj->img->decompress();
}
obj->img->resize(16, 16, Image::INTERPOLATE_LANCZOS);
[menu_item setImage:_convert_to_nsimg(obj->img)];
} else {
obj->img = Ref<Image>();
[menu_item setImage:nil];
}
}
}
}
2022-08-01 09:28:16 +00:00
void DisplayServerMacOS::global_menu_set_item_indentation_level(const String &p_menu_root, int p_idx, int p_level) {
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
}
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
NSMenuItem *menu_item = [menu itemAtIndex:p_idx];
if (menu_item) {
[menu_item setIndentationLevel:p_level];
}
}
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::global_menu_get_item_count(const String &p_menu_root) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
return [menu numberOfItems] - 1;
} else {
return [menu numberOfItems];
}
2020-03-07 16:02:54 +00:00
} else {
return 0;
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_remove_item(const String &p_menu_root, int p_idx) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx < 0);
if (menu == [NSApp mainMenu]) { // Skip Apple menu.
p_idx++;
2020-03-07 16:02:54 +00:00
}
2022-08-01 09:28:16 +00:00
ERR_FAIL_COND(p_idx >= [menu numberOfItems]);
2020-03-07 16:02:54 +00:00
[menu removeItemAtIndex:p_idx];
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::global_menu_clear(const String &p_menu_root) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSMenu *menu = _get_menu_root(p_menu_root);
if (menu) {
[menu removeAllItems];
// Restore Apple menu.
if (menu == [NSApp mainMenu]) {
NSMenuItem *menu_item = [menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
[menu setSubmenu:apple_menu forItem:menu_item];
}
2022-08-01 09:28:16 +00:00
if (submenu.has(p_menu_root)) {
submenu.erase(p_menu_root);
}
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::tts_is_speaking() const {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
return [tts isSpeaking];
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::tts_is_paused() const {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_V_MSG(!tts, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
return [tts isPaused];
}
2022-08-05 01:41:48 +00:00
TypedArray<Dictionary> DisplayServerMacOS::tts_get_voices() const {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_V_MSG(!tts, Array(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
return [tts getVoices];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
[tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::tts_pause() {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
[tts pauseSpeaking];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::tts_resume() {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
[tts resumeSpeaking];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::tts_stop() {
2023-05-16 11:18:12 +00:00
ERR_FAIL_COND_MSG(!tts, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
2021-11-04 12:33:37 +00:00
[tts stopSpeaking];
}
2022-08-29 08:24:48 +00:00
bool DisplayServerMacOS::is_dark_mode_supported() const {
if (@available(macOS 10.14, *)) {
return true;
} else {
return false;
}
}
bool DisplayServerMacOS::is_dark_mode() const {
if (@available(macOS 10.14, *)) {
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"AppleInterfaceStyle"]) {
return false;
} else {
return ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"] isEqual:@"Dark"]);
}
} else {
return false;
}
}
Color DisplayServerMacOS::get_accent_color() const {
if (@available(macOS 10.14, *)) {
NSColor *color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
if (color) {
CGFloat components[4];
[color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
return Color(components[0], components[1], components[2], components[3]);
} else {
return Color(0, 0, 0, 0);
}
} else {
return Color(0, 0, 0, 0);
}
}
2022-07-20 06:28:22 +00:00
Error DisplayServerMacOS::dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSAlert *window = [[NSAlert alloc] init];
NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
for (int i = 0; i < p_buttons.size(); i++) {
NSString *ns_button = [NSString stringWithUTF8String:p_buttons[i].utf8().get_data()];
[window addButtonWithTitle:ns_button];
}
[window setMessageText:ns_title];
[window setInformativeText:ns_description];
[window setAlertStyle:NSAlertStyleInformational];
int button_pressed;
NSInteger ret = [window runModal];
if (ret == NSAlertFirstButtonReturn) {
button_pressed = 0;
} else if (ret == NSAlertSecondButtonReturn) {
button_pressed = 1;
} else if (ret == NSAlertThirdButtonReturn) {
button_pressed = 2;
} else {
button_pressed = 2 + (ret - NSAlertThirdButtonReturn);
}
if (!p_callback.is_null()) {
Variant button = button_pressed;
Variant *buttonp = &button;
2023-05-11 10:32:23 +00:00
Variant fun_ret;
2020-03-07 16:02:54 +00:00
Callable::CallError ce;
2023-05-11 10:32:23 +00:00
p_callback.callp((const Variant **)&buttonp, 1, fun_ret, ce);
2020-03-07 16:02:54 +00:00
}
return OK;
}
2021-03-30 12:42:50 +00:00
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) {
_THREAD_SAFE_METHOD_
NSString *url = [NSString stringWithUTF8String:p_current_directory.utf8().get_data()];
NSMutableArray *allowed_types = [[NSMutableArray alloc] init];
bool allow_other = false;
for (int i = 0; i < p_filters.size(); i++) {
Vector<String> tokens = p_filters[i].split(";");
if (tokens.size() > 0) {
if (tokens[0].strip_edges() == "*.*") {
allow_other = true;
} else {
[allowed_types addObject:[NSString stringWithUTF8String:tokens[0].replace("*.", "").strip_edges().utf8().get_data()]];
}
}
}
Callable callback = p_callback; // Make a copy for async completion handler.
switch (p_mode) {
case FILE_DIALOG_MODE_SAVE_FILE: {
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setDirectoryURL:[NSURL fileURLWithPath:url]];
if ([allowed_types count]) {
[panel setAllowedFileTypes:allowed_types];
}
[panel setAllowsOtherFileTypes:allow_other];
[panel setExtensionHidden:YES];
[panel setCanSelectHiddenExtension:YES];
[panel setCanCreateDirectories:YES];
[panel setShowsHiddenFiles:p_show_hidden];
if (p_filename != "") {
NSString *fileurl = [NSString stringWithUTF8String:p_filename.utf8().get_data()];
[panel setNameFieldStringValue:fileurl];
}
[panel beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow]
completionHandler:^(NSInteger ret) {
if (ret == NSModalResponseOK) {
// Save bookmark for folder.
if (OS::get_singleton()->is_sandboxed()) {
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"];
bool skip = false;
for (id bookmark in bookmarks) {
NSError *error = nil;
BOOL isStale = NO;
NSURL *exurl = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error];
if (!error && !isStale && ([[exurl path] compare:[[panel directoryURL] path]] == NSOrderedSame)) {
skip = true;
break;
}
}
if (!skip) {
NSError *error = nil;
NSData *bookmark = [[panel directoryURL] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
if (!error) {
NSArray *new_bookmarks = [bookmarks arrayByAddingObject:bookmark];
[[NSUserDefaults standardUserDefaults] setObject:new_bookmarks forKey:@"sec_bookmarks"];
}
}
}
// Callback.
Vector<String> files;
String url;
url.parse_utf8([[[panel URL] path] UTF8String]);
files.push_back(url);
if (!callback.is_null()) {
Variant v_status = true;
Variant v_files = files;
Variant *v_args[2] = { &v_status, &v_files };
Variant ret;
Callable::CallError ce;
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
} else {
if (!callback.is_null()) {
Variant v_status = false;
Variant v_files = Vector<String>();
Variant *v_args[2] = { &v_status, &v_files };
Variant ret;
Callable::CallError ce;
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
}
}];
} break;
case FILE_DIALOG_MODE_OPEN_ANY:
case FILE_DIALOG_MODE_OPEN_FILE:
case FILE_DIALOG_MODE_OPEN_FILES:
case FILE_DIALOG_MODE_OPEN_DIR: {
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setDirectoryURL:[NSURL fileURLWithPath:url]];
if ([allowed_types count]) {
[panel setAllowedFileTypes:allowed_types];
}
[panel setAllowsOtherFileTypes:allow_other];
[panel setExtensionHidden:YES];
[panel setCanSelectHiddenExtension:YES];
[panel setCanCreateDirectories:YES];
[panel setCanChooseFiles:(p_mode != FILE_DIALOG_MODE_OPEN_DIR)];
[panel setCanChooseDirectories:(p_mode == FILE_DIALOG_MODE_OPEN_DIR || p_mode == FILE_DIALOG_MODE_OPEN_ANY)];
[panel setShowsHiddenFiles:p_show_hidden];
if (p_filename != "") {
NSString *fileurl = [NSString stringWithUTF8String:p_filename.utf8().get_data()];
[panel setNameFieldStringValue:fileurl];
}
[panel setAllowsMultipleSelection:(p_mode == FILE_DIALOG_MODE_OPEN_FILES)];
[panel beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow]
completionHandler:^(NSInteger ret) {
if (ret == NSModalResponseOK) {
// Save bookmark for folder.
NSArray *urls = [(NSOpenPanel *)panel URLs];
if (OS::get_singleton()->is_sandboxed()) {
NSArray *bookmarks = [[NSUserDefaults standardUserDefaults] arrayForKey:@"sec_bookmarks"];
NSMutableArray *new_bookmarks = [bookmarks mutableCopy];
for (NSUInteger i = 0; i != [urls count]; ++i) {
bool skip = false;
for (id bookmark in bookmarks) {
NSError *error = nil;
BOOL isStale = NO;
NSURL *exurl = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&error];
if (!error && !isStale && ([[exurl path] compare:[[urls objectAtIndex:i] path]] == NSOrderedSame)) {
skip = true;
break;
}
}
if (!skip) {
NSError *error = nil;
NSData *bookmark = [[urls objectAtIndex:i] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
if (!error) {
[new_bookmarks addObject:bookmark];
}
}
}
[[NSUserDefaults standardUserDefaults] setObject:new_bookmarks forKey:@"sec_bookmarks"];
}
// Callback.
Vector<String> files;
for (NSUInteger i = 0; i != [urls count]; ++i) {
String url;
url.parse_utf8([[[urls objectAtIndex:i] path] UTF8String]);
files.push_back(url);
}
if (!callback.is_null()) {
Variant v_status = true;
Variant v_files = files;
Variant *v_args[2] = { &v_status, &v_files };
Variant ret;
Callable::CallError ce;
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
} else {
if (!callback.is_null()) {
Variant v_status = false;
Variant v_files = Vector<String>();
Variant *v_args[2] = { &v_status, &v_files };
Variant ret;
Callable::CallError ce;
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
}
}];
} break;
}
return OK;
}
2022-07-20 06:28:22 +00:00
Error DisplayServerMacOS::dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSAlert *window = [[NSAlert alloc] init];
NSString *ns_title = [NSString stringWithUTF8String:p_title.utf8().get_data()];
NSString *ns_description = [NSString stringWithUTF8String:p_description.utf8().get_data()];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 30)];
[window addButtonWithTitle:@"OK"];
[window setMessageText:ns_title];
[window setInformativeText:ns_description];
[window setAlertStyle:NSAlertStyleInformational];
[input setStringValue:[NSString stringWithUTF8String:p_partial.utf8().get_data()]];
[window setAccessoryView:input];
[window runModal];
String ret;
2022-01-26 09:01:27 +00:00
ret.parse_utf8([[input stringValue] UTF8String]);
2020-03-07 16:02:54 +00:00
if (!p_callback.is_null()) {
Variant text = ret;
Variant *textp = &text;
2023-05-11 10:32:23 +00:00
Variant fun_ret;
2020-03-07 16:02:54 +00:00
Callable::CallError ce;
2023-05-11 10:32:23 +00:00
p_callback.callp((const Variant **)&textp, 1, fun_ret, ce);
2020-03-07 16:02:54 +00:00
}
return OK;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::mouse_set_mode(MouseMode p_mode) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2020-07-03 10:06:03 +00:00
if (p_mode == mouse_mode) {
2020-03-07 16:02:54 +00:00
return;
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
2022-09-07 17:31:57 +00:00
WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
2022-02-01 02:49:51 +00:00
WindowData &wd = windows[window_id];
2022-11-10 17:23:10 +00:00
bool show_cursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
bool previously_shown = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED);
if (show_cursor && !previously_shown) {
2023-05-11 10:32:23 +00:00
window_id = get_window_at_screen_position(mouse_get_position());
2022-11-10 17:23:10 +00:00
if (window_id != INVALID_WINDOW_ID) {
send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER);
}
}
2020-03-07 16:02:54 +00:00
if (p_mode == MOUSE_MODE_CAPTURED) {
// Apple Docs state that the display parameter is not used.
// "This parameter is not used. By default, you may pass kCGDirectMainDisplay."
// https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/Quartz_Services_Ref/Reference/reference.html
2022-11-10 17:23:10 +00:00
if (previously_shown) {
2020-06-22 08:38:22 +00:00
CGDisplayHideCursor(kCGDirectMainDisplay);
}
2020-03-07 16:02:54 +00:00
CGAssociateMouseAndMouseCursorPosition(false);
2021-11-11 12:02:08 +00:00
[wd.window_object setMovable:NO];
2020-09-25 05:55:07 +00:00
const NSRect contentRect = [wd.window_view frame];
NSRect pointInWindowRect = NSMakeRect(contentRect.size.width / 2, contentRect.size.height / 2, 0, 0);
NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
CGWarpMouseCursorPosition(lMouseWarpPos);
2020-03-07 16:02:54 +00:00
} else if (p_mode == MOUSE_MODE_HIDDEN) {
2022-11-10 17:23:10 +00:00
if (previously_shown) {
2020-06-22 08:38:22 +00:00
CGDisplayHideCursor(kCGDirectMainDisplay);
}
2021-11-11 12:02:08 +00:00
[wd.window_object setMovable:YES];
2020-03-07 16:02:54 +00:00
CGAssociateMouseAndMouseCursorPosition(true);
2020-07-02 12:55:53 +00:00
} else if (p_mode == MOUSE_MODE_CONFINED) {
CGDisplayShowCursor(kCGDirectMainDisplay);
2021-11-11 12:02:08 +00:00
[wd.window_object setMovable:NO];
2020-07-02 12:55:53 +00:00
CGAssociateMouseAndMouseCursorPosition(false);
2021-03-30 22:35:08 +00:00
} else if (p_mode == MOUSE_MODE_CONFINED_HIDDEN) {
2022-11-10 17:23:10 +00:00
if (previously_shown) {
2021-03-30 22:35:08 +00:00
CGDisplayHideCursor(kCGDirectMainDisplay);
}
2021-11-11 12:02:08 +00:00
[wd.window_object setMovable:NO];
2021-03-30 22:35:08 +00:00
CGAssociateMouseAndMouseCursorPosition(false);
} else { // MOUSE_MODE_VISIBLE
2020-03-07 16:02:54 +00:00
CGDisplayShowCursor(kCGDirectMainDisplay);
2021-11-11 12:02:08 +00:00
[wd.window_object setMovable:YES];
2020-03-07 16:02:54 +00:00
CGAssociateMouseAndMouseCursorPosition(true);
}
2022-01-26 09:01:27 +00:00
last_warp = [[NSProcessInfo processInfo] systemUptime];
ignore_warp = true;
warp_events.clear();
mouse_mode = p_mode;
2022-11-10 17:23:10 +00:00
if (show_cursor) {
2022-01-26 09:01:27 +00:00
cursor_update_shape();
}
}
2022-07-20 06:28:22 +00:00
DisplayServer::MouseMode DisplayServerMacOS::mouse_get_mode() const {
2022-01-26 09:01:27 +00:00
return mouse_mode;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
if (ignore_warp) {
// Discard late events, before warp.
if (p_timestamp < last_warp) {
return true;
}
ignore_warp = false;
return true;
}
if (mouse_mode == DisplayServer::MOUSE_MODE_CONFINED || mouse_mode == DisplayServer::MOUSE_MODE_CONFINED_HIDDEN) {
// Discard late events.
if (p_timestamp < last_warp) {
return true;
}
// Warp affects next event delta, subtract previous warp deltas.
List<WarpEvent>::Element *F = warp_events.front();
while (F) {
if (F->get().timestamp < p_timestamp) {
2022-07-20 06:28:22 +00:00
List<DisplayServerMacOS::WarpEvent>::Element *E = F;
2022-01-26 09:01:27 +00:00
r_delta.x -= E->get().delta.x;
r_delta.y -= E->get().delta.y;
F = F->next();
warp_events.erase(E);
} else {
F = F->next();
}
}
// Confine mouse position to the window, and update delta.
NSRect frame = [p_wd.window_object frame];
NSPoint conf_pos = r_mpos;
conf_pos.x = CLAMP(conf_pos.x + r_delta.x, 0.f, frame.size.width);
conf_pos.y = CLAMP(conf_pos.y - r_delta.y, 0.f, frame.size.height);
r_delta.x = conf_pos.x - r_mpos.x;
r_delta.y = r_mpos.y - conf_pos.y;
r_mpos = conf_pos;
2021-06-23 07:51:01 +00:00
2022-01-26 09:01:27 +00:00
// Move mouse cursor.
NSRect point_in_window_rect = NSMakeRect(conf_pos.x, conf_pos.y, 0, 0);
conf_pos = [[p_wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
conf_pos.y = CGDisplayBounds(CGMainDisplayID()).size.height - conf_pos.y;
CGWarpMouseCursorPosition(conf_pos);
// Save warp data.
last_warp = [[NSProcessInfo processInfo] systemUptime];
2022-07-20 06:28:22 +00:00
DisplayServerMacOS::WarpEvent ev;
2022-01-26 09:01:27 +00:00
ev.timestamp = last_warp;
ev.delta = r_delta;
warp_events.push_back(ev);
2021-06-23 07:51:01 +00:00
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
return false;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::warp_mouse(const Point2i &p_position) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-01-26 09:01:27 +00:00
if (mouse_mode != MOUSE_MODE_CAPTURED) {
2022-09-07 17:31:57 +00:00
WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
2022-02-01 02:49:51 +00:00
WindowData &wd = windows[window_id];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Local point in window coords.
2020-03-07 16:02:54 +00:00
const NSRect contentRect = [wd.window_view frame];
2020-07-03 10:06:03 +00:00
const float scale = screen_get_max_scale();
2023-02-21 06:30:28 +00:00
NSRect pointInWindowRect = NSMakeRect(p_position.x / scale, contentRect.size.height - (p_position.y / scale), scale, scale);
2020-03-07 16:02:54 +00:00
NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
2022-01-26 09:01:27 +00:00
// Point in scren coords.
2020-03-07 16:02:54 +00:00
CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
2022-01-26 09:01:27 +00:00
// Do the warping.
2020-03-07 16:02:54 +00:00
CGEventSourceRef lEventRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsSuppressionInterval(lEventRef, 0.0);
CGAssociateMouseAndMouseCursorPosition(false);
CGWarpMouseCursorPosition(lMouseWarpPos);
2021-03-30 22:35:08 +00:00
if (mouse_mode != MOUSE_MODE_CONFINED && mouse_mode != MOUSE_MODE_CONFINED_HIDDEN) {
2020-07-02 12:55:53 +00:00
CGAssociateMouseAndMouseCursorPosition(true);
}
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::mouse_get_position() const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
const NSPoint mouse_pos = [NSEvent mouseLocation];
2020-07-03 10:06:03 +00:00
const float scale = screen_get_max_scale();
2020-03-07 16:02:54 +00:00
for (NSScreen *screen in [NSScreen screens]) {
NSRect frame = [screen frame];
if (NSMouseInRect(mouse_pos, frame, NO)) {
2022-03-24 10:04:41 +00:00
Vector2i pos = Vector2i((int)mouse_pos.x, (int)mouse_pos.y);
pos *= scale;
pos -= _get_screens_origin();
pos.y *= -1;
return pos;
2020-03-07 16:02:54 +00:00
}
}
return Vector2i();
}
2023-01-07 23:55:54 +00:00
void DisplayServerMacOS::mouse_set_button_state(BitField<MouseButtonMask> p_state) {
2022-01-26 09:01:27 +00:00
last_button_state = p_state;
}
2023-01-07 23:55:54 +00:00
BitField<MouseButtonMask> DisplayServerMacOS::mouse_get_button_state() const {
2020-03-07 16:02:54 +00:00
return last_button_state;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::clipboard_set(const String &p_text) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSString *copiedString = [NSString stringWithUTF8String:p_text.utf8().get_data()];
NSArray *copiedStringArray = [NSArray arrayWithObject:copiedString];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:copiedStringArray];
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::clipboard_get() const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if (!ok) {
return "";
}
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSString *string = [objectsToPaste objectAtIndex:0];
String ret;
2022-01-26 09:01:27 +00:00
ret.parse_utf8([string UTF8String]);
2020-03-07 16:02:54 +00:00
return ret;
}
2022-08-02 12:05:37 +00:00
Ref<Image> DisplayServerMacOS::clipboard_get_image() const {
Ref<Image> image;
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *result = [pasteboard availableTypeFromArray:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSPasteboardTypePNG, nil]];
if (!result) {
return image;
}
NSData *data = [pasteboard dataForType:result];
if (!data) {
return image;
}
NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:data];
NSData *pngData = [bitmap representationUsingType:NSPNGFileType properties:@{}];
image.instantiate();
PNGDriverCommon::png_to_image((const uint8_t *)pngData.bytes, pngData.length, false, image);
return image;
}
bool DisplayServerMacOS::clipboard_has() const {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
return [pasteboard canReadObjectForClasses:classArray options:options];
}
bool DisplayServerMacOS::clipboard_has_image() const {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *result = [pasteboard availableTypeFromArray:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSPasteboardTypePNG, nil]];
return result;
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::get_screen_count() const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
NSArray *screenArray = [NSScreen screens];
return [screenArray count];
}
2023-01-04 22:00:02 +00:00
int DisplayServerMacOS::get_primary_screen() const {
return 0;
}
2023-03-21 11:08:46 +00:00
int DisplayServerMacOS::get_keyboard_focus_screen() const {
const NSUInteger index = [[NSScreen screens] indexOfObject:[NSScreen mainScreen]];
return (index == NSNotFound) ? 0 : index;
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::screen_get_position(int p_screen) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2020-03-07 16:02:54 +00:00
Point2i position = _get_native_screen_position(p_screen) - _get_screens_origin();
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2022-01-26 09:01:27 +00:00
// Godot expects a positive value.
2020-03-07 16:02:54 +00:00
position.y *= -1;
return position;
}
2022-07-20 06:28:22 +00:00
Size2i DisplayServerMacOS::screen_get_size(int p_screen) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2020-03-07 16:02:54 +00:00
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
2022-01-26 09:01:27 +00:00
// Note: Use frame to get the whole screen size.
2020-03-07 16:02:54 +00:00
NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
2020-07-03 10:06:03 +00:00
return Size2i(nsrect.size.width, nsrect.size.height) * screen_get_max_scale();
2020-03-07 16:02:54 +00:00
}
return Size2i();
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::screen_get_dpi(int p_screen) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2020-03-07 16:02:54 +00:00
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
2020-10-01 19:52:20 +00:00
const NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
const CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
float scale = [[screenArray objectAtIndex:p_screen] backingScaleFactor];
float den2 = (displayPhysicalSize.width / 25.4f) * (displayPhysicalSize.width / 25.4f) + (displayPhysicalSize.height / 25.4f) * (displayPhysicalSize.height / 25.4f);
if (den2 > 0.0f) {
return ceil(sqrt(displayPixelSize.width * displayPixelSize.width + displayPixelSize.height * displayPixelSize.height) / sqrt(den2) * scale);
}
2020-03-07 16:02:54 +00:00
}
2020-10-01 19:52:20 +00:00
return 72;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
float DisplayServerMacOS::screen_get_scale(int p_screen) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2020-07-03 10:06:03 +00:00
if (OS::get_singleton()->is_hidpi_allowed()) {
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
if ([[screenArray objectAtIndex:p_screen] respondsToSelector:@selector(backingScaleFactor)]) {
return fmax(1.0, [[screenArray objectAtIndex:p_screen] backingScaleFactor]);
}
}
2020-03-07 16:02:54 +00:00
}
return 1.f;
}
2022-07-20 06:28:22 +00:00
float DisplayServerMacOS::screen_get_max_scale() const {
2020-07-03 10:06:03 +00:00
_THREAD_SAFE_METHOD_
2022-01-15 18:47:34 +00:00
// Note: Do not update max display scale on screen configuration change, existing editor windows can't be rescaled on the fly.
return display_max_scale;
2020-07-03 10:06:03 +00:00
}
2022-07-20 06:28:22 +00:00
Rect2i DisplayServerMacOS::screen_get_usable_rect(int p_screen) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2020-03-07 16:02:54 +00:00
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
2020-07-03 10:06:03 +00:00
const float scale = screen_get_max_scale();
2020-03-07 16:02:54 +00:00
NSRect nsrect = [[screenArray objectAtIndex:p_screen] visibleFrame];
2020-07-03 10:06:03 +00:00
Point2i position = Point2i(nsrect.origin.x, nsrect.origin.y + nsrect.size.height) * scale - _get_screens_origin();
2020-03-07 16:02:54 +00:00
position.y *= -1;
2020-07-03 10:06:03 +00:00
Size2i size = Size2i(nsrect.size.width, nsrect.size.height) * scale;
2020-03-07 16:02:54 +00:00
return Rect2i(position, size);
}
return Rect2i();
}
2023-02-27 18:36:13 +00:00
Color DisplayServerMacOS::screen_get_pixel(const Point2i &p_position) const {
Point2i position = p_position;
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2023-02-27 18:36:13 +00:00
// Godot passes a positive value.
position.y *= -1;
position += _get_screens_origin();
position /= screen_get_max_scale();
for (NSScreen *screen in [NSScreen screens]) {
NSRect frame = [screen frame];
if (NSMouseInRect(NSMakePoint(position.x, position.y), frame, NO)) {
NSDictionary *screenDescription = [screen deviceDescription];
CGDirectDisplayID display_id = [[screenDescription objectForKey:@"NSScreenNumber"] unsignedIntValue];
CGImageRef image = CGDisplayCreateImageForRect(display_id, CGRectMake(position.x - frame.origin.x, frame.size.height - (position.y - frame.origin.y), 1, 1));
if (image) {
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
NSColor *color = [bitmap colorAtX:0 y:0];
if (color) {
CGFloat components[4];
[color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
return Color(components[0], components[1], components[2], components[3]);
}
}
}
}
return Color();
}
2023-03-16 21:26:09 +00:00
Ref<Image> DisplayServerMacOS::screen_get_image(int p_screen) const {
ERR_FAIL_INDEX_V(p_screen, get_screen_count(), Ref<Image>());
switch (p_screen) {
case SCREEN_PRIMARY: {
p_screen = get_primary_screen();
} break;
case SCREEN_OF_MAIN_WINDOW: {
p_screen = window_get_current_screen(MAIN_WINDOW_ID);
} break;
default:
break;
}
Ref<Image> img;
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSRect nsrect = [[screenArray objectAtIndex:p_screen] frame];
NSDictionary *screenDescription = [[screenArray objectAtIndex:p_screen] deviceDescription];
CGDirectDisplayID display_id = [[screenDescription objectForKey:@"NSScreenNumber"] unsignedIntValue];
CGImageRef image = CGDisplayCreateImageForRect(display_id, CGRectMake(0, 0, nsrect.size.width, nsrect.size.height));
if (image) {
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
if (color_space) {
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
Vector<uint8_t> img_data;
img_data.resize(height * width * 4);
CGContextRef context = CGBitmapContextCreate(img_data.ptrw(), width, height, 8, 4 * width, color_space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if (context) {
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, img_data);
CGContextRelease(context);
}
CGColorSpaceRelease(color_space);
}
CGImageRelease(image);
}
}
return img;
}
2022-07-20 06:28:22 +00:00
float DisplayServerMacOS::screen_get_refresh_rate(int p_screen) const {
2022-01-27 19:46:57 +00:00
_THREAD_SAFE_METHOD_
2023-03-21 11:08:46 +00:00
p_screen = _get_screen_index(p_screen);
2022-01-27 19:46:57 +00:00
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
const CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
const double displayRefreshRate = CGDisplayModeGetRefreshRate(displayMode);
return (float)displayRefreshRate;
}
2022-02-10 11:00:11 +00:00
ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
2022-01-27 19:46:57 +00:00
return SCREEN_REFRESH_RATE_FALLBACK;
}
2022-08-05 10:14:13 +00:00
bool DisplayServerMacOS::screen_is_kept_on() const {
return (screen_keep_on_assertion);
}
2022-08-04 07:38:26 +00:00
void DisplayServerMacOS::screen_set_keep_on(bool p_enable) {
if (screen_keep_on_assertion) {
IOPMAssertionRelease(screen_keep_on_assertion);
screen_keep_on_assertion = kIOPMNullAssertionID;
}
if (p_enable) {
2022-10-18 14:43:37 +00:00
String app_name_string = GLOBAL_GET("application/config/name");
2022-08-04 07:38:26 +00:00
NSString *name = [NSString stringWithUTF8String:(app_name_string.is_empty() ? "Godot Engine" : app_name_string.utf8().get_data())];
NSString *reason = @"Godot Engine running with display/window/energy_saving/keep_screen_on = true";
IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep, (__bridge CFStringRef)name, (__bridge CFStringRef)reason, (__bridge CFStringRef)reason, nullptr, 0, nullptr, &screen_keep_on_assertion);
}
}
2022-07-20 06:28:22 +00:00
Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
Vector<int> ret;
2022-05-13 13:04:37 +00:00
for (const KeyValue<WindowID, WindowData> &E : windows) {
ret.push_back(E.key);
2020-03-07 16:02:54 +00:00
}
return ret;
}
2023-01-04 22:00:02 +00:00
DisplayServer::WindowID DisplayServerMacOS::create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-01-04 22:00:02 +00:00
WindowID id = _create_window(p_mode, p_vsync_mode, p_rect);
2020-03-07 16:02:54 +00:00
for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
if (p_flags & (1 << i)) {
window_set_flag(WindowFlags(i), true, id);
}
}
2020-08-21 07:39:30 +00:00
return id;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::show_window(WindowID p_id) {
2020-08-21 07:39:30 +00:00
WindowData &wd = windows[p_id];
2022-02-24 09:21:23 +00:00
popup_open(p_id);
2022-09-15 07:05:41 +00:00
if ([wd.window_object isMiniaturized]) {
return;
2023-06-09 05:03:13 +00:00
} else if (wd.no_focus) {
2020-07-03 10:06:03 +00:00
[wd.window_object orderFront:nil];
} else {
[wd.window_object makeKeyAndOrderFront:nil];
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::delete_sub_window(WindowID p_id) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_id));
ERR_FAIL_COND_MSG(p_id == MAIN_WINDOW_ID, "Main window can't be deleted");
WindowData &wd = windows[p_id];
[wd.window_object setContentView:nil];
[wd.window_object close];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.rect_changed_callback = p_callable;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.event_callback = p_callable;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.input_event_callback = p_callable;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.input_text_callback = p_callable;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.drop_files_callback = p_callable;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_title(const String &p_title, WindowID p_window) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
[wd.window_object setTitle:[NSString stringWithUTF8String:p_title.utf8().get_data()]];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.mpath = p_region;
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::window_get_current_screen(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), -1);
const WindowData &wd = windows[p_window];
const NSUInteger index = [[NSScreen screens] indexOfObject:[wd.window_object screen]];
return (index == NSNotFound) ? 0 : index;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_current_screen(int p_screen, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-01-15 18:47:34 +00:00
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2022-07-19 09:26:11 +00:00
if (window_get_current_screen(p_window) == p_screen) {
return;
}
2022-01-15 18:47:34 +00:00
bool was_fullscreen = false;
if (wd.fullscreen) {
// Temporary exit fullscreen mode to move window.
[wd.window_object toggleFullScreen:nil];
was_fullscreen = true;
}
2022-12-08 18:34:15 +00:00
Rect2i srect = screen_get_usable_rect(p_screen);
2020-03-07 16:02:54 +00:00
Point2i wpos = window_get_position(p_window) - screen_get_position(window_get_current_screen(p_window));
2022-12-08 18:34:15 +00:00
Size2i wsize = window_get_size(p_window);
wpos += srect.position;
wpos.x = CLAMP(wpos.x, srect.position.x, srect.position.x + srect.size.width - wsize.width / 3);
wpos.y = CLAMP(wpos.y, srect.position.y, srect.position.y + srect.size.height - wsize.height / 3);
window_set_position(wpos, p_window);
2022-01-15 18:47:34 +00:00
if (was_fullscreen) {
// Re-enter fullscreen mode.
[wd.window_object toggleFullScreen:nil];
}
2020-03-07 16:02:54 +00:00
}
2022-11-19 20:25:43 +00:00
void DisplayServerMacOS::reparent_check(WindowID p_window) {
2022-01-19 12:04:05 +00:00
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2022-11-19 20:25:43 +00:00
NSScreen *screen = [wd.window_object screen];
if (wd.transient_parent != INVALID_WINDOW_ID) {
WindowData &wd_parent = windows[wd.transient_parent];
NSScreen *parent_screen = [wd_parent.window_object screen];
if (parent_screen == screen) {
if (![[wd_parent.window_object childWindows] containsObject:wd.window_object]) {
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
2022-01-19 12:04:05 +00:00
[wd_parent.window_object addChildWindow:wd.window_object ordered:NSWindowAbove];
2022-11-19 20:25:43 +00:00
}
} else {
if ([[wd_parent.window_object childWindows] containsObject:wd.window_object]) {
2022-01-19 12:04:05 +00:00
[wd_parent.window_object removeChildWindow:wd.window_object];
2022-11-19 20:25:43 +00:00
[wd.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
[wd.window_object orderFront:nil];
2022-01-19 12:04:05 +00:00
}
}
}
2022-11-19 20:25:43 +00:00
for (const WindowID &child : wd.transient_children) {
WindowData &wd_child = windows[child];
NSScreen *child_screen = [wd_child.window_object screen];
if (child_screen == screen) {
if (![[wd.window_object childWindows] containsObject:wd_child.window_object]) {
if (wd_child.fullscreen) {
[wd_child.window_object toggleFullScreen:nil];
}
[wd_child.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
[wd.window_object addChildWindow:wd_child.window_object ordered:NSWindowAbove];
}
} else {
if ([[wd.window_object childWindows] containsObject:wd_child.window_object]) {
[wd.window_object removeChildWindow:wd_child.window_object];
[wd_child.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
}
}
}
}
void DisplayServerMacOS::window_set_exclusive(WindowID p_window, bool p_exclusive) {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
if (wd.exclusive != p_exclusive) {
wd.exclusive = p_exclusive;
reparent_check(p_window);
}
2022-01-19 12:04:05 +00:00
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::window_get_position(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
const WindowData &wd = windows[p_window];
2022-01-15 18:47:34 +00:00
// Use content rect position (without titlebar / window border).
const NSRect contentRect = [wd.window_view frame];
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect];
2020-03-07 16:02:54 +00:00
Point2i pos;
2023-03-12 16:48:52 +00:00
// Return the position of the top-left corner, for macOS the y starts at the bottom.
2020-07-03 10:06:03 +00:00
const float scale = screen_get_max_scale();
pos.x = nsrect.origin.x;
pos.y = (nsrect.origin.y + nsrect.size.height);
pos *= scale;
2020-03-07 16:02:54 +00:00
pos -= _get_screens_origin();
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2022-01-26 09:01:27 +00:00
// Godot expects a positive value.
2020-03-07 16:02:54 +00:00
pos.y *= -1;
return pos;
}
2022-11-30 08:28:16 +00:00
Point2i DisplayServerMacOS::window_get_position_with_decorations(WindowID p_window) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Point2i());
const WindowData &wd = windows[p_window];
const NSRect nsrect = [wd.window_object frame];
Point2i pos;
2023-03-12 16:48:52 +00:00
// Return the position of the top-left corner, for macOS the y starts at the bottom.
2022-11-30 08:28:16 +00:00
const float scale = screen_get_max_scale();
pos.x = nsrect.origin.x;
pos.y = (nsrect.origin.y + nsrect.size.height);
pos *= scale;
pos -= _get_screens_origin();
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2022-11-30 08:28:16 +00:00
// Godot expects a positive value.
pos.y *= -1;
return pos;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_position(const Point2i &p_position, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2022-11-30 09:34:40 +00:00
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) {
2022-09-15 07:05:41 +00:00
return;
}
2020-03-07 16:02:54 +00:00
Point2i position = p_position;
2023-03-12 16:48:52 +00:00
// macOS native y-coordinate relative to _get_screens_origin() is negative,
2022-01-26 09:01:27 +00:00
// Godot passes a positive value.
2020-03-07 16:02:54 +00:00
position.y *= -1;
position += _get_screens_origin();
2020-07-03 10:06:03 +00:00
position /= screen_get_max_scale();
2020-03-07 16:02:54 +00:00
2022-01-15 18:47:34 +00:00
// Remove titlebar / window border size.
const NSRect contentRect = [wd.window_view frame];
const NSRect windowRect = [wd.window_object frame];
const NSRect nsrect = [wd.window_object convertRectToScreen:contentRect];
Point2i offset;
offset.x = (nsrect.origin.x - windowRect.origin.x);
offset.y = (nsrect.origin.y + nsrect.size.height);
offset.y -= (windowRect.origin.y + windowRect.size.height);
[wd.window_object setFrameTopLeftPoint:NSMakePoint(position.x - offset.x, position.y - offset.y)];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
_update_window_style(wd);
update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_transient(WindowID p_window, WindowID p_parent) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(p_window == p_parent);
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd_window = windows[p_window];
ERR_FAIL_COND(wd_window.transient_parent == p_parent);
ERR_FAIL_COND_MSG(wd_window.on_top, "Windows with the 'on top' can't become transient.");
if (p_parent == INVALID_WINDOW_ID) {
// Remove transient.
ERR_FAIL_COND(wd_window.transient_parent == INVALID_WINDOW_ID);
ERR_FAIL_COND(!windows.has(wd_window.transient_parent));
WindowData &wd_parent = windows[wd_window.transient_parent];
wd_window.transient_parent = INVALID_WINDOW_ID;
wd_parent.transient_children.erase(p_window);
2022-11-19 20:25:43 +00:00
if ([[wd_parent.window_object childWindows] containsObject:wd_window.window_object]) {
2022-01-19 12:04:05 +00:00
[wd_parent.window_object removeChildWindow:wd_window.window_object];
}
2022-11-19 20:25:43 +00:00
[wd_window.window_object setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
2022-01-26 09:01:27 +00:00
} else {
ERR_FAIL_COND(!windows.has(p_parent));
ERR_FAIL_COND_MSG(wd_window.transient_parent != INVALID_WINDOW_ID, "Window already has a transient parent");
WindowData &wd_parent = windows[p_parent];
wd_window.transient_parent = p_parent;
wd_parent.transient_children.insert(p_window);
2022-11-19 20:25:43 +00:00
reparent_check(p_window);
2022-01-26 09:01:27 +00:00
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_max_size(const Size2i p_size, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
if ((p_size != Size2i()) && ((p_size.x < wd.min_size.x) || (p_size.y < wd.min_size.y))) {
ERR_PRINT("Maximum window size can't be smaller than minimum window size!");
return;
}
wd.max_size = p_size;
if ((wd.max_size != Size2i()) && !wd.fullscreen) {
2020-07-03 10:06:03 +00:00
Size2i size = wd.max_size / screen_get_max_scale();
2020-03-07 16:02:54 +00:00
[wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
} else {
[wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
}
}
2022-07-20 06:28:22 +00:00
Size2i DisplayServerMacOS::window_get_max_size(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
const WindowData &wd = windows[p_window];
return wd.max_size;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_min_size(const Size2i p_size, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
if ((p_size != Size2i()) && (wd.max_size != Size2i()) && ((p_size.x > wd.max_size.x) || (p_size.y > wd.max_size.y))) {
ERR_PRINT("Minimum window size can't be larger than maximum window size!");
return;
}
wd.min_size = p_size;
if ((wd.min_size != Size2i()) && !wd.fullscreen) {
2020-07-03 10:06:03 +00:00
Size2i size = wd.min_size / screen_get_max_scale();
2020-03-07 16:02:54 +00:00
[wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
} else {
[wd.window_object setContentMinSize:NSMakeSize(0, 0)];
}
}
2022-07-20 06:28:22 +00:00
Size2i DisplayServerMacOS::window_get_min_size(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
const WindowData &wd = windows[p_window];
return wd.min_size;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_size(const Size2i p_size, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2022-11-30 09:34:40 +00:00
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) {
2022-09-15 07:05:41 +00:00
return;
}
2020-07-03 10:06:03 +00:00
Size2i size = p_size / screen_get_max_scale();
2020-03-07 16:02:54 +00:00
2020-07-14 12:09:36 +00:00
NSPoint top_left;
NSRect old_frame = [wd.window_object frame];
top_left.x = old_frame.origin.x;
top_left.y = NSMaxY(old_frame);
NSRect new_frame = NSMakeRect(0, 0, size.x, size.y);
new_frame = [wd.window_object frameRectForContentRect:new_frame];
new_frame.origin.x = top_left.x;
new_frame.origin.y = top_left.y - new_frame.size.height;
2020-03-07 16:02:54 +00:00
2020-07-14 12:09:36 +00:00
[wd.window_object setFrame:new_frame display:YES];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
_update_window_style(wd);
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
Size2i DisplayServerMacOS::window_get_size(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
const WindowData &wd = windows[p_window];
return wd.size;
}
2022-11-30 08:28:16 +00:00
Size2i DisplayServerMacOS::window_get_size_with_decorations(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Size2i());
const WindowData &wd = windows[p_window];
NSRect frame = [wd.window_object frame];
2020-07-03 10:06:03 +00:00
return Size2i(frame.size.width, frame.size.height) * screen_get_max_scale();
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_mode(WindowMode p_mode, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
WindowMode old_mode = window_get_mode(p_window);
if (old_mode == p_mode) {
2022-01-26 09:01:27 +00:00
return; // Do nothing.
2020-03-07 16:02:54 +00:00
}
switch (old_mode) {
case WINDOW_MODE_WINDOWED: {
2022-01-26 09:01:27 +00:00
// Do nothing.
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_MODE_MINIMIZED: {
[wd.window_object deminiaturize:nil];
} break;
2022-01-28 09:19:53 +00:00
case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
2020-03-07 16:02:54 +00:00
case WINDOW_MODE_FULLSCREEN: {
2022-05-10 18:02:26 +00:00
[(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel];
2022-01-26 09:01:27 +00:00
_set_window_per_pixel_transparency_enabled(true, p_window);
if (wd.resize_disabled) { // Restore resize disabled.
2020-03-07 16:02:54 +00:00
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
if (wd.min_size != Size2i()) {
2020-07-03 10:06:03 +00:00
Size2i size = wd.min_size / screen_get_max_scale();
2020-03-07 16:02:54 +00:00
[wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
}
if (wd.max_size != Size2i()) {
2020-07-03 10:06:03 +00:00
Size2i size = wd.max_size / screen_get_max_scale();
2020-03-07 16:02:54 +00:00
[wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
}
[wd.window_object toggleFullScreen:nil];
2022-11-04 14:23:01 +00:00
if (old_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
}
2020-03-07 16:02:54 +00:00
wd.fullscreen = false;
2022-11-04 14:23:01 +00:00
wd.exclusive_fullscreen = false;
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_MODE_MAXIMIZED: {
2022-11-30 09:34:40 +00:00
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) {
2020-03-07 16:02:54 +00:00
[wd.window_object zoom:nil];
}
} break;
}
switch (p_mode) {
case WINDOW_MODE_WINDOWED: {
2022-01-26 09:01:27 +00:00
// Do nothing.
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_MODE_MINIMIZED: {
[wd.window_object performMiniaturize:nil];
} break;
2022-01-28 09:19:53 +00:00
case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
2020-03-07 16:02:54 +00:00
case WINDOW_MODE_FULLSCREEN: {
2022-01-26 09:01:27 +00:00
_set_window_per_pixel_transparency_enabled(false, p_window);
if (wd.resize_disabled) { // Fullscreen window should be resizable to work.
2020-03-07 16:02:54 +00:00
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
2022-01-26 09:01:27 +00:00
}
2020-03-07 16:02:54 +00:00
[wd.window_object setContentMinSize:NSMakeSize(0, 0)];
[wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[wd.window_object toggleFullScreen:nil];
2022-11-04 14:23:01 +00:00
2020-03-07 16:02:54 +00:00
wd.fullscreen = true;
2022-11-04 14:23:01 +00:00
if (p_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
const NSUInteger presentationOptions = NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
[NSApp setPresentationOptions:presentationOptions];
wd.exclusive_fullscreen = true;
} else {
wd.exclusive_fullscreen = false;
}
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_MODE_MAXIMIZED: {
2022-11-30 09:34:40 +00:00
if (!NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) {
2020-03-07 16:02:54 +00:00
[wd.window_object zoom:nil];
}
} break;
}
}
2022-07-20 06:28:22 +00:00
DisplayServer::WindowMode DisplayServerMacOS::window_get_mode(WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), WINDOW_MODE_WINDOWED);
const WindowData &wd = windows[p_window];
2022-01-26 09:01:27 +00:00
if (wd.fullscreen) { // If fullscreen, it's not in another mode.
2022-11-04 14:23:01 +00:00
if (wd.exclusive_fullscreen) {
return WINDOW_MODE_EXCLUSIVE_FULLSCREEN;
} else {
return WINDOW_MODE_FULLSCREEN;
}
2020-03-07 16:02:54 +00:00
}
2022-11-30 09:34:40 +00:00
if (NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame])) {
2020-03-07 16:02:54 +00:00
return WINDOW_MODE_MAXIMIZED;
}
if ([wd.window_object respondsToSelector:@selector(isMiniaturized)]) {
if ([wd.window_object isMiniaturized]) {
return WINDOW_MODE_MINIMIZED;
}
}
2022-01-26 09:01:27 +00:00
// All other discarded, return windowed.
2020-03-07 16:02:54 +00:00
return WINDOW_MODE_WINDOWED;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::window_is_maximize_allowed(WindowID p_window) const {
2022-01-26 09:01:27 +00:00
return true;
}
2022-08-23 10:40:48 +00:00
bool DisplayServerMacOS::window_maximize_on_title_dbl_click() const {
id value = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleActionOnDoubleClick"];
if ([value isKindOfClass:[NSString class]]) {
return [value isEqualToString:@"Maximize"];
}
return false;
}
bool DisplayServerMacOS::window_minimize_on_title_dbl_click() const {
id value = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleActionOnDoubleClick"];
if ([value isKindOfClass:[NSString class]]) {
return [value isEqualToString:@"Minimize"];
}
return false;
}
2022-09-20 08:12:49 +00:00
void DisplayServerMacOS::window_set_window_buttons_offset(const Vector2i &p_offset, WindowID p_window) {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
2022-10-10 07:32:41 +00:00
float scale = screen_get_max_scale();
wd.wb_offset = p_offset / scale;
wd.wb_offset.x = MAX(wd.wb_offset.x, 12);
wd.wb_offset.y = MAX(wd.wb_offset.y, 12);
if (wd.window_button_view) {
[wd.window_button_view setOffset:NSMakePoint(wd.wb_offset.x, wd.wb_offset.y)];
}
2022-09-20 08:12:49 +00:00
}
2022-10-10 07:32:41 +00:00
Vector3i DisplayServerMacOS::window_get_safe_title_margins(WindowID p_window) const {
2022-08-23 10:40:48 +00:00
_THREAD_SAFE_METHOD_
2022-10-10 07:32:41 +00:00
ERR_FAIL_COND_V(!windows.has(p_window), Vector3i());
2022-08-23 10:40:48 +00:00
const WindowData &wd = windows[p_window];
2022-09-22 09:06:11 +00:00
if (!wd.window_button_view) {
2022-10-10 07:32:41 +00:00
return Vector3i();
2022-08-23 10:40:48 +00:00
}
2022-10-10 07:32:41 +00:00
float scale = screen_get_max_scale();
float max_x = [wd.window_button_view getOffset].x + [wd.window_button_view frame].size.width;
float max_y = [wd.window_button_view getOffset].y + [wd.window_button_view frame].size.height;
2022-09-22 09:06:11 +00:00
if ([wd.window_object windowTitlebarLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft) {
2022-10-10 07:32:41 +00:00
return Vector3i(0, max_x * scale, max_y * scale);
2022-09-22 09:06:11 +00:00
} else {
2022-10-10 07:32:41 +00:00
return Vector3i(max_x * scale, 0, max_y * scale);
2022-09-22 09:06:11 +00:00
}
2022-08-23 10:40:48 +00:00
}
2022-09-21 08:15:19 +00:00
void DisplayServerMacOS::window_set_custom_window_buttons(WindowData &p_wd, bool p_enabled) {
if (p_wd.window_button_view) {
[p_wd.window_button_view removeFromSuperview];
p_wd.window_button_view = nil;
}
if (p_enabled) {
2022-09-22 09:06:11 +00:00
float cb_frame = NSMinX([[p_wd.window_object standardWindowButton:NSWindowCloseButton] frame]);
float mb_frame = NSMinX([[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] frame]);
bool is_rtl = ([p_wd.window_object windowTitlebarLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft);
float window_buttons_spacing = (is_rtl) ? (cb_frame - mb_frame) : (mb_frame - cb_frame);
2022-09-21 08:15:19 +00:00
[p_wd.window_object setTitleVisibility:NSWindowTitleHidden];
[[p_wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:YES];
[[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[p_wd.window_object standardWindowButton:NSWindowCloseButton] setHidden:YES];
p_wd.window_button_view = [[GodotButtonView alloc] initWithFrame:NSZeroRect];
2022-09-22 09:06:11 +00:00
[p_wd.window_button_view initButtons:window_buttons_spacing offset:NSMakePoint(p_wd.wb_offset.x, p_wd.wb_offset.y) rtl:is_rtl];
2022-09-21 08:15:19 +00:00
[p_wd.window_view addSubview:p_wd.window_button_view];
} else {
[p_wd.window_object setTitleVisibility:NSWindowTitleVisible];
[[p_wd.window_object standardWindowButton:NSWindowZoomButton] setHidden:NO];
[[p_wd.window_object standardWindowButton:NSWindowMiniaturizeButton] setHidden:NO];
[[p_wd.window_object standardWindowButton:NSWindowCloseButton] setHidden:NO];
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
switch (p_flag) {
case WINDOW_FLAG_RESIZE_DISABLED: {
wd.resize_disabled = p_enabled;
2022-01-26 09:01:27 +00:00
if (wd.fullscreen) { // Fullscreen window should be resizable, style will be applied on exiting fullscreen.
2020-03-07 16:02:54 +00:00
return;
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
if (p_enabled) {
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
2022-11-30 09:34:40 +00:00
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:NO];
2020-03-07 16:02:54 +00:00
} else {
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskResizable];
2022-11-30 09:34:40 +00:00
[[wd.window_object standardWindowButton:NSWindowZoomButton] setEnabled:YES];
2020-03-07 16:02:54 +00:00
}
} break;
2022-08-23 10:40:48 +00:00
case WINDOW_FLAG_EXTEND_TO_TITLE: {
NSRect rect = [wd.window_object frame];
if (p_enabled) {
[wd.window_object setTitlebarAppearsTransparent:YES];
[wd.window_object setStyleMask:[wd.window_object styleMask] | NSWindowStyleMaskFullSizeContentView];
2022-09-20 08:12:49 +00:00
2022-09-21 08:15:19 +00:00
if (!wd.fullscreen) {
window_set_custom_window_buttons(wd, true);
}
2022-08-23 10:40:48 +00:00
} else {
[wd.window_object setTitlebarAppearsTransparent:NO];
[wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskFullSizeContentView];
2022-09-20 08:12:49 +00:00
2022-09-21 08:15:19 +00:00
if (!wd.fullscreen) {
window_set_custom_window_buttons(wd, false);
}
2022-08-23 10:40:48 +00:00
}
[wd.window_object setFrame:rect display:YES];
2022-09-22 09:06:11 +00:00
send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
2022-08-23 10:40:48 +00:00
} break;
2020-03-07 16:02:54 +00:00
case WINDOW_FLAG_BORDERLESS: {
2022-01-26 09:01:27 +00:00
// OrderOut prevents a lose focus bug with the window.
2020-07-26 12:46:07 +00:00
if ([wd.window_object isVisible]) {
[wd.window_object orderOut:nil];
}
2020-03-07 16:02:54 +00:00
wd.borderless = p_enabled;
if (p_enabled) {
[wd.window_object setStyleMask:NSWindowStyleMaskBorderless];
} else {
2022-01-26 09:01:27 +00:00
_set_window_per_pixel_transparency_enabled(false, p_window);
2020-03-07 16:02:54 +00:00
[wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
2022-01-26 09:01:27 +00:00
// Force update of the window styles.
2020-03-07 16:02:54 +00:00
NSRect frameRect = [wd.window_object frame];
[wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
[wd.window_object setFrame:frameRect display:NO];
}
2022-01-26 09:01:27 +00:00
_update_window_style(wd);
2020-07-26 12:46:07 +00:00
if ([wd.window_object isVisible]) {
2022-09-15 07:05:41 +00:00
if ([wd.window_object isMiniaturized]) {
return;
2023-06-09 05:03:13 +00:00
} else if (wd.no_focus) {
2020-07-26 12:46:07 +00:00
[wd.window_object orderFront:nil];
} else {
[wd.window_object makeKeyAndOrderFront:nil];
}
}
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_FLAG_ALWAYS_ON_TOP: {
wd.on_top = p_enabled;
2021-05-24 07:53:01 +00:00
if (wd.fullscreen) {
return;
}
2020-03-07 16:02:54 +00:00
if (p_enabled) {
2022-05-10 18:02:26 +00:00
[(NSWindow *)wd.window_object setLevel:NSFloatingWindowLevel];
2020-03-07 16:02:54 +00:00
} else {
2022-05-10 18:02:26 +00:00
[(NSWindow *)wd.window_object setLevel:NSNormalWindowLevel];
2020-03-07 16:02:54 +00:00
}
} break;
case WINDOW_FLAG_TRANSPARENT: {
if (p_enabled) {
2022-01-26 09:01:27 +00:00
[wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; // Force borderless.
2020-03-07 16:02:54 +00:00
} else if (!wd.borderless) {
[wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
}
_set_window_per_pixel_transparency_enabled(p_enabled, p_window);
2020-07-03 10:06:03 +00:00
} break;
case WINDOW_FLAG_NO_FOCUS: {
wd.no_focus = p_enabled;
2020-03-07 16:02:54 +00:00
} break;
2023-01-15 10:05:25 +00:00
case WINDOW_FLAG_MOUSE_PASSTHROUGH: {
wd.mpass = p_enabled;
} break;
2022-02-24 09:21:23 +00:00
case WINDOW_FLAG_POPUP: {
ERR_FAIL_COND_MSG(p_window == MAIN_WINDOW_ID, "Main window can't be popup.");
2022-03-08 09:46:44 +00:00
ERR_FAIL_COND_MSG([wd.window_object isVisible] && (wd.is_popup != p_enabled), "Popup flag can't changed while window is opened.");
2022-02-24 09:21:23 +00:00
wd.is_popup = p_enabled;
} break;
2020-03-07 16:02:54 +00:00
default: {
}
}
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), false);
const WindowData &wd = windows[p_window];
switch (p_flag) {
case WINDOW_FLAG_RESIZE_DISABLED: {
return wd.resize_disabled;
} break;
2022-08-23 10:40:48 +00:00
case WINDOW_FLAG_EXTEND_TO_TITLE: {
return [wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView;
} break;
2020-03-07 16:02:54 +00:00
case WINDOW_FLAG_BORDERLESS: {
return [wd.window_object styleMask] == NSWindowStyleMaskBorderless;
} break;
case WINDOW_FLAG_ALWAYS_ON_TOP: {
2021-05-24 07:53:01 +00:00
if (wd.fullscreen) {
return wd.on_top;
} else {
2022-05-10 18:02:26 +00:00
return [(NSWindow *)wd.window_object level] == NSFloatingWindowLevel;
2021-05-24 07:53:01 +00:00
}
2020-03-07 16:02:54 +00:00
} break;
case WINDOW_FLAG_TRANSPARENT: {
return wd.layered_window;
} break;
2020-07-03 10:06:03 +00:00
case WINDOW_FLAG_NO_FOCUS: {
return wd.no_focus;
} break;
2023-01-15 10:05:25 +00:00
case WINDOW_FLAG_MOUSE_PASSTHROUGH: {
return wd.mpass;
} break;
2022-02-24 09:21:23 +00:00
case WINDOW_FLAG_POPUP: {
return wd.is_popup;
} break;
2020-03-07 16:02:54 +00:00
default: {
}
}
return false;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_request_attention(WindowID p_window) {
2020-03-07 16:02:54 +00:00
// It's app global, ignore window id.
[NSApp requestUserAttention:NSCriticalRequest];
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_move_to_foreground(WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
const WindowData &wd = windows[p_window];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
2022-02-24 09:21:23 +00:00
if (wd.no_focus || wd.is_popup) {
2020-07-26 12:46:07 +00:00
[wd.window_object orderFront:nil];
} else {
[wd.window_object makeKeyAndOrderFront:nil];
}
2020-03-07 16:02:54 +00:00
}
2023-06-15 07:53:31 +00:00
bool DisplayServerMacOS::window_is_focused(WindowID p_window) const {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), false);
const WindowData &wd = windows[p_window];
return wd.focused;
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::window_can_draw(WindowID p_window) const {
2023-07-17 07:29:49 +00:00
return (window_get_mode(p_window) != WINDOW_MODE_MINIMIZED) && [windows[p_window].window_object isOnActiveSpace];
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::can_any_window_draw() const {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-05-13 13:04:37 +00:00
for (const KeyValue<WindowID, WindowData> &E : windows) {
2023-07-17 07:29:49 +00:00
if ((window_get_mode(E.key) != WINDOW_MODE_MINIMIZED) && [E.value.window_object isOnActiveSpace]) {
2020-03-07 16:02:54 +00:00
return true;
}
}
return false;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_ime_active(const bool p_active, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.im_active = p_active;
2020-07-03 10:06:03 +00:00
if (!p_active) {
2020-03-07 16:02:54 +00:00
[wd.window_view cancelComposition];
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_ime_position(const Point2i &p_pos, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.im_position = p_pos;
}
2022-07-20 06:28:22 +00:00
DisplayServer::WindowID DisplayServerMacOS::get_window_at_screen_position(const Point2i &p_position) const {
2022-01-26 09:01:27 +00:00
Point2i position = p_position;
position.y *= -1;
position += _get_screens_origin();
position /= screen_get_max_scale();
NSInteger wnum = [NSWindow windowNumberAtPoint:NSMakePoint(position.x, position.y) belowWindowWithWindowNumber:0 /*topmost*/];
2022-05-13 13:04:37 +00:00
for (const KeyValue<WindowID, WindowData> &E : windows) {
if ([E.value.window_object windowNumber] == wnum) {
return E.key;
2022-01-26 09:01:27 +00:00
}
}
return INVALID_WINDOW_ID;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
int64_t DisplayServerMacOS::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
2022-01-26 09:01:27 +00:00
ERR_FAIL_COND_V(!windows.has(p_window), 0);
switch (p_handle_type) {
case DISPLAY_HANDLE: {
return 0; // Not supported.
}
case WINDOW_HANDLE: {
return (int64_t)windows[p_window].window_object;
}
case WINDOW_VIEW: {
return (int64_t)windows[p_window].window_view;
}
2022-10-23 01:29:15 +00:00
#ifdef GLES3_ENABLED
case OPENGL_CONTEXT: {
2022-11-13 13:06:26 +00:00
if (gl_manager) {
return (int64_t)gl_manager->get_context(p_window);
}
return 0;
2022-10-23 01:29:15 +00:00
}
#endif
2022-01-26 09:01:27 +00:00
default: {
return 0;
}
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-01-26 09:01:27 +00:00
ERR_FAIL_COND(!windows.has(p_window));
windows[p_window].instance_id = p_instance;
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
ObjectID DisplayServerMacOS::window_get_attached_instance_id(WindowID p_window) const {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), ObjectID());
return windows[p_window].instance_id;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::gl_window_make_current(DisplayServer::WindowID p_window_id) {
2022-01-26 09:01:27 +00:00
#if defined(GLES3_ENABLED)
2023-01-20 22:20:30 +00:00
if (gl_manager) {
gl_manager->window_make_current(p_window_id);
}
2022-01-26 09:01:27 +00:00
#endif
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
#if defined(GLES3_ENABLED)
if (gl_manager) {
2022-11-15 22:14:21 +00:00
gl_manager->set_use_vsync(p_vsync_mode != DisplayServer::VSYNC_DISABLED);
2022-01-26 09:01:27 +00:00
}
#endif
#if defined(VULKAN_ENABLED)
if (context_vulkan) {
context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
2020-07-03 10:06:03 +00:00
}
2022-01-26 09:01:27 +00:00
#endif
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
DisplayServer::VSyncMode DisplayServerMacOS::window_get_vsync_mode(WindowID p_window) const {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
#if defined(GLES3_ENABLED)
if (gl_manager) {
return (gl_manager->is_using_vsync() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED);
}
#endif
#if defined(VULKAN_ENABLED)
if (context_vulkan) {
return context_vulkan->get_vsync_mode(p_window);
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
#endif
return DisplayServer::VSYNC_ENABLED;
}
2022-07-20 06:28:22 +00:00
Point2i DisplayServerMacOS::ime_get_selection() const {
2022-01-26 09:01:27 +00:00
return im_selection;
}
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::ime_get_text() const {
2022-01-26 09:01:27 +00:00
return im_text;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::cursor_update_shape() {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
if (cursors[cursor_shape] != nullptr) {
[cursors[cursor_shape] set];
2020-03-07 16:02:54 +00:00
} else {
2022-01-26 09:01:27 +00:00
switch (cursor_shape) {
2020-05-10 11:00:47 +00:00
case CURSOR_ARROW:
[[NSCursor arrowCursor] set];
break;
case CURSOR_IBEAM:
[[NSCursor IBeamCursor] set];
break;
case CURSOR_POINTING_HAND:
[[NSCursor pointingHandCursor] set];
break;
case CURSOR_CROSS:
[[NSCursor crosshairCursor] set];
break;
case CURSOR_WAIT:
[[NSCursor arrowCursor] set];
break;
case CURSOR_BUSY:
[[NSCursor arrowCursor] set];
break;
case CURSOR_DRAG:
[[NSCursor closedHandCursor] set];
break;
case CURSOR_CAN_DROP:
[[NSCursor openHandCursor] set];
break;
case CURSOR_FORBIDDEN:
[[NSCursor operationNotAllowedCursor] set];
break;
case CURSOR_VSIZE:
2022-01-26 09:01:27 +00:00
[_cursor_from_selector(@selector(_windowResizeNorthSouthCursor), @selector(resizeUpDownCursor)) set];
2020-05-10 11:00:47 +00:00
break;
case CURSOR_HSIZE:
2022-01-26 09:01:27 +00:00
[_cursor_from_selector(@selector(_windowResizeEastWestCursor), @selector(resizeLeftRightCursor)) set];
2020-05-10 11:00:47 +00:00
break;
case CURSOR_BDIAGSIZE:
2022-01-26 09:01:27 +00:00
[_cursor_from_selector(@selector(_windowResizeNorthEastSouthWestCursor)) set];
2020-05-10 11:00:47 +00:00
break;
case CURSOR_FDIAGSIZE:
2022-01-26 09:01:27 +00:00
[_cursor_from_selector(@selector(_windowResizeNorthWestSouthEastCursor)) set];
2020-05-10 11:00:47 +00:00
break;
case CURSOR_MOVE:
[[NSCursor arrowCursor] set];
break;
case CURSOR_VSPLIT:
[[NSCursor resizeUpDownCursor] set];
break;
case CURSOR_HSPLIT:
[[NSCursor resizeLeftRightCursor] set];
break;
case CURSOR_HELP:
2022-01-26 09:01:27 +00:00
[_cursor_from_selector(@selector(_helpCursor)) set];
2020-05-10 11:00:47 +00:00
break;
2020-03-07 16:02:54 +00:00
default: {
}
}
}
2022-01-26 09:01:27 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::cursor_set_shape(CursorShape p_shape) {
2022-01-26 09:01:27 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
if (cursor_shape == p_shape) {
return;
}
2020-03-07 16:02:54 +00:00
cursor_shape = p_shape;
2022-01-26 09:01:27 +00:00
if (mouse_mode != MOUSE_MODE_VISIBLE && mouse_mode != MOUSE_MODE_CONFINED) {
return;
}
cursor_update_shape();
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
DisplayServerMacOS::CursorShape DisplayServerMacOS::cursor_get_shape() const {
2020-03-07 16:02:54 +00:00
return cursor_shape;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-02-17 13:17:37 +00:00
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
2020-03-07 16:02:54 +00:00
if (p_cursor.is_valid()) {
2022-05-13 13:04:37 +00:00
HashMap<CursorShape, Vector<Variant>>::Iterator cursor_c = cursors_cache.find(p_shape);
2020-03-07 16:02:54 +00:00
if (cursor_c) {
2022-05-13 13:04:37 +00:00
if (cursor_c->value[0] == p_cursor && cursor_c->value[1] == p_hotspot) {
2020-03-07 16:02:54 +00:00
cursor_set_shape(p_shape);
return;
}
cursors_cache.erase(p_shape);
}
Ref<Texture2D> texture = p_cursor;
2023-04-08 14:54:02 +00:00
ERR_FAIL_COND(!texture.is_valid());
2020-03-07 16:02:54 +00:00
Ref<AtlasTexture> atlas_texture = p_cursor;
Size2 texture_size;
Rect2 atlas_rect;
2023-04-08 14:54:02 +00:00
if (atlas_texture.is_valid()) {
2020-03-07 16:02:54 +00:00
texture = atlas_texture->get_atlas();
atlas_rect.size.width = texture->get_width();
atlas_rect.size.height = texture->get_height();
atlas_rect.position.x = atlas_texture->get_region().position.x;
atlas_rect.position.y = atlas_texture->get_region().position.y;
texture_size.width = atlas_texture->get_region().size.x;
texture_size.height = atlas_texture->get_region().size.y;
2023-04-08 14:54:02 +00:00
} else {
2020-03-07 16:02:54 +00:00
texture_size.width = texture->get_width();
texture_size.height = texture->get_height();
}
ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
2023-04-08 14:54:02 +00:00
Ref<Image> image = texture->get_image();
2020-03-07 16:02:54 +00:00
ERR_FAIL_COND(!image.is_valid());
2023-04-21 00:15:54 +00:00
if (image->is_compressed()) {
image = image->duplicate(true);
Error err = image->decompress();
ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
}
2020-03-07 16:02:54 +00:00
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
2021-10-12 07:30:55 +00:00
initWithBitmapDataPlanes:nullptr
2020-03-07 16:02:54 +00:00
pixelsWide:int(texture_size.width)
pixelsHigh:int(texture_size.height)
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:int(texture_size.width) * 4
bitsPerPixel:32];
ERR_FAIL_COND(imgrep == nil);
uint8_t *pixels = [imgrep bitmapData];
int len = int(texture_size.width * texture_size.height);
for (int i = 0; i < len; i++) {
int row_index = floor(i / texture_size.width) + atlas_rect.position.y;
int column_index = (i % int(texture_size.width)) + atlas_rect.position.x;
if (atlas_texture.is_valid()) {
column_index = MIN(column_index, atlas_rect.size.width - 1);
row_index = MIN(row_index, atlas_rect.size.height - 1);
}
uint32_t color = image->get_pixel(column_index, row_index).to_argb32();
uint8_t alpha = (color >> 24) & 0xFF;
pixels[i * 4 + 0] = ((color >> 16) & 0xFF) * alpha / 255;
pixels[i * 4 + 1] = ((color >> 8) & 0xFF) * alpha / 255;
pixels[i * 4 + 2] = ((color)&0xFF) * alpha / 255;
pixels[i * 4 + 3] = alpha;
}
NSImage *nsimage = [[NSImage alloc] initWithSize:NSMakeSize(texture_size.width, texture_size.height)];
[nsimage addRepresentation:imgrep];
NSCursor *cursor = [[NSCursor alloc] initWithImage:nsimage hotSpot:NSMakePoint(p_hotspot.x, p_hotspot.y)];
cursors[p_shape] = cursor;
Vector<Variant> params;
params.push_back(p_cursor);
params.push_back(p_hotspot);
cursors_cache.insert(p_shape, params);
if (p_shape == cursor_shape) {
if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) {
[cursor set];
}
}
} else {
2022-01-26 09:01:27 +00:00
// Reset to default system cursor.
2021-04-29 09:47:24 +00:00
if (cursors[p_shape] != nullptr) {
cursors[p_shape] = nullptr;
2020-03-07 16:02:54 +00:00
}
cursors_cache.erase(p_shape);
2023-03-06 21:00:34 +00:00
cursor_update_shape();
2020-03-07 16:02:54 +00:00
}
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::get_swap_cancel_ok() {
2022-01-26 09:01:27 +00:00
return false;
2020-06-11 09:27:07 +00:00
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::keyboard_get_layout_count() const {
2020-06-11 09:27:07 +00:00
if (keyboard_layout_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
2020-03-07 16:02:54 +00:00
}
2020-06-11 09:27:07 +00:00
return kbd_layouts.size();
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::keyboard_set_current_layout(int p_index) {
2020-06-11 09:27:07 +00:00
if (keyboard_layout_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
2020-06-11 09:27:07 +00:00
}
ERR_FAIL_INDEX(p_index, kbd_layouts.size());
NSString *cur_name = [NSString stringWithUTF8String:kbd_layouts[p_index].name.utf8().get_data()];
NSDictionary *filter_kbd = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardLayout };
2022-01-25 08:07:01 +00:00
NSArray *list_kbd = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_kbd, false);
2020-06-11 09:27:07 +00:00
for (NSUInteger i = 0; i < [list_kbd count]; i++) {
2022-01-25 08:07:01 +00:00
NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i], kTISPropertyLocalizedName);
2020-06-11 09:27:07 +00:00
if ([name isEqualToString:cur_name]) {
2022-01-25 08:07:01 +00:00
TISSelectInputSource((__bridge TISInputSourceRef)[list_kbd objectAtIndex:i]);
2020-06-11 09:27:07 +00:00
break;
}
}
NSDictionary *filter_ime = @{ (NSString *)kTISPropertyInputSourceType : (NSString *)kTISTypeKeyboardInputMode };
2022-01-25 08:07:01 +00:00
NSArray *list_ime = (__bridge NSArray *)TISCreateInputSourceList((__bridge CFDictionaryRef)filter_ime, false);
2020-06-11 09:27:07 +00:00
for (NSUInteger i = 0; i < [list_ime count]; i++) {
2022-01-25 08:07:01 +00:00
NSString *name = (__bridge NSString *)TISGetInputSourceProperty((__bridge TISInputSourceRef)[list_ime objectAtIndex:i], kTISPropertyLocalizedName);
2020-06-11 09:27:07 +00:00
if ([name isEqualToString:cur_name]) {
2022-01-25 08:07:01 +00:00
TISSelectInputSource((__bridge TISInputSourceRef)[list_ime objectAtIndex:i]);
2020-06-11 09:27:07 +00:00
break;
}
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
int DisplayServerMacOS::keyboard_get_current_layout() const {
2020-06-11 09:27:07 +00:00
if (keyboard_layout_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
2020-06-11 09:27:07 +00:00
}
2020-03-07 16:02:54 +00:00
2020-06-11 09:27:07 +00:00
return current_layout;
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::keyboard_get_layout_language(int p_index) const {
2020-03-07 16:02:54 +00:00
if (keyboard_layout_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
2020-06-11 09:27:07 +00:00
}
2020-03-07 16:02:54 +00:00
2020-06-11 09:27:07 +00:00
ERR_FAIL_INDEX_V(p_index, kbd_layouts.size(), "");
return kbd_layouts[p_index].code;
}
2020-03-07 16:02:54 +00:00
2022-07-20 06:28:22 +00:00
String DisplayServerMacOS::keyboard_get_layout_name(int p_index) const {
2020-06-11 09:27:07 +00:00
if (keyboard_layout_dirty) {
2022-07-20 06:28:22 +00:00
const_cast<DisplayServerMacOS *>(this)->_update_keyboard_layouts();
2020-03-07 16:02:54 +00:00
}
2020-06-11 09:27:07 +00:00
ERR_FAIL_INDEX_V(p_index, kbd_layouts.size(), "");
return kbd_layouts[p_index].name;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
Key DisplayServerMacOS::keyboard_get_keycode_from_physical(Key p_keycode) const {
2022-12-10 23:21:22 +00:00
if (p_keycode == Key::PAUSE || p_keycode == Key::NONE) {
2021-09-17 01:07:59 +00:00
return p_keycode;
}
2021-08-13 21:31:57 +00:00
Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK;
Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK;
2022-12-10 23:21:22 +00:00
unsigned int macos_keycode = KeyMappingMacOS::unmap_key(keycode_no_mod);
return (Key)(KeyMappingMacOS::remap_key(macos_keycode, 0, false) | modifiers);
2020-03-07 16:02:54 +00:00
}
2023-06-08 06:26:48 +00:00
Key DisplayServerMacOS::keyboard_get_label_from_physical(Key p_keycode) const {
if (p_keycode == Key::PAUSE || p_keycode == Key::NONE) {
return p_keycode;
}
Key modifiers = p_keycode & KeyModifierMask::MODIFIER_MASK;
Key keycode_no_mod = p_keycode & KeyModifierMask::CODE_MASK;
unsigned int macos_keycode = KeyMappingMacOS::unmap_key(keycode_no_mod);
return (Key)(KeyMappingMacOS::remap_key(macos_keycode, 0, true) | modifiers);
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::process_events() {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
while (true) {
NSEvent *event = [NSApp
nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
2020-07-03 10:06:03 +00:00
if (event == nil) {
2020-03-07 16:02:54 +00:00
break;
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
[NSApp sendEvent:event];
}
2022-09-24 20:57:00 +00:00
// Process "menu_callback"s.
2023-03-23 11:26:11 +00:00
while (List<MenuCall>::Element *call_p = deferred_menu_calls.front()) {
MenuCall call = call_p->get();
deferred_menu_calls.pop_front(); // Remove before call to avoid infinite loop in case callback is using `process_events` (e.g. EditorProgress).
Variant tag = call.tag;
2022-09-24 20:57:00 +00:00
Variant *tagp = &tag;
Variant ret;
Callable::CallError ce;
2023-03-23 11:26:11 +00:00
call.callback.callp((const Variant **)&tagp, 1, ret, ce);
2022-09-24 20:57:00 +00:00
}
2020-03-07 16:02:54 +00:00
if (!drop_events) {
_process_key_events();
2021-08-12 22:38:41 +00:00
Input::get_singleton()->flush_buffered_events();
2020-03-07 16:02:54 +00:00
}
2022-05-13 13:04:37 +00:00
for (KeyValue<WindowID, WindowData> &E : windows) {
WindowData &wd = E.value;
2023-01-15 10:05:25 +00:00
if (wd.mpass) {
if (![wd.window_object ignoresMouseEvents]) {
[wd.window_object setIgnoresMouseEvents:YES];
}
} else if (wd.mpath.size() > 0) {
2022-01-26 09:01:27 +00:00
update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
if (Geometry2D::is_point_in_polygon(wd.mouse_pos, wd.mpath)) {
2020-06-29 09:31:36 +00:00
if ([wd.window_object ignoresMouseEvents]) {
[wd.window_object setIgnoresMouseEvents:NO];
}
} else {
if (![wd.window_object ignoresMouseEvents]) {
[wd.window_object setIgnoresMouseEvents:YES];
}
}
} else {
if ([wd.window_object ignoresMouseEvents]) {
[wd.window_object setIgnoresMouseEvents:NO];
}
}
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::force_process_and_drop_events() {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
drop_events = true;
process_events();
drop_events = false;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::release_rendering_thread() {
2022-01-26 09:01:27 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::make_rendering_thread() {
2022-01-26 09:01:27 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::swap_buffers() {
2022-01-26 09:01:27 +00:00
#if defined(GLES3_ENABLED)
2022-07-06 06:56:47 +00:00
if (gl_manager) {
gl_manager->swap_buffers();
}
2022-01-26 09:01:27 +00:00
#endif
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::set_native_icon(const String &p_filename) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2022-03-23 09:08:58 +00:00
Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::READ);
ERR_FAIL_COND(f.is_null());
2020-03-07 16:02:54 +00:00
Vector<uint8_t> data;
2021-05-25 06:58:49 +00:00
uint64_t len = f->get_length();
2023-07-04 06:09:40 +00:00
ERR_FAIL_COND_MSG(len < 8, "Error reading icon data."); // "icns" + 32-bit length
2020-03-07 16:02:54 +00:00
data.resize(len);
f->get_buffer((uint8_t *)&data.write[0], len);
2023-07-04 06:09:40 +00:00
@try {
NSData *icon_data = [[NSData alloc] initWithBytes:&data.write[0] length:len];
ERR_FAIL_COND_MSG(!icon_data, "Error reading icon data.");
2020-03-07 16:02:54 +00:00
2023-07-04 06:09:40 +00:00
NSImage *icon = [[NSImage alloc] initWithData:icon_data];
ERR_FAIL_COND_MSG(!icon, "Error loading icon.");
2020-03-07 16:02:54 +00:00
2023-07-04 06:09:40 +00:00
[NSApp setApplicationIconImage:icon];
} @catch (NSException *exception) {
ERR_FAIL_MSG("NSException: " + String::utf8([exception reason].UTF8String));
}
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::set_icon(const Ref<Image> &p_icon) {
2020-03-07 16:02:54 +00:00
_THREAD_SAFE_METHOD_
2023-06-19 13:18:39 +00:00
if (p_icon.is_valid()) {
ERR_FAIL_COND(p_icon->get_width() <= 0 || p_icon->get_height() <= 0);
2020-03-07 16:02:54 +00:00
2023-06-19 13:18:39 +00:00
Ref<Image> img = p_icon->duplicate();
img->convert(Image::FORMAT_RGBA8);
2020-03-07 16:02:54 +00:00
2023-06-19 13:18:39 +00:00
NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nullptr
pixelsWide:img->get_width()
pixelsHigh:img->get_height()
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:img->get_width() * 4
bitsPerPixel:32];
ERR_FAIL_COND(imgrep == nil);
uint8_t *pixels = [imgrep bitmapData];
2020-03-07 16:02:54 +00:00
2023-06-19 13:18:39 +00:00
int len = img->get_width() * img->get_height();
const uint8_t *r = img->get_data().ptr();
2020-03-07 16:02:54 +00:00
2023-06-19 13:18:39 +00:00
/* Premultiply the alpha channel */
for (int i = 0; i < len; i++) {
uint8_t alpha = r[i * 4 + 3];
pixels[i * 4 + 0] = (uint8_t)(((uint16_t)r[i * 4 + 0] * alpha) / 255);
pixels[i * 4 + 1] = (uint8_t)(((uint16_t)r[i * 4 + 1] * alpha) / 255);
pixels[i * 4 + 2] = (uint8_t)(((uint16_t)r[i * 4 + 2] * alpha) / 255);
pixels[i * 4 + 3] = alpha;
}
NSImage *nsimg = [[NSImage alloc] initWithSize:NSMakeSize(img->get_width(), img->get_height())];
ERR_FAIL_COND(nsimg == nil);
[nsimg addRepresentation:imgrep];
[NSApp setApplicationIconImage:nsimg];
} else {
[NSApp setApplicationIconImage:nil];
}
2020-03-07 16:02:54 +00:00
}
2023-01-04 22:00:02 +00:00
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, Error &r_error) {
DisplayServer *ds = memnew(DisplayServerMacOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, r_error));
2020-07-13 16:24:04 +00:00
if (r_error != OK) {
2022-10-17 17:47:32 +00:00
if (p_rendering_driver == "vulkan") {
String executable_command;
if (OS::get_singleton()->get_bundle_resource_dir() == OS::get_singleton()->get_executable_path().get_base_dir()) {
2023-03-08 21:27:42 +00:00
executable_command = vformat("\"%s\" --rendering-driver opengl3", OS::get_singleton()->get_executable_path());
2022-10-17 17:47:32 +00:00
} else {
2023-03-08 21:27:42 +00:00
executable_command = vformat("open \"%s\" --args --rendering-driver opengl3", OS::get_singleton()->get_bundle_resource_dir().path_join("../..").simplify_path());
2022-10-17 17:47:32 +00:00
}
2023-01-17 14:26:10 +00:00
OS::get_singleton()->alert(
vformat("Your video card drivers seem not to support the required Vulkan version.\n\n"
"If possible, consider updating your macOS version or using the OpenGL 3 driver.\n\n"
"You can enable the OpenGL 3 driver by starting the engine from the\n"
2023-03-08 21:27:42 +00:00
"command line with the command:\n\n %s",
2023-01-17 14:26:10 +00:00
executable_command),
"Unable to initialize Vulkan video driver");
2022-10-17 17:47:32 +00:00
} else {
2023-01-17 14:26:10 +00:00
OS::get_singleton()->alert(
"Your video card drivers seem not to support the required OpenGL 3.3 version.\n\n"
"If possible, consider updating your macOS version.",
"Unable to initialize OpenGL video driver");
2022-10-17 17:47:32 +00:00
}
2020-07-13 16:24:04 +00:00
}
return ds;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
Vector<String> DisplayServerMacOS::get_rendering_drivers_func() {
2022-01-26 09:01:27 +00:00
Vector<String> drivers;
2020-03-07 16:02:54 +00:00
#if defined(VULKAN_ENABLED)
2022-01-26 09:01:27 +00:00
drivers.push_back("vulkan");
2020-03-07 16:02:54 +00:00
#endif
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2022-01-26 09:01:27 +00:00
drivers.push_back("opengl3");
2020-03-07 16:02:54 +00:00
#endif
2022-01-26 09:01:27 +00:00
return drivers;
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::register_macos_driver() {
register_create_function("macos", create_func, get_rendering_drivers_func);
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
DisplayServer::WindowID DisplayServerMacOS::window_get_active_popup() const {
2022-02-24 09:21:23 +00:00
const List<WindowID>::Element *E = popup_list.back();
if (E) {
return E->get();
} else {
return INVALID_WINDOW_ID;
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) {
2022-02-24 09:21:23 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];
wd.parent_safe_rect = p_rect;
}
2022-07-20 06:28:22 +00:00
Rect2i DisplayServerMacOS::window_get_popup_safe_rect(WindowID p_window) const {
2022-02-24 09:21:23 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_COND_V(!windows.has(p_window), Rect2i());
const WindowData &wd = windows[p_window];
return wd.parent_safe_rect;
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::popup_open(WindowID p_window) {
2022-04-05 09:34:27 +00:00
_THREAD_SAFE_METHOD_
2021-04-15 18:22:58 +00:00
bool has_popup_ancestor = false;
WindowID transient_root = p_window;
while (true) {
WindowID parent = windows[transient_root].transient_parent;
if (parent == INVALID_WINDOW_ID) {
break;
} else {
transient_root = parent;
if (windows[parent].is_popup) {
has_popup_ancestor = true;
break;
}
}
}
2022-02-24 09:21:23 +00:00
WindowData &wd = windows[p_window];
2021-04-15 18:22:58 +00:00
if (wd.is_popup || has_popup_ancestor) {
2022-02-24 09:21:23 +00:00
bool was_empty = popup_list.is_empty();
2022-04-05 09:34:27 +00:00
// Find current popup parent, or root popup if new window is not transient.
List<WindowID>::Element *C = nullptr;
2022-02-24 09:21:23 +00:00
List<WindowID>::Element *E = popup_list.back();
while (E) {
if (wd.transient_parent != E->get() || wd.transient_parent == INVALID_WINDOW_ID) {
2022-04-05 09:34:27 +00:00
C = E;
E = E->prev();
2022-02-24 09:21:23 +00:00
} else {
break;
}
}
2022-04-05 09:34:27 +00:00
if (C) {
2022-07-20 06:28:22 +00:00
send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
2022-04-05 09:34:27 +00:00
}
2022-02-24 09:21:23 +00:00
if (was_empty && popup_list.is_empty()) {
// Inform OS that popup was opened, to close other native popups.
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.beginMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
}
time_since_popup = OS::get_singleton()->get_ticks_msec();
popup_list.push_back(p_window);
}
}
2022-07-20 06:28:22 +00:00
void DisplayServerMacOS::popup_close(WindowID p_window) {
2022-04-05 09:34:27 +00:00
_THREAD_SAFE_METHOD_
2022-02-24 09:21:23 +00:00
bool was_empty = popup_list.is_empty();
List<WindowID>::Element *E = popup_list.find(p_window);
while (E) {
List<WindowID>::Element *F = E->next();
2022-04-05 09:34:27 +00:00
WindowID win_id = E->get();
2022-02-24 09:21:23 +00:00
popup_list.erase(E);
2022-04-05 09:34:27 +00:00
2022-07-20 06:28:22 +00:00
send_window_event(windows[win_id], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
2022-02-24 09:21:23 +00:00
E = F;
}
if (!was_empty && popup_list.is_empty()) {
// Inform OS that all popups are closed.
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
}
}
2022-07-20 06:28:22 +00:00
bool DisplayServerMacOS::mouse_process_popups(bool p_close) {
2022-02-24 09:21:23 +00:00
_THREAD_SAFE_METHOD_
bool was_empty = popup_list.is_empty();
2022-05-30 08:06:08 +00:00
bool closed = false;
2022-02-24 09:21:23 +00:00
if (p_close) {
// Close all popups.
List<WindowID>::Element *E = popup_list.front();
2022-04-05 09:34:27 +00:00
if (E) {
2022-07-20 06:28:22 +00:00
send_window_event(windows[E->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
2022-05-30 08:06:08 +00:00
closed = true;
2022-02-24 09:21:23 +00:00
}
if (!was_empty) {
// Inform OS that all popups are closed.
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
}
} else {
uint64_t delta = OS::get_singleton()->get_ticks_msec() - time_since_popup;
if (delta < 250) {
2022-05-30 08:06:08 +00:00
return false;
2022-02-24 09:21:23 +00:00
}
Point2i pos = mouse_get_position();
2022-04-05 09:34:27 +00:00
List<WindowID>::Element *C = nullptr;
2022-02-24 09:21:23 +00:00
List<WindowID>::Element *E = popup_list.back();
2022-04-05 09:34:27 +00:00
// Find top popup to close.
2022-02-24 09:21:23 +00:00
while (E) {
// Popup window area.
Rect2i win_rect = Rect2i(window_get_position(E->get()), window_get_size(E->get()));
// Area of the parent window, which responsible for opening sub-menu.
Rect2i safe_rect = window_get_popup_safe_rect(E->get());
if (win_rect.has_point(pos)) {
break;
} else if (safe_rect != Rect2i() && safe_rect.has_point(pos)) {
break;
} else {
2022-04-05 09:34:27 +00:00
C = E;
E = E->prev();
2022-02-24 09:21:23 +00:00
}
}
2022-04-05 09:34:27 +00:00
if (C) {
2022-07-20 06:28:22 +00:00
send_window_event(windows[C->get()], DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
2022-05-30 08:06:08 +00:00
closed = true;
2022-04-05 09:34:27 +00:00
}
2022-02-24 09:21:23 +00:00
if (!was_empty && popup_list.is_empty()) {
// Inform OS that all popups are closed.
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:@"org.godotengine.godot.popup_window"];
}
}
2022-05-30 08:06:08 +00:00
return closed;
2022-02-24 09:21:23 +00:00
}
2023-01-04 22:00:02 +00:00
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, Error &r_error) {
2022-12-10 23:21:22 +00:00
KeyMappingMacOS::initialize();
2020-04-28 13:19:37 +00:00
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
2020-03-07 16:02:54 +00:00
r_error = OK;
memset(cursors, 0, sizeof(cursors));
2022-01-26 09:01:27 +00:00
event_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
ERR_FAIL_COND(!event_source);
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
CGEventSourceSetLocalEventsSuppressionInterval(event_source, 0.0);
2022-01-15 18:47:34 +00:00
int screen_count = get_screen_count();
for (int i = 0; i < screen_count; i++) {
display_max_scale = fmax(display_max_scale, screen_get_scale(i));
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Register to be notified on keyboard layout changes.
2020-03-07 16:02:54 +00:00
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
2022-01-26 09:01:27 +00:00
nullptr, _keyboard_layout_changed,
2021-04-29 09:47:24 +00:00
kTISNotifySelectedKeyboardInputSourceChanged, nullptr,
2020-03-07 16:02:54 +00:00
CFNotificationSuspensionBehaviorDeliverImmediately);
2022-01-26 09:01:27 +00:00
// Register to be notified on displays arrangement changes.
CGDisplayRegisterReconfigurationCallback(_displays_arrangement_changed, nullptr);
2020-03-07 16:02:54 +00:00
2021-11-04 12:33:37 +00:00
// Init TTS
2023-05-16 11:18:12 +00:00
bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
if (tts_enabled) {
tts = [[TTS_MacOS alloc] init];
}
2021-11-04 12:33:37 +00:00
2020-03-07 16:02:54 +00:00
NSMenuItem *menu_item;
NSString *title;
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
2020-07-03 10:06:03 +00:00
if (nsappname == nil) {
2020-03-07 16:02:54 +00:00
nsappname = [[NSProcessInfo processInfo] processName];
2020-07-03 10:06:03 +00:00
}
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Setup Dock menu.
2020-03-07 16:02:54 +00:00
dock_menu = [[NSMenu alloc] initWithTitle:@"_dock"];
2022-03-22 09:26:36 +00:00
[dock_menu setAutoenablesItems:NO];
2020-03-07 16:02:54 +00:00
2022-01-26 09:01:27 +00:00
// Setup Apple menu.
2022-01-25 08:07:01 +00:00
apple_menu = [[NSMenu alloc] initWithTitle:@""];
2020-03-07 16:02:54 +00:00
title = [NSString stringWithFormat:NSLocalizedString(@"About %@", nil), nsappname];
[apple_menu addItemWithTitle:title action:@selector(showAbout:) keyEquivalent:@""];
2022-03-22 09:26:36 +00:00
[apple_menu setAutoenablesItems:NO];
2020-03-07 16:02:54 +00:00
[apple_menu addItem:[NSMenuItem separatorItem]];
NSMenu *services = [[NSMenu alloc] initWithTitle:@""];
menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Services", nil) action:nil keyEquivalent:@""];
[apple_menu setSubmenu:services forItem:menu_item];
[NSApp setServicesMenu:services];
[apple_menu addItem:[NSMenuItem separatorItem]];
title = [NSString stringWithFormat:NSLocalizedString(@"Hide %@", nil), nsappname];
[apple_menu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
menu_item = [apple_menu addItemWithTitle:NSLocalizedString(@"Hide Others", nil) action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
[menu_item setKeyEquivalentModifierMask:(NSEventModifierFlagOption | NSEventModifierFlagCommand)];
[apple_menu addItemWithTitle:NSLocalizedString(@"Show all", nil) action:@selector(unhideAllApplications:) keyEquivalent:@""];
[apple_menu addItem:[NSMenuItem separatorItem]];
2022-09-15 19:27:27 +00:00
title = [NSString stringWithFormat:NSLocalizedString(@"Quit %@", nil), nsappname];
2020-03-07 16:02:54 +00:00
[apple_menu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
2022-01-26 09:01:27 +00:00
// Add items to the menu bar.
2021-07-22 16:23:48 +00:00
NSMenu *main_menu = [NSApp mainMenu];
2020-03-07 16:02:54 +00:00
menu_item = [main_menu addItemWithTitle:@"" action:nil keyEquivalent:@""];
[main_menu setSubmenu:apple_menu forItem:menu_item];
2022-03-22 09:26:36 +00:00
[main_menu setAutoenablesItems:NO];
2020-03-07 16:02:54 +00:00
2022-08-31 08:11:52 +00:00
menu_delegate = [[GodotMenuDelegate alloc] init];
2020-03-07 16:02:54 +00:00
//!!!!!!!!!!!!!!!!!!!!!!!!!!
2021-09-26 23:07:10 +00:00
//TODO - do Vulkan and OpenGL support checks, driver selection and fallback
2020-03-07 16:02:54 +00:00
rendering_driver = p_rendering_driver;
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2021-11-02 10:27:38 +00:00
if (rendering_driver == "opengl3") {
2022-07-20 06:28:22 +00:00
GLManager_MacOS::ContextType opengl_api_type = GLManager_MacOS::GLES_3_0_COMPATIBLE;
gl_manager = memnew(GLManager_MacOS(opengl_api_type));
2021-11-02 10:27:38 +00:00
if (gl_manager->initialize() != OK) {
memdelete(gl_manager);
gl_manager = nullptr;
r_error = ERR_UNAVAILABLE;
ERR_FAIL_MSG("Could not initialize OpenGL");
}
2020-03-07 16:02:54 +00:00
}
#endif
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
2022-07-20 06:28:22 +00:00
context_vulkan = memnew(VulkanContextMacOS);
2020-03-07 16:02:54 +00:00
if (context_vulkan->initialize() != OK) {
memdelete(context_vulkan);
2021-04-29 09:47:24 +00:00
context_vulkan = nullptr;
2020-03-07 16:02:54 +00:00
r_error = ERR_CANT_CREATE;
ERR_FAIL_MSG("Could not initialize Vulkan");
}
}
#endif
2023-01-04 22:00:02 +00:00
Point2i window_position;
2022-10-23 10:34:41 +00:00
if (p_position != nullptr) {
window_position = *p_position;
2023-01-04 22:00:02 +00:00
} else {
if (p_screen == SCREEN_OF_MAIN_WINDOW) {
p_screen = SCREEN_PRIMARY;
}
window_position = screen_get_position(p_screen) + (screen_get_size(p_screen) - p_resolution) / 2;
2022-10-23 10:34:41 +00:00
}
2023-01-04 22:00:02 +00:00
WindowID main_window = _create_window(p_mode, p_vsync_mode, Rect2i(window_position, p_resolution));
2020-07-13 16:24:04 +00:00
ERR_FAIL_COND(main_window == INVALID_WINDOW_ID);
2020-03-07 16:02:54 +00:00
for (int i = 0; i < WINDOW_FLAG_MAX; i++) {
if (p_flags & (1 << i)) {
window_set_flag(WindowFlags(i), true, main_window);
}
}
2020-08-21 07:39:30 +00:00
show_window(MAIN_WINDOW_ID);
2023-01-29 12:18:22 +00:00
force_process_and_drop_events();
2020-03-07 16:02:54 +00:00
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2021-11-02 10:27:38 +00:00
if (rendering_driver == "opengl3") {
RasterizerGLES3::make_current();
2020-07-03 10:06:03 +00:00
}
#endif
2020-03-07 16:02:54 +00:00
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
rendering_device_vulkan = memnew(RenderingDeviceVulkan);
rendering_device_vulkan->initialize(context_vulkan);
2020-12-04 18:26:24 +00:00
RendererCompositorRD::make_current();
2020-03-07 16:02:54 +00:00
}
#endif
2022-08-04 07:38:26 +00:00
2022-08-06 20:10:24 +00:00
screen_set_keep_on(GLOBAL_GET("display/window/energy_saving/keep_screen_on"));
2020-03-07 16:02:54 +00:00
}
2022-07-20 06:28:22 +00:00
DisplayServerMacOS::~DisplayServerMacOS() {
2022-08-04 07:38:26 +00:00
if (screen_keep_on_assertion) {
IOPMAssertionRelease(screen_keep_on_assertion);
screen_keep_on_assertion = kIOPMNullAssertionID;
}
2022-01-26 09:01:27 +00:00
// Destroy all windows.
2022-05-13 13:04:37 +00:00
for (HashMap<WindowID, WindowData>::Iterator E = windows.begin(); E;) {
HashMap<WindowID, WindowData>::Iterator F = E;
++E;
[F->value.window_object setContentView:nil];
[F->value.window_object close];
2020-03-07 16:02:54 +00:00
}
2022-01-26 09:01:27 +00:00
// Destroy drivers.
2021-10-26 15:18:39 +00:00
#if defined(GLES3_ENABLED)
2021-11-02 10:27:38 +00:00
if (gl_manager) {
memdelete(gl_manager);
gl_manager = nullptr;
2020-07-03 10:06:03 +00:00
}
#endif
2020-03-07 16:02:54 +00:00
#if defined(VULKAN_ENABLED)
2021-11-02 10:27:38 +00:00
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
rendering_device_vulkan = nullptr;
}
2020-03-07 16:02:54 +00:00
2021-11-02 10:27:38 +00:00
if (context_vulkan) {
memdelete(context_vulkan);
context_vulkan = nullptr;
2020-03-07 16:02:54 +00:00
}
#endif
2021-04-29 09:47:24 +00:00
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDistributedCenter(), nullptr, kTISNotifySelectedKeyboardInputSourceChanged, nullptr);
2022-01-26 09:01:27 +00:00
CGDisplayRemoveReconfigurationCallback(_displays_arrangement_changed, nullptr);
2020-03-07 16:02:54 +00:00
cursors_cache.clear();
}