Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
66645 views
1
/*
2
* Copyright (c) 2000, 2021, 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
#include <stdio.h>
27
#include <dlfcn.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <jni.h>
31
#include <jni_util.h>
32
#include <jvm.h>
33
#include "gdefs.h"
34
35
#include <sys/param.h>
36
#include <sys/utsname.h>
37
38
#ifdef AIX
39
#include "porting_aix.h" /* For the 'dladdr' function. */
40
#endif
41
42
#ifdef __APPLE__
43
#include <TargetConditionals.h>
44
#endif
45
46
#ifdef DEBUG
47
#define VERBOSE_AWT_DEBUG
48
#endif
49
50
static void *awtHandle = NULL;
51
52
typedef jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved);
53
54
/* Initialize the Java VM instance variable when the library is
55
first loaded */
56
JNIEXPORT JavaVM *jvm;
57
58
JNIEXPORT jboolean JNICALL AWTIsHeadless() {
59
static JNIEnv *env = NULL;
60
static jboolean isHeadless;
61
jmethodID headlessFn;
62
jclass graphicsEnvClass;
63
64
if (env == NULL) {
65
env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
66
graphicsEnvClass = (*env)->FindClass(env,
67
"java/awt/GraphicsEnvironment");
68
if (graphicsEnvClass == NULL) {
69
return JNI_TRUE;
70
}
71
headlessFn = (*env)->GetStaticMethodID(env,
72
graphicsEnvClass, "isHeadless", "()Z");
73
if (headlessFn == NULL) {
74
return JNI_TRUE;
75
}
76
isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,
77
headlessFn);
78
if ((*env)->ExceptionCheck(env)) {
79
(*env)->ExceptionClear(env);
80
return JNI_TRUE;
81
}
82
}
83
return isHeadless;
84
}
85
86
#define CHECK_EXCEPTION_FATAL(env, message) \
87
if ((*env)->ExceptionCheck(env)) { \
88
(*env)->ExceptionClear(env); \
89
(*env)->FatalError(env, message); \
90
}
91
92
/*
93
* Pathnames to the various awt toolkits
94
*/
95
96
#ifdef TARGET_OS_IPHONE
97
#undef MACOSX
98
#define XAWT_PATH "/libawt_xawt.dylib"
99
#define DEFAULT_PATH XAWT_PATH
100
#define HEADLESS_PATH "/libawt_headless.dylib"
101
#else
102
#ifdef MACOSX
103
#define LWAWT_PATH "/libawt_lwawt.dylib"
104
#define DEFAULT_PATH LWAWT_PATH
105
#else
106
#define XAWT_PATH "/libawt_xawt.so"
107
#define DEFAULT_PATH XAWT_PATH
108
#define HEADLESS_PATH "/libawt_headless.so"
109
#endif
110
#endif
111
112
jint
113
AWT_OnLoad(JavaVM *vm, void *reserved)
114
{
115
Dl_info dlinfo;
116
char buf[MAXPATHLEN];
117
int32_t len;
118
char *p, *tk;
119
JNI_OnLoad_type *JNI_OnLoad_ptr;
120
struct utsname name;
121
JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);
122
void *v;
123
jstring fmanager = NULL;
124
jstring fmProp = NULL;
125
126
if (awtHandle != NULL) {
127
/* Avoid several loading attempts */
128
return JNI_VERSION_1_2;
129
}
130
131
jvm = vm;
132
#ifndef STATIC_BUILD
133
/* Get address of this library and the directory containing it. */
134
dladdr((void *)AWT_OnLoad, &dlinfo);
135
realpath((char *)dlinfo.dli_fname, buf);
136
len = strlen(buf);
137
p = strrchr(buf, '/');
138
#endif
139
/*
140
* The code below is responsible for:
141
* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless
142
* 2. Set the "sun.font.fontmanager" system property.
143
*/
144
145
fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");
146
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");
147
148
#ifdef MACOSX
149
fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");
150
tk = LWAWT_PATH;
151
#else
152
fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");
153
tk = XAWT_PATH;
154
#endif
155
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");
156
157
if (fmanager && fmProp) {
158
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",
159
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
160
fmProp, fmanager);
161
CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");
162
}
163
164
#ifndef MACOSX
165
if (AWTIsHeadless()) {
166
tk = HEADLESS_PATH;
167
}
168
#endif
169
170
#ifndef STATIC_BUILD
171
/* Calculate library name to load */
172
strncpy(p, tk, MAXPATHLEN-len-1);
173
#endif
174
175
if (fmProp) {
176
(*env)->DeleteLocalRef(env, fmProp);
177
}
178
if (fmanager) {
179
(*env)->DeleteLocalRef(env, fmanager);
180
}
181
182
183
#ifndef STATIC_BUILD
184
jstring jbuf = JNU_NewStringPlatform(env, buf);
185
CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");
186
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",
187
"(Ljava/lang/String;)V",
188
jbuf);
189
190
awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
191
#endif
192
return JNI_VERSION_1_2;
193
}
194
195
JNIEXPORT jint JNICALL
196
DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
197
{
198
return AWT_OnLoad(vm, reserved);
199
}
200
201