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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include "NativewindowCommon.h"
#include <string.h>
#include <sys/time.h>
// #define STDERR_TO_FILE 1
static const char * const ClazzNameRuntimeException = "java/lang/RuntimeException";
static jclass runtimeExceptionClz=NULL;
static JavaVM *_jvmHandle = NULL;
static int _jvmVersion = 0;
int NativewindowCommon_init(JNIEnv *env) {
if(NULL==_jvmHandle) {
if(0 != (*env)->GetJavaVM(env, &_jvmHandle)) {
NativewindowCommon_FatalError(env, "Nativewindow: Can't fetch JavaVM handle");
} else {
_jvmVersion = (*env)->GetVersion(env);
}
jclass c = (*env)->FindClass(env, ClazzNameRuntimeException);
if(NULL==c) {
NativewindowCommon_FatalError(env, "Nativewindow: Can't find %s", ClazzNameRuntimeException);
}
runtimeExceptionClz = (jclass)(*env)->NewGlobalRef(env, c);
(*env)->DeleteLocalRef(env, c);
if(NULL==runtimeExceptionClz) {
NativewindowCommon_FatalError(env, "Nativewindow: Can't use %s", ClazzNameRuntimeException);
}
#ifdef STDERR_TO_FILE
FILE * old_stderr = stderr;
FILE * new_stderr = freopen("jogamp_stderr.log", "w", stderr);
fprintf(stderr, "STDERR_TO_FILE: %p -> %p\n", old_stderr, new_stderr);
#endif
return 1;
}
return 0;
}
void NativewindowCommon_FatalError(JNIEnv *env, const char* msg, ...)
{
char buffer[512];
va_list ap;
if( NULL != msg ) {
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);
}
}
}
void NativewindowCommon_throwNewRuntimeException(JNIEnv *env, const char* msg, ...)
{
char buffer[512];
va_list ap;
if(NULL==_jvmHandle) {
NativewindowCommon_FatalError(env, "Nativewindow: NULL JVM handle, call NativewindowCommon_init 1st\n");
return;
}
if( NULL != msg ) {
va_start(ap, msg);
vsnprintf(buffer, sizeof(buffer), msg, ap);
va_end(ap);
if(NULL != env) {
(*env)->ThrowNew(env, runtimeExceptionClz, buffer);
}
}
}
const char * NativewindowCommon_GetStaticStringMethod(JNIEnv *jniEnv, jclass clazz, jmethodID jGetStrID, char *dest, int destSize, const char *altText) {
if(NULL != jniEnv && NULL != clazz && NULL != jGetStrID) {
jstring jstr = (jstring) (*jniEnv)->CallStaticObjectMethod(jniEnv, clazz, jGetStrID);
if(NULL != jstr) {
const char * str = (*jniEnv)->GetStringUTFChars(jniEnv, jstr, NULL);
if( NULL != str) {
strncpy(dest, str, destSize-1);
dest[destSize-1] = 0; // EOS
(*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, str);
return dest;
}
}
}
strncpy(dest, altText, destSize-1);
dest[destSize-1] = 0; // EOS
return dest;
}
jchar* NativewindowCommon_GetNullTerminatedStringChars(JNIEnv* env, jstring str)
{
jchar* strChars = NULL;
if( NULL != env && 0 != str ) {
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* NativewindowCommon_GetJNIEnv (int asDaemon, int * shallBeDetached) {
JNIEnv* curEnv = NULL;
JNIEnv* newEnv = NULL;
int envRes;
if(NULL==_jvmHandle) {
fprintf(stderr, "Nativewindow GetJNIEnv: NULL JVM handle, call NativewindowCommon_init 1st\n");
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( asDaemon ) {
envRes = (*_jvmHandle)->AttachCurrentThreadAsDaemon(_jvmHandle, (void**) &newEnv, NULL);
} else {
envRes = (*_jvmHandle)->AttachCurrentThread(_jvmHandle, (void**) &newEnv, NULL);
}
if( JNI_OK != envRes ) {
fprintf(stderr, "Nativewindow GetJNIEnv: Can't attach thread: %d\n", envRes);
return NULL;
}
curEnv = newEnv;
} else if( JNI_OK != envRes ) {
// oops ..
fprintf(stderr, "Nativewindow GetJNIEnv: Can't GetEnv: %d\n", envRes);
return NULL;
}
if (curEnv==NULL) {
fprintf(stderr, "Nativewindow GetJNIEnv: env is NULL\n");
return NULL;
}
*shallBeDetached = NULL != newEnv;
return curEnv;
}
void NativewindowCommon_ReleaseJNIEnv (int shallBeDetached) {
if(NULL == _jvmHandle) {
fprintf(stderr, "Nativewindow ReleaseJNIEnv: No JavaVM handle registered, call NativewindowCommon_init(..) 1st");
} else if(shallBeDetached) {
(*_jvmHandle)->DetachCurrentThread(_jvmHandle);
}
}
int64_t NativewindowCommon_CurrentTimeMillis() {
struct timeval tv;
gettimeofday(&tv,NULL);
return (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
|