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 6036 f2a89f2a6157
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
--- a/include/SDL_clipboard.h	Sat Oct 29 23:34:19 2011 -0700
+++ b/include/SDL_clipboard.h	Sat Oct 29 23:43:59 2011 -0700
@@ -55,7 +55,7 @@
 extern DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
 
 /**
- * \brief Returns whether the clipboard has text
+ * \brief Returns a flag indicating whether the clipboard exists and contains a text string that it non-empty
  *
  * \sa SDL_GetClipboardText()
  */
--- a/src/video/SDL_clipboard.c	Sat Oct 29 23:34:19 2011 -0700
+++ b/src/video/SDL_clipboard.c	Sat Oct 29 23:43:59 2011 -0700
@@ -35,6 +35,9 @@
     if (_this->SetClipboardText) {
         return _this->SetClipboardText(_this, text);
     } else {
+        if (_this->clipboard_text) {
+            SDL_free(_this->clipboard_text);
+        }
         _this->clipboard_text = SDL_strdup(text);
         return 0;
     }
@@ -64,7 +67,7 @@
     if (_this->HasClipboardText) {
         return _this->HasClipboardText(_this);
     } else {
-        if (_this->clipboard_text) {
+        if ((_this->clipboard_text) && (SDL_strlen(_this->clipboard_text)>0)) {
             return SDL_TRUE;
         } else {
             return SDL_FALSE;
--- a/src/video/bwindow/SDL_bclipboard.cc	Sat Oct 29 23:34:19 2011 -0700
+++ b/src/video/bwindow/SDL_bclipboard.cc	Sat Oct 29 23:43:59 2011 -0700
@@ -51,8 +51,9 @@
 
 char *BE_GetClipboardText(_THIS) {
 	BMessage *clip = NULL;
-	const char *text;
+	const char *text = NULL;	
 	ssize_t length;
+	char *result;
 	if(be_clipboard->Lock()) {
 		if((clip = be_clipboard->Data())) {
 			/* Presumably the string of characters is ascii-format */
@@ -60,37 +61,29 @@
 				&length);
 		} else {
 			be_clipboard->Unlock();
-			return NULL;
 		}
 		be_clipboard->Unlock();
-	} else {
-		return NULL;
-	}
+	} 
 	
-	/* Copy the data and pass on to SDL */
-	char *result = (char*)SDL_calloc(1, sizeof(char*)*length);
-	SDL_strlcpy(result, text, length);
+	if (!text) {
+		result = SDL_strdup("");
+	} else {
+		/* Copy the data and pass on to SDL */
+		result = (char*)SDL_calloc(1, sizeof(char*)*length);
+		SDL_strlcpy(result, text, length);
+	}
 	
 	return result;
 }
 
 SDL_bool BE_HasClipboardText(_THIS) {
-	BMessage *clip = NULL;
-	const char *text;
-	ssize_t length;
-	SDL_bool retval = SDL_FALSE;
-	
-	if(be_clipboard->Lock()) {
-		if((clip = be_clipboard->Data())) {
-			/* Presumably the string of characters is ascii-format */
-			clip->FindData("text/plain", B_MIME_TYPE, (const void**)&text,
-				&length);
-			if( text ) retval = SDL_TRUE;
-		}
-		be_clipboard->Unlock();
-	}
-	return retval;
-
+	SDL_bool result = SDL_FALSE;
+	char *text = BE_GetClipboardText(_this);
+	if (text) {
+		result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
+		SDL_free(text);
+	} 
+	return result;
 }
 
 #ifdef __cplusplus
--- a/src/video/cocoa/SDL_cocoaclipboard.m	Sat Oct 29 23:34:19 2011 -0700
+++ b/src/video/cocoa/SDL_cocoaclipboard.m	Sat Oct 29 23:43:59 2011 -0700
@@ -94,24 +94,12 @@
 SDL_bool
 Cocoa_HasClipboardText(_THIS)
 {
-    NSAutoreleasePool *pool;
-    NSPasteboard *pasteboard;
-    NSString *format = GetTextFormat(_this);
-    NSString *available;
-    SDL_bool result;
-
-    pool = [[NSAutoreleasePool alloc] init];
-
-    pasteboard = [NSPasteboard generalPasteboard];
-    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
-    if ([available isEqualToString:format]) {
-        result = SDL_TRUE;
-    } else {
-        result = SDL_FALSE;
+    SDL_bool result = SDL_FALSE;
+    char *text = Cocoa_GetClipboardText(_this);
+    if (text) {
+	result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
+	SDL_free(text);
     }
-
-    [pool release];
-
     return result;
 }
 
--- a/src/video/windows/SDL_windowsclipboard.c	Sat Oct 29 23:34:19 2011 -0700
+++ b/src/video/windows/SDL_windowsclipboard.c	Sat Oct 29 23:43:59 2011 -0700
@@ -136,11 +136,13 @@
 SDL_bool
 WIN_HasClipboardText(_THIS)
 {
-    if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
-        return SDL_TRUE;
-    } else {
-        return SDL_FALSE;
-    }
+    SDL_bool result = SDL_FALSE;
+    char *text = WIN_GetClipboardText(_this);
+    if (text) {
+        result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
+        SDL_free(text);
+    } 
+    return result;
 }
 
 void
--- a/src/video/x11/SDL_x11clipboard.c	Sat Oct 29 23:34:19 2011 -0700
+++ b/src/video/x11/SDL_x11clipboard.c	Sat Oct 29 23:43:59 2011 -0700
@@ -129,25 +129,20 @@
     if (!text) {
         text = SDL_strdup("");
     }
+    
     return text;
 }
 
 SDL_bool
 X11_HasClipboardText(_THIS)
 {
-    /* Not an easy way to tell with X11, as far as I know... */
-    char *text;
-    SDL_bool retval;
-
-    text = X11_GetClipboardText(_this);
-    if (*text) {
-        retval = SDL_TRUE;
-    } else {
-        retval = SDL_FALSE;
-    }
-    SDL_free(text);
-
-    return retval;
+    SDL_bool result = SDL_FALSE;
+    char *text = X11_GetClipboardText(_this);
+    if (text) {
+        result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
+        SDL_free(text);
+    }    
+    return result;
 }
 
 #endif /* SDL_VIDEO_DRIVER_X11 */