1 /*
   2  * Copyright (c) 2012, 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.GraphicsConfiguration;
  29 import java.awt.GraphicsDevice;
  30 import java.awt.Window;
  31 import java.awt.AWTPermission;
  32 import java.awt.DisplayMode;
  33 import java.util.Objects;
  34 
  35 import sun.java2d.opengl.CGLGraphicsConfig;
  36 
  37 public final class CGraphicsDevice extends GraphicsDevice {
  38 
  39     // CoreGraphics display ID
  40     private final int displayID;
  41 
  42     // Array of all GraphicsConfig instances for this device
  43     private final GraphicsConfiguration[] configs;
  44 
  45     // Default config (temporarily hard coded)
  46     private final int DEFAULT_CONFIG = 0;
  47 
  48     private static AWTPermission fullScreenExclusivePermission;
  49 
  50     // Save/restore DisplayMode for the Full Screen mode
  51     private DisplayMode originalMode;
  52 
  53     public CGraphicsDevice(int displayID) {
  54         this.displayID = displayID;
  55         configs = new GraphicsConfiguration[] {
  56             CGLGraphicsConfig.getConfig(this, 0)
  57         };
  58     }
  59 
  60     /**
  61      * @return CoreGraphics display id.
  62      */
  63     public int getCoreGraphicsScreen() {
  64         return displayID;
  65     }
  66 
  67     /**
  68      * Return a list of all configurations.
  69      */
  70     @Override
  71     public GraphicsConfiguration[] getConfigurations() {
  72         return configs.clone();
  73     }
  74 
  75     /**
  76      * Return the default configuration.
  77      */
  78     @Override
  79     public GraphicsConfiguration getDefaultConfiguration() {
  80         return configs[DEFAULT_CONFIG];
  81     }
  82 
  83     /**
  84      * Return a human-readable screen description.
  85      */
  86     @Override
  87     public String getIDstring() {
  88         return "Display " + this.displayID;
  89     }
  90 
  91     /**
  92      * Returns the type of the graphics device.
  93      * @see #TYPE_RASTER_SCREEN
  94      * @see #TYPE_PRINTER
  95      * @see #TYPE_IMAGE_BUFFER
  96      */
  97     @Override
  98     public int getType() {
  99         return TYPE_RASTER_SCREEN;
 100     }
 101 
 102     public double getXResolution() {
 103         return nativeGetXResolution(displayID);
 104     }
 105 
 106     public double getYResolution() {
 107         return nativeGetYResolution(displayID);
 108     }
 109 
 110     private static native double nativeGetXResolution(int displayID);
 111     private static native double nativeGetYResolution(int displayID);
 112 
 113     /**
 114      * Enters full-screen mode, or returns to windowed mode.
 115      */
 116     @Override
 117     public synchronized void setFullScreenWindow(Window w) {
 118         Window old = getFullScreenWindow();
 119         if (w == old) {
 120             return;
 121         }
 122 
 123         boolean fsSupported = isFullScreenSupported();
 124 
 125         if (fsSupported && old != null) {
 126             // restore original display mode and enter windowed mode.
 127             if (originalMode != null) {
 128                 setDisplayMode(originalMode);
 129                 originalMode = null;
 130             }
 131             exitFullScreenExclusive(old);
 132         }
 133 
 134         super.setFullScreenWindow(w);
 135 
 136         if (fsSupported && w != null) {
 137             if (isDisplayChangeSupported()) {
 138                 originalMode = getDisplayMode();
 139             }
 140             // enter fullscreen mode
 141             enterFullScreenExclusive(w);
 142         }
 143     }
 144 
 145     /**
 146      * Returns true if this GraphicsDevice supports
 147      * full-screen exclusive mode and false otherwise.
 148      */
 149     @Override
 150     public boolean isFullScreenSupported() {
 151         return isFSExclusiveModeAllowed();
 152     }
 153 
 154     private static boolean isFSExclusiveModeAllowed() {
 155         SecurityManager security = System.getSecurityManager();
 156         if (security != null) {
 157             if (fullScreenExclusivePermission == null) {
 158                 fullScreenExclusivePermission =
 159                     new AWTPermission("fullScreenExclusive");
 160             }
 161             try {
 162                 security.checkPermission(fullScreenExclusivePermission);
 163             } catch (SecurityException e) {
 164                 return false;
 165             }
 166         }
 167         return true;
 168     }
 169 
 170     private static void enterFullScreenExclusive(Window w) {
 171         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 172         if (peer != null) {
 173             peer.enterFullScreenMode();
 174         }
 175     }
 176 
 177     private static void exitFullScreenExclusive(Window w) {
 178         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 179         if (peer != null) {
 180             peer.exitFullScreenMode();
 181         }
 182     }
 183 
 184     @Override
 185     public boolean isDisplayChangeSupported() {
 186         return true;
 187     }
 188 
 189     @Override
 190     public void setDisplayMode(final DisplayMode dm) {
 191         if (dm == null) {
 192             throw new IllegalArgumentException("Invalid display mode");
 193         }
 194         if (!Objects.equals(dm, getDisplayMode())) {
 195             final Window w = getFullScreenWindow();
 196             if (w != null) {
 197                 exitFullScreenExclusive(w);
 198             }
 199             nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),
 200                                  dm.getBitDepth(), dm.getRefreshRate());
 201             if (isFullScreenSupported() && w != null) {
 202                 enterFullScreenExclusive(w);
 203             }
 204         }
 205     }
 206 
 207     @Override
 208     public DisplayMode getDisplayMode() {
 209         return nativeGetDisplayMode(displayID);
 210     }
 211 
 212     @Override
 213     public DisplayMode[] getDisplayModes() {
 214         return nativeGetDisplayModes(displayID);
 215     }
 216 
 217     private native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);
 218 
 219     private native DisplayMode nativeGetDisplayMode(int displayID);
 220 
 221     private native DisplayMode[] nativeGetDisplayModes(int displayID);
 222 }