Commit c5e05365 authored by Andreas Schiffler's avatar Andreas Schiffler

Update SDL_HasClipboardText functions to return value based on clipboard...

Update SDL_HasClipboardText functions to return value based on clipboard content; Fix memory leak in fallback SetClipboard implementation
parent ab5d30e6
...@@ -55,7 +55,7 @@ extern DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text); ...@@ -55,7 +55,7 @@ extern DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text);
extern DECLSPEC char * SDLCALL SDL_GetClipboardText(void); 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() * \sa SDL_GetClipboardText()
*/ */
......
...@@ -35,6 +35,9 @@ SDL_SetClipboardText(const char *text) ...@@ -35,6 +35,9 @@ SDL_SetClipboardText(const char *text)
if (_this->SetClipboardText) { if (_this->SetClipboardText) {
return _this->SetClipboardText(_this, text); return _this->SetClipboardText(_this, text);
} else { } else {
if (_this->clipboard_text) {
SDL_free(_this->clipboard_text);
}
_this->clipboard_text = SDL_strdup(text); _this->clipboard_text = SDL_strdup(text);
return 0; return 0;
} }
...@@ -64,7 +67,7 @@ SDL_HasClipboardText(void) ...@@ -64,7 +67,7 @@ SDL_HasClipboardText(void)
if (_this->HasClipboardText) { if (_this->HasClipboardText) {
return _this->HasClipboardText(_this); return _this->HasClipboardText(_this);
} else { } else {
if (_this->clipboard_text) { if ((_this->clipboard_text) && (SDL_strlen(_this->clipboard_text)>0)) {
return SDL_TRUE; return SDL_TRUE;
} else { } else {
return SDL_FALSE; return SDL_FALSE;
......
...@@ -51,8 +51,9 @@ int BE_SetClipboardText(_THIS, const char *text) { ...@@ -51,8 +51,9 @@ int BE_SetClipboardText(_THIS, const char *text) {
char *BE_GetClipboardText(_THIS) { char *BE_GetClipboardText(_THIS) {
BMessage *clip = NULL; BMessage *clip = NULL;
const char *text; const char *text = NULL;
ssize_t length; ssize_t length;
char *result;
if(be_clipboard->Lock()) { if(be_clipboard->Lock()) {
if((clip = be_clipboard->Data())) { if((clip = be_clipboard->Data())) {
/* Presumably the string of characters is ascii-format */ /* Presumably the string of characters is ascii-format */
...@@ -60,37 +61,29 @@ char *BE_GetClipboardText(_THIS) { ...@@ -60,37 +61,29 @@ char *BE_GetClipboardText(_THIS) {
&length); &length);
} else { } else {
be_clipboard->Unlock(); be_clipboard->Unlock();
return NULL;
} }
be_clipboard->Unlock(); be_clipboard->Unlock();
}
if (!text) {
result = SDL_strdup("");
} else { } else {
return NULL; /* Copy the data and pass on to SDL */
result = (char*)SDL_calloc(1, sizeof(char*)*length);
SDL_strlcpy(result, text, length);
} }
/* Copy the data and pass on to SDL */
char *result = (char*)SDL_calloc(1, sizeof(char*)*length);
SDL_strlcpy(result, text, length);
return result; return result;
} }
SDL_bool BE_HasClipboardText(_THIS) { SDL_bool BE_HasClipboardText(_THIS) {
BMessage *clip = NULL; SDL_bool result = SDL_FALSE;
const char *text; char *text = BE_GetClipboardText(_this);
ssize_t length; if (text) {
SDL_bool retval = SDL_FALSE; result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
SDL_free(text);
if(be_clipboard->Lock()) { }
if((clip = be_clipboard->Data())) { return result;
/* 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;
} }
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -94,24 +94,12 @@ Cocoa_GetClipboardText(_THIS) ...@@ -94,24 +94,12 @@ Cocoa_GetClipboardText(_THIS)
SDL_bool SDL_bool
Cocoa_HasClipboardText(_THIS) Cocoa_HasClipboardText(_THIS)
{ {
NSAutoreleasePool *pool; SDL_bool result = SDL_FALSE;
NSPasteboard *pasteboard; char *text = Cocoa_GetClipboardText(_this);
NSString *format = GetTextFormat(_this); if (text) {
NSString *available; result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
SDL_bool result; SDL_free(text);
pool = [[NSAutoreleasePool alloc] init];
pasteboard = [NSPasteboard generalPasteboard];
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
if ([available isEqualToString:format]) {
result = SDL_TRUE;
} else {
result = SDL_FALSE;
} }
[pool release];
return result; return result;
} }
......
...@@ -136,11 +136,13 @@ WIN_GetClipboardText(_THIS) ...@@ -136,11 +136,13 @@ WIN_GetClipboardText(_THIS)
SDL_bool SDL_bool
WIN_HasClipboardText(_THIS) WIN_HasClipboardText(_THIS)
{ {
if (IsClipboardFormatAvailable(TEXT_FORMAT)) { SDL_bool result = SDL_FALSE;
return SDL_TRUE; char *text = WIN_GetClipboardText(_this);
} else { if (text) {
return SDL_FALSE; result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
} SDL_free(text);
}
return result;
} }
void void
......
...@@ -129,25 +129,20 @@ X11_GetClipboardText(_THIS) ...@@ -129,25 +129,20 @@ X11_GetClipboardText(_THIS)
if (!text) { if (!text) {
text = SDL_strdup(""); text = SDL_strdup("");
} }
return text; return text;
} }
SDL_bool SDL_bool
X11_HasClipboardText(_THIS) X11_HasClipboardText(_THIS)
{ {
/* Not an easy way to tell with X11, as far as I know... */ SDL_bool result = SDL_FALSE;
char *text; char *text = X11_GetClipboardText(_this);
SDL_bool retval; if (text) {
result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
text = X11_GetClipboardText(_this); SDL_free(text);
if (*text) { }
retval = SDL_TRUE; return result;
} else {
retval = SDL_FALSE;
}
SDL_free(text);
return retval;
} }
#endif /* SDL_VIDEO_DRIVER_X11 */ #endif /* SDL_VIDEO_DRIVER_X11 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment