1 /*
2 * Copyright (c) 2011, 2013, 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
62 // Install the correct surface manager factory.
63 SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());
64 }
65
66 /**
67 * Register the instance with CGDisplayRegisterReconfigurationCallback().
68 * The registration uses a weak global reference -- if our instance is
69 * garbage collected, the reference will be dropped.
70 *
71 * @return Return the registration context (a pointer).
72 */
73 private native long registerDisplayReconfiguration();
74
75 /**
76 * Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
77 */
78 private native void deregisterDisplayReconfiguration(long context);
79
80 /** Available CoreGraphics displays. */
81 private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);
82
83 /** Reference to the display reconfiguration callback context. */
84 private final long displayReconfigContext;
85
86 /**
87 * Construct a new instance.
88 */
89 public CGraphicsEnvironment() {
90 if (isHeadless()) {
91 displayReconfigContext = 0L;
92 return;
93 }
94
95 /* Populate the device table */
96 initDevices();
97
98 /* Register our display reconfiguration listener */
99 displayReconfigContext = registerDisplayReconfiguration();
100 if (displayReconfigContext == 0L) {
101 throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
119 initDevices();
120 }
121
122 @Override
123 protected void finalize() throws Throwable {
124 try {
125 super.finalize();
126 } finally {
127 deregisterDisplayReconfiguration(displayReconfigContext);
128 }
129 }
130
131 /**
132 * (Re)create all CGraphicsDevices, reuses a devices if it is possible.
133 */
134 private void initDevices() {
135 synchronized (this) {
136 final Map<Integer, CGraphicsDevice> old = new HashMap<>(devices);
137 devices.clear();
138
139 int mainID = getMainDisplayID();
140
141 // initialization of the graphics device may change
142 // list of displays on hybrid systems via an activation
143 // of discrete video.
144 // So, we initialize the main display first, and then
145 // retrieve actual list of displays.
146 if (!old.containsKey(mainID)) {
147 old.put(mainID, new CGraphicsDevice(mainID));
148 }
149
150 for (final int id : getDisplayIDs()) {
151 devices.put(id, old.containsKey(id) ? old.get(id)
152 : new CGraphicsDevice(id));
153 }
154 }
155 displayChanged();
156 }
157
158 @Override
159 public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {
160 final int mainDisplayID = getMainDisplayID();
161 CGraphicsDevice d = devices.get(mainDisplayID);
162 if (d == null) {
163 // we do not expect that this may happen, the only response
164 // is to re-initialize the list of devices
165 initDevices();
166
167 d = devices.get(mainDisplayID);
168 if (d == null) {
169 throw new AWTError("no screen devices");
170 }
171 }
172 return d;
173 }
174
175 @Override
176 public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {
177 return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
178 }
179
180 public synchronized GraphicsDevice getScreenDevice(int displayID) {
|
1 /*
2 * Copyright (c) 2011, 2017, 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
62 // Install the correct surface manager factory.
63 SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());
64 }
65
66 /**
67 * Register the instance with CGDisplayRegisterReconfigurationCallback().
68 * The registration uses a weak global reference -- if our instance is
69 * garbage collected, the reference will be dropped.
70 *
71 * @return Return the registration context (a pointer).
72 */
73 private native long registerDisplayReconfiguration();
74
75 /**
76 * Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
77 */
78 private native void deregisterDisplayReconfiguration(long context);
79
80 /** Available CoreGraphics displays. */
81 private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);
82 /**
83 * The key in the {@link #devices} for the main display.
84 */
85 private int mainDisplayID;
86
87 /** Reference to the display reconfiguration callback context. */
88 private final long displayReconfigContext;
89
90 /**
91 * Construct a new instance.
92 */
93 public CGraphicsEnvironment() {
94 if (isHeadless()) {
95 displayReconfigContext = 0L;
96 return;
97 }
98
99 /* Populate the device table */
100 initDevices();
101
102 /* Register our display reconfiguration listener */
103 displayReconfigContext = registerDisplayReconfiguration();
104 if (displayReconfigContext == 0L) {
105 throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
123 initDevices();
124 }
125
126 @Override
127 protected void finalize() throws Throwable {
128 try {
129 super.finalize();
130 } finally {
131 deregisterDisplayReconfiguration(displayReconfigContext);
132 }
133 }
134
135 /**
136 * (Re)create all CGraphicsDevices, reuses a devices if it is possible.
137 */
138 private void initDevices() {
139 synchronized (this) {
140 final Map<Integer, CGraphicsDevice> old = new HashMap<>(devices);
141 devices.clear();
142
143 mainDisplayID = getMainDisplayID();
144
145 // initialization of the graphics device may change
146 // list of displays on hybrid systems via an activation
147 // of discrete video.
148 // So, we initialize the main display first, and then
149 // retrieve actual list of displays.
150 if (!old.containsKey(mainDisplayID)) {
151 old.put(mainDisplayID, new CGraphicsDevice(mainDisplayID));
152 }
153
154 for (final int id : getDisplayIDs()) {
155 devices.put(id, old.containsKey(id) ? old.get(id)
156 : new CGraphicsDevice(id));
157 }
158 }
159 displayChanged();
160 }
161
162 @Override
163 public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {
164 CGraphicsDevice d = devices.get(mainDisplayID);
165 if (d == null) {
166 // we do not expect that this may happen, the only response
167 // is to re-initialize the list of devices
168 initDevices();
169
170 d = devices.get(mainDisplayID);
171 if (d == null) {
172 throw new AWTError("no screen devices");
173 }
174 }
175 return d;
176 }
177
178 @Override
179 public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {
180 return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
181 }
182
183 public synchronized GraphicsDevice getScreenDevice(int displayID) {
|