1 /*
   2  * Copyright (c) 2012, 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 #import "LWCToolkit.h"
  27 #import "ThreadUtilities.h"
  28 
  29 /*
  30  * Convert the mode string to the more convinient bits per pixel value
  31  */
  32 static int getBPPFromModeString(CFStringRef mode)
  33 {
  34     if ((CFStringCompare(mode, CFSTR(kIO30BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)) {
  35         // This is a strange mode, where we using 10 bits per RGB component and pack it into 32 bits
  36         // Java is not ready to work with this mode but we have to specify it as supported
  37         return 30;
  38     }
  39     else if (CFStringCompare(mode, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  40         return 32;
  41     }
  42     else if (CFStringCompare(mode, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  43         return 16;
  44     }
  45     else if (CFStringCompare(mode, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  46         return 8;
  47     }
  48 
  49     return 0;
  50 }
  51 
  52 static BOOL isValidDisplayMode(CGDisplayModeRef mode){
  53     return (1 < CGDisplayModeGetWidth(mode) && 1 < CGDisplayModeGetHeight(mode));
  54 }
  55 
  56 static CFMutableArrayRef getAllValidDisplayModes(jint displayID){
  57     CFArrayRef allModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
  58 
  59     CFIndex numModes = CFArrayGetCount(allModes);
  60     CFMutableArrayRef validModes = CFArrayCreateMutable(kCFAllocatorDefault, numModes + 1, NULL);
  61 
  62     CFIndex n;
  63     for (n=0; n < numModes; n++) {
  64         CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
  65         if (cRef != NULL && isValidDisplayMode(cRef)) {
  66             CFArrayAppendValue(validModes, cRef);
  67         }
  68     }
  69     CFRelease(allModes);
  70     
  71     CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
  72 
  73     BOOL containsCurrentMode = NO;
  74     numModes = CFArrayGetCount(validModes);
  75     for (n=0; n < numModes; n++) {
  76         if(CFArrayGetValueAtIndex(validModes, n) == currentMode){
  77             containsCurrentMode = YES;
  78             break;
  79         }
  80     }
  81 
  82     if (!containsCurrentMode) {
  83         CFArrayAppendValue(validModes, currentMode);
  84     }
  85     CGDisplayModeRelease(currentMode);
  86 
  87     return validModes;
  88 }
  89 
  90 /*
  91  * Find the best possible match in the list of display modes that we can switch to based on
  92  * the provided parameters.
  93  */
  94 static CGDisplayModeRef getBestModeForParameters(CFArrayRef allModes, int w, int h, int bpp, int refrate) {
  95     CGDisplayModeRef bestGuess = NULL;
  96     CFIndex numModes = CFArrayGetCount(allModes), n;
  97     int thisBpp = 0;
  98     for(n = 0; n < numModes; n++ ) {
  99         CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
 100         if(cRef == NULL) {
 101             continue;
 102         }
 103         CFStringRef modeString = CGDisplayModeCopyPixelEncoding(cRef);
 104         thisBpp = getBPPFromModeString(modeString);
 105         CFRelease(modeString);
 106         if (thisBpp != bpp || (int)CGDisplayModeGetHeight(cRef) != h || (int)CGDisplayModeGetWidth(cRef) != w) {
 107             // One of the key parameters does not match
 108             continue;
 109         }
 110 
 111         if (refrate == 0) { // REFRESH_RATE_UNKNOWN
 112             return cRef;
 113         }
 114 
 115         // Refresh rate might be 0 in display mode and we ask for specific display rate
 116         // but if we do not find exact match then 0 refresh rate might be just Ok
 117         if (CGDisplayModeGetRefreshRate(cRef) == refrate) {
 118             // Exact match
 119             return cRef;
 120         }
 121         if (CGDisplayModeGetRefreshRate(cRef) == 0) {
 122             // Not exactly what was asked for, but may fit our needs if we don't find an exact match
 123             bestGuess = cRef;
 124         }
 125     }
 126     return bestGuess;
 127 }
 128 
 129 /*
 130  * Create a new java.awt.DisplayMode instance based on provided CGDisplayModeRef
 131  */
 132 static jobject createJavaDisplayMode(CGDisplayModeRef mode, JNIEnv *env, jint displayID) {
 133     jobject ret = NULL;
 134     jint h, w, bpp, refrate;
 135     JNF_COCOA_ENTER(env);
 136     CFStringRef currentBPP = CGDisplayModeCopyPixelEncoding(mode);
 137     bpp = getBPPFromModeString(currentBPP);
 138     refrate = CGDisplayModeGetRefreshRate(mode);
 139     h = CGDisplayModeGetHeight(mode);
 140     w = CGDisplayModeGetWidth(mode);
 141     CFRelease(currentBPP);
 142     static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
 143     static JNF_CTOR_CACHE(jc_DisplayMode_ctor, jc_DisplayMode, "(IIII)V");
 144     ret = JNFNewObject(env, jc_DisplayMode_ctor, w, h, bpp, refrate);
 145     JNF_COCOA_EXIT(env);
 146     return ret;
 147 }
 148 
 149 
 150 /*
 151  * Class:     sun_awt_CGraphicsDevice
 152  * Method:    nativeGetXResolution
 153  * Signature: (I)D
 154  */
 155 JNIEXPORT jdouble JNICALL
 156 Java_sun_awt_CGraphicsDevice_nativeGetXResolution
 157   (JNIEnv *env, jclass class, jint displayID)
 158 {
 159     // TODO: this is the physically correct answer, but we probably want
 160     // to use NSScreen API instead...
 161     CGSize size = CGDisplayScreenSize(displayID);
 162     CGRect rect = CGDisplayBounds(displayID);
 163     // 1 inch == 25.4 mm
 164     jfloat inches = size.width / 25.4f;
 165     jfloat dpi = rect.size.width / inches;
 166     return dpi;
 167 }
 168 
 169 /*
 170  * Class:     sun_awt_CGraphicsDevice
 171  * Method:    nativeGetYResolution
 172  * Signature: (I)D
 173  */
 174 JNIEXPORT jdouble JNICALL
 175 Java_sun_awt_CGraphicsDevice_nativeGetYResolution
 176   (JNIEnv *env, jclass class, jint displayID)
 177 {
 178     // TODO: this is the physically correct answer, but we probably want
 179     // to use NSScreen API instead...
 180     CGSize size = CGDisplayScreenSize(displayID);
 181     CGRect rect = CGDisplayBounds(displayID);
 182     // 1 inch == 25.4 mm
 183     jfloat inches = size.height / 25.4f;
 184     jfloat dpi = rect.size.height / inches;
 185     return dpi;
 186 }
 187 
 188 /*
 189  * Class:     sun_awt_CGraphicsDevice
 190  * Method:    nativeGetScreenInsets
 191  * Signature: (I)D
 192  */
 193 JNIEXPORT jobject JNICALL
 194 Java_sun_awt_CGraphicsDevice_nativeGetScreenInsets
 195   (JNIEnv *env, jclass class, jint displayID)
 196 {
 197     jobject ret = NULL;
 198     __block NSRect frame = NSZeroRect;
 199     __block NSRect visibleFrame = NSZeroRect;
 200 JNF_COCOA_ENTER(env);
 201     
 202     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 203         NSArray *screens = [NSScreen screens];
 204         for (NSScreen *screen in screens) {
 205             NSDictionary *screenInfo = [screen deviceDescription];
 206             NSNumber *screenID = [screenInfo objectForKey:@"NSScreenNumber"];
 207             if ([screenID pointerValue] == displayID){
 208                 frame = [screen frame];
 209                 visibleFrame = [screen visibleFrame];
 210                 break;
 211             }
 212         }
 213     }];
 214     // Convert between Cocoa's coordinate system and Java.
 215     jint bottom = visibleFrame.origin.y - frame.origin.y;
 216     jint top = frame.size.height - visibleFrame.size.height - bottom;
 217     jint left = visibleFrame.origin.x - frame.origin.x;
 218     jint right = frame.size.width - visibleFrame.size.width - left;
 219     
 220     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
 221     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
 222     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
 223 
 224 JNF_COCOA_EXIT(env);
 225 
 226     return ret;
 227 }
 228 
 229 /*
 230  * Class:     sun_awt_CGraphicsDevice
 231  * Method:    nativeSetDisplayMode
 232  * Signature: (IIIII)V
 233  */
 234 JNIEXPORT void JNICALL
 235 Java_sun_awt_CGraphicsDevice_nativeSetDisplayMode
 236 (JNIEnv *env, jclass class, jint displayID, jint w, jint h, jint bpp, jint refrate)
 237 {
 238     JNF_COCOA_ENTER(env);
 239     CFArrayRef allModes = getAllValidDisplayModes(displayID);
 240 
 241     CGDisplayModeRef closestMatch = getBestModeForParameters(allModes, (int)w, (int)h, (int)bpp, (int)refrate);
 242     __block CGError retCode = kCGErrorSuccess;
 243     if (closestMatch != NULL) {
 244         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 245             CGDisplayConfigRef config;
 246             retCode = CGBeginDisplayConfiguration(&config);
 247             if (retCode == kCGErrorSuccess) {
 248                 CGConfigureDisplayWithDisplayMode(config, displayID, closestMatch, NULL);
 249                 retCode = CGCompleteDisplayConfiguration(config, kCGConfigureForAppOnly);
 250             }
 251         }];
 252     } else {
 253         [JNFException raise:env as:kIllegalArgumentException reason:"Invalid display mode"];
 254     }
 255 
 256     if (retCode != kCGErrorSuccess){
 257         [JNFException raise:env as:kIllegalArgumentException reason:"Unable to set display mode!"];
 258     }    
 259 
 260     CFRelease(allModes);
 261     JNF_COCOA_EXIT(env);
 262 }
 263 /*
 264  * Class:     sun_awt_CGraphicsDevice
 265  * Method:    nativeGetDisplayMode
 266  * Signature: (I)Ljava/awt/DisplayMode
 267  */
 268 JNIEXPORT jobject JNICALL
 269 Java_sun_awt_CGraphicsDevice_nativeGetDisplayMode
 270 (JNIEnv *env, jclass class, jint displayID)
 271 {
 272     jobject ret = NULL;
 273     CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
 274     ret = createJavaDisplayMode(currentMode, env, displayID);
 275     CGDisplayModeRelease(currentMode);
 276     return ret;
 277 }
 278 
 279 /*
 280  * Class:     sun_awt_CGraphicsDevice
 281  * Method:    nativeGetDisplayMode
 282  * Signature: (I)[Ljava/awt/DisplayModes
 283  */
 284 JNIEXPORT jobjectArray JNICALL
 285 Java_sun_awt_CGraphicsDevice_nativeGetDisplayModes
 286 (JNIEnv *env, jclass class, jint displayID)
 287 {
 288     jobjectArray jreturnArray = NULL;
 289     JNF_COCOA_ENTER(env);
 290     CFArrayRef allModes = getAllValidDisplayModes(displayID);
 291 
 292     CFIndex numModes = CFArrayGetCount(allModes);
 293     static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
 294 
 295     jreturnArray = JNFNewObjectArray(env, &jc_DisplayMode, (jsize) numModes);
 296     if (!jreturnArray) {
 297         NSLog(@"CGraphicsDevice can't create java array of DisplayMode objects");
 298         return nil;
 299     }
 300 
 301     CFIndex n;
 302     for (n=0; n < numModes; n++) {
 303         CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
 304         if (cRef != NULL) {
 305             jobject oneMode = createJavaDisplayMode(cRef, env, displayID);
 306             (*env)->SetObjectArrayElement(env, jreturnArray, n, oneMode);
 307             if ((*env)->ExceptionOccurred(env)) {
 308                 (*env)->ExceptionDescribe(env);
 309                 (*env)->ExceptionClear(env);
 310                 continue;
 311             }
 312             (*env)->DeleteLocalRef(env, oneMode);
 313         }
 314     }
 315     CFRelease(allModes);
 316     JNF_COCOA_EXIT(env);
 317 
 318     return jreturnArray;
 319 }
 320 
 321 /*
 322  * Class:     sun_awt_CGraphicsDevice
 323  * Method:    nativeGetScaleFactor
 324  * Signature: (I)D
 325  */
 326 JNIEXPORT jdouble JNICALL
 327 Java_sun_awt_CGraphicsDevice_nativeGetScaleFactor
 328 (JNIEnv *env, jclass class, jint displayID)
 329 {
 330     __block jdouble ret = 1.0f;
 331 
 332 JNF_COCOA_ENTER(env);
 333 
 334     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 335         NSArray *screens = [NSScreen screens];
 336         for (NSScreen *screen in screens) {
 337             NSDictionary *screenInfo = [screen deviceDescription];
 338             NSNumber *screenID = [screenInfo objectForKey:@"NSScreenNumber"];
 339             if ([screenID pointerValue] == displayID){
 340                 if ([screen respondsToSelector:@selector(backingScaleFactor)]) {
 341                     ret = [screen backingScaleFactor];
 342                 }
 343                 break;
 344             }
 345         }
 346     }];
 347 
 348 JNF_COCOA_EXIT(env);
 349     return ret;
 350 }