Update SDL_HasClipboardText functions to return value based on clipboard content; Fix memory leak in fallback SetClipboard implementation
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Sat, 29 Oct 2011 23:43:59 -0700
changeset 6036f2a89f2a6157
parent 6035 9494ca1b6cf5
child 6037 2feab4874268
Update SDL_HasClipboardText functions to return value based on clipboard content; Fix memory leak in fallback SetClipboard implementation
include/SDL_clipboard.h
src/video/SDL_clipboard.c
src/video/bwindow/SDL_bclipboard.cc
src/video/cocoa/SDL_cocoaclipboard.m
src/video/windows/SDL_windowsclipboard.c
src/video/x11/SDL_x11clipboard.c
     1.1 --- a/include/SDL_clipboard.h	Sat Oct 29 23:34:19 2011 -0700
     1.2 +++ b/include/SDL_clipboard.h	Sat Oct 29 23:43:59 2011 -0700
     1.3 @@ -55,7 +55,7 @@
     1.4  extern DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
     1.5  
     1.6  /**
     1.7 - * \brief Returns whether the clipboard has text
     1.8 + * \brief Returns a flag indicating whether the clipboard exists and contains a text string that it non-empty
     1.9   *
    1.10   * \sa SDL_GetClipboardText()
    1.11   */
     2.1 --- a/src/video/SDL_clipboard.c	Sat Oct 29 23:34:19 2011 -0700
     2.2 +++ b/src/video/SDL_clipboard.c	Sat Oct 29 23:43:59 2011 -0700
     2.3 @@ -35,6 +35,9 @@
     2.4      if (_this->SetClipboardText) {
     2.5          return _this->SetClipboardText(_this, text);
     2.6      } else {
     2.7 +        if (_this->clipboard_text) {
     2.8 +            SDL_free(_this->clipboard_text);
     2.9 +        }
    2.10          _this->clipboard_text = SDL_strdup(text);
    2.11          return 0;
    2.12      }
    2.13 @@ -64,7 +67,7 @@
    2.14      if (_this->HasClipboardText) {
    2.15          return _this->HasClipboardText(_this);
    2.16      } else {
    2.17 -        if (_this->clipboard_text) {
    2.18 +        if ((_this->clipboard_text) && (SDL_strlen(_this->clipboard_text)>0)) {
    2.19              return SDL_TRUE;
    2.20          } else {
    2.21              return SDL_FALSE;
     3.1 --- a/src/video/bwindow/SDL_bclipboard.cc	Sat Oct 29 23:34:19 2011 -0700
     3.2 +++ b/src/video/bwindow/SDL_bclipboard.cc	Sat Oct 29 23:43:59 2011 -0700
     3.3 @@ -51,8 +51,9 @@
     3.4  
     3.5  char *BE_GetClipboardText(_THIS) {
     3.6  	BMessage *clip = NULL;
     3.7 -	const char *text;
     3.8 +	const char *text = NULL;	
     3.9  	ssize_t length;
    3.10 +	char *result;
    3.11  	if(be_clipboard->Lock()) {
    3.12  		if((clip = be_clipboard->Data())) {
    3.13  			/* Presumably the string of characters is ascii-format */
    3.14 @@ -60,37 +61,29 @@
    3.15  				&length);
    3.16  		} else {
    3.17  			be_clipboard->Unlock();
    3.18 -			return NULL;
    3.19  		}
    3.20  		be_clipboard->Unlock();
    3.21 +	} 
    3.22 +	
    3.23 +	if (!text) {
    3.24 +		result = SDL_strdup("");
    3.25  	} else {
    3.26 -		return NULL;
    3.27 +		/* Copy the data and pass on to SDL */
    3.28 +		result = (char*)SDL_calloc(1, sizeof(char*)*length);
    3.29 +		SDL_strlcpy(result, text, length);
    3.30  	}
    3.31  	
    3.32 -	/* Copy the data and pass on to SDL */
    3.33 -	char *result = (char*)SDL_calloc(1, sizeof(char*)*length);
    3.34 -	SDL_strlcpy(result, text, length);
    3.35 -	
    3.36  	return result;
    3.37  }
    3.38  
    3.39  SDL_bool BE_HasClipboardText(_THIS) {
    3.40 -	BMessage *clip = NULL;
    3.41 -	const char *text;
    3.42 -	ssize_t length;
    3.43 -	SDL_bool retval = SDL_FALSE;
    3.44 -	
    3.45 -	if(be_clipboard->Lock()) {
    3.46 -		if((clip = be_clipboard->Data())) {
    3.47 -			/* Presumably the string of characters is ascii-format */
    3.48 -			clip->FindData("text/plain", B_MIME_TYPE, (const void**)&text,
    3.49 -				&length);
    3.50 -			if( text ) retval = SDL_TRUE;
    3.51 -		}
    3.52 -		be_clipboard->Unlock();
    3.53 -	}
    3.54 -	return retval;
    3.55 -
    3.56 +	SDL_bool result = SDL_FALSE;
    3.57 +	char *text = BE_GetClipboardText(_this);
    3.58 +	if (text) {
    3.59 +		result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
    3.60 +		SDL_free(text);
    3.61 +	} 
    3.62 +	return result;
    3.63  }
    3.64  
    3.65  #ifdef __cplusplus
     4.1 --- a/src/video/cocoa/SDL_cocoaclipboard.m	Sat Oct 29 23:34:19 2011 -0700
     4.2 +++ b/src/video/cocoa/SDL_cocoaclipboard.m	Sat Oct 29 23:43:59 2011 -0700
     4.3 @@ -94,24 +94,12 @@
     4.4  SDL_bool
     4.5  Cocoa_HasClipboardText(_THIS)
     4.6  {
     4.7 -    NSAutoreleasePool *pool;
     4.8 -    NSPasteboard *pasteboard;
     4.9 -    NSString *format = GetTextFormat(_this);
    4.10 -    NSString *available;
    4.11 -    SDL_bool result;
    4.12 -
    4.13 -    pool = [[NSAutoreleasePool alloc] init];
    4.14 -
    4.15 -    pasteboard = [NSPasteboard generalPasteboard];
    4.16 -    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    4.17 -    if ([available isEqualToString:format]) {
    4.18 -        result = SDL_TRUE;
    4.19 -    } else {
    4.20 -        result = SDL_FALSE;
    4.21 +    SDL_bool result = SDL_FALSE;
    4.22 +    char *text = Cocoa_GetClipboardText(_this);
    4.23 +    if (text) {
    4.24 +	result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
    4.25 +	SDL_free(text);
    4.26      }
    4.27 -
    4.28 -    [pool release];
    4.29 -
    4.30      return result;
    4.31  }
    4.32  
     5.1 --- a/src/video/windows/SDL_windowsclipboard.c	Sat Oct 29 23:34:19 2011 -0700
     5.2 +++ b/src/video/windows/SDL_windowsclipboard.c	Sat Oct 29 23:43:59 2011 -0700
     5.3 @@ -136,11 +136,13 @@
     5.4  SDL_bool
     5.5  WIN_HasClipboardText(_THIS)
     5.6  {
     5.7 -    if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
     5.8 -        return SDL_TRUE;
     5.9 -    } else {
    5.10 -        return SDL_FALSE;
    5.11 -    }
    5.12 +    SDL_bool result = SDL_FALSE;
    5.13 +    char *text = WIN_GetClipboardText(_this);
    5.14 +    if (text) {
    5.15 +        result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
    5.16 +        SDL_free(text);
    5.17 +    } 
    5.18 +    return result;
    5.19  }
    5.20  
    5.21  void
     6.1 --- a/src/video/x11/SDL_x11clipboard.c	Sat Oct 29 23:34:19 2011 -0700
     6.2 +++ b/src/video/x11/SDL_x11clipboard.c	Sat Oct 29 23:43:59 2011 -0700
     6.3 @@ -129,25 +129,20 @@
     6.4      if (!text) {
     6.5          text = SDL_strdup("");
     6.6      }
     6.7 +    
     6.8      return text;
     6.9  }
    6.10  
    6.11  SDL_bool
    6.12  X11_HasClipboardText(_THIS)
    6.13  {
    6.14 -    /* Not an easy way to tell with X11, as far as I know... */
    6.15 -    char *text;
    6.16 -    SDL_bool retval;
    6.17 -
    6.18 -    text = X11_GetClipboardText(_this);
    6.19 -    if (*text) {
    6.20 -        retval = SDL_TRUE;
    6.21 -    } else {
    6.22 -        retval = SDL_FALSE;
    6.23 -    }
    6.24 -    SDL_free(text);
    6.25 -
    6.26 -    return retval;
    6.27 +    SDL_bool result = SDL_FALSE;
    6.28 +    char *text = X11_GetClipboardText(_this);
    6.29 +    if (text) {
    6.30 +        result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
    6.31 +        SDL_free(text);
    6.32 +    }    
    6.33 +    return result;
    6.34  }
    6.35  
    6.36  #endif /* SDL_VIDEO_DRIVER_X11 */