FreeRDP
Loading...
Searching...
No Matches
TestClipboardFormats.c
1
2#include <winpr/crt.h>
3#include <winpr/print.h>
4#include <winpr/image.h>
5#include <winpr/clipboard.h>
6
7int TestClipboardFormats(int argc, char* argv[])
8{
9 int rc = -1;
10 UINT32 count = 0;
11 UINT32* pFormatIds = NULL;
12 const char* formatName = NULL;
13 wClipboard* clipboard = NULL;
14 UINT32 utf8StringFormatId = 0;
15
16 WINPR_UNUSED(argc);
17 WINPR_UNUSED(argv);
18
19 clipboard = ClipboardCreate();
20 if (!clipboard)
21 return -1;
22
23 const char* mime_types[] = { "text/html", "text/html", "image/bmp",
24 "image/png", "image/webp", "image/jpeg" };
25 for (size_t x = 0; x < ARRAYSIZE(mime_types); x++)
26 {
27 const char* mime = mime_types[x];
28 UINT32 id = ClipboardRegisterFormat(clipboard, mime);
29 (void)fprintf(stderr, "ClipboardRegisterFormat(%s) -> 0x%08" PRIx32 "\n", mime, id);
30 if (id == 0)
31 goto fail;
32 }
33
34 utf8StringFormatId = ClipboardRegisterFormat(clipboard, "UTF8_STRING");
35 pFormatIds = NULL;
36 count = ClipboardGetRegisteredFormatIds(clipboard, &pFormatIds);
37
38 for (UINT32 index = 0; index < count; index++)
39 {
40 UINT32 formatId = pFormatIds[index];
41 formatName = ClipboardGetFormatName(clipboard, formatId);
42 (void)fprintf(stderr, "Format: 0x%08" PRIX32 " %s\n", formatId, formatName);
43 }
44
45 free(pFormatIds);
46
47 if (1)
48 {
49 BOOL bSuccess = 0;
50 UINT32 SrcSize = 0;
51 UINT32 DstSize = 0;
52 const char pSrcData[] = "this is a test string";
53 char* pDstData = NULL;
54
55 SrcSize = (UINT32)(strnlen(pSrcData, ARRAYSIZE(pSrcData)) + 1);
56 bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, pSrcData, SrcSize);
57 (void)fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
58 DstSize = 0;
59 pDstData = (char*)ClipboardGetData(clipboard, utf8StringFormatId, &DstSize);
60 (void)fprintf(stderr, "ClipboardGetData: %s\n", pDstData);
61 free(pDstData);
62 }
63
64 if (1)
65 {
66 UINT32 DstSize = 0;
67 char* pSrcData = NULL;
68 WCHAR* pDstData = NULL;
69 DstSize = 0;
70 pDstData = (WCHAR*)ClipboardGetData(clipboard, CF_UNICODETEXT, &DstSize);
71 pSrcData = ConvertWCharNToUtf8Alloc(pDstData, DstSize / sizeof(WCHAR), NULL);
72
73 (void)fprintf(stderr, "ClipboardGetData (synthetic): %s\n", pSrcData);
74 free(pDstData);
75 free(pSrcData);
76 }
77
78 pFormatIds = NULL;
79 count = ClipboardGetFormatIds(clipboard, &pFormatIds);
80
81 for (UINT32 index = 0; index < count; index++)
82 {
83 UINT32 formatId = pFormatIds[index];
84 formatName = ClipboardGetFormatName(clipboard, formatId);
85 (void)fprintf(stderr, "Format: 0x%08" PRIX32 " %s\n", formatId, formatName);
86 }
87
88 if (1)
89 {
90 const char* name = TEST_CLIP_BMP;
91 BOOL bSuccess = FALSE;
92 UINT32 idBmp = ClipboardRegisterFormat(clipboard, "image/bmp");
93
94 wImage* img = winpr_image_new();
95 if (!img)
96 goto fail;
97
98 if (winpr_image_read(img, name) <= 0)
99 {
100 winpr_image_free(img, TRUE);
101 goto fail;
102 }
103
104 size_t bmpsize = 0;
105 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_BITMAP, &bmpsize);
106 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
107 (void)fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
108
109 free(data);
110 winpr_image_free(img, TRUE);
111 if (!bSuccess)
112 goto fail;
113
114 {
115 UINT32 id = CF_DIB;
116
117 UINT32 DstSize = 0;
118 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
119 (void)fprintf(stderr, "ClipboardGetData: [CF_DIB] %p [%" PRIu32 "]\n", pDstData,
120 DstSize);
121 if (!pDstData)
122 goto fail;
123 bSuccess = ClipboardSetData(clipboard, id, pDstData, DstSize);
124 free(pDstData);
125 if (!bSuccess)
126 goto fail;
127 }
128 {
129 const uint32_t id = ClipboardGetFormatId(clipboard, "HTML Format");
130 UINT32 DstSize = 0;
131 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
132 if (!pDstData)
133 goto fail;
134 {
135 FILE* fp = fopen("test.html", "w");
136 if (fp)
137 {
138 (void)fwrite(pDstData, 1, DstSize, fp);
139 (void)fclose(fp);
140 }
141 }
142 free(pDstData);
143 }
144 {
145 UINT32 id = ClipboardRegisterFormat(clipboard, "image/bmp");
146
147 UINT32 DstSize = 0;
148 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
149 (void)fprintf(stderr, "ClipboardGetData: [image/bmp] %p [%" PRIu32 "]\n", pDstData,
150 DstSize);
151 if (!pDstData)
152 goto fail;
153 free(pDstData);
154 if (DstSize != bmpsize)
155 goto fail;
156 }
157
158#if defined(WINPR_UTILS_IMAGE_PNG)
159 {
160 UINT32 id = ClipboardRegisterFormat(clipboard, "image/png");
161
162 UINT32 DstSize = 0;
163 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
164 (void)fprintf(stderr, "ClipboardGetData: [image/png] %p\n", pDstData);
165 if (!pDstData)
166 goto fail;
167 free(pDstData);
168 }
169 {
170 const char* name = TEST_CLIP_PNG;
171 BOOL bSuccess = FALSE;
172 UINT32 idBmp = ClipboardRegisterFormat(clipboard, "image/png");
173
174 wImage* img = winpr_image_new();
175 if (!img)
176 goto fail;
177
178 if (winpr_image_read(img, name) <= 0)
179 {
180 winpr_image_free(img, TRUE);
181 goto fail;
182 }
183
184 size_t bmpsize = 0;
185 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_PNG, &bmpsize);
186 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
187 (void)fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
188
189 free(data);
190 winpr_image_free(img, TRUE);
191 if (!bSuccess)
192 goto fail;
193 }
194 {
195 UINT32 id = CF_DIB;
196
197 UINT32 DstSize = 0;
198 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
199 (void)fprintf(stderr, "ClipboardGetData: [CF_DIB] %p [%" PRIu32 "]\n", pDstData,
200 DstSize);
201 if (!pDstData)
202 goto fail;
203 bSuccess = ClipboardSetData(clipboard, id, pDstData, DstSize);
204 free(pDstData);
205 if (!bSuccess)
206 goto fail;
207 }
208#endif
209
210#if defined(WINPR_UTILS_IMAGE_WEBP)
211 {
212 UINT32 id = ClipboardRegisterFormat(clipboard, "image/webp");
213
214 UINT32 DstSize = 0;
215 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
216 (void)fprintf(stderr, "ClipboardGetData: [image/webp] %p\n", pDstData);
217 if (!pDstData)
218 goto fail;
219 free(pDstData);
220 }
221#endif
222
223#if defined(WINPR_UTILS_IMAGE_JPEG)
224 {
225 UINT32 id = ClipboardRegisterFormat(clipboard, "image/jpeg");
226
227 UINT32 DstSize = 0;
228 void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
229 (void)fprintf(stderr, "ClipboardGetData: [image/jpeg] %p\n", pDstData);
230 if (!pDstData)
231 goto fail;
232 free(pDstData);
233 }
234#endif
235 }
236
237 rc = 0;
238
239fail:
240 free(pFormatIds);
241 ClipboardDestroy(clipboard);
242 return rc;
243}