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 /*
  53  * Find the best possible match in the list of display modes that we can switch to based on
  54  * the provided parameters.
  55  */
  56 static CGDisplayModeRef getBestModeForParameters(CFArrayRef allModes, int w, int h, int bpp, int refrate) {
  57     CGDisplayModeRef bestGuess = NULL;
  58     CFIndex numModes = CFArrayGetCount(allModes), n;
  59     int thisBpp = 0;
  60     for(n = 0; n < numModes; n++ ) {
  61         CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
  62         if(cRef == NULL) {
  63             continue;
  64         }
  65         CFStringRef modeString = CGDisplayModeCopyPixelEncoding(cRef);
  66         thisBpp = getBPPFromModeString(modeString);
  67         CFRelease(modeString);
  68         if (thisBpp != bpp || (int)CGDisplayModeGetHeight(cRef) != h || (int)CGDisplayModeGetWidth(cRef) != w) {
  69             // One of the key parameters does not match
  70             continue;
  71         }
  72 
  73         if (refrate == 0) { // REFRESH_RATE_UNKNOWN
  74             return cRef;
  75         }
  76 
  77         // Refresh rate might be 0 in display mode and we ask for specific display rate
  78         // but if we do not find exact match then 0 refresh rate might be just Ok
  79         if (CGDisplayModeGetRefreshRate(cRef) == refrate) {
  80             // Exact match
  81             return cRef;
  82         }
  83         if (CGDisplayModeGetRefreshRate(cRef) == 0) {
  84             // Not exactly what was asked for, but may fit our needs if we don't find an exact match
  85             bestGuess = cRef;
  86         }
  87     }
  88     return bestGuess;
  89 }
  90 
  91 /*
  92  * Create a new java.awt.DisplayMode instance based on provided CGDisplayModeRef
  93  */
  94 static jobject createJavaDisplayMode(CGDisplayModeRef mode, JNIEnv *env, jint displayID) {
  95     jobject ret = NULL;
  96     jint h, w, bpp, refrate;
  97     JNF_COCOA_ENTER(env);
  98     CFStringRef currentBPP = CGDisplayModeCopyPixelEncoding(mode);
  99     bpp = getBPPFromModeString(currentBPP);
 100     refrate = CGDisplayModeGetRefreshRate(mode);
 101     h = CGDisplayModeGetHeight(mode);
 102     w = CGDisplayModeGetWidth(mode);
 103     CFRelease(currentBPP);
 104     static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
 105     static JNF_CTOR_CACHE(jc_DisplayMode_ctor, jc_DisplayMode, "(IIII)V");
 106     ret = JNFNewObject(env, jc_DisplayMode_ctor, w, h, bpp, refrate);
 107     JNF_COCOA_EXIT(env);
 108     return ret;
 109 }
 110 
 111 
 112 /*
 113  * Class:     sun_awt_CGraphicsDevice
 114  * Method:    nativeGetXResolution
 115  * Signature: (I)D
 116  */
 117 JNIEXPORT jdouble JNICALL
 118 Java_sun_awt_CGraphicsDevice_nativeGetXResolution
 119   (JNIEnv *env, jclass class, jint displayID)
 120 {
 121     // TODO: this is the physically correct answer, but we probably want
 122     // to use NSScreen API instead...
 123     CGSize size = CGDisplayScreenSize(displayID);
 124     CGRect rect = CGDisplayBounds(displayID);
 125     // 1 inch == 25.4 mm
 126     jfloat inches = size.width / 25.4f;
 127     jfloat dpi = rect.size.width / inches;
 128     return dpi;
 129 }
 130 
 131 /*
 132  * Class:     sun_awt_CGraphicsDevice
 133  * Method:    nativeGetYResolution
 134  * Signature: (I)D
 135  */
 136 JNIEXPORT jdouble JNICALL
 137 Java_sun_awt_CGraphicsDevice_nativeGetYResolution
 138   (JNIEnv *env, jclass class, jint displayID)
 139 {
 140     // TODO: this is the physically correct answer, but we probably want
 141     // to use NSScreen API instead...
 142     CGSize size = CGDisplayScreenSize(displayID);
 143     CGRect rect = CGDisplayBounds(displayID);
 144     // 1 inch == 25.4 mm
 145     jfloat inches = size.height / 25.4f;
 146     jfloat dpi = rect.size.height / inches;
 147     return dpi;
 148 }
 149 
 150 /*
 151  * Class:     sun_awt_CGraphicsDevice
 152  * Method:    nativeGetScreenInsets
 153  * Signature: (I)D
 154  */
 155 JNIEXPORT jobject JNICALL
 156 Java_sun_awt_CGraphicsDevice_nativeGetScreenInsets
 157   (JNIEnv *env, jclass class, jint displayID)
 158 {
 159     jobject ret = NULL;
 160     __block NSRect frame = NSZeroRect;
 161     __block NSRect visibleFrame = NSZeroRect;
 162 JNF_COCOA_ENTER(env);
 163     
 164     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 165         NSArray *screens = [NSScreen screens];
 166         for (NSScreen *screen in screens) {
 167             NSDictionary *screenInfo = [screen deviceDescription];
 168             NSNumber *screenID = [screenInfo objectForKey:@"NSScreenNumber"];
 169             if ([screenID pointerValue] == displayID){
 170                 frame = [screen frame];
 171                 visibleFrame = [screen visibleFrame];
 172                 break;
 173             }
 174         }
 175     }];
 176     // Convert between Cocoa's coordinate system and Java.
 177     jint bottom = visibleFrame.origin.y - frame.origin.y;
 178     jint top = frame.size.height - visibleFrame.size.height - bottom;
 179     jint left = visibleFrame.origin.x - frame.origin.x;
 180     jint right = frame.size.width - visibleFrame.size.width - left;
 181     
 182     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
 183     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
 184     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
 185 
 186 JNF_COCOA_EXIT(env);
 187 
 188     return ret;
 189 }
 190 
 191 /*
 192  * Class:     sun_awt_CGraphicsDevice
 193  * Method:    nativeSetDisplayMode
 194  * Signature: (IIIII)V
 195  */
 196 JNIEXPORT void JNICALL
 197 Java_sun_awt_CGraphicsDevice_nativeSetDisplayMode
 198 (JNIEnv *env, jclass class, jint displayID, jint w, jint h, jint bpp, jint refrate)
 199 {
 200     JNF_COCOA_ENTER(env);
 201     CFArrayRef allModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
 202     CGDisplayModeRef closestMatch = getBestModeForParameters(allModes, (int)w, (int)h, (int)bpp, (int)refrate);
 203     if (closestMatch != NULL) {
 204         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 205             CGDisplayConfigRef config;
 206             CGError retCode = CGBeginDisplayConfiguration(&config);
 207             if (retCode == kCGErrorSuccess) {
 208                 CGConfigureDisplayWithDisplayMode(config, displayID, closestMatch, NULL);
 209                 CGCompleteDisplayConfiguration(config, kCGConfigureForAppOnly);
 210                 if (config != NULL) {
 211                     CFRelease(config);
 212                 }
 213             }
 214         }];
 215     } else {
 216         [JNFException raise:env as:kIllegalArgumentException reason:"Invalid display mode"];
 217     }
 218 
 219     CFRelease(allModes);
 220     JNF_COCOA_EXIT(env);
 221 }
 222 
 223 /*
 224  * Class:     sun_awt_CGraphicsDevice
 225  * Method:    nativeGetDisplayMode
 226  * Signature: (I)Ljava/awt/DisplayMode
 227  */
 228 JNIEXPORT jobject JNICALL
 229 Java_sun_awt_CGraphicsDevice_nativeGetDisplayMode
 230 (JNIEnv *env, jclass class, jint displayID)
 231 {
 232     jobject ret = NULL;
 233     CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(displayID);
 234     ret = createJavaDisplayMode(currentMode, env, displayID);
 235     CGDisplayModeRelease(currentMode);
 236     return ret;
 237 }
 238 
 239 /*
 240  * Class:     sun_awt_CGraphicsDevice
 241  * Method:    nativeGetDisplayMode
 242  * Signature: (I)[Ljava/awt/DisplayModes
 243  */
 244 JNIEXPORT jobjectArray JNICALL
 245 Java_sun_awt_CGraphicsDevice_nativeGetDisplayModes
 246 (JNIEnv *env, jclass class, jint displayID)
 247 {
 248     jobjectArray jreturnArray = NULL;
 249     JNF_COCOA_ENTER(env);
 250     CFArrayRef allModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
 251     CFIndex numModes = CFArrayGetCount(allModes);
 252     static JNF_CLASS_CACHE(jc_DisplayMode, "java/awt/DisplayMode");
 253 
 254     jreturnArray = JNFNewObjectArray(env, &jc_DisplayMode, (jsize) numModes);
 255     if (!jreturnArray) {
 256         NSLog(@"CGraphicsDevice can't create java array of DisplayMode objects");
 257         return nil;
 258     }
 259 
 260     CFIndex n;
 261     for (n=0; n < numModes; n++) {
 262         CGDisplayModeRef cRef = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, n);
 263         if (cRef != NULL) {
 264             jobject oneMode = createJavaDisplayMode(cRef, env, displayID);
 265             (*env)->SetObjectArrayElement(env, jreturnArray, n, oneMode);
 266             if ((*env)->ExceptionOccurred(env)) {
 267                 (*env)->ExceptionDescribe(env);
 268                 (*env)->ExceptionClear(env);
 269                 continue;
 270             }
 271             (*env)->DeleteLocalRef(env, oneMode);
 272         }
 273     }
 274     CFRelease(allModes);
 275     JNF_COCOA_EXIT(env);
 276 
 277     return jreturnArray;
 278 }