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