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 BOOL isSWTRunning() { 130 char envVar[80]; 131 // If this property is present we are running SWT 132 snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid()); 133 return getenv(envVar) != NULL; 134 } 135 136 char* SplashGetScaledImageName(const char* jar, const char* file, 137 float *scaleFactor) { 138 *scaleFactor = 1; 139 140 if(isSWTRunning()){ 141 return nil; 142 } 143 144 NSAutoreleasePool *pool = [NSAutoreleasePool new]; 145 char* scaledFile = nil; 146 __block float screenScaleFactor = 1; 147 148 [ThreadUtilities performOnMainThreadWaiting:YES block:^(){ 149 // initialize NSApplication and AWT stuff 150 [NSApplicationAWT sharedApplication]; 151 screenScaleFactor = [SplashNSScreen() backingScaleFactor]; 152 }]; 153 154 if (screenScaleFactor > 1) { 155 NSString *fileName = [NSString stringWithUTF8String: file]; 156 NSUInteger length = [fileName length]; 157 NSRange range = [fileName rangeOfString: @"." 158 options:NSBackwardsSearch]; 159 NSUInteger dotIndex = range.location; 160 NSString *fileName2x = nil; 161 162 if (dotIndex == NSNotFound) { 163 fileName2x = [fileName stringByAppendingString: @"@2x"]; 164 } else { 165 fileName2x = [fileName substringToIndex: dotIndex]; 166 fileName2x = [fileName2x stringByAppendingString: @"@2x"]; 167 fileName2x = [fileName2x stringByAppendingString: 168 [fileName substringFromIndex: dotIndex]]; 169 } 170 171 if ((fileName2x != nil) && (jar || [[NSFileManager defaultManager] 172 fileExistsAtPath: fileName2x])){ 173 *scaleFactor = 2; 174 scaledFile = strdup([fileName2x UTF8String]); 175 } 176 } 177 [pool drain]; 178 return scaledFile; 179 } 180 181 void 182 SplashInitPlatform(Splash * splash) { 183 pthread_mutex_init(&splash->lock, NULL); 184 185 splash->maskRequired = 0; 186 187 188 //TODO: the following is too much of a hack but should work in 90% cases. 189 // besides we don't use device-dependant drawing, so probably 190 // that's very fine indeed 191 splash->byteAlignment = 1; 192 initFormat(&splash->screenFormat, 0xff << 8, 193 0xff << 16, 0xff << 24, 0xff << 0); 194 splash->screenFormat.byteOrder = 1 ? BYTE_ORDER_LSBFIRST : BYTE_ORDER_MSBFIRST; 195 splash->screenFormat.depthBytes = 4; 196 197 // If we are running SWT we should not start a runLoop 198 if (!isSWTRunning()) { 199 [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^() { 200 [NSApplicationAWT runAWTLoopWithApp:[NSApplicationAWT sharedApplication]]; 201 }]; 202 } 203 } 204 205 void 206 SplashCleanupPlatform(Splash * splash) { 207 splash->maskRequired = 0; 208 } 209 210 void 211 SplashDonePlatform(Splash * splash) { 212 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 213 214 pthread_mutex_destroy(&splash->lock); 215 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ 216 if (splash->window) { 217 [splash->window orderOut:nil]; 218 [splash->window release]; 219 } 220 }]; 221 [pool drain]; 222 } 223 224 void 225 SplashLock(Splash * splash) { 226 pthread_mutex_lock(&splash->lock); 227 } 228 229 void 230 SplashUnlock(Splash * splash) { 231 pthread_mutex_unlock(&splash->lock); 232 } 233 234 void 235 SplashInitFrameShape(Splash * splash, int imageIndex) { 236 // No shapes, we rely on alpha compositing 237 } 238 239 void * SplashScreenThread(void *param); 240 void 241 SplashCreateThread(Splash * splash) { 242 pthread_t thr; 243 pthread_attr_t attr; 244 int rc; 245 246 pthread_attr_init(&attr); 247 rc = pthread_create(&thr, &attr, SplashScreenThread, (void *) splash); 248 } 249 250 void 251 SplashRedrawWindow(Splash * splash) { 252 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 253 254 SplashUpdateScreenData(splash); 255 256 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ 257 // NSDeviceRGBColorSpace vs. NSCalibratedRGBColorSpace ? 258 NSBitmapImageRep * rep = [[NSBitmapImageRep alloc] 259 initWithBitmapDataPlanes: (unsigned char**)&splash->screenData 260 pixelsWide: splash->width 261 pixelsHigh: splash->height 262 bitsPerSample: 8 263 samplesPerPixel: 4 264 hasAlpha: YES 265 isPlanar: NO 266 colorSpaceName: NSDeviceRGBColorSpace 267 bitmapFormat: NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat 268 bytesPerRow: splash->width * 4 269 bitsPerPixel: 32]; 270 271 NSImage * image = [[NSImage alloc] 272 initWithSize: NSMakeSize(splash->width, splash->height)]; 273 [image setBackgroundColor: [NSColor clearColor]]; 274 275 [image addRepresentation: rep]; 276 float scaleFactor = splash->scaleFactor; 277 if (scaleFactor > 0 && scaleFactor != 1) { 278 NSSize size = [image size]; 279 size.width /= scaleFactor; 280 size.height /= scaleFactor; 281 [image setSize: size]; 282 } 283 284 NSImageView * view = [[NSImageView alloc] init]; 285 286 [view setImage: image]; 287 [view setEditable: NO]; 288 //NOTE: we don't set a 'wait cursor' for the view because: 289 // 1. The Cocoa GUI guidelines suggest to avoid it, and use a progress 290 // bar instead. 291 // 2. There simply isn't an instance of NSCursor that represent 292 // the 'wait cursor'. So that is undoable. 293 294 //TODO: only the first image in an animated gif preserves transparency. 295 // Loos like the splash->screenData contains inappropriate data 296 // for all but the first frame. 297 298 [image release]; 299 [rep release]; 300 301 [splash->window setContentView: view]; 302 [splash->window orderFrontRegardless]; 303 }]; 304 305 [pool drain]; 306 } 307 308 void SplashReconfigureNow(Splash * splash) { 309 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 310 311 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ 312 SplashCenter(splash); 313 314 if (!splash->window) { 315 return; 316 } 317 318 [splash->window orderOut:nil]; 319 [splash->window setFrame: NSMakeRect(splash->x, splash->y, splash->width, splash->height) 320 display: NO]; 321 }]; 322 323 [pool drain]; 324 325 SplashRedrawWindow(splash); 326 } 327 328 void 329 SplashEventLoop(Splash * splash) { 330 331 /* we should have splash _locked_ on entry!!! */ 332 333 while (1) { 334 struct pollfd pfd[1]; 335 int timeout = -1; 336 int ctl = splash->controlpipe[0]; 337 int rc; 338 int pipes_empty; 339 340 pfd[0].fd = ctl; 341 pfd[0].events = POLLIN | POLLPRI; 342 343 errno = 0; 344 if (splash->isVisible>0 && SplashIsStillLooping(splash)) { 345 timeout = splash->time + splash->frames[splash->currentFrame].delay 346 - SplashTime(); 347 if (timeout < 0) { 348 timeout = 0; 349 } 350 } 351 SplashUnlock(splash); 352 rc = poll(pfd, 1, timeout); 353 SplashLock(splash); 354 if (splash->isVisible > 0 && splash->currentFrame >= 0 && 355 SplashTime() >= splash->time + splash->frames[splash->currentFrame].delay) { 356 SplashNextFrame(splash); 357 SplashRedrawWindow(splash); 358 } 359 if (rc <= 0) { 360 errno = 0; 361 continue; 362 } 363 pipes_empty = 0; 364 while(!pipes_empty) { 365 char buf; 366 367 pipes_empty = 1; 368 if (read(ctl, &buf, sizeof(buf)) > 0) { 369 pipes_empty = 0; 370 switch (buf) { 371 case SPLASHCTL_UPDATE: 372 if (splash->isVisible>0) { 373 SplashRedrawWindow(splash); 374 } 375 break; 376 case SPLASHCTL_RECONFIGURE: 377 if (splash->isVisible>0) { 378 SplashReconfigureNow(splash); 379 } 380 break; 381 case SPLASHCTL_QUIT: 382 return; 383 } 384 } 385 } 386 } 387 } 388 389 void * 390 SplashScreenThread(void *param) { 391 objc_registerThreadWithCollector(); 392 393 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 394 Splash *splash = (Splash *) param; 395 396 SplashLock(splash); 397 pipe(splash->controlpipe); 398 fcntl(splash->controlpipe[0], F_SETFL, 399 fcntl(splash->controlpipe[0], F_GETFL, 0) | O_NONBLOCK); 400 splash->time = SplashTime(); 401 splash->currentFrame = 0; 402 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ 403 SplashCenter(splash); 404 405 splash->window = (void*) [[NSWindow alloc] 406 initWithContentRect: NSMakeRect(splash->x, splash->y, splash->width, splash->height) 407 styleMask: NSBorderlessWindowMask 408 backing: NSBackingStoreBuffered 409 defer: NO 410 screen: SplashNSScreen()]; 411 412 [splash->window setOpaque: NO]; 413 [splash->window setBackgroundColor: [NSColor clearColor]]; 414 }]; 415 fflush(stdout); 416 if (splash->window) { 417 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ 418 [splash->window orderFrontRegardless]; 419 }]; 420 SplashRedrawWindow(splash); 421 SplashEventLoop(splash); 422 } 423 SplashUnlock(splash); 424 SplashDone(splash); 425 426 splash->isVisible=-1; 427 428 [pool drain]; 429 430 return 0; 431 } 432 433 void 434 sendctl(Splash * splash, char code) { 435 if (splash && splash->controlpipe[1]) { 436 write(splash->controlpipe[1], &code, 1); 437 } 438 } 439 440 void 441 SplashClosePlatform(Splash * splash) { 442 sendctl(splash, SPLASHCTL_QUIT); 443 } 444 445 void 446 SplashUpdate(Splash * splash) { 447 sendctl(splash, SPLASHCTL_UPDATE); 448 } 449 450 void 451 SplashReconfigure(Splash * splash) { 452 sendctl(splash, SPLASHCTL_RECONFIGURE); 453 } 454