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