This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_android.cpp
241 lines (196 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2010 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
24
25
#include "SDL_android.h"
26
27
28
extern "C" {
#include "events/SDL_events_c.h"
#include "video/android/SDL_androidkeyboard.h"
29
#include "video/android/SDL_androidvideo.h"
30
31
32
33
/* Impelemented in audio/android/SDL_androidaudio.c */
extern void Android_RunAudioThread();
} // C
34
35
36
37
38
39
40
41
42
43
44
/*******************************************************************************
This file links the Java side of Android with libsdl
*******************************************************************************/
#include <jni.h>
#include <android/log.h>
/*******************************************************************************
Globals
*******************************************************************************/
45
46
47
static JavaVM* mVM = NULL;
static JNIEnv* mEnv = NULL;
static JNIEnv* mAudioEnv = NULL;
48
49
50
// Main activity
static jclass mActivityInstance;
51
52
53
54
55
56
57
// method signatures
static jmethodID midCreateGLContext;
static jmethodID midFlipBuffers;
static jmethodID midAudioInit;
static jmethodID midAudioWriteShortBuffer;
static jmethodID midAudioWriteByteBuffer;
58
static jmethodID midAudioQuit;
59
60
// Accelerometer data storage
61
62
63
64
65
float fLastAccelerometer[3];
/*******************************************************************************
Functions called by JNI
66
*******************************************************************************/
67
68
69
70
71
// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
mVM = vm;
72
73
74
return JNI_VERSION_1_4;
}
75
76
77
78
// Called before SDL_main() to initialize JNI bindings
extern "C" void SDL_Android_Init(JNIEnv* env)
{
79
80
mEnv = env;
81
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
82
83
84
85
86
jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity");
mActivityInstance = cls;
midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
87
88
89
90
midAudioInit = mEnv->GetStaticMethodID(cls, "audioInit", "(IZZI)Ljava/lang/Object;");
midAudioWriteShortBuffer = mEnv->GetStaticMethodID(cls, "audioWriteShortBuffer", "([S)V");
midAudioWriteByteBuffer = mEnv->GetStaticMethodID(cls, "audioWriteByteBuffer", "([B)V");
midAudioQuit = mEnv->GetStaticMethodID(cls, "audioQuit", "()V");
91
92
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
93
94
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
95
96
97
}
}
98
99
100
101
102
103
104
105
// Resize
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
JNIEnv* env, jobject obj,
jint width, jint height, jint format)
{
Android_SetScreenResolution(width, height, format);
}
106
// Keydown
107
108
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
JNIEnv* env, jobject obj, jint keycode)
109
{
110
Android_OnKeyDown(keycode);
111
112
}
113
// Keyup
114
115
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
JNIEnv* env, jobject obj, jint keycode)
116
{
117
Android_OnKeyUp(keycode);
118
119
}
120
// Touch
121
122
123
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
JNIEnv* env, jobject obj,
jint action, jfloat x, jfloat y, jfloat p)
124
{
125
126
127
128
129
130
131
132
133
#ifdef DEBUG
__android_log_print(ANDROID_LOG_INFO, "SDL",
"SDL: native touch event %d @ %f/%f, pressure %f\n",
action, x, y, p);
#endif
//TODO: Pass this off to the SDL multitouch stuff
}
134
135
136
137
138
139
140
141
142
143
// Accelerometer
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
JNIEnv* env, jobject obj,
jfloat x, jfloat y, jfloat z)
{
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
}
144
// Quit
145
146
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
JNIEnv* env, jobject obj)
147
148
149
{
// Inject a SDL_QUIT event
SDL_SendQuit();
150
151
}
152
153
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
JNIEnv* env)
154
{
155
156
mVM->AttachCurrentThread(&mAudioEnv, NULL);
Android_RunAudioThread();
157
158
159
160
161
162
}
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
163
extern "C" void Android_JNI_CreateContext()
164
{
165
mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext);
166
167
}
168
extern "C" void Android_JNI_SwapWindow()
169
170
{
mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers);
171
172
}
173
174
175
//
// Audio support
//
176
177
178
179
static jboolean audioBuffer16Bit = JNI_FALSE;
static jboolean audioBufferStereo = JNI_FALSE;
static jobject audioBuffer = NULL;
static void* audioBufferPinned = NULL;
180
181
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
182
{
183
int audioBufferFrames;
184
185
186
187
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
audioBuffer16Bit = is16Bit;
audioBufferStereo = channelCount > 1;
188
189
audioBuffer = mEnv->CallStaticObjectMethod(mActivityInstance, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
if (audioBuffer == NULL) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
return 0;
}
audioBuffer = mEnv->NewGlobalRef(audioBuffer);
jboolean isCopy = JNI_FALSE;
if (audioBuffer16Bit) {
audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
} else {
audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
}
if (audioBufferStereo) {
audioBufferFrames /= 2;
}
208
209
return audioBufferFrames;
210
211
}
212
extern "C" void * Android_JNI_GetAudioBuffer()
213
{
214
215
216
//jboolean isCopy = JNI_FALSE;
//audioBufferPinned = mAudioEnv->GetPrimitiveArrayCritical((jarray)audioBuffer, &isCopy);
return audioBufferPinned;
217
218
}
219
extern "C" void Android_JNI_WriteAudioBuffer()
220
{
221
222
223
224
225
226
227
228
229
230
//mAudioEnv->ReleasePrimitiveArrayCritical((jarray)audioBuffer, audioBufferPinned, 0);
if (audioBuffer16Bit) {
mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
mAudioEnv->CallStaticVoidMethod(mActivityInstance, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
} else {
mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
mAudioEnv->CallStaticVoidMethod(mActivityInstance, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
}
/* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
231
232
233
234
}
extern "C" void Android_JNI_CloseAudioDevice()
{
235
mEnv->CallStaticVoidMethod(mActivityInstance, midAudioQuit);
236
237
238
mEnv->DeleteGlobalRef(audioBuffer);
audioBuffer = NULL;
239
}
240
241
/* vi: set ts=4 sw=4 expandtab: */