1 /* 2 * Copyright (c) 2012, 2014, 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 implements DisplayChangedListener { 40 41 /** 42 * CoreGraphics display ID. This identifier can become non-valid at any time 43 * therefore methods, which is using this id should be ready to it. 44 */ 45 private volatile int displayID; 46 private volatile Insets screenInsets; 47 private volatile double xResolution; 48 private volatile double yResolution; 49 private volatile int scale; 50 51 // Array of all GraphicsConfig instances for this device 52 private final GraphicsConfiguration[] configs; 53 54 // Default config (temporarily hard coded) 55 private final int DEFAULT_CONFIG = 0; 56 57 private static AWTPermission fullScreenExclusivePermission; 58 59 // Save/restore DisplayMode for the Full Screen mode 60 private DisplayMode originalMode; 61 62 public CGraphicsDevice(final int displayID) { 63 this.displayID = displayID; 64 configs = new GraphicsConfiguration[] { 65 CGLGraphicsConfig.getConfig(this, 0) 66 }; 67 } 68 69 /** 70 * Returns CGDirectDisplayID, which is the same id as @"NSScreenNumber" in 71 * NSScreen. 72 * 73 * @return CoreGraphics display id. 74 */ 75 public int getCGDisplayID() { 76 return displayID; 77 } 78 79 /** 80 * Return a list of all configurations. 81 */ 82 @Override 83 public GraphicsConfiguration[] getConfigurations() { 84 return configs.clone(); 85 } 86 87 /** 88 * Return the default configuration. 89 */ 90 @Override 91 public GraphicsConfiguration getDefaultConfiguration() { 92 return configs[DEFAULT_CONFIG]; 93 } 94 95 /** 96 * Return a human-readable screen description. 97 */ 98 @Override 99 public String getIDstring() { 100 return "Display " + displayID; 101 } 102 103 /** 104 * Returns the type of the graphics device. 105 * @see #TYPE_RASTER_SCREEN 106 * @see #TYPE_PRINTER 107 * @see #TYPE_IMAGE_BUFFER 108 */ 109 @Override 110 public int getType() { 111 return TYPE_RASTER_SCREEN; 112 } 113 114 public double getXResolution() { 115 return xResolution; 116 } 117 118 public double getYResolution() { 119 return yResolution; 120 } 121 122 public Insets getScreenInsets() { 123 return screenInsets; 124 } 125 126 public int getScaleFactor() { 127 return scale; 128 } 129 130 public void invalidate(final int defaultDisplayID) { 131 displayID = defaultDisplayID; 132 } 133 134 @Override 135 public void displayChanged() { 136 xResolution = nativeGetXResolution(displayID); 137 yResolution = nativeGetYResolution(displayID); 138 screenInsets = nativeGetScreenInsets(displayID); 139 scale = (int) nativeGetScaleFactor(displayID); 140 //TODO configs/fullscreenWindow/modes? 141 } 142 143 @Override 144 public void paletteChanged() { 145 // devices do not need to react to this event. 146 } 147 148 /** 149 * Enters full-screen mode, or returns to windowed mode. 150 */ 151 @Override 152 public synchronized void setFullScreenWindow(Window w) { 153 Window old = getFullScreenWindow(); 154 if (w == old) { 155 return; 156 } 157 158 boolean fsSupported = isFullScreenSupported(); 159 160 if (fsSupported && old != null) { 161 // enter windowed mode and restore original display mode 162 exitFullScreenExclusive(old); 163 if (originalMode != null) { 164 setDisplayMode(originalMode); 165 originalMode = null; 166 } 167 } 168 169 super.setFullScreenWindow(w); 170 171 if (fsSupported && w != null) { 172 if (isDisplayChangeSupported()) { 173 originalMode = getDisplayMode(); 174 } 175 // enter fullscreen mode 176 enterFullScreenExclusive(w); 177 } 178 } 179 180 /** 181 * Returns true if this GraphicsDevice supports 182 * full-screen exclusive mode and false otherwise. 183 */ 184 @Override 185 public boolean isFullScreenSupported() { 186 return isFSExclusiveModeAllowed(); 187 } 188 189 private static boolean isFSExclusiveModeAllowed() { 190 SecurityManager security = System.getSecurityManager(); 191 if (security != null) { 192 if (fullScreenExclusivePermission == null) { 193 fullScreenExclusivePermission = 194 new AWTPermission("fullScreenExclusive"); 195 } 196 try { 197 security.checkPermission(fullScreenExclusivePermission); 198 } catch (SecurityException e) { 199 return false; 200 } 201 } 202 return true; 203 } 204 205 @SuppressWarnings("deprecation") 206 private static void enterFullScreenExclusive(Window w) { 207 FullScreenCapable peer = (FullScreenCapable)w.getPeer(); 208 if (peer != null) { 209 peer.enterFullScreenMode(); 210 } 211 } 212 213 @SuppressWarnings("deprecation") 214 private static void exitFullScreenExclusive(Window w) { 215 FullScreenCapable peer = (FullScreenCapable)w.getPeer(); 216 if (peer != null) { 217 peer.exitFullScreenMode(); 218 } 219 } 220 221 @Override 222 public boolean isDisplayChangeSupported() { 223 return true; 224 } 225 226 @Override 227 public void setDisplayMode(final DisplayMode dm) { 228 if (dm == null) { 229 throw new IllegalArgumentException("Invalid display mode"); 230 } 231 if (!Objects.equals(dm, getDisplayMode())) { 232 nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(), 233 dm.getBitDepth(), dm.getRefreshRate()); 234 if (isFullScreenSupported() && getFullScreenWindow() != null) { 235 getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight()); 236 } 237 } 238 } 239 240 @Override 241 public DisplayMode getDisplayMode() { 242 return nativeGetDisplayMode(displayID); 243 } 244 245 @Override 246 public DisplayMode[] getDisplayModes() { 247 return nativeGetDisplayModes(displayID); 248 } 249 250 private static native double nativeGetScaleFactor(int displayID); 251 252 private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate); 253 254 private static native DisplayMode nativeGetDisplayMode(int displayID); 255 256 private static native DisplayMode[] nativeGetDisplayModes(int displayID); 257 258 private static native double nativeGetXResolution(int displayID); 259 260 private static native double nativeGetYResolution(int displayID); 261 262 private static native Insets nativeGetScreenInsets(int displayID); 263 }