diff --git a/SDLMain.m b/SDLMain.m index 083900aab..30ac47cd8 100644 --- a/SDLMain.m +++ b/SDLMain.m @@ -360,11 +360,13 @@ char * readUserPreferences() { prefData = ""; char *prefDataCopy = calloc([prefDataNSString length]+1, 1); - SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]); + SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]+1); [prefDataNSString release]; [prefs release]; + puts(prefDataCopy); + return prefDataCopy; } @@ -379,6 +381,36 @@ void writeUserPreferences(const char * prefData) { [prefs release]; } +char * readClipboard() { + NSPasteboard *clipboard = [NSPasteboard generalPasteboard]; + + NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil]; + NSDictionary *options = [NSDictionary dictionary]; + NSArray *clipboardItems = [clipboard readObjectsForClasses:classes options:options]; + + if(clipboardItems == nil || [clipboardItems count] == 0) return NULL; + + NSString *newString = [clipboardItems objectAtIndex:0]; + const char * clipboardData = [newString UTF8String]; + if(clipboardData == NULL) + clipboardData = ""; + + char *clipboardDataCopy = calloc([newString length]+1, 1); + SDL_strlcpy(clipboardDataCopy, clipboardData, [newString length]+1); + + return clipboardDataCopy; +} + +void writeClipboard(const char * clipboardData) { + NSPasteboard *clipboard = [NSPasteboard generalPasteboard]; + + NSString *newString = [NSString stringWithUTF8String: clipboardData]; + + [clipboard clearContents]; + [clipboard setString:newString forType:NSStringPboardType]; + +} + #ifdef main # undef main diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index fa9033b6a..6ba1f62d5 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -24,6 +24,10 @@ #endif #ifdef MACOSX #include +extern "C" { + char * readClipboard(); + void writeClipboard(const char * clipboardData); +} #endif #include "Format.h" @@ -69,14 +73,7 @@ void ClipboardPush(char * text) } clipboardText = mystrdup(text); #ifdef MACOSX - PasteboardRef newclipboard; - - if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return; - if (PasteboardClear(newclipboard)!=noErr) return; - PasteboardSynchronize(newclipboard); - - CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text)); - PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0); + writeClipboard(text); #elif defined(WIN) if (OpenClipboard(NULL)) { @@ -109,7 +106,9 @@ void EventProcess(SDL_Event event); char * ClipboardPull() { #ifdef MACOSX - printf("Not implemented: get text from clipboard\n"); + char * data = readClipboard(); + if(!data) return mystrdup(""); + return mystrdup(data); #elif defined(WIN) if (OpenClipboard(NULL)) {