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