SDL  2.0
SDL_androidwindow.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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 #if SDL_VIDEO_DRIVER_ANDROID
24 
25 #include "SDL_syswm.h"
26 #include "../SDL_sysvideo.h"
27 #include "../../events/SDL_keyboard_c.h"
28 #include "../../events/SDL_mouse_c.h"
29 #include "../../events/SDL_windowevents_c.h"
30 #include "../../core/android/SDL_android.h"
31 
32 #include "SDL_androidvideo.h"
33 #include "SDL_androidwindow.h"
34 #include "SDL_hints.h"
35 
36 /* Currently only one window */
38 
39 int
41 {
43  int retval = 0;
44 
46 
47  if (Android_Window) {
48  retval = SDL_SetError("Android only supports one window");
49  goto endfunction;
50  }
51 
52  /* Set orientation */
54 
55  /* Adjust the window data to match the screen */
56  window->x = 0;
57  window->y = 0;
60 
61  window->flags &= ~SDL_WINDOW_HIDDEN;
62  window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
63 
64  /* One window, it always has focus */
67 
68  data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
69  if (!data) {
71  goto endfunction;
72  }
73 
74  data->native_window = Android_JNI_GetNativeWindow();
75 
76  if (!data->native_window) {
77  SDL_free(data);
78  retval = SDL_SetError("Could not fetch native window");
79  goto endfunction;
80  }
81 
82  /* Do not create EGLSurface for Vulkan window since it will then make the window
83  incompatible with vkCreateAndroidSurfaceKHR */
84  if ((window->flags & SDL_WINDOW_OPENGL) != 0) {
85  data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
86 
87  if (data->egl_surface == EGL_NO_SURFACE) {
88  ANativeWindow_release(data->native_window);
89  SDL_free(data);
90  retval = -1;
91  goto endfunction;
92  }
93  }
94 
95  window->driverdata = data;
97 
98 endfunction:
99 
101 
102  return retval;
103 }
104 
105 void
107 {
109 }
110 
111 void
113 {
115 
116  if (window == Android_Window) {
117 
118  /* If the window is being destroyed don't change visible state */
119  if (!window->is_destroying) {
120  Android_JNI_SetWindowStyle(fullscreen);
121  }
122 
123  /* Ensure our size matches reality after we've executed the window style change.
124  *
125  * It is possible that we've set width and height to the full-size display, but on
126  * Samsung DeX or Chromebooks or other windowed Android environemtns, our window may
127  * still not be the full display size.
128  */
129  if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
130  goto endfunction;
131  }
132 
133  SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
134 
135  if (!data || !data->native_window) {
136  if (data && !data->native_window) {
137  SDL_SetError("Missing native window");
138  }
139  goto endfunction;
140  }
141 
142  int old_w = window->w;
143  int old_h = window->h;
144 
145  int new_w = ANativeWindow_getWidth(data->native_window);
146  int new_h = ANativeWindow_getHeight(data->native_window);
147 
148  if (new_w < 0 || new_h < 0) {
149  SDL_SetError("ANativeWindow_getWidth/Height() fails");
150  }
151 
152  if (old_w != new_w || old_h != new_h) {
154  }
155  }
156 
157 endfunction:
158 
160 }
161 
162 void
164 {
166 }
167 
168 void
170 {
172 
173  if (window == Android_Window) {
175 
176  if (window->driverdata) {
177  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
178  if (data->egl_surface != EGL_NO_SURFACE) {
179  SDL_EGL_DestroySurface(_this, data->egl_surface);
180  }
181  if (data->native_window) {
182  ANativeWindow_release(data->native_window);
183  }
184  SDL_free(window->driverdata);
185  window->driverdata = NULL;
186  }
187  }
188 
190 }
191 
192 SDL_bool
194 {
195  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
196 
197  if (info->version.major == SDL_MAJOR_VERSION &&
198  info->version.minor == SDL_MINOR_VERSION) {
200  info->info.android.window = data->native_window;
201  info->info.android.surface = data->egl_surface;
202  return SDL_TRUE;
203  } else {
204  SDL_SetError("Application not compiled with SDL %d.%d",
206  return SDL_FALSE;
207  }
208 }
209 
210 #endif /* SDL_VIDEO_DRIVER_ANDROID */
211 
212 /* vi: set ts=4 sw=4 expandtab: */
Android_JNI_SetOrientation
void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
Android_JNI_SetActivityTitle
void Android_JNI_SetActivityTitle(const char *title)
Android_JNI_SetWindowStyle
void Android_JNI_SetWindowStyle(SDL_bool fullscreen)
Android_ActivityMutex
SDL_mutex * Android_ActivityMutex
if
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
Definition: pixman-arm-neon-asm.h:469
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
NativeWindowType
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:112
SDL_SysWMinfo
Definition: SDL_syswm.h:201
SDL_WindowData
Definition: SDL_androidwindow.h:38
SDL_version::minor
Uint8 minor
Definition: SDL_version.h:54
SDL_SysWMinfo::window
Window window
Definition: SDL_syswm.h:225
SDL_WINDOW_OPENGL
@ SDL_WINDOW_OPENGL
Definition: SDL_video.h:100
SDL_androidvideo.h
SDL_WINDOWEVENT_RESIZED
@ SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:154
SDL_SYSWM_ANDROID
@ SDL_SYSWM_ANDROID
Definition: SDL_syswm.h:133
SDL_SetKeyboardFocus
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:630
SDL_GetHint
#define SDL_GetHint
Definition: SDL_dynapi_overrides.h:191
SDL_SetMouseFocus
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:203
Android_SetWindowTitle
void Android_SetWindowTitle(_THIS, SDL_Window *window)
SDL_WINDOW_RESIZABLE
@ SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:104
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
Android_DestroyWindow
void Android_DestroyWindow(_THIS, SDL_Window *window)
Android_SurfaceHeight
int Android_SurfaceHeight
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_androidwindow.h
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
Android_ActivityMutex_Lock_Running
void Android_ActivityMutex_Lock_Running(void)
SDL_MINOR_VERSION
#define SDL_MINOR_VERSION
Definition: SDL_version.h:61
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_SysWMinfo::subsystem
SDL_SYSWM_TYPE subsystem
Definition: SDL_syswm.h:204
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
EGL_NO_SURFACE
#define EGL_NO_SURFACE
Definition: egl.h:100
Android_JNI_MinizeWindow
void Android_JNI_MinizeWindow(void)
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_IsChromebook
SDL_bool SDL_IsChromebook(void)
Android_SetWindowFullscreen
void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
Android_SurfaceWidth
int Android_SurfaceWidth
SDL_WINDOW_SHOWN
@ SDL_WINDOW_SHOWN
Definition: SDL_video.h:101
Android_CreateWindow
int Android_CreateWindow(_THIS, SDL_Window *window)
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
SDL_IsDeXMode
SDL_bool SDL_IsDeXMode(void)
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_WINDOW_HIDDEN
@ SDL_WINDOW_HIDDEN
Definition: SDL_video.h:102
SDL_SysWMinfo::version
SDL_version version
Definition: SDL_syswm.h:203
SDL_HINT_ORIENTATIONS
#define SDL_HINT_ORIENTATIONS
A variable controlling which orientations are allowed on iOS/Android.
Definition: SDL_hints.h:392
SDL_VideoDisplay
Definition: SDL_sysvideo.h:126
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
Android_JNI_GetNativeWindow
ANativeWindow * Android_JNI_GetNativeWindow(void)
Android_GetWindowWMInfo
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
SDL_hints.h
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_SysWMinfo::info
union SDL_SysWMinfo::@10 info
Android_MinimizeWindow
void Android_MinimizeWindow(_THIS, SDL_Window *window)
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_version::major
Uint8 major
Definition: SDL_version.h:53
SDL_MAJOR_VERSION
#define SDL_MAJOR_VERSION
Definition: SDL_version.h:60
SDL_SysWMinfo::surface
struct wl_surface * surface
Definition: SDL_syswm.h:263
Android_Window
SDL_Window * Android_Window
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
SDL_syswm.h