/
NSApplicationAWT.m
478 lines (398 loc) · 17.7 KB
/
NSApplicationAWT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#import "NSApplicationAWT.h"
#import <objc/runtime.h>
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>
#import "PropertiesUtilities.h"
#import "ThreadUtilities.h"
#import "QueuingApplicationDelegate.h"
#import "AWTIconData.h"
/*
* Declare library specific JNI_Onload entry if static build
*/
DEF_STATIC_JNI_OnLoad
static BOOL sUsingDefaultNIB = YES;
static NSString *SHARED_FRAMEWORK_BUNDLE = @"/System/Library/Frameworks/JavaVM.framework";
static id <NSApplicationDelegate> applicationDelegate = nil;
static QueuingApplicationDelegate * qad = nil;
// Flag used to indicate to the Plugin2 event synthesis code to do a postEvent instead of sendEvent
BOOL postEventDuringEventSynthesis = NO;
/**
* Subtypes of NSApplicationDefined, which are used for custom events.
*/
enum {
ExecuteBlockEvent = 777, NativeSyncQueueEvent
};
@implementation NSApplicationAWT
- (id) init
{
// Headless: NO
// Embedded: NO
// Multiple Calls: NO
// Caller: +[NSApplication sharedApplication]
AWT_ASSERT_APPKIT_THREAD;
fApplicationName = nil;
dummyEventTimestamp = 0.0;
seenDummyEventLock = nil;
// NSApplication will call _RegisterApplication with the application's bundle, but there may not be one.
// So, we need to call it ourselves to ensure the app is set up properly.
[self registerWithProcessManager];
return [super init];
}
- (void)dealloc
{
[fApplicationName release];
fApplicationName = nil;
[super dealloc];
}
- (void)finishLaunching
{
AWT_ASSERT_APPKIT_THREAD;
JNIEnv *env = [ThreadUtilities getJNIEnv];
SEL appearanceSel = @selector(setAppearance:); // macOS 10.14+
if ([self respondsToSelector:appearanceSel]) {
NSString *appearanceProp = [PropertiesUtilities
javaSystemPropertyForKey:@"apple.awt.application.appearance"
withEnv:env];
if (![@"system" isEqual:appearanceProp]) {
// by default use light mode, because dark mode is not supported yet
NSAppearance *appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
if (appearanceProp != nil) {
NSAppearance *requested = [NSAppearance appearanceNamed:appearanceProp];
if (requested != nil) {
appearance = requested;
}
}
// [self setAppearance:appearance];
[self performSelector:appearanceSel withObject:appearance];
}
}
// Get default nib file location
// NOTE: This should learn about the current java.version. Probably best thru
// the Makefile system's -DFRAMEWORK_VERSION define. Need to be able to pass this
// thru to PB from the Makefile system and for local builds.
NSString *defaultNibFile = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.nib" withEnv:env];
if (!defaultNibFile) {
NSBundle *javaBundle = [NSBundle bundleWithPath:SHARED_FRAMEWORK_BUNDLE];
defaultNibFile = [javaBundle pathForResource:@"DefaultApp" ofType:@"nib"];
} else {
sUsingDefaultNIB = NO;
}
[NSBundle loadNibFile:defaultNibFile externalNameTable: [NSDictionary dictionaryWithObject:self forKey:@"NSOwner"] withZone:nil];
// Set user defaults to not try to parse application arguments.
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * noOpenDict = [NSDictionary dictionaryWithObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"];
[defs registerDefaults:noOpenDict];
// Fix up the dock icon now that we are registered with CAS and the Dock.
[self setDockIconWithEnv:env];
// If we are using our nib (the default application NIB) we need to put the app name into
// the application menu, which has placeholders for the name.
if (sUsingDefaultNIB) {
NSUInteger i, itemCount;
NSMenu *theMainMenu = [NSApp mainMenu];
// First submenu off the main menu is the application menu.
NSMenuItem *appMenuItem = [theMainMenu itemAtIndex:0];
NSMenu *appMenu = [appMenuItem submenu];
itemCount = [appMenu numberOfItems];
for (i = 0; i < itemCount; i++) {
NSMenuItem *anItem = [appMenu itemAtIndex:i];
NSString *oldTitle = [anItem title];
[anItem setTitle:[NSString stringWithFormat:oldTitle, fApplicationName]];
}
}
if (applicationDelegate) {
[self setDelegate:applicationDelegate];
} else {
qad = [QueuingApplicationDelegate sharedDelegate];
[self setDelegate:qad];
}
[super finishLaunching];
}
- (void) registerWithProcessManager
{
// Headless: NO
// Embedded: NO
// Multiple Calls: NO
// Caller: -[NSApplicationAWT init]
AWT_ASSERT_APPKIT_THREAD;
JNIEnv *env = [ThreadUtilities getJNIEnv];
char envVar[80];
// The following environment variable is set from the -Xdock:name param. It should be UTF8.
snprintf(envVar, sizeof(envVar), "APP_NAME_%d", getpid());
char *appName = getenv(envVar);
if (appName != NULL) {
fApplicationName = [NSString stringWithUTF8String:appName];
unsetenv(envVar);
}
// If it wasn't specified as an argument, see if it was specified as a system property.
if (fApplicationName == nil) {
fApplicationName = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.name" withEnv:env];
}
// If we STILL don't have it, the app name is retrieved from an environment variable (set in java.c) It should be UTF8.
if (fApplicationName == nil) {
char mainClassEnvVar[80];
snprintf(mainClassEnvVar, sizeof(mainClassEnvVar), "JAVA_MAIN_CLASS_%d", getpid());
char *mainClass = getenv(mainClassEnvVar);
if (mainClass != NULL) {
fApplicationName = [NSString stringWithUTF8String:mainClass];
unsetenv(mainClassEnvVar);
NSRange lastPeriod = [fApplicationName rangeOfString:@"." options:NSBackwardsSearch];
if (lastPeriod.location != NSNotFound) {
fApplicationName = [fApplicationName substringFromIndex:lastPeriod.location + 1];
}
}
}
// The dock name is nil for double-clickable Java apps (bundled and Web Start apps)
// When that happens get the display name, and if that's not available fall back to
// CFBundleName.
NSBundle *mainBundle = [NSBundle mainBundle];
if (fApplicationName == nil) {
fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
if (fApplicationName == nil) {
fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
if (fApplicationName == nil) {
fApplicationName = (NSString *)[mainBundle objectForInfoDictionaryKey: (NSString *)kCFBundleExecutableKey];
if (fApplicationName == nil) {
// Name of last resort is the last part of the applicatoin name without the .app (consistent with CopyProcessName)
fApplicationName = [[mainBundle bundlePath] lastPathComponent];
if ([fApplicationName hasSuffix:@".app"]) {
fApplicationName = [fApplicationName stringByDeletingPathExtension];
}
}
}
}
}
// We're all done trying to determine the app name. Hold on to it.
[fApplicationName retain];
NSDictionary *registrationOptions = [NSMutableDictionary dictionaryWithObject:fApplicationName forKey:@"JRSAppNameKey"];
NSString *launcherType = [PropertiesUtilities javaSystemPropertyForKey:@"sun.java.launcher" withEnv:env];
if ([@"SUN_STANDARD" isEqualToString:launcherType]) {
[registrationOptions setValue:[NSNumber numberWithBool:YES] forKey:@"JRSAppIsCommandLineKey"];
}
NSString *uiElementProp = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.UIElement" withEnv:env];
if ([@"true" isCaseInsensitiveLike:uiElementProp]) {
[registrationOptions setValue:[NSNumber numberWithBool:YES] forKey:@"JRSAppIsUIElementKey"];
}
NSString *backgroundOnlyProp = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.BackgroundOnly" withEnv:env];
if ([@"true" isCaseInsensitiveLike:backgroundOnlyProp]) {
[registrationOptions setValue:[NSNumber numberWithBool:YES] forKey:@"JRSAppIsBackgroundOnlyKey"];
}
// TODO replace with direct call
// [JRSAppKitAWT registerAWTAppWithOptions:registrationOptions];
// and remove below transform/activate/run hack
id jrsAppKitAWTClass = objc_getClass("JRSAppKitAWT");
SEL registerSel = @selector(registerAWTAppWithOptions:);
if ([jrsAppKitAWTClass respondsToSelector:registerSel]) {
[jrsAppKitAWTClass performSelector:registerSel withObject:registrationOptions];
return;
}
// HACK BEGIN
// The following is necessary to make the java process behave like a
// proper foreground application...
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
}];
// HACK END
}
- (void) setDockIconWithEnv:(JNIEnv *)env {
NSString *theIconPath = nil;
// The following environment variable is set in java.c. It is probably UTF8.
char envVar[80];
snprintf(envVar, sizeof(envVar), "APP_ICON_%d", getpid());
char *appIcon = getenv(envVar);
if (appIcon != NULL) {
theIconPath = [NSString stringWithUTF8String:appIcon];
unsetenv(envVar);
}
if (theIconPath == nil) {
theIconPath = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.icon" withEnv:env];
}
// Use the path specified to get the icon image
NSImage* iconImage = nil;
if (theIconPath != nil) {
iconImage = [[NSImage alloc] initWithContentsOfFile:theIconPath];
}
// If no icon file was specified or we failed to get the icon image
// and there is no bundle's icon, then use the default icon
if (iconImage == nil) {
NSString* bundleIcon = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIconFile"];
if (bundleIcon == nil) {
NSData* iconData;
iconData = [[NSData alloc] initWithBytesNoCopy: sAWTIconData length: sizeof(sAWTIconData) freeWhenDone: NO];
iconImage = [[NSImage alloc] initWithData: iconData];
[iconData release];
}
}
// Set up the dock icon if we have an icon image.
if (iconImage != nil) {
[NSApp setApplicationIconImage:iconImage];
[iconImage release];
}
}
+ (void) runAWTLoopWithApp:(NSApplication*)app {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
// Make sure that when we run in javaRunLoopMode we don't exit randomly
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:[ThreadUtilities javaRunLoopMode]];
do {
@try {
[app run];
} @catch (NSException* e) {
NSLog(@"Apple AWT Startup Exception: %@", [e description]);
NSLog(@"Apple AWT Startup Exception callstack: %@", [e callStackSymbols]);
NSLog(@"Apple AWT Restarting Native Event Thread");
[app stop:app];
}
} while (YES);
[pool drain];
}
- (BOOL)usingDefaultNib {
return sUsingDefaultNIB;
}
- (void)orderFrontStandardAboutPanelWithOptions:(NSDictionary *)optionsDictionary {
if (!optionsDictionary) {
optionsDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
[optionsDictionary setValue:[[[[[NSApp mainMenu] itemAtIndex:0] submenu] itemAtIndex:0] title] forKey:@"ApplicationName"];
if (![NSImage imageNamed:@"NSApplicationIcon"]) {
[optionsDictionary setValue:[NSApp applicationIconImage] forKey:@"ApplicationIcon"];
}
}
[super orderFrontStandardAboutPanelWithOptions:optionsDictionary];
}
#define DRAGMASK (NSMouseMovedMask | NSLeftMouseDraggedMask | NSRightMouseDownMask | NSRightMouseDraggedMask | NSLeftMouseUpMask | NSRightMouseUpMask | NSFlagsChangedMask | NSKeyDownMask)
#if defined(MAC_OS_X_VERSION_10_12) && __LP64__
// 10.12 changed `mask` to NSEventMask (unsigned long long) for x86_64 builds.
- (NSEvent *)nextEventMatchingMask:(NSEventMask)mask
#else
- (NSEvent *)nextEventMatchingMask:(NSUInteger)mask
#endif
untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)deqFlag {
if (mask == DRAGMASK && [((NSString *)kCFRunLoopDefaultMode) isEqual:mode]) {
postEventDuringEventSynthesis = YES;
}
NSEvent *event = [super nextEventMatchingMask:mask untilDate:expiration inMode:mode dequeue: deqFlag];
postEventDuringEventSynthesis = NO;
return event;
}
// NSTimeInterval has microseconds precision
#define TS_EQUAL(ts1, ts2) (fabs((ts1) - (ts2)) < 1e-6)
- (void)sendEvent:(NSEvent *)event
{
if ([event type] == NSApplicationDefined
&& TS_EQUAL([event timestamp], dummyEventTimestamp)
&& (short)[event subtype] == NativeSyncQueueEvent
&& [event data1] == NativeSyncQueueEvent
&& [event data2] == NativeSyncQueueEvent) {
[seenDummyEventLock lockWhenCondition:NO];
[seenDummyEventLock unlockWithCondition:YES];
} else if ([event type] == NSApplicationDefined
&& (short)[event subtype] == ExecuteBlockEvent
&& [event data1] != 0 && [event data2] == ExecuteBlockEvent) {
void (^block)() = (void (^)()) [event data1];
block();
[block release];
} else if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask)) {
// Cocoa won't send us key up event when releasing a key while Cmd is down,
// so we have to do it ourselves.
[[self keyWindow] sendEvent:event];
} else {
[super sendEvent:event];
}
}
/*
* Posts the block to the AppKit event queue which will be executed
* on the main AppKit loop.
* While running nested loops this event will be ignored.
*/
- (void)postRunnableEvent:(void (^)())block
{
void (^copy)() = [block copy];
NSInteger encode = (NSInteger) copy;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
location: NSMakePoint(0,0)
modifierFlags: 0
timestamp: 0
windowNumber: 0
context: nil
subtype: ExecuteBlockEvent
data1: encode
data2: ExecuteBlockEvent];
[NSApp postEvent: event atStart: NO];
[pool drain];
}
- (void)postDummyEvent:(bool)useCocoa {
seenDummyEventLock = [[NSConditionLock alloc] initWithCondition:NO];
dummyEventTimestamp = [NSProcessInfo processInfo].systemUptime;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
location: NSMakePoint(0,0)
modifierFlags: 0
timestamp: dummyEventTimestamp
windowNumber: 0
context: nil
subtype: NativeSyncQueueEvent
data1: NativeSyncQueueEvent
data2: NativeSyncQueueEvent];
if (useCocoa) {
[NSApp postEvent:event atStart:NO];
} else {
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
CGEventPostToPSN(&psn, [event CGEvent]);
}
[pool drain];
}
- (void)waitForDummyEvent:(double)timeout {
bool unlock = true;
if (timeout >= 0) {
double sec = timeout / 1000;
unlock = [seenDummyEventLock lockWhenCondition:YES
beforeDate:[NSDate dateWithTimeIntervalSinceNow:sec]];
} else {
[seenDummyEventLock lockWhenCondition:YES];
}
if (unlock) {
[seenDummyEventLock unlock];
}
[seenDummyEventLock release];
seenDummyEventLock = nil;
}
@end
void OSXAPP_SetApplicationDelegate(id <NSApplicationDelegate> newdelegate)
{
AWT_ASSERT_APPKIT_THREAD;
applicationDelegate = newdelegate;
if (NSApp != nil) {
[NSApp setDelegate: applicationDelegate];
if (applicationDelegate && qad) {
[qad processQueuedEventsWithTargetDelegate: applicationDelegate];
qad = nil;
}
}
}