This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_android.cpp
263 lines (215 loc) · 8.54 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
static JNIEnv* mEnv = NULL;
static JNIEnv* mAudioEnv = NULL;
47
48
// Main activity
49
static jclass mActivityClass;
50
51
52
53
54
55
56
// method signatures
static jmethodID midCreateGLContext;
static jmethodID midFlipBuffers;
static jmethodID midAudioInit;
static jmethodID midAudioWriteShortBuffer;
static jmethodID midAudioWriteByteBuffer;
57
static jmethodID midAudioQuit;
58
59
// Accelerometer data storage
60
static float fLastAccelerometer[3];
61
62
63
64
/*******************************************************************************
Functions called by JNI
65
*******************************************************************************/
66
67
68
69
70
71
// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
return JNI_VERSION_1_4;
}
72
73
// Called before SDL_main() to initialize JNI bindings
74
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
75
76
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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");
93
94
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
95
96
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
97
98
99
}
}
100
101
// Resize
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
102
JNIEnv* env, jclass jcls,
103
104
105
106
107
jint width, jint height, jint format)
{
Android_SetScreenResolution(width, height, format);
}
108
// Keydown
109
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
110
JNIEnv* env, jclass jcls, jint keycode)
111
{
112
Android_OnKeyDown(keycode);
113
114
}
115
// Keyup
116
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
117
JNIEnv* env, jclass jcls, jint keycode)
118
{
119
Android_OnKeyUp(keycode);
120
121
}
122
// Touch
123
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
124
JNIEnv* env, jclass jcls,
125
jint action, jfloat x, jfloat y, jfloat p)
126
{
127
128
129
130
131
132
133
134
135
#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
}
136
137
// Accelerometer
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
138
JNIEnv* env, jclass jcls,
139
140
141
142
143
144
145
jfloat x, jfloat y, jfloat z)
{
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
}
146
// Quit
147
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
148
JNIEnv* env, jclass cls)
149
150
151
{
// Inject a SDL_QUIT event
SDL_SendQuit();
152
153
}
154
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
155
JNIEnv* env, jclass cls)
156
{
157
158
159
/* This is the audio thread, with a different environment */
mAudioEnv = env;
160
Android_RunAudioThread();
161
162
163
164
165
166
}
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
167
extern "C" void Android_JNI_CreateContext()
168
{
169
mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext);
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: */