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