| 1 | #if !defined(_WIN32) |
| 2 | # error "This module is only made for Windows OS" |
| 3 | #endif |
| 4 | |
| 5 | #ifndef WIN32_CLIPBOARD_ |
| 6 | #define WIN32_CLIPBOARD_ |
| 7 | unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long long int *dataSize); |
| 8 | #endif |
| 9 | |
| 10 | #ifdef WIN32_CLIPBOARD_IMPLEMENTATION |
| 11 | #include <stdio.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <assert.h> |
| 15 | |
| 16 | |
| 17 | |
| 18 | #if !defined(_X86_) && !defined(_68K_) && !defined(_MPPC_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_IX86) |
| 19 | #define _X86_ |
| 20 | #if !defined(_CHPE_X86_ARM64_) && defined(_M_HYBRID) |
| 21 | #define _CHPE_X86_ARM64_ |
| 22 | #endif |
| 23 | #endif |
| 24 | |
| 25 | #if !defined(_AMD64_) && !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && (defined(_M_AMD64) || defined(_M_ARM64EC)) |
| 26 | #define _AMD64_ |
| 27 | #endif |
| 28 | |
| 29 | #if !defined(_ARM_) && !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_ARM) |
| 30 | #define _ARM_ |
| 31 | #endif |
| 32 | |
| 33 | #if !defined(_ARM64_) && !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64EC_) && defined(_M_ARM64) |
| 34 | #define _ARM64_ |
| 35 | #endif |
| 36 | |
| 37 | #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_ARM_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_ARM64EC) |
| 38 | #define _ARM64EC_ |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_M68K) |
| 42 | #define _68K_ |
| 43 | #endif |
| 44 | |
| 45 | #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_MPPC) |
| 46 | #define _MPPC_ |
| 47 | #endif |
| 48 | |
| 49 | #if !defined(_IA64_) && !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_M_IX86) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && !defined(_ARM64EC_) && defined(_M_IA64) |
| 50 | #define _IA64_ |
| 51 | #endif |
| 52 | |
| 53 | |
| 54 | #define WIN32_LEAN_AND_MEAN |
| 55 | |
| 56 | |
| 57 | |
| 58 | #include <minwindef.h> |
| 59 | |
| 60 | |
| 61 | #ifndef WINAPI |
| 62 | #if defined(_ARM_) |
| 63 | #define WINAPI |
| 64 | #else |
| 65 | #define WINAPI __stdcall |
| 66 | #endif |
| 67 | #endif |
| 68 | |
| 69 | #ifndef WINAPI |
| 70 | #if defined(_ARM_) |
| 71 | #define WINAPI |
| 72 | #else |
| 73 | #define WINAPI __stdcall |
| 74 | #endif |
| 75 | #endif |
| 76 | |
| 77 | #ifndef WINBASEAPI |
| 78 | #ifndef _KERNEL32_ |
| 79 | #define WINBASEAPI DECLSPEC_IMPORT |
| 80 | #else |
| 81 | #define WINBASEAPI |
| 82 | #endif |
| 83 | #endif |
| 84 | |
| 85 | #ifndef WINUSERAPI |
| 86 | #ifndef _USER32_ |
| 87 | #define WINUSERAPI __declspec (dllimport) |
| 88 | #else |
| 89 | #define WINUSERAPI |
| 90 | #endif |
| 91 | #endif |
| 92 | |
| 93 | typedef int WINBOOL; |
| 94 | |
| 95 | #if !defined(_WINUSER_) || !defined(WINUSER_ALREADY_INCLUDED) |
| 96 | WINUSERAPI WINBOOL WINAPI OpenClipboard(HWND hWndNewOwner); |
| 97 | WINUSERAPI WINBOOL WINAPI CloseClipboard(VOID); |
| 98 | WINUSERAPI DWORD WINAPI GetClipboardSequenceNumber(VOID); |
| 99 | WINUSERAPI HWND WINAPI GetClipboardOwner(VOID); |
| 100 | WINUSERAPI HWND WINAPI SetClipboardViewer(HWND hWndNewViewer); |
| 101 | WINUSERAPI HWND WINAPI GetClipboardViewer(VOID); |
| 102 | WINUSERAPI WINBOOL WINAPI ChangeClipboardChain(HWND hWndRemove, HWND hWndNewNext); |
| 103 | WINUSERAPI HANDLE WINAPI SetClipboardData(UINT uFormat, HANDLE hMem); |
| 104 | WINUSERAPI HANDLE WINAPI GetClipboardData(UINT uFormat); |
| 105 | WINUSERAPI UINT WINAPI RegisterClipboardFormatA(LPCSTR lpszFormat); |
| 106 | WINUSERAPI UINT WINAPI RegisterClipboardFormatW(LPCWSTR lpszFormat); |
| 107 | WINUSERAPI int WINAPI CountClipboardFormats(VOID); |
| 108 | WINUSERAPI UINT WINAPI EnumClipboardFormats(UINT format); |
| 109 | WINUSERAPI int WINAPI GetClipboardFormatNameA(UINT format, LPSTR lpszFormatName, int cchMaxCount); |
| 110 | WINUSERAPI int WINAPI GetClipboardFormatNameW(UINT format, LPWSTR lpszFormatName, int cchMaxCount); |
| 111 | WINUSERAPI WINBOOL WINAPI EmptyClipboard(VOID); |
| 112 | WINUSERAPI WINBOOL WINAPI IsClipboardFormatAvailable(UINT format); |
| 113 | WINUSERAPI int WINAPI GetPriorityClipboardFormat(UINT *paFormatPriorityList, int cFormats); |
| 114 | WINUSERAPI HWND WINAPI GetOpenClipboardWindow(VOID); |
| 115 | #endif |
| 116 | |
| 117 | #ifndef HGLOBAL |
| 118 | #define HGLOBAL void* |
| 119 | #endif |
| 120 | |
| 121 | #if !defined(_WINBASE_) || !defined(WINBASE_ALREADY_INCLUDED) |
| 122 | WINBASEAPI SIZE_T WINAPI GlobalSize (HGLOBAL hMem); |
| 123 | WINBASEAPI LPVOID WINAPI GlobalLock (HGLOBAL hMem); |
| 124 | WINBASEAPI WINBOOL WINAPI GlobalUnlock (HGLOBAL hMem); |
| 125 | #endif |
| 126 | |
| 127 | #if !defined(_WINGDI_) || !defined(WINGDI_ALREADY_INCLUDED) |
| 128 | #ifndef BITMAPINFOHEADER_ALREADY_DEFINED |
| 129 | #define BITMAPINFOHEADER_ALREADY_DEFINED |
| 130 | |
| 131 | #pragma pack(push, 1) |
| 132 | typedef struct tagBITMAPINFOHEADER { |
| 133 | DWORD biSize; |
| 134 | LONG biWidth; |
| 135 | LONG biHeight; |
| 136 | WORD biPlanes; |
| 137 | WORD biBitCount; |
| 138 | DWORD biCompression; |
| 139 | DWORD biSizeImage; |
| 140 | LONG biXPelsPerMeter; |
| 141 | LONG biYPelsPerMeter; |
| 142 | DWORD biClrUsed; |
| 143 | DWORD biClrImportant; |
| 144 | } BITMAPINFOHEADER,*LPBITMAPINFOHEADER,*PBITMAPINFOHEADER; |
| 145 | #pragma pack(pop) |
| 146 | #endif |
| 147 | |
| 148 | #ifndef BITMAPFILEHEADER_ALREADY_DEFINED |
| 149 | #define BITMAPFILEHEADER_ALREADY_DEFINED |
| 150 | #pragma pack(push, 1) |
| 151 | typedef struct tagBITMAPFILEHEADER { |
| 152 | WORD bfType; |
| 153 | DWORD bfSize; |
| 154 | WORD bfReserved1; |
| 155 | WORD bfReserved2; |
| 156 | DWORD bfOffBits; |
| 157 | } BITMAPFILEHEADER,*LPBITMAPFILEHEADER,*PBITMAPFILEHEADER; |
| 158 | #pragma pack(pop) |
| 159 | #endif |
| 160 | |
| 161 | #ifndef RGBQUAD_ALREADY_DEFINED |
| 162 | #define RGBQUAD_ALREADY_DEFINED |
| 163 | typedef struct tagRGBQUAD { |
| 164 | BYTE rgbBlue; |
| 165 | BYTE rgbGreen; |
| 166 | BYTE rgbRed; |
| 167 | BYTE rgbReserved; |
| 168 | } RGBQUAD, *LPRGBQUAD; |
| 169 | #endif |
| 170 | |
| 171 | |
| 172 | #define BI_RGB 0x0000 |
| 173 | #define BI_RLE8 0x0001 |
| 174 | #define BI_RLE4 0x0002 |
| 175 | #define BI_BITFIELDS 0x0003 |
| 176 | #define BI_JPEG 0x0004 |
| 177 | #define BI_PNG 0x0005 |
| 178 | #define BI_CMYK 0x000B |
| 179 | #define BI_CMYKRLE8 0x000C |
| 180 | #define BI_CMYKRLE4 0x000D |
| 181 | #endif |
| 182 | |
| 183 | #ifndef BI_ALPHABITFIELDS |
| 184 | |
| 185 | |
| 186 | #define BI_ALPHABITFIELDS 0x0006 |
| 187 | #endif |
| 188 | |
| 189 | |
| 190 | #define CF_DIB 8 |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | static BOOL OpenClipboardRetrying(HWND handle); |
| 211 | static int GetPixelDataOffset(BITMAPINFOHEADER bih); |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long long int *dataSize) |
| 217 | { |
| 218 | unsigned char *bmpData = NULL; |
| 219 | |
| 220 | if (OpenClipboardRetrying(NULL)) |
| 221 | { |
| 222 | HGLOBAL clipHandle = (HGLOBAL)GetClipboardData(CF_DIB); |
| 223 | if (clipHandle != NULL) |
| 224 | { |
| 225 | BITMAPINFOHEADER *bmpInfoHeader = (BITMAPINFOHEADER *)GlobalLock(clipHandle); |
| 226 | if (bmpInfoHeader) |
| 227 | { |
| 228 | *width = bmpInfoHeader->biWidth; |
| 229 | *height = bmpInfoHeader->biHeight; |
| 230 | SIZE_T clipDataSize = GlobalSize(clipHandle); |
| 231 | if (clipDataSize >= sizeof(BITMAPINFOHEADER)) |
| 232 | { |
| 233 | int pixelOffset = GetPixelDataOffset(*bmpInfoHeader); |
| 234 | |
| 235 | |
| 236 | |
| 237 | BITMAPFILEHEADER bmpFileHeader = { 0 }; |
| 238 | SIZE_T bmpFileSize = sizeof(bmpFileHeader) + clipDataSize; |
| 239 | *dataSize = bmpFileSize; |
| 240 | |
| 241 | bmpFileHeader.bfType = 0x4D42; |
| 242 | bmpFileHeader.bfSize = (DWORD)bmpFileSize; |
| 243 | bmpFileHeader.bfOffBits = sizeof(bmpFileHeader) + pixelOffset; |
| 244 | |
| 245 | bmpData = (unsigned char *)RL_MALLOC(sizeof(bmpFileHeader) + clipDataSize); |
| 246 | memcpy(bmpData, &bmpFileHeader, sizeof(bmpFileHeader)); |
| 247 | memcpy(bmpData + sizeof(bmpFileHeader), bmpInfoHeader, clipDataSize); |
| 248 | |
| 249 | GlobalUnlock(clipHandle); |
| 250 | CloseClipboard(); |
| 251 | |
| 252 | TRACELOG(LOG_INFO, "Clipboad image acquired successfully"); |
| 253 | |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | TRACELOG(LOG_WARNING, "Clipboard data is malformed"); |
| 258 | GlobalUnlock(clipHandle); |
| 259 | CloseClipboard(); |
| 260 | } |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | TRACELOG(LOG_WARNING, "Clipboard data failed to be locked"); |
| 265 | GlobalUnlock(clipHandle); |
| 266 | CloseClipboard(); |
| 267 | } |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | TRACELOG(LOG_WARNING, "Clipboard data is not an image"); |
| 272 | CloseClipboard(); |
| 273 | } |
| 274 | } |
| 275 | else TRACELOG(LOG_WARNING, "Clipboard can not be opened"); |
| 276 | |
| 277 | return bmpData; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | static BOOL OpenClipboardRetrying(HWND hWnd) |
| 286 | { |
| 287 | static const int maxTries = 20; |
| 288 | static const int sleepTimeMS = 60; |
| 289 | |
| 290 | for (int i = 0; i < maxTries; i++) |
| 291 | { |
| 292 | |
| 293 | |
| 294 | if (OpenClipboard(hWnd)) return true; |
| 295 | |
| 296 | Sleep(sleepTimeMS); |
| 297 | } |
| 298 | |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | static int GetPixelDataOffset(BITMAPINFOHEADER bih) |
| 306 | { |
| 307 | int offset = 0; |
| 308 | const unsigned int rgbaSize = sizeof(RGBQUAD); |
| 309 | |
| 310 | |
| 311 | |
| 312 | if ((bih.biSize == 40) && (sizeof(BITMAPINFOHEADER) == 40)) |
| 313 | { |
| 314 | |
| 315 | |
| 316 | |
| 317 | if (bih.biBitCount > 8) |
| 318 | { |
| 319 | |
| 320 | |
| 321 | if (bih.biCompression == BI_BITFIELDS) offset += 3*rgbaSize; |
| 322 | else if (bih.biCompression == BI_ALPHABITFIELDS) offset += 4 * rgbaSize; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | if (bih.biClrUsed > 0) offset += bih.biClrUsed*rgbaSize; |
| 332 | else |
| 333 | { |
| 334 | if (bih.biBitCount < 16) offset = offset + (rgbaSize << bih.biBitCount); |
| 335 | } |
| 336 | |
| 337 | return bih.biSize + offset; |
| 338 | } |
| 339 | #endif |