Browse Source

[rcore]: PLATFORM_SDL: improve clipboard data retrieval

pull/4459/head
evertonse 1 week ago
parent
commit
4d5dfb150c
2 changed files with 39 additions and 13 deletions
  1. +9
    -2
      src/config.h
  2. +30
    -11
      src/platforms/rcore_desktop_sdl.c

+ 9
- 2
src/config.h View File

@ -283,14 +283,21 @@
#define STBI_REQUIRED
#endif
#ifndef SUPPORT_FILEFORMAT_BMP
#ifndef SUPPORT_FILEFORMAT_BMP // For clipboard image on Windows
#define SUPPORT_FILEFORMAT_BMP 1
#endif
#ifndef SUPPORT_FILEFORMAT_PNG // Wayland uses png for prints, at least it was on 22 LTS ubuntu
#define SUPPORT_FILEFORMAT_PNG 1
#endif
#ifndef SUPPORT_FILEFORMAT_JPG
#define SUPPORT_FILEFORMAT_JPG 1
#endif
#ifndef SUPPORT_MODULE_RTEXTURES
#define SUPPORT_MODULE_RTEXTURES 1
#endif
#endif
#endif // CONFIG_H

+ 30
- 11
src/platforms/rcore_desktop_sdl.c View File

@ -1086,20 +1086,39 @@ const char *GetClipboardText(void)
// Get clipboard image
Image GetClipboardImage(void)
{
Image image = {0};
// Let's hope compiler put these arrays in static memory
const char *image_formats[] = {
"image/bmp",
"image/png",
"image/jpg",
"image/tiff",
};
const char *image_extensions[] = {
".bmp",
".png",
".jpg",
".tiff",
};
Image image = {0};
size_t dataSize = 0;
// NOTE: This pointer should be free with SDL_free() at some point.
// TODO: In case of failure let's try to cycle in to other mime types (formats)
void* fileData = SDL_GetClipboardData("image/bmp", &dataSize); // returns NULL on failure;
if (fileData == NULL)
{
TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data. %s", SDL_GetError());
}
else
{
image = LoadImageFromMemory(".bmp", fileData, dataSize);
void *fileData = NULL;
for (int i = 0; i < SDL_arraysize(image_formats); ++i)
{
// NOTE: This pointer should be free with SDL_free() at some point.
fileData = SDL_GetClipboardData(image_formats[i], &dataSize);
if (fileData) {
image = LoadImageFromMemory(image_extensions[i], fileData, dataSize);
if (IsImageValid(image))
{
TRACELOG(LOG_INFO, "Clipboard image: Got image from clipboard as a `%s` successfully", image_extensions[i]);
return image;
}
}
}
TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data. %s", SDL_GetError());
return image;
}
#endif

Loading…
Cancel
Save