SDL  2.0
SDL_androidevents.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_ANDROID
24 
25 #include "SDL_androidevents.h"
26 #include "SDL_events.h"
27 #include "SDL_androidkeyboard.h"
28 #include "SDL_androidwindow.h"
29 
30 /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
31  * because of THIS redefinition */
32 
33 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
34 extern void ANDROIDAUDIO_ResumeDevices(void);
35 extern void ANDROIDAUDIO_PauseDevices(void);
36 #else
37 static void ANDROIDAUDIO_ResumeDevices(void) {}
38 static void ANDROIDAUDIO_PauseDevices(void) {}
39 #endif
40 
41 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES
42 extern void openslES_ResumeDevices(void);
43 extern void openslES_PauseDevices(void);
44 #else
45 static void openslES_ResumeDevices(void) {}
46 static void openslES_PauseDevices(void) {}
47 #endif
48 
49 /* Number of 'type' events in the event queue */
50 static int
51 SDL_NumberOfEvents(Uint32 type)
52 {
54 }
55 
56 static void
57 android_egl_context_restore(SDL_Window *window)
58 {
59  if (window) {
61  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
62  if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
63  /* The context is no longer valid, create a new one */
66  event.type = SDL_RENDER_DEVICE_RESET;
68  }
69  }
70 }
71 
72 static void
73 android_egl_context_backup(SDL_Window *window)
74 {
75  if (window) {
76  /* Keep a copy of the EGL Context so we can try to restore it when we resume */
77  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
79  /* We need to do this so the EGLSurface can be freed */
81  }
82 }
83 
84 
85 /*
86  * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
87  * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted.
88  *
89  * No polling necessary
90  */
91 
92 void
94 {
96 
97  if (videodata->isPaused) {
98 
99  /* Make sure this is the last thing we do before pausing */
101  android_egl_context_backup(Android_Window);
103 
106 
107  if (SDL_SemWait(Android_ResumeSem) == 0) {
108 
109  videodata->isPaused = 0;
110 
113 
114  /* Restore the GL Context from here, as this operation is thread dependent */
115  if (!SDL_HasEvent(SDL_QUIT)) {
117  android_egl_context_restore(Android_Window);
119  }
120 
121  /* Make sure SW Keyboard is restored when an app becomes foreground */
122  if (SDL_IsTextInputActive()) {
123  Android_StartTextInput(_this); /* Only showTextInput */
124  }
125  }
126  } else {
127  if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
128  /* We've been signaled to pause (potentially several times), but before we block ourselves,
129  * we need to make sure that the very last event (of the first pause sequence, if several)
130  * has reached the app */
131  if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
132  videodata->isPausing = 1;
133  } else {
134  videodata->isPausing = 0;
135  videodata->isPaused = 1;
136  }
137  }
138  }
139 }
140 
141 void
143 {
144  SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
145  static int backup_context = 0;
146 
147  if (videodata->isPaused) {
148 
149  if (backup_context) {
150 
152  android_egl_context_backup(Android_Window);
154 
157 
158  backup_context = 0;
159  }
160 
161 
162  if (SDL_SemTryWait(Android_ResumeSem) == 0) {
163 
164  videodata->isPaused = 0;
165 
168 
169  /* Restore the GL Context from here, as this operation is thread dependent */
170  if (!SDL_HasEvent(SDL_QUIT)) {
172  android_egl_context_restore(Android_Window);
174  }
175 
176  /* Make sure SW Keyboard is restored when an app becomes foreground */
177  if (SDL_IsTextInputActive()) {
178  Android_StartTextInput(_this); /* Only showTextInput */
179  }
180  }
181  } else {
182  if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
183  /* We've been signaled to pause (potentially several times), but before we block ourselves,
184  * we need to make sure that the very last event (of the first pause sequence, if several)
185  * has reached the app */
186  if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
187  videodata->isPausing = 1;
188  } else {
189  videodata->isPausing = 0;
190  videodata->isPaused = 1;
191  backup_context = 1;
192  }
193  }
194  }
195 }
196 
197 #endif /* SDL_VIDEO_DRIVER_ANDROID */
198 
199 /* vi: set ts=4 sw=4 expandtab: */
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
EGLContext
void * EGLContext
Definition: egl.h:60
SDL_events.h
SDL_VideoDevice::driverdata
void * driverdata
Definition: SDL_sysvideo.h:381
SDL_androidevents.h
SDL_APP_DIDENTERBACKGROUND
@ SDL_APP_DIDENTERBACKGROUND
Definition: SDL_events.h:75
Android_ActivityMutex
SDL_mutex * Android_ActivityMutex
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
SDL_GL_CreateContext
#define SDL_GL_CreateContext
Definition: SDL_dynapi_overrides.h:559
SDL_WindowData
Definition: SDL_androidwindow.h:38
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_SemTryWait
#define SDL_SemTryWait
Definition: SDL_dynapi_overrides.h:267
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_androidwindow.h
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
Android_PumpEvents_NonBlocking
void Android_PumpEvents_NonBlocking(_THIS)
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:125
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_PeepEvents
#define SDL_PeepEvents
Definition: SDL_dynapi_overrides.h:117
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_VideoData::isPausing
int isPausing
Definition: SDL_androidvideo.h:40
SDL_GL_GetCurrentContext
#define SDL_GL_GetCurrentContext
Definition: SDL_dynapi_overrides.h:562
SDL_VideoData::isPaused
int isPaused
Definition: SDL_androidvideo.h:39
Android_PauseSem
SDL_sem * Android_PauseSem
ANDROIDAUDIO_PauseDevices
void ANDROIDAUDIO_PauseDevices(void)
Definition: SDL_androidaudio.c:206
Android_PumpEvents_Blocking
void Android_PumpEvents_Blocking(_THIS)
SDL_GLContext
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:193
SDL_SemWait
#define SDL_SemWait
Definition: SDL_dynapi_overrides.h:266
SDL_WindowData::egl_context
EGLContext egl_context
Definition: SDL_androidwindow.h:41
SDL_RENDER_DEVICE_RESET
@ SDL_RENDER_DEVICE_RESET
Definition: SDL_events.h:155
SDL_GL_MakeCurrent
#define SDL_GL_MakeCurrent
Definition: SDL_dynapi_overrides.h:560
ANDROIDAUDIO_ResumeDevices
void ANDROIDAUDIO_ResumeDevices(void)
Definition: SDL_androidaudio.c:205
SDL_HasEvent
#define SDL_HasEvent
Definition: SDL_dynapi_overrides.h:118
SDL_SemValue
#define SDL_SemValue
Definition: SDL_dynapi_overrides.h:270
openslES_ResumeDevices
void openslES_ResumeDevices(void)
SDL_androidkeyboard.h
SDL_Event
General event structure.
Definition: SDL_events.h:557
SDL_IsTextInputActive
#define SDL_IsTextInputActive
Definition: SDL_dynapi_overrides.h:227
Android_StartTextInput
void Android_StartTextInput(_THIS)
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
type
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
Android_Window
SDL_Window * Android_Window
SDL_PEEKEVENT
@ SDL_PEEKEVENT
Definition: SDL_events.h:616
SDL_VideoData
Definition: SDL_androidvideo.h:36
openslES_PauseDevices
void openslES_PauseDevices(void)
Android_ResumeSem
SDL_sem * Android_ResumeSem
Definition: SDL_androidvideo.h:45