Debian Codegraph @

src/external/win32_clipboard.h

raw symbols SCIP
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 // WIN32_CLIPBOARD_
9
10 #ifdef WIN32_CLIPBOARD_IMPLEMENTATION
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <assert.h>
15
16 // NOTE: These search for architecture is taken from "windows.h", and it's necessary to avoid including windows.h
17 // and still make it compile on msvc, because import indirectly importing "winnt.h" (e.g. <minwindef.h>) can cause problems is these are not defined
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 // #include <sdkddkver.h>
56 // #include <windows.h>
57 // #include <winuser.h>
58 #include <minwindef.h>
59 // #include <minwinbase.h>
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 // Does this header need to be packed ? by the windowps header it doesnt seem to be
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 // REF: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-wmf/4e588f70-bd92-4a6f-b77f-35d0feaf7a57
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 // Bitmap not compressed and that the color table consists of four DWORD color masks,
185 // that specify the red, green, blue, and alpha components of each pixel
186 #define BI_ALPHABITFIELDS 0x0006
187 #endif
188
189 // REF: https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats
190 #define CF_DIB 8
191
192 // REF: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setsystemcursor
193 // #define OCR_NORMAL 32512 // Normal select
194 // #define OCR_IBEAM 32513 // Text select
195 // #define OCR_WAIT 32514 // Busy
196 // #define OCR_CROSS 32515 // Precision select
197 // #define OCR_UP 32516 // Alternate select
198 // #define OCR_SIZENWSE 32642 // Diagonal resize 1
199 // #define OCR_SIZENESW 32643 // Diagonal resize 2
200 // #define OCR_SIZEWE 32644 // Horizontal resize
201 // #define OCR_SIZENS 32645 // Vertical resize
202 // #define OCR_SIZEALL 32646 // Move
203 // #define OCR_NO 32648 // Unavailable
204 // #define OCR_HAND 32649 // Link select
205 // #define OCR_APPSTARTING 32650 //
206
207 //----------------------------------------------------------------------------------
208 // Module Internal Functions Declaration
209 //----------------------------------------------------------------------------------
210 static BOOL OpenClipboardRetrying(HWND handle); // Open clipboard with a number of retries
211 static int GetPixelDataOffset(BITMAPINFOHEADER bih); // Get pixel data offset from DIB image
212
213 //----------------------------------------------------------------------------------
214 // Module Functions Definition
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 // Create the bytes for a correct BMP file and copy the data to a pointer
236 //------------------------------------------------------------------------
237 BITMAPFILEHEADER bmpFileHeader = { 0 };
238 SIZE_T bmpFileSize = sizeof(bmpFileHeader) + clipDataSize;
239 *dataSize = bmpFileSize;
240
241 bmpFileHeader.bfType = 0x4D42; // BMP fil type constant
242 bmpFileHeader.bfSize = (DWORD)bmpFileSize; // Up to 4GB works fine
243 bmpFileHeader.bfOffBits = sizeof(bmpFileHeader) + pixelOffset;
244
245 bmpData = (unsigned char *)RL_MALLOC(sizeof(bmpFileHeader) + clipDataSize);
246 memcpy(bmpData, &bmpFileHeader, sizeof(bmpFileHeader)); // Add BMP file header data
247 memcpy(bmpData + sizeof(bmpFileHeader), bmpInfoHeader, clipDataSize); // Add BMP info header data
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 // Module Internal Functions Definition
282 //----------------------------------------------------------------------------------
283 // Open clipboard with several tries
284 // NOTE: If parameter is NULL, the open clipboard is associated with the current task
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 // Might be being hold by another process
293 // Or yourself forgot to CloseClipboard
294 if (OpenClipboard(hWnd)) return true;
295
296 Sleep(sleepTimeMS);
297 }
298
299 return false;
300 }
301
302 // Get the byte offset where does the pixels data start (from a packed DIB)
303 // REF: https://stackoverflow.com/questions/30552255/how-to-read-a-bitmap-from-the-windows-clipboard#30552856
304 // REF: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
305 static int GetPixelDataOffset(BITMAPINFOHEADER bih)
306 {
307 int offset = 0;
308 const unsigned int rgbaSize = sizeof(RGBQUAD);
309
310 // NOTE: biSize specifies the number of bytes required by the structure
311 // It's expected to be always 40 because it should be packed
312 if ((bih.biSize == 40) && (sizeof(BITMAPINFOHEADER) == 40))
313 {
314 // NOTE: biBitCount specifies the number of bits per pixel
315 // Might exist some bit masks *after* the header and *before* the pixel offset
316 // we're looking, but only if more than 8 bits per pixel, so it needs to be ajusted for that
317 if (bih.biBitCount > 8)
318 {
319 // If (bih.biCompression == BI_RGB) no need to be offset more
320
321 if (bih.biCompression == BI_BITFIELDS) offset += 3*rgbaSize;
322 else if (bih.biCompression == BI_ALPHABITFIELDS) offset += 4 * rgbaSize; // Not widely supported, but valid
323 }
324 }
325
326 // NOTE: biClrUsed specifies the number of color indices in the color table that are actually used by the bitmap
327 // If this value is zero, the bitmap uses the maximum number of colors
328 // corresponding to the value of the biBitCount member for the compression mode specified by biCompression
329 // If biClrUsed is nonzero and the biBitCount member is less than 16
330 // the biClrUsed member specifies the actual number of colors
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 // WIN32_CLIPBOARD_IMPLEMENTATION