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         boolean dcSupported = isDisplayChangeSupported();
 131 
 132         if (fsSupported && old != null) {
 133             // enter windowed mode (and restore original display mode)
 134             exitFullScreenExclusive(old);
 135             if (originalMode != null) {
 136                 if (dcSupported) {
 137                     setDisplayMode(originalMode);
 138                 }
 139                 originalMode = null;
 140             }
 141         }
 142 
 143 
 144         super.setFullScreenWindow(w);
 145 
 146         if (fsSupported && w != null) {
 147             if (dcSupported) {
 148                 originalMode = getDisplayMode();
 149             }
 150             // enter fullscreen mode
 151             enterFullScreenExclusive(w);
 152         }
 153     }
 154 
 155     /**
 156      * Returns true if this GraphicsDevice supports
 157      * full-screen exclusive mode and false otherwise.
 158      */
 159     @Override
 160     public boolean isFullScreenSupported() {
 161         return isFSExclusiveModeAllowed();
 162     }
 163 
 164     private static boolean isFSExclusiveModeAllowed() {
 165         SecurityManager security = System.getSecurityManager();
 166         if (security != null) {
 167             if (fullScreenExclusivePermission == null) {
 168                 fullScreenExclusivePermission =
 169                     new AWTPermission("fullScreenExclusive");
 170             }
 171             try {
 172                 security.checkPermission(fullScreenExclusivePermission);
 173             } catch (SecurityException e) {
 174                 return false;
 175             }
 176         }
 177         return true;
 178     }
 179 
 180     private static void enterFullScreenExclusive(Window w) {
 181         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 182         if (peer != null) {
 183             peer.enterFullScreenMode();
 184         }
 185     }
 186 
 187     private static void exitFullScreenExclusive(Window w) {
 188         FullScreenCapable peer = (FullScreenCapable)w.getPeer();
 189         if (peer != null) {
 190             peer.exitFullScreenMode();
 191         }
 192     }
 193 
 194     @Override
 195     public boolean isDisplayChangeSupported() {
 196         return true;
 197     }
 198 
 199     @Override
 200     public void setDisplayMode(DisplayMode dm) {
 201         nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(), dm.getBitDepth(), dm.getRefreshRate());
 202         if (isFullScreenSupported() && getFullScreenWindow() != null) {
 203             getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight());
 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 }