-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorg_libdivecomputer_Context.c
More file actions
64 lines (51 loc) · 1.83 KB
/
org_libdivecomputer_Context.c
File metadata and controls
64 lines (51 loc) · 1.83 KB
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
#include "org_libdivecomputer_Context.h"
#include "exception.h"
#include <libdivecomputer/context.h>
typedef struct jni_context_t {
JNIEnv *env;
jobject obj;
jclass cls;
jmethodID method;
} jni_context_t;
static void
log_cb (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *message, void *userdata)
{
jni_context_t *jni = userdata;
(*jni->env)->CallVoidMethod(jni->env, jni->obj, jni->method,
loglevel,
(*jni->env)->NewStringUTF(jni->env, file),
line,
(*jni->env)->NewStringUTF(jni->env, function),
(*jni->env)->NewStringUTF(jni->env, message));
}
JNIEXPORT jlong JNICALL Java_org_libdivecomputer_Context_New
(JNIEnv *env, jobject obj)
{
dc_context_t *context = NULL;
DC_EXCEPTION_THROW(dc_context_new (&context));
return (jlong) context;
}
JNIEXPORT void JNICALL Java_org_libdivecomputer_Context_Free
(JNIEnv *env, jobject obj, jlong handle)
{
DC_EXCEPTION_THROW(dc_context_free ((dc_context_t *) handle));
}
JNIEXPORT void JNICALL Java_org_libdivecomputer_Context_SetLogLevel
(JNIEnv *env, jobject obj, jlong handle, jint loglevel)
{
DC_EXCEPTION_THROW(dc_context_set_loglevel ((dc_context_t *) handle, loglevel));
}
JNIEXPORT void JNICALL Java_org_libdivecomputer_Context_SetLogFunc
(JNIEnv *env, jobject obj, jlong handle, jobject logfunc)
{
static jni_context_t jni = {0}; // FIXME: Not thread-safe.
if (logfunc) {
jni.env = env;
jni.obj = (*env)->NewGlobalRef(env, logfunc); // FIXME: Memory leak.
jni.cls = (*env)->GetObjectClass(env, logfunc);
jni.method = (*env)->GetMethodID(env, jni.cls, "Log", "(ILjava/lang/String;ILjava/lang/String;Ljava/lang/String;)V");
DC_EXCEPTION_THROW(dc_context_set_logfunc ((dc_context_t *) handle, log_cb, &jni));
} else {
DC_EXCEPTION_THROW(dc_context_set_logfunc ((dc_context_t *) handle, NULL, NULL));
}
}