1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include "JoglCommon.h"
#include <assert.h>
#include <KHR/khrplatform.h>
static const char * const ClazzNameRuntimeException = "java/lang/RuntimeException";
static jclass runtimeExceptionClz=NULL;
static JavaVM *_jvmHandle = NULL;
static int _jvmVersion = 0;
void JoglCommon_init(JNIEnv *env) {
if(NULL==runtimeExceptionClz) {
jclass c = (*env)->FindClass(env, ClazzNameRuntimeException);
if(NULL==c) {
JoglCommon_FatalError(env, "JOGL: can't find %s", ClazzNameRuntimeException);
}
runtimeExceptionClz = (jclass)(*env)->NewGlobalRef(env, c);
(*env)->DeleteLocalRef(env, c);
if(NULL==runtimeExceptionClz) {
JoglCommon_FatalError(env, "JOGL: can't use %s", ClazzNameRuntimeException);
}
}
if(0 != (*env)->GetJavaVM(env, &_jvmHandle)) {
JoglCommon_FatalError(env, "JOGL: can't fetch JavaVM handle");
} else {
_jvmVersion = (*env)->GetVersion(env);
}
}
void JoglCommon_FatalError(JNIEnv *env, const char* msg, ...)
{
char buffer[512];
va_list ap;
int shallBeDetached = 0;
if(NULL == env) {
env = JoglCommon_GetJNIEnv (&shallBeDetached);
}
va_start(ap, msg);
vsnprintf(buffer, sizeof(buffer), msg, ap);
va_end(ap);
fprintf(stderr, "%s\n", buffer);
if(NULL != env) {
(*env)->FatalError(env, buffer);
JoglCommon_ReleaseJNIEnv (shallBeDetached);
}
}
void JoglCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...)
{
char buffer[512];
va_list ap;
int shallBeDetached = 0;
if(NULL == env) {
env = JoglCommon_GetJNIEnv (&shallBeDetached);
}
va_start(ap, msg);
vsnprintf(buffer, sizeof(buffer), msg, ap);
va_end(ap);
if(NULL != env) {
(*env)->ThrowNew(env, runtimeExceptionClz, buffer);
JoglCommon_ReleaseJNIEnv (shallBeDetached);
}
}
JavaVM *JoglCommon_GetJVMHandle() {
return _jvmHandle;
}
int JoglCommon_GetJVMVersion() {
return _jvmVersion;
}
jchar* JoglCommon_GetNullTerminatedStringChars(JNIEnv* env, jstring str)
{
jchar* strChars = NULL;
strChars = calloc((*env)->GetStringLength(env, str) + 1, sizeof(jchar));
if (strChars != NULL) {
(*env)->GetStringRegion(env, str, 0, (*env)->GetStringLength(env, str), strChars);
}
return strChars;
}
JNIEnv* JoglCommon_GetJNIEnv (int * shallBeDetached)
{
JNIEnv* curEnv = NULL;
JNIEnv* newEnv = NULL;
int envRes;
if(NULL == _jvmHandle) {
fprintf(stderr, "JOGL: No JavaVM handle registered, call JoglCommon_init(..) 1st");
return NULL;
}
// retrieve this thread's JNIEnv curEnv - or detect it's detached
envRes = (*_jvmHandle)->GetEnv(_jvmHandle, (void **) &curEnv, _jvmVersion) ;
if( JNI_EDETACHED == envRes ) {
// detached thread - attach to JVM
if( JNI_OK != ( envRes = (*_jvmHandle)->AttachCurrentThread(_jvmHandle, (void**) &newEnv, NULL) ) ) {
fprintf(stderr, "JNIEnv: can't attach thread: %d\n", envRes);
return NULL;
}
curEnv = newEnv;
} else if( JNI_OK != envRes ) {
// oops ..
fprintf(stderr, "can't GetEnv: %d\n", envRes);
return NULL;
}
if (curEnv==NULL) {
fprintf(stderr, "env is NULL\n");
return NULL;
}
*shallBeDetached = NULL != newEnv;
return curEnv;
}
void JoglCommon_ReleaseJNIEnv (int shallBeDetached) {
if(NULL == _jvmHandle) {
fprintf(stderr, "JOGL: No JavaVM handle registered, call JoglCommon_init(..) 1st");
}
if(shallBeDetached) {
(*_jvmHandle)->DetachCurrentThread(_jvmHandle);
}
}
|