FreeRDP
TestClipboardFormats.c
1 
2 #include <winpr/crt.h>
3 #include <winpr/print.h>
4 #include <winpr/image.h>
5 #include <winpr/clipboard.h>
6 
7 int 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  UINT32 id = ClipboardRegisterFormat(clipboard, "image/bmp");
130 
131  UINT32 DstSize = 0;
132  void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
133  (void)fprintf(stderr, "ClipboardGetData: [image/bmp] %p [%" PRIu32 "]\n", pDstData,
134  DstSize);
135  if (!pDstData)
136  goto fail;
137  free(pDstData);
138  if (DstSize != bmpsize)
139  goto fail;
140  }
141 
142 #if defined(WINPR_UTILS_IMAGE_PNG)
143  {
144  UINT32 id = ClipboardRegisterFormat(clipboard, "image/png");
145 
146  UINT32 DstSize = 0;
147  void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
148  (void)fprintf(stderr, "ClipboardGetData: [image/png] %p\n", pDstData);
149  if (!pDstData)
150  goto fail;
151  free(pDstData);
152  }
153  {
154  const char* name = TEST_CLIP_PNG;
155  BOOL bSuccess = FALSE;
156  UINT32 idBmp = ClipboardRegisterFormat(clipboard, "image/png");
157 
158  wImage* img = winpr_image_new();
159  if (!img)
160  goto fail;
161 
162  if (winpr_image_read(img, name) <= 0)
163  {
164  winpr_image_free(img, TRUE);
165  goto fail;
166  }
167 
168  size_t bmpsize = 0;
169  void* data = winpr_image_write_buffer(img, WINPR_IMAGE_PNG, &bmpsize);
170  bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
171  (void)fprintf(stderr, "ClipboardSetData: %" PRId32 "\n", bSuccess);
172 
173  free(data);
174  winpr_image_free(img, TRUE);
175  if (!bSuccess)
176  goto fail;
177  }
178  {
179  UINT32 id = CF_DIB;
180 
181  UINT32 DstSize = 0;
182  void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
183  (void)fprintf(stderr, "ClipboardGetData: [CF_DIB] %p [%" PRIu32 "]\n", pDstData,
184  DstSize);
185  if (!pDstData)
186  goto fail;
187  bSuccess = ClipboardSetData(clipboard, id, pDstData, DstSize);
188  free(pDstData);
189  if (!bSuccess)
190  goto fail;
191  }
192 #endif
193 
194 #if defined(WINPR_UTILS_IMAGE_WEBP)
195  {
196  UINT32 id = ClipboardRegisterFormat(clipboard, "image/webp");
197 
198  UINT32 DstSize = 0;
199  void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
200  (void)fprintf(stderr, "ClipboardGetData: [image/webp] %p\n", pDstData);
201  if (!pDstData)
202  goto fail;
203  free(pDstData);
204  }
205 #endif
206 
207 #if defined(WINPR_UTILS_IMAGE_JPEG)
208  {
209  UINT32 id = ClipboardRegisterFormat(clipboard, "image/jpeg");
210 
211  UINT32 DstSize = 0;
212  void* pDstData = ClipboardGetData(clipboard, id, &DstSize);
213  (void)fprintf(stderr, "ClipboardGetData: [image/jpeg] %p\n", pDstData);
214  if (!pDstData)
215  goto fail;
216  free(pDstData);
217  }
218 #endif
219  }
220 
221  rc = 0;
222 
223 fail:
224  free(pFormatIds);
225  ClipboardDestroy(clipboard);
226  return rc;
227 }