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 package sun.awt;
  27 
  28 import java.awt.AWTPermission;
  29 import java.awt.DisplayMode;
  30 import java.awt.GraphicsConfiguration;
  31 import java.awt.GraphicsDevice;
  32 import java.awt.Insets;
  33 import java.awt.Window;
  34 import java.util.Objects;
  35 
  36 import sun.java2d.opengl.CGLGraphicsConfig;
  37 
  38 public final class CGraphicsDevice extends GraphicsDevice
  39         implements DisplayChangedListener {
  40 
  41     /**
  42      * CoreGraphics display ID. This identifier can become non-valid at any time
  43      * therefore methods, which is using this id should be ready to it.
  44      */
  45     private volatile int displayID;
  46     private volatile Insets screenInsets;
  47     private volatile double xResolution;
  48     private volatile double yResolution;
  49     private volatile int scale;
  50 
  51     // Array of all GraphicsConfig instances for this device
  52     private final GraphicsConfiguration[] configs;
  53 
  54     // Default config (temporarily hard coded)
  55     private final int DEFAULT_CONFIG = 0;
  56 
  57     private static AWTPermission fullScreenExclusivePermission;
  58 
  59     // Save/restore DisplayMode for the Full Screen mode
  60     private DisplayMode originalMode;
  61 
  62     public CGraphicsDevice(final int displayID) {
  63         this.displayID = displayID;
  64         configs = new GraphicsConfiguration[] {
  65             CGLGraphicsConfig.getConfig(this, 0)
  66         };
  67     }
  68 
  69     /**
  70      * Returns CGDirectDisplayID, which is the same id as @"NSScreenNumber" in
  71      * NSScreen.
  72      *
  73      * @return CoreGraphics display id.
  74      */
  75     public int getCGDisplayID() {
  76         return displayID;
  77     }
  78 
  79     /**
  80      * Return a list of all configurations.
  81      */
  82     @Override
  83     public GraphicsConfiguration[] getConfigurations() {
  84         return configs.clone();
  85     }
  86 
  87     /**
  88      * Return the default configuration.
  89      */
  90     @Override
  91     public GraphicsConfiguration getDefaultConfiguration() {
  92         return configs[DEFAULT_CONFIG];
  93     }
  94 
  95     /**
  96      * Return a human-readable screen description.
  97      */
  98     @Override
  99     public String getIDstring() {
 100         return "Display " + displayID;
 101     }
 102 
 103     /**
 104      * Returns the type of the graphics device.
 105      * @see #TYPE_RASTER_SCREEN
 106      * @see #TYPE_PRINTER
 107      * @see #TYPE_IMAGE_BUFFER
 108      */
 109     @Override
 110     public int getType() {
 111         return TYPE_RASTER_SCREEN;
 112     }
 113 
 114     public double getXResolution() {
 115         return xResolution;
 116     }
 117 
 118     public double getYResolution() {
 119         return yResolution;
 120     }
 121 
 122     public Insets getScreenInsets() {
 123         return screenInsets;
 124     }
 125 
 126     public int getScaleFactor() {
 127         return scale;
 128     }
 129 
 130     public void invalidate(final int defaultDisplayID) {
 131         displayID = defaultDisplayID;
 132     }
 133     
 134     @Override
 135     public void displayChanged() {
 136         xResolution = nativeGetXResolution(displayID);
 137         yResolution = nativeGetYResolution(displayID);
 138         screenInsets = nativeGetScreenInsets(displayID);
 139         scale = (int) nativeGetScaleFactor(displayID);
 140         //TODO configs/fullscreenWindow/modes?
 141     }
 142 
 143     @Override
 144     public void paletteChanged() {
 145         // devices do not need to react to this event.
 146     }
 147 
 148     /**
 149      * Enters full-screen mode, or returns to windowed mode.
 150      */
 151     @Override
 152     public synchronized void setFullScreenWindow(Window w) {
 153         Window old = getFullScreenWindow();
 154         if (w == old) {
 155             return;
 156         }
 157 
 158         boolean fsSupported = isFullScreenSupported();
 159 
 160         if (fsSupported && old != null) {
 161             // restore original display mode and enter windowed mode.
 162             if (originalMode != null) {
 163                 setDisplayMode(originalMode);
 164                 originalMode = null;
 165             }
 166             exitFullScreenExclusive(old);
 167         }
 168 
 169         super.setFullScreenWindow(w);
 170 
 171         if (fsSupported && w != null) {
 172             if (isDisplayChangeSupported()) {
 173                 originalMode = getDisplayMode();
 174             }
 175             // enter fullscreen mode
 176             enterFullScreenExclusive(w);
 177         }
 178     }
 179 
 180     /**
 181      * Returns true if this GraphicsDevice supports
 182      * full-screen exclusive mode and false otherwise.
 183      */
 184     @Override
 185     public boolean isFullScreenSupported() {
 186         return isFSExclusiveModeAllowed();
 187     }
 188 
 189     private static boolean isFSExclusiveModeAllowed() {
 190         SecurityManager security = System.getSecurityManager();
 191         if (security != null) {
 192             if (fullScreenExclusivePermission == null) {
 193                 fullScreenExclusivePermission =
 194                     new AWTPermission("fullScreenExclusive");
 195             }
 196             try {
 197                 security.checkPermission(fullScreenExclusivePermission);
 198             } catch (SecurityException e) {
 199                 return false;
 200             }
 201         }
 202         return true;
 203     }
 204 
 205     private static void enterFullScreenExclusive(Window w) {
 206         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 207         if (peer != null) {
 208             peer.enterFullScreenMode();
 209         }
 210     }
 211 
 212     private static void exitFullScreenExclusive(Window w) {
 213         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 214         if (peer != null) {
 215             peer.exitFullScreenMode();
 216         }
 217     }
 218 
 219     @Override
 220     public boolean isDisplayChangeSupported() {
 221         return true;
 222     }
 223 
 224     @Override
 225     public void setDisplayMode(final DisplayMode dm) {
 226         if (dm == null) {
 227             throw new IllegalArgumentException("Invalid display mode");
 228         }
 229         if (!Objects.equals(dm, getDisplayMode())) {
 230             final Window w = getFullScreenWindow();
 231             if (w != null) {
 232                 exitFullScreenExclusive(w);
 233             }
 234             nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),
 235                                  dm.getBitDepth(), dm.getRefreshRate());
 236             if (isFullScreenSupported() && w != null) {
 237                 enterFullScreenExclusive(w);
 238             }
 239         }
 240     }
 241 
 242     @Override
 243     public DisplayMode getDisplayMode() {
 244         return nativeGetDisplayMode(displayID);
 245     }
 246 
 247     @Override
 248     public DisplayMode[] getDisplayModes() {
 249         return nativeGetDisplayModes(displayID);
 250     }
 251 
 252     private static native double nativeGetScaleFactor(int displayID);
 253 
 254     private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);
 255 
 256     private static native DisplayMode nativeGetDisplayMode(int displayID);
 257 
 258     private static native DisplayMode[] nativeGetDisplayModes(int displayID);
 259 
 260     private static native double nativeGetXResolution(int displayID);
 261 
 262     private static native double nativeGetYResolution(int displayID);
 263 
 264     private static native Insets nativeGetScreenInsets(int displayID);
 265 }