SDL  2.0
SDL_androidwindow.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 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;
58  window->w = Android_SurfaceWidth;
59  window->h = Android_SurfaceHeight;
60 
61  window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
62  window->flags &= ~SDL_WINDOW_HIDDEN;
63  window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
64 
65  /* One window, it always has focus */
66  SDL_SetMouseFocus(window);
67  SDL_SetKeyboardFocus(window);
68 
69  data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
70  if (!data) {
71  retval = SDL_OutOfMemory();
72  goto endfunction;
73  }
74 
76 
77  if (!data->native_window) {
78  SDL_free(data);
79  retval = SDL_SetError("Could not fetch native window");
80  goto endfunction;
81  }
82 
83  /* Do not create EGLSurface for Vulkan window since it will then make the window
84  incompatible with vkCreateAndroidSurfaceKHR */
85  if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
86  data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
87 
88  if (data->egl_surface == EGL_NO_SURFACE) {
89  ANativeWindow_release(data->native_window);
90  SDL_free(data);
91  retval = -1;
92  goto endfunction;
93  }
94  }
95 
96  window->driverdata = data;
97  Android_Window = window;
98 
99 endfunction:
100 
102 
103  return retval;
104 }
105 
106 void
108 {
110 }
111 
112 void
114 {
116 
117  if (window == Android_Window) {
118 
119  /* If the window is being destroyed don't change visible state */
120  if (!window->is_destroying) {
121  Android_JNI_SetWindowStyle(fullscreen);
122  }
123 
124  /* Ensure our size matches reality after we've executed the window style change.
125  *
126  * It is possible that we've set width and height to the full-size display, but on
127  * Samsung DeX or Chromebooks or other windowed Android environemtns, our window may
128  * still not be the full display size.
129  */
130  if (!SDL_IsDeXMode() && !SDL_IsChromebook()) {
131  goto endfunction;
132  }
133 
134  SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
135 
136  if (!data || !data->native_window) {
137  if (data && !data->native_window) {
138  SDL_SetError("Missing native window");
139  }
140  goto endfunction;
141  }
142 
143  int old_w = window->w;
144  int old_h = window->h;
145 
146  int new_w = ANativeWindow_getWidth(data->native_window);
147  int new_h = ANativeWindow_getHeight(data->native_window);
148 
149  if (new_w < 0 || new_h < 0) {
150  SDL_SetError("ANativeWindow_getWidth/Height() fails");
151  }
152 
153  if (old_w != new_w || old_h != new_h) {
154  SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, new_w, new_h);
155  }
156  }
157 
158 endfunction:
159 
161 }
162 
163 void
165 {
167 }
168 
169 void
171 {
173 
174  if (window == Android_Window) {
175  Android_Window = NULL;
176 
177  if (window->driverdata) {
178  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
179  if (data->egl_surface != EGL_NO_SURFACE) {
180  SDL_EGL_DestroySurface(_this, data->egl_surface);
181  }
182  if (data->native_window) {
183  ANativeWindow_release(data->native_window);
184  }
185  SDL_free(window->driverdata);
186  window->driverdata = NULL;
187  }
188  }
189 
191 }
192 
193 SDL_bool
195 {
196  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
197 
198  if (info->version.major == SDL_MAJOR_VERSION &&
199  info->version.minor == SDL_MINOR_VERSION) {
201  info->info.android.window = data->native_window;
202  info->info.android.surface = data->egl_surface;
203  return SDL_TRUE;
204  } else {
205  SDL_SetError("Application not compiled with SDL %d.%d",
207  return SDL_FALSE;
208  }
209 }
210 
211 #endif /* SDL_VIDEO_DRIVER_ANDROID */
212 
213 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_MINOR_VERSION
Definition: SDL_version.h:61
#define SDL_LockMutex
void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint)
void SDL_SetKeyboardFocus(SDL_Window *window)
Definition: SDL_keyboard.c:630
void Android_ActivityMutex_Lock_Running(void)
void Android_DestroyWindow(_THIS, SDL_Window *window)
#define EGL_NO_SURFACE
Definition: egl.h:100
struct wl_surface * surface
Definition: SDL_syswm.h:259
#define SDL_MAJOR_VERSION
Definition: SDL_version.h:60
int Android_SurfaceWidth
SDL_Window * Android_Window
void Android_JNI_SetActivityTitle(const char *title)
#define SDL_GetHint
SDL_version version
Definition: SDL_syswm.h:199
Uint8 major
Definition: SDL_version.h:53
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_bool is_destroying
Definition: SDL_sysvideo.h:101
SDL_SYSWM_TYPE subsystem
Definition: SDL_syswm.h:200
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:211
void Android_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
SDL_bool SDL_IsDeXMode(void)
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:112
SDL_bool retval
#define _THIS
#define SDL_free
SDL_mutex * Android_ActivityMutex
SDL_bool SDL_IsChromebook(void)
Uint8 minor
Definition: SDL_version.h:54
char * title
Definition: SDL_sysvideo.h:77
int Android_SurfaceHeight
Window window
Definition: SDL_syswm.h:221
#define SDL_HINT_ORIENTATIONS
A variable controlling which orientations are allowed on iOS/Android.
Definition: SDL_hints.h:360
#define NULL
Definition: begin_code.h:167
void Android_JNI_MinizeWindow(void)
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:161
void Android_MinimizeWindow(_THIS, SDL_Window *window)
void Android_SetWindowTitle(_THIS, SDL_Window *window)
ANativeWindow * Android_JNI_GetNativeWindow(void)
#define SDL_SetError
#define SDL_calloc
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:73
union SDL_SysWMinfo::@17 info
int Android_CreateWindow(_THIS, SDL_Window *window)
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
#define SDL_UnlockMutex
void Android_JNI_SetWindowStyle(SDL_bool fullscreen)
void * driverdata
Definition: SDL_sysvideo.h:111
ANativeWindow * native_window
Uint32 flags
Definition: SDL_sysvideo.h:83
EGLSurface egl_surface