SDL_androidevents.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* We're going to do this by default */
  21. #define SDL_ANDROID_BLOCK_ON_PAUSE 1
  22. #include "SDL_androidevents.h"
  23. #include "SDL_events.h"
  24. #include "SDL_androidkeyboard.h"
  25. #include "SDL_androidwindow.h"
  26. #if !SDL_AUDIO_DISABLED
  27. /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
  28. * because of THIS redefinition */
  29. extern void ANDROIDAUDIO_ResumeDevices(void);
  30. extern void ANDROIDAUDIO_PauseDevices(void);
  31. #else
  32. static void ANDROIDAUDIO_ResumeDevices(void) {}
  33. static void ANDROIDAUDIO_PauseDevices(void) {}
  34. #endif
  35. /* Number of 'type' events in the event queue */
  36. static int
  37. SDL_NumberOfEvents(Uint32 type)
  38. {
  39. return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
  40. }
  41. static void
  42. android_egl_context_restore(SDL_Window *window)
  43. {
  44. SDL_Event event;
  45. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  46. if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
  47. /* The context is no longer valid, create a new one */
  48. data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
  49. SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
  50. event.type = SDL_RENDER_DEVICE_RESET;
  51. SDL_PushEvent(&event);
  52. }
  53. }
  54. static void
  55. android_egl_context_backup(SDL_Window *window)
  56. {
  57. /* Keep a copy of the EGL Context so we can try to restore it when we resume */
  58. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  59. data->egl_context = SDL_GL_GetCurrentContext();
  60. /* We need to do this so the EGLSurface can be freed */
  61. SDL_GL_MakeCurrent(window, NULL);
  62. }
  63. /*
  64. * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
  65. * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
  66. *
  67. * No polling necessary
  68. */
  69. #if SDL_ANDROID_BLOCK_ON_PAUSE
  70. void
  71. Android_PumpEvents(_THIS)
  72. {
  73. static int isPaused = 0;
  74. static int isPausing = 0;
  75. if (isPaused) {
  76. /* Make sure this is the last thing we do before pausing */
  77. SDL_LockMutex(Android_ActivityMutex);
  78. android_egl_context_backup(Android_Window);
  79. SDL_UnlockMutex(Android_ActivityMutex);
  80. ANDROIDAUDIO_PauseDevices();
  81. if (SDL_SemWait(Android_ResumeSem) == 0) {
  82. isPaused = 0;
  83. ANDROIDAUDIO_ResumeDevices();
  84. /* Restore the GL Context from here, as this operation is thread dependent */
  85. if (!SDL_HasEvent(SDL_QUIT)) {
  86. SDL_LockMutex(Android_ActivityMutex);
  87. android_egl_context_restore(Android_Window);
  88. SDL_UnlockMutex(Android_ActivityMutex);
  89. }
  90. /* Make sure SW Keyboard is restored when an app becomes foreground */
  91. if (SDL_IsTextInputActive()) {
  92. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  93. Android_StartTextInput(_this); /* Only showTextInput */
  94. }
  95. }
  96. } else {
  97. if (isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
  98. /* We've been signaled to pause (potentially several times), but before we block ourselves,
  99. * we need to make sure that the very last event (of the first pause sequence, if several)
  100. * has reached the app */
  101. if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
  102. isPausing = 1;
  103. }
  104. else {
  105. isPausing = 0;
  106. isPaused = 1;
  107. }
  108. }
  109. }
  110. }
  111. #else
  112. void
  113. Android_PumpEvents(_THIS)
  114. {
  115. static int isPaused = 0;
  116. if (isPaused) {
  117. if (SDL_SemTryWait(Android_ResumeSem) == 0) {
  118. isPaused = 0;
  119. ANDROIDAUDIO_ResumeDevices();
  120. /* Restore the GL Context from here, as this operation is thread dependent */
  121. if (!SDL_HasEvent(SDL_QUIT)) {
  122. SDL_LockMutex(Android_ActivityMutex);
  123. android_egl_context_restore(Android_Window);
  124. SDL_UnlockMutex(Android_ActivityMutex);
  125. }
  126. /* Make sure SW Keyboard is restored when an app becomes foreground */
  127. if (SDL_IsTextInputActive()) {
  128. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  129. Android_StartTextInput(_this); /* Only showTextInput */
  130. }
  131. }
  132. } else {
  133. if (SDL_SemTryWait(Android_PauseSem) == 0) {
  134. SDL_LockMutex(Android_ActivityMutex);
  135. android_egl_context_backup(Android_Window);
  136. SDL_UnlockMutex(Android_ActivityMutex);
  137. ANDROIDAUDIO_PauseDevices();
  138. isPaused = 1;
  139. }
  140. }
  141. }
  142. #endif
  143. #endif /* SDL_VIDEO_DRIVER_ANDROID */
  144. /* vi: set ts=4 sw=4 expandtab: */