1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "CClipboard.h" 27 #include "CDataTransferer.h" 28 #import <Cocoa/Cocoa.h> 29 #import <JavaNativeFoundation/JavaNativeFoundation.h> 30 31 #include "ThreadUtilities.h" 32 33 34 static CClipboard *sClipboard = nil; 35 36 // 37 // CClipboardUpdate is used for mulitple calls to setData that happen before 38 // the model and AppKit can get back in sync. 39 // 40 41 @interface CClipboardUpdate : NSObject { 42 NSData *fData; 43 NSString *fFormat; 44 } 45 46 - (id)initWithData:(NSData *)inData withFormat:(NSString *)inFormat; 47 - (NSData *)data; 48 - (NSString *)format; 49 50 @end 51 52 @implementation CClipboardUpdate 53 54 - (id)initWithData:(NSData *)inData withFormat:(NSString *)inFormat 55 { 56 self = [super init]; 57 58 if (self != nil) { 59 fData = [inData retain]; 60 fFormat = [inFormat retain]; 61 } 62 63 return self; 64 } 65 66 - (void)dealloc 67 { 68 [fData release]; 69 fData = nil; 70 71 [fFormat release]; 72 fFormat = nil; 73 74 [super dealloc]; 75 } 76 //- (void)finalize { [super finalize]; } 77 78 - (NSData *)data { 79 return fData; 80 } 81 82 - (NSString *)format { 83 return fFormat; 84 } 85 @end 86 87 @implementation CClipboard 88 89 // Clipboard creation is synchronized at the Java level. 90 + (CClipboard *) sharedClipboard 91 { 92 if (sClipboard == nil) { 93 sClipboard = [[CClipboard alloc] init]; 94 [[NSNotificationCenter defaultCenter] addObserver:sClipboard selector: @selector(checkPasteboard:) name: NSApplicationDidBecomeActiveNotification object: nil]; 95 } 96 97 return sClipboard; 98 } 99 100 - (id) init 101 { 102 self = [super init]; 103 104 if (self != nil) { 105 fChangeCount = [[NSPasteboard generalPasteboard] changeCount]; 106 } 107 108 return self; 109 } 110 111 - (void) javaDeclareTypes:(NSArray *)inTypes withOwner:(jobject)inClipboard jniEnv:(JNIEnv *)inEnv { 112 AWT_ASSERT_NOT_APPKIT_THREAD; 113 114 //NSLog(@"CClipboard javaDeclareTypes %@ withOwner", inTypes); 115 116 @synchronized(self) { 117 if (inClipboard != NULL) { 118 if (fClipboardOwner != NULL) { 119 JNFDeleteGlobalRef(inEnv, fClipboardOwner); 120 } 121 fClipboardOwner = JNFNewGlobalRef(inEnv, inClipboard); 122 } 123 } 124 [ThreadUtilities performOnMainThread:@selector(_nativeDeclareTypes:) onObject:self withObject:inTypes waitUntilDone:YES awtMode:YES]; 125 } 126 127 - (void) _nativeDeclareTypes:(NSArray *)inTypes { 128 AWT_ASSERT_APPKIT_THREAD; 129 130 //NSLog(@"CClipboard _nativeDeclareTypes %@ withOwner", inTypes); 131 132 fChangeCount = [[NSPasteboard generalPasteboard] declareTypes:inTypes owner:self]; 133 } 134 135 136 - (NSArray *) javaGetTypes { 137 AWT_ASSERT_NOT_APPKIT_THREAD; 138 139 NSMutableArray *args = [NSMutableArray arrayWithCapacity:1]; 140 [ThreadUtilities performOnMainThread:@selector(_nativeGetTypes:) onObject:self withObject:args waitUntilDone:YES awtMode:YES]; 141 142 //NSLog(@"CClipboard getTypes returns %@", [args lastObject]); 143 return [args lastObject]; 144 } 145 146 - (void) _nativeGetTypes:(NSMutableArray *)args { 147 AWT_ASSERT_APPKIT_THREAD; 148 149 [args addObject:[[NSPasteboard generalPasteboard] types]]; 150 151 //NSLog(@"CClipboard getTypes returns %@", [args lastObject]); 152 } 153 154 - (void) javaSetData:(NSData *)inData forType:(NSString *) inFormat { 155 AWT_ASSERT_NOT_APPKIT_THREAD; 156 157 CClipboardUpdate *newUpdate = [[CClipboardUpdate alloc] initWithData:inData withFormat:inFormat]; 158 [ThreadUtilities performOnMainThread:@selector(_nativeSetData:) onObject:self withObject:newUpdate waitUntilDone:YES awtMode:YES]; 159 [newUpdate release]; 160 161 //NSLog(@"CClipboard javaSetData forType %@", inFormat); 162 } 163 164 - (void) _nativeSetData:(CClipboardUpdate *)newUpdate { 165 AWT_ASSERT_APPKIT_THREAD; 166 167 [[NSPasteboard generalPasteboard] setData:[newUpdate data] forType:[newUpdate format]]; 168 169 //NSLog(@"CClipboard _nativeSetData setData %@", [newUpdate data]); 170 //NSLog(@"CClipboard _nativeSetData forType %@", [newUpdate format]); 171 } 172 173 - (NSData *) javaGetDataForType:(NSString *) inFormat { 174 AWT_ASSERT_NOT_APPKIT_THREAD; 175 176 NSMutableArray *args = [NSMutableArray arrayWithObject:inFormat]; 177 [ThreadUtilities performOnMainThread:@selector(_nativeGetDataForType:) onObject:self withObject:args waitUntilDone:YES awtMode:YES]; 178 179 //NSLog(@"CClipboard javaGetDataForType %@ returns an NSData", inFormat); 180 return [args lastObject]; 181 } 182 183 - (void) _nativeGetDataForType:(NSMutableArray *) args { 184 AWT_ASSERT_APPKIT_THREAD; 185 186 NSData *returnValue = [[NSPasteboard generalPasteboard] dataForType:[args objectAtIndex:0]]; 187 188 if (returnValue) [args replaceObjectAtIndex:0 withObject:returnValue]; 189 else [args removeLastObject]; 190 191 //NSLog(@"CClipboard _nativeGetDataForType"); 192 } 193 194 - (void) checkPasteboard:(id)application { 195 AWT_ASSERT_APPKIT_THREAD; 196 197 //NSLog(@"CClipboard checkPasteboard oldCount %d newCount %d newTypes %@", fChangeCount, [[NSPasteboard generalPasteboard] changeCount], [[NSPasteboard generalPasteboard] types]); 198 199 // This is called via NSApplicationDidBecomeActiveNotification. 200 201 // If the change count on the general pasteboard is different than when we set it 202 // someone else put data on the clipboard. That means the current owner lost ownership. 203 NSInteger newChangeCount = [[NSPasteboard generalPasteboard] changeCount]; 204 205 if (fChangeCount != newChangeCount) { 206 fChangeCount = newChangeCount; 207 208 [self pasteboardChangedOwner:[NSPasteboard generalPasteboard]]; 209 } 210 } 211 212 - (void)pasteboardChangedOwner:(NSPasteboard *)sender; { 213 AWT_ASSERT_APPKIT_THREAD; 214 215 static JNF_CLASS_CACHE(jc_CClipboard, "sun/lwawt/macosx/CClipboard"); 216 static JNF_MEMBER_CACHE(jm_lostOwnership, jc_CClipboard, "lostSelectionOwnershipImpl", "()V"); 217 218 //NSLog(@"CClipboard pasteboardChangedOwner"); 219 220 // If we have a Java pasteboard owner, tell it that it doesn't own the pasteboard anymore. 221 @synchronized(self) { 222 if (fClipboardOwner) { 223 JNIEnv *env = [ThreadUtilities getJNIEnv]; 224 JNFCallVoidMethod(env, fClipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event) 225 JNFDeleteGlobalRef(env, fClipboardOwner); 226 fClipboardOwner = NULL; 227 } 228 } 229 } 230 231 @end 232 233 /* 234 * Class: sun_lwawt_macosx_CClipboard 235 * Method: declareTypes 236 * Signature: ([JLsun/awt/datatransfer/SunClipboard;)V 237 */ 238 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CClipboard_declareTypes 239 (JNIEnv *env, jobject inObject, jlongArray inTypes, jobject inJavaClip) 240 { 241 JNF_COCOA_ENTER(env); 242 243 jint i; 244 jint nElements = (*env)->GetArrayLength(env, inTypes); 245 NSMutableArray *formatArray = [NSMutableArray arrayWithCapacity:nElements]; 246 jlong *elements = (*env)->GetPrimitiveArrayCritical(env, inTypes, NULL); 247 248 for (i = 0; i < nElements; i++) { 249 NSString *pbFormat = formatForIndex(elements[i]); 250 if (pbFormat) 251 [formatArray addObject:pbFormat]; 252 } 253 254 (*env)->ReleasePrimitiveArrayCritical(env, inTypes, elements, JNI_ABORT); 255 [[CClipboard sharedClipboard] javaDeclareTypes:formatArray withOwner:inJavaClip jniEnv:env]; 256 JNF_COCOA_EXIT(env); 257 } 258 259 /* 260 * Class: sun_lwawt_macosx_CClipboard 261 * Method: setData 262 * Signature: ([BJ)V 263 */ 264 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CClipboard_setData 265 (JNIEnv *env, jobject inObject, jbyteArray inBytes, jlong inFormat) 266 { 267 if (inBytes == NULL) { 268 return; 269 } 270 271 JNF_COCOA_ENTER(env); 272 273 //NSLog(@"Java_sun_lwawt_macosx_CClipboard_setData"); 274 275 jint nBytes = (*env)->GetArrayLength(env, inBytes); 276 jbyte *rawBytes = (*env)->GetPrimitiveArrayCritical(env, inBytes, NULL); 277 NSData *bytesAsData = [NSData dataWithBytes:rawBytes length:nBytes]; 278 (*env)->ReleasePrimitiveArrayCritical(env, inBytes, rawBytes, JNI_ABORT); 279 NSString *format = formatForIndex(inFormat); 280 [[CClipboard sharedClipboard] javaSetData:bytesAsData forType:format]; 281 JNF_COCOA_EXIT(env); 282 } 283 284 /* 285 * Class: sun_lwawt_macosx_CClipboard 286 * Method: getClipboardFormats 287 * Signature: (J)[J 288 */ 289 JNIEXPORT jlongArray JNICALL Java_sun_lwawt_macosx_CClipboard_getClipboardFormats 290 (JNIEnv *env, jobject inObject) 291 { 292 jlongArray returnValue = NULL; 293 JNF_COCOA_ENTER(env); 294 295 //NSLog(@"Java_sun_lwawt_macosx_CClipboard_getClipboardFormats"); 296 297 NSArray *dataTypes = [[CClipboard sharedClipboard] javaGetTypes]; 298 NSUInteger nFormats = [dataTypes count]; 299 NSUInteger knownFormats = 0; 300 NSUInteger i; 301 302 // There can be any number of formats on the general pasteboard. Find out which ones 303 // we know about (i.e., live in the flavormap.properties). 304 for (i = 0; i < nFormats; i++) { 305 NSString *format = (NSString *)[dataTypes objectAtIndex:i]; 306 if (indexForFormat(format) != -1) 307 knownFormats++; 308 } 309 310 returnValue = (*env)->NewLongArray(env, knownFormats); 311 if (returnValue == NULL) { 312 return NULL; 313 } 314 315 if (knownFormats == 0) { 316 return returnValue; 317 } 318 319 // Now go back and map the formats we found back to Java indexes. 320 jboolean isCopy; 321 jlong *lFormats = (*env)->GetLongArrayElements(env, returnValue, &isCopy); 322 jlong *saveFormats = lFormats; 323 324 for (i = 0; i < nFormats; i++) { 325 NSString *format = (NSString *)[dataTypes objectAtIndex:i]; 326 jlong index = indexForFormat(format); 327 328 if (index != -1) { 329 *lFormats = index; 330 lFormats++; 331 } 332 } 333 334 (*env)->ReleaseLongArrayElements(env, returnValue, saveFormats, JNI_COMMIT); 335 JNF_COCOA_EXIT(env); 336 return returnValue; 337 } 338 339 /* 340 * Class: sun_lwawt_macosx_CClipboard 341 * Method: getClipboardData 342 * Signature: (JJ)[B 343 */ 344 JNIEXPORT jbyteArray JNICALL Java_sun_lwawt_macosx_CClipboard_getClipboardData 345 (JNIEnv *env, jobject inObject, jlong format) 346 { 347 jbyteArray returnValue = NULL; 348 349 // Note that this routine makes no attempt to interpret the data, since we're returning 350 // a byte array back to Java. CDataTransferer will do that if necessary. 351 JNF_COCOA_ENTER(env); 352 353 //NSLog(@"Java_sun_lwawt_macosx_CClipboard_getClipboardData"); 354 355 NSString *formatAsString = formatForIndex(format); 356 NSData *clipData = [[CClipboard sharedClipboard] javaGetDataForType:formatAsString]; 357 358 if (clipData == NULL) { 359 [JNFException raise:env as:"java/io/IOException" reason:"Font transform has NaN position"]; 360 return NULL; 361 } 362 363 NSUInteger dataSize = [clipData length]; 364 returnValue = (*env)->NewByteArray(env, dataSize); 365 if (returnValue == NULL) { 366 return NULL; 367 } 368 369 if (dataSize != 0) { 370 const void *dataBuffer = [clipData bytes]; 371 (*env)->SetByteArrayRegion(env, returnValue, 0, dataSize, (jbyte *)dataBuffer); 372 } 373 374 JNF_COCOA_EXIT(env); 375 return returnValue; 376 } 377 378