SDL  2.0
SDL_uikitvideo.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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_UIKIT
24 
25 #import <UIKit/UIKit.h>
26 
27 #include "SDL_video.h"
28 #include "SDL_mouse.h"
29 #include "SDL_hints.h"
30 #include "../SDL_sysvideo.h"
31 #include "../SDL_pixels_c.h"
32 #include "../../events/SDL_events_c.h"
33 
34 #include "SDL_uikitvideo.h"
35 #include "SDL_uikitevents.h"
36 #include "SDL_uikitmodes.h"
37 #include "SDL_uikitwindow.h"
38 #include "SDL_uikitopengles.h"
39 #include "SDL_uikitclipboard.h"
40 #include "SDL_uikitvulkan.h"
41 
42 #define UIKITVID_DRIVER_NAME "uikit"
43 
44 @implementation SDL_VideoData
45 
46 @end
47 
48 /* Initialization/Query functions */
49 static int UIKit_VideoInit(_THIS);
50 static void UIKit_VideoQuit(_THIS);
51 
52 /* DUMMY driver bootstrap functions */
53 
54 static int
55 UIKit_Available(void)
56 {
57  return 1;
58 }
59 
60 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
61 {
62  @autoreleasepool {
63  CFRelease(device->driverdata);
64  SDL_free(device);
65  }
66 }
67 
68 static SDL_VideoDevice *
69 UIKit_CreateDevice(int devindex)
70 {
71  @autoreleasepool {
74 
75  /* Initialize all variables that we clean on shutdown */
76  device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
77  if (device) {
78  data = [SDL_VideoData new];
79  } else {
80  SDL_free(device);
82  return (0);
83  }
84 
85  device->driverdata = (void *) CFBridgingRetain(data);
86 
87  /* Set the function pointers */
88  device->VideoInit = UIKit_VideoInit;
89  device->VideoQuit = UIKit_VideoQuit;
92  device->PumpEvents = UIKit_PumpEvents;
96  device->ShowWindow = UIKit_ShowWindow;
97  device->HideWindow = UIKit_HideWindow;
104 
105 #if SDL_IPHONE_KEYBOARD
106  device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
107  device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
108  device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
109  device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
110  device->SetTextInputRect = UIKit_SetTextInputRect;
111 #endif
112 
116 
117  /* OpenGL (ES) functions */
125  device->free = UIKit_DeleteDevice;
126 
127 #if SDL_VIDEO_VULKAN
128  device->Vulkan_LoadLibrary = UIKit_Vulkan_LoadLibrary;
129  device->Vulkan_UnloadLibrary = UIKit_Vulkan_UnloadLibrary;
131  = UIKit_Vulkan_GetInstanceExtensions;
132  device->Vulkan_CreateSurface = UIKit_Vulkan_CreateSurface;
133  device->Vulkan_GetDrawableSize = UIKit_Vulkan_GetDrawableSize;
134 #endif
135 
136  device->gl_config.accelerated = 1;
137 
138  return device;
139  }
140 }
141 
143  UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
144  UIKit_Available, UIKit_CreateDevice
145 };
146 
147 
148 int
149 UIKit_VideoInit(_THIS)
150 {
152 
153  if (UIKit_InitModes(_this) < 0) {
154  return -1;
155  }
156  return 0;
157 }
158 
159 void
160 UIKit_VideoQuit(_THIS)
161 {
163 }
164 
165 void
167 {
168  @autoreleasepool {
169  /* Ignore ScreenSaver API calls if the idle timer hint has been set. */
170  /* FIXME: The idle timer hint should be deprecated for SDL 2.1. */
172  UIApplication *app = [UIApplication sharedApplication];
173 
174  /* Prevent the display from dimming and going to sleep. */
175  app.idleTimerDisabled = (_this->suspend_screensaver != SDL_FALSE);
176  }
177  }
178 }
179 
180 SDL_bool
181 UIKit_IsSystemVersionAtLeast(double version)
182 {
183  return [[UIDevice currentDevice].systemVersion doubleValue] >= version;
184 }
185 
186 CGRect
187 UIKit_ComputeViewFrame(SDL_Window *window, UIScreen *screen)
188 {
189  CGRect frame = screen.bounds;
190 
191 #if !TARGET_OS_TV && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0)
192  BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0);
193 
194  /* The view should always show behind the status bar in iOS 7+. */
195  if (!hasiOS7 && !(window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
196  frame = screen.applicationFrame;
197  }
198 #endif
199 
200 #if !TARGET_OS_TV
201  /* iOS 10 seems to have a bug where, in certain conditions, putting the
202  * device to sleep with the a landscape-only app open, re-orienting the
203  * device to portrait, and turning it back on will result in the screen
204  * bounds returning portrait orientation despite the app being in landscape.
205  * This is a workaround until a better solution can be found.
206  * https://bugzilla.libsdl.org/show_bug.cgi?id=3505
207  * https://bugzilla.libsdl.org/show_bug.cgi?id=3465
208  * https://forums.developer.apple.com/thread/65337 */
209  if (UIKit_IsSystemVersionAtLeast(8.0)) {
210  UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
211  BOOL isLandscape = UIInterfaceOrientationIsLandscape(orient);
212 
213  if (isLandscape != (frame.size.width > frame.size.height)) {
214  float height = frame.size.width;
215  frame.size.width = frame.size.height;
216  frame.size.height = height;
217  }
218  }
219 #endif
220 
221  return frame;
222 }
223 
224 /*
225  * iOS log support.
226  *
227  * This doesn't really have aything to do with the interfaces of the SDL video
228  * subsystem, but we need to stuff this into an Objective-C source code file.
229  */
230 
231 void SDL_NSLog(const char *text)
232 {
233  NSLog(@"%s", text);
234 }
235 
236 #endif /* SDL_VIDEO_DRIVER_UIKIT */
237 
238 /* vi: set ts=4 sw=4 expandtab: */
int(* Vulkan_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:270
int UIKit_InitModes(_THIS)
SDL_bool UIKit_IsSystemVersionAtLeast(double version)
void UIKit_QuitModes(_THIS)
void UIKit_PumpEvents(_THIS)
void UIKit_SuspendScreenSaver(_THIS)
SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
int UIKit_CreateWindow(_THIS, SDL_Window *window)
void UIKit_DestroyWindow(_THIS, SDL_Window *window)
SDL_bool(* IsScreenKeyboardShown)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:294
void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
void(* free)(_THIS)
Definition: SDL_sysvideo.h:390
void UIKit_GL_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
void(* ShowWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:222
void UIKit_SetWindowTitle(_THIS, SDL_Window *window)
void UIKit_RaiseWindow(_THIS, SDL_Window *window)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
void UIKit_HideWindow(_THIS, SDL_Window *window)
void UIKit_SetWindowBordered(_THIS, SDL_Window *window, SDL_bool bordered)
int UIKit_GL_LoadLibrary(_THIS, const char *path)
void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
int(* GL_LoadLibrary)(_THIS, const char *path)
Definition: SDL_sysvideo.h:254
int UIKit_GL_SwapWindow(_THIS, SDL_Window *window)
int(* SetDisplayMode)(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
Definition: SDL_sysvideo.h:204
#define SDL_GetHintBoolean
void(* SetWindowBordered)(_THIS, SDL_Window *window, SDL_bool bordered)
Definition: SDL_sysvideo.h:228
void(* GL_GetDrawableSize)(_THIS, SDL_Window *window, int *w, int *h)
Definition: SDL_sysvideo.h:259
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
void(* HideWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:223
static SDL_AudioDeviceID device
Definition: loopwave.c:37
void(* RaiseWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:224
void(* SetTextInputRect)(_THIS, SDL_Rect *rect)
Definition: SDL_sysvideo.h:288
SDL_bool(* Vulkan_GetInstanceExtensions)(_THIS, SDL_Window *window, unsigned *count, const char **names)
Definition: SDL_sysvideo.h:272
SDL_bool(* GetWindowWMInfo)(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
Definition: SDL_sysvideo.h:247
SDL_GLContext(* GL_CreateContext)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:257
void(* Vulkan_GetDrawableSize)(_THIS, SDL_Window *window, int *w, int *h)
Definition: SDL_sysvideo.h:274
void UIKit_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
SDL_bool(* Vulkan_CreateSurface)(_THIS, SDL_Window *window, VkInstance instance, VkSurfaceKHR *surface)
Definition: SDL_sysvideo.h:273
#define _THIS
int(* GL_MakeCurrent)(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: SDL_sysvideo.h:258
#define SDL_free
int frame
Definition: teststreaming.c:60
void(* Vulkan_UnloadLibrary)(_THIS)
Definition: SDL_sysvideo.h:271
void(* DestroyWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:234
BOOL(WINAPI *CloseTouchInputHandle)(HTOUCHINPUT)
#define SDL_HINT_IDLE_TIMER_DISABLED
A variable controlling whether the idle timer is disabled on iOS.
Definition: SDL_hints.h:329
SDL_bool(* HasScreenKeyboardSupport)(_THIS)
Definition: SDL_sysvideo.h:291
SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window *window)
void * UIKit_GL_GetProcAddress(_THIS, const char *proc)
void(* GetDisplayModes)(_THIS, SDL_VideoDisplay *display)
Definition: SDL_sysvideo.h:196
int UIKit_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
int(* CreateSDLWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:210
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:139
void(* VideoQuit)(_THIS)
Definition: SDL_sysvideo.h:166
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_calloc
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
int(* GL_SwapWindow)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:262
void(* ShowScreenKeyboard)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:292
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_bool(* HasClipboardText)(_THIS)
Definition: SDL_sysvideo.h:299
int(* VideoInit)(_THIS)
Definition: SDL_sysvideo.h:160
SDL_bool suspend_screensaver
Definition: SDL_sysvideo.h:310
void(* SetWindowFullscreen)(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
Definition: SDL_sysvideo.h:230
VideoBootStrap UIKIT_bootstrap
struct SDL_VideoDevice::@34 gl_config
void(* GL_DeleteContext)(_THIS, SDL_GLContext context)
Definition: SDL_sysvideo.h:263
char *(* GetClipboardText)(_THIS)
Definition: SDL_sysvideo.h:298
void(* SetWindowTitle)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:212
int(* GetDisplayUsableBounds)(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
Definition: SDL_sysvideo.h:191
int(* SetClipboardText)(_THIS, const char *text)
Definition: SDL_sysvideo.h:297
void UIKit_ShowWindow(_THIS, SDL_Window *window)
Uint32 flags
Definition: SDL_sysvideo.h:83
SDL_Renderer * screen
void(* SuspendScreenSaver)(_THIS)
Definition: SDL_sysvideo.h:283
int UIKit_GL_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
int UIKit_SetClipboardText(_THIS, const char *text)
void *(* GL_GetProcAddress)(_THIS, const char *proc)
Definition: SDL_sysvideo.h:255
void(* PumpEvents)(_THIS)
Definition: SDL_sysvideo.h:280
void(* HideScreenKeyboard)(_THIS, SDL_Window *window)
Definition: SDL_sysvideo.h:293
char * UIKit_GetClipboardText(_THIS)
int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
SDL_bool UIKit_HasClipboardText(_THIS)