SDL_androidwindow.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #ifdef SDL_VIDEO_DRIVER_ANDROID
  20. #include "../SDL_sysvideo.h"
  21. #include "../../events/SDL_keyboard_c.h"
  22. #include "../../events/SDL_mouse_c.h"
  23. #include "../../events/SDL_windowevents_c.h"
  24. #include "../../core/android/SDL_android.h"
  25. #include "SDL_androidvideo.h"
  26. #include "SDL_androidevents.h"
  27. #include "SDL_androidwindow.h"
  28. // Currently only one window
  29. SDL_Window *Android_Window = NULL;
  30. bool Android_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props)
  31. {
  32. SDL_WindowData *data;
  33. bool result = true;
  34. if (!Android_WaitActiveAndLockActivity()) {
  35. return false;
  36. }
  37. if (Android_Window) {
  38. result = SDL_SetError("Android only supports one window");
  39. goto endfunction;
  40. }
  41. // Set orientation
  42. Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
  43. // Adjust the window data to match the screen
  44. window->x = 0;
  45. window->y = 0;
  46. window->w = Android_SurfaceWidth;
  47. window->h = Android_SurfaceHeight;
  48. // One window, it always has focus
  49. SDL_SetMouseFocus(window);
  50. SDL_SetKeyboardFocus(window);
  51. data = (SDL_WindowData *)SDL_calloc(1, sizeof(*data));
  52. if (!data) {
  53. result = false;
  54. goto endfunction;
  55. }
  56. data->native_window = Android_JNI_GetNativeWindow();
  57. if (!data->native_window) {
  58. SDL_free(data);
  59. result = SDL_SetError("Could not fetch native window");
  60. goto endfunction;
  61. }
  62. SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_ANDROID_WINDOW_POINTER, data->native_window);
  63. /* Do not create EGLSurface for Vulkan window since it will then make the window
  64. incompatible with vkCreateAndroidSurfaceKHR */
  65. #ifdef SDL_VIDEO_OPENGL_EGL
  66. if (window->flags & SDL_WINDOW_OPENGL) {
  67. data->egl_surface = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)data->native_window);
  68. if (data->egl_surface == EGL_NO_SURFACE) {
  69. ANativeWindow_release(data->native_window);
  70. SDL_free(data);
  71. result = false;
  72. goto endfunction;
  73. }
  74. }
  75. SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_ANDROID_SURFACE_POINTER, data->egl_surface);
  76. #endif
  77. SDL_SetWindowSafeAreaInsets(window, Android_SafeInsetLeft, Android_SafeInsetRight, Android_SafeInsetTop, Android_SafeInsetBottom);
  78. window->internal = data;
  79. Android_Window = window;
  80. endfunction:
  81. Android_UnlockActivityMutex();
  82. return result;
  83. }
  84. void Android_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  85. {
  86. Android_JNI_SetActivityTitle(window->title);
  87. }
  88. SDL_FullscreenResult Android_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen)
  89. {
  90. Android_LockActivityMutex();
  91. if (window == Android_Window) {
  92. SDL_WindowData *data;
  93. int old_w, old_h, new_w, new_h;
  94. // If the window is being destroyed don't change visible state
  95. if (!window->is_destroying) {
  96. Android_JNI_SetWindowStyle(fullscreen);
  97. }
  98. /* Ensure our size matches reality after we've executed the window style change.
  99. *
  100. * It is possible that we've set width and height to the full-size display, but on
  101. * Samsung DeX or Chromebooks or other windowed Android environments, our window may
  102. * still not be the full display size.
  103. */
  104. if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
  105. goto endfunction;
  106. }
  107. data = window->internal;
  108. if (!data || !data->native_window) {
  109. if (data && !data->native_window) {
  110. SDL_SetError("Missing native window");
  111. }
  112. goto endfunction;
  113. }
  114. old_w = window->w;
  115. old_h = window->h;
  116. new_w = ANativeWindow_getWidth(data->native_window);
  117. new_h = ANativeWindow_getHeight(data->native_window);
  118. if (new_w < 0 || new_h < 0) {
  119. SDL_SetError("ANativeWindow_getWidth/Height() fails");
  120. }
  121. if (old_w != new_w || old_h != new_h) {
  122. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, new_w, new_h);
  123. }
  124. }
  125. endfunction:
  126. Android_UnlockActivityMutex();
  127. return SDL_FULLSCREEN_SUCCEEDED;
  128. }
  129. void Android_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
  130. {
  131. Android_JNI_MinimizeWindow();
  132. }
  133. void Android_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable)
  134. {
  135. // Set orientation
  136. Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
  137. }
  138. void Android_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  139. {
  140. Android_LockActivityMutex();
  141. if (window == Android_Window) {
  142. Android_Window = NULL;
  143. if (window->internal) {
  144. SDL_WindowData *data = window->internal;
  145. #ifdef SDL_VIDEO_OPENGL_EGL
  146. if (data->egl_surface != EGL_NO_SURFACE) {
  147. SDL_EGL_DestroySurface(_this, data->egl_surface);
  148. }
  149. #endif
  150. if (data->native_window) {
  151. ANativeWindow_release(data->native_window);
  152. }
  153. SDL_free(window->internal);
  154. window->internal = NULL;
  155. }
  156. }
  157. Android_UnlockActivityMutex();
  158. }
  159. #endif // SDL_VIDEO_DRIVER_ANDROID