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