SDL_androidevents.c (8565B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 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 #include "../SDL_sysvideo.h" 30 #include "../../events/SDL_events_c.h" 31 32 /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h" 33 * because of THIS redefinition */ 34 35 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID 36 extern void ANDROIDAUDIO_ResumeDevices(void); 37 extern void ANDROIDAUDIO_PauseDevices(void); 38 #else 39 static void ANDROIDAUDIO_ResumeDevices(void) {} 40 static void ANDROIDAUDIO_PauseDevices(void) {} 41 #endif 42 43 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES 44 extern void openslES_ResumeDevices(void); 45 extern void openslES_PauseDevices(void); 46 #else 47 static void openslES_ResumeDevices(void) {} 48 static void openslES_PauseDevices(void) {} 49 #endif 50 51 /* Number of 'type' events in the event queue */ 52 static int 53 SDL_NumberOfEvents(Uint32 type) 54 { 55 return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type); 56 } 57 58 static void 59 android_egl_context_restore(SDL_Window *window) 60 { 61 if (window) { 62 SDL_Event event; 63 SDL_WindowData *data = (SDL_WindowData *) window->driverdata; 64 if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) { 65 /* The context is no longer valid, create a new one */ 66 data->egl_context = (EGLContext) SDL_GL_CreateContext(window); 67 SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context); 68 event.type = SDL_RENDER_DEVICE_RESET; 69 SDL_PushEvent(&event); 70 } 71 data->backup_done = 0; 72 } 73 } 74 75 static void 76 android_egl_context_backup(SDL_Window *window) 77 { 78 if (window) { 79 /* Keep a copy of the EGL Context so we can try to restore it when we resume */ 80 SDL_WindowData *data = (SDL_WindowData *) window->driverdata; 81 data->egl_context = SDL_GL_GetCurrentContext(); 82 /* We need to do this so the EGLSurface can be freed */ 83 SDL_GL_MakeCurrent(window, NULL); 84 data->backup_done = 1; 85 } 86 } 87 88 89 /* 90 * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume 91 * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted. 92 * 93 * No polling necessary 94 */ 95 96 void 97 Android_PumpEvents_Blocking(_THIS) 98 { 99 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; 100 101 if (videodata->isPaused) { 102 SDL_bool isContextExternal = SDL_IsVideoContextExternal(); 103 104 /* Make sure this is the last thing we do before pausing */ 105 if (!isContextExternal) { 106 SDL_LockMutex(Android_ActivityMutex); 107 android_egl_context_backup(Android_Window); 108 SDL_UnlockMutex(Android_ActivityMutex); 109 } 110 111 ANDROIDAUDIO_PauseDevices(); 112 openslES_PauseDevices(); 113 114 if (SDL_SemWait(Android_ResumeSem) == 0) { 115 116 videodata->isPaused = 0; 117 118 /* Android_ResumeSem was signaled */ 119 SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND); 120 SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND); 121 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0); 122 123 ANDROIDAUDIO_ResumeDevices(); 124 openslES_ResumeDevices(); 125 126 /* Restore the GL Context from here, as this operation is thread dependent */ 127 if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) { 128 SDL_LockMutex(Android_ActivityMutex); 129 android_egl_context_restore(Android_Window); 130 SDL_UnlockMutex(Android_ActivityMutex); 131 } 132 133 /* Make sure SW Keyboard is restored when an app becomes foreground */ 134 if (SDL_IsTextInputActive()) { 135 Android_StartTextInput(_this); /* Only showTextInput */ 136 } 137 } 138 } else { 139 if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) { 140 141 /* Android_PauseSem was signaled */ 142 if (videodata->isPausing == 0) { 143 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); 144 SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND); 145 SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND); 146 } 147 148 /* We've been signaled to pause (potentially several times), but before we block ourselves, 149 * we need to make sure that the very last event (of the first pause sequence, if several) 150 * has reached the app */ 151 if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) { 152 videodata->isPausing = 1; 153 } else { 154 videodata->isPausing = 0; 155 videodata->isPaused = 1; 156 } 157 } 158 } 159 } 160 161 void 162 Android_PumpEvents_NonBlocking(_THIS) 163 { 164 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; 165 static int backup_context = 0; 166 167 if (videodata->isPaused) { 168 169 SDL_bool isContextExternal = SDL_IsVideoContextExternal(); 170 if (backup_context) { 171 172 if (!isContextExternal) { 173 SDL_LockMutex(Android_ActivityMutex); 174 android_egl_context_backup(Android_Window); 175 SDL_UnlockMutex(Android_ActivityMutex); 176 } 177 178 if (videodata->pauseAudio) { 179 ANDROIDAUDIO_PauseDevices(); 180 openslES_PauseDevices(); 181 } 182 183 backup_context = 0; 184 } 185 186 187 if (SDL_SemTryWait(Android_ResumeSem) == 0) { 188 189 videodata->isPaused = 0; 190 191 /* Android_ResumeSem was signaled */ 192 SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND); 193 SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND); 194 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0); 195 196 if (videodata->pauseAudio) { 197 ANDROIDAUDIO_ResumeDevices(); 198 openslES_ResumeDevices(); 199 } 200 201 /* Restore the GL Context from here, as this operation is thread dependent */ 202 if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) { 203 SDL_LockMutex(Android_ActivityMutex); 204 android_egl_context_restore(Android_Window); 205 SDL_UnlockMutex(Android_ActivityMutex); 206 } 207 208 /* Make sure SW Keyboard is restored when an app becomes foreground */ 209 if (SDL_IsTextInputActive()) { 210 Android_StartTextInput(_this); /* Only showTextInput */ 211 } 212 } 213 } else { 214 if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) { 215 216 /* Android_PauseSem was signaled */ 217 if (videodata->isPausing == 0) { 218 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0); 219 SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND); 220 SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND); 221 } 222 223 /* We've been signaled to pause (potentially several times), but before we block ourselves, 224 * we need to make sure that the very last event (of the first pause sequence, if several) 225 * has reached the app */ 226 if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) { 227 videodata->isPausing = 1; 228 } else { 229 videodata->isPausing = 0; 230 videodata->isPaused = 1; 231 backup_context = 1; 232 } 233 } 234 } 235 } 236 237 #endif /* SDL_VIDEO_DRIVER_ANDROID */ 238 239 /* vi: set ts=4 sw=4 expandtab: */