Atlas - SDL_androidwindow.c
Home / ext / SDL / src / video / android Lines: 1 | Size: 6391 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#ifdef SDL_VIDEO_DRIVER_ANDROID 24 25#include "../SDL_sysvideo.h" 26#include "../../events/SDL_keyboard_c.h" 27#include "../../events/SDL_mouse_c.h" 28#include "../../events/SDL_windowevents_c.h" 29#include "../../core/android/SDL_android.h" 30 31#include "SDL_androidvideo.h" 32#include "SDL_androidevents.h" 33#include "SDL_androidwindow.h" 34 35 36// Currently only one window 37SDL_Window *Android_Window = NULL; 38 39bool Android_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props) 40{ 41 SDL_WindowData *data; 42 bool result = true; 43 44 if (!Android_WaitActiveAndLockActivity()) { 45 return false; 46 } 47 48 if (Android_Window) { 49 result = SDL_SetError("Android only supports one window"); 50 goto endfunction; 51 } 52 53 // Set orientation 54 Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS)); 55 56 // Adjust the window data to match the screen 57 window->x = 0; 58 window->y = 0; 59 window->w = Android_SurfaceWidth; 60 window->h = Android_SurfaceHeight; 61 62 // One window, it always has focus 63 SDL_SetMouseFocus(window); 64 SDL_SetKeyboardFocus(window); 65 66 data = (SDL_WindowData *)SDL_calloc(1, sizeof(*data)); 67 if (!data) { 68 result = false; 69 goto endfunction; 70 } 71 72 data->native_window = Android_JNI_GetNativeWindow(); 73 if (!data->native_window) { 74 SDL_free(data); 75 result = SDL_SetError("Could not fetch native window"); 76 goto endfunction; 77 } 78 SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_ANDROID_WINDOW_POINTER, data->native_window); 79 80 /* Do not create EGLSurface for Vulkan window since it will then make the window 81 incompatible with vkCreateAndroidSurfaceKHR */ 82#ifdef SDL_VIDEO_OPENGL_EGL 83 if (window->flags & SDL_WINDOW_OPENGL) { 84 data->egl_surface = SDL_EGL_CreateSurface(_this, window, (NativeWindowType)data->native_window); 85 86 if (data->egl_surface == EGL_NO_SURFACE) { 87 ANativeWindow_release(data->native_window); 88 SDL_free(data); 89 result = false; 90 goto endfunction; 91 } 92 } 93 SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_ANDROID_SURFACE_POINTER, data->egl_surface); 94#endif 95 96 SDL_SetWindowSafeAreaInsets(window, Android_SafeInsetLeft, Android_SafeInsetRight, Android_SafeInsetTop, Android_SafeInsetBottom); 97 98 window->internal = data; 99 Android_Window = window; 100 101endfunction: 102 103 Android_UnlockActivityMutex(); 104 105 return result; 106} 107 108void Android_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window) 109{ 110 Android_JNI_SetActivityTitle(window->title); 111} 112 113SDL_FullscreenResult Android_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen) 114{ 115 Android_LockActivityMutex(); 116 117 if (window == Android_Window) { 118 SDL_WindowData *data; 119 int old_w, old_h, new_w, new_h; 120 121 // If the window is being destroyed don't change visible state 122 if (!window->is_destroying) { 123 Android_JNI_SetWindowStyle(fullscreen); 124 } 125 126 /* Ensure our size matches reality after we've executed the window style change. 127 * 128 * It is possible that we've set width and height to the full-size display, but on 129 * Samsung DeX or Chromebooks or other windowed Android environments, our window may 130 * still not be the full display size. 131 */ 132 if (!SDL_IsDeXMode() && !SDL_IsChromebook()) { 133 goto endfunction; 134 } 135 136 data = window->internal; 137 if (!data || !data->native_window) { 138 if (data && !data->native_window) { 139 SDL_SetError("Missing native window"); 140 } 141 goto endfunction; 142 } 143 144 old_w = window->w; 145 old_h = window->h; 146 147 new_w = ANativeWindow_getWidth(data->native_window); 148 new_h = ANativeWindow_getHeight(data->native_window); 149 150 if (new_w < 0 || new_h < 0) { 151 SDL_SetError("ANativeWindow_getWidth/Height() fails"); 152 } 153 154 if (old_w != new_w || old_h != new_h) { 155 SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, new_w, new_h); 156 } 157 } 158 159endfunction: 160 161 Android_UnlockActivityMutex(); 162 163 return SDL_FULLSCREEN_SUCCEEDED; 164} 165 166void Android_MinimizeWindow(SDL_VideoDevice *_this, SDL_Window *window) 167{ 168 Android_JNI_MinimizeWindow(); 169} 170 171void Android_SetWindowResizable(SDL_VideoDevice *_this, SDL_Window *window, bool resizable) 172{ 173 // Set orientation 174 Android_JNI_SetOrientation(window->w, window->h, window->flags & SDL_WINDOW_RESIZABLE, SDL_GetHint(SDL_HINT_ORIENTATIONS)); 175} 176 177void Android_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) 178{ 179 Android_LockActivityMutex(); 180 181 if (window == Android_Window) { 182 Android_Window = NULL; 183 184 if (window->internal) { 185 SDL_WindowData *data = window->internal; 186 187#ifdef SDL_VIDEO_OPENGL_EGL 188 if (data->egl_surface != EGL_NO_SURFACE) { 189 SDL_EGL_DestroySurface(_this, data->egl_surface); 190 } 191#endif 192 193 if (data->native_window) { 194 ANativeWindow_release(data->native_window); 195 } 196 SDL_free(window->internal); 197 window->internal = NULL; 198 } 199 } 200 201 Android_UnlockActivityMutex(); 202} 203 204#endif // SDL_VIDEO_DRIVER_ANDROID 205[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.