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