| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* MacOS X clipboard handling. |
| 12 |
* |
| 13 |
* By Beoran. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include "allegro5/allegro.h" |
| 20 |
#include "allegro5/allegro_opengl.h" |
| 21 |
#include "allegro5/internal/aintern.h" |
| 22 |
#include "allegro5/internal/aintern_driver.h" |
| 23 |
#include "allegro5/internal/aintern_events.h" |
| 24 |
#include "allegro5/internal/aintern_keyboard.h" |
| 25 |
#include "allegro5/internal/aintern_osxclipboard.h" |
| 26 |
#include "allegro5/platform/aintosx.h" |
| 27 |
#include "./osxgl.h" |
| 28 |
|
| 29 |
#ifndef ALLEGRO_MACOSX |
| 30 |
#error Something is wrong with the makefile |
| 31 |
#endif |
| 32 |
|
| 33 |
/* Ensure that we have the right version number available. */ |
| 34 |
#ifndef NSAppKitVersionNumber10_6 |
| 35 |
#define NSAppKitVersionNumber10_6 1038 |
| 36 |
#endif |
| 37 |
|
| 38 |
static NSString *osx_get_text_format(ALLEGRO_DISPLAY *display) |
| 39 |
{ |
| 40 |
(void) display; |
| 41 |
|
| 42 |
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 |
| 43 |
return NSStringPboardType; |
| 44 |
#else |
| 45 |
/* This is now the preferred way of version checking, |
| 46 |
Gestalt has been deprecated. |
| 47 |
*/ |
| 48 |
if ( floor(NSAppKitVersionNumber) < NSAppKitVersionNumber10_6 ) { |
| 49 |
return NSPasteboardTypeString; |
| 50 |
} |
| 51 |
else { |
| 52 |
return NSStringPboardType; |
| 53 |
} |
| 54 |
#endif |
| 55 |
} |
| 56 |
|
| 57 |
static bool osx_set_clipboard_text(ALLEGRO_DISPLAY *display, const char *text) |
| 58 |
{ |
| 59 |
NSAutoreleasePool *pool; |
| 60 |
NSPasteboard *pasteboard; |
| 61 |
NSString *format = osx_get_text_format(display); |
| 62 |
BOOL ok; |
| 63 |
|
| 64 |
pool = [[NSAutoreleasePool alloc] init]; |
| 65 |
if (!pool) return false; |
| 66 |
|
| 67 |
pasteboard = [NSPasteboard generalPasteboard]; |
| 68 |
if (!pasteboard) return false; |
| 69 |
/* First clear the clipboard, otherwise the setString will fail. */ |
| 70 |
[pasteboard clearContents]; |
| 71 |
ok = [pasteboard setString:[NSString stringWithUTF8String:text] forType:format]; |
| 72 |
|
| 73 |
[pool release]; |
| 74 |
|
| 75 |
return ok == YES; |
| 76 |
} |
| 77 |
|
| 78 |
static char * osx_get_clipboard_text(ALLEGRO_DISPLAY *display) |
| 79 |
{ |
| 80 |
NSAutoreleasePool *pool; |
| 81 |
NSPasteboard *pasteboard; |
| 82 |
NSString *format = osx_get_text_format(display); |
| 83 |
NSString *available; |
| 84 |
char *text; |
| 85 |
|
| 86 |
pool = [[NSAutoreleasePool alloc] init]; |
| 87 |
|
| 88 |
pasteboard = [NSPasteboard generalPasteboard]; |
| 89 |
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]]; |
| 90 |
if ([available isEqualToString:format]) { |
| 91 |
NSString* string; |
| 92 |
const char *utf8; |
| 93 |
|
| 94 |
string = [pasteboard stringForType:format]; |
| 95 |
if (string == nil) { |
| 96 |
text = NULL; |
| 97 |
} else { |
| 98 |
size_t size; |
| 99 |
utf8 = [string UTF8String]; |
| 100 |
size = strlen(utf8); |
| 101 |
text = al_malloc(size+1); |
| 102 |
text = _al_sane_strncpy(text, utf8, size+1); |
| 103 |
} |
| 104 |
} else { |
| 105 |
text = NULL; |
| 106 |
} |
| 107 |
|
| 108 |
[pool release]; |
| 109 |
|
| 110 |
return text; |
| 111 |
} |
| 112 |
|
| 113 |
static bool osx_has_clipboard_text(ALLEGRO_DISPLAY *display) |
| 114 |
{ |
| 115 |
NSAutoreleasePool *pool; |
| 116 |
NSPasteboard *pasteboard; |
| 117 |
NSString *format = osx_get_text_format(display); |
| 118 |
NSString *available; |
| 119 |
bool result = false; |
| 120 |
|
| 121 |
pool = [[NSAutoreleasePool alloc] init]; |
| 122 |
|
| 123 |
pasteboard = [NSPasteboard generalPasteboard]; |
| 124 |
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]]; |
| 125 |
|
| 126 |
if ([available isEqualToString:format]) { |
| 127 |
NSString* string; |
| 128 |
string = [pasteboard stringForType:format]; |
| 129 |
if (string != nil) { |
| 130 |
result = true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
[pool release]; |
| 135 |
|
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
void _al_osx_add_clipboard_functions(ALLEGRO_DISPLAY_INTERFACE *vt) |
| 140 |
{ |
| 141 |
vt->set_clipboard_text = osx_set_clipboard_text; |
| 142 |
vt->get_clipboard_text = osx_get_clipboard_text; |
| 143 |
vt->has_clipboard_text = osx_has_clipboard_text; |
| 144 |
} |
| 145 |
|
| 146 |
/* vim: set sts=3 sw=3 et: */ |