SDL_androidwindow.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_syswm.h"
  21. #include "../SDL_sysvideo.h"
  22. #include "../../events/SDL_keyboard_c.h"
  23. #include "../../events/SDL_mouse_c.h"
  24. #include "../../events/SDL_windowevents_c.h"
  25. #include "../../core/android/SDL_android.h"
  26. #include "SDL_androidvideo.h"
  27. #include "SDL_androidwindow.h"
  28. #include "SDL_hints.h"
  29. /* Currently only one window */
  30. SDL_Window *Android_Window = NULL;
  31. int
  32. Android_CreateWindow(_THIS, SDL_Window * window)
  33. {
  34. SDL_WindowData *data;
  35. int retval = 0;
  36. Android_ActivityMutex_Lock_Running();
  37. if (Android_Window) {
  38. retval = 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. window->flags &= ~SDL_WINDOW_HIDDEN;
  49. window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
  50. /* One window, it always has focus */
  51. SDL_SetMouseFocus(window);
  52. SDL_SetKeyboardFocus(window);
  53. data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
  54. if (!data) {
  55. retval = SDL_OutOfMemory();
  56. goto endfunction;
  57. }
  58. data->native_window = Android_JNI_GetNativeWindow();
  59. if (!data->native_window) {
  60. SDL_free(data);
  61. retval = SDL_SetError("Could not fetch native window");
  62. goto endfunction;
  63. }
  64. /* Do not create EGLSurface for Vulkan window since it will then make the window
  65. incompatible with vkCreateAndroidSurfaceKHR */
  66. #if SDL_VIDEO_OPENGL_EGL
  67. if ((window->flags & SDL_WINDOW_OPENGL) != 0) {
  68. data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
  69. if (data->egl_surface == EGL_NO_SURFACE) {
  70. ANativeWindow_release(data->native_window);
  71. SDL_free(data);
  72. retval = -1;
  73. goto endfunction;
  74. }
  75. }
  76. #endif
  77. window->driverdata = data;
  78. Android_Window = window;
  79. endfunction:
  80. SDL_UnlockMutex(Android_ActivityMutex);
  81. return retval;
  82. }
  83. void
  84. Android_SetWindowTitle(_THIS, SDL_Window *window)
  85. {
  86. Android_JNI_SetActivityTitle(window->title);
  87. }
  88. void
  89. Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
  90. {
  91. SDL_LockMutex(Android_ActivityMutex);
  92. if (window == Android_Window) {
  93. SDL_WindowData *data;
  94. int old_w, old_h, new_w, new_h;
  95. /* If the window is being destroyed don't change visible state */
  96. if (!window->is_destroying) {
  97. Android_JNI_SetWindowStyle(fullscreen);
  98. }
  99. /* Ensure our size matches reality after we've executed the window style change.
  100. *
  101. * It is possible that we've set width and height to the full-size display, but on
  102. * Samsung DeX or Chromebooks or other windowed Android environemtns, our window may
  103. * still not be the full display size.
  104. */
  105. if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
  106. goto endfunction;
  107. }
  108. data = (SDL_WindowData *)window->driverdata;
  109. if (!data || !data->native_window) {
  110. if (data && !data->native_window) {
  111. SDL_SetError("Missing native window");
  112. }
  113. goto endfunction;
  114. }
  115. old_w = window->w;
  116. old_h = window->h;
  117. new_w = ANativeWindow_getWidth(data->native_window);
  118. new_h = ANativeWindow_getHeight(data->native_window);
  119. if (new_w < 0 || new_h < 0) {
  120. SDL_SetError("ANativeWindow_getWidth/Height() fails");
  121. }
  122. if (old_w != new_w || old_h != new_h) {
  123. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
  124. }
  125. }
  126. endfunction:
  127. SDL_UnlockMutex(Android_ActivityMutex);
  128. }
  129. void
  130. Android_MinimizeWindow(_THIS, SDL_Window *window)
  131. {
  132. Android_JNI_MinizeWindow();
  133. }
  134. void Android_SetWindowResizable(_THIS, SDL_Window *window, SDL_bool resizable)
  135. {
  136. /* Set orientation */
  137. Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS));
  138. }
  139. void
  140. Android_DestroyWindow(_THIS, SDL_Window *window)
  141. {
  142. SDL_LockMutex(Android_ActivityMutex);
  143. if (window == Android_Window) {
  144. Android_Window = NULL;
  145. if (window->driverdata) {
  146. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  147. #if SDL_VIDEO_OPENGL_EGL
  148. if (data->egl_surface != EGL_NO_SURFACE) {
  149. SDL_EGL_DestroySurface(_this, data->egl_surface);
  150. }
  151. #endif
  152. if (data->native_window) {
  153. ANativeWindow_release(data->native_window);
  154. }
  155. SDL_free(window->driverdata);
  156. window->driverdata = NULL;
  157. }
  158. }
  159. SDL_UnlockMutex(Android_ActivityMutex);
  160. }
  161. SDL_bool
  162. Android_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
  163. {
  164. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  165. if (info->version.major == SDL_MAJOR_VERSION) {
  166. info->subsystem = SDL_SYSWM_ANDROID;
  167. info->info.android.window = data->native_window;
  168. #if SDL_VIDEO_OPENGL_EGL
  169. info->info.android.surface = data->egl_surface;
  170. #endif
  171. return SDL_TRUE;
  172. } else {
  173. SDL_SetError("Application not compiled with SDL %d",
  174. SDL_MAJOR_VERSION);
  175. return SDL_FALSE;
  176. }
  177. }
  178. #endif /* SDL_VIDEO_DRIVER_ANDROID */
  179. /* vi: set ts=4 sw=4 expandtab: */