Simplified clipboard API for sanity's sake.
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Jul 2010 23:54:03 -0700
changeset 4495dbbfdb9ea716
parent 4494 621dc91c7a04
child 4496 aea9e96d7973
Simplified clipboard API for sanity's sake.

A complete clipboard implementation would support multiple formats that could be queried at runtime, events for when the clipboard contents changed, support for HTML, images, etc. We're not going that crazy, at least for now. :)
include/SDL_clipboard.h
src/video/SDL_clipboard.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
     1.1 --- a/include/SDL_clipboard.h	Wed Jul 07 23:26:07 2010 -0700
     1.2 +++ b/include/SDL_clipboard.h	Wed Jul 07 23:54:03 2010 -0700
     1.3 @@ -30,7 +30,6 @@
     1.4  #define _SDL_clipboard_h
     1.5  
     1.6  #include "SDL_stdinc.h"
     1.7 -#include "SDL_surface.h"
     1.8  
     1.9  #include "begin_code.h"
    1.10  /* Set up for C function definitions, even when using C++ */
    1.11 @@ -43,14 +42,14 @@
    1.12  /* Function prototypes */
    1.13  
    1.14  /**
    1.15 - * \brief Put text into the clipboard
    1.16 + * \brief Put UTF-8 text into the clipboard
    1.17   *
    1.18   * \sa SDL_GetClipboardText()
    1.19   */
    1.20  extern DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text);
    1.21  
    1.22  /**
    1.23 - * \brief Get text from the clipboard, which must be freed with SDL_free()
    1.24 + * \brief Get UTF-8 text from the clipboard, which must be freed with SDL_free()
    1.25   *
    1.26   * \sa SDL_SetClipboardText()
    1.27   */
    1.28 @@ -63,54 +62,6 @@
    1.29   */
    1.30  extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText();
    1.31  
    1.32 -/**
    1.33 - * \brief Put an image into the clipboard
    1.34 - *
    1.35 - * \sa SDL_GetClipboardImage()
    1.36 - */
    1.37 -extern DECLSPEC int SDLCALL SDL_SetClipboardImage(SDL_Surface *image);
    1.38 -
    1.39 -/**
    1.40 - * \brief Get image from the clipboard, which must be freed with SDL_FreeSurface()
    1.41 - *
    1.42 - * \sa SDL_SetClipboard()
    1.43 - */
    1.44 -extern DECLSPEC SDL_Surface * SDLCALL SDL_GetClipboardImage();
    1.45 -
    1.46 -/**
    1.47 - * \brief Returns whether the clipboard has data in the specified format
    1.48 - *
    1.49 - * \sa SDL_GetClipboardImage()
    1.50 - */
    1.51 -extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardImage();
    1.52 -
    1.53 -/**
    1.54 - * \brief Set the data in the clipboard in the specified format
    1.55 - *
    1.56 - * \sa SDL_GetClipboard()
    1.57 - */
    1.58 -extern DECLSPEC int SDLCALL SDL_SetClipboard(Uint32 format, void *data, size_t length);
    1.59 -
    1.60 -/**
    1.61 - * \brief Get the data in the clipboard in the specified format, which must be
    1.62 - *        freed with SDL_free()
    1.63 - *
    1.64 - * \sa SDL_SetClipboard()
    1.65 - */
    1.66 -extern DECLSPEC int SDLCALL SDL_GetClipboard(Uint32 format, void **data, size_t *length);
    1.67 -
    1.68 -/**
    1.69 - * \brief Returns whether the clipboard has data in the specified format
    1.70 - *
    1.71 - * \sa SDL_GetClipboard()
    1.72 - */
    1.73 -extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardFormat(Uint32 format);
    1.74 -
    1.75 -/**
    1.76 - * \brief Clear any data out of the clipboard, if possible.
    1.77 - */
    1.78 -extern DECLSPEC void SDLCALL SDL_ClearClipboard(void);
    1.79 -
    1.80  
    1.81  /* Ends C function definitions when using C++ */
    1.82  #ifdef __cplusplus
     2.1 --- a/src/video/SDL_clipboard.c	Wed Jul 07 23:26:07 2010 -0700
     2.2 +++ b/src/video/SDL_clipboard.c	Wed Jul 07 23:54:03 2010 -0700
     2.3 @@ -24,78 +24,50 @@
     2.4  #include "SDL_clipboard.h"
     2.5  #include "SDL_sysvideo.h"
     2.6  
     2.7 -/* FOURCC values for text and image clipboard formats */
     2.8 -#define TEXT_DATA  SDL_FOURCC('T', 'E', 'X', 'T')
     2.9 -#define IMAGE_DATA SDL_FOURCC('B', 'M', 'P', ' ')
    2.10  
    2.11  int
    2.12  SDL_SetClipboardText(const char *text)
    2.13  {
    2.14 -    return SDL_SetClipboard(TEXT_DATA, text, SDL_strlen(text)+1);
    2.15 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    2.16 +
    2.17 +    if (_this->SetClipboardText) {
    2.18 +        return _this->SetClipboardText(_this, text);
    2.19 +    } else {
    2.20 +        _this->clipboard_text = SDL_strdup(text);
    2.21 +        return 0;
    2.22 +    }
    2.23  }
    2.24  
    2.25  char *
    2.26  SDL_GetClipboardText()
    2.27  {
    2.28 -    void *data;
    2.29 -    size_t length;
    2.30 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    2.31  
    2.32 -    if (SDL_GetClipboard(TEXT_DATA, &data, &length) == 0) {
    2.33 -        return SDL_static_cast(char*, data);
    2.34 +    if (_this->GetClipboardText) {
    2.35 +        return _this->GetClipboardText(_this);
    2.36      } else {
    2.37 -        return NULL;
    2.38 +        const char *text = _this->clipboard_text;
    2.39 +        if (!text) {
    2.40 +            text = "";
    2.41 +        }
    2.42 +        return SDL_strdup(text);
    2.43      }
    2.44  }
    2.45  
    2.46  SDL_bool
    2.47  SDL_HasClipboardText()
    2.48  {
    2.49 -    return SDL_HasClipboardFormat(TEXT_DATA);
    2.50 -}
    2.51 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    2.52  
    2.53 -int
    2.54 -SDL_SetClipboardImage(SDL_Surface *image)
    2.55 -{
    2.56 -    SDL_Unsupported();
    2.57 -    return -1;
    2.58 -}
    2.59 -
    2.60 -SDL_Surface *
    2.61 -SDL_GetClipboardImage()
    2.62 -{
    2.63 -    SDL_Unsupported();
    2.64 -    return NULL;
    2.65 -}
    2.66 -
    2.67 -SDL_bool
    2.68 -SDL_HasClipboardImage()
    2.69 -{
    2.70 -    return SDL_FALSE;
    2.71 -}
    2.72 -
    2.73 -int
    2.74 -SDL_SetClipboard(Uint32 format, void *data, size_t length)
    2.75 -{
    2.76 -    SDL_Unsupported();
    2.77 -    return -1;
    2.78 -}
    2.79 -
    2.80 -int
    2.81 -SDL_GetClipboard(Uint32 format, void **data, size_t *length)
    2.82 -{
    2.83 -    SDL_Unsupported();
    2.84 -    return -1;
    2.85 -}
    2.86 -
    2.87 -SDL_bool
    2.88 -SDL_HasClipboardFormat(Uint32 format)
    2.89 -{
    2.90 -    return SDL_FALSE;
    2.91 -}
    2.92 -
    2.93 -void
    2.94 -SDL_ClearClipboard(void)
    2.95 -{
    2.96 +    if (_this->HasClipboardText) {
    2.97 +        return _this->HasClipboardText(_this);
    2.98 +    } else {
    2.99 +        if (_this->clipboard_text) {
   2.100 +            return SDL_TRUE;
   2.101 +        } else {
   2.102 +            return SDL_FALSE;
   2.103 +        }
   2.104 +    }
   2.105  }
   2.106  
   2.107  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/SDL_sysvideo.h	Wed Jul 07 23:26:07 2010 -0700
     3.2 +++ b/src/video/SDL_sysvideo.h	Wed Jul 07 23:54:03 2010 -0700
     3.3 @@ -303,6 +303,11 @@
     3.4      void (*StopTextInput) (_THIS);
     3.5      void (*SetTextInputRect) (_THIS, SDL_Rect *rect);
     3.6  
     3.7 +    /* Clipboard */
     3.8 +    int (*SetClipboardText) (_THIS, const char *text);
     3.9 +    char * (*GetClipboardText) (_THIS);
    3.10 +    SDL_bool (*HasClipboardText) (_THIS);
    3.11 +
    3.12      /* * * */
    3.13      /* Data common to all drivers */
    3.14      SDL_bool suspend_screensaver;
    3.15 @@ -312,6 +317,7 @@
    3.16      Uint8 window_magic;
    3.17      Uint8 texture_magic;
    3.18      Uint32 next_object_id;
    3.19 +    char * clipboard_text;
    3.20  
    3.21      /* * * */
    3.22      /* Data used by the GL drivers */
     4.1 --- a/src/video/SDL_video.c	Wed Jul 07 23:26:07 2010 -0700
     4.2 +++ b/src/video/SDL_video.c	Wed Jul 07 23:54:03 2010 -0700
     4.3 @@ -2833,6 +2833,10 @@
     4.4          SDL_free(_this->displays);
     4.5          _this->displays = NULL;
     4.6      }
     4.7 +    if (_this->clipboard_text) {
     4.8 +        SDL_free(_this->clipboard_text);
     4.9 +        _this->clipboard_text = NULL;
    4.10 +    }
    4.11      _this->free(_this);
    4.12      _this = NULL;
    4.13  }