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