Skip to content

Commit 09727b2

Browse files
committed
[macOS] Fix clipboard and TTS not working in embedded game mode.
1 parent 978b387 commit 09727b2

File tree

6 files changed

+243
-160
lines changed

6 files changed

+243
-160
lines changed

platform/macos/SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ files = [
1111
"godot_application_delegate.mm",
1212
"crash_handler_macos.mm",
1313
"macos_terminal_logger.mm",
14+
"display_server_macos_base.mm",
1415
"display_server_embedded.mm",
1516
"display_server_macos.mm",
1617
"embedded_debugger.mm",

platform/macos/display_server_embedded.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131
#pragma once
3232

33-
#include "core/input/input.h"
34-
#include "servers/display_server.h"
33+
#include "display_server_macos_base.h"
3534

3635
@class CAContext;
3736
@class CALayer;
@@ -54,10 +53,8 @@ struct DisplayServerEmbeddedState {
5453
}
5554
};
5655

57-
class DisplayServerEmbedded : public DisplayServer {
58-
GDCLASS(DisplayServerEmbedded, DisplayServer)
59-
60-
_THREAD_SAFE_CLASS_
56+
class DisplayServerEmbedded : public DisplayServerMacOSBase {
57+
GDSOFTCLASS(DisplayServerEmbedded, DisplayServerMacOSBase)
6158

6259
DisplayServerEmbeddedState state;
6360

platform/macos/display_server_macos.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131
#pragma once
3232

33-
#include "core/input/input.h"
34-
#include "servers/display_server.h"
33+
#include "display_server_macos_base.h"
3534

3635
#if defined(GLES3_ENABLED)
3736
#include "gl_manager_macos_angle.h"
@@ -74,10 +73,8 @@
7473

7574
class EmbeddedProcessMacOS;
7675

77-
class DisplayServerMacOS : public DisplayServer {
78-
GDSOFTCLASS(DisplayServerMacOS, DisplayServer);
79-
80-
_THREAD_SAFE_CLASS_
76+
class DisplayServerMacOS : public DisplayServerMacOSBase {
77+
GDSOFTCLASS(DisplayServerMacOS, DisplayServerMacOSBase);
8178

8279
public:
8380
struct KeyEvent {
@@ -175,7 +172,6 @@ class DisplayServerMacOS : public DisplayServer {
175172
Vector<KeyEvent> key_event_buffer;
176173
int key_event_pos = 0;
177174

178-
id tts = nullptr;
179175
id menu_delegate = nullptr;
180176
NativeMenuMacOS *native_menu = nullptr;
181177

@@ -253,8 +249,6 @@ class DisplayServerMacOS : public DisplayServer {
253249

254250
Error _file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, bool p_options_in_cb, WindowID p_window_id);
255251

256-
void initialize_tts() const;
257-
258252
struct EmbeddedProcessData {
259253
EmbeddedProcessMacOS *process;
260254
WindowData *wd = nullptr;
@@ -315,15 +309,6 @@ class DisplayServerMacOS : public DisplayServer {
315309
Callable _help_get_search_callback() const;
316310
Callable _help_get_action_callback() const;
317311

318-
virtual bool tts_is_speaking() const override;
319-
virtual bool tts_is_paused() const override;
320-
virtual TypedArray<Dictionary> tts_get_voices() const override;
321-
322-
virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
323-
virtual void tts_pause() override;
324-
virtual void tts_resume() override;
325-
virtual void tts_stop() override;
326-
327312
virtual bool is_dark_mode_supported() const override;
328313
virtual bool is_dark_mode() const override;
329314
virtual Color get_accent_color() const override;
@@ -350,12 +335,6 @@ class DisplayServerMacOS : public DisplayServer {
350335
virtual Point2i mouse_get_position() const override;
351336
virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
352337

353-
virtual void clipboard_set(const String &p_text) override;
354-
virtual String clipboard_get() const override;
355-
virtual Ref<Image> clipboard_get_image() const override;
356-
virtual bool clipboard_has() const override;
357-
virtual bool clipboard_has_image() const override;
358-
359338
virtual int get_screen_count() const override;
360339
virtual int get_primary_screen() const override;
361340
virtual int get_keyboard_focus_screen() const override;

platform/macos/display_server_macos.mm

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#import "key_mapping_macos.h"
4444
#import "macos_quartz_core_spi.h"
4545
#import "os_macos.h"
46-
#import "tts_macos.h"
4746

4847
#include "core/config/project_settings.h"
4948
#include "core/io/marshalls.h"
@@ -916,66 +915,6 @@
916915
return help_action_callback;
917916
}
918917

919-
void DisplayServerMacOS::initialize_tts() const {
920-
const_cast<DisplayServerMacOS *>(this)->tts = [[TTS_MacOS alloc] init];
921-
}
922-
923-
bool DisplayServerMacOS::tts_is_speaking() const {
924-
if (unlikely(!tts)) {
925-
initialize_tts();
926-
}
927-
ERR_FAIL_NULL_V(tts, false);
928-
return [tts isSpeaking];
929-
}
930-
931-
bool DisplayServerMacOS::tts_is_paused() const {
932-
if (unlikely(!tts)) {
933-
initialize_tts();
934-
}
935-
ERR_FAIL_NULL_V(tts, false);
936-
return [tts isPaused];
937-
}
938-
939-
TypedArray<Dictionary> DisplayServerMacOS::tts_get_voices() const {
940-
if (unlikely(!tts)) {
941-
initialize_tts();
942-
}
943-
ERR_FAIL_NULL_V(tts, TypedArray<Dictionary>());
944-
return [tts getVoices];
945-
}
946-
947-
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) {
948-
if (unlikely(!tts)) {
949-
initialize_tts();
950-
}
951-
ERR_FAIL_NULL(tts);
952-
[tts speak:p_text voice:p_voice volume:p_volume pitch:p_pitch rate:p_rate utterance_id:p_utterance_id interrupt:p_interrupt];
953-
}
954-
955-
void DisplayServerMacOS::tts_pause() {
956-
if (unlikely(!tts)) {
957-
initialize_tts();
958-
}
959-
ERR_FAIL_NULL(tts);
960-
[tts pauseSpeaking];
961-
}
962-
963-
void DisplayServerMacOS::tts_resume() {
964-
if (unlikely(!tts)) {
965-
initialize_tts();
966-
}
967-
ERR_FAIL_NULL(tts);
968-
[tts resumeSpeaking];
969-
}
970-
971-
void DisplayServerMacOS::tts_stop() {
972-
if (unlikely(!tts)) {
973-
initialize_tts();
974-
}
975-
ERR_FAIL_NULL(tts);
976-
[tts stopSpeaking];
977-
}
978-
979918
bool DisplayServerMacOS::is_dark_mode_supported() const {
980919
if (@available(macOS 10.14, *)) {
981920
return true;
@@ -1642,69 +1581,6 @@
16421581
return last_button_state;
16431582
}
16441583

1645-
void DisplayServerMacOS::clipboard_set(const String &p_text) {
1646-
_THREAD_SAFE_METHOD_
1647-
1648-
NSString *copiedString = [NSString stringWithUTF8String:p_text.utf8().get_data()];
1649-
NSArray *copiedStringArray = [NSArray arrayWithObject:copiedString];
1650-
1651-
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
1652-
[pasteboard clearContents];
1653-
[pasteboard writeObjects:copiedStringArray];
1654-
}
1655-
1656-
String DisplayServerMacOS::clipboard_get() const {
1657-
_THREAD_SAFE_METHOD_
1658-
1659-
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
1660-
NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
1661-
NSDictionary *options = [NSDictionary dictionary];
1662-
1663-
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
1664-
1665-
if (!ok) {
1666-
return "";
1667-
}
1668-
1669-
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
1670-
NSString *string = [objectsToPaste objectAtIndex:0];
1671-
1672-
String ret;
1673-
ret.append_utf8([string UTF8String]);
1674-
return ret;
1675-
}
1676-
1677-
Ref<Image> DisplayServerMacOS::clipboard_get_image() const {
1678-
Ref<Image> image;
1679-
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
1680-
NSString *result = [pasteboard availableTypeFromArray:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSPasteboardTypePNG, nil]];
1681-
if (!result) {
1682-
return image;
1683-
}
1684-
NSData *data = [pasteboard dataForType:result];
1685-
if (!data) {
1686-
return image;
1687-
}
1688-
NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:data];
1689-
NSData *pngData = [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
1690-
image.instantiate();
1691-
PNGDriverCommon::png_to_image((const uint8_t *)pngData.bytes, pngData.length, false, image);
1692-
return image;
1693-
}
1694-
1695-
bool DisplayServerMacOS::clipboard_has() const {
1696-
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
1697-
NSArray *classArray = [NSArray arrayWithObject:[NSString class]];
1698-
NSDictionary *options = [NSDictionary dictionary];
1699-
return [pasteboard canReadObjectForClasses:classArray options:options];
1700-
}
1701-
1702-
bool DisplayServerMacOS::clipboard_has_image() const {
1703-
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
1704-
NSString *result = [pasteboard availableTypeFromArray:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSPasteboardTypePNG, nil]];
1705-
return result;
1706-
}
1707-
17081584
int DisplayServerMacOS::get_screen_count() const {
17091585
_THREAD_SAFE_METHOD_
17101586

@@ -4013,12 +3889,6 @@
40133889
// Register to be notified on displays arrangement changes.
40143890
CGDisplayRegisterReconfigurationCallback(_displays_arrangement_changed, nullptr);
40153891

4016-
// Init TTS
4017-
bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
4018-
if (tts_enabled) {
4019-
initialize_tts();
4020-
}
4021-
40223892
native_menu = memnew(NativeMenuMacOS);
40233893

40243894
#ifdef ACCESSKIT_ENABLED
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**************************************************************************/
2+
/* display_server_macos_base.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/input/input.h"
34+
#include "servers/display_server.h"
35+
36+
#define FontVariation __FontVariation
37+
38+
#import <AppKit/AppKit.h>
39+
40+
#undef FontVariation
41+
42+
class DisplayServerMacOSBase : public DisplayServer {
43+
GDSOFTCLASS(DisplayServerMacOSBase, DisplayServer)
44+
45+
id tts = nullptr;
46+
47+
protected:
48+
_THREAD_SAFE_CLASS_
49+
50+
void initialize_tts() const;
51+
52+
public:
53+
virtual void clipboard_set(const String &p_text) override;
54+
virtual String clipboard_get() const override;
55+
virtual Ref<Image> clipboard_get_image() const override;
56+
virtual bool clipboard_has() const override;
57+
virtual bool clipboard_has_image() const override;
58+
59+
virtual bool tts_is_speaking() const override;
60+
virtual bool tts_is_paused() const override;
61+
virtual TypedArray<Dictionary> tts_get_voices() const override;
62+
63+
virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
64+
virtual void tts_pause() override;
65+
virtual void tts_resume() override;
66+
virtual void tts_stop() override;
67+
68+
DisplayServerMacOSBase();
69+
};

0 commit comments

Comments
 (0)