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