Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CClipboard.m
38829 views
1
/*
2
* Copyright (c) 2011, 2016, 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
#import "CClipboard.h"
27
#import "CDataTransferer.h"
28
#import "ThreadUtilities.h"
29
#import "jni_util.h"
30
#import <Cocoa/Cocoa.h>
31
#import <JavaNativeFoundation/JavaNativeFoundation.h>
32
33
static CClipboard *sClipboard = nil;
34
35
//
36
// CClipboardUpdate is used for mulitple calls to setData that happen before
37
// the model and AppKit can get back in sync.
38
//
39
40
@interface CClipboardUpdate : NSObject {
41
NSData *fData;
42
NSString *fFormat;
43
}
44
45
- (id)initWithData:(NSData *)inData withFormat:(NSString *)inFormat;
46
- (NSData *)data;
47
- (NSString *)format;
48
49
@end
50
51
@implementation CClipboardUpdate
52
53
- (id)initWithData:(NSData *)inData withFormat:(NSString *)inFormat
54
{
55
self = [super init];
56
57
if (self != nil) {
58
fData = [inData retain];
59
fFormat = [inFormat retain];
60
}
61
62
return self;
63
}
64
65
- (void)dealloc
66
{
67
[fData release];
68
fData = nil;
69
70
[fFormat release];
71
fFormat = nil;
72
73
[super dealloc];
74
}
75
76
- (NSData *)data {
77
return fData;
78
}
79
80
- (NSString *)format {
81
return fFormat;
82
}
83
@end
84
85
@implementation CClipboard
86
87
// Clipboard creation is synchronized at the Java level.
88
+ (CClipboard *) sharedClipboard
89
{
90
if (sClipboard == nil) {
91
sClipboard = [[CClipboard alloc] init];
92
[[NSNotificationCenter defaultCenter] addObserver:sClipboard selector: @selector(checkPasteboard:)
93
name: NSApplicationDidBecomeActiveNotification
94
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
113
@synchronized(self) {
114
if (inClipboard != NULL) {
115
if (fClipboardOwner != NULL) {
116
JNFDeleteGlobalRef(inEnv, fClipboardOwner);
117
}
118
fClipboardOwner = JNFNewGlobalRef(inEnv, inClipboard);
119
}
120
}
121
[ThreadUtilities performOnMainThread:@selector(_nativeDeclareTypes:) on:self withObject:inTypes waitUntilDone:YES];
122
}
123
124
- (void) _nativeDeclareTypes:(NSArray *)inTypes {
125
AWT_ASSERT_APPKIT_THREAD;
126
127
fChangeCount = [[NSPasteboard generalPasteboard] declareTypes:inTypes owner:self];
128
}
129
130
131
- (NSArray *) javaGetTypes {
132
133
NSMutableArray *args = [NSMutableArray arrayWithCapacity:1];
134
[ThreadUtilities performOnMainThread:@selector(_nativeGetTypes:) on:self withObject:args waitUntilDone:YES];
135
return [args lastObject];
136
}
137
138
- (void) _nativeGetTypes:(NSMutableArray *)args {
139
AWT_ASSERT_APPKIT_THREAD;
140
141
[args addObject:[[NSPasteboard generalPasteboard] types]];
142
}
143
144
- (void) javaSetData:(NSData *)inData forType:(NSString *) inFormat {
145
146
CClipboardUpdate *newUpdate = [[CClipboardUpdate alloc] initWithData:inData withFormat:inFormat];
147
[ThreadUtilities performOnMainThread:@selector(_nativeSetData:) on:self withObject:newUpdate waitUntilDone:YES];
148
[newUpdate release];
149
}
150
151
- (void) _nativeSetData:(CClipboardUpdate *)newUpdate {
152
AWT_ASSERT_APPKIT_THREAD;
153
154
[[NSPasteboard generalPasteboard] setData:[newUpdate data] forType:[newUpdate format]];
155
}
156
157
- (NSData *) javaGetDataForType:(NSString *) inFormat {
158
159
NSMutableArray *args = [NSMutableArray arrayWithObject:inFormat];
160
[ThreadUtilities performOnMainThread:@selector(_nativeGetDataForType:) on:self withObject:args waitUntilDone:YES];
161
return [args lastObject];
162
}
163
164
- (void) _nativeGetDataForType:(NSMutableArray *) args {
165
AWT_ASSERT_APPKIT_THREAD;
166
167
NSData *returnValue = [[NSPasteboard generalPasteboard] dataForType:[args objectAtIndex:0]];
168
169
if (returnValue) [args replaceObjectAtIndex:0 withObject:returnValue];
170
else [args removeLastObject];
171
}
172
173
174
175
- (void) checkPasteboard:(id)application {
176
AWT_ASSERT_APPKIT_THREAD;
177
178
// This is called via NSApplicationDidBecomeActiveNotification.
179
180
// If the change count on the general pasteboard is different than when we set it
181
// someone else put data on the clipboard. That means the current owner lost ownership.
182
NSInteger newChangeCount = [[NSPasteboard generalPasteboard] changeCount];
183
184
if (fChangeCount != newChangeCount) {
185
fChangeCount = newChangeCount;
186
187
// Notify that the content might be changed
188
static JNF_CLASS_CACHE(jc_CClipboard, "sun/lwawt/macosx/CClipboard");
189
static JNF_STATIC_MEMBER_CACHE(jm_contentChanged, jc_CClipboard, "notifyChanged", "()V");
190
JNIEnv *env = [ThreadUtilities getJNIEnv];
191
JNFCallStaticVoidMethod(env, jm_contentChanged);
192
193
// If we have a Java pasteboard owner, tell it that it doesn't own the pasteboard anymore.
194
static JNF_MEMBER_CACHE(jm_lostOwnership, jc_CClipboard, "notifyLostOwnership", "()V");
195
@synchronized(self) {
196
if (fClipboardOwner) {
197
JNIEnv *env = [ThreadUtilities getJNIEnv];
198
JNFCallVoidMethod(env, fClipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event)
199
JNFDeleteGlobalRef(env, fClipboardOwner);
200
fClipboardOwner = NULL;
201
}
202
}
203
}
204
}
205
206
- (BOOL) checkPasteboardWithoutNotification:(id)application {
207
AWT_ASSERT_APPKIT_THREAD;
208
209
NSInteger newChangeCount = [[NSPasteboard generalPasteboard] changeCount];
210
211
if (fChangeCount != newChangeCount) {
212
fChangeCount = newChangeCount;
213
return YES;
214
} else {
215
return NO;
216
}
217
}
218
219
@end
220
221
/*
222
* Class: sun_lwawt_macosx_CClipboard
223
* Method: declareTypes
224
* Signature: ([JLsun/awt/datatransfer/SunClipboard;)V
225
*/
226
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CClipboard_declareTypes
227
(JNIEnv *env, jobject inObject, jlongArray inTypes, jobject inJavaClip)
228
{
229
JNF_COCOA_ENTER(env);
230
231
jint i;
232
jint nElements = (*env)->GetArrayLength(env, inTypes);
233
NSMutableArray *formatArray = [NSMutableArray arrayWithCapacity:nElements];
234
jlong *elements = (*env)->GetPrimitiveArrayCritical(env, inTypes, NULL);
235
236
for (i = 0; i < nElements; i++) {
237
NSString *pbFormat = formatForIndex(elements[i]);
238
if (pbFormat)
239
[formatArray addObject:pbFormat];
240
}
241
242
(*env)->ReleasePrimitiveArrayCritical(env, inTypes, elements, JNI_ABORT);
243
[[CClipboard sharedClipboard] javaDeclareTypes:formatArray withOwner:inJavaClip jniEnv:env];
244
JNF_COCOA_EXIT(env);
245
}
246
247
/*
248
* Class: sun_lwawt_macosx_CClipboard
249
* Method: setData
250
* Signature: ([BJ)V
251
*/
252
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CClipboard_setData
253
(JNIEnv *env, jobject inObject, jbyteArray inBytes, jlong inFormat)
254
{
255
if (inBytes == NULL) {
256
return;
257
}
258
259
JNF_COCOA_ENTER(env);
260
jint nBytes = (*env)->GetArrayLength(env, inBytes);
261
jbyte *rawBytes = (*env)->GetPrimitiveArrayCritical(env, inBytes, NULL);
262
CHECK_NULL(rawBytes);
263
NSData *bytesAsData = [NSData dataWithBytes:rawBytes length:nBytes];
264
(*env)->ReleasePrimitiveArrayCritical(env, inBytes, rawBytes, JNI_ABORT);
265
NSString *format = formatForIndex(inFormat);
266
[[CClipboard sharedClipboard] javaSetData:bytesAsData forType:format];
267
JNF_COCOA_EXIT(env);
268
}
269
270
/*
271
* Class: sun_lwawt_macosx_CClipboard
272
* Method: getClipboardFormats
273
* Signature: (J)[J
274
*/
275
JNIEXPORT jlongArray JNICALL Java_sun_lwawt_macosx_CClipboard_getClipboardFormats
276
(JNIEnv *env, jobject inObject)
277
{
278
jlongArray returnValue = NULL;
279
JNF_COCOA_ENTER(env);
280
281
NSArray *dataTypes = [[CClipboard sharedClipboard] javaGetTypes];
282
NSUInteger nFormats = [dataTypes count];
283
NSUInteger knownFormats = 0;
284
NSUInteger i;
285
286
// There can be any number of formats on the general pasteboard. Find out which ones
287
// we know about (i.e., live in the flavormap.properties).
288
for (i = 0; i < nFormats; i++) {
289
NSString *format = (NSString *)[dataTypes objectAtIndex:i];
290
if (indexForFormat(format) != -1)
291
knownFormats++;
292
}
293
294
returnValue = (*env)->NewLongArray(env, knownFormats);
295
if (returnValue == NULL) {
296
return NULL;
297
}
298
299
if (knownFormats == 0) {
300
return returnValue;
301
}
302
303
// Now go back and map the formats we found back to Java indexes.
304
jboolean isCopy;
305
jlong *lFormats = (*env)->GetLongArrayElements(env, returnValue, &isCopy);
306
jlong *saveFormats = lFormats;
307
308
for (i = 0; i < nFormats; i++) {
309
NSString *format = (NSString *)[dataTypes objectAtIndex:i];
310
jlong index = indexForFormat(format);
311
312
if (index != -1) {
313
*lFormats = index;
314
lFormats++;
315
}
316
}
317
318
(*env)->ReleaseLongArrayElements(env, returnValue, saveFormats, JNI_COMMIT);
319
JNF_COCOA_EXIT(env);
320
return returnValue;
321
}
322
323
/*
324
* Class: sun_lwawt_macosx_CClipboard
325
* Method: getClipboardData
326
* Signature: (JJ)[B
327
*/
328
JNIEXPORT jbyteArray JNICALL Java_sun_lwawt_macosx_CClipboard_getClipboardData
329
(JNIEnv *env, jobject inObject, jlong format)
330
{
331
jbyteArray returnValue = NULL;
332
333
// Note that this routine makes no attempt to interpret the data, since we're returning
334
// a byte array back to Java. CDataTransferer will do that if necessary.
335
JNF_COCOA_ENTER(env);
336
337
NSString *formatAsString = formatForIndex(format);
338
NSData *clipData = [[CClipboard sharedClipboard] javaGetDataForType:formatAsString];
339
340
if (clipData == NULL) {
341
[JNFException raise:env as:"java/io/IOException" reason:"Font transform has NaN position"];
342
return NULL;
343
}
344
345
NSUInteger dataSize = [clipData length];
346
returnValue = (*env)->NewByteArray(env, dataSize);
347
if (returnValue == NULL) {
348
return NULL;
349
}
350
351
if (dataSize != 0) {
352
const void *dataBuffer = [clipData bytes];
353
(*env)->SetByteArrayRegion(env, returnValue, 0, dataSize, (jbyte *)dataBuffer);
354
}
355
356
JNF_COCOA_EXIT(env);
357
return returnValue;
358
}
359
360
/*
361
* Class: sun_lwawt_macosx_CClipboard
362
* Method: checkPasteboard
363
* Signature: ()V
364
*/
365
JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CClipboard_checkPasteboardWithoutNotification
366
(JNIEnv *env, jobject inObject)
367
{
368
__block BOOL ret = NO;
369
JNF_COCOA_ENTER(env);
370
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
371
ret = [[CClipboard sharedClipboard] checkPasteboardWithoutNotification:nil];
372
}];
373
374
JNF_COCOA_EXIT(env);
375
return ret;
376
}
377
378
379
380