SDL_androidevents.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_ANDROID
  20. #include "SDL_androidevents.h"
  21. #include "SDL_events.h"
  22. #include "SDL_androidkeyboard.h"
  23. #include "SDL_androidwindow.h"
  24. #include "../SDL_sysvideo.h"
  25. /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
  26. * because of THIS redefinition */
  27. #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
  28. extern void ANDROIDAUDIO_ResumeDevices(void);
  29. extern void ANDROIDAUDIO_PauseDevices(void);
  30. #else
  31. static void ANDROIDAUDIO_ResumeDevices(void) {}
  32. static void ANDROIDAUDIO_PauseDevices(void) {}
  33. #endif
  34. #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES
  35. extern void openslES_ResumeDevices(void);
  36. extern void openslES_PauseDevices(void);
  37. #else
  38. static void openslES_ResumeDevices(void) {}
  39. static void openslES_PauseDevices(void) {}
  40. #endif
  41. /* Number of 'type' events in the event queue */
  42. static int
  43. SDL_NumberOfEvents(Uint32 type)
  44. {
  45. return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
  46. }
  47. static void
  48. android_egl_context_restore(SDL_Window *window)
  49. {
  50. if (window) {
  51. SDL_Event event;
  52. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  53. if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
  54. /* The context is no longer valid, create a new one */
  55. data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
  56. SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
  57. event.type = SDL_RENDER_DEVICE_RESET;
  58. SDL_PushEvent(&event);
  59. }
  60. }
  61. }
  62. static void
  63. android_egl_context_backup(SDL_Window *window)
  64. {
  65. if (window) {
  66. /* Keep a copy of the EGL Context so we can try to restore it when we resume */
  67. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  68. data->egl_context = SDL_GL_GetCurrentContext();
  69. /* We need to do this so the EGLSurface can be freed */
  70. SDL_GL_MakeCurrent(window, NULL);
  71. }
  72. }
  73. /*
  74. * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
  75. * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted.
  76. *
  77. * No polling necessary
  78. */
  79. void
  80. Android_PumpEvents_Blocking(_THIS)
  81. {
  82. SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
  83. if (videodata->isPaused) {
  84. SDL_bool isContextExternal = SDL_IsVideoContextExternal();
  85. /* Make sure this is the last thing we do before pausing */
  86. if (!isContextExternal) {
  87. SDL_LockMutex(Android_ActivityMutex);
  88. android_egl_context_backup(Android_Window);
  89. SDL_UnlockMutex(Android_ActivityMutex);
  90. }
  91. ANDROIDAUDIO_PauseDevices();
  92. openslES_PauseDevices();
  93. if (SDL_SemWait(Android_ResumeSem) == 0) {
  94. videodata->isPaused = 0;
  95. ANDROIDAUDIO_ResumeDevices();
  96. openslES_ResumeDevices();
  97. /* Restore the GL Context from here, as this operation is thread dependent */
  98. if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
  99. SDL_LockMutex(Android_ActivityMutex);
  100. android_egl_context_restore(Android_Window);
  101. SDL_UnlockMutex(Android_ActivityMutex);
  102. }
  103. /* Make sure SW Keyboard is restored when an app becomes foreground */
  104. if (SDL_IsTextInputActive()) {
  105. Android_StartTextInput(_this); /* Only showTextInput */
  106. }
  107. }
  108. } else {
  109. if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
  110. /* We've been signaled to pause (potentially several times), but before we block ourselves,
  111. * we need to make sure that the very last event (of the first pause sequence, if several)
  112. * has reached the app */
  113. if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
  114. videodata->isPausing = 1;
  115. } else {
  116. videodata->isPausing = 0;
  117. videodata->isPaused = 1;
  118. }
  119. }
  120. }
  121. }
  122. void
  123. Android_PumpEvents_NonBlocking(_THIS)
  124. {
  125. SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
  126. static int backup_context = 0;
  127. if (videodata->isPaused) {
  128. SDL_bool isContextExternal = SDL_IsVideoContextExternal();
  129. if (backup_context) {
  130. if (!isContextExternal) {
  131. SDL_LockMutex(Android_ActivityMutex);
  132. android_egl_context_backup(Android_Window);
  133. SDL_UnlockMutex(Android_ActivityMutex);
  134. }
  135. ANDROIDAUDIO_PauseDevices();
  136. openslES_PauseDevices();
  137. backup_context = 0;
  138. }
  139. if (SDL_SemTryWait(Android_ResumeSem) == 0) {
  140. videodata->isPaused = 0;
  141. ANDROIDAUDIO_ResumeDevices();
  142. openslES_ResumeDevices();
  143. /* Restore the GL Context from here, as this operation is thread dependent */
  144. if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
  145. SDL_LockMutex(Android_ActivityMutex);
  146. android_egl_context_restore(Android_Window);
  147. SDL_UnlockMutex(Android_ActivityMutex);
  148. }
  149. /* Make sure SW Keyboard is restored when an app becomes foreground */
  150. if (SDL_IsTextInputActive()) {
  151. Android_StartTextInput(_this); /* Only showTextInput */
  152. }
  153. }
  154. } else {
  155. if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
  156. /* We've been signaled to pause (potentially several times), but before we block ourselves,
  157. * we need to make sure that the very last event (of the first pause sequence, if several)
  158. * has reached the app */
  159. if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
  160. videodata->isPausing = 1;
  161. } else {
  162. videodata->isPausing = 0;
  163. videodata->isPaused = 1;
  164. backup_context = 1;
  165. }
  166. }
  167. }
  168. }
  169. #endif /* SDL_VIDEO_DRIVER_ANDROID */
  170. /* vi: set ts=4 sw=4 expandtab: */