This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_android.cpp
263 lines (216 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
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"
23
#include "SDL_stdinc.h"
24
25
26
#include "SDL_android.h"
27
extern "C" {
28
29
30
31
#include "../../events/SDL_events_c.h"
#include "../../video/android/SDL_androidkeyboard.h"
#include "../../video/android/SDL_androidtouch.h"
#include "../../video/android/SDL_androidvideo.h"
32
33
34
35
/* Impelemented in audio/android/SDL_androidaudio.c */
extern void Android_RunAudioThread();
} // C
36
37
38
39
40
41
42
43
44
45
46
/*******************************************************************************
This file links the Java side of Android with libsdl
*******************************************************************************/
#include <jni.h>
#include <android/log.h>
/*******************************************************************************
Globals
*******************************************************************************/
47
48
static JNIEnv* mEnv = NULL;
static JNIEnv* mAudioEnv = NULL;
49
50
// Main activity
51
static jclass mActivityClass;
52
53
54
55
56
57
58
// method signatures
static jmethodID midCreateGLContext;
static jmethodID midFlipBuffers;
static jmethodID midAudioInit;
static jmethodID midAudioWriteShortBuffer;
static jmethodID midAudioWriteByteBuffer;
59
static jmethodID midAudioQuit;
60
61
// Accelerometer data storage
62
static float fLastAccelerometer[3];
63
64
65
66
/*******************************************************************************
Functions called by JNI
67
*******************************************************************************/
68
69
70
71
72
73
// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
return JNI_VERSION_1_4;
}
74
75
// Called before SDL_main() to initialize JNI bindings
76
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
77
78
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
79
80
81
82
83
mEnv = env;
mActivityClass = cls;
midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
84
"createGLContext","(II)Z");
85
86
87
88
89
90
91
92
93
94
midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
"flipBuffers","()V");
midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
"audioInit", "(IZZI)Ljava/lang/Object;");
midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
"audioWriteShortBuffer", "([S)V");
midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
"audioWriteByteBuffer", "([B)V");
midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
"audioQuit", "()V");
95
96
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
97
98
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
99
100
101
}
}
102
103
// Resize
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
104
JNIEnv* env, jclass jcls,
105
106
107
108
109
jint width, jint height, jint format)
{
Android_SetScreenResolution(width, height, format);
}
110
// Keydown
111
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
112
JNIEnv* env, jclass jcls, jint keycode)
113
{
114
Android_OnKeyDown(keycode);
115
116
}
117
// Keyup
118
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
119
JNIEnv* env, jclass jcls, jint keycode)
120
{
121
Android_OnKeyUp(keycode);
122
123
}
124
// Touch
125
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
126
JNIEnv* env, jclass jcls,
127
jint action, jfloat x, jfloat y, jfloat p)
128
{
129
Android_OnTouch(action, x, y, p);
130
131
}
132
133
// Accelerometer
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
134
JNIEnv* env, jclass jcls,
135
136
137
138
139
140
141
jfloat x, jfloat y, jfloat z)
{
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
}
142
// Quit
143
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
144
JNIEnv* env, jclass cls)
145
146
147
{
// Inject a SDL_QUIT event
SDL_SendQuit();
148
149
}
150
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
151
JNIEnv* env, jclass cls)
152
{
153
154
155
/* This is the audio thread, with a different environment */
mAudioEnv = env;
156
Android_RunAudioThread();
157
158
159
160
161
162
}
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
163
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
164
{
165
166
167
168
169
if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
return SDL_TRUE;
} else {
return SDL_FALSE;
}
170
171
}
172
extern "C" void Android_JNI_SwapWindow()
173
{
174
175
176
177
178
179
180
181
182
183
184
mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
}
extern "C" void Android_JNI_SetActivityTitle(const char *title)
{
jmethodID mid;
mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
if (mid) {
mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
}
185
186
}
187
188
189
190
191
192
193
194
extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
{
int i;
for (i = 0; i < 3; ++i) {
values[i] = fLastAccelerometer[i];
}
}
195
196
197
//
// Audio support
//
198
199
200
201
static jboolean audioBuffer16Bit = JNI_FALSE;
static jboolean audioBufferStereo = JNI_FALSE;
static jobject audioBuffer = NULL;
static void* audioBufferPinned = NULL;
202
203
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
204
{
205
int audioBufferFrames;
206
207
208
209
__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
audioBuffer16Bit = is16Bit;
audioBufferStereo = channelCount > 1;
210
211
audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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;
}
230
231
return audioBufferFrames;
232
233
}
234
extern "C" void * Android_JNI_GetAudioBuffer()
235
{
236
return audioBufferPinned;
237
238
}
239
extern "C" void Android_JNI_WriteAudioBuffer()
240
{
241
242
if (audioBuffer16Bit) {
mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
243
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
244
245
} else {
mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
246
mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
247
248
249
}
/* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
250
251
252
253
}
extern "C" void Android_JNI_CloseAudioDevice()
{
254
mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit);
255
256
257
258
259
260
if (audioBuffer) {
mEnv->DeleteGlobalRef(audioBuffer);
audioBuffer = NULL;
audioBufferPinned = NULL;
}
261
}
262
263
/* vi: set ts=4 sw=4 expandtab: */