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