Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libsplashscreen/splashscreen_sys.m
41149 views
1
/*
2
* Copyright (c) 2011, 2019, 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 "splashscreen_impl.h"
27
28
#import <Cocoa/Cocoa.h>
29
#import <objc/objc-auto.h>
30
31
#include <Security/AuthSession.h>
32
#import "NSApplicationAWT.h"
33
34
#include <sys/time.h>
35
#include <pthread.h>
36
#include <iconv.h>
37
#include <langinfo.h>
38
#include <locale.h>
39
#include <fcntl.h>
40
#include <poll.h>
41
#include <errno.h>
42
#include <sys/types.h>
43
#include <signal.h>
44
#include <unistd.h>
45
#include <dlfcn.h>
46
47
#include <sizecalc.h>
48
#import "ThreadUtilities.h"
49
50
NSString* findScaledImageName(NSString *fileName,
51
NSUInteger dotIndex,
52
NSString *strToAppend);
53
54
static NSScreen* SplashNSScreen()
55
{
56
return [[NSScreen screens] objectAtIndex: 0];
57
}
58
59
static void SplashCenter(Splash * splash)
60
{
61
NSRect screenFrame = [SplashNSScreen() frame];
62
63
splash->x = (screenFrame.size.width - splash->width) / 2;
64
splash->y = (screenFrame.size.height - splash->height) / 2 + screenFrame.origin.y;
65
}
66
67
unsigned
68
SplashTime(void) {
69
struct timeval tv;
70
struct timezone tz;
71
unsigned long long msec;
72
73
gettimeofday(&tv, &tz);
74
msec = (unsigned long long) tv.tv_sec * 1000 +
75
(unsigned long long) tv.tv_usec / 1000;
76
77
return (unsigned) msec;
78
}
79
80
/* Could use npt but decided to cut down on linked code size */
81
char* SplashConvertStringAlloc(const char* in, int* size) {
82
const char *codeset;
83
const char *codeset_out;
84
iconv_t cd;
85
size_t rc;
86
char *buf = NULL, *out;
87
size_t bufSize, inSize, outSize;
88
const char* old_locale;
89
90
if (!in) {
91
return NULL;
92
}
93
old_locale = setlocale(LC_ALL, "");
94
95
codeset = nl_langinfo(CODESET);
96
if ( codeset == NULL || codeset[0] == 0 ) {
97
goto done;
98
}
99
/* we don't need BOM in output so we choose native BE or LE encoding here */
100
codeset_out = (platformByteOrder()==BYTE_ORDER_MSBFIRST) ?
101
"UCS-2BE" : "UCS-2LE";
102
103
cd = iconv_open(codeset_out, codeset);
104
if (cd == (iconv_t)-1 ) {
105
goto done;
106
}
107
inSize = strlen(in);
108
buf = SAFE_SIZE_ARRAY_ALLOC(malloc, inSize, 2);
109
if (!buf) {
110
return NULL;
111
}
112
bufSize = inSize*2; // need 2 bytes per char for UCS-2, this is
113
// 2 bytes per source byte max
114
out = buf; outSize = bufSize;
115
/* linux iconv wants char** source and solaris wants const char**...
116
cast to void* */
117
rc = iconv(cd, (void*)&in, &inSize, &out, &outSize);
118
iconv_close(cd);
119
120
if (rc == (size_t)-1) {
121
free(buf);
122
buf = NULL;
123
} else {
124
if (size) {
125
*size = (bufSize-outSize)/2; /* bytes to wchars */
126
}
127
}
128
done:
129
setlocale(LC_ALL, old_locale);
130
return buf;
131
}
132
133
BOOL isSWTRunning() {
134
char envVar[80];
135
// If this property is present we are running SWT
136
snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid());
137
return getenv(envVar) != NULL;
138
}
139
140
jboolean SplashGetScaledImageName(const char* jar, const char* file,
141
float *scaleFactor, char *scaledFile,
142
const size_t scaledImageLength) {
143
*scaleFactor = 1;
144
145
if(isSWTRunning()){
146
return JNI_FALSE;
147
}
148
149
NSAutoreleasePool *pool = [NSAutoreleasePool new];
150
__block float screenScaleFactor = 1;
151
152
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
153
// initialize NSApplication and AWT stuff
154
[NSApplicationAWT sharedApplication];
155
screenScaleFactor = [SplashNSScreen() backingScaleFactor];
156
}];
157
158
if (screenScaleFactor > 1) {
159
NSString *fileName = [NSString stringWithUTF8String: file];
160
NSUInteger length = [fileName length];
161
NSRange range = [fileName rangeOfString: @"."
162
options:NSBackwardsSearch];
163
NSUInteger dotIndex = range.location;
164
NSString *fileName2x = nil;
165
166
fileName2x = findScaledImageName(fileName, dotIndex, @"@2x");
167
if(![[NSFileManager defaultManager]
168
fileExistsAtPath: fileName2x]) {
169
fileName2x = findScaledImageName(fileName, dotIndex, @"@200pct");
170
}
171
if (jar || [[NSFileManager defaultManager]
172
fileExistsAtPath: fileName2x]){
173
if (strlen([fileName2x UTF8String]) > scaledImageLength) {
174
[pool drain];
175
return JNI_FALSE;
176
}
177
*scaleFactor = 2;
178
strcpy(scaledFile, [fileName2x UTF8String]);
179
[pool drain];
180
return JNI_TRUE;
181
}
182
}
183
[pool drain];
184
return JNI_FALSE;
185
}
186
187
static int isInAquaSession() {
188
// environment variable to bypass the aqua session check
189
char *ev = getenv("AWT_FORCE_HEADFUL");
190
if (ev && (strncasecmp(ev, "true", 4) == 0)) {
191
// if "true" then tell the caller we're in
192
// an Aqua session without actually checking
193
return 1;
194
}
195
// Is the WindowServer available?
196
SecuritySessionId session_id;
197
SessionAttributeBits session_info;
198
OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info);
199
if (status == noErr) {
200
if (session_info & sessionHasGraphicAccess) {
201
return 1;
202
}
203
}
204
return 0;
205
}
206
207
int
208
SplashInitPlatform(Splash * splash) {
209
if (!isInAquaSession()) {
210
return 0;
211
}
212
pthread_mutex_init(&splash->lock, NULL);
213
214
splash->maskRequired = 0;
215
216
217
//TODO: the following is too much of a hack but should work in 90% cases.
218
// besides we don't use device-dependent drawing, so probably
219
// that's very fine indeed
220
splash->byteAlignment = 1;
221
initFormat(&splash->screenFormat, 0xff << 8,
222
0xff << 16, 0xff << 24, 0xff << 0);
223
splash->screenFormat.byteOrder = 1 ? BYTE_ORDER_LSBFIRST : BYTE_ORDER_MSBFIRST;
224
splash->screenFormat.depthBytes = 4;
225
226
// If we are running SWT we should not start a runLoop
227
if (!isSWTRunning()) {
228
[ThreadUtilities performOnMainThreadWaiting:NO block:^() {
229
[NSApplicationAWT runAWTLoopWithApp:[NSApplicationAWT sharedApplication]];
230
}];
231
}
232
return 1;
233
}
234
235
void
236
SplashCleanupPlatform(Splash * splash) {
237
splash->maskRequired = 0;
238
}
239
240
void
241
SplashDonePlatform(Splash * splash) {
242
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
243
244
pthread_mutex_destroy(&splash->lock);
245
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
246
if (splash->window) {
247
[splash->window orderOut:nil];
248
[splash->window release];
249
}
250
}];
251
[pool drain];
252
}
253
254
void
255
SplashLock(Splash * splash) {
256
pthread_mutex_lock(&splash->lock);
257
}
258
259
void
260
SplashUnlock(Splash * splash) {
261
pthread_mutex_unlock(&splash->lock);
262
}
263
264
void
265
SplashInitFrameShape(Splash * splash, int imageIndex) {
266
// No shapes, we rely on alpha compositing
267
}
268
269
void * SplashScreenThread(void *param);
270
void
271
SplashCreateThread(Splash * splash) {
272
pthread_t thr;
273
pthread_attr_t attr;
274
int rc;
275
276
pthread_attr_init(&attr);
277
rc = pthread_create(&thr, &attr, SplashScreenThread, (void *) splash);
278
}
279
280
void
281
SplashRedrawWindow(Splash * splash) {
282
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
283
284
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
285
// drop the reference to the old view and image
286
[splash->window setContentView: nil];
287
SplashUpdateScreenData(splash);
288
289
// NSDeviceRGBColorSpace vs. NSCalibratedRGBColorSpace ?
290
NSBitmapImageRep * rep = [[NSBitmapImageRep alloc]
291
initWithBitmapDataPlanes: (unsigned char**)&splash->screenData
292
pixelsWide: splash->width
293
pixelsHigh: splash->height
294
bitsPerSample: 8
295
samplesPerPixel: 4
296
hasAlpha: YES
297
isPlanar: NO
298
colorSpaceName: NSDeviceRGBColorSpace
299
bitmapFormat: NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat
300
bytesPerRow: splash->width * 4
301
bitsPerPixel: 32];
302
303
NSImage * image = [[NSImage alloc]
304
initWithSize: NSMakeSize(splash->width, splash->height)];
305
[image setBackgroundColor: [NSColor clearColor]];
306
307
[image addRepresentation: rep];
308
float scaleFactor = splash->scaleFactor;
309
if (scaleFactor > 0 && scaleFactor != 1) {
310
NSSize size = [image size];
311
size.width /= scaleFactor;
312
size.height /= scaleFactor;
313
[image setSize: size];
314
}
315
316
NSImageView * view = [[NSImageView alloc] init];
317
318
[view setImage: image];
319
[view setEditable: NO];
320
//NOTE: we don't set a 'wait cursor' for the view because:
321
// 1. The Cocoa GUI guidelines suggest to avoid it, and use a progress
322
// bar instead.
323
// 2. There simply isn't an instance of NSCursor that represent
324
// the 'wait cursor'. So that is undoable.
325
326
//TODO: only the first image in an animated gif preserves transparency.
327
// Loos like the splash->screenData contains inappropriate data
328
// for all but the first frame.
329
330
[image release];
331
[rep release];
332
333
[splash->window setContentView: view];
334
[splash->window orderFrontRegardless];
335
}];
336
337
[pool drain];
338
}
339
340
void SplashReconfigureNow(Splash * splash) {
341
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
342
343
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
344
SplashCenter(splash);
345
346
if (!splash->window) {
347
return;
348
}
349
350
[splash->window orderOut:nil];
351
[splash->window setFrame: NSMakeRect(splash->x, splash->y, splash->width, splash->height)
352
display: NO];
353
}];
354
355
[pool drain];
356
357
SplashRedrawWindow(splash);
358
}
359
360
void
361
SplashEventLoop(Splash * splash) {
362
363
/* we should have splash _locked_ on entry!!! */
364
365
while (1) {
366
struct pollfd pfd[1];
367
int timeout = -1;
368
int ctl = splash->controlpipe[0];
369
int rc;
370
int pipes_empty;
371
372
pfd[0].fd = ctl;
373
pfd[0].events = POLLIN | POLLPRI;
374
375
errno = 0;
376
if (splash->isVisible>0 && SplashIsStillLooping(splash)) {
377
timeout = splash->time + splash->frames[splash->currentFrame].delay
378
- SplashTime();
379
if (timeout < 0) {
380
timeout = 0;
381
}
382
}
383
SplashUnlock(splash);
384
rc = poll(pfd, 1, timeout);
385
SplashLock(splash);
386
if (splash->isVisible > 0 && splash->currentFrame >= 0 &&
387
SplashTime() >= splash->time + splash->frames[splash->currentFrame].delay) {
388
SplashNextFrame(splash);
389
SplashRedrawWindow(splash);
390
}
391
if (rc <= 0) {
392
errno = 0;
393
continue;
394
}
395
pipes_empty = 0;
396
while(!pipes_empty) {
397
char buf;
398
399
pipes_empty = 1;
400
if (read(ctl, &buf, sizeof(buf)) > 0) {
401
pipes_empty = 0;
402
switch (buf) {
403
case SPLASHCTL_UPDATE:
404
if (splash->isVisible>0) {
405
SplashRedrawWindow(splash);
406
}
407
break;
408
case SPLASHCTL_RECONFIGURE:
409
if (splash->isVisible>0) {
410
SplashReconfigureNow(splash);
411
}
412
break;
413
case SPLASHCTL_QUIT:
414
return;
415
}
416
}
417
}
418
}
419
}
420
421
void *
422
SplashScreenThread(void *param) {
423
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
424
Splash *splash = (Splash *) param;
425
426
SplashLock(splash);
427
pipe(splash->controlpipe);
428
fcntl(splash->controlpipe[0], F_SETFL,
429
fcntl(splash->controlpipe[0], F_GETFL, 0) | O_NONBLOCK);
430
splash->time = SplashTime();
431
splash->currentFrame = 0;
432
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
433
SplashCenter(splash);
434
435
splash->window = (void*) [[NSWindow alloc]
436
initWithContentRect: NSMakeRect(splash->x, splash->y, splash->width, splash->height)
437
styleMask: NSBorderlessWindowMask
438
backing: NSBackingStoreBuffered
439
defer: NO
440
screen: SplashNSScreen()];
441
442
[splash->window setOpaque: NO];
443
[splash->window setBackgroundColor: [NSColor clearColor]];
444
}];
445
fflush(stdout);
446
if (splash->window) {
447
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
448
[splash->window orderFrontRegardless];
449
}];
450
SplashRedrawWindow(splash);
451
SplashEventLoop(splash);
452
}
453
SplashUnlock(splash);
454
SplashDone(splash);
455
456
splash->isVisible=-1;
457
458
[pool drain];
459
460
return 0;
461
}
462
463
void
464
sendctl(Splash * splash, char code) {
465
if (splash && splash->controlpipe[1]) {
466
write(splash->controlpipe[1], &code, 1);
467
}
468
}
469
470
void
471
SplashClosePlatform(Splash * splash) {
472
sendctl(splash, SPLASHCTL_QUIT);
473
}
474
475
void
476
SplashUpdate(Splash * splash) {
477
sendctl(splash, SPLASHCTL_UPDATE);
478
}
479
480
void
481
SplashReconfigure(Splash * splash) {
482
sendctl(splash, SPLASHCTL_RECONFIGURE);
483
}
484
485
NSString* findScaledImageName(NSString *fileName, NSUInteger dotIndex, NSString *strToAppend) {
486
NSString *fileName2x = nil;
487
if (dotIndex == NSNotFound) {
488
fileName2x = [fileName stringByAppendingString: strToAppend];
489
} else {
490
fileName2x = [fileName substringToIndex: dotIndex];
491
fileName2x = [fileName2x stringByAppendingString: strToAppend];
492
fileName2x = [fileName2x stringByAppendingString:
493
[fileName substringFromIndex: dotIndex]];
494
}
495
return fileName2x;
496
}
497
498
499