src/video/android/SDL_androidvideo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Jun 2014 09:01:26 -0700
changeset 8799 e2fd743d84ea
parent 8149 681eb46b8ac4
child 9173 c677ab1148c5
permissions -rw-r--r--
Added a way to get the native Android window and EGL context
paul@4701
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@6464
    36
#include "SDL_androidclipboard.h"
paul@4703
    37
#include "SDL_androidevents.h"
slouken@4980
    38
#include "SDL_androidkeyboard.h"
philipp@7786
    39
#include "SDL_androidtouch.h"
slouken@4998
    40
#include "SDL_androidwindow.h"
paul@4701
    41
paul@4701
    42
#define ANDROID_VID_DRIVER_NAME "Android"
paul@4701
    43
paul@4701
    44
/* Initialization/Query functions */
paul@4701
    45
static int Android_VideoInit(_THIS);
paul@4701
    46
static void Android_VideoQuit(_THIS);
paul@4701
    47
gabomdq@8021
    48
#include "../SDL_egl_c.h"
paul@4701
    49
/* GL functions (SDL_androidgl.c) */
gabomdq@7659
    50
extern SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window * window);
gabomdq@7659
    51
extern int Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
gabomdq@7659
    52
extern void Android_GLES_SwapWindow(_THIS, SDL_Window * window);
gabomdq@7659
    53
extern int Android_GLES_LoadLibrary(_THIS, const char *path);
gabomdq@7659
    54
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
gabomdq@7659
    55
#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
gabomdq@7659
    56
#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
gabomdq@7659
    57
#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
gabomdq@7659
    58
#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
paul@4701
    59
paul@4701
    60
/* Android driver bootstrap functions */
paul@4701
    61
paul@4701
    62
slouken@7191
    63
/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
slouken@4998
    64
int Android_ScreenWidth = 0;
slouken@4998
    65
int Android_ScreenHeight = 0;
slouken@4998
    66
Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
gabomdq@6330
    67
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
paul@4716
    68
slouken@5001
    69
/* Currently only one window */
slouken@5001
    70
SDL_Window *Android_Window = 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
paul@4701
    79
Android_DeleteDevice(SDL_VideoDevice * device)
paul@4701
    80
{
paul@4701
    81
    SDL_free(device);
paul@4701
    82
}
paul@4701
    83
paul@4701
    84
static SDL_VideoDevice *
paul@4701
    85
Android_CreateDevice(int devindex)
paul@4701
    86
{
paul@4701
    87
    SDL_VideoDevice *device;
slouken@6555
    88
    SDL_VideoData *data;
paul@4701
    89
paul@4701
    90
    /* Initialize all variables that we clean on shutdown */
paul@4701
    91
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
paul@4701
    92
    if (!device) {
paul@4701
    93
        SDL_OutOfMemory();
slouken@6555
    94
        return NULL;
paul@4701
    95
    }
paul@4701
    96
slouken@6555
    97
    data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@6555
    98
    if (!data) {
slouken@6555
    99
        SDL_OutOfMemory();
slouken@6555
   100
        SDL_free(device);
slouken@6555
   101
        return NULL;
slouken@6555
   102
    }
slouken@6555
   103
slouken@6555
   104
    device->driverdata = data;
slouken@6555
   105
paul@4701
   106
    /* Set the function pointers */
paul@4701
   107
    device->VideoInit = Android_VideoInit;
paul@4701
   108
    device->VideoQuit = Android_VideoQuit;
paul@4701
   109
    device->PumpEvents = Android_PumpEvents;
slouken@4980
   110
slouken@4998
   111
    device->CreateWindow = Android_CreateWindow;
slouken@4998
   112
    device->SetWindowTitle = Android_SetWindowTitle;
slouken@4998
   113
    device->DestroyWindow = Android_DestroyWindow;
slouken@8799
   114
    device->GetWindowWMInfo = Android_GetWindowWMInfo;
slouken@4998
   115
paul@4701
   116
    device->free = Android_DeleteDevice;
paul@4701
   117
paul@4701
   118
    /* GL pointers */
gabomdq@7659
   119
    device->GL_LoadLibrary = Android_GLES_LoadLibrary;
gabomdq@7659
   120
    device->GL_GetProcAddress = Android_GLES_GetProcAddress;
gabomdq@7659
   121
    device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
gabomdq@7659
   122
    device->GL_CreateContext = Android_GLES_CreateContext;
gabomdq@7659
   123
    device->GL_MakeCurrent = Android_GLES_MakeCurrent;
gabomdq@7659
   124
    device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
gabomdq@7659
   125
    device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
gabomdq@7659
   126
    device->GL_SwapWindow = Android_GLES_SwapWindow;
gabomdq@7659
   127
    device->GL_DeleteContext = Android_GLES_DeleteContext;
paul@4701
   128
slouken@6654
   129
    /* Text input */
slouken@6654
   130
    device->StartTextInput = Android_StartTextInput;
slouken@6654
   131
    device->StopTextInput = Android_StopTextInput;
slouken@6654
   132
    device->SetTextInputRect = Android_SetTextInputRect;
slouken@6654
   133
slouken@6392
   134
    /* Screen keyboard */
philipp@7135
   135
    device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
philipp@7135
   136
    device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
slouken@6392
   137
slouken@6464
   138
    /* Clipboard */
slouken@6464
   139
    device->SetClipboardText = Android_SetClipboardText;
slouken@6464
   140
    device->GetClipboardText = Android_GetClipboardText;
slouken@6464
   141
    device->HasClipboardText = Android_HasClipboardText;
slouken@6464
   142
paul@4701
   143
    return device;
paul@4701
   144
}
paul@4701
   145
paul@4701
   146
VideoBootStrap Android_bootstrap = {
paul@4701
   147
    ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
paul@4701
   148
    Android_Available, Android_CreateDevice
paul@4701
   149
};
paul@4701
   150
paul@4701
   151
paul@4701
   152
int
paul@4701
   153
Android_VideoInit(_THIS)
paul@4701
   154
{
paul@4701
   155
    SDL_DisplayMode mode;
paul@4701
   156
slouken@4998
   157
    mode.format = Android_ScreenFormat;
slouken@4998
   158
    mode.w = Android_ScreenWidth;
slouken@4998
   159
    mode.h = Android_ScreenHeight;
paul@4701
   160
    mode.refresh_rate = 0;
paul@4701
   161
    mode.driverdata = NULL;
paul@4701
   162
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
paul@4701
   163
        return -1;
paul@4701
   164
    }
paul@4701
   165
paul@4701
   166
    SDL_AddDisplayMode(&_this->displays[0], &mode);
paul@4701
   167
slouken@4980
   168
    Android_InitKeyboard();
paul@4713
   169
philipp@7786
   170
    Android_InitTouch();
philipp@7786
   171
paul@4701
   172
    /* We're done! */
paul@4701
   173
    return 0;
paul@4701
   174
}
paul@4701
   175
paul@4701
   176
void
paul@4701
   177
Android_VideoQuit(_THIS)
paul@4701
   178
{
paul@4701
   179
}
paul@4701
   180
slouken@4998
   181
/* This function gets called before VideoInit() */
slouken@4981
   182
void
slouken@4981
   183
Android_SetScreenResolution(int width, int height, Uint32 format)
slouken@4981
   184
{
slouken@4998
   185
    Android_ScreenWidth = width;
slouken@7191
   186
    Android_ScreenHeight = height;
slouken@4998
   187
    Android_ScreenFormat = format;
slouken@5001
   188
slouken@5001
   189
    if (Android_Window) {
slouken@5001
   190
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
slouken@5001
   191
    }
paul@4716
   192
}
paul@4716
   193
slouken@6044
   194
#endif /* SDL_VIDEO_DRIVER_ANDROID */
slouken@6044
   195
paul@4701
   196
/* vi: set ts=4 sw=4 expandtab: */