Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clipboard_has/get_image methods to DisplayServer #63826

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
Returns the user's clipboard as a string if possible.
</description>
</method>
<method name="clipboard_get_image" qualifiers="const">
<return type="Image" />
<description>
Returns the user's clipboard as an image if possible.
</description>
</method>
<method name="clipboard_get_primary" qualifiers="const">
<return type="String" />
<description>
Expand All @@ -26,7 +32,13 @@
<method name="clipboard_has" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if there is content on the user's clipboard.
Returns [code]true[/code] if there is a text content on the user's clipboard.
</description>
</method>
<method name="clipboard_has_image" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if there is an image content on the user's clipboard.
</description>
</method>
<method name="clipboard_set">
Expand Down
3 changes: 3 additions & 0 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ class DisplayServerMacOS : public DisplayServer {

virtual void clipboard_set(const String &p_text) override;
virtual String clipboard_get() const override;
virtual Ref<Image> clipboard_get_image() const override;
virtual bool clipboard_has() const override;
virtual bool clipboard_has_image() const override;

virtual int get_screen_count() const override;
virtual int get_primary_screen() const override;
Expand Down
32 changes: 32 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "core/io/marshalls.h"
#include "core/math/geometry_2d.h"
#include "core/os/keyboard.h"
#include "drivers/png/png_driver_common.h"
#include "main/main.h"
#include "scene/resources/texture.h"

Expand Down Expand Up @@ -2100,6 +2101,37 @@
return ret;
}

Ref<Image> DisplayServerMacOS::clipboard_get_image() const {
Ref<Image> image;
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *result = [pasteboard availableTypeFromArray:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSPasteboardTypePNG, nil]];
RedworkDE marked this conversation as resolved.
Show resolved Hide resolved
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;
}

int DisplayServerMacOS::get_screen_count() const {
_THREAD_SAFE_METHOD_

Expand Down
63 changes: 63 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "os_windows.h"

#include "core/io/marshalls.h"
#include "drivers/png/png_driver_common.h"
#include "main/main.h"
#include "scene/resources/texture.h"

Expand Down Expand Up @@ -341,6 +342,68 @@ String DisplayServerWindows::clipboard_get() const {
return ret;
}

Ref<Image> DisplayServerWindows::clipboard_get_image() const {
Ref<Image> image;
if (!windows.has(last_focused_window)) {
return image; // No focused window?
}
if (!OpenClipboard(windows[last_focused_window].hWnd)) {
ERR_FAIL_V_MSG(image, "Unable to open clipboard.");
}
UINT png_format = RegisterClipboardFormatA("PNG");
if (png_format && IsClipboardFormatAvailable(png_format)) {
HANDLE png_handle = GetClipboardData(png_format);
if (png_handle) {
size_t png_size = GlobalSize(png_handle);
uint8_t *png_data = (uint8_t *)GlobalLock(png_handle);
image.instantiate();

PNGDriverCommon::png_to_image(png_data, png_size, false, image);

GlobalUnlock(png_handle);
}
} else if (IsClipboardFormatAvailable(CF_DIB)) {
HGLOBAL mem = GetClipboardData(CF_DIB);
if (mem != NULL) {
BITMAPINFO *ptr = static_cast<BITMAPINFO *>(GlobalLock(mem));

if (ptr != NULL) {
BITMAPINFOHEADER *info = &ptr->bmiHeader;
PackedByteArray pba;

for (LONG y = info->biHeight - 1; y > -1; y--) {
for (LONG x = 0; x < info->biWidth; x++) {
tagRGBQUAD *rgbquad = ptr->bmiColors + (info->biWidth * y) + x;
pba.append(rgbquad->rgbRed);
pba.append(rgbquad->rgbGreen);
pba.append(rgbquad->rgbBlue);
pba.append(rgbquad->rgbReserved);
}
}
image.instantiate();
image->create_from_data(info->biWidth, info->biHeight, false, Image::Format::FORMAT_RGBA8, pba);

GlobalUnlock(mem);
}
}
}
bruvzg marked this conversation as resolved.
Show resolved Hide resolved

CloseClipboard();

return image;
}

bool DisplayServerWindows::clipboard_has() const {
return (IsClipboardFormatAvailable(CF_TEXT) ||
IsClipboardFormatAvailable(CF_UNICODETEXT) ||
IsClipboardFormatAvailable(CF_OEMTEXT));
}

bool DisplayServerWindows::clipboard_has_image() const {
UINT png_format = RegisterClipboardFormatA("PNG");
return ((png_format && IsClipboardFormatAvailable(png_format)) || IsClipboardFormatAvailable(CF_DIB));
}

typedef struct {
int count;
int screen;
Expand Down
3 changes: 3 additions & 0 deletions platform/windows/display_server_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ class DisplayServerWindows : public DisplayServer {

virtual void clipboard_set(const String &p_text) override;
virtual String clipboard_get() const override;
virtual Ref<Image> clipboard_get_image() const override;
virtual bool clipboard_has() const override;
virtual bool clipboard_has_image() const override;

virtual int get_screen_count() const override;
virtual int get_primary_screen() const override;
Expand Down
10 changes: 10 additions & 0 deletions servers/display_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,18 @@ String DisplayServer::clipboard_get() const {
ERR_FAIL_V_MSG(String(), "Clipboard is not supported by this display server.");
}

Ref<Image> DisplayServer::clipboard_get_image() const {
ERR_FAIL_V_MSG(Ref<Image>(), "Clipboard is not supported by this display server.");
}

bool DisplayServer::clipboard_has() const {
return !clipboard_get().is_empty();
}

bool DisplayServer::clipboard_has_image() const {
return clipboard_get_image().is_valid();
}

void DisplayServer::clipboard_set_primary(const String &p_text) {
WARN_PRINT("Primary clipboard is not supported by this display server.");
}
Expand Down Expand Up @@ -640,7 +648,9 @@ void DisplayServer::_bind_methods() {

ClassDB::bind_method(D_METHOD("clipboard_set", "clipboard"), &DisplayServer::clipboard_set);
ClassDB::bind_method(D_METHOD("clipboard_get"), &DisplayServer::clipboard_get);
ClassDB::bind_method(D_METHOD("clipboard_get_image"), &DisplayServer::clipboard_get_image);
ClassDB::bind_method(D_METHOD("clipboard_has"), &DisplayServer::clipboard_has);
ClassDB::bind_method(D_METHOD("clipboard_has_image"), &DisplayServer::clipboard_has_image);
ClassDB::bind_method(D_METHOD("clipboard_set_primary", "clipboard_primary"), &DisplayServer::clipboard_set_primary);
ClassDB::bind_method(D_METHOD("clipboard_get_primary"), &DisplayServer::clipboard_get_primary);

Expand Down
2 changes: 2 additions & 0 deletions servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ class DisplayServer : public Object {

virtual void clipboard_set(const String &p_text);
virtual String clipboard_get() const;
virtual Ref<Image> clipboard_get_image() const;
virtual bool clipboard_has() const;
virtual bool clipboard_has_image() const;
virtual void clipboard_set_primary(const String &p_text);
virtual String clipboard_get_primary() const;

Expand Down
Loading