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