1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*
pygame - Python Game Library
Copyright (C) 2006, 2007 Rene Dudfield, Marcus von Appen
Originally written and put in the public domain by Sam Lantinga.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Windows.h>
#if !defined(CF_DIBV5)
/* Missing from the MinGW win32api-3.11 winuser.h header */
#define CF_DIBV5 17
#endif
static HWND SDL_Window;
#define MAX_CHUNK_SIZE INT_MAX
static UINT _format_MIME_PLAIN;
/**
* \brief Converts the passed type into a system specific clipboard type
* to use for the clipboard.
*
* \param type The type to convert.
* \return A system specific type.
*/
static UINT
_convert_format(char *type)
{
return RegisterClipboardFormat(type);
}
/**
* \brief Gets a system specific clipboard format type for a certain type.
*
* \param type The name of the format to get the mapped format type for.
* \return The format type or -1 if no such type was found.
*/
static UINT
_convert_internal_type(char *type)
{
if (strcmp(type, PYGAME_SCRAP_TEXT) == 0)
return CF_TEXT;
if (strcmp(type, "text/plain;charset=utf-8") == 0)
return CF_UNICODETEXT;
if (strcmp(type, "image/tiff") == 0)
return CF_TIFF;
if (strcmp(type, PYGAME_SCRAP_BMP) == 0)
return CF_DIB;
if (strcmp(type, "audio/wav") == 0)
return CF_WAVE;
return -1;
}
/**
* \brief Looks up the name for the specific clipboard format type.
*
* \param format The format to get the name for.
* \param buf The buffer to copy the name into.
* \param size The size of the buffer.
* \return The length of the format name.
*/
static int
_lookup_clipboard_format(UINT format, char *buf, int size)
{
int len;
char *cpy;
memset(buf, 0, size);
switch (format) {
case CF_TEXT:
len = strlen(PYGAME_SCRAP_TEXT);
cpy = PYGAME_SCRAP_TEXT;
break;
case CF_UNICODETEXT:
len = 24;
cpy = "text/plain;charset=utf-8";
break;
case CF_TIFF:
len = 10;
cpy = "image/tiff";
break;
case CF_DIB:
len = strlen(PYGAME_SCRAP_BMP);
cpy = PYGAME_SCRAP_BMP;
break;
case CF_WAVE:
len = 9;
cpy = "audio/wav";
break;
default:
len = GetClipboardFormatName(format, buf, size);
return len;
}
if (len != 0)
memcpy(buf, cpy, len);
return len;
}
/**
* \brief Creates a BMP character buffer with all headers from a DIB
* HANDLE. The caller has to free the returned buffer.
* \param data The DIB handle data.
* \param count The size of the DIB handle.
* \return The character buffer containing the BMP information.
*/
static char *
_create_dib_buffer(char *data, unsigned long *count)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER bihdr;
char *buf;
if (!data)
return NULL;
bihdr = (LPBITMAPINFOHEADER)data;
/* Create the BMP header. */
hdr.bfType = 'M' << 8 | 'B'; /* Specs say, it is always BM */
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfSize =
(DWORD)(sizeof(BITMAPFILEHEADER) + bihdr->biSize +
bihdr->biClrUsed * sizeof(RGBQUAD) + bihdr->biSizeImage);
hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + bihdr->biSize +
bihdr->biClrUsed * sizeof(RGBQUAD));
/* Copy both to the buffer. */
buf = malloc(sizeof(hdr) + (*count));
if (!buf)
return NULL;
memcpy(buf, &hdr, sizeof(hdr));
memcpy(buf + sizeof(BITMAPFILEHEADER), data, *count);
/* Increase count for the correct size. */
*count += sizeof(hdr);
return buf;
}
int
pygame_scrap_init(void)
{
SDL_SysWMinfo info;
int retval = 0;
/* Grab the window manager specific information */
SDL_SetError("SDL is not running on known window manager");
SDL_VERSION(&info.version);
if (SDL_GetWMInfo(&info)) {
/* Save the information for later use */
SDL_Window = info.window;
retval = 1;
}
if (retval)
_scrapinitialized = 1;
_format_MIME_PLAIN = RegisterClipboardFormat(PYGAME_SCRAP_TEXT);
return retval;
}
int
pygame_scrap_lost(void)
{
if (!pygame_scrap_initialized()) {
PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
return 0;
}
return (GetClipboardOwner() != SDL_Window);
}
int
pygame_scrap_put(char *type, int srclen, char *src)
{
UINT format;
int nulledlen = srclen + 1;
HANDLE hMem;
if (!pygame_scrap_initialized()) {
PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
return 0;
}
format = _convert_internal_type(type);
if (format == -1)
format = _convert_format(type);
if (!OpenClipboard(SDL_Window))
return 0; /* Could not open the clipboard. */
if (format == CF_DIB || format == CF_DIBV5)
nulledlen -= sizeof(BITMAPFILEHEADER); /* We won't copy the header */
hMem = GlobalAlloc((GMEM_MOVEABLE | GMEM_DDESHARE), nulledlen);
if (hMem) {
char *dst = GlobalLock(hMem);
memset(dst, 0, nulledlen);
if (format == CF_DIB || format == CF_DIBV5)
memcpy(dst, src + sizeof(BITMAPFILEHEADER), nulledlen - 1);
else
memcpy(dst, src, srclen);
GlobalUnlock(hMem);
EmptyClipboard();
SetClipboardData(format, hMem);
if (format == _format_MIME_PLAIN) {
/* Setting SCRAP_TEXT, also set CF_TEXT. */
SetClipboardData(CF_TEXT, hMem);
}
}
else {
/* Could not access the clipboard, raise an error. */
CloseClipboard();
return 0;
}
CloseClipboard();
return 1;
}
char *
pygame_scrap_get(char *type, unsigned long *count)
{
UINT format = _convert_format(type);
char *retval = NULL;
if (!pygame_scrap_initialized()) {
PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
return NULL;
}
if (!pygame_scrap_lost())
return Bytes_AsString(PyDict_GetItemString(_clipdata, type));
if (!OpenClipboard(SDL_Window))
return NULL;
if (!IsClipboardFormatAvailable(format)) {
/* The format was not found - was it a mapped type? */
format = _convert_internal_type(type);
if (format == -1) {
CloseClipboard();
return NULL;
}
}
if (IsClipboardFormatAvailable(format)) {
HANDLE hMem;
char *src;
src = NULL;
hMem = GetClipboardData(format);
if (hMem) {
*count = 0;
/* CF_BITMAP is not a global, so do not lock it. */
if (format != CF_BITMAP) {
src = GlobalLock(hMem);
if (!src) {
CloseClipboard();
return NULL;
}
*count = GlobalSize(hMem);
}
if (format == CF_DIB || format == CF_DIBV5) {
/* Count will be increased accordingly in
* _create_dib_buffer.
*/
src = _create_dib_buffer(src, count);
GlobalUnlock(hMem);
CloseClipboard();
return src;
}
else if (*count != 0) {
/* weird error, shouldn't get here. */
if (!src) {
return NULL;
}
retval = malloc(*count);
if (retval) {
memset(retval, 0, *count);
memcpy(retval, src, *count);
}
}
GlobalUnlock(hMem);
}
}
CloseClipboard();
return retval;
}
char **
pygame_scrap_get_types(void)
{
UINT format = 0;
char **types = NULL;
char **tmptypes;
int i = 0;
int count = -1;
int len;
char tmp[100] = {'\0'};
int size = 0;
if (!OpenClipboard(SDL_Window))
return NULL;
size = CountClipboardFormats();
if (size == 0) {
CloseClipboard();
return NULL; /* No clipboard data. */
}
for (i = 0; i < size; i++) {
format = EnumClipboardFormats(format);
if (format == 0) {
/* Something wicked happened. */
while (i > 0)
free(types[i]);
free(types);
CloseClipboard();
return NULL;
}
/* No predefined name, get the (truncated) name. */
len = _lookup_clipboard_format(format, tmp, 100);
if (len == 0)
continue;
count++;
tmptypes = realloc(types, sizeof(char *) * (count + 1));
if (!tmptypes) {
while (count > 0) {
free(types[count]);
count--;
}
free(types);
CloseClipboard();
return NULL;
}
types = tmptypes;
types[count] = malloc(sizeof(char) * (len + 1));
if (!types[count]) {
while (count > 0) {
free(types[count]);
count--;
}
free(types);
CloseClipboard();
return NULL;
}
memset(types[count], 0, len + 1);
memcpy(types[count], tmp, len);
}
tmptypes = realloc(types, sizeof(char *) * (count + 1));
if (!tmptypes) {
while (count > 0) {
free(types[count]);
count--;
}
free(types);
CloseClipboard();
return NULL;
}
types = tmptypes;
types[count] = NULL;
CloseClipboard();
return types;
}
int
pygame_scrap_contains(char *type)
{
return IsClipboardFormatAvailable(_convert_format(type));
}
|