src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:33:28 -0800
changeset 10737 3406a0f8b041
parent 10672 10fb0ebc4fbf
child 10937 9ff840e45cfb
permissions -rw-r--r--
Updated copyright for 2017
paul@4701
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
paul@4701
    25
/* Android SDL video driver implementation
paul@4701
    26
*/
paul@4701
    27
paul@4701
    28
#include "SDL_video.h"
paul@4701
    29
#include "SDL_mouse.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"
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);
paul@4701
    49
gabomdq@8021
    50
#include "../SDL_egl_c.h"
gabomdq@7659
    51
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
gabomdq@7659
    52
#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
gabomdq@7659
    53
#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
gabomdq@7659
    54
#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
gabomdq@7659
    55
#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
paul@4701
    56
paul@4701
    57
/* Android driver bootstrap functions */
paul@4701
    58
paul@4701
    59
slouken@7191
    60
/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
slouken@4998
    61
int Android_ScreenWidth = 0;
slouken@4998
    62
int Android_ScreenHeight = 0;
slouken@4998
    63
Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
slouken@10672
    64
static int Android_ScreenRate = 0;
philipp@9314
    65
gabomdq@6330
    66
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
paul@4716
    67
slouken@5001
    68
/* Currently only one window */
slouken@5001
    69
SDL_Window *Android_Window = NULL;
paul@4716
    70
paul@4701
    71
static int
paul@4701
    72
Android_Available(void)
paul@4701
    73
{
paul@4701
    74
    return 1;
paul@4701
    75
}
paul@4701
    76
paul@4701
    77
static void
gabomdq@9173
    78
Android_SuspendScreenSaver(_THIS)
gabomdq@9173
    79
{
gabomdq@9173
    80
    Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
gabomdq@9173
    81
}
gabomdq@9173
    82
gabomdq@9173
    83
static void
paul@4701
    84
Android_DeleteDevice(SDL_VideoDevice * device)
paul@4701
    85
{
philipp@9317
    86
    SDL_free(device->driverdata);
paul@4701
    87
    SDL_free(device);
paul@4701
    88
}
paul@4701
    89
paul@4701
    90
static SDL_VideoDevice *
paul@4701
    91
Android_CreateDevice(int devindex)
paul@4701
    92
{
paul@4701
    93
    SDL_VideoDevice *device;
slouken@6555
    94
    SDL_VideoData *data;
paul@4701
    95
paul@4701
    96
    /* Initialize all variables that we clean on shutdown */
paul@4701
    97
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
paul@4701
    98
    if (!device) {
paul@4701
    99
        SDL_OutOfMemory();
slouken@6555
   100
        return NULL;
paul@4701
   101
    }
paul@4701
   102
slouken@6555
   103
    data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@6555
   104
    if (!data) {
slouken@6555
   105
        SDL_OutOfMemory();
slouken@6555
   106
        SDL_free(device);
slouken@6555
   107
        return NULL;
slouken@6555
   108
    }
slouken@6555
   109
slouken@6555
   110
    device->driverdata = data;
slouken@6555
   111
paul@4701
   112
    /* Set the function pointers */
paul@4701
   113
    device->VideoInit = Android_VideoInit;
paul@4701
   114
    device->VideoQuit = Android_VideoQuit;
paul@4701
   115
    device->PumpEvents = Android_PumpEvents;
slouken@4980
   116
slouken@4998
   117
    device->CreateWindow = Android_CreateWindow;
slouken@4998
   118
    device->SetWindowTitle = Android_SetWindowTitle;
slouken@4998
   119
    device->DestroyWindow = Android_DestroyWindow;
slouken@8799
   120
    device->GetWindowWMInfo = Android_GetWindowWMInfo;
slouken@4998
   121
paul@4701
   122
    device->free = Android_DeleteDevice;
paul@4701
   123
paul@4701
   124
    /* GL pointers */
gabomdq@7659
   125
    device->GL_LoadLibrary = Android_GLES_LoadLibrary;
gabomdq@7659
   126
    device->GL_GetProcAddress = Android_GLES_GetProcAddress;
gabomdq@7659
   127
    device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
gabomdq@7659
   128
    device->GL_CreateContext = Android_GLES_CreateContext;
gabomdq@7659
   129
    device->GL_MakeCurrent = Android_GLES_MakeCurrent;
gabomdq@7659
   130
    device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
gabomdq@7659
   131
    device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
gabomdq@7659
   132
    device->GL_SwapWindow = Android_GLES_SwapWindow;
gabomdq@7659
   133
    device->GL_DeleteContext = Android_GLES_DeleteContext;
paul@4701
   134
gabomdq@9173
   135
    /* Screensaver */
gabomdq@9173
   136
    device->SuspendScreenSaver = Android_SuspendScreenSaver;
gabomdq@9173
   137
slouken@6654
   138
    /* Text input */
slouken@6654
   139
    device->StartTextInput = Android_StartTextInput;
slouken@6654
   140
    device->StopTextInput = Android_StopTextInput;
slouken@6654
   141
    device->SetTextInputRect = Android_SetTextInputRect;
slouken@6654
   142
slouken@6392
   143
    /* Screen keyboard */
philipp@7135
   144
    device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
philipp@7135
   145
    device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
slouken@6392
   146
slouken@6464
   147
    /* Clipboard */
slouken@6464
   148
    device->SetClipboardText = Android_SetClipboardText;
slouken@6464
   149
    device->GetClipboardText = Android_GetClipboardText;
slouken@6464
   150
    device->HasClipboardText = Android_HasClipboardText;
slouken@6464
   151
paul@4701
   152
    return device;
paul@4701
   153
}
paul@4701
   154
paul@4701
   155
VideoBootStrap Android_bootstrap = {
paul@4701
   156
    ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
paul@4701
   157
    Android_Available, Android_CreateDevice
paul@4701
   158
};
paul@4701
   159
paul@4701
   160
paul@4701
   161
int
paul@4701
   162
Android_VideoInit(_THIS)
paul@4701
   163
{
paul@4701
   164
    SDL_DisplayMode mode;
paul@4701
   165
slouken@4998
   166
    mode.format = Android_ScreenFormat;
slouken@4998
   167
    mode.w = Android_ScreenWidth;
slouken@4998
   168
    mode.h = Android_ScreenHeight;
philipp@9314
   169
    mode.refresh_rate = Android_ScreenRate;
paul@4701
   170
    mode.driverdata = NULL;
paul@4701
   171
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
paul@4701
   172
        return -1;
paul@4701
   173
    }
paul@4701
   174
paul@4701
   175
    SDL_AddDisplayMode(&_this->displays[0], &mode);
paul@4701
   176
slouken@4980
   177
    Android_InitKeyboard();
paul@4713
   178
philipp@7786
   179
    Android_InitTouch();
philipp@7786
   180
philipp@10051
   181
    Android_InitMouse();
philipp@10051
   182
paul@4701
   183
    /* We're done! */
paul@4701
   184
    return 0;
paul@4701
   185
}
paul@4701
   186
paul@4701
   187
void
paul@4701
   188
Android_VideoQuit(_THIS)
paul@4701
   189
{
icculus@9441
   190
    Android_QuitTouch();
paul@4701
   191
}
paul@4701
   192
slouken@4981
   193
void
philipp@9314
   194
Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
slouken@4981
   195
{
slouken@4998
   196
    Android_ScreenWidth = width;
slouken@7191
   197
    Android_ScreenHeight = height;
slouken@4998
   198
    Android_ScreenFormat = format;
philipp@9314
   199
    Android_ScreenRate = rate;
slouken@5001
   200
mbvett@10438
   201
    /*
mbvett@10438
   202
      Update the resolution of the desktop mode, so that the window
mbvett@10438
   203
      can be properly resized. The screen resolution change can for
mbvett@10438
   204
      example happen when the Activity enters or exists immersive mode,
mbvett@10438
   205
      which can happen after VideoInit().
mbvett@10438
   206
    */
mbvett@10438
   207
    SDL_VideoDevice* device = SDL_GetVideoDevice();
mbvett@10438
   208
    if (device && device->num_displays > 0)
mbvett@10438
   209
    {
mbvett@10438
   210
        SDL_VideoDisplay* display = &device->displays[0];
mbvett@10438
   211
        display->desktop_mode.format = Android_ScreenFormat;
mbvett@10438
   212
        display->desktop_mode.w = Android_ScreenWidth;
mbvett@10438
   213
        display->desktop_mode.h = Android_ScreenHeight;
mbvett@10438
   214
        display->desktop_mode.refresh_rate  = Android_ScreenRate;
mbvett@10438
   215
    }
mbvett@10438
   216
slouken@5001
   217
    if (Android_Window) {
slouken@5001
   218
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
slouken@10475
   219
slouken@10475
   220
        /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
slouken@10475
   221
         * will fall back to the old mode */
slouken@10475
   222
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(Android_Window);
slouken@10475
   223
slouken@10475
   224
        display->current_mode.format = format;
slouken@10475
   225
        display->current_mode.w = width;
slouken@10475
   226
        display->current_mode.h = height;
slouken@10475
   227
        display->current_mode.refresh_rate = rate;
slouken@5001
   228
    }
paul@4716
   229
}
paul@4716
   230
slouken@6044
   231
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   232
paul@4701
   233
/* vi: set ts=4 sw=4 expandtab: */