Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CGraphicsEnv.m
38829 views
1
/*
2
* Copyright (c) 2011, 2016, 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
#import <JavaNativeFoundation/JavaNativeFoundation.h>
27
28
#import "jni_util.h"
29
#import "ThreadUtilities.h"
30
#import "LWCToolkit.h"
31
#import "AWT_debug.h"
32
33
34
/*
35
* Class: sun_awt_CGraphicsEnvironment
36
* Method: initCocoa
37
* Signature: ()V
38
*/
39
JNIEXPORT void JNICALL
40
Java_sun_awt_CGraphicsEnvironment_initCocoa
41
(JNIEnv *env, jclass self)
42
{
43
JNF_COCOA_ENTER(env);
44
45
// Inform Cocoa that we're multi-threaded.
46
// Creating a short-lived NSThread is the recommended way of doing so.
47
[NSThread detachNewThreadSelector:@selector(self) toTarget:[NSObject class] withObject:nil];
48
49
JNF_COCOA_EXIT(env);
50
}
51
52
#define MAX_DISPLAYS 64
53
54
/*
55
* Class: sun_awt_CGraphicsEnvironment
56
* Method: getDisplayIDs
57
* Signature: ()[I
58
*/
59
JNIEXPORT jintArray JNICALL
60
Java_sun_awt_CGraphicsEnvironment_getDisplayIDs
61
(JNIEnv *env, jclass class)
62
{
63
jintArray ret = NULL;
64
65
JNF_COCOA_ENTER(env);
66
67
/* Get the count */
68
CGDisplayCount displayCount;
69
if (CGGetOnlineDisplayList(MAX_DISPLAYS, NULL, &displayCount) != kCGErrorSuccess) {
70
[JNFException raise:env
71
as:kInternalError
72
reason:"CGGetOnlineDisplayList() failed to get display count"];
73
return NULL;
74
}
75
76
/* Allocate an array and get the size list of display Ids */
77
CGDirectDisplayID displays[MAX_DISPLAYS];
78
if (CGGetOnlineDisplayList(displayCount, displays, &displayCount) != kCGErrorSuccess) {
79
[JNFException raise:env
80
as:kInternalError
81
reason:"CGGetOnlineDisplayList() failed to get display list"];
82
return NULL;
83
}
84
85
CGDisplayCount i;
86
CGDisplayCount displayActiveCount = 0; //Active and sleeping.
87
for (i = 0; i < displayCount; ++i) {
88
if (CGDisplayMirrorsDisplay(displays[i]) == kCGNullDirectDisplay) {
89
++displayActiveCount;
90
} else {
91
displays[i] = kCGNullDirectDisplay;
92
}
93
}
94
95
/* Allocate a java array for display identifiers */
96
ret = JNFNewIntArray(env, displayActiveCount);
97
98
/* Initialize and return the backing int array */
99
assert(sizeof(jint) >= sizeof(CGDirectDisplayID));
100
jint *elems = (*env)->GetIntArrayElements(env, ret, 0);
101
CHECK_NULL_RETURN(elems, NULL);
102
103
/* Filter out the mirrored displays */
104
for (i = 0; i < displayCount; ++i) {
105
if (displays[i] != kCGNullDirectDisplay) {
106
elems[--displayActiveCount] = displays[i];
107
}
108
}
109
110
(*env)->ReleaseIntArrayElements(env, ret, elems, 0);
111
112
JNF_COCOA_EXIT(env);
113
114
return ret;
115
}
116
117
/*
118
* Class: sun_awt_CGraphicsEnvironment
119
* Method: getMainDisplayID
120
* Signature: ()I
121
*/
122
JNIEXPORT jint JNICALL
123
Java_sun_awt_CGraphicsEnvironment_getMainDisplayID
124
(JNIEnv *env, jclass class)
125
{
126
return CGMainDisplayID();
127
}
128
129
/*
130
* Post the display reconfiguration event.
131
*/
132
static void displaycb_handle
133
(CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo)
134
{
135
if (flags == kCGDisplayBeginConfigurationFlag) return;
136
137
[ThreadUtilities performOnMainThreadWaiting:NO block:^() {
138
139
JNFPerformEnvBlock(JNFThreadDetachImmediately, ^(JNIEnv *env) {
140
JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)userInfo;
141
142
jobject graphicsEnv = [wrapper jObjectWithEnv:env];
143
if (graphicsEnv == NULL) return; // ref already GC'd
144
static JNF_CLASS_CACHE(jc_CGraphicsEnvironment, "sun/awt/CGraphicsEnvironment");
145
static JNF_MEMBER_CACHE(jm_displayReconfiguration,
146
jc_CGraphicsEnvironment, "_displayReconfiguration","(IZ)V");
147
JNFCallVoidMethod(env, graphicsEnv, jm_displayReconfiguration,
148
(jint) display, (jboolean) flags & kCGDisplayRemoveFlag);
149
(*env)->DeleteLocalRef(env, graphicsEnv);
150
});
151
}];
152
}
153
154
/*
155
* Class: sun_awt_CGraphicsEnvironment
156
* Method: registerDisplayReconfiguration
157
* Signature: ()J
158
*/
159
JNIEXPORT jlong JNICALL
160
Java_sun_awt_CGraphicsEnvironment_registerDisplayReconfiguration
161
(JNIEnv *env, jobject this)
162
{
163
jlong ret = 0L;
164
165
JNF_COCOA_ENTER(env);
166
167
JNFWeakJObjectWrapper *wrapper = [[JNFWeakJObjectWrapper wrapperWithJObject:this withEnv:env] retain];
168
169
/* Register the callback */
170
if (CGDisplayRegisterReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
171
[JNFException raise:env
172
as:kInternalError
173
reason:"CGDisplayRegisterReconfigurationCallback() failed"];
174
return 0L;
175
}
176
177
ret = ptr_to_jlong(wrapper);
178
179
JNF_COCOA_EXIT(env);
180
181
return ret;
182
}
183
184
/*
185
* Class: sun_awt_CGraphicsEnvironment
186
* Method: deregisterDisplayReconfiguration
187
* Signature: (J)V
188
*/
189
JNIEXPORT void JNICALL
190
Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration
191
(JNIEnv *env, jobject this, jlong p)
192
{
193
JNF_COCOA_ENTER(env);
194
195
JNFWeakJObjectWrapper *wrapper = (JNFWeakJObjectWrapper *)jlong_to_ptr(p);
196
if (!wrapper) return;
197
198
/* Remove the registration */
199
if (CGDisplayRemoveReconfigurationCallback(&displaycb_handle, wrapper) != kCGErrorSuccess) {
200
[JNFException raise:env
201
as:kInternalError
202
reason:"CGDisplayRemoveReconfigurationCallback() failed, leaking the callback context!"];
203
return;
204
}
205
206
[wrapper setJObject:NULL withEnv:env]; // more efficient to pre-clear
207
[wrapper release];
208
209
JNF_COCOA_EXIT(env);
210
}
211
212