src/core/android/SDL_android.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 29 Nov 2013 10:06:08 -0300
changeset 8039 3b0346b37e0f
parent 8029 cf81e6709b3d
child 8047 a5270cef21a7
permissions -rw-r--r--
Improve Android pause/resume behavior.
slouken@4964
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@4964
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@4964
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@4964
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@4964
    20
*/
slouken@4964
    21
#include "SDL_config.h"
slouken@5222
    22
#include "SDL_stdinc.h"
slouken@6284
    23
#include "SDL_assert.h"
slouken@6650
    24
#include "SDL_log.h"
slouken@4964
    25
slouken@6044
    26
#ifdef __ANDROID__
slouken@6044
    27
slouken@6630
    28
#include "SDL_system.h"
slouken@4989
    29
#include "SDL_android.h"
slouken@6792
    30
#include <EGL/egl.h>
slouken@4989
    31
slouken@5092
    32
#include "../../events/SDL_events_c.h"
slouken@5092
    33
#include "../../video/android/SDL_androidkeyboard.h"
slouken@5092
    34
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    35
#include "../../video/android/SDL_androidvideo.h"
gabomdq@7659
    36
#include "../../video/android/SDL_androidwindow.h"
philipp@7927
    37
#include "../../joystick/android/SDL_sysjoystick.h"
slouken@4995
    38
icculus@5996
    39
#include <android/log.h>
gabomdq@6354
    40
#include <pthread.h>
gabomdq@6806
    41
#include <sys/types.h>
gabomdq@6806
    42
#include <unistd.h>
icculus@5996
    43
#define LOG_TAG "SDL_android"
gabomdq@7678
    44
/* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
gabomdq@7678
    45
/* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
icculus@5996
    46
#define LOGI(...) do {} while (false)
icculus@5996
    47
#define LOGE(...) do {} while (false)
icculus@5996
    48
slouken@6650
    49
/* Uncomment this to log messages entering and exiting methods in this file */
gabomdq@7678
    50
/* #define DEBUG_JNI */
icculus@5996
    51
philipp@7246
    52
static void Android_JNI_ThreadDestroyed(void*);
slouken@4980
    53
slouken@4964
    54
/*******************************************************************************
slouken@4964
    55
 This file links the Java side of Android with libsdl
slouken@4964
    56
*******************************************************************************/
slouken@4964
    57
#include <jni.h>
slouken@4964
    58
#include <android/log.h>
ewing@7501
    59
#include <stdbool.h>
slouken@4964
    60
slouken@4964
    61
slouken@4964
    62
/*******************************************************************************
slouken@4964
    63
                               Globals
slouken@4964
    64
*******************************************************************************/
gabomdq@6354
    65
static pthread_key_t mThreadKey;
icculus@5996
    66
static JavaVM* mJavaVM;
slouken@4964
    67
gabomdq@7663
    68
/* Main activity */
slouken@4998
    69
static jclass mActivityClass;
slouken@4964
    70
gabomdq@7663
    71
/* method signatures */
gabomdq@7659
    72
static jmethodID midGetNativeSurface;
slouken@4995
    73
static jmethodID midFlipBuffers;
slouken@4995
    74
static jmethodID midAudioInit;
slouken@4995
    75
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    76
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    77
static jmethodID midAudioQuit;
slouken@4964
    78
gabomdq@7663
    79
/* Accelerometer data storage */
slouken@5000
    80
static float fLastAccelerometer[3];
slouken@6212
    81
static bool bHasNewData;
slouken@4964
    82
slouken@4964
    83
/*******************************************************************************
slouken@4964
    84
                 Functions called by JNI
slouken@4964
    85
*******************************************************************************/
slouken@4964
    86
gabomdq@7663
    87
/* Library init */
ewing@7501
    88
jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    89
{
icculus@5996
    90
    JNIEnv *env;
icculus@5996
    91
    mJavaVM = vm;
icculus@5996
    92
    LOGI("JNI_OnLoad called");
ewing@7501
    93
    if ((*mJavaVM)->GetEnv(mJavaVM, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
icculus@5996
    94
        LOGE("Failed to get the environment using GetEnv()");
icculus@5996
    95
        return -1;
icculus@5996
    96
    }
gabomdq@6354
    97
    /*
gabomdq@6354
    98
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
gabomdq@6354
    99
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
gabomdq@6354
   100
     */
gabomdq@6354
   101
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed)) {
gabomdq@6354
   102
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
gabomdq@6354
   103
    }
gabomdq@6354
   104
    else {
gabomdq@6354
   105
        Android_JNI_SetupThread();
gabomdq@6354
   106
    }
icculus@5996
   107
slouken@4964
   108
    return JNI_VERSION_1_4;
slouken@4964
   109
}
slouken@4964
   110
gabomdq@7663
   111
/* Called before SDL_main() to initialize JNI bindings */
ewing@7501
   112
void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
slouken@4964
   113
{
slouken@4964
   114
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
   115
gabomdq@6354
   116
    Android_JNI_SetupThread();
gabomdq@6354
   117
ewing@7501
   118
    mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
slouken@4998
   119
gabomdq@7659
   120
    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@7659
   121
                                "getNativeSurface","()Landroid/view/Surface;");
ewing@7501
   122
    midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   123
                                "flipBuffers","()V");
ewing@7501
   124
    midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@7552
   125
                                "audioInit", "(IZZI)I");
ewing@7501
   126
    midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   127
                                "audioWriteShortBuffer", "([S)V");
ewing@7501
   128
    midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   129
                                "audioWriteByteBuffer", "([B)V");
ewing@7501
   130
    midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   131
                                "audioQuit", "()V");
slouken@4964
   132
slouken@6212
   133
    bHasNewData = false;
slouken@6212
   134
gabomdq@7659
   135
    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
slouken@4996
   136
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
slouken@4996
   137
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
   138
    }
icculus@5996
   139
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
slouken@4964
   140
}
slouken@4964
   141
gabomdq@7663
   142
/* Resize */
ewing@7501
   143
void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   144
                                    JNIEnv* env, jclass jcls,
slouken@4995
   145
                                    jint width, jint height, jint format)
slouken@4995
   146
{
slouken@4995
   147
    Android_SetScreenResolution(width, height, format);
slouken@4995
   148
}
slouken@4995
   149
gabomdq@7907
   150
// Paddown
gabomdq@8013
   151
int Java_org_libsdl_app_SDLActivity_onNativePadDown(
gabomdq@7907
   152
                                    JNIEnv* env, jclass jcls,
gabomdq@7907
   153
                                    jint padId, jint keycode)
gabomdq@7907
   154
{
gabomdq@8013
   155
    return Android_OnPadDown(padId, keycode);
gabomdq@7907
   156
}
gabomdq@7907
   157
gabomdq@7907
   158
// Padup
gabomdq@8013
   159
int Java_org_libsdl_app_SDLActivity_onNativePadUp(
gabomdq@7907
   160
                                   JNIEnv* env, jclass jcls,
gabomdq@7907
   161
                                   jint padId, jint keycode)
gabomdq@7907
   162
{
gabomdq@8013
   163
    return Android_OnPadUp(padId, keycode);
gabomdq@7907
   164
}
gabomdq@7907
   165
philipp@7926
   166
/* Joy */
gabomdq@7907
   167
void Java_org_libsdl_app_SDLActivity_onNativeJoy(
gabomdq@7907
   168
                                    JNIEnv* env, jclass jcls,
gabomdq@7907
   169
                                    jint joyId, jint axis, jfloat value)
gabomdq@7907
   170
{
gabomdq@7907
   171
    Android_OnJoy(joyId, axis, value);
gabomdq@7907
   172
}
gabomdq@7907
   173
gabomdq@7659
   174
gabomdq@7663
   175
/* Surface Created */
gabomdq@7659
   176
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
gabomdq@7659
   177
{
gabomdq@7659
   178
    SDL_WindowData *data;
gabomdq@7659
   179
    SDL_VideoDevice *_this;
gabomdq@7659
   180
gabomdq@7659
   181
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   182
        return;
gabomdq@7659
   183
    }
gabomdq@7659
   184
    
gabomdq@7659
   185
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   186
    data =  (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   187
    
gabomdq@7659
   188
    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
gabomdq@7659
   189
    if (data->egl_surface == EGL_NO_SURFACE) {
gabomdq@7659
   190
        if(data->native_window) {
gabomdq@7659
   191
            ANativeWindow_release(data->native_window);
gabomdq@7659
   192
        }
gabomdq@7659
   193
        data->native_window = Android_JNI_GetNativeWindow();
gabomdq@7659
   194
        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
gabomdq@7659
   195
    }
gabomdq@7659
   196
    
gabomdq@7659
   197
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   198
    
gabomdq@7659
   199
}
gabomdq@7659
   200
gabomdq@7663
   201
/* Surface Destroyed */
gabomdq@7659
   202
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
gabomdq@7659
   203
{
gabomdq@7659
   204
    /* We have to clear the current context and destroy the egl surface here
gabomdq@7659
   205
     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
gabomdq@7659
   206
     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
gabomdq@7659
   207
     */
gabomdq@7659
   208
    SDL_WindowData *data;
gabomdq@7659
   209
    SDL_VideoDevice *_this;
gabomdq@7659
   210
    
gabomdq@7659
   211
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   212
        return;
gabomdq@7659
   213
    }
gabomdq@7659
   214
    
gabomdq@7659
   215
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   216
    data = (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   217
    
gabomdq@7659
   218
    if (data->egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   219
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   220
        SDL_EGL_DestroySurface(_this, data->egl_surface);
gabomdq@7659
   221
        data->egl_surface = EGL_NO_SURFACE;
gabomdq@7659
   222
    }
gabomdq@7659
   223
    
gabomdq@7659
   224
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   225
gabomdq@7659
   226
}
gabomdq@7659
   227
gabomdq@7659
   228
void Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
gabomdq@7659
   229
{
gabomdq@7659
   230
    SDL_GL_SwapWindow(Android_Window);
gabomdq@7659
   231
}
gabomdq@7659
   232
gabomdq@7663
   233
/* Keydown */
ewing@7501
   234
void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   235
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   236
{
slouken@4980
   237
    Android_OnKeyDown(keycode);
slouken@4964
   238
}
slouken@4964
   239
gabomdq@7663
   240
/* Keyup */
ewing@7501
   241
void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   242
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   243
{
slouken@4980
   244
    Android_OnKeyUp(keycode);
slouken@4964
   245
}
slouken@4964
   246
gabomdq@7663
   247
/* Keyboard Focus Lost */
gabomdq@7564
   248
void Java_org_libsdl_app_SDLActivity_onNativeKeyboardFocusLost(
gabomdq@7564
   249
                                    JNIEnv* env, jclass jcls)
gabomdq@7564
   250
{
gabomdq@7564
   251
    /* Calling SDL_StopTextInput will take care of hiding the keyboard and cleaning up the DummyText widget */
gabomdq@7564
   252
    SDL_StopTextInput();
gabomdq@7564
   253
}
gabomdq@7564
   254
gabomdq@7564
   255
gabomdq@7663
   256
/* Touch */
ewing@7501
   257
void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   258
                                    JNIEnv* env, jclass jcls,
icculus@5982
   259
                                    jint touch_device_id_in, jint pointer_finger_id_in,
slouken@4995
   260
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   261
{
icculus@5982
   262
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
slouken@4964
   263
}
slouken@4964
   264
gabomdq@7663
   265
/* Accelerometer */
ewing@7501
   266
void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   267
                                    JNIEnv* env, jclass jcls,
slouken@4995
   268
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   269
{
slouken@4964
   270
    fLastAccelerometer[0] = x;
slouken@4964
   271
    fLastAccelerometer[1] = y;
slouken@6212
   272
    fLastAccelerometer[2] = z;
slouken@6212
   273
    bHasNewData = true;
slouken@4964
   274
}
slouken@4964
   275
gabomdq@7663
   276
/* Low memory */
ewing@7501
   277
void Java_org_libsdl_app_SDLActivity_nativeLowMemory(
slouken@7190
   278
                                    JNIEnv* env, jclass cls)
slouken@7191
   279
{
slouken@7190
   280
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
slouken@7190
   281
}
slouken@7190
   282
gabomdq@7663
   283
/* Quit */
ewing@7501
   284
void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   285
                                    JNIEnv* env, jclass cls)
slouken@7191
   286
{
gabomdq@7910
   287
    /* Discard previous events. The user should have handled state storage
gabomdq@7910
   288
     * in SDL_APP_WILLENTERBACKGROUND. After nativeQuit() is called, no
gabomdq@7910
   289
     * events other than SDL_QUIT and SDL_APP_TERMINATING should fire */
gabomdq@7910
   290
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
gabomdq@7663
   291
    /* Inject a SDL_QUIT event */
slouken@4995
   292
    SDL_SendQuit();
slouken@7190
   293
    SDL_SendAppEvent(SDL_APP_TERMINATING);
gabomdq@7910
   294
    /* Resume the event loop so that the app can catch SDL_QUIT which
gabomdq@7910
   295
     * should now be the top event in the event queue. */
gabomdq@7910
   296
    if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
slouken@4995
   297
}
slouken@4995
   298
gabomdq@7663
   299
/* Pause */
ewing@7501
   300
void Java_org_libsdl_app_SDLActivity_nativePause(
slouken@6186
   301
                                    JNIEnv* env, jclass cls)
slouken@6186
   302
{
gabomdq@8039
   303
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
slouken@6186
   304
    if (Android_Window) {
slouken@6186
   305
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@6191
   306
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
gabomdq@8039
   307
        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
gabomdq@8039
   308
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
gabomdq@8039
   309
    
gabomdq@8039
   310
        /* *After* sending the relevant events, signal the pause semaphore 
gabomdq@8039
   311
         * so the event loop knows to pause and (optionally) block itself */
gabomdq@8039
   312
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
slouken@6186
   313
    }
slouken@6186
   314
}
slouken@6186
   315
gabomdq@7663
   316
/* Resume */
ewing@7501
   317
void Java_org_libsdl_app_SDLActivity_nativeResume(
slouken@6186
   318
                                    JNIEnv* env, jclass cls)
slouken@6186
   319
{
slouken@7190
   320
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
slouken@7190
   321
slouken@6186
   322
    if (Android_Window) {
gabomdq@6330
   323
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
gabomdq@6330
   324
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
gabomdq@6330
   325
         * and this function will be called from the Java thread instead.
gabomdq@6330
   326
         */
gabomdq@6330
   327
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
gabomdq@8039
   328
        
gabomdq@8039
   329
        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
gabomdq@8039
   330
        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
slouken@6186
   331
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@6191
   332
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@6186
   333
    }
slouken@6186
   334
}
slouken@6186
   335
ewing@7501
   336
void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
slouken@6555
   337
                                    JNIEnv* env, jclass cls,
slouken@6555
   338
                                    jstring text, jint newCursorPosition)
slouken@6555
   339
{
ewing@7501
   340
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   341
slouken@6555
   342
    SDL_SendKeyboardText(utftext);
slouken@6555
   343
ewing@7501
   344
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   345
}
slouken@6555
   346
ewing@7501
   347
void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
slouken@6555
   348
                                    JNIEnv* env, jclass cls,
slouken@6555
   349
                                    jstring text, jint newCursorPosition)
slouken@6555
   350
{
ewing@7501
   351
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   352
slouken@6555
   353
    SDL_SendEditingText(utftext, 0, 0);
slouken@6555
   354
ewing@7501
   355
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   356
}
slouken@6555
   357
slouken@6555
   358
slouken@6555
   359
slouken@4964
   360
/*******************************************************************************
slouken@4964
   361
             Functions called by SDL into Java
slouken@4964
   362
*******************************************************************************/
slouken@6284
   363
ewing@7501
   364
static int s_active = 0;
ewing@7501
   365
struct LocalReferenceHolder
slouken@6284
   366
{
slouken@6284
   367
    JNIEnv *m_env;
slouken@6650
   368
    const char *m_func;
slouken@6284
   369
};
slouken@6284
   370
ewing@7501
   371
static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func)
ewing@7501
   372
{
ewing@7501
   373
    struct LocalReferenceHolder refholder;
ewing@7501
   374
    refholder.m_env = NULL;
ewing@7501
   375
    refholder.m_func = func;
ewing@7501
   376
#ifdef DEBUG_JNI
ewing@7501
   377
    SDL_Log("Entering function %s", func);
ewing@7501
   378
#endif
ewing@7501
   379
    return refholder;
ewing@7501
   380
}
ewing@7501
   381
ewing@7501
   382
static SDL_bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env)
ewing@7501
   383
{
ewing@7501
   384
    const int capacity = 16;
ewing@7501
   385
    if ((*env)->PushLocalFrame(env, capacity) < 0) {
ewing@7501
   386
        SDL_SetError("Failed to allocate enough JVM local references");
philipp@7516
   387
        return SDL_FALSE;
ewing@7501
   388
    }
ewing@7501
   389
    ++s_active;
ewing@7501
   390
    refholder->m_env = env;
ewing@7501
   391
    return SDL_TRUE;
ewing@7501
   392
}
ewing@7501
   393
ewing@7501
   394
static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder)
ewing@7501
   395
{
ewing@7501
   396
#ifdef DEBUG_JNI
ewing@7501
   397
    SDL_Log("Leaving function %s", refholder->m_func);
ewing@7501
   398
#endif
ewing@7501
   399
    if (refholder->m_env) {
ewing@7501
   400
        JNIEnv* env = refholder->m_env;
ewing@7501
   401
        (*env)->PopLocalFrame(env, NULL);
ewing@7501
   402
        --s_active;
ewing@7501
   403
    }
ewing@7501
   404
}
ewing@7501
   405
ewing@7501
   406
static SDL_bool LocalReferenceHolder_IsActive()
ewing@7501
   407
{
ewing@7501
   408
    return s_active > 0;    
ewing@7501
   409
}
ewing@7501
   410
gabomdq@7659
   411
ANativeWindow* Android_JNI_GetNativeWindow(void)
slouken@4964
   412
{
gabomdq@7659
   413
    ANativeWindow* anw;
gabomdq@7659
   414
    jobject s;
slouken@6792
   415
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6792
   416
gabomdq@7659
   417
    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
gabomdq@7659
   418
    anw = ANativeWindow_fromSurface(env, s);
gabomdq@7659
   419
    (*env)->DeleteLocalRef(env, s);
gabomdq@7659
   420
  
gabomdq@7659
   421
    return anw;
gabomdq@7567
   422
}
gabomdq@7567
   423
ewing@7501
   424
void Android_JNI_SwapWindow()
slouken@4964
   425
{
gabomdq@6354
   426
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   427
    (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midFlipBuffers);
slouken@4998
   428
}
slouken@4998
   429
ewing@7501
   430
void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   431
{
slouken@4998
   432
    jmethodID mid;
gabomdq@6354
   433
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   434
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
slouken@4998
   435
    if (mid) {
ewing@7501
   436
        jstring jtitle = (jstring)((*mEnv)->NewStringUTF(mEnv, title));
ewing@7501
   437
        (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, mid, jtitle);
ewing@7501
   438
        (*mEnv)->DeleteLocalRef(mEnv, jtitle);
slouken@4998
   439
    }
slouken@4964
   440
}
slouken@4964
   441
ewing@7501
   442
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   443
{
slouken@5000
   444
    int i;
slouken@6212
   445
    SDL_bool retval = SDL_FALSE;
slouken@6212
   446
slouken@6212
   447
    if (bHasNewData) {
slouken@6212
   448
        for (i = 0; i < 3; ++i) {
slouken@6212
   449
            values[i] = fLastAccelerometer[i];
slouken@6212
   450
        }
slouken@6212
   451
        bHasNewData = false;
slouken@6212
   452
        retval = SDL_TRUE;
slouken@5000
   453
    }
slouken@6212
   454
slouken@6212
   455
    return retval;
slouken@5000
   456
}
slouken@5000
   457
gabomdq@6354
   458
static void Android_JNI_ThreadDestroyed(void* value) {
gabomdq@6354
   459
    /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
gabomdq@6354
   460
    JNIEnv *env = (JNIEnv*) value;
gabomdq@6354
   461
    if (env != NULL) {
ewing@7501
   462
        (*mJavaVM)->DetachCurrentThread(mJavaVM);
gabomdq@6354
   463
        pthread_setspecific(mThreadKey, NULL);
gabomdq@6354
   464
    }
gabomdq@6354
   465
}
gabomdq@6354
   466
gabomdq@6354
   467
JNIEnv* Android_JNI_GetEnv(void) {
gabomdq@6354
   468
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   469
     * All threads are Linux threads, scheduled by the kernel.
gabomdq@6354
   470
     * They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
gabomdq@6354
   471
     * attached to the JavaVM. For example, a thread started with pthread_create can be attached with the
gabomdq@6354
   472
     * JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv,
gabomdq@6354
   473
     * and cannot make JNI calls.
gabomdq@6354
   474
     * Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main"
gabomdq@6354
   475
     * ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread
gabomdq@6354
   476
     * is a no-op.
gabomdq@6354
   477
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   478
     */
gabomdq@6354
   479
gabomdq@6354
   480
    JNIEnv *env;
ewing@7501
   481
    int status = (*mJavaVM)->AttachCurrentThread(mJavaVM, &env, NULL);
gabomdq@6354
   482
    if(status < 0) {
gabomdq@6354
   483
        LOGE("failed to attach current thread");
gabomdq@6354
   484
        return 0;
gabomdq@6354
   485
    }
gabomdq@6354
   486
gabomdq@6354
   487
    return env;
gabomdq@6354
   488
}
gabomdq@6354
   489
gabomdq@6354
   490
int Android_JNI_SetupThread(void) {
gabomdq@6354
   491
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   492
     * Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
gabomdq@6354
   493
     * in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
gabomdq@6354
   494
     * called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific
gabomdq@6354
   495
     * to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
gabomdq@6354
   496
     * Note: The destructor is not called unless the stored value is != NULL
gabomdq@6354
   497
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   498
     *       (except for some lost CPU cycles)
gabomdq@6354
   499
     */
gabomdq@6354
   500
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@6354
   501
    pthread_setspecific(mThreadKey, (void*) env);
gabomdq@6354
   502
    return 1;
gabomdq@6354
   503
}
gabomdq@6354
   504
gabomdq@7663
   505
/*
gabomdq@7663
   506
 * Audio support
gabomdq@7663
   507
 */
slouken@4996
   508
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   509
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   510
static jobject audioBuffer = NULL;
slouken@4996
   511
static void* audioBufferPinned = NULL;
slouken@4995
   512
ewing@7501
   513
int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   514
{
slouken@4996
   515
    int audioBufferFrames;
slouken@4964
   516
gabomdq@6354
   517
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@6354
   518
gabomdq@6354
   519
    if (!env) {
gabomdq@6354
   520
        LOGE("callback_handler: failed to attach current thread");
icculus@5996
   521
    }
gabomdq@6354
   522
    Android_JNI_SetupThread();
icculus@5996
   523
slouken@4996
   524
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   525
    audioBuffer16Bit = is16Bit;
slouken@4996
   526
    audioBufferStereo = channelCount > 1;
slouken@4964
   527
gabomdq@7552
   528
    if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
gabomdq@7552
   529
        /* Error during audio initialization */
gabomdq@7552
   530
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
gabomdq@7552
   531
        return 0;
gabomdq@7552
   532
    }
gabomdq@6802
   533
gabomdq@6802
   534
    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
gabomdq@6802
   535
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
slouken@7191
   536
gabomdq@6802
   537
    if (is16Bit) {
ewing@7501
   538
        jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   539
        if (audioBufferLocal) {
ewing@7501
   540
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   541
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   542
        }
gabomdq@6802
   543
    }
gabomdq@6802
   544
    else {
ewing@7501
   545
        jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   546
        if (audioBufferLocal) {
ewing@7501
   547
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   548
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   549
        }
gabomdq@6802
   550
    }
slouken@4995
   551
slouken@4996
   552
    if (audioBuffer == NULL) {
gabomdq@6802
   553
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
slouken@4996
   554
        return 0;
slouken@4996
   555
    }
slouken@4995
   556
slouken@4996
   557
    jboolean isCopy = JNI_FALSE;
slouken@4996
   558
    if (audioBuffer16Bit) {
ewing@7501
   559
        audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
ewing@7501
   560
        audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
slouken@4996
   561
    } else {
ewing@7501
   562
        audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
ewing@7501
   563
        audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
slouken@4996
   564
    }
slouken@4996
   565
    if (audioBufferStereo) {
slouken@4996
   566
        audioBufferFrames /= 2;
slouken@4996
   567
    }
gabomdq@6864
   568
slouken@4996
   569
    return audioBufferFrames;
slouken@4995
   570
}
slouken@4995
   571
ewing@7501
   572
void * Android_JNI_GetAudioBuffer()
slouken@4995
   573
{
slouken@4996
   574
    return audioBufferPinned;
slouken@4995
   575
}
slouken@4995
   576
ewing@7501
   577
void Android_JNI_WriteAudioBuffer()
slouken@4995
   578
{
gabomdq@6354
   579
    JNIEnv *mAudioEnv = Android_JNI_GetEnv();
gabomdq@6354
   580
slouken@4996
   581
    if (audioBuffer16Bit) {
ewing@7501
   582
        (*mAudioEnv)->ReleaseShortArrayElements(mAudioEnv, (jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   583
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   584
    } else {
ewing@7501
   585
        (*mAudioEnv)->ReleaseByteArrayElements(mAudioEnv, (jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   586
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   587
    }
slouken@4995
   588
slouken@4996
   589
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   590
}
slouken@4995
   591
ewing@7501
   592
void Android_JNI_CloseAudioDevice()
slouken@4995
   593
{
gabomdq@6354
   594
    JNIEnv *env = Android_JNI_GetEnv();
icculus@5996
   595
ewing@7501
   596
    (*env)->CallStaticVoidMethod(env, mActivityClass, midAudioQuit);
slouken@4964
   597
slouken@4997
   598
    if (audioBuffer) {
ewing@7501
   599
        (*env)->DeleteGlobalRef(env, audioBuffer);
slouken@4997
   600
        audioBuffer = NULL;
slouken@4997
   601
        audioBufferPinned = NULL;
slouken@4997
   602
    }
slouken@4964
   603
}
slouken@4981
   604
gabomdq@7663
   605
/* Test for an exception and call SDL_SetError with its detail if one occurs */
gabomdq@7663
   606
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
ewing@7501
   607
static bool Android_JNI_ExceptionOccurred(bool silent)
tim@5650
   608
{
ewing@7501
   609
    SDL_assert(LocalReferenceHolder_IsActive());
gabomdq@6354
   610
    JNIEnv *mEnv = Android_JNI_GetEnv();
slouken@6284
   611
ewing@7501
   612
    jthrowable exception = (*mEnv)->ExceptionOccurred(mEnv);
tim@5650
   613
    if (exception != NULL) {
tim@5650
   614
        jmethodID mid;
tim@5650
   615
gabomdq@7663
   616
        /* Until this happens most JNI operations have undefined behaviour */
ewing@7501
   617
        (*mEnv)->ExceptionClear(mEnv);
tim@5650
   618
slouken@7045
   619
        if (!silent) {
ewing@7501
   620
            jclass exceptionClass = (*mEnv)->GetObjectClass(mEnv, exception);
ewing@7501
   621
            jclass classClass = (*mEnv)->FindClass(mEnv, "java/lang/Class");
tim@5650
   622
ewing@7501
   623
            mid = (*mEnv)->GetMethodID(mEnv, classClass, "getName", "()Ljava/lang/String;");
ewing@7501
   624
            jstring exceptionName = (jstring)(*mEnv)->CallObjectMethod(mEnv, exceptionClass, mid);
ewing@7501
   625
            const char* exceptionNameUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionName, 0);
tim@5650
   626
ewing@7501
   627
            mid = (*mEnv)->GetMethodID(mEnv, exceptionClass, "getMessage", "()Ljava/lang/String;");
ewing@7501
   628
            jstring exceptionMessage = (jstring)(*mEnv)->CallObjectMethod(mEnv, exception, mid);
tim@5650
   629
slouken@7045
   630
            if (exceptionMessage != NULL) {
ewing@7501
   631
                const char* exceptionMessageUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionMessage, 0);
slouken@7045
   632
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
ewing@7501
   633
                (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionMessage, exceptionMessageUTF8);
slouken@7045
   634
            } else {
slouken@7045
   635
                SDL_SetError("%s", exceptionNameUTF8);
slouken@7045
   636
            }
slouken@7045
   637
ewing@7501
   638
            (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionName, exceptionNameUTF8);
tim@5650
   639
        }
tim@5650
   640
tim@5650
   641
        return true;
tim@5650
   642
    }
tim@5650
   643
tim@5650
   644
    return false;
tim@5650
   645
}
tim@5650
   646
ewing@7501
   647
static int Internal_Android_JNI_FileOpen(SDL_RWops* ctx)
icculus@5582
   648
{
ewing@7501
   649
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
   650
tim@5650
   651
    int result = 0;
tim@5650
   652
tim@5650
   653
    jmethodID mid;
tim@5650
   654
    jobject context;
tim@5650
   655
    jobject assetManager;
tim@5650
   656
    jobject inputStream;
tim@5650
   657
    jclass channels;
tim@5650
   658
    jobject readableByteChannel;
tim@5650
   659
    jstring fileNameJString;
gabomdq@6806
   660
    jobject fd;
gabomdq@6806
   661
    jclass fdCls;
gabomdq@6806
   662
    jfieldID descriptor;
tim@5650
   663
gabomdq@6354
   664
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   665
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
tim@5650
   666
        goto failure;
tim@5650
   667
    }
tim@5650
   668
gabomdq@6308
   669
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
gabomdq@6806
   670
    ctx->hidden.androidio.position = 0;
icculus@5582
   671
gabomdq@7663
   672
    /* context = SDLActivity.getContext(); */
ewing@7501
   673
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@5582
   674
            "getContext","()Landroid/content/Context;");
ewing@7501
   675
    context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
slouken@7191
   676
icculus@5582
   677
gabomdq@7663
   678
    /* assetManager = context.getAssets(); */
ewing@7501
   679
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
tim@5650
   680
            "getAssets", "()Landroid/content/res/AssetManager;");
ewing@7501
   681
    assetManager = (*mEnv)->CallObjectMethod(mEnv, context, mid);
icculus@5582
   682
gabomdq@6806
   683
    /* First let's try opening the file to obtain an AssetFileDescriptor.
gabomdq@6806
   684
    * This method reads the files directly from the APKs using standard *nix calls
gabomdq@6806
   685
    */
ewing@7501
   686
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
ewing@7501
   687
    inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString);
slouken@7045
   688
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   689
        goto fallback;
icculus@5582
   690
    }
icculus@5582
   691
ewing@7501
   692
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getStartOffset", "()J");
ewing@7501
   693
    ctx->hidden.androidio.offset = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
slouken@7045
   694
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   695
        goto fallback;
icculus@5582
   696
    }
icculus@5582
   697
ewing@7501
   698
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getDeclaredLength", "()J");
ewing@7501
   699
    ctx->hidden.androidio.size = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
slouken@7045
   700
    if (Android_JNI_ExceptionOccurred(true)) {
gabomdq@6806
   701
        goto fallback;
icculus@5582
   702
    }
icculus@5582
   703
ewing@7501
   704
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
ewing@7501
   705
    fd = (*mEnv)->CallObjectMethod(mEnv, inputStream, mid);
ewing@7501
   706
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
ewing@7501
   707
    descriptor = (*mEnv)->GetFieldID(mEnv, fdCls, "descriptor", "I");
ewing@7501
   708
    ctx->hidden.androidio.fd = (*mEnv)->GetIntField(mEnv, fd, descriptor);
ewing@7501
   709
    ctx->hidden.androidio.assetFileDescriptorRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
tim@5650
   710
gabomdq@7663
   711
    /* Seek to the correct offset in the file. */
gabomdq@6816
   712
    lseek(ctx->hidden.androidio.fd, (off_t)ctx->hidden.androidio.offset, SEEK_SET);
gabomdq@6816
   713
gabomdq@6806
   714
    if (false) {
gabomdq@6806
   715
fallback:
gabomdq@7663
   716
        /* Disabled log message because of spam on the Nexus 7 */
gabomdq@7678
   717
        /* __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file"); */
slouken@7190
   718
gabomdq@6806
   719
        /* Try the old method using InputStream */
gabomdq@6806
   720
        ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
   721
gabomdq@7663
   722
        /* inputStream = assetManager.open(<filename>); */
ewing@7501
   723
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager),
gabomdq@6806
   724
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
gabomdq@7678
   725
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
ewing@7501
   726
        if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   727
            goto failure;
gabomdq@6806
   728
        }
gabomdq@6806
   729
ewing@7501
   730
        ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
gabomdq@6806
   731
gabomdq@7663
   732
        /* Despite all the visible documentation on [Asset]InputStream claiming
gabomdq@7663
   733
         * that the .available() method is not guaranteed to return the entire file
gabomdq@7663
   734
         * size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
gabomdq@7663
   735
         * android/apis/content/ReadAsset.java imply that Android's
gabomdq@7663
   736
         * AssetInputStream.available() /will/ always return the total file size
gabomdq@7663
   737
        */
gabomdq@7663
   738
        
gabomdq@7663
   739
        /* size = inputStream.available(); */
ewing@7501
   740
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   741
                "available", "()I");
ewing@7501
   742
        ctx->hidden.androidio.size = (long)(*mEnv)->CallIntMethod(mEnv, inputStream, mid);
ewing@7501
   743
        if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   744
            goto failure;
gabomdq@6806
   745
        }
gabomdq@6806
   746
gabomdq@7663
   747
        /* readableByteChannel = Channels.newChannel(inputStream); */
ewing@7501
   748
        channels = (*mEnv)->FindClass(mEnv, "java/nio/channels/Channels");
ewing@7501
   749
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
gabomdq@6806
   750
                "newChannel",
gabomdq@6806
   751
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
ewing@7501
   752
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
ewing@7501
   753
                mEnv, channels, mid, inputStream);
ewing@7501
   754
        if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   755
            goto failure;
gabomdq@6806
   756
        }
gabomdq@6806
   757
gabomdq@6806
   758
        ctx->hidden.androidio.readableByteChannelRef =
ewing@7501
   759
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
gabomdq@6806
   760
gabomdq@7663
   761
        /* Store .read id for reading purposes */
ewing@7501
   762
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, readableByteChannel),
gabomdq@6806
   763
                "read", "(Ljava/nio/ByteBuffer;)I");
gabomdq@6806
   764
        ctx->hidden.androidio.readMethod = mid;
gabomdq@6806
   765
    }
icculus@5582
   766
tim@5650
   767
    if (false) {
tim@5650
   768
failure:
tim@5650
   769
        result = -1;
tim@5650
   770
ewing@7501
   771
        (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
tim@5650
   772
tim@5650
   773
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
ewing@7501
   774
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
tim@5650
   775
        }
gabomdq@6308
   776
gabomdq@6308
   777
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
ewing@7501
   778
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
gabomdq@6308
   779
        }
gabomdq@6308
   780
gabomdq@6806
   781
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
ewing@7501
   782
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
gabomdq@6806
   783
        }
gabomdq@6806
   784
tim@5650
   785
    }
ewing@7501
   786
    
ewing@7501
   787
    LocalReferenceHolder_Cleanup(&refs);
tim@5650
   788
    return result;
icculus@5582
   789
}
icculus@5582
   790
ewing@7501
   791
int Android_JNI_FileOpen(SDL_RWops* ctx,
ewing@7501
   792
        const char* fileName, const char* mode)
icculus@5582
   793
{
ewing@7501
   794
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
gabomdq@6354
   795
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   796
    int retval;
slouken@6284
   797
ewing@7501
   798
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   799
        LocalReferenceHolder_Cleanup(&refs);        
slouken@6284
   800
        return -1;
slouken@6284
   801
    }
slouken@6284
   802
icculus@5582
   803
    if (!ctx) {
ewing@7501
   804
        LocalReferenceHolder_Cleanup(&refs);
icculus@5582
   805
        return -1;
icculus@5582
   806
    }
icculus@5582
   807
ewing@7501
   808
    jstring fileNameJString = (*mEnv)->NewStringUTF(mEnv, fileName);
ewing@7501
   809
    ctx->hidden.androidio.fileNameRef = (*mEnv)->NewGlobalRef(mEnv, fileNameJString);
tim@5650
   810
    ctx->hidden.androidio.inputStreamRef = NULL;
gabomdq@6335
   811
    ctx->hidden.androidio.readableByteChannelRef = NULL;
gabomdq@6335
   812
    ctx->hidden.androidio.readMethod = NULL;
gabomdq@6806
   813
    ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
   814
ewing@7501
   815
    retval = Internal_Android_JNI_FileOpen(ctx);
ewing@7501
   816
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
   817
    return retval;
icculus@5582
   818
}
icculus@5582
   819
ewing@7501
   820
size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
icculus@5582
   821
        size_t size, size_t maxnum)
icculus@5582
   822
{
ewing@7501
   823
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
icculus@5582
   824
gabomdq@6806
   825
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   826
        size_t bytesMax = size * maxnum;
gabomdq@7678
   827
        if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
gabomdq@6806
   828
            bytesMax = ctx->hidden.androidio.size - ctx->hidden.androidio.position;
gabomdq@6806
   829
        }
gabomdq@6806
   830
        size_t result = read(ctx->hidden.androidio.fd, buffer, bytesMax );
gabomdq@6806
   831
        if (result > 0) {
gabomdq@6806
   832
            ctx->hidden.androidio.position += result;
ewing@7501
   833
            LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
   834
            return result / size;
gabomdq@6806
   835
        }
ewing@7501
   836
        LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
   837
        return 0;
gabomdq@6806
   838
    } else {
gabomdq@6806
   839
        jlong bytesRemaining = (jlong) (size * maxnum);
gabomdq@6806
   840
        jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
gabomdq@6806
   841
        int bytesRead = 0;
gabomdq@6377
   842
gabomdq@6806
   843
        /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
gabomdq@6806
   844
        if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
slouken@6284
   845
gabomdq@6806
   846
        JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   847
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   848
            LocalReferenceHolder_Cleanup(&refs);            
philipp@7368
   849
            return 0;
icculus@5582
   850
        }
icculus@5582
   851
gabomdq@6806
   852
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
gabomdq@6806
   853
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
ewing@7501
   854
        jobject byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
gabomdq@6806
   855
gabomdq@6806
   856
        while (bytesRemaining > 0) {
gabomdq@7663
   857
            /* result = readableByteChannel.read(...); */
ewing@7501
   858
            int result = (*mEnv)->CallIntMethod(mEnv, readableByteChannel, readMethod, byteBuffer);
gabomdq@6806
   859
ewing@7501
   860
            if (Android_JNI_ExceptionOccurred(false)) {
ewing@7501
   861
                LocalReferenceHolder_Cleanup(&refs);            
gabomdq@6806
   862
                return 0;
gabomdq@6806
   863
            }
gabomdq@6806
   864
gabomdq@6806
   865
            if (result < 0) {
gabomdq@6806
   866
                break;
gabomdq@6806
   867
            }
gabomdq@6806
   868
gabomdq@6806
   869
            bytesRemaining -= result;
gabomdq@6806
   870
            bytesRead += result;
gabomdq@6806
   871
            ctx->hidden.androidio.position += result;
icculus@5582
   872
        }
ewing@7501
   873
        LocalReferenceHolder_Cleanup(&refs);                    
gabomdq@6806
   874
        return bytesRead / size;
slouken@7191
   875
    }
icculus@5582
   876
}
icculus@5582
   877
ewing@7501
   878
size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
icculus@5582
   879
        size_t size, size_t num)
icculus@5582
   880
{
icculus@5582
   881
    SDL_SetError("Cannot write to Android package filesystem");
icculus@5582
   882
    return 0;
icculus@5582
   883
}
icculus@5582
   884
ewing@7501
   885
static int Internal_Android_JNI_FileClose(SDL_RWops* ctx, bool release)
icculus@5582
   886
{
ewing@7501
   887
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
   888
icculus@5582
   889
    int result = 0;
gabomdq@6354
   890
    JNIEnv *mEnv = Android_JNI_GetEnv();
icculus@5582
   891
ewing@7501
   892
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
   893
        LocalReferenceHolder_Cleanup(&refs);
icculus@7037
   894
        return SDL_SetError("Failed to allocate enough JVM local references");
slouken@6284
   895
    }
slouken@6284
   896
icculus@5582
   897
    if (ctx) {
icculus@5582
   898
        if (release) {
ewing@7501
   899
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   900
        }
icculus@5582
   901
gabomdq@6806
   902
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   903
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
ewing@7501
   904
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   905
                    "close", "()V");
ewing@7501
   906
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
   907
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
ewing@7501
   908
            if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   909
                result = -1;
gabomdq@6806
   910
            }
gabomdq@6806
   911
        }
gabomdq@6806
   912
        else {
gabomdq@6806
   913
            jobject inputStream = (jobject)ctx->hidden.androidio.inputStreamRef;
icculus@5582
   914
gabomdq@7677
   915
            /* inputStream.close(); */
ewing@7501
   916
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
   917
                    "close", "()V");
ewing@7501
   918
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
   919
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
ewing@7501
   920
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
ewing@7501
   921
            if (Android_JNI_ExceptionOccurred(false)) {
gabomdq@6806
   922
                result = -1;
gabomdq@6806
   923
            }
icculus@5582
   924
        }
icculus@5582
   925
icculus@5582
   926
        if (release) {
icculus@5582
   927
            SDL_FreeRW(ctx);
icculus@5582
   928
        }
icculus@5582
   929
    }
icculus@5582
   930
ewing@7501
   931
    LocalReferenceHolder_Cleanup(&refs);
icculus@5582
   932
    return result;
icculus@5582
   933
}
icculus@5582
   934
icculus@5582
   935
ewing@7501
   936
Sint64 Android_JNI_FileSize(SDL_RWops* ctx)
icculus@5582
   937
{
slouken@6642
   938
    return ctx->hidden.androidio.size;
slouken@6642
   939
}
slouken@6642
   940
ewing@7501
   941
Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
slouken@6642
   942
{
gabomdq@6806
   943
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
   944
        switch (whence) {
gabomdq@6806
   945
            case RW_SEEK_SET:
gabomdq@7678
   946
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
   947
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
   948
                break;
gabomdq@6806
   949
            case RW_SEEK_CUR:
gabomdq@6806
   950
                offset += ctx->hidden.androidio.position;
gabomdq@7678
   951
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
   952
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
   953
                break;
gabomdq@6806
   954
            case RW_SEEK_END:
gabomdq@6806
   955
                offset = ctx->hidden.androidio.offset + ctx->hidden.androidio.size + offset;
gabomdq@6806
   956
                break;
gabomdq@6806
   957
            default:
icculus@7037
   958
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
   959
        }
gabomdq@6806
   960
        whence = SEEK_SET;
icculus@5582
   961
gabomdq@6806
   962
        off_t ret = lseek(ctx->hidden.androidio.fd, (off_t)offset, SEEK_SET);
gabomdq@6806
   963
        if (ret == -1) return -1;
gabomdq@6806
   964
        ctx->hidden.androidio.position = ret - ctx->hidden.androidio.offset;
gabomdq@6806
   965
    } else {
gabomdq@6806
   966
        Sint64 newPosition;
gabomdq@6806
   967
gabomdq@6806
   968
        switch (whence) {
gabomdq@6806
   969
            case RW_SEEK_SET:
gabomdq@6806
   970
                newPosition = offset;
gabomdq@6806
   971
                break;
gabomdq@6806
   972
            case RW_SEEK_CUR:
gabomdq@6806
   973
                newPosition = ctx->hidden.androidio.position + offset;
gabomdq@6806
   974
                break;
gabomdq@6806
   975
            case RW_SEEK_END:
gabomdq@6806
   976
                newPosition = ctx->hidden.androidio.size + offset;
gabomdq@6806
   977
                break;
gabomdq@6806
   978
            default:
icculus@7037
   979
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
   980
        }
gabomdq@6806
   981
gabomdq@6806
   982
        /* Validate the new position */
gabomdq@6806
   983
        if (newPosition < 0) {
icculus@7037
   984
            return SDL_Error(SDL_EFSEEK);
gabomdq@6806
   985
        }
gabomdq@6806
   986
        if (newPosition > ctx->hidden.androidio.size) {
gabomdq@6806
   987
            newPosition = ctx->hidden.androidio.size;
gabomdq@6806
   988
        }
slouken@6642
   989
gabomdq@6806
   990
        Sint64 movement = newPosition - ctx->hidden.androidio.position;
gabomdq@6806
   991
        if (movement > 0) {
gabomdq@6806
   992
            unsigned char buffer[4096];
icculus@5582
   993
gabomdq@7663
   994
            /* The easy case where we're seeking forwards */
gabomdq@6806
   995
            while (movement > 0) {
gabomdq@6806
   996
                Sint64 amount = sizeof (buffer);
gabomdq@6806
   997
                if (amount > movement) {
gabomdq@6806
   998
                    amount = movement;
gabomdq@6806
   999
                }
gabomdq@6806
  1000
                size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);
gabomdq@6806
  1001
                if (result <= 0) {
gabomdq@7663
  1002
                    /* Failed to read/skip the required amount, so fail */
gabomdq@6806
  1003
                    return -1;
gabomdq@6806
  1004
                }
tim@5993
  1005
gabomdq@6806
  1006
                movement -= result;
icculus@5582
  1007
            }
tim@5993
  1008
gabomdq@6806
  1009
        } else if (movement < 0) {
gabomdq@7663
  1010
            /* We can't seek backwards so we have to reopen the file and seek */
gabomdq@7663
  1011
            /* forwards which obviously isn't very efficient */
ewing@7501
  1012
            Internal_Android_JNI_FileClose(ctx, false);
ewing@7501
  1013
            Internal_Android_JNI_FileOpen(ctx);
gabomdq@6806
  1014
            Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
icculus@5582
  1015
        }
icculus@5582
  1016
    }
icculus@5582
  1017
icculus@5582
  1018
    return ctx->hidden.androidio.position;
slouken@7191
  1019
icculus@5582
  1020
}
icculus@5582
  1021
ewing@7501
  1022
int Android_JNI_FileClose(SDL_RWops* ctx)
icculus@5582
  1023
{
ewing@7501
  1024
    return Internal_Android_JNI_FileClose(ctx, true);
icculus@5582
  1025
}
icculus@5582
  1026
gabomdq@7663
  1027
/* returns a new global reference which needs to be released later */
slouken@6464
  1028
static jobject Android_JNI_GetSystemServiceObject(const char* name)
slouken@6464
  1029
{
ewing@7501
  1030
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6464
  1031
    JNIEnv* env = Android_JNI_GetEnv();
ewing@7501
  1032
    jobject retval = NULL;
ewing@7501
  1033
ewing@7501
  1034
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1035
        LocalReferenceHolder_Cleanup(&refs);
slouken@6464
  1036
        return NULL;
slouken@6464
  1037
    }
slouken@6464
  1038
ewing@7501
  1039
    jstring service = (*env)->NewStringUTF(env, name);
slouken@6464
  1040
slouken@6464
  1041
    jmethodID mid;
slouken@6464
  1042
ewing@7501
  1043
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@7501
  1044
    jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6464
  1045
philipp@8029
  1046
    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemServiceFromUiThread", "(Ljava/lang/String;)Ljava/lang/Object;");
ewing@7501
  1047
    jobject manager = (*env)->CallObjectMethod(env, context, mid, service);
slouken@6464
  1048
ewing@7501
  1049
    (*env)->DeleteLocalRef(env, service);
slouken@6464
  1050
ewing@7501
  1051
    retval = manager ? (*env)->NewGlobalRef(env, manager) : NULL;
ewing@7501
  1052
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1053
    return retval;
slouken@6464
  1054
}
slouken@6464
  1055
slouken@6464
  1056
#define SETUP_CLIPBOARD(error) \
ewing@7501
  1057
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__); \
slouken@6464
  1058
    JNIEnv* env = Android_JNI_GetEnv(); \
ewing@7501
  1059
    if (!LocalReferenceHolder_Init(&refs, env)) { \
ewing@7501
  1060
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1061
        return error; \
slouken@6464
  1062
    } \
slouken@6464
  1063
    jobject clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
slouken@6464
  1064
    if (!clipboard) { \
ewing@7501
  1065
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1066
        return error; \
slouken@6464
  1067
    }
slouken@6464
  1068
ewing@7501
  1069
#define CLEANUP_CLIPBOARD() \
ewing@7501
  1070
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1071
ewing@7501
  1072
int Android_JNI_SetClipboardText(const char* text)
slouken@6464
  1073
{
slouken@6464
  1074
    SETUP_CLIPBOARD(-1)
slouken@6464
  1075
ewing@7501
  1076
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "setText", "(Ljava/lang/CharSequence;)V");
ewing@7501
  1077
    jstring string = (*env)->NewStringUTF(env, text);
ewing@7501
  1078
    (*env)->CallVoidMethod(env, clipboard, mid, string);
ewing@7501
  1079
    (*env)->DeleteGlobalRef(env, clipboard);
ewing@7501
  1080
    (*env)->DeleteLocalRef(env, string);
ewing@7501
  1081
ewing@7501
  1082
    CLEANUP_CLIPBOARD();
ewing@7501
  1083
slouken@6464
  1084
    return 0;
slouken@6464
  1085
}
slouken@6464
  1086
ewing@7501
  1087
char* Android_JNI_GetClipboardText()
slouken@6464
  1088
{
slouken@6464
  1089
    SETUP_CLIPBOARD(SDL_strdup(""))
slouken@6464
  1090
ewing@7501
  1091
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "getText", "()Ljava/lang/CharSequence;");
ewing@7501
  1092
    jobject sequence = (*env)->CallObjectMethod(env, clipboard, mid);
ewing@7501
  1093
    (*env)->DeleteGlobalRef(env, clipboard);
slouken@6464
  1094
    if (sequence) {
ewing@7501
  1095
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, sequence), "toString", "()Ljava/lang/String;");
ewing@7501
  1096
        jstring string = (jstring)((*env)->CallObjectMethod(env, sequence, mid));
ewing@7501
  1097
        const char* utf = (*env)->GetStringUTFChars(env, string, 0);
slouken@6464
  1098
        if (utf) {
slouken@6464
  1099
            char* text = SDL_strdup(utf);
ewing@7501
  1100
            (*env)->ReleaseStringUTFChars(env, string, utf);
ewing@7501
  1101
ewing@7501
  1102
            CLEANUP_CLIPBOARD();
ewing@7501
  1103
slouken@6464
  1104
            return text;
slouken@6464
  1105
        }
slouken@6464
  1106
    }
ewing@7501
  1107
ewing@7501
  1108
    CLEANUP_CLIPBOARD();    
ewing@7501
  1109
slouken@6464
  1110
    return SDL_strdup("");
slouken@6464
  1111
}
slouken@6464
  1112
ewing@7501
  1113
SDL_bool Android_JNI_HasClipboardText()
slouken@6464
  1114
{
slouken@6464
  1115
    SETUP_CLIPBOARD(SDL_FALSE)
slouken@6464
  1116
ewing@7501
  1117
    jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "hasText", "()Z");
ewing@7501
  1118
    jboolean has = (*env)->CallBooleanMethod(env, clipboard, mid);
ewing@7501
  1119
    (*env)->DeleteGlobalRef(env, clipboard);
ewing@7501
  1120
ewing@7501
  1121
    CLEANUP_CLIPBOARD();
ewing@7501
  1122
    
slouken@6464
  1123
    return has ? SDL_TRUE : SDL_FALSE;
slouken@6464
  1124
}
slouken@6464
  1125
slouken@6464
  1126
gabomdq@7663
  1127
/* returns 0 on success or -1 on error (others undefined then)
gabomdq@7663
  1128
 * returns truthy or falsy value in plugged, charged and battery
gabomdq@7663
  1129
 * returns the value in seconds and percent or -1 if not available
gabomdq@7663
  1130
 */
ewing@7501
  1131
int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
slouken@6448
  1132
{
ewing@7501
  1133
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6448
  1134
    JNIEnv* env = Android_JNI_GetEnv();
ewing@7501
  1135
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1136
        LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1137
        return -1;
slouken@6448
  1138
    }
slouken@6448
  1139
slouken@6448
  1140
    jmethodID mid;
slouken@6448
  1141
ewing@7501
  1142
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@7501
  1143
    jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6448
  1144
ewing@7501
  1145
    jstring action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED");
slouken@6448
  1146
ewing@7501
  1147
    jclass cls = (*env)->FindClass(env, "android/content/IntentFilter");
slouken@6448
  1148
ewing@7501
  1149
    mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
ewing@7501
  1150
    jobject filter = (*env)->NewObject(env, cls, mid, action);
slouken@6448
  1151
ewing@7501
  1152
    (*env)->DeleteLocalRef(env, action);
slouken@6448
  1153
ewing@7501
  1154
    mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
ewing@7501
  1155
    jobject intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter);
slouken@6448
  1156
ewing@7501
  1157
    (*env)->DeleteLocalRef(env, filter);
slouken@6448
  1158
ewing@7501
  1159
    cls = (*env)->GetObjectClass(env, intent);
slouken@6448
  1160
slouken@6448
  1161
    jstring iname;
ewing@7501
  1162
    jmethodID imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I");
slouken@6448
  1163
slouken@6448
  1164
#define GET_INT_EXTRA(var, key) \
ewing@7501
  1165
    iname = (*env)->NewStringUTF(env, key); \
ewing@7501
  1166
    int var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \
ewing@7501
  1167
    (*env)->DeleteLocalRef(env, iname);
slouken@6448
  1168
slouken@6448
  1169
    jstring bname;
ewing@7501
  1170
    jmethodID bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
slouken@6448
  1171
slouken@6448
  1172
#define GET_BOOL_EXTRA(var, key) \
ewing@7501
  1173
    bname = (*env)->NewStringUTF(env, key); \
ewing@7501
  1174
    int var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \
ewing@7501
  1175
    (*env)->DeleteLocalRef(env, bname);
slouken@6448
  1176
slouken@6448
  1177
    if (plugged) {
gabomdq@7663
  1178
        GET_INT_EXTRA(plug, "plugged") /* == BatteryManager.EXTRA_PLUGGED (API 5) */
slouken@6448
  1179
        if (plug == -1) {
ewing@7501
  1180
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1181
            return -1;
slouken@6448
  1182
        }
gabomdq@7663
  1183
        /* 1 == BatteryManager.BATTERY_PLUGGED_AC */
gabomdq@7663
  1184
        /* 2 == BatteryManager.BATTERY_PLUGGED_USB */
slouken@6448
  1185
        *plugged = (0 < plug) ? 1 : 0;
slouken@6448
  1186
    }
slouken@6448
  1187
slouken@6448
  1188
    if (charged) {
gabomdq@7663
  1189
        GET_INT_EXTRA(status, "status") /* == BatteryManager.EXTRA_STATUS (API 5) */
slouken@6448
  1190
        if (status == -1) {
ewing@7501
  1191
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1192
            return -1;
slouken@6448
  1193
        }
gabomdq@7663
  1194
        /* 5 == BatteryManager.BATTERY_STATUS_FULL */
slouken@6448
  1195
        *charged = (status == 5) ? 1 : 0;
slouken@6448
  1196
    }
slouken@6448
  1197
slouken@6448
  1198
    if (battery) {
gabomdq@7663
  1199
        GET_BOOL_EXTRA(present, "present") /* == BatteryManager.EXTRA_PRESENT (API 5) */
slouken@6448
  1200
        *battery = present ? 1 : 0;
slouken@6448
  1201
    }
slouken@6448
  1202
slouken@6448
  1203
    if (seconds) {
gabomdq@7663
  1204
        *seconds = -1; /* not possible */
slouken@6448
  1205
    }
slouken@6448
  1206
slouken@6448
  1207
    if (percent) {
gabomdq@7663
  1208
        GET_INT_EXTRA(level, "level") /* == BatteryManager.EXTRA_LEVEL (API 5) */
gabomdq@7663
  1209
        GET_INT_EXTRA(scale, "scale") /* == BatteryManager.EXTRA_SCALE (API 5) */
slouken@6448
  1210
        if ((level == -1) || (scale == -1)) {
ewing@7501
  1211
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1212
            return -1;
slouken@6448
  1213
        }
slouken@6448
  1214
        *percent = level * 100 / scale;
slouken@6448
  1215
    }
slouken@6448
  1216
ewing@7501
  1217
    (*env)->DeleteLocalRef(env, intent);
slouken@6448
  1218
ewing@7501
  1219
    LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1220
    return 0;
slouken@6448
  1221
}
slouken@6448
  1222
philipp@7786
  1223
/* returns number of found touch devices as return value and ids in parameter ids */
philipp@7786
  1224
int Android_JNI_GetTouchDeviceIds(int **ids) {
philipp@7786
  1225
    JNIEnv *env = Android_JNI_GetEnv();
philipp@7786
  1226
    jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
philipp@7786
  1227
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "inputGetInputDeviceIds", "(I)[I");
philipp@7786
  1228
    jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, mid, sources);
philipp@7786
  1229
    int number = 0;
philipp@7786
  1230
    *ids = NULL;
philipp@7786
  1231
    if (array) {
philipp@7786
  1232
        number = (int) (*env)->GetArrayLength(env, array);
philipp@7786
  1233
        if (0 < number) {
philipp@7786
  1234
            jint* elements = (*env)->GetIntArrayElements(env, array, NULL);
philipp@7786
  1235
            if (elements) {
philipp@7786
  1236
                int i;
slouken@7863
  1237
                *ids = SDL_malloc(number * sizeof (**ids));
philipp@7786
  1238
                for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
slouken@7863
  1239
                    (*ids)[i] = elements[i];
philipp@7786
  1240
                }
philipp@7786
  1241
                (*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
philipp@7786
  1242
            }
philipp@7786
  1243
        }
philipp@7786
  1244
        (*env)->DeleteLocalRef(env, array);
philipp@7786
  1245
    }
philipp@7786
  1246
    return number;
philipp@7786
  1247
}
philipp@7786
  1248
gabomdq@7907
  1249
/* return the total number of plugged in joysticks */
gabomdq@7907
  1250
int Android_JNI_GetNumJoysticks()
gabomdq@7907
  1251
{
gabomdq@7907
  1252
    JNIEnv* env = Android_JNI_GetEnv();
gabomdq@7907
  1253
    if (!env) {
gabomdq@7907
  1254
        return -1;
gabomdq@7907
  1255
    }
gabomdq@7907
  1256
    
gabomdq@7907
  1257
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getNumJoysticks", "()I");
gabomdq@7907
  1258
    if (!mid) {
gabomdq@7907
  1259
        return -1;
gabomdq@7907
  1260
    }
gabomdq@7907
  1261
    
gabomdq@7907
  1262
    return (int)(*env)->CallStaticIntMethod(env, mActivityClass, mid);
gabomdq@7907
  1263
}
gabomdq@7907
  1264
gabomdq@7907
  1265
/* Return the name of joystick number "i" */
gabomdq@7907
  1266
char* Android_JNI_GetJoystickName(int i)
gabomdq@7907
  1267
{
gabomdq@7907
  1268
    JNIEnv* env = Android_JNI_GetEnv();
gabomdq@7907
  1269
    if (!env) {
gabomdq@7907
  1270
        return SDL_strdup("");
gabomdq@7907
  1271
    }
gabomdq@7907
  1272
    
gabomdq@7907
  1273
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
gabomdq@7907
  1274
    if (!mid) {
gabomdq@7907
  1275
        return SDL_strdup("");
gabomdq@7907
  1276
    }
gabomdq@7907
  1277
    jstring string = (jstring)((*env)->CallStaticObjectMethod(env, mActivityClass, mid, i));
gabomdq@7907
  1278
    const char* utf = (*env)->GetStringUTFChars(env, string, 0);
gabomdq@7907
  1279
    if (!utf) {
gabomdq@7907
  1280
        return SDL_strdup("");
gabomdq@7907
  1281
    }
gabomdq@7907
  1282
    
gabomdq@7907
  1283
    char* text = SDL_strdup(utf);
gabomdq@7907
  1284
    (*env)->ReleaseStringUTFChars(env, string, utf);
gabomdq@7907
  1285
    return text;
gabomdq@7907
  1286
}
gabomdq@7907
  1287
gabomdq@7907
  1288
/* return the number of axes in the given joystick */
gabomdq@7907
  1289
int Android_JNI_GetJoystickAxes(int joy)
gabomdq@7907
  1290
{
gabomdq@7907
  1291
    JNIEnv* env = Android_JNI_GetEnv();
gabomdq@7907
  1292
    if (!env) {
gabomdq@7907
  1293
        return -1;
gabomdq@7907
  1294
    }
gabomdq@7907
  1295
    
gabomdq@7907
  1296
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickAxes", "(I)I");
gabomdq@7907
  1297
    if (!mid) {
gabomdq@7907
  1298
        return -1;
gabomdq@7907
  1299
    }
gabomdq@7907
  1300
    
gabomdq@7907
  1301
    return (int)(*env)->CallIntMethod(env, mActivityClass, mid, joy);
gabomdq@7907
  1302
}
gabomdq@7907
  1303
gabomdq@7907
  1304
gabomdq@7663
  1305
/* sends message to be handled on the UI event dispatch thread */
ewing@7501
  1306
int Android_JNI_SendMessage(int command, int param)
slouken@6392
  1307
{
slouken@6392
  1308
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6392
  1309
    if (!env) {
slouken@6392
  1310
        return -1;
slouken@6392
  1311
    }
ewing@7501
  1312
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
slouken@6392
  1313
    if (!mid) {
slouken@6392
  1314
        return -1;
slouken@6392
  1315
    }
ewing@7501
  1316
    jboolean success = (*env)->CallStaticBooleanMethod(env, mActivityClass, mid, command, param);
philipp@7149
  1317
    return success ? 0 : -1;
slouken@6392
  1318
}
slouken@6392
  1319
ewing@7501
  1320
void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
slouken@6555
  1321
{
slouken@6555
  1322
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6555
  1323
    if (!env) {
slouken@6654
  1324
        return;
slouken@6555
  1325
    }
slouken@6555
  1326
ewing@7501
  1327
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "showTextInput", "(IIII)Z");
slouken@6555
  1328
    if (!mid) {
slouken@6654
  1329
        return;
slouken@6555
  1330
    }
ewing@7501
  1331
    (*env)->CallStaticBooleanMethod(env, mActivityClass, mid,
slouken@6555
  1332
                               inputRect->x,
slouken@6555
  1333
                               inputRect->y,
slouken@6555
  1334
                               inputRect->w,
slouken@6555
  1335
                               inputRect->h );
slouken@6555
  1336
}
slouken@6555
  1337
ewing@7501
  1338
void Android_JNI_HideTextInput()
slouken@6555
  1339
{
gabomdq@7663
  1340
    /* has to match Activity constant */
slouken@6654
  1341
    const int COMMAND_TEXTEDIT_HIDE = 3;
slouken@6654
  1342
    Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
slouken@6654
  1343
}
slouken@6555
  1344
gabomdq@7663
  1345
/*
slouken@6630
  1346
//////////////////////////////////////////////////////////////////////////////
slouken@6630
  1347
//
slouken@6630
  1348
// Functions exposed to SDL applications in SDL_system.h
gabomdq@7663
  1349
//////////////////////////////////////////////////////////////////////////////
gabomdq@7663
  1350
*/
slouken@6630
  1351
ewing@7501
  1352
void *SDL_AndroidGetJNIEnv()
slouken@6630
  1353
{
slouken@6630
  1354
    return Android_JNI_GetEnv();
slouken@6630
  1355
}
slouken@6630
  1356
gabomdq@7095
  1357
gabomdq@7083
  1358
ewing@7501
  1359
void *SDL_AndroidGetActivity()
slouken@6630
  1360
{
gabomdq@7095
  1361
    /* See SDL_system.h for caveats on using this function. */
slouken@7191
  1362
slouken@6630
  1363
    jmethodID mid;
slouken@6630
  1364
slouken@6630
  1365
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@7083
  1366
    if (!env) {
slouken@6630
  1367
        return NULL;
slouken@6630
  1368
    }
slouken@6630
  1369
gabomdq@7663
  1370
    /* return SDLActivity.getContext(); */
ewing@7501
  1371
    mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1372
            "getContext","()Landroid/content/Context;");
ewing@7501
  1373
    return (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1374
}
slouken@6630
  1375
ewing@7501
  1376
const char * SDL_AndroidGetInternalStoragePath()
slouken@6630
  1377
{
slouken@6630
  1378
    static char *s_AndroidInternalFilesPath = NULL;
slouken@6630
  1379
slouken@6630
  1380
    if (!s_AndroidInternalFilesPath) {
ewing@7501
  1381
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1382
        jmethodID mid;
slouken@6630
  1383
        jobject context;
slouken@6630
  1384
        jobject fileObject;
slouken@6630
  1385
        jstring pathString;
slouken@6630
  1386
        const char *path;
slouken@6630
  1387
slouken@6630
  1388
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1389
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1390
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1391
            return NULL;
slouken@6630
  1392
        }
slouken@6630
  1393
gabomdq@7663
  1394
        /* context = SDLActivity.getContext(); */
ewing@7501
  1395
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1396
                "getContext","()Landroid/content/Context;");
ewing@7501
  1397
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1398
gabomdq@7663
  1399
        /* fileObj = context.getFilesDir(); */
ewing@7501
  1400
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1401
                "getFilesDir", "()Ljava/io/File;");
ewing@7501
  1402
        fileObject = (*env)->CallObjectMethod(env, context, mid);
slouken@6630
  1403
        if (!fileObject) {
slouken@6630
  1404
            SDL_SetError("Couldn't get internal directory");
ewing@7501
  1405
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1406
            return NULL;
slouken@6630
  1407
        }
slouken@6630
  1408
gabomdq@7663
  1409
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1410
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1411
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1412
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1413
ewing@7501
  1414
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1415
        s_AndroidInternalFilesPath = SDL_strdup(path);
ewing@7501
  1416
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1417
ewing@7501
  1418
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1419
    }
slouken@6630
  1420
    return s_AndroidInternalFilesPath;
slouken@6630
  1421
}
slouken@6630
  1422
ewing@7501
  1423
int SDL_AndroidGetExternalStorageState()
slouken@6630
  1424
{
ewing@7501
  1425
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1426
    jmethodID mid;
slouken@6630
  1427
    jclass cls;
slouken@6630
  1428
    jstring stateString;
slouken@6630
  1429
    const char *state;
slouken@6630
  1430
    int stateFlags;
slouken@6630
  1431
slouken@6630
  1432
    JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1433
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1434
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1435
        return 0;
slouken@6630
  1436
    }
slouken@6630
  1437
ewing@7501
  1438
    cls = (*env)->FindClass(env, "android/os/Environment");
ewing@7501
  1439
    mid = (*env)->GetStaticMethodID(env, cls,
slouken@6630
  1440
            "getExternalStorageState", "()Ljava/lang/String;");
ewing@7501
  1441
    stateString = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
slouken@6630
  1442
ewing@7501
  1443
    state = (*env)->GetStringUTFChars(env, stateString, NULL);
slouken@6630
  1444
gabomdq@7663
  1445
    /* Print an info message so people debugging know the storage state */
slouken@6630
  1446
    __android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state);
slouken@6630
  1447
slouken@6630
  1448
    if (SDL_strcmp(state, "mounted") == 0) {
slouken@6630
  1449
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ |
slouken@6630
  1450
                     SDL_ANDROID_EXTERNAL_STORAGE_WRITE;
slouken@6630
  1451
    } else if (SDL_strcmp(state, "mounted_ro") == 0) {
slouken@6630
  1452
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ;
slouken@6630
  1453
    } else {
slouken@6630
  1454
        stateFlags = 0;
slouken@6630
  1455
    }
ewing@7501
  1456
    (*env)->ReleaseStringUTFChars(env, stateString, state);
slouken@6630
  1457
ewing@7501
  1458
    LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1459
    return stateFlags;
slouken@6630
  1460
}
slouken@6630
  1461
ewing@7501
  1462
const char * SDL_AndroidGetExternalStoragePath()
slouken@6630
  1463
{
slouken@6630
  1464
    static char *s_AndroidExternalFilesPath = NULL;
slouken@6630
  1465
slouken@6630
  1466
    if (!s_AndroidExternalFilesPath) {
ewing@7501
  1467
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1468
        jmethodID mid;
slouken@6630
  1469
        jobject context;
slouken@6630
  1470
        jobject fileObject;
slouken@6630
  1471
        jstring pathString;
slouken@6630
  1472
        const char *path;
slouken@6630
  1473
slouken@6630
  1474
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1475
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1476
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1477
            return NULL;
slouken@6630
  1478
        }
slouken@6630
  1479
gabomdq@7663
  1480
        /* context = SDLActivity.getContext(); */
ewing@7501
  1481
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1482
                "getContext","()Landroid/content/Context;");
ewing@7501
  1483
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1484
gabomdq@7663
  1485
        /* fileObj = context.getExternalFilesDir(); */
ewing@7501
  1486
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1487
                "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
ewing@7501
  1488
        fileObject = (*env)->CallObjectMethod(env, context, mid, NULL);
slouken@6630
  1489
        if (!fileObject) {
slouken@6630
  1490
            SDL_SetError("Couldn't get external directory");
ewing@7501
  1491
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1492
            return NULL;
slouken@6630
  1493
        }
slouken@6630
  1494
gabomdq@7663
  1495
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1496
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1497
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1498
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1499
ewing@7501
  1500
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1501
        s_AndroidExternalFilesPath = SDL_strdup(path);
ewing@7501
  1502
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1503
ewing@7501
  1504
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1505
    }
slouken@6630
  1506
    return s_AndroidExternalFilesPath;
slouken@6630
  1507
}
slouken@6630
  1508
slouken@6044
  1509
#endif /* __ANDROID__ */
slouken@6044
  1510
slouken@4981
  1511
/* vi: set ts=4 sw=4 expandtab: */
gabomdq@7612
  1512