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