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 public final class CGraphicsDevice extends GraphicsDevice { 37 38 // CoreGraphics display ID 39 private final int displayID; 40 41 // Array of all GraphicsConfig instances for this device 42 private final GraphicsConfiguration[] configs; 43 44 // Default config (temporarily hard coded) 45 private final int DEFAULT_CONFIG = 0; 46 47 private static AWTPermission fullScreenExclusivePermission; 48 49 // Save/restore DisplayMode for the Full Screen mode 50 private DisplayMode originalMode; 51 52 public CGraphicsDevice(int displayID) { 53 this.displayID = displayID; 54 configs = new GraphicsConfiguration[] { 55 CGLGraphicsConfig.getConfig(this, 0) 56 }; 57 } 58 59 /** 60 * @return CoreGraphics display id. 61 */ 62 public int getCoreGraphicsScreen() { 63 return displayID; 64 } 65 66 /** 67 * Return a list of all configurations. 68 */ 69 @Override 70 public GraphicsConfiguration[] getConfigurations() { 71 return configs.clone(); 72 } 73 74 /** 75 * Return the default configuration. 76 */ 77 @Override 78 public GraphicsConfiguration getDefaultConfiguration() { 79 return configs[DEFAULT_CONFIG]; 80 } 81 82 /** 83 * Return a human-readable screen description. 84 */ 85 @Override 86 public String getIDstring() { 87 return "Display " + this.displayID; 88 } 89 90 /** 91 * Returns the type of the graphics device. 92 * @see #TYPE_RASTER_SCREEN 93 * @see #TYPE_PRINTER 94 * @see #TYPE_IMAGE_BUFFER 95 */ 96 @Override 97 public int getType() { 98 return TYPE_RASTER_SCREEN; 99 } 100 101 public double getXResolution() { 102 return nativeGetXResolution(displayID); 103 } 104 105 public double getYResolution() { 106 return nativeGetYResolution(displayID); 107 } 108 109 private static native double nativeGetXResolution(int displayID); 110 private static native double nativeGetYResolution(int displayID); 111 112 /** 113 * Enters full-screen mode, or returns to windowed mode. 114 */ 115 @Override 116 public synchronized void setFullScreenWindow(Window w) { 117 Window old = getFullScreenWindow(); 118 if (w == old) { 119 return; 120 } 121 122 boolean fsSupported = isFullScreenSupported(); 123 124 if (fsSupported && old != null) { 125 // enter windowed mode (and restore original display mode) 126 exitFullScreenExclusive(old); 127 if (originalMode != null) { 128 setDisplayMode(originalMode); 129 originalMode = null; 130 } 131 } 132 133 super.setFullScreenWindow(w); 134 135 if (fsSupported && w != null) { 136 if (isDisplayChangeSupported()) { 137 originalMode = getDisplayMode(); 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 if (dm == null) { 191 throw new IllegalArgumentException("Invalid display mode"); 192 } 193 nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(), dm.getBitDepth(), dm.getRefreshRate()); 194 if (isFullScreenSupported() && getFullScreenWindow() != null) { 195 getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight()); 196 } 197 } 198 199 @Override 200 public DisplayMode getDisplayMode() { 201 return nativeGetDisplayMode(displayID); 202 } 203 204 @Override 205 public DisplayMode[] getDisplayModes() { 206 return nativeGetDisplayModes(displayID); 207 } 208 209 private native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate); 210 211 private native DisplayMode nativeGetDisplayMode(int displayID); 212 213 private native DisplayMode[] nativeGetDisplayModes(int displayID); 214 }