1 /* 2 * Copyright (c) 2011, 2018, 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.AWTError; 29 import java.awt.Font; 30 import java.awt.GraphicsConfiguration; 31 import java.awt.GraphicsDevice; 32 import java.awt.HeadlessException; 33 import java.awt.Toolkit; 34 import java.lang.ref.WeakReference; 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.List; 38 import java.util.ListIterator; 39 import java.util.Map; 40 41 import sun.java2d.MacosxSurfaceManagerFactory; 42 import sun.java2d.SunGraphicsEnvironment; 43 import sun.java2d.SurfaceManagerFactory; 44 45 /** 46 * This is an implementation of a GraphicsEnvironment object for the default 47 * local GraphicsEnvironment used by the Java Runtime Environment for Mac OS X 48 * GUI environments. 49 * 50 * @see GraphicsDevice 51 * @see GraphicsConfiguration 52 */ 53 public final class CGraphicsEnvironment extends SunGraphicsEnvironment { 54 55 /** 56 * Fetch an array of all valid CoreGraphics display identifiers. 57 */ 58 private static native int[] getDisplayIDs(); 59 60 /** 61 * Fetch the CoreGraphics display ID for the 'main' display. 62 */ 63 private static native int getMainDisplayID(); 64 65 /** 66 * Noop function that just acts as an entry point for someone to force a 67 * static initialization of this class. 68 */ 69 public static void init() { } 70 71 static { 72 // Load libraries and initialize the Toolkit. 73 Toolkit.getDefaultToolkit(); 74 // Install the correct surface manager factory. 75 SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory()); 76 } 77 78 /** 79 * Register the instance with CGDisplayRegisterReconfigurationCallback(). 80 * The registration uses a weak global reference -- if our instance is 81 * garbage collected, the reference will be dropped. 82 * 83 * @return Return the registration context (a pointer). 84 */ 85 private native long registerDisplayReconfiguration(); 86 87 /** 88 * Remove the instance's registration with CGDisplayRemoveReconfigurationCallback() 89 */ 90 private native void deregisterDisplayReconfiguration(long context); 91 92 /** Available CoreGraphics displays. */ 93 private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5); 94 /** 95 * The key in the {@link #devices} for the main display. 96 */ 97 private int mainDisplayID; 98 99 /** Reference to the display reconfiguration callback context. */ 100 private final long displayReconfigContext; 101 102 // list of invalidated graphics devices (those which were removed) 103 private List<WeakReference<CGraphicsDevice>> oldDevices = new ArrayList<>(); 104 105 /** 106 * Construct a new instance. 107 */ 108 public CGraphicsEnvironment() { 109 if (isHeadless()) { 110 displayReconfigContext = 0L; 111 return; 112 } 113 114 /* Populate the device table */ 115 initDevices(); 116 117 /* Register our display reconfiguration listener */ 118 displayReconfigContext = registerDisplayReconfiguration(); 119 if (displayReconfigContext == 0L) { 120 throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback"); 121 } 122 } 123 124 /** 125 * Called by the CoreGraphics Display Reconfiguration Callback. 126 * 127 * @param displayId CoreGraphics displayId 128 * @param removed true if displayId was removed, false otherwise. 129 */ 130 void _displayReconfiguration(final int displayId, final boolean removed) { 131 synchronized (this) { 132 if (removed && devices.containsKey(displayId)) { 133 final CGraphicsDevice gd = devices.remove(displayId); 134 oldDevices.add(new WeakReference<>(gd)); 135 } 136 } 137 initDevices(); 138 139 // Need to notify old devices, in case the user hold the reference to it 140 for (ListIterator<WeakReference<CGraphicsDevice>> it = 141 oldDevices.listIterator(); it.hasNext(); ) { 142 CGraphicsDevice gd = it.next().get(); 143 if (gd != null) { 144 gd.invalidate(mainDisplayID); 145 gd.displayChanged(); 146 } else { 147 // no more references to this device, remove it 148 it.remove(); 149 } 150 } 151 } 152 153 @Override 154 @SuppressWarnings("deprecation") 155 protected void finalize() throws Throwable { 156 try { 157 super.finalize(); 158 } finally { 159 deregisterDisplayReconfiguration(displayReconfigContext); 160 } 161 } 162 163 /** 164 * (Re)create all CGraphicsDevices, reuses a devices if it is possible. 165 */ 166 private void initDevices() { 167 synchronized (this) { 168 final Map<Integer, CGraphicsDevice> old = new HashMap<>(devices); 169 devices.clear(); 170 171 mainDisplayID = getMainDisplayID(); 172 173 // initialization of the graphics device may change 174 // list of displays on hybrid systems via an activation 175 // of discrete video. 176 // So, we initialize the main display first, and then 177 // retrieve actual list of displays. 178 if (!old.containsKey(mainDisplayID)) { 179 old.put(mainDisplayID, new CGraphicsDevice(mainDisplayID)); 180 } 181 182 for (final int id : getDisplayIDs()) { 183 devices.put(id, old.containsKey(id) ? old.get(id) 184 : new CGraphicsDevice(id)); 185 } 186 } 187 displayChanged(); 188 } 189 190 @Override 191 public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException { 192 CGraphicsDevice d = devices.get(mainDisplayID); 193 if (d == null) { 194 // we do not expect that this may happen, the only response 195 // is to re-initialize the list of devices 196 initDevices(); 197 198 d = devices.get(mainDisplayID); 199 if (d == null) { 200 throw new AWTError("no screen devices"); 201 } 202 } 203 return d; 204 } 205 206 @Override 207 public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException { 208 return devices.values().toArray(new CGraphicsDevice[devices.values().size()]); 209 } 210 211 public synchronized GraphicsDevice getScreenDevice(int displayID) { 212 return devices.get(displayID); 213 } 214 215 @Override 216 protected synchronized int getNumScreens() { 217 return devices.size(); 218 } 219 220 @Override 221 protected GraphicsDevice makeScreenDevice(int screennum) { 222 throw new UnsupportedOperationException("This method is unused and should not be called in this implementation"); 223 } 224 225 @Override 226 public boolean isDisplayLocal() { 227 return true; 228 } 229 230 static String[] sLogicalFonts = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" }; 231 232 @Override 233 public Font[] getAllFonts() { 234 235 Font[] newFonts; 236 Font[] superFonts = super.getAllFonts(); 237 238 int numLogical = sLogicalFonts.length; 239 int numOtherFonts = superFonts.length; 240 241 newFonts = new Font[numOtherFonts + numLogical]; 242 System.arraycopy(superFonts,0,newFonts,numLogical,numOtherFonts); 243 244 for (int i = 0; i < numLogical; i++) 245 { 246 newFonts[i] = new Font(sLogicalFonts[i], Font.PLAIN, 1); 247 } 248 return newFonts; 249 } 250 251 }