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