SDL_androidwindow.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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 ((window->flags & SDL_WINDOW_OPENGL) != 0) {
  67. data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
  68. if (data->egl_surface == EGL_NO_SURFACE) {
  69. ANativeWindow_release(data->native_window);
  70. SDL_free(data);
  71. retval = -1;
  72. goto endfunction;
  73. }
  74. }
  75. window->driverdata = data;
  76. Android_Window = window;
  77. endfunction:
  78. SDL_UnlockMutex(Android_ActivityMutex);
  79. return retval;
  80. }
  81. void
  82. Android_SetWindowTitle(_THIS, SDL_Window *window)
  83. {
  84. Android_JNI_SetActivityTitle(window->title);
  85. }
  86. void
  87. Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
  88. {
  89. SDL_LockMutex(Android_ActivityMutex);
  90. if (window == Android_Window) {
  91. /* If the window is being destroyed don't change visible state */
  92. if (!window->is_destroying) {
  93. Android_JNI_SetWindowStyle(fullscreen);
  94. }
  95. /* Ensure our size matches reality after we've executed the window style change.
  96. *
  97. * It is possible that we've set width and height to the full-size display, but on
  98. * Samsung DeX or Chromebooks or other windowed Android environemtns, our window may
  99. * still not be the full display size.
  100. */
  101. if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
  102. goto endfunction;
  103. }
  104. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  105. if (!data || !data->native_window) {
  106. if (data && !data->native_window) {
  107. SDL_SetError("Missing native window");
  108. }
  109. goto endfunction;
  110. }
  111. int old_w = window->w;
  112. int old_h = window->h;
  113. int new_w = ANativeWindow_getWidth(data->native_window);
  114. int new_h = ANativeWindow_getHeight(data->native_window);
  115. if (new_w < 0 || new_h < 0) {
  116. SDL_SetError("ANativeWindow_getWidth/Height() fails");
  117. }
  118. if (old_w != new_w || old_h != new_h) {
  119. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
  120. }
  121. }
  122. endfunction:
  123. SDL_UnlockMutex(Android_ActivityMutex);
  124. }
  125. void
  126. Android_MinimizeWindow(_THIS, SDL_Window *window)
  127. {
  128. Android_JNI_MinizeWindow();
  129. }
  130. void
  131. Android_DestroyWindow(_THIS, SDL_Window *window)
  132. {
  133. SDL_LockMutex(Android_ActivityMutex);
  134. if (window == Android_Window) {
  135. Android_Window = NULL;
  136. if (window->driverdata) {
  137. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  138. if (data->egl_surface != EGL_NO_SURFACE) {
  139. SDL_EGL_DestroySurface(_this, data->egl_surface);
  140. }
  141. if (data->native_window) {
  142. ANativeWindow_release(data->native_window);
  143. }
  144. SDL_free(window->driverdata);
  145. window->driverdata = NULL;
  146. }
  147. }
  148. SDL_UnlockMutex(Android_ActivityMutex);
  149. }
  150. SDL_bool
  151. Android_GetWindowWMInfo(_THIS, SDL_Window *window, SDL_SysWMinfo *info)
  152. {
  153. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  154. if (info->version.major == SDL_MAJOR_VERSION &&
  155. info->version.minor == SDL_MINOR_VERSION) {
  156. info->subsystem = SDL_SYSWM_ANDROID;
  157. info->info.android.window = data->native_window;
  158. info->info.android.surface = data->egl_surface;
  159. return SDL_TRUE;
  160. } else {
  161. SDL_SetError("Application not compiled with SDL %d.%d",
  162. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  163. return SDL_FALSE;
  164. }
  165. }
  166. #endif /* SDL_VIDEO_DRIVER_ANDROID */
  167. /* vi: set ts=4 sw=4 expandtab: */