int pygame_scrap_put (char *type, int srclen, char *src) { UINT format; int nulledlen = srclen + 1; HANDLE hMem; if (!pygame_scrap_initialized ()) { PyErr_SetString (PyExc_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; }
int scrap_put_win (char *type, char *data, unsigned int size) { UINT format; int nulledlen = size + 1; HANDLE hMem; format = _convert_internal_type (type); if (format == (UINT)-1) format = _convert_format (type); if (!OpenClipboard (_sdlwindow)) { SDL_SetError ("could not access clipboard"); return -1; /* 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, data + sizeof (BITMAPFILEHEADER), nulledlen - 1); else memcpy (dst, data, size); 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. */ SDL_SetError ("could not acquire the memory pointer"); CloseClipboard (); return -1; } CloseClipboard (); return 1; }
int scrap_get_win (char *type, char **data, unsigned int *size) { UINT format = _convert_format (type); if (!OpenClipboard (_sdlwindow)) { SDL_SetError ("could not access clipboard"); return -1; } if (!IsClipboardFormatAvailable (format)) { /* The format was not found - was it a mapped type? */ format = _convert_internal_type (type); if (format == (UINT)-1) { CloseClipboard (); SDL_SetError ("no matching format on clipboard found"); return -1; } } if (IsClipboardFormatAvailable (format)) { HANDLE hMem; char *src = NULL; int retval = 0; hMem = GetClipboardData (format); if (hMem) { *size = 0; /* CF_BITMAP is not a global, so do not lock it. */ if (format != CF_BITMAP) { src = GlobalLock (hMem); if (!src) { CloseClipboard (); SDL_SetError ("could not acquire the memory pointer"); return -1; } *size = GlobalSize (hMem); } if (format == CF_DIB || format == CF_DIBV5) { /* size will be increased accordingly in * _create_dib_buffer. */ *data = _create_dib_buffer (src, size); retval = 1; } else if (*size != 0) { *data = malloc (*size); if (*data) { memset (*data, 0, *size); memcpy (*data, src, *size); retval = 1; } else { SDL_SetError ("could not allocate memory"); retval = -1; } } GlobalUnlock (hMem); CloseClipboard (); return retval; } } CloseClipboard (); return 0; }
int scrap_contains_win (char *type) { return IsClipboardFormatAvailable (_convert_format (type)); }
int scrap_put_x11 (char *type, char *data, unsigned int size) { Atom clip, cliptype; Time timestamp = CurrentTime; time_t start; XEvent ev; Lock_Display (); clip = GET_CLIPATOM (_currentmode); cliptype = _convert_format (type); /* We've some types which should not be set by the user. */ if (cliptype == _atom_TARGETS || cliptype == _atom_SDL || cliptype == _atom_TIMESTAMP) { SDL_SetError ("the requested format type is reserved"); Unlock_Display (); return -1; } /* Update the clipboard property with the buffer. */ XChangeProperty (_sdldisplay, _sdlwindow, clip, cliptype, 8, PropModeReplace, (unsigned char *) data, (int) size); _add_clip_data (cliptype, data, size); if (cliptype == _atom_MIME_PLAIN) { /* Set SCRAP_FORMAT_TEXT. Also set XA_STRING, TEXT and * UTF8_STRING if they are not set in the dictionary. */ _add_clip_data (XA_STRING, data, size); _add_clip_data (_atom_UTF8, data, size); _add_clip_data (_atom_TEXT, data, size); } XSync (_sdldisplay, False); /* Update the timestamp */ for (start = time (0);;) { if (XCheckTypedWindowEvent (_sdldisplay, _sdlwindow, PropertyNotify, &ev)) break; if (time (0) - start >= 5) { /* Timeout, damn. */ Unlock_Display (); goto SETSELECTIONOWNER; } } if (ev.xproperty.atom == clip) { timestamp = ev.xproperty.time; if (cliptype == XA_PRIMARY) _selectiontime = timestamp; else _cliptime = timestamp; } else timestamp = (cliptype == XA_PRIMARY) ? _selectiontime : _cliptime; SETSELECTIONOWNER: /* Set the selection owner to the own window. */ XSetSelectionOwner (_sdldisplay, clip, _sdlwindow, timestamp); if (XGetSelectionOwner (_sdldisplay, clip) != _sdlwindow) { /* Ouch, we could not toggle the selection owner. Raise an * error, as it's not guaranteed, that the clipboard * contains valid data. */ Unlock_Display (); SDL_SetError ("could not set the proper owner for the clipboard"); return -1; } Unlock_Display (); return 1; }
int scrap_get_x11 (char *type, char **data, unsigned int *size) { return _get_data_as (GET_CLIPATOM (_currentmode), _convert_format (type), data, size); }
int pygame_scrap_contains (char *type) { return IsClipboardFormatAvailable (_convert_format(type)); }
char* pygame_scrap_get (char *type, unsigned long *count) { UINT format = _convert_format (type); char *retval = NULL; if (!pygame_scrap_initialized ()) { PyErr_SetString (PyExc_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; }