Debian Codegraph @

src/android/jni_helpers.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Android jni helpers
12 *
13 * By Thomas Fjellstrom.
14 *
15 */
16
17 #include "allegro5/allegro.h"
18 #include "allegro5/internal/aintern_android.h"
19 #include <jni.h>
20 #include <stdarg.h>
21
22 ALLEGRO_DEBUG_CHANNEL("jni")
23
24 #define VERBOSE_DEBUG(a, ...) (void)0
25
26 /* jni helpers */
27
28 void __jni_checkException(JNIEnv *env, const char *file, const char *func, int line)
29 {
30 jthrowable exc;
31
32 exc = (*env)->ExceptionOccurred(env);
33 if (exc) {
34 ALLEGRO_DEBUG("GOT AN EXCEPTION @ %s:%i %s", file, line, func);
35 /* We don't do much with the exception, except that
36 we print a debug message for it, clear it, and
37 throw a new exception. */
38 (*env)->ExceptionDescribe(env);
39 (*env)->ExceptionClear(env);
40 (*env)->FatalError(env, "EXCEPTION");
41 //(*env)->ThrowNew(env, system_data.illegal_argument_exception_class, "thrown from C code");
42 }
43 }
44
45 jobject _jni_callObjectMethod(JNIEnv *env, jobject object,
46 const char *name, const char *sig)
47 {
48 VERBOSE_DEBUG("%s (%s)", name, sig);
49
50 jclass class_id = _jni_call(env, jclass, GetObjectClass, object);
51 jmethodID method_id = _jni_call(env, jmethodID, GetMethodID, class_id, name, sig);
52 jobject ret = _jni_call(env, jobject, CallObjectMethod, object, method_id);
53
54 _jni_callv(env, DeleteLocalRef, class_id);
55
56 return ret;
57 }
58
59 jobject _jni_callObjectMethodV(JNIEnv *env, jobject object,
60 const char *name, const char *sig, ...)
61 {
62 va_list ap;
63
64 VERBOSE_DEBUG("%s (%s)", name, sig);
65
66 jclass class_id = _jni_call(env, jclass, GetObjectClass, object);
67 jmethodID method_id = _jni_call(env, jmethodID, GetMethodID, class_id, name, sig);
68
69 va_start(ap, sig);
70 jobject ret = _jni_call(env, jobject, CallObjectMethodV, object, method_id, ap);
71 va_end(ap);
72
73 _jni_callv(env, DeleteLocalRef, class_id);
74
75 VERBOSE_DEBUG("callObjectMethodV end");
76 return ret;
77 }
78
79 ALLEGRO_USTR *_jni_getString(JNIEnv *env, jstring str_obj)
80 {
81 VERBOSE_DEBUG("GetStringUTFLength");
82 jsize len = _jni_call(env, jsize, GetStringUTFLength, str_obj);
83
84 const char *str = _jni_call(env, const char *, GetStringUTFChars, str_obj, NULL);
85
86 VERBOSE_DEBUG("al_ustr_new_from_buffer");
87 ALLEGRO_USTR *ustr = al_ustr_new_from_buffer(str, len);
88
89 _jni_callv(env, ReleaseStringUTFChars, str_obj, str);
90
91 return ustr;
92 }
93
94 ALLEGRO_USTR *_jni_callStringMethod(JNIEnv *env, jobject obj,
95 const char *name, const char *sig)
96 {
97 jstring str_obj = (jstring)_jni_callObjectMethod(env, obj, name, sig);
98 ALLEGRO_USTR *ustr = _jni_getString(env, str_obj);
99
100 _jni_callv(env, DeleteLocalRef, str_obj);
101
102 return ustr;
103 }
104
105 jobject _jni_callStaticObjectMethodV(JNIEnv *env, jclass cls,
106 const char *name, const char *sig, ...)
107 {
108 jmethodID mid;
109 jobject ret;
110 va_list ap;
111
112 mid = _jni_call(env, jmethodID, GetStaticMethodID, cls, name, sig);
113
114 va_start(ap, sig);
115 ret = _jni_call(env, jobject, CallStaticObjectMethodV, cls, mid, ap);
116 va_end(ap);
117
118 return ret;
119 }
120
121 jint _jni_callStaticIntMethodV(JNIEnv *env, jclass cls,
122 const char *name, const char *sig, ...)
123 {
124 jmethodID mid;
125 jint ret;
126 va_list ap;
127
128 mid = _jni_call(env, jmethodID, GetStaticMethodID, cls, name, sig);
129
130 va_start(ap, sig);
131 ret = _jni_call(env, jint, CallStaticIntMethodV, cls, mid, ap);
132 va_end(ap);
133
134 return ret;
135 }
136
137 /* vim: set sts=3 sw=3 et: */