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 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 * Returns CGDirectDisplayID, which is the same id as @"NSScreenNumber" in 63 * NSScreen. 64 * 65 * @return CoreGraphics display id. 66 */ 67 public int getCGDisplayID() { 68 return displayID; 69 } 70 71 /** 72 * Return a list of all configurations. 73 */ 74 @Override 75 public GraphicsConfiguration[] getConfigurations() { 76 return configs.clone(); 77 } 78 79 /** 80 * Return the default configuration. 81 */ 82 @Override 83 public GraphicsConfiguration getDefaultConfiguration() { 84 return configs[DEFAULT_CONFIG]; 85 } 86 87 /** 88 * Return a human-readable screen description. 89 */ 90 @Override 91 public String getIDstring() { 92 return "Display " + this.displayID; 93 } 94 95 /** 96 * Returns the type of the graphics device. 97 * @see #TYPE_RASTER_SCREEN 98 * @see #TYPE_PRINTER 99 * @see #TYPE_IMAGE_BUFFER 100 */ 101 @Override 102 public int getType() { 103 return TYPE_RASTER_SCREEN; 104 } 105 106 public double getXResolution() { 107 return nativeGetXResolution(displayID); 108 } 109 110 public double getYResolution() { 111 return nativeGetYResolution(displayID); 112 } 113 114 public Insets getScreenInsets() { 115 return nativeGetScreenInsets(displayID); 116 } 117 118 /** 119 * Enters full-screen mode, or returns to windowed mode. 120 */ 121 @Override 122 public synchronized void setFullScreenWindow(Window w) { 123 Window old = getFullScreenWindow(); 124 if (w == old) { 125 return; 126 } 127 128 boolean fsSupported = isFullScreenSupported(); 129 130 if (fsSupported && old != null) { 131 // restore original display mode and enter windowed mode. 132 if (originalMode != null) { 133 setDisplayMode(originalMode); 134 originalMode = null; 135 } 136 exitFullScreenExclusive(old); 137 } 138 139 super.setFullScreenWindow(w); 140 141 if (fsSupported && w != null) { 142 if (isDisplayChangeSupported()) { 143 originalMode = getDisplayMode(); 144 } 145 // enter fullscreen mode 146 enterFullScreenExclusive(w); 147 } 148 } 149 150 /** 151 * Returns true if this GraphicsDevice supports 152 * full-screen exclusive mode and false otherwise. 153 */ 154 @Override 155 public boolean isFullScreenSupported() { 156 return isFSExclusiveModeAllowed(); 157 } 158 159 private static boolean isFSExclusiveModeAllowed() { 160 SecurityManager security = System.getSecurityManager(); 161 if (security != null) { 162 if (fullScreenExclusivePermission == null) { 163 fullScreenExclusivePermission = 164 new AWTPermission("fullScreenExclusive"); 165 } 166 try { 167 security.checkPermission(fullScreenExclusivePermission); 168 } catch (SecurityException e) { 169 return false; 170 } 171 } 172 return true; 173 } 174 175 private static void enterFullScreenExclusive(Window w) { 176 FullScreenCapable peer = (FullScreenCapable)w.getPeer(); 177 if (peer != null) { 178 peer.enterFullScreenMode(); 179 } 180 } 181 182 private static void exitFullScreenExclusive(Window w) { 183 FullScreenCapable peer = (FullScreenCapable)w.getPeer(); 184 if (peer != null) { 185 peer.exitFullScreenMode(); 186 } 187 } 188 189 @Override 190 public boolean isDisplayChangeSupported() { 191 return true; 192 } 193 194 @Override 195 public void setDisplayMode(final DisplayMode dm) { 196 if (dm == null) { 197 throw new IllegalArgumentException("Invalid display mode"); 198 } 199 if (!Objects.equals(dm, getDisplayMode())) { 200 final Window w = getFullScreenWindow(); 201 if (w != null) { 202 exitFullScreenExclusive(w); 203 } 204 nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(), 205 dm.getBitDepth(), dm.getRefreshRate()); 206 if (isFullScreenSupported() && w != null) { 207 enterFullScreenExclusive(w); 208 } 209 } 210 } 211 212 @Override 213 public DisplayMode getDisplayMode() { 214 return nativeGetDisplayMode(displayID); 215 } 216 217 @Override 218 public DisplayMode[] getDisplayModes() { 219 return nativeGetDisplayModes(displayID); 220 } 221 222 public int getScaleFactor() { 223 return (int) nativeGetScaleFactor(displayID); 224 } 225 226 private static native double nativeGetScaleFactor(int displayID); 227 228 private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate); 229 230 private static native DisplayMode nativeGetDisplayMode(int displayID); 231 232 private static native DisplayMode[] nativeGetDisplayModes(int displayID); 233 234 private static native double nativeGetXResolution(int displayID); 235 236 private static native double nativeGetYResolution(int displayID); 237 238 private static native Insets nativeGetScreenInsets(int displayID); 239 }