src/core/android/SDL_android.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Thu, 17 Sep 2015 22:30:24 +0200
changeset 9870 6dd5ab47534b
parent 9869 4ba43d626c4a
child 9873 b0f121cfa074
permissions -rw-r--r--
Android: Fixed trying to read from APK expansion files without version hint set. This also fixed overwriting the asset error message which is more useful if no APK expansion files are available and the requested file was not found.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
9619
b94b6d0bff0f Updated the copyright year to 2015
Sam Lantinga <slouken@libsdl.org>
parents: 9594
diff changeset
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8057
diff changeset
    21
#include "../../SDL_internal.h"
5222
c66b2a778b7e Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
Sam Lantinga <slouken@libsdl.org>
parents: 5092
diff changeset
    22
#include "SDL_stdinc.h"
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
    23
#include "SDL_assert.h"
8961
7dbbee37826b Fixed compiler warning on Android
Sam Lantinga <slouken@libsdl.org>
parents: 8900
diff changeset
    24
#include "SDL_hints.h"
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    25
#include "SDL_log.h"
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    27
#ifdef __ANDROID__
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    28
6630
55910871076b Added some platform specific API functions for Android:
Sam Lantinga <slouken@libsdl.org>
parents: 6555
diff changeset
    29
#include "SDL_system.h"
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    30
#include "SDL_android.h"
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
    31
#include <EGL/egl.h>
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    32
5092
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    33
#include "../../events/SDL_events_c.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    34
#include "../../video/android/SDL_androidkeyboard.h"
9438
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
    35
#include "../../video/android/SDL_androidmouse.h"
5092
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    36
#include "../../video/android/SDL_androidtouch.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    37
#include "../../video/android/SDL_androidvideo.h"
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
    38
#include "../../video/android/SDL_androidwindow.h"
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
    39
#include "../../joystick/android/SDL_sysjoystick_c.h"
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    40
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    41
#include <android/log.h>
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    42
#include <pthread.h>
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    43
#include <sys/types.h>
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    44
#include <unistd.h>
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    45
#define LOG_TAG "SDL_android"
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    46
/* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    47
/* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
    48
#define LOGI(...) do {} while (0)
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
    49
#define LOGE(...) do {} while (0)
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    50
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    51
/* Uncomment this to log messages entering and exiting methods in this file */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    52
/* #define DEBUG_JNI */
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    53
7246
be3beed886ea Moved prototype for static function out of header file to prevent warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    54
static void Android_JNI_ThreadDestroyed(void*);
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
    55
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
 This file links the Java side of Android with libsdl
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
#include <jni.h>
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
#include <android/log.h>
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
                               Globals
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
*******************************************************************************/
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    66
static pthread_key_t mThreadKey;
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    67
static JavaVM* mJavaVM;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    69
/* Main activity */
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
    70
static jclass mActivityClass;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    72
/* method signatures */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
    73
static jmethodID midGetNativeSurface;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    74
static jmethodID midFlipBuffers;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    75
static jmethodID midAudioInit;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    76
static jmethodID midAudioWriteShortBuffer;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    77
static jmethodID midAudioWriteByteBuffer;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
    78
static jmethodID midAudioQuit;
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
    79
static jmethodID midPollInputDevices;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    81
/* Accelerometer data storage */
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    82
static float fLastAccelerometer[3];
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
    83
static SDL_bool bHasNewData;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
                 Functions called by JNI
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    89
/* Library init */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
    90
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
{
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    92
    JNIEnv *env;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    93
    mJavaVM = vm;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    94
    LOGI("JNI_OnLoad called");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    95
    if ((*mJavaVM)->GetEnv(mJavaVM, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    96
        LOGE("Failed to get the environment using GetEnv()");
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    97
        return -1;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    98
    }
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    99
    /*
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   100
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   101
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   102
     */
8055
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   103
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed) != 0) {
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   104
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   105
    }
8055
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   106
    Android_JNI_SetupThread();
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   107
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
    return JNI_VERSION_1_4;
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   111
/* Called before SDL_main() to initialize JNI bindings */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   112
JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
{
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   116
    Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   117
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   118
    mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   119
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   120
    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   121
                                "getNativeSurface","()Landroid/view/Surface;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   122
    midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   123
                                "flipBuffers","()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   124
    midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
7552
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   125
                                "audioInit", "(IZZI)I");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   126
    midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   127
                                "audioWriteShortBuffer", "([S)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   128
    midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   129
                                "audioWriteByteBuffer", "([B)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   130
    midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   131
                                "audioQuit", "()V");
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   132
    midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   133
                                "pollInputDevices", "()V");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   135
    bHasNewData = SDL_FALSE;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   136
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   137
    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   138
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit || !midPollInputDevices) {
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   139
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    }
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   141
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
9640
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   144
/* Drop file */
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   145
void Java_org_libsdl_app_SDLActivity_onNativeDropFile(
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   146
                                    JNIEnv* env, jclass jcls,
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   147
                                    jstring filename)
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   148
{
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   149
    const char *path = (*env)->GetStringUTFChars(env, filename, NULL);
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   150
    SDL_SendDropFile(path);
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   151
    (*env)->ReleaseStringUTFChars(env, filename, path);
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   152
}
d86771e28c51 Android: Added basic drop file support (thanks, "noxalus"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   153
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   154
/* Resize */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   155
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeResize(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   156
                                    JNIEnv* env, jclass jcls,
9314
8d826bc39a45 Fixed bug 2816 - [patch] Android: Expose screen refresh rate
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9271
diff changeset
   157
                                    jint width, jint height, jint format, jfloat rate)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
{
9314
8d826bc39a45 Fixed bug 2816 - [patch] Android: Expose screen refresh rate
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9271
diff changeset
   159
    Android_SetScreenResolution(width, height, format, rate);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   160
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   161
8776
29aac8b813d9 Changed C++ style comments to fix pedantic warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8760
diff changeset
   162
/* Paddown */
9858
6d6a972746b3 Android: Changed return type of four JNI functions from int to jint.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9640
diff changeset
   163
JNIEXPORT jint JNICALL Java_org_libsdl_app_SDLActivity_onNativePadDown(
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   164
                                    JNIEnv* env, jclass jcls,
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   165
                                    jint device_id, jint keycode)
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   166
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   167
    return Android_OnPadDown(device_id, keycode);
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   168
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   169
8776
29aac8b813d9 Changed C++ style comments to fix pedantic warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8760
diff changeset
   170
/* Padup */
9858
6d6a972746b3 Android: Changed return type of four JNI functions from int to jint.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9640
diff changeset
   171
JNIEXPORT jint JNICALL Java_org_libsdl_app_SDLActivity_onNativePadUp(
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   172
                                   JNIEnv* env, jclass jcls,
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   173
                                   jint device_id, jint keycode)
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   174
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   175
    return Android_OnPadUp(device_id, keycode);
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   176
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   177
7926
2d2a85852f74 Corrected source comment.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7910
diff changeset
   178
/* Joy */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   179
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeJoy(
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   180
                                    JNIEnv* env, jclass jcls,
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   181
                                    jint device_id, jint axis, jfloat value)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   182
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   183
    Android_OnJoy(device_id, axis, value);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   184
}
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   185
8140
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   186
/* POV Hat */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   187
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeHat(
8140
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   188
                                    JNIEnv* env, jclass jcls,
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   189
                                    jint device_id, jint hat_id, jint x, jint y)
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   190
{
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   191
    Android_OnHat(device_id, hat_id, x, y);
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   192
}
8372466c003c fixed hat code validation.
dbrady <dbrady@telltalegames.com>
parents: 8093
diff changeset
   193
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   194
9858
6d6a972746b3 Android: Changed return type of four JNI functions from int to jint.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9640
diff changeset
   195
JNIEXPORT jint JNICALL Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   196
    JNIEnv* env, jclass jcls,
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   197
    jint device_id, jstring device_name, jint is_accelerometer, 
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   198
    jint nbuttons, jint naxes, jint nhats, jint nballs)
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   199
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   200
    int retval;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   201
    const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   202
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   203
    retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   204
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   205
    (*env)->ReleaseStringUTFChars(env, device_name, name);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   206
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   207
    return retval;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   208
}
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   209
9858
6d6a972746b3 Android: Changed return type of four JNI functions from int to jint.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9640
diff changeset
   210
JNIEXPORT jint JNICALL Java_org_libsdl_app_SDLActivity_nativeRemoveJoystick(
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   211
    JNIEnv* env, jclass jcls, jint device_id)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   212
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8055
diff changeset
   213
    return Android_RemoveJoystick(device_id);
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   214
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   215
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   216
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   217
/* Surface Created */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   218
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   219
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   220
    SDL_WindowData *data;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   221
    SDL_VideoDevice *_this;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   222
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   223
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   224
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   225
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   226
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   227
    _this =  SDL_GetVideoDevice();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   228
    data =  (SDL_WindowData *) Android_Window->driverdata;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   229
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   230
    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   231
    if (data->egl_surface == EGL_NO_SURFACE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   232
        if(data->native_window) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   233
            ANativeWindow_release(data->native_window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   234
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   235
        data->native_window = Android_JNI_GetNativeWindow();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   236
        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   237
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   238
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   239
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   240
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   241
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   242
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   243
/* Surface Destroyed */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   244
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   245
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   246
    /* We have to clear the current context and destroy the egl surface here
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   247
     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   248
     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   249
     */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   250
    SDL_WindowData *data;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   251
    SDL_VideoDevice *_this;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   252
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   253
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   254
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   255
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   256
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   257
    _this =  SDL_GetVideoDevice();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   258
    data = (SDL_WindowData *) Android_Window->driverdata;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   259
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   260
    if (data->egl_surface != EGL_NO_SURFACE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   261
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   262
        SDL_EGL_DestroySurface(_this, data->egl_surface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   263
        data->egl_surface = EGL_NO_SURFACE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   264
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   265
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   266
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   267
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   268
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   269
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   270
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   271
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   272
    SDL_GL_SwapWindow(Android_Window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   273
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   274
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   275
/* Keydown */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   276
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   277
                                    JNIEnv* env, jclass jcls, jint keycode)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   278
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   279
    Android_OnKeyDown(keycode);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   282
/* Keyup */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   283
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   284
                                    JNIEnv* env, jclass jcls, jint keycode)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   286
    Android_OnKeyUp(keycode);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   289
/* Keyboard Focus Lost */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   290
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeKeyboardFocusLost(
7564
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   291
                                    JNIEnv* env, jclass jcls)
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   292
{
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   293
    /* Calling SDL_StopTextInput will take care of hiding the keyboard and cleaning up the DummyText widget */
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   294
    SDL_StopTextInput();
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   295
}
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   296
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   297
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   298
/* Touch */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   299
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeTouch(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   300
                                    JNIEnv* env, jclass jcls,
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   301
                                    jint touch_device_id_in, jint pointer_finger_id_in,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   302
                                    jint action, jfloat x, jfloat y, jfloat p)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   304
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
9438
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   307
/* Mouse */
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   308
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeMouse(
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   309
                                    JNIEnv* env, jclass jcls,
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   310
                                    jint button, jint action, jfloat x, jfloat y)
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   311
{
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   312
    Android_OnMouse(button, action, x, y);
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   313
}
fff5af5de6dd * Improve mouse support in Android. These changes require Android API v12 to compile
Joseba García Etxebarria <joseba.gar@gmail.com>
parents: 9322
diff changeset
   314
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   315
/* Accelerometer */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   316
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_onNativeAccel(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   317
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   318
                                    jfloat x, jfloat y, jfloat z)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
{
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
    fLastAccelerometer[0] = x;
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
    fLastAccelerometer[1] = y;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   322
    fLastAccelerometer[2] = z;
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   323
    bHasNewData = SDL_TRUE;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   326
/* Low memory */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   327
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeLowMemory(
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   328
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   329
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   330
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   331
}
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   332
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   333
/* Quit */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   334
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeQuit(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   335
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   336
{
7910
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   337
    /* Discard previous events. The user should have handled state storage
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   338
     * in SDL_APP_WILLENTERBACKGROUND. After nativeQuit() is called, no
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   339
     * events other than SDL_QUIT and SDL_APP_TERMINATING should fire */
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   340
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   341
    /* Inject a SDL_QUIT event */
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   342
    SDL_SendQuit();
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   343
    SDL_SendAppEvent(SDL_APP_TERMINATING);
7910
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   344
    /* Resume the event loop so that the app can catch SDL_QUIT which
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   345
     * should now be the top event in the event queue. */
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   346
    if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   347
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   348
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   349
/* Pause */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   350
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativePause(
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   351
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   352
{
8039
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   353
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   354
    if (Android_Window) {
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   355
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
6191
2c0d35b1af4e Made the application activity events consistent between iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 6189
diff changeset
   356
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
8039
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   357
        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   358
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   359
    
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   360
        /* *After* sending the relevant events, signal the pause semaphore 
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   361
         * so the event loop knows to pause and (optionally) block itself */
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   362
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   363
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   364
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   365
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   366
/* Resume */
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   367
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLActivity_nativeResume(
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   368
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   369
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   370
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   371
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   372
    if (Android_Window) {
8047
a5270cef21a7 [Android] Signal the resume semaphore after pushing the relevant events
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8039
diff changeset
   373
        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
a5270cef21a7 [Android] Signal the resume semaphore after pushing the relevant events
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8039
diff changeset
   374
        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
a5270cef21a7 [Android] Signal the resume semaphore after pushing the relevant events
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8039
diff changeset
   375
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
a5270cef21a7 [Android] Signal the resume semaphore after pushing the relevant events
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8039
diff changeset
   376
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   377
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   378
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   379
         * and this function will be called from the Java thread instead.
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   380
         */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   381
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   382
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   383
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   384
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   385
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   386
                                    JNIEnv* env, jclass cls,
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   387
                                    jstring text, jint newCursorPosition)
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   388
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   389
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   390
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   391
    SDL_SendKeyboardText(utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   392
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   393
    (*env)->ReleaseStringUTFChars(env, text, utftext);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   394
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   395
8760
27eedcca604d Export android jni functions even when build with -fvisibility=hidden flag
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 8149
diff changeset
   396
JNIEXPORT void JNICALL Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   397
                                    JNIEnv* env, jclass cls,
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   398
                                    jstring text, jint newCursorPosition)
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   399
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   400
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   401
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   402
    SDL_SendEditingText(utftext, 0, 0);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   403
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   404
    (*env)->ReleaseStringUTFChars(env, text, utftext);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   405
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   406
9268
7f2833a2191b Fixed bug 2811 - [patch] Android core: Fix JNI 'nativeGetHint' symbol not being exported
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9231
diff changeset
   407
JNIEXPORT jstring JNICALL Java_org_libsdl_app_SDLActivity_nativeGetHint(JNIEnv* env, jclass cls, jstring name) {
8896
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   408
    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   409
    const char *hint = SDL_GetHint(utfname);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   410
8896
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   411
    jstring result = (*env)->NewStringUTF(env, hint);
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   412
    (*env)->ReleaseStringUTFChars(env, name, utfname);
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   413
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   414
    return result;
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   415
}
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   416
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
             Functions called by SDL into Java
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
*******************************************************************************/
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   420
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   421
static int s_active = 0;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   422
struct LocalReferenceHolder
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   423
{
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   424
    JNIEnv *m_env;
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   425
    const char *m_func;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   426
};
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   427
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   428
static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   429
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   430
    struct LocalReferenceHolder refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   431
    refholder.m_env = NULL;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   432
    refholder.m_func = func;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   433
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   434
    SDL_Log("Entering function %s", func);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   435
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   436
    return refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   437
}
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   438
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   439
static SDL_bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   440
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   441
    const int capacity = 16;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   442
    if ((*env)->PushLocalFrame(env, capacity) < 0) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   443
        SDL_SetError("Failed to allocate enough JVM local references");
7516
4a98853e8e4c Corrected return value to be of correct type.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7515
diff changeset
   444
        return SDL_FALSE;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   445
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   446
    ++s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   447
    refholder->m_env = env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   448
    return SDL_TRUE;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   449
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   450
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   451
static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   452
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   453
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   454
    SDL_Log("Leaving function %s", refholder->m_func);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   455
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   456
    if (refholder->m_env) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   457
        JNIEnv* env = refholder->m_env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   458
        (*env)->PopLocalFrame(env, NULL);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   459
        --s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   460
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   461
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   462
9594
0285b5e1c5ab Android: Fixed empty parameter list in signatures of internal functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9450
diff changeset
   463
static SDL_bool LocalReferenceHolder_IsActive(void)
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   464
{
9173
c677ab1148c5 Bug 2739 - [Android] No support for SDL_DisableScreenSaver by Martin Gerhardy
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9135
diff changeset
   465
    return s_active > 0;
c677ab1148c5 Bug 2739 - [Android] No support for SDL_DisableScreenSaver by Martin Gerhardy
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9135
diff changeset
   466
}
c677ab1148c5 Bug 2739 - [Android] No support for SDL_DisableScreenSaver by Martin Gerhardy
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9135
diff changeset
   467
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   468
ANativeWindow* Android_JNI_GetNativeWindow(void)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   469
{
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   470
    ANativeWindow* anw;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   471
    jobject s;
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   472
    JNIEnv *env = Android_JNI_GetEnv();
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   473
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   474
    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   475
    anw = ANativeWindow_fromSurface(env, s);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   476
    (*env)->DeleteLocalRef(env, s);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   477
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   478
    return anw;
7567
52da75545aaa Fixes bug #1889, allows for GL Context deletion/recreation on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7564
diff changeset
   479
}
52da75545aaa Fixes bug #1889, allows for GL Context deletion/recreation on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7564
diff changeset
   480
9594
0285b5e1c5ab Android: Fixed empty parameter list in signatures of internal functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9450
diff changeset
   481
void Android_JNI_SwapWindow(void)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   482
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   483
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   484
    (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midFlipBuffers);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   485
}
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   486
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   487
void Android_JNI_SetActivityTitle(const char *title)
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   488
{
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   489
    jmethodID mid;
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   490
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   491
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   492
    if (mid) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   493
        jstring jtitle = (jstring)((*mEnv)->NewStringUTF(mEnv, title));
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   494
        (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, mid, jtitle);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   495
        (*mEnv)->DeleteLocalRef(mEnv, jtitle);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   496
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   497
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   499
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   500
{
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   501
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   502
    SDL_bool retval = SDL_FALSE;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   503
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   504
    if (bHasNewData) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   505
        for (i = 0; i < 3; ++i) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   506
            values[i] = fLastAccelerometer[i];
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   507
        }
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   508
        bHasNewData = SDL_FALSE;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   509
        retval = SDL_TRUE;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   510
    }
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   511
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   512
    return retval;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   513
}
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   514
8055
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   515
static void Android_JNI_ThreadDestroyed(void* value)
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   516
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   517
    /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   518
    JNIEnv *env = (JNIEnv*) value;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   519
    if (env != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   520
        (*mJavaVM)->DetachCurrentThread(mJavaVM);
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   521
        pthread_setspecific(mThreadKey, NULL);
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   522
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   523
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   524
8055
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   525
JNIEnv* Android_JNI_GetEnv(void)
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   526
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   527
    /* From http://developer.android.com/guide/practices/jni.html
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   528
     * All threads are Linux threads, scheduled by the kernel.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   529
     * They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   530
     * attached to the JavaVM. For example, a thread started with pthread_create can be attached with the
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   531
     * JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv,
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   532
     * and cannot make JNI calls.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   533
     * Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main"
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   534
     * ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   535
     * is a no-op.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   536
     * Note: You can call this function any number of times for the same thread, there's no harm in it
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   537
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   538
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   539
    JNIEnv *env;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   540
    int status = (*mJavaVM)->AttachCurrentThread(mJavaVM, &env, NULL);
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   541
    if(status < 0) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   542
        LOGE("failed to attach current thread");
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   543
        return 0;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   544
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   545
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   546
    /* From http://developer.android.com/guide/practices/jni.html
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   547
     * Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   548
     * in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   549
     * called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   550
     * to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   551
     * Note: The destructor is not called unless the stored value is != NULL
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   552
     * Note: You can call this function any number of times for the same thread, there's no harm in it
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   553
     *       (except for some lost CPU cycles)
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   554
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   555
    pthread_setspecific(mThreadKey, (void*) env);
8055
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   556
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   557
    return env;
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   558
}
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   559
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   560
int Android_JNI_SetupThread(void)
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   561
{
3e2f230a6d62 Associate the environment with any thread that calls Android_JNI_GetEnv(), in case it's been manually created with pthread_create() or C++11.
Sam Lantinga <slouken@libsdl.org>
parents: 8047
diff changeset
   562
    Android_JNI_GetEnv();
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   563
    return 1;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   564
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   565
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   566
/*
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   567
 * Audio support
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   568
 */
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   569
static jboolean audioBuffer16Bit = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   570
static jobject audioBuffer = NULL;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   571
static void* audioBufferPinned = NULL;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   572
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   573
int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   574
{
9322
a45110bca65e Changed static variable to local variable in implementation for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9314
diff changeset
   575
    jboolean audioBufferStereo;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   576
    int audioBufferFrames;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   577
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   578
    JNIEnv *env = Android_JNI_GetEnv();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   579
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   580
    if (!env) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   581
        LOGE("callback_handler: failed to attach current thread");
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   582
    }
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   583
    Android_JNI_SetupThread();
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   584
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   585
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   586
    audioBuffer16Bit = is16Bit;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   587
    audioBufferStereo = channelCount > 1;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   588
7552
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   589
    if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   590
        /* Error during audio initialization */
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   591
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   592
        return 0;
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   593
    }
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   594
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   595
    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   596
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   597
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   598
    if (is16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   599
        jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   600
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   601
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   602
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   603
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   604
    }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   605
    else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   606
        jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   607
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   608
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   609
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   610
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   611
    }
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   612
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   613
    if (audioBuffer == NULL) {
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   614
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   615
        return 0;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   616
    }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   617
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   618
    jboolean isCopy = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   619
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   620
        audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   621
        audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   622
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   623
        audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   624
        audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   625
    }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   626
    if (audioBufferStereo) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   627
        audioBufferFrames /= 2;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   628
    }
6864
97187387ad79 Bug 1703 - SDL calls a wrong JNI function on Android by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6828
diff changeset
   629
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   630
    return audioBufferFrames;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   631
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   632
9594
0285b5e1c5ab Android: Fixed empty parameter list in signatures of internal functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9450
diff changeset
   633
void * Android_JNI_GetAudioBuffer(void)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   634
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   635
    return audioBufferPinned;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   636
}
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   637
9594
0285b5e1c5ab Android: Fixed empty parameter list in signatures of internal functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9450
diff changeset
   638
void Android_JNI_WriteAudioBuffer(void)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   639
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   640
    JNIEnv *mAudioEnv = Android_JNI_GetEnv();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   641
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   642
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   643
        (*mAudioEnv)->ReleaseShortArrayElements(mAudioEnv, (jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   644
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   645
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   646
        (*mAudioEnv)->ReleaseByteArrayElements(mAudioEnv, (jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   647
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   648
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   649
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   650
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   651
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   652
9594
0285b5e1c5ab Android: Fixed empty parameter list in signatures of internal functions.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9450
diff changeset
   653
void Android_JNI_CloseAudioDevice(void)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   654
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   655
    JNIEnv *env = Android_JNI_GetEnv();
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   656
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   657
    (*env)->CallStaticVoidMethod(env, mActivityClass, midAudioQuit);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   658
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   659
    if (audioBuffer) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   660
        (*env)->DeleteGlobalRef(env, audioBuffer);
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   661
        audioBuffer = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   662
        audioBufferPinned = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   663
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   664
}
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
   665
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   666
/* Test for an exception and call SDL_SetError with its detail if one occurs */
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   667
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   668
static SDL_bool Android_JNI_ExceptionOccurred(SDL_bool silent)
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   669
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   670
    SDL_assert(LocalReferenceHolder_IsActive());
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   671
    JNIEnv *mEnv = Android_JNI_GetEnv();
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   672
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   673
    jthrowable exception = (*mEnv)->ExceptionOccurred(mEnv);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   674
    if (exception != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   675
        jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   676
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   677
        /* Until this happens most JNI operations have undefined behaviour */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   678
        (*mEnv)->ExceptionClear(mEnv);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   679
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   680
        if (!silent) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   681
            jclass exceptionClass = (*mEnv)->GetObjectClass(mEnv, exception);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   682
            jclass classClass = (*mEnv)->FindClass(mEnv, "java/lang/Class");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   683
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   684
            mid = (*mEnv)->GetMethodID(mEnv, classClass, "getName", "()Ljava/lang/String;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   685
            jstring exceptionName = (jstring)(*mEnv)->CallObjectMethod(mEnv, exceptionClass, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   686
            const char* exceptionNameUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionName, 0);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   687
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   688
            mid = (*mEnv)->GetMethodID(mEnv, exceptionClass, "getMessage", "()Ljava/lang/String;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   689
            jstring exceptionMessage = (jstring)(*mEnv)->CallObjectMethod(mEnv, exception, mid);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   690
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   691
            if (exceptionMessage != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   692
                const char* exceptionMessageUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionMessage, 0);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   693
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   694
                (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionMessage, exceptionMessageUTF8);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   695
            } else {
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   696
                SDL_SetError("%s", exceptionNameUTF8);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   697
            }
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   698
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   699
            (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionName, exceptionNameUTF8);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   700
        }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   701
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   702
        return SDL_TRUE;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   703
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   704
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   705
    return SDL_FALSE;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   706
}
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   707
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   708
static int Internal_Android_JNI_FileOpen(SDL_RWops* ctx)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   709
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   710
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   711
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   712
    int result = 0;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   713
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   714
    jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   715
    jobject context;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   716
    jobject assetManager;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   717
    jobject inputStream;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   718
    jclass channels;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   719
    jobject readableByteChannel;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   720
    jstring fileNameJString;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   721
    jobject fd;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   722
    jclass fdCls;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   723
    jfieldID descriptor;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   724
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   725
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   726
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   727
        goto failure;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   728
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   729
6308
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   730
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   731
    ctx->hidden.androidio.position = 0;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   732
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   733
    /* context = SDLActivity.getContext(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   734
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   735
            "getContext","()Landroid/content/Context;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   736
    context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   737
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   738
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   739
    /* assetManager = context.getAssets(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   740
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   741
            "getAssets", "()Landroid/content/res/AssetManager;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   742
    assetManager = (*mEnv)->CallObjectMethod(mEnv, context, mid);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   743
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   744
    /* First let's try opening the file to obtain an AssetFileDescriptor.
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   745
    * This method reads the files directly from the APKs using standard *nix calls
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   746
    */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   747
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   748
    inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   749
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   750
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   751
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   752
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   753
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getStartOffset", "()J");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   754
    ctx->hidden.androidio.offset = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   755
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   756
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   757
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   758
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   759
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getDeclaredLength", "()J");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   760
    ctx->hidden.androidio.size = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   761
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   762
        goto fallback;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   763
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   764
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   765
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   766
    fd = (*mEnv)->CallObjectMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   767
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   768
    descriptor = (*mEnv)->GetFieldID(mEnv, fdCls, "descriptor", "I");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   769
    ctx->hidden.androidio.fd = (*mEnv)->GetIntField(mEnv, fd, descriptor);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   770
    ctx->hidden.androidio.assetFileDescriptorRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   771
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   772
    /* Seek to the correct offset in the file. */
6816
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   773
    lseek(ctx->hidden.androidio.fd, (off_t)ctx->hidden.androidio.offset, SEEK_SET);
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   774
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   775
    if (0) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   776
fallback:
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   777
        /* Disabled log message because of spam on the Nexus 7 */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   778
        /* __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file"); */
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   779
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   780
        /* Try the old method using InputStream */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   781
        ctx->hidden.androidio.assetFileDescriptorRef = NULL;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   782
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   783
        /* inputStream = assetManager.open(<filename>); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   784
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   785
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   786
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   787
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
9869
4ba43d626c4a Android: Renamed SDLActivity's Java method used for APK expansion files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9858
diff changeset
   788
            /* Try fallback to APK expansion files */
8896
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   789
            mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
9869
4ba43d626c4a Android: Renamed SDLActivity's Java method used for APK expansion files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9858
diff changeset
   790
                "openAPKExpansionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
8896
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   791
            inputStream = (*mEnv)->CallObjectMethod(mEnv, context, mid, fileNameJString);
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   792
9870
6dd5ab47534b Android: Fixed trying to read from APK expansion files without version hint set.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9869
diff changeset
   793
            /* Exception is checked first because it always needs to be cleared.
6dd5ab47534b Android: Fixed trying to read from APK expansion files without version hint set.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9869
diff changeset
   794
             * If no exception occurred then the last SDL error message is kept.
6dd5ab47534b Android: Fixed trying to read from APK expansion files without version hint set.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9869
diff changeset
   795
             */
6dd5ab47534b Android: Fixed trying to read from APK expansion files without version hint set.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9869
diff changeset
   796
            if (Android_JNI_ExceptionOccurred(SDL_FALSE) || !inputStream) {
8896
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   797
                goto failure;
d931167c375a inital apk extension support
stopiccot <alexey.petruchik@gmail.com>
parents: 8149
diff changeset
   798
            }
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   799
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   800
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   801
        ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   802
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   803
        /* Despite all the visible documentation on [Asset]InputStream claiming
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   804
         * that the .available() method is not guaranteed to return the entire file
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   805
         * size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   806
         * android/apis/content/ReadAsset.java imply that Android's
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   807
         * AssetInputStream.available() /will/ always return the total file size
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   808
        */
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   809
        
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   810
        /* size = inputStream.available(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   811
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   812
                "available", "()I");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   813
        ctx->hidden.androidio.size = (long)(*mEnv)->CallIntMethod(mEnv, inputStream, mid);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   814
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   815
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   816
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   817
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   818
        /* readableByteChannel = Channels.newChannel(inputStream); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   819
        channels = (*mEnv)->FindClass(mEnv, "java/nio/channels/Channels");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   820
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   821
                "newChannel",
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   822
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   823
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   824
                mEnv, channels, mid, inputStream);
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   825
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   826
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   827
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   828
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   829
        ctx->hidden.androidio.readableByteChannelRef =
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   830
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   831
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   832
        /* Store .read id for reading purposes */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   833
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, readableByteChannel),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   834
                "read", "(Ljava/nio/ByteBuffer;)I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   835
        ctx->hidden.androidio.readMethod = mid;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   836
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   837
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   838
    if (0) {
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   839
failure:
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   840
        result = -1;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   841
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   842
        (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   843
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   844
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   845
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   846
        }
6308
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   847
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   848
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   849
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
6308
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   850
        }
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   851
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   852
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   853
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   854
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   855
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   856
    }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   857
    
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   858
    LocalReferenceHolder_Cleanup(&refs);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   859
    return result;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   860
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   861
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   862
int Android_JNI_FileOpen(SDL_RWops* ctx,
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   863
        const char* fileName, const char* mode)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   864
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   865
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   866
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   867
    int retval;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   868
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   869
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   870
        LocalReferenceHolder_Cleanup(&refs);        
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   871
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   872
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   873
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   874
    if (!ctx) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   875
        LocalReferenceHolder_Cleanup(&refs);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   876
        return -1;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   877
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   878
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   879
    jstring fileNameJString = (*mEnv)->NewStringUTF(mEnv, fileName);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   880
    ctx->hidden.androidio.fileNameRef = (*mEnv)->NewGlobalRef(mEnv, fileNameJString);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   881
    ctx->hidden.androidio.inputStreamRef = NULL;
6335
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   882
    ctx->hidden.androidio.readableByteChannelRef = NULL;
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   883
    ctx->hidden.androidio.readMethod = NULL;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   884
    ctx->hidden.androidio.assetFileDescriptorRef = NULL;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   885
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   886
    retval = Internal_Android_JNI_FileOpen(ctx);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   887
    LocalReferenceHolder_Cleanup(&refs);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   888
    return retval;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   889
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   890
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   891
size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   892
        size_t size, size_t maxnum)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   893
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   894
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
6377
3d868ca4782f Fixes Android_JNI_FileRead behaviour where reading past the end of a file returns zero instead of the number of bytes read.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6354
diff changeset
   895
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   896
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   897
        size_t bytesMax = size * maxnum;
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   898
        if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   899
            bytesMax = ctx->hidden.androidio.size - ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   900
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   901
        size_t result = read(ctx->hidden.androidio.fd, buffer, bytesMax );
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   902
        if (result > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   903
            ctx->hidden.androidio.position += result;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   904
            LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   905
            return result / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   906
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   907
        LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   908
        return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   909
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   910
        jlong bytesRemaining = (jlong) (size * maxnum);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   911
        jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   912
        int bytesRead = 0;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   913
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   914
        /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   915
        if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   916
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   917
        JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   918
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   919
            LocalReferenceHolder_Cleanup(&refs);            
7368
0e12e09df086 Fixed SDL_RWread() returning -1 as unsigned instead of 0 if error on Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7246
diff changeset
   920
            return 0;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   921
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   922
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   923
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   924
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   925
        jobject byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   926
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   927
        while (bytesRemaining > 0) {
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   928
            /* result = readableByteChannel.read(...); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   929
            int result = (*mEnv)->CallIntMethod(mEnv, readableByteChannel, readMethod, byteBuffer);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   930
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   931
            if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   932
                LocalReferenceHolder_Cleanup(&refs);            
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   933
                return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   934
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   935
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   936
            if (result < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   937
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   938
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   939
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   940
            bytesRemaining -= result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   941
            bytesRead += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   942
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   943
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   944
        LocalReferenceHolder_Cleanup(&refs);                    
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   945
        return bytesRead / size;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   946
    }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   947
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   948
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   949
size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   950
        size_t size, size_t num)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   951
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   952
    SDL_SetError("Cannot write to Android package filesystem");
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   953
    return 0;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   954
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   955
9271
8f71cb5dc099 Removed dependency to stdbool.h in implementation file for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9268
diff changeset
   956
static int Internal_Android_JNI_FileClose(SDL_RWops* ctx, SDL_bool release)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   957
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   958
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   959
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   960
    int result = 0;
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   961
    JNIEnv *mEnv = Android_JNI_GetEnv();
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   962
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   963
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   964
        LocalReferenceHolder_Cleanup(&refs);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   965
        return SDL_SetError("Failed to allocate enough JVM local references");
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   966
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   967
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   968
    if (ctx) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   969
        if (release) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   970
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   971
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   972
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   973
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   974
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   975
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),