slouken@4964
|
1 |
/*
|
slouken@5535
|
2 |
Simple DirectMedia Layer
|
slouken@6138
|
3 |
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
slouken@4964
|
4 |
|
slouken@5535
|
5 |
This software is provided 'as-is', without any express or implied
|
slouken@5535
|
6 |
warranty. In no event will the authors be held liable for any damages
|
slouken@5535
|
7 |
arising from the use of this software.
|
slouken@4964
|
8 |
|
slouken@5535
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@5535
|
10 |
including commercial applications, and to alter it and redistribute it
|
slouken@5535
|
11 |
freely, subject to the following restrictions:
|
slouken@4964
|
12 |
|
slouken@5535
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
slouken@5535
|
14 |
claim that you wrote the original software. If you use this software
|
slouken@5535
|
15 |
in a product, an acknowledgment in the product documentation would be
|
slouken@5535
|
16 |
appreciated but is not required.
|
slouken@5535
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
slouken@5535
|
18 |
misrepresented as being the original software.
|
slouken@5535
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
slouken@4964
|
20 |
*/
|
slouken@4964
|
21 |
#include "SDL_config.h"
|
slouken@5222
|
22 |
#include "SDL_stdinc.h"
|
slouken@6284
|
23 |
#include "SDL_assert.h"
|
slouken@4964
|
24 |
|
slouken@6044
|
25 |
#ifdef __ANDROID__
|
slouken@6044
|
26 |
|
slouken@4989
|
27 |
#include "SDL_android.h"
|
slouken@4989
|
28 |
|
slouken@4980
|
29 |
extern "C" {
|
slouken@5092
|
30 |
#include "../../events/SDL_events_c.h"
|
slouken@5092
|
31 |
#include "../../video/android/SDL_androidkeyboard.h"
|
slouken@5092
|
32 |
#include "../../video/android/SDL_androidtouch.h"
|
slouken@5092
|
33 |
#include "../../video/android/SDL_androidvideo.h"
|
slouken@4995
|
34 |
|
icculus@5996
|
35 |
#include <android/log.h>
|
icculus@5996
|
36 |
#define LOG_TAG "SDL_android"
|
icculus@5996
|
37 |
//#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
|
icculus@5996
|
38 |
//#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
|
icculus@5996
|
39 |
#define LOGI(...) do {} while (false)
|
icculus@5996
|
40 |
#define LOGE(...) do {} while (false)
|
icculus@5996
|
41 |
|
icculus@5996
|
42 |
|
slouken@6191
|
43 |
/* Implemented in audio/android/SDL_androidaudio.c */
|
slouken@4995
|
44 |
extern void Android_RunAudioThread();
|
slouken@4995
|
45 |
} // C
|
slouken@4980
|
46 |
|
slouken@4964
|
47 |
/*******************************************************************************
|
slouken@4964
|
48 |
This file links the Java side of Android with libsdl
|
slouken@4964
|
49 |
*******************************************************************************/
|
slouken@4964
|
50 |
#include <jni.h>
|
slouken@4964
|
51 |
#include <android/log.h>
|
slouken@4964
|
52 |
|
slouken@4964
|
53 |
|
slouken@4964
|
54 |
/*******************************************************************************
|
slouken@4964
|
55 |
Globals
|
slouken@4964
|
56 |
*******************************************************************************/
|
slouken@4995
|
57 |
static JNIEnv* mEnv = NULL;
|
slouken@4995
|
58 |
static JNIEnv* mAudioEnv = NULL;
|
icculus@5996
|
59 |
static JavaVM* mJavaVM;
|
slouken@4964
|
60 |
|
slouken@4995
|
61 |
// Main activity
|
slouken@4998
|
62 |
static jclass mActivityClass;
|
slouken@4964
|
63 |
|
slouken@4995
|
64 |
// method signatures
|
slouken@4995
|
65 |
static jmethodID midCreateGLContext;
|
slouken@4995
|
66 |
static jmethodID midFlipBuffers;
|
slouken@4995
|
67 |
static jmethodID midAudioInit;
|
slouken@4995
|
68 |
static jmethodID midAudioWriteShortBuffer;
|
slouken@4995
|
69 |
static jmethodID midAudioWriteByteBuffer;
|
slouken@4996
|
70 |
static jmethodID midAudioQuit;
|
slouken@4964
|
71 |
|
slouken@4995
|
72 |
// Accelerometer data storage
|
slouken@5000
|
73 |
static float fLastAccelerometer[3];
|
slouken@6212
|
74 |
static bool bHasNewData;
|
slouken@4964
|
75 |
|
slouken@4964
|
76 |
/*******************************************************************************
|
slouken@4964
|
77 |
Functions called by JNI
|
slouken@4964
|
78 |
*******************************************************************************/
|
slouken@4964
|
79 |
|
slouken@4964
|
80 |
// Library init
|
slouken@4964
|
81 |
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
|
slouken@4964
|
82 |
{
|
icculus@5996
|
83 |
JNIEnv *env;
|
icculus@5996
|
84 |
mJavaVM = vm;
|
icculus@5996
|
85 |
LOGI("JNI_OnLoad called");
|
icculus@5996
|
86 |
if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
|
icculus@5996
|
87 |
LOGE("Failed to get the environment using GetEnv()");
|
icculus@5996
|
88 |
return -1;
|
icculus@5996
|
89 |
}
|
icculus@5996
|
90 |
|
slouken@4964
|
91 |
return JNI_VERSION_1_4;
|
slouken@4964
|
92 |
}
|
slouken@4964
|
93 |
|
slouken@4964
|
94 |
// Called before SDL_main() to initialize JNI bindings
|
slouken@4998
|
95 |
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
|
slouken@4964
|
96 |
{
|
slouken@4964
|
97 |
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
|
slouken@4964
|
98 |
|
slouken@4998
|
99 |
mEnv = env;
|
tim@6189
|
100 |
mActivityClass = (jclass)env->NewGlobalRef(cls);
|
slouken@4998
|
101 |
|
slouken@4998
|
102 |
midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@5222
|
103 |
"createGLContext","(II)Z");
|
slouken@4998
|
104 |
midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@4998
|
105 |
"flipBuffers","()V");
|
slouken@4998
|
106 |
midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@4998
|
107 |
"audioInit", "(IZZI)Ljava/lang/Object;");
|
slouken@4998
|
108 |
midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@4998
|
109 |
"audioWriteShortBuffer", "([S)V");
|
slouken@4998
|
110 |
midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@4998
|
111 |
"audioWriteByteBuffer", "([B)V");
|
slouken@4998
|
112 |
midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
|
slouken@4998
|
113 |
"audioQuit", "()V");
|
slouken@4964
|
114 |
|
slouken@6212
|
115 |
bHasNewData = false;
|
slouken@6212
|
116 |
|
slouken@4995
|
117 |
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
|
slouken@4996
|
118 |
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
|
slouken@4996
|
119 |
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
|
slouken@4964
|
120 |
}
|
icculus@5996
|
121 |
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
|
slouken@4964
|
122 |
}
|
slouken@4964
|
123 |
|
slouken@4995
|
124 |
// Resize
|
slouken@4995
|
125 |
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
|
slouken@4998
|
126 |
JNIEnv* env, jclass jcls,
|
slouken@4995
|
127 |
jint width, jint height, jint format)
|
slouken@4964
|
128 |
{
|
slouken@4995
|
129 |
Android_SetScreenResolution(width, height, format);
|
slouken@4995
|
130 |
}
|
slouken@4995
|
131 |
|
slouken@4995
|
132 |
// Keydown
|
slouken@4995
|
133 |
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
|
slouken@4998
|
134 |
JNIEnv* env, jclass jcls, jint keycode)
|
slouken@4995
|
135 |
{
|
slouken@4980
|
136 |
Android_OnKeyDown(keycode);
|
slouken@4964
|
137 |
}
|
slouken@4964
|
138 |
|
slouken@4964
|
139 |
// Keyup
|
slouken@4995
|
140 |
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
|
slouken@4998
|
141 |
JNIEnv* env, jclass jcls, jint keycode)
|
slouken@4964
|
142 |
{
|
slouken@4980
|
143 |
Android_OnKeyUp(keycode);
|
slouken@4964
|
144 |
}
|
slouken@4964
|
145 |
|
slouken@4964
|
146 |
// Touch
|
slouken@4995
|
147 |
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
|
slouken@4998
|
148 |
JNIEnv* env, jclass jcls,
|
icculus@5982
|
149 |
jint touch_device_id_in, jint pointer_finger_id_in,
|
slouken@4995
|
150 |
jint action, jfloat x, jfloat y, jfloat p)
|
slouken@4964
|
151 |
{
|
icculus@5982
|
152 |
Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
|
slouken@4964
|
153 |
}
|
slouken@4964
|
154 |
|
slouken@4995
|
155 |
// Accelerometer
|
slouken@4964
|
156 |
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
|
slouken@4998
|
157 |
JNIEnv* env, jclass jcls,
|
slouken@4995
|
158 |
jfloat x, jfloat y, jfloat z)
|
slouken@4964
|
159 |
{
|
slouken@4964
|
160 |
fLastAccelerometer[0] = x;
|
slouken@4964
|
161 |
fLastAccelerometer[1] = y;
|
slouken@6212
|
162 |
fLastAccelerometer[2] = z;
|
slouken@6212
|
163 |
bHasNewData = true;
|
slouken@4964
|
164 |
}
|
slouken@4964
|
165 |
|
slouken@4995
|
166 |
// Quit
|
slouken@4995
|
167 |
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
|
slouken@4998
|
168 |
JNIEnv* env, jclass cls)
|
slouken@4995
|
169 |
{
|
slouken@4995
|
170 |
// Inject a SDL_QUIT event
|
slouken@4995
|
171 |
SDL_SendQuit();
|
slouken@4995
|
172 |
}
|
slouken@4995
|
173 |
|
slouken@6186
|
174 |
// Pause
|
slouken@6186
|
175 |
extern "C" void Java_org_libsdl_app_SDLActivity_nativePause(
|
slouken@6186
|
176 |
JNIEnv* env, jclass cls)
|
slouken@6186
|
177 |
{
|
slouken@6186
|
178 |
if (Android_Window) {
|
slouken@6186
|
179 |
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
|
slouken@6191
|
180 |
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
|
slouken@6186
|
181 |
}
|
slouken@6186
|
182 |
}
|
slouken@6186
|
183 |
|
slouken@6186
|
184 |
// Resume
|
slouken@6186
|
185 |
extern "C" void Java_org_libsdl_app_SDLActivity_nativeResume(
|
slouken@6186
|
186 |
JNIEnv* env, jclass cls)
|
slouken@6186
|
187 |
{
|
slouken@6186
|
188 |
if (Android_Window) {
|
slouken@6186
|
189 |
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
|
slouken@6191
|
190 |
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
|
slouken@6186
|
191 |
}
|
slouken@6186
|
192 |
}
|
slouken@6186
|
193 |
|
slouken@4995
|
194 |
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
|
slouken@4998
|
195 |
JNIEnv* env, jclass cls)
|
slouken@4995
|
196 |
{
|
slouken@4998
|
197 |
/* This is the audio thread, with a different environment */
|
slouken@4998
|
198 |
mAudioEnv = env;
|
slouken@4998
|
199 |
|
slouken@4996
|
200 |
Android_RunAudioThread();
|
slouken@4995
|
201 |
}
|
slouken@4964
|
202 |
|
slouken@4964
|
203 |
|
slouken@4964
|
204 |
/*******************************************************************************
|
slouken@4964
|
205 |
Functions called by SDL into Java
|
slouken@4964
|
206 |
*******************************************************************************/
|
slouken@6284
|
207 |
|
slouken@6284
|
208 |
class LocalReferenceHolder
|
slouken@6284
|
209 |
{
|
slouken@6284
|
210 |
private:
|
slouken@6284
|
211 |
static int s_active;
|
slouken@6284
|
212 |
|
slouken@6284
|
213 |
public:
|
slouken@6284
|
214 |
static bool IsActive() {
|
slouken@6284
|
215 |
return s_active > 0;
|
slouken@6284
|
216 |
}
|
slouken@6284
|
217 |
|
slouken@6284
|
218 |
public:
|
slouken@6284
|
219 |
LocalReferenceHolder() : m_env(NULL) { }
|
slouken@6284
|
220 |
~LocalReferenceHolder() {
|
slouken@6284
|
221 |
if (m_env) {
|
slouken@6284
|
222 |
m_env->PopLocalFrame(NULL);
|
slouken@6284
|
223 |
--s_active;
|
slouken@6284
|
224 |
}
|
slouken@6284
|
225 |
}
|
slouken@6284
|
226 |
|
slouken@6284
|
227 |
bool init(JNIEnv *env, jint capacity = 16) {
|
slouken@6284
|
228 |
if (env->PushLocalFrame(capacity) < 0) {
|
slouken@6284
|
229 |
SDL_SetError("Failed to allocate enough JVM local references");
|
slouken@6284
|
230 |
return false;
|
slouken@6284
|
231 |
}
|
slouken@6284
|
232 |
++s_active;
|
slouken@6284
|
233 |
m_env = env;
|
slouken@6284
|
234 |
return true;
|
slouken@6284
|
235 |
}
|
slouken@6284
|
236 |
|
slouken@6284
|
237 |
protected:
|
slouken@6284
|
238 |
JNIEnv *m_env;
|
slouken@6284
|
239 |
};
|
slouken@6284
|
240 |
int LocalReferenceHolder::s_active;
|
slouken@6284
|
241 |
|
slouken@5222
|
242 |
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
|
slouken@4964
|
243 |
{
|
slouken@5222
|
244 |
if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
|
slouken@5222
|
245 |
return SDL_TRUE;
|
slouken@5222
|
246 |
} else {
|
slouken@5222
|
247 |
return SDL_FALSE;
|
slouken@5222
|
248 |
}
|
slouken@4964
|
249 |
}
|
slouken@4964
|
250 |
|
slouken@4989
|
251 |
extern "C" void Android_JNI_SwapWindow()
|
slouken@4964
|
252 |
{
|
slouken@4998
|
253 |
mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
|
slouken@4998
|
254 |
}
|
slouken@4998
|
255 |
|
slouken@4998
|
256 |
extern "C" void Android_JNI_SetActivityTitle(const char *title)
|
slouken@4998
|
257 |
{
|
slouken@4998
|
258 |
jmethodID mid;
|
slouken@4998
|
259 |
|
slouken@4998
|
260 |
mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
|
slouken@4998
|
261 |
if (mid) {
|
gabomdq@6307
|
262 |
jstring jtitle = reinterpret_cast<jstring>(mEnv->NewStringUTF(title));
|
gabomdq@6307
|
263 |
mEnv->CallStaticVoidMethod(mActivityClass, mid, jtitle);
|
gabomdq@6307
|
264 |
mEnv->DeleteLocalRef(jtitle);
|
slouken@4998
|
265 |
}
|
slouken@4964
|
266 |
}
|
slouken@4964
|
267 |
|
slouken@6212
|
268 |
extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
|
slouken@5000
|
269 |
{
|
slouken@5000
|
270 |
int i;
|
slouken@6212
|
271 |
SDL_bool retval = SDL_FALSE;
|
slouken@6212
|
272 |
|
slouken@6212
|
273 |
if (bHasNewData) {
|
slouken@6212
|
274 |
for (i = 0; i < 3; ++i) {
|
slouken@6212
|
275 |
values[i] = fLastAccelerometer[i];
|
slouken@6212
|
276 |
}
|
slouken@6212
|
277 |
bHasNewData = false;
|
slouken@6212
|
278 |
retval = SDL_TRUE;
|
slouken@5000
|
279 |
}
|
slouken@6212
|
280 |
|
slouken@6212
|
281 |
return retval;
|
slouken@5000
|
282 |
}
|
slouken@5000
|
283 |
|
slouken@4995
|
284 |
//
|
slouken@4995
|
285 |
// Audio support
|
slouken@4995
|
286 |
//
|
slouken@4996
|
287 |
static jboolean audioBuffer16Bit = JNI_FALSE;
|
slouken@4996
|
288 |
static jboolean audioBufferStereo = JNI_FALSE;
|
slouken@4996
|
289 |
static jobject audioBuffer = NULL;
|
slouken@4996
|
290 |
static void* audioBufferPinned = NULL;
|
slouken@4995
|
291 |
|
slouken@4995
|
292 |
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
|
slouken@4995
|
293 |
{
|
slouken@4996
|
294 |
int audioBufferFrames;
|
slouken@4996
|
295 |
|
icculus@5996
|
296 |
int status;
|
icculus@5996
|
297 |
JNIEnv *env;
|
icculus@5996
|
298 |
static bool isAttached = false;
|
icculus@5996
|
299 |
status = mJavaVM->GetEnv((void **) &env, JNI_VERSION_1_4);
|
icculus@5996
|
300 |
if(status < 0) {
|
icculus@5996
|
301 |
LOGE("callback_handler: failed to get JNI environment, assuming native thread");
|
icculus@5996
|
302 |
status = mJavaVM->AttachCurrentThread(&env, NULL);
|
icculus@5996
|
303 |
if(status < 0) {
|
icculus@5996
|
304 |
LOGE("callback_handler: failed to attach current thread");
|
icculus@5996
|
305 |
return 0;
|
icculus@5996
|
306 |
}
|
icculus@5996
|
307 |
isAttached = true;
|
icculus@5996
|
308 |
}
|
icculus@5996
|
309 |
|
icculus@5996
|
310 |
|
slouken@4996
|
311 |
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
|
slouken@4996
|
312 |
audioBuffer16Bit = is16Bit;
|
slouken@4996
|
313 |
audioBufferStereo = channelCount > 1;
|
slouken@4995
|
314 |
|
icculus@5996
|
315 |
audioBuffer = env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
|
slouken@4996
|
316 |
|
slouken@4996
|
317 |
if (audioBuffer == NULL) {
|
slouken@4996
|
318 |
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
|
slouken@4996
|
319 |
return 0;
|
slouken@4996
|
320 |
}
|
icculus@5996
|
321 |
audioBuffer = env->NewGlobalRef(audioBuffer);
|
slouken@4995
|
322 |
|
slouken@4996
|
323 |
jboolean isCopy = JNI_FALSE;
|
slouken@4996
|
324 |
if (audioBuffer16Bit) {
|
icculus@5996
|
325 |
audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
|
icculus@5996
|
326 |
audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
|
slouken@4996
|
327 |
} else {
|
icculus@5996
|
328 |
audioBufferPinned = env->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
|
icculus@5996
|
329 |
audioBufferFrames = env->GetArrayLength((jbyteArray)audioBuffer);
|
slouken@4996
|
330 |
}
|
slouken@4996
|
331 |
if (audioBufferStereo) {
|
slouken@4996
|
332 |
audioBufferFrames /= 2;
|
slouken@4996
|
333 |
}
|
icculus@5996
|
334 |
|
icculus@5996
|
335 |
if (isAttached) {
|
icculus@5996
|
336 |
mJavaVM->DetachCurrentThread();
|
icculus@5996
|
337 |
}
|
slouken@4995
|
338 |
|
slouken@4996
|
339 |
return audioBufferFrames;
|
slouken@4995
|
340 |
}
|
slouken@4995
|
341 |
|
slouken@4996
|
342 |
extern "C" void * Android_JNI_GetAudioBuffer()
|
slouken@4964
|
343 |
{
|
slouken@4996
|
344 |
return audioBufferPinned;
|
slouken@4995
|
345 |
}
|
slouken@4964
|
346 |
|
slouken@4996
|
347 |
extern "C" void Android_JNI_WriteAudioBuffer()
|
slouken@4995
|
348 |
{
|
slouken@4996
|
349 |
if (audioBuffer16Bit) {
|
slouken@4996
|
350 |
mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
|
slouken@4998
|
351 |
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
|
slouken@4996
|
352 |
} else {
|
slouken@4996
|
353 |
mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
|
slouken@4998
|
354 |
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
|
slouken@4996
|
355 |
}
|
slouken@4964
|
356 |
|
slouken@4996
|
357 |
/* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
|
slouken@4995
|
358 |
}
|
slouken@4995
|
359 |
|
slouken@4995
|
360 |
extern "C" void Android_JNI_CloseAudioDevice()
|
slouken@4995
|
361 |
{
|
icculus@5996
|
362 |
int status;
|
icculus@5996
|
363 |
JNIEnv *env;
|
icculus@5996
|
364 |
static bool isAttached = false;
|
icculus@5996
|
365 |
status = mJavaVM->GetEnv((void **) &env, JNI_VERSION_1_4);
|
icculus@5996
|
366 |
if(status < 0) {
|
icculus@5996
|
367 |
LOGE("callback_handler: failed to get JNI environment, assuming native thread");
|
icculus@5996
|
368 |
status = mJavaVM->AttachCurrentThread(&env, NULL);
|
icculus@5996
|
369 |
if(status < 0) {
|
icculus@5996
|
370 |
LOGE("callback_handler: failed to attach current thread");
|
icculus@5996
|
371 |
return;
|
icculus@5996
|
372 |
}
|
icculus@5996
|
373 |
isAttached = true;
|
icculus@5996
|
374 |
}
|
icculus@5996
|
375 |
|
icculus@5996
|
376 |
env->CallStaticVoidMethod(mActivityClass, midAudioQuit);
|
slouken@4964
|
377 |
|
slouken@4997
|
378 |
if (audioBuffer) {
|
icculus@5996
|
379 |
env->DeleteGlobalRef(audioBuffer);
|
slouken@4997
|
380 |
audioBuffer = NULL;
|
slouken@4997
|
381 |
audioBufferPinned = NULL;
|
slouken@4997
|
382 |
}
|
icculus@5996
|
383 |
|
icculus@5996
|
384 |
if (isAttached) {
|
icculus@5996
|
385 |
mJavaVM->DetachCurrentThread();
|
icculus@5996
|
386 |
}
|
slouken@4964
|
387 |
}
|
slouken@4981
|
388 |
|
tim@5650
|
389 |
// Test for an exception and call SDL_SetError with its detail if one occurs
|
tim@5650
|
390 |
static bool Android_JNI_ExceptionOccurred()
|
tim@5650
|
391 |
{
|
slouken@6284
|
392 |
SDL_assert(LocalReferenceHolder::IsActive());
|
slouken@6284
|
393 |
|
tim@5650
|
394 |
jthrowable exception = mEnv->ExceptionOccurred();
|
tim@5650
|
395 |
if (exception != NULL) {
|
tim@5650
|
396 |
jmethodID mid;
|
tim@5650
|
397 |
|
tim@5650
|
398 |
// Until this happens most JNI operations have undefined behaviour
|
tim@5650
|
399 |
mEnv->ExceptionClear();
|
tim@5650
|
400 |
|
tim@5650
|
401 |
jclass exceptionClass = mEnv->GetObjectClass(exception);
|
tim@5650
|
402 |
jclass classClass = mEnv->FindClass("java/lang/Class");
|
tim@5650
|
403 |
|
tim@5650
|
404 |
mid = mEnv->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
|
tim@5650
|
405 |
jstring exceptionName = (jstring)mEnv->CallObjectMethod(exceptionClass, mid);
|
tim@5650
|
406 |
const char* exceptionNameUTF8 = mEnv->GetStringUTFChars(exceptionName, 0);
|
tim@5650
|
407 |
|
tim@5650
|
408 |
mid = mEnv->GetMethodID(exceptionClass, "getMessage", "()Ljava/lang/String;");
|
icculus@5860
|
409 |
jstring exceptionMessage = (jstring)mEnv->CallObjectMethod(exception, mid);
|
tim@5650
|
410 |
|
tim@5650
|
411 |
if (exceptionMessage != NULL) {
|
tim@5650
|
412 |
const char* exceptionMessageUTF8 = mEnv->GetStringUTFChars(
|
tim@5650
|
413 |
exceptionMessage, 0);
|
tim@5650
|
414 |
SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
|
tim@5650
|
415 |
mEnv->ReleaseStringUTFChars(exceptionMessage, exceptionMessageUTF8);
|
tim@5650
|
416 |
} else {
|
tim@5650
|
417 |
SDL_SetError("%s", exceptionNameUTF8);
|
tim@5650
|
418 |
}
|
tim@5650
|
419 |
|
tim@5650
|
420 |
mEnv->ReleaseStringUTFChars(exceptionName, exceptionNameUTF8);
|
tim@5650
|
421 |
|
tim@5650
|
422 |
return true;
|
tim@5650
|
423 |
}
|
tim@5650
|
424 |
|
tim@5650
|
425 |
return false;
|
tim@5650
|
426 |
}
|
tim@5650
|
427 |
|
icculus@5582
|
428 |
static int Android_JNI_FileOpen(SDL_RWops* ctx)
|
icculus@5582
|
429 |
{
|
slouken@6284
|
430 |
LocalReferenceHolder refs;
|
tim@5650
|
431 |
int result = 0;
|
tim@5650
|
432 |
|
tim@5650
|
433 |
jmethodID mid;
|
tim@5650
|
434 |
jobject context;
|
tim@5650
|
435 |
jobject assetManager;
|
tim@5650
|
436 |
jobject inputStream;
|
tim@5650
|
437 |
jclass channels;
|
tim@5650
|
438 |
jobject readableByteChannel;
|
tim@5650
|
439 |
jstring fileNameJString;
|
tim@5650
|
440 |
|
slouken@6284
|
441 |
if (!refs.init(mEnv)) {
|
tim@5650
|
442 |
goto failure;
|
tim@5650
|
443 |
}
|
tim@5650
|
444 |
|
tim@5650
|
445 |
fileNameJString = (jstring)ctx->hidden.androidio.fileName;
|
icculus@5582
|
446 |
|
icculus@5582
|
447 |
// context = SDLActivity.getContext();
|
tim@5650
|
448 |
mid = mEnv->GetStaticMethodID(mActivityClass,
|
icculus@5582
|
449 |
"getContext","()Landroid/content/Context;");
|
tim@5650
|
450 |
context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
|
icculus@5582
|
451 |
|
icculus@5582
|
452 |
// assetManager = context.getAssets();
|
icculus@5582
|
453 |
mid = mEnv->GetMethodID(mEnv->GetObjectClass(context),
|
tim@5650
|
454 |
"getAssets", "()Landroid/content/res/AssetManager;");
|
tim@5650
|
455 |
assetManager = mEnv->CallObjectMethod(context, mid);
|
icculus@5582
|
456 |
|
icculus@5582
|
457 |
// inputStream = assetManager.open(<filename>);
|
icculus@5582
|
458 |
mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
|
icculus@5582
|
459 |
"open", "(Ljava/lang/String;)Ljava/io/InputStream;");
|
tim@5650
|
460 |
inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
|
tim@5650
|
461 |
if (Android_JNI_ExceptionOccurred()) {
|
tim@5650
|
462 |
goto failure;
|
icculus@5582
|
463 |
}
|
icculus@5582
|
464 |
|
tim@5650
|
465 |
ctx->hidden.androidio.inputStream = inputStream;
|
tim@5650
|
466 |
ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
|
tim@5650
|
467 |
|
icculus@5582
|
468 |
// Despite all the visible documentation on [Asset]InputStream claiming
|
icculus@5582
|
469 |
// that the .available() method is not guaranteed to return the entire file
|
icculus@5582
|
470 |
// size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
|
icculus@5582
|
471 |
// android/apis/content/ReadAsset.java imply that Android's
|
icculus@5582
|
472 |
// AssetInputStream.available() /will/ always return the total file size
|
icculus@5582
|
473 |
|
icculus@5582
|
474 |
// size = inputStream.available();
|
icculus@5582
|
475 |
mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
|
icculus@5582
|
476 |
"available", "()I");
|
icculus@5582
|
477 |
ctx->hidden.androidio.size = mEnv->CallIntMethod(inputStream, mid);
|
tim@5650
|
478 |
if (Android_JNI_ExceptionOccurred()) {
|
tim@5650
|
479 |
goto failure;
|
icculus@5582
|
480 |
}
|
icculus@5582
|
481 |
|
icculus@5582
|
482 |
// readableByteChannel = Channels.newChannel(inputStream);
|
tim@5650
|
483 |
channels = mEnv->FindClass("java/nio/channels/Channels");
|
icculus@5582
|
484 |
mid = mEnv->GetStaticMethodID(channels,
|
icculus@5582
|
485 |
"newChannel",
|
icculus@5582
|
486 |
"(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
|
tim@5650
|
487 |
readableByteChannel = mEnv->CallStaticObjectMethod(
|
icculus@5582
|
488 |
channels, mid, inputStream);
|
tim@5650
|
489 |
if (Android_JNI_ExceptionOccurred()) {
|
tim@5650
|
490 |
goto failure;
|
icculus@5582
|
491 |
}
|
icculus@5582
|
492 |
|
tim@5650
|
493 |
ctx->hidden.androidio.readableByteChannel = readableByteChannel;
|
tim@5650
|
494 |
ctx->hidden.androidio.readableByteChannelRef =
|
tim@5650
|
495 |
mEnv->NewGlobalRef(readableByteChannel);
|
tim@5650
|
496 |
|
icculus@5582
|
497 |
// Store .read id for reading purposes
|
icculus@5582
|
498 |
mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
|
icculus@5582
|
499 |
"read", "(Ljava/nio/ByteBuffer;)I");
|
icculus@5582
|
500 |
ctx->hidden.androidio.readMethod = mid;
|
icculus@5582
|
501 |
|
icculus@5582
|
502 |
ctx->hidden.androidio.position = 0;
|
icculus@5582
|
503 |
|
tim@5650
|
504 |
if (false) {
|
tim@5650
|
505 |
failure:
|
tim@5650
|
506 |
result = -1;
|
tim@5650
|
507 |
|
tim@5650
|
508 |
mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
|
tim@5650
|
509 |
|
tim@5650
|
510 |
if(ctx->hidden.androidio.inputStreamRef != NULL) {
|
tim@5650
|
511 |
mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
|
tim@5650
|
512 |
}
|
tim@5650
|
513 |
}
|
tim@5650
|
514 |
|
tim@5650
|
515 |
return result;
|
icculus@5582
|
516 |
}
|
icculus@5582
|
517 |
|
icculus@5582
|
518 |
extern "C" int Android_JNI_FileOpen(SDL_RWops* ctx,
|
icculus@5582
|
519 |
const char* fileName, const char*)
|
icculus@5582
|
520 |
{
|
slouken@6284
|
521 |
LocalReferenceHolder refs;
|
slouken@6284
|
522 |
|
slouken@6284
|
523 |
if (!refs.init(mEnv)) {
|
slouken@6284
|
524 |
return -1;
|
slouken@6284
|
525 |
}
|
slouken@6284
|
526 |
|
icculus@5582
|
527 |
if (!ctx) {
|
icculus@5582
|
528 |
return -1;
|
icculus@5582
|
529 |
}
|
icculus@5582
|
530 |
|
icculus@5582
|
531 |
jstring fileNameJString = mEnv->NewStringUTF(fileName);
|
icculus@5582
|
532 |
ctx->hidden.androidio.fileName = fileNameJString;
|
icculus@5582
|
533 |
ctx->hidden.androidio.fileNameRef = mEnv->NewGlobalRef(fileNameJString);
|
tim@5650
|
534 |
ctx->hidden.androidio.inputStreamRef = NULL;
|
icculus@5582
|
535 |
|
icculus@5582
|
536 |
return Android_JNI_FileOpen(ctx);
|
icculus@5582
|
537 |
}
|
icculus@5582
|
538 |
|
icculus@5582
|
539 |
extern "C" size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
|
icculus@5582
|
540 |
size_t size, size_t maxnum)
|
icculus@5582
|
541 |
{
|
slouken@6284
|
542 |
LocalReferenceHolder refs;
|
icculus@5582
|
543 |
int bytesRemaining = size * maxnum;
|
icculus@5582
|
544 |
int bytesRead = 0;
|
icculus@5582
|
545 |
|
slouken@6284
|
546 |
if (!refs.init(mEnv)) {
|
slouken@6284
|
547 |
return -1;
|
slouken@6284
|
548 |
}
|
slouken@6284
|
549 |
|
icculus@5582
|
550 |
jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannel;
|
icculus@5582
|
551 |
jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
|
icculus@5582
|
552 |
jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
|
icculus@5582
|
553 |
|
icculus@5582
|
554 |
while (bytesRemaining > 0) {
|
icculus@5582
|
555 |
// result = readableByteChannel.read(...);
|
icculus@5582
|
556 |
int result = mEnv->CallIntMethod(readableByteChannel, readMethod, byteBuffer);
|
icculus@5582
|
557 |
|
tim@5650
|
558 |
if (Android_JNI_ExceptionOccurred()) {
|
icculus@5582
|
559 |
return 0;
|
icculus@5582
|
560 |
}
|
icculus@5582
|
561 |
|
icculus@5582
|
562 |
if (result < 0) {
|
icculus@5582
|
563 |
break;
|
icculus@5582
|
564 |
}
|
icculus@5582
|
565 |
|
icculus@5582
|
566 |
bytesRemaining -= result;
|
icculus@5582
|
567 |
bytesRead += result;
|
icculus@5582
|
568 |
ctx->hidden.androidio.position += result;
|
icculus@5582
|
569 |
}
|
icculus@5582
|
570 |
|
icculus@5582
|
571 |
return bytesRead / size;
|
icculus@5582
|
572 |
}
|
icculus@5582
|
573 |
|
icculus@5582
|
574 |
extern "C" size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
|
icculus@5582
|
575 |
size_t size, size_t num)
|
icculus@5582
|
576 |
{
|
icculus@5582
|
577 |
SDL_SetError("Cannot write to Android package filesystem");
|
icculus@5582
|
578 |
return 0;
|
icculus@5582
|
579 |
}
|
icculus@5582
|
580 |
|
icculus@5582
|
581 |
static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
|
icculus@5582
|
582 |
{
|
slouken@6284
|
583 |
LocalReferenceHolder refs;
|
icculus@5582
|
584 |
int result = 0;
|
icculus@5582
|
585 |
|
slouken@6284
|
586 |
if (!refs.init(mEnv)) {
|
slouken@6284
|
587 |
SDL_SetError("Failed to allocate enough JVM local references");
|
slouken@6284
|
588 |
return -1;
|
slouken@6284
|
589 |
}
|
slouken@6284
|
590 |
|
icculus@5582
|
591 |
if (ctx) {
|
icculus@5582
|
592 |
if (release) {
|
icculus@5582
|
593 |
mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
|
icculus@5582
|
594 |
}
|
icculus@5582
|
595 |
|
icculus@5582
|
596 |
jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
|
icculus@5582
|
597 |
|
icculus@5582
|
598 |
// inputStream.close();
|
icculus@5582
|
599 |
jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
|
icculus@5582
|
600 |
"close", "()V");
|
icculus@5582
|
601 |
mEnv->CallVoidMethod(inputStream, mid);
|
icculus@5582
|
602 |
mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
|
icculus@5582
|
603 |
mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
|
tim@5650
|
604 |
if (Android_JNI_ExceptionOccurred()) {
|
icculus@5582
|
605 |
result = -1;
|
icculus@5582
|
606 |
}
|
icculus@5582
|
607 |
|
icculus@5582
|
608 |
if (release) {
|
icculus@5582
|
609 |
SDL_FreeRW(ctx);
|
icculus@5582
|
610 |
}
|
icculus@5582
|
611 |
}
|
icculus@5582
|
612 |
|
icculus@5582
|
613 |
return result;
|
icculus@5582
|
614 |
}
|
icculus@5582
|
615 |
|
icculus@5582
|
616 |
|
icculus@5582
|
617 |
extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)
|
icculus@5582
|
618 |
{
|
icculus@5582
|
619 |
long newPosition;
|
icculus@5582
|
620 |
|
icculus@5582
|
621 |
switch (whence) {
|
icculus@5582
|
622 |
case RW_SEEK_SET:
|
icculus@5582
|
623 |
newPosition = offset;
|
icculus@5582
|
624 |
break;
|
icculus@5582
|
625 |
case RW_SEEK_CUR:
|
icculus@5582
|
626 |
newPosition = ctx->hidden.androidio.position + offset;
|
icculus@5582
|
627 |
break;
|
icculus@5582
|
628 |
case RW_SEEK_END:
|
icculus@5582
|
629 |
newPosition = ctx->hidden.androidio.size + offset;
|
icculus@5582
|
630 |
break;
|
icculus@5582
|
631 |
default:
|
icculus@5582
|
632 |
SDL_SetError("Unknown value for 'whence'");
|
icculus@5582
|
633 |
return -1;
|
icculus@5582
|
634 |
}
|
icculus@5582
|
635 |
if (newPosition < 0) {
|
icculus@5582
|
636 |
newPosition = 0;
|
icculus@5582
|
637 |
}
|
icculus@5582
|
638 |
if (newPosition > ctx->hidden.androidio.size) {
|
icculus@5582
|
639 |
newPosition = ctx->hidden.androidio.size;
|
icculus@5582
|
640 |
}
|
icculus@5582
|
641 |
|
icculus@5582
|
642 |
long movement = newPosition - ctx->hidden.androidio.position;
|
icculus@5582
|
643 |
jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
|
icculus@5582
|
644 |
|
icculus@5582
|
645 |
if (movement > 0) {
|
tim@5993
|
646 |
unsigned char buffer[1024];
|
tim@5993
|
647 |
|
icculus@5582
|
648 |
// The easy case where we're seeking forwards
|
icculus@5582
|
649 |
while (movement > 0) {
|
icculus@5994
|
650 |
long amount = (long) sizeof (buffer);
|
icculus@5994
|
651 |
if (amount > movement) {
|
icculus@5994
|
652 |
amount = movement;
|
icculus@5994
|
653 |
}
|
icculus@5994
|
654 |
size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);
|
tim@5993
|
655 |
|
tim@5993
|
656 |
if (result <= 0) {
|
tim@5993
|
657 |
// Failed to read/skip the required amount, so fail
|
icculus@5582
|
658 |
return -1;
|
icculus@5582
|
659 |
}
|
tim@5993
|
660 |
|
tim@5993
|
661 |
movement -= result;
|
icculus@5582
|
662 |
}
|
icculus@5582
|
663 |
} else if (movement < 0) {
|
icculus@5582
|
664 |
// We can't seek backwards so we have to reopen the file and seek
|
icculus@5582
|
665 |
// forwards which obviously isn't very efficient
|
icculus@5582
|
666 |
Android_JNI_FileClose(ctx, false);
|
icculus@5582
|
667 |
Android_JNI_FileOpen(ctx);
|
icculus@5582
|
668 |
Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
|
icculus@5582
|
669 |
}
|
icculus@5582
|
670 |
|
icculus@5582
|
671 |
ctx->hidden.androidio.position = newPosition;
|
icculus@5582
|
672 |
|
icculus@5582
|
673 |
return ctx->hidden.androidio.position;
|
icculus@5582
|
674 |
}
|
icculus@5582
|
675 |
|
icculus@5582
|
676 |
extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
|
icculus@5582
|
677 |
{
|
icculus@5582
|
678 |
return Android_JNI_FileClose(ctx, true);
|
icculus@5582
|
679 |
}
|
icculus@5582
|
680 |
|
slouken@6044
|
681 |
#endif /* __ANDROID__ */
|
slouken@6044
|
682 |
|
slouken@4981
|
683 |
/* vi: set ts=4 sw=4 expandtab: */
|