Android: preparation bug 4142, reduce usage of global variable Android_Window

This commit is contained in:
Sylvain Becker
2019-01-03 13:14:16 +01:00
parent a95f91bcea
commit 5dc25fef3b
10 changed files with 194 additions and 205 deletions

View File

@@ -40,27 +40,27 @@ static void ANDROIDAUDIO_PauseDevices(void) {}
#endif
static void
android_egl_context_restore()
android_egl_context_restore(SDL_Window *window)
{
SDL_Event event;
SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
/* The context is no longer valid, create a new one */
data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
event.type = SDL_RENDER_DEVICE_RESET;
SDL_PushEvent(&event);
}
}
static void
android_egl_context_backup()
android_egl_context_backup(SDL_Window *window)
{
/* Keep a copy of the EGL Context so we can try to restore it when we resume */
SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
data->egl_context = SDL_GL_GetCurrentContext();
/* We need to do this so the EGLSurface can be freed */
SDL_GL_MakeCurrent(Android_Window, NULL);
SDL_GL_MakeCurrent(window, NULL);
}
void
@@ -80,24 +80,24 @@ Android_PumpEvents(_THIS)
#if SDL_ANDROID_BLOCK_ON_PAUSE
if (isPaused && !isPausing) {
/* Make sure this is the last thing we do before pausing */
android_egl_context_backup();
android_egl_context_backup(Android_Window);
ANDROIDAUDIO_PauseDevices();
if(SDL_SemWait(Android_ResumeSem) == 0) {
if (SDL_SemWait(Android_ResumeSem) == 0) {
#else
if (isPaused) {
if(SDL_SemTryWait(Android_ResumeSem) == 0) {
if (SDL_SemTryWait(Android_ResumeSem) == 0) {
#endif
isPaused = 0;
ANDROIDAUDIO_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) {
android_egl_context_restore();
android_egl_context_restore(Android_Window);
}
}
}
else {
#if SDL_ANDROID_BLOCK_ON_PAUSE
if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
if (isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
/* We've been signaled to pause, but before we block ourselves,
we need to make sure that certain key events have reached the app */
if (SDL_HasEvent(SDL_WINDOWEVENT) || SDL_HasEvent(SDL_APP_WILLENTERBACKGROUND) || SDL_HasEvent(SDL_APP_DIDENTERBACKGROUND) ) {
@@ -109,8 +109,8 @@ Android_PumpEvents(_THIS)
}
}
#else
if(SDL_SemTryWait(Android_PauseSem) == 0) {
android_egl_context_backup();
if (SDL_SemTryWait(Android_PauseSem) == 0) {
android_egl_context_backup(Android_Window);
ANDROIDAUDIO_PauseDevices();
isPaused = 1;
}

View File

@@ -62,7 +62,7 @@ Android_WrapCursor(int custom_cursor, int system_cursor)
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)SDL_calloc(1, sizeof(*data));
SDL_AndroidCursorData *data = (SDL_AndroidCursorData *)SDL_calloc(1, sizeof(*data));
if (data) {
data->custom_cursor = custom_cursor;
data->system_cursor = system_cursor;
@@ -141,13 +141,13 @@ Android_DestroyEmptyCursor()
}
static int
Android_ShowCursor(SDL_Cursor * cursor)
Android_ShowCursor(SDL_Cursor *cursor)
{
if (!cursor) {
cursor = Android_CreateEmptyCursor();
}
if (cursor) {
SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata;
SDL_AndroidCursorData *data = (SDL_AndroidCursorData *)cursor->driverdata;
if (data->custom_cursor) {
if (!Android_JNI_SetCustomCursor(data->custom_cursor)) {
return SDL_Unsupported();
@@ -220,12 +220,12 @@ TranslateButton(int state)
}
void
Android_OnMouse(int state, int action, float x, float y, SDL_bool relative)
Android_OnMouse(SDL_Window *window, int state, int action, float x, float y, SDL_bool relative)
{
int changes;
Uint8 button;
if (!Android_Window) {
if (!window) {
return;
}
@@ -234,25 +234,25 @@ Android_OnMouse(int state, int action, float x, float y, SDL_bool relative)
changes = state & ~last_state;
button = TranslateButton(changes);
last_state = state;
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(Android_Window, 0, SDL_PRESSED, button);
SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
break;
case ACTION_UP:
changes = last_state & ~state;
button = TranslateButton(changes);
last_state = state;
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(Android_Window, 0, SDL_RELEASED, button);
SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
break;
case ACTION_MOVE:
case ACTION_HOVER_MOVE:
SDL_SendMouseMotion(Android_Window, 0, relative, (int)x, (int)y);
SDL_SendMouseMotion(window, 0, relative, (int)x, (int)y);
break;
case ACTION_SCROLL:
SDL_SendMouseWheel(Android_Window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
break;
default:

View File

@@ -25,7 +25,7 @@
#include "SDL_androidvideo.h"
extern void Android_InitMouse(void);
extern void Android_OnMouse(int button, int action, float x, float y, SDL_bool relative);
extern void Android_OnMouse(SDL_Window *window, int button, int action, float x, float y, SDL_bool relative);
extern void Android_QuitMouse(void);
#endif /* SDL_androidmouse_h_ */

View File

@@ -40,12 +40,12 @@
#define ACTION_POINTER_DOWN 5
#define ACTION_POINTER_UP 6
static void Android_GetWindowCoordinates(float x, float y,
static void Android_GetWindowCoordinates(SDL_Window *window, float x, float y,
int *window_x, int *window_y)
{
int window_w, window_h;
SDL_GetWindowSize(Android_Window, &window_w, &window_h);
SDL_GetWindowSize(window, &window_w, &window_h);
*window_x = (int)(x * window_w);
*window_y = (int)(y * window_h);
}
@@ -64,7 +64,7 @@ SeparateEventsHintWatcher(void *userdata, const char *name,
void Android_InitTouch(void)
{
int i;
int* ids;
int *ids;
const int number = Android_JNI_GetTouchDeviceIds(&ids);
SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
@@ -85,14 +85,14 @@ void Android_QuitTouch(void)
separate_mouse_and_touch = SDL_FALSE;
}
void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
{
SDL_TouchID touchDeviceId = 0;
SDL_FingerID fingerId = 0;
int window_x, window_y;
static SDL_FingerID pointerFingerID = 0;
if (!Android_Window) {
if (!window) {
return;
}
@@ -106,11 +106,11 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
case ACTION_DOWN:
/* Primary pointer down */
if (!separate_mouse_and_touch) {
Android_GetWindowCoordinates(x, y, &window_x, &window_y);
Android_GetWindowCoordinates(window, x, y, &window_x, &window_y);
/* send moved event */
SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
/* send mouse down event */
SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
}
pointerFingerID = fingerId;
case ACTION_POINTER_DOWN:
@@ -121,9 +121,9 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
case ACTION_MOVE:
if (!pointerFingerID) {
if (!separate_mouse_and_touch) {
Android_GetWindowCoordinates(x, y, &window_x, &window_y);
Android_GetWindowCoordinates(window, x, y, &window_x, &window_y);
/* send moved event */
SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
}
}
SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
@@ -133,7 +133,7 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
/* Primary pointer up */
if (!separate_mouse_and_touch) {
/* send mouse up */
SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
}
pointerFingerID = (SDL_FingerID) 0;
case ACTION_POINTER_UP:

View File

@@ -24,6 +24,6 @@
extern void Android_InitTouch(void);
extern void Android_QuitTouch(void);
extern void Android_OnTouch( int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p);
extern void Android_OnTouch(SDL_Window *window, int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p);
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -22,8 +22,7 @@
#if SDL_VIDEO_DRIVER_ANDROID
/* Android SDL video driver implementation
*/
/* Android SDL video driver implementation */
#include "SDL_video.h"
#include "SDL_mouse.h"
@@ -47,7 +46,7 @@
/* Initialization/Query functions */
static int Android_VideoInit(_THIS);
static void Android_VideoQuit(_THIS);
int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi);
int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
#include "../SDL_egl_c.h"
#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
@@ -69,9 +68,6 @@ static int Android_ScreenRate = 0;
SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
/* Currently only one window */
SDL_Window *Android_Window = NULL;
static int
Android_Available(void)
{
@@ -85,7 +81,7 @@ Android_SuspendScreenSaver(_THIS)
}
static void
Android_DeleteDevice(SDL_VideoDevice * device)
Android_DeleteDevice(SDL_VideoDevice *device)
{
SDL_free(device->driverdata);
SDL_free(device);
@@ -104,7 +100,7 @@ Android_CreateDevice(int devindex)
return NULL;
}
data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
if (!data) {
SDL_OutOfMemory();
SDL_free(device);
@@ -206,15 +202,15 @@ Android_VideoQuit(_THIS)
}
int
Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
{
return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
}
void
Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
{
SDL_VideoDevice* device;
SDL_VideoDevice *device;
SDL_VideoDisplay *display;
Android_SurfaceWidth = surfaceWidth;
Android_SurfaceHeight = surfaceHeight;
@@ -239,10 +235,10 @@ Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth
display->desktop_mode.refresh_rate = Android_ScreenRate;
}
if (Android_Window) {
if (window) {
/* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
* will fall back to the old mode */
display = SDL_GetDisplayForWindow(Android_Window);
display = SDL_GetDisplayForWindow(window);
display->display_modes[0].format = format;
display->display_modes[0].w = Android_DeviceWidth;
@@ -250,7 +246,7 @@ Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth
display->display_modes[0].refresh_rate = (int)rate;
display->current_mode = display->display_modes[0];
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
}
}

View File

@@ -28,7 +28,7 @@
#include "../SDL_sysvideo.h"
/* Called by the JNI layer when the screen changes size or format */
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
extern void Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
/* Private display data */
@@ -42,8 +42,6 @@ extern int Android_SurfaceHeight;
extern int Android_DeviceWidth;
extern int Android_DeviceHeight;
extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
extern SDL_Window *Android_Window;
#endif /* SDL_androidvideo_h_ */

View File

@@ -33,6 +33,9 @@
#include "SDL_androidwindow.h"
#include "SDL_hints.h"
/* Currently only one window */
SDL_Window *Android_Window = NULL;
int
Android_CreateWindow(_THIS, SDL_Window * window)
{
@@ -94,13 +97,13 @@ Android_CreateWindow(_THIS, SDL_Window * window)
}
void
Android_SetWindowTitle(_THIS, SDL_Window * window)
Android_SetWindowTitle(_THIS, SDL_Window *window)
{
Android_JNI_SetActivityTitle(window->title);
}
void
Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
{
/* If the window is being destroyed don't change visible state */
if (!window->is_destroying) {
@@ -117,7 +120,7 @@ Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * displ
return;
}
SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
if (!data || !data->native_window) {
return;
@@ -135,7 +138,7 @@ Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * displ
}
void
Android_DestroyWindow(_THIS, SDL_Window * window)
Android_DestroyWindow(_THIS, SDL_Window *window)
{
SDL_WindowData *data;
@@ -161,7 +164,7 @@ Android_DestroyWindow(_THIS, SDL_Window * window)
}
SDL_bool
Android_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
Android_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

View File

@@ -26,17 +26,18 @@
#include "../../core/android/SDL_android.h"
#include "../SDL_egl_c.h"
extern int Android_CreateWindow(_THIS, SDL_Window * window);
extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
extern void Android_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
extern void Android_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo * info);
extern int Android_CreateWindow(_THIS, SDL_Window *window);
extern void Android_SetWindowTitle(_THIS, SDL_Window *window);
extern void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen);
extern void Android_DestroyWindow(_THIS, SDL_Window *window);
extern SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info);
extern SDL_Window *Android_Window;
typedef struct
{
EGLSurface egl_surface;
EGLContext egl_context; /* We use this to preserve the context when losing focus */
ANativeWindow* native_window;
ANativeWindow *native_window;
} SDL_WindowData;