src/video/android/SDL_androidvideo.c
author Sylvain Becker <sylvain.becker@gmail.com>
Fri, 05 Apr 2019 09:16:30 +0200
changeset 12693 d311fcb8066a
parent 12625 e58468e48179
child 12823 f7629f5761d8
permissions -rw-r--r--
Android: add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE
to set whether the event loop will block itself when the app is paused.
paul@4701
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
paul@4701
     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.
paul@4701
     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:
paul@4701
    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.
paul@4701
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
paul@4701
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_ANDROID
slouken@6044
    24
sylvain@12494
    25
/* Android SDL video driver implementation */
paul@4701
    26
paul@4701
    27
#include "SDL_video.h"
paul@4701
    28
#include "SDL_mouse.h"
sylvain@12693
    29
#include "SDL_hints.h"
paul@4701
    30
#include "../SDL_sysvideo.h"
paul@4701
    31
#include "../SDL_pixels_c.h"
paul@4701
    32
#include "../../events/SDL_events_c.h"
slouken@5001
    33
#include "../../events/SDL_windowevents_c.h"
paul@4701
    34
paul@4701
    35
#include "SDL_androidvideo.h"
slouken@10672
    36
#include "SDL_androidgl.h"
slouken@6464
    37
#include "SDL_androidclipboard.h"
paul@4703
    38
#include "SDL_androidevents.h"
slouken@4980
    39
#include "SDL_androidkeyboard.h"
philipp@10051
    40
#include "SDL_androidmouse.h"
philipp@7786
    41
#include "SDL_androidtouch.h"
slouken@4998
    42
#include "SDL_androidwindow.h"
icculus@11365
    43
#include "SDL_androidvulkan.h"
paul@4701
    44
paul@4701
    45
#define ANDROID_VID_DRIVER_NAME "Android"
paul@4701
    46
paul@4701
    47
/* Initialization/Query functions */
paul@4701
    48
static int Android_VideoInit(_THIS);
paul@4701
    49
static void Android_VideoQuit(_THIS);
sylvain@12494
    50
int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
paul@4701
    51
gabomdq@8021
    52
#include "../SDL_egl_c.h"
gabomdq@7659
    53
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
gabomdq@7659
    54
#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
gabomdq@7659
    55
#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
gabomdq@7659
    56
#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
gabomdq@7659
    57
#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
paul@4701
    58
paul@4701
    59
/* Android driver bootstrap functions */
paul@4701
    60
paul@4701
    61
slouken@7191
    62
/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
sylvain@12556
    63
int Android_SurfaceWidth           = 0;
sylvain@12556
    64
int Android_SurfaceHeight          = 0;
sylvain@12556
    65
static int Android_DeviceWidth     = 0;
sylvain@12556
    66
static int Android_DeviceHeight    = 0;
sylvain@12462
    67
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
sylvain@12556
    68
static int Android_ScreenRate      = 0;
sylvain@12556
    69
SDL_sem *Android_PauseSem          = NULL;
sylvain@12556
    70
SDL_sem *Android_ResumeSem         = NULL;
sylvain@12556
    71
SDL_mutex *Android_ActivityMutex   = NULL;
paul@4716
    72
paul@4701
    73
static int
paul@4701
    74
Android_Available(void)
paul@4701
    75
{
paul@4701
    76
    return 1;
paul@4701
    77
}
paul@4701
    78
paul@4701
    79
static void
gabomdq@9173
    80
Android_SuspendScreenSaver(_THIS)
gabomdq@9173
    81
{
gabomdq@9173
    82
    Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
gabomdq@9173
    83
}
gabomdq@9173
    84
gabomdq@9173
    85
static void
sylvain@12494
    86
Android_DeleteDevice(SDL_VideoDevice *device)
paul@4701
    87
{
philipp@9317
    88
    SDL_free(device->driverdata);
paul@4701
    89
    SDL_free(device);
paul@4701
    90
}
paul@4701
    91
paul@4701
    92
static SDL_VideoDevice *
paul@4701
    93
Android_CreateDevice(int devindex)
paul@4701
    94
{
paul@4701
    95
    SDL_VideoDevice *device;
slouken@6555
    96
    SDL_VideoData *data;
sylvain@12693
    97
    SDL_bool block_on_pause;
paul@4701
    98
paul@4701
    99
    /* Initialize all variables that we clean on shutdown */
paul@4701
   100
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
paul@4701
   101
    if (!device) {
paul@4701
   102
        SDL_OutOfMemory();
slouken@6555
   103
        return NULL;
paul@4701
   104
    }
paul@4701
   105
sylvain@12494
   106
    data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@6555
   107
    if (!data) {
slouken@6555
   108
        SDL_OutOfMemory();
slouken@6555
   109
        SDL_free(device);
slouken@6555
   110
        return NULL;
slouken@6555
   111
    }
slouken@6555
   112
slouken@6555
   113
    device->driverdata = data;
slouken@6555
   114
paul@4701
   115
    /* Set the function pointers */
paul@4701
   116
    device->VideoInit = Android_VideoInit;
paul@4701
   117
    device->VideoQuit = Android_VideoQuit;
sylvain@12693
   118
    block_on_pause = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, SDL_TRUE);
sylvain@12693
   119
    if (block_on_pause) {
sylvain@12693
   120
        device->PumpEvents = Android_PumpEvents_Blocking;
sylvain@12693
   121
    } else {
sylvain@12693
   122
        device->PumpEvents = Android_PumpEvents_NonBlocking;
sylvain@12693
   123
    }
slouken@4980
   124
slouken@11662
   125
    device->GetDisplayDPI = Android_GetDisplayDPI;
slouken@11662
   126
slouken@11383
   127
    device->CreateSDLWindow = Android_CreateWindow;
slouken@4998
   128
    device->SetWindowTitle = Android_SetWindowTitle;
slouken@11865
   129
    device->SetWindowFullscreen = Android_SetWindowFullscreen;
slouken@4998
   130
    device->DestroyWindow = Android_DestroyWindow;
slouken@8799
   131
    device->GetWindowWMInfo = Android_GetWindowWMInfo;
slouken@4998
   132
paul@4701
   133
    device->free = Android_DeleteDevice;
paul@4701
   134
paul@4701
   135
    /* GL pointers */
gabomdq@7659
   136
    device->GL_LoadLibrary = Android_GLES_LoadLibrary;
gabomdq@7659
   137
    device->GL_GetProcAddress = Android_GLES_GetProcAddress;
gabomdq@7659
   138
    device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
gabomdq@7659
   139
    device->GL_CreateContext = Android_GLES_CreateContext;
gabomdq@7659
   140
    device->GL_MakeCurrent = Android_GLES_MakeCurrent;
gabomdq@7659
   141
    device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
gabomdq@7659
   142
    device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
gabomdq@7659
   143
    device->GL_SwapWindow = Android_GLES_SwapWindow;
gabomdq@7659
   144
    device->GL_DeleteContext = Android_GLES_DeleteContext;
paul@4701
   145
slouken@11381
   146
#if SDL_VIDEO_VULKAN
icculus@11365
   147
    device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
icculus@11365
   148
    device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
icculus@11365
   149
    device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
icculus@11365
   150
    device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
icculus@11365
   151
#endif
icculus@11365
   152
gabomdq@9173
   153
    /* Screensaver */
gabomdq@9173
   154
    device->SuspendScreenSaver = Android_SuspendScreenSaver;
gabomdq@9173
   155
slouken@6654
   156
    /* Text input */
slouken@6654
   157
    device->StartTextInput = Android_StartTextInput;
slouken@6654
   158
    device->StopTextInput = Android_StopTextInput;
slouken@6654
   159
    device->SetTextInputRect = Android_SetTextInputRect;
slouken@6654
   160
slouken@6392
   161
    /* Screen keyboard */
philipp@7135
   162
    device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
philipp@7135
   163
    device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
slouken@6392
   164
slouken@6464
   165
    /* Clipboard */
slouken@6464
   166
    device->SetClipboardText = Android_SetClipboardText;
slouken@6464
   167
    device->GetClipboardText = Android_GetClipboardText;
slouken@6464
   168
    device->HasClipboardText = Android_HasClipboardText;
slouken@6464
   169
paul@4701
   170
    return device;
paul@4701
   171
}
paul@4701
   172
paul@4701
   173
VideoBootStrap Android_bootstrap = {
paul@4701
   174
    ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
paul@4701
   175
    Android_Available, Android_CreateDevice
paul@4701
   176
};
paul@4701
   177
paul@4701
   178
paul@4701
   179
int
paul@4701
   180
Android_VideoInit(_THIS)
paul@4701
   181
{
slouken@12625
   182
    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
slouken@12625
   183
    int display_index;
slouken@12625
   184
    SDL_VideoDisplay *display;
slouken@12625
   185
    SDL_DisplayMode mode;
sylvain@12552
   186
sylvain@12552
   187
    videodata->isPaused  = SDL_FALSE;
sylvain@12552
   188
    videodata->isPausing = SDL_FALSE;
paul@4701
   189
sylvain@12556
   190
    mode.format          = Android_ScreenFormat;
sylvain@12556
   191
    mode.w               = Android_DeviceWidth;
sylvain@12556
   192
    mode.h               = Android_DeviceHeight;
sylvain@12556
   193
    mode.refresh_rate    = Android_ScreenRate;
sylvain@12556
   194
    mode.driverdata      = NULL;
sylvain@12556
   195
slouken@12625
   196
    display_index = SDL_AddBasicVideoDisplay(&mode);
slouken@12625
   197
    if (display_index < 0) {
paul@4701
   198
        return -1;
paul@4701
   199
    }
slouken@12625
   200
    display = SDL_GetDisplay(display_index);
slouken@12625
   201
    display->orientation = Android_JNI_GetDisplayOrientation();    
paul@4701
   202
paul@4701
   203
    SDL_AddDisplayMode(&_this->displays[0], &mode);
paul@4701
   204
slouken@4980
   205
    Android_InitKeyboard();
paul@4713
   206
philipp@7786
   207
    Android_InitTouch();
philipp@7786
   208
philipp@10051
   209
    Android_InitMouse();
philipp@10051
   210
paul@4701
   211
    /* We're done! */
paul@4701
   212
    return 0;
paul@4701
   213
}
paul@4701
   214
paul@4701
   215
void
paul@4701
   216
Android_VideoQuit(_THIS)
paul@4701
   217
{
slouken@12025
   218
    Android_QuitMouse();
icculus@9441
   219
    Android_QuitTouch();
paul@4701
   220
}
paul@4701
   221
slouken@11662
   222
int
sylvain@12494
   223
Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
slouken@11662
   224
{
slouken@11662
   225
    return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
slouken@11662
   226
}
slouken@11662
   227
slouken@4981
   228
void
sylvain@12556
   229
Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
slouken@4981
   230
{
sylvain@12556
   231
    Android_SurfaceWidth  = surfaceWidth;
slouken@12012
   232
    Android_SurfaceHeight = surfaceHeight;
sylvain@12556
   233
    Android_DeviceWidth   = deviceWidth;
sylvain@12556
   234
    Android_DeviceHeight  = deviceHeight;
sylvain@12556
   235
    Android_ScreenFormat  = format;
sylvain@12556
   236
    Android_ScreenRate    = (int)rate;
sylvain@12556
   237
}
slouken@5001
   238
sylvain@12556
   239
void Android_SendResize(SDL_Window *window)
sylvain@12556
   240
{
mbvett@10438
   241
    /*
mbvett@10438
   242
      Update the resolution of the desktop mode, so that the window
mbvett@10438
   243
      can be properly resized. The screen resolution change can for
slouken@11865
   244
      example happen when the Activity enters or exits immersive mode,
mbvett@10438
   245
      which can happen after VideoInit().
mbvett@10438
   246
    */
sylvain@12556
   247
    SDL_VideoDevice *device = SDL_GetVideoDevice();
mbvett@10438
   248
    if (device && device->num_displays > 0)
mbvett@10438
   249
    {
sylvain@12556
   250
        SDL_VideoDisplay *display          = &device->displays[0];
sylvain@12556
   251
        display->desktop_mode.format       = Android_ScreenFormat;
sylvain@12556
   252
        display->desktop_mode.w            = Android_DeviceWidth;
sylvain@12556
   253
        display->desktop_mode.h            = Android_DeviceHeight;
sylvain@12556
   254
        display->desktop_mode.refresh_rate = Android_ScreenRate;
mbvett@10438
   255
    }
mbvett@10438
   256
sylvain@12494
   257
    if (window) {
slouken@10475
   258
        /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
slouken@10475
   259
         * will fall back to the old mode */
sylvain@12556
   260
        SDL_VideoDisplay *display              = SDL_GetDisplayForWindow(window);
sylvain@12556
   261
        display->display_modes[0].format       = Android_ScreenFormat;
sylvain@12556
   262
        display->display_modes[0].w            = Android_DeviceWidth;
sylvain@12556
   263
        display->display_modes[0].h            = Android_DeviceHeight;
sylvain@12556
   264
        display->display_modes[0].refresh_rate = Android_ScreenRate;
sylvain@12556
   265
        display->current_mode                  = display->display_modes[0];
slouken@10475
   266
sylvain@12556
   267
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, Android_SurfaceWidth, Android_SurfaceHeight);
slouken@5001
   268
    }
paul@4716
   269
}
paul@4716
   270
slouken@6044
   271
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   272
paul@4701
   273
/* vi: set ts=4 sw=4 expandtab: */