SDL  2.0
SDL_androidwindow.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2017 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 
30 #include "SDL_androidvideo.h"
31 #include "SDL_androidwindow.h"
32 #include "SDL_hints.h"
33 
34 int
36 {
38 
39  if (Android_Window) {
40  return SDL_SetError("Android only supports one window");
41  }
42 
45 
46  /* Set orientation */
48 
49  /* Adjust the window data to match the screen */
50  window->x = 0;
51  window->y = 0;
52  window->w = Android_ScreenWidth;
53  window->h = Android_ScreenHeight;
54 
55  window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
56  window->flags |= SDL_WINDOW_FULLSCREEN; /* window is always fullscreen */
57  window->flags &= ~SDL_WINDOW_HIDDEN;
58  window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
59  window->flags |= SDL_WINDOW_INPUT_FOCUS; /* always has input focus */
60 
61  /* One window, it always has focus */
62  SDL_SetMouseFocus(window);
63  SDL_SetKeyboardFocus(window);
64 
65  data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
66  if (!data) {
67  return SDL_OutOfMemory();
68  }
69 
71 
72  if (!data->native_window) {
73  SDL_free(data);
74  return SDL_SetError("Could not fetch native window");
75  }
76 
77  /* Do not create EGLSurface for Vulkan window since it will then make the window
78  incompatible with vkCreateAndroidSurfaceKHR */
79  if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
80  data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
81 
82  if (data->egl_surface == EGL_NO_SURFACE) {
83  ANativeWindow_release(data->native_window);
84  SDL_free(data);
85  return SDL_SetError("Could not create GLES window surface");
86  }
87  }
88 
89  window->driverdata = data;
91 
92  return 0;
93 }
94 
95 void
97 {
99 }
100 
101 void
103 {
105 
106  if (window == Android_Window) {
112 
113  if(window->driverdata) {
114  data = (SDL_WindowData *) window->driverdata;
115  if (data->egl_surface != EGL_NO_SURFACE) {
116  SDL_EGL_DestroySurface(_this, data->egl_surface);
117  }
118  if (data->native_window) {
119  ANativeWindow_release(data->native_window);
120  }
121  SDL_free(window->driverdata);
122  window->driverdata = NULL;
123  }
124  }
125 }
126 
127 SDL_bool
129 {
130  SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
131 
132  if (info->version.major == SDL_MAJOR_VERSION &&
133  info->version.minor == SDL_MINOR_VERSION) {
135  info->info.android.window = data->native_window;
136  info->info.android.surface = data->egl_surface;
137  return SDL_TRUE;
138  } else {
139  SDL_SetError("Application not compiled with SDL %d.%d",
141  return SDL_FALSE;
142  }
143 }
144 
145 #endif /* SDL_VIDEO_DRIVER_ANDROID */
146 
147 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_MINOR_VERSION
Definition: SDL_version.h:61
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_DestroyWindow(_THIS, SDL_Window *window)
#define EGL_NO_SURFACE
Definition: egl.h:100
#define SDL_MAJOR_VERSION
Definition: SDL_version.h:60
#define SDL_CreateSemaphore
int Android_ScreenHeight
void Android_JNI_SetActivityTitle(const char *title)
#define SDL_GetHint
SDL_version version
Definition: SDL_syswm.h:196
Uint8 major
Definition: SDL_version.h:53
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_SYSWM_TYPE subsystem
Definition: SDL_syswm.h:197
void SDL_SetMouseFocus(SDL_Window *window)
Definition: SDL_mouse.c:149
SDL_sem * Android_PauseSem
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:112
void * SDL_calloc(size_t nmemb, size_t size)
#define _THIS
void SDL_free(void *mem)
SDL_Window * Android_Window
Uint8 minor
Definition: SDL_version.h:54
char * title
Definition: SDL_sysvideo.h:77
int Android_ScreenWidth
Window window
Definition: SDL_syswm.h:218
#define SDL_HINT_ORIENTATIONS
A variable controlling which orientations are allowed on iOS.
Definition: SDL_hints.h:327
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:139
void Android_SetWindowTitle(_THIS, SDL_Window *window)
ANativeWindow * Android_JNI_GetNativeWindow(void)
#define SDL_SetError
SDL_sem * Android_ResumeSem
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:73
int Android_CreateWindow(_THIS, SDL_Window *window)
#define SDL_DestroySemaphore
SDL_bool Android_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
union SDL_SysWMinfo::@18 info
void * driverdata
Definition: SDL_sysvideo.h:111
ANativeWindow * native_window
Uint32 flags
Definition: SDL_sysvideo.h:83
EGLSurface egl_surface