SDL  2.0
SDL_uikitvideo.m
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_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 #include "SDL_uikitmetalview.h"
42 
43 #define UIKITVID_DRIVER_NAME "uikit"
44 
45 @implementation SDL_VideoData
46 
47 @end
48 
49 /* Initialization/Query functions */
50 static int UIKit_VideoInit(_THIS);
51 static void UIKit_VideoQuit(_THIS);
52 
53 /* DUMMY driver bootstrap functions */
54 
55 static int
56 UIKit_Available(void)
57 {
58  return 1;
59 }
60 
61 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
62 {
63  @autoreleasepool {
64  CFRelease(device->driverdata);
66  }
67 }
68 
69 static SDL_VideoDevice *
70 UIKit_CreateDevice(int devindex)
71 {
72  @autoreleasepool {
75 
76  /* Initialize all variables that we clean on shutdown */
78  if (device) {
79  data = [SDL_VideoData new];
80  } else {
83  return (0);
84  }
85 
86  device->driverdata = (void *) CFBridgingRetain(data);
87 
88  /* Set the function pointers */
89  device->VideoInit = UIKit_VideoInit;
90  device->VideoQuit = UIKit_VideoQuit;
91  device->GetDisplayModes = UIKit_GetDisplayModes;
92  device->SetDisplayMode = UIKit_SetDisplayMode;
93  device->PumpEvents = UIKit_PumpEvents;
94  device->SuspendScreenSaver = UIKit_SuspendScreenSaver;
95  device->CreateSDLWindow = UIKit_CreateWindow;
96  device->SetWindowTitle = UIKit_SetWindowTitle;
97  device->ShowWindow = UIKit_ShowWindow;
98  device->HideWindow = UIKit_HideWindow;
99  device->RaiseWindow = UIKit_RaiseWindow;
100  device->SetWindowBordered = UIKit_SetWindowBordered;
101  device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
102  device->DestroyWindow = UIKit_DestroyWindow;
103  device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
104  device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds;
105 
106 #if SDL_IPHONE_KEYBOARD
107  device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
108  device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
109  device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
110  device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
111  device->SetTextInputRect = UIKit_SetTextInputRect;
112 #endif
113 
114  device->SetClipboardText = UIKit_SetClipboardText;
115  device->GetClipboardText = UIKit_GetClipboardText;
116  device->HasClipboardText = UIKit_HasClipboardText;
117 
118  /* OpenGL (ES) functions */
119 #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
120  device->GL_MakeCurrent = UIKit_GL_MakeCurrent;
121  device->GL_GetDrawableSize = UIKit_GL_GetDrawableSize;
122  device->GL_SwapWindow = UIKit_GL_SwapWindow;
123  device->GL_CreateContext = UIKit_GL_CreateContext;
124  device->GL_DeleteContext = UIKit_GL_DeleteContext;
125  device->GL_GetProcAddress = UIKit_GL_GetProcAddress;
126  device->GL_LoadLibrary = UIKit_GL_LoadLibrary;
127 #endif
128  device->free = UIKit_DeleteDevice;
129 
130 #if SDL_VIDEO_VULKAN
131  device->Vulkan_LoadLibrary = UIKit_Vulkan_LoadLibrary;
132  device->Vulkan_UnloadLibrary = UIKit_Vulkan_UnloadLibrary;
133  device->Vulkan_GetInstanceExtensions
134  = UIKit_Vulkan_GetInstanceExtensions;
135  device->Vulkan_CreateSurface = UIKit_Vulkan_CreateSurface;
136  device->Vulkan_GetDrawableSize = UIKit_Vulkan_GetDrawableSize;
137 #endif
138 
139 #if SDL_VIDEO_METAL
140  device->Metal_CreateView = UIKit_Metal_CreateView;
141  device->Metal_DestroyView = UIKit_Metal_DestroyView;
142 #endif
143 
144  device->gl_config.accelerated = 1;
145 
146  return device;
147  }
148 }
149 
151  UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
152  UIKit_Available, UIKit_CreateDevice
153 };
154 
155 
156 int
157 UIKit_VideoInit(_THIS)
158 {
160 
161  if (UIKit_InitModes(_this) < 0) {
162  return -1;
163  }
164  return 0;
165 }
166 
167 void
168 UIKit_VideoQuit(_THIS)
169 {
171 }
172 
173 void
175 {
176  @autoreleasepool {
177  /* Ignore ScreenSaver API calls if the idle timer hint has been set. */
178  /* FIXME: The idle timer hint should be deprecated for SDL 2.1. */
180  UIApplication *app = [UIApplication sharedApplication];
181 
182  /* Prevent the display from dimming and going to sleep. */
183  app.idleTimerDisabled = (_this->suspend_screensaver != SDL_FALSE);
184  }
185  }
186 }
187 
188 SDL_bool
189 UIKit_IsSystemVersionAtLeast(double version)
190 {
191  return [[UIDevice currentDevice].systemVersion doubleValue] >= version;
192 }
193 
194 CGRect
195 UIKit_ComputeViewFrame(SDL_Window *window, UIScreen *screen)
196 {
197  SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
198  CGRect frame = screen.bounds;
199 
200  /* Use the UIWindow bounds instead of the UIScreen bounds, when possible.
201  * The uiwindow bounds may be smaller than the screen bounds when Split View
202  * is used on an iPad. */
203  if (data != nil && data.uiwindow != nil) {
204  frame = data.uiwindow.bounds;
205  }
206 
207 #if !TARGET_OS_TV && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0)
208  BOOL hasiOS7 = UIKit_IsSystemVersionAtLeast(7.0);
209 
210  /* The view should always show behind the status bar in iOS 7+. */
211  if (!hasiOS7 && !(window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN))) {
212  frame = screen.applicationFrame;
213  }
214 #endif
215 
216 #if !TARGET_OS_TV
217  /* iOS 10 seems to have a bug where, in certain conditions, putting the
218  * device to sleep with the a landscape-only app open, re-orienting the
219  * device to portrait, and turning it back on will result in the screen
220  * bounds returning portrait orientation despite the app being in landscape.
221  * This is a workaround until a better solution can be found.
222  * https://bugzilla.libsdl.org/show_bug.cgi?id=3505
223  * https://bugzilla.libsdl.org/show_bug.cgi?id=3465
224  * https://forums.developer.apple.com/thread/65337 */
225  if (UIKit_IsSystemVersionAtLeast(8.0)) {
226  UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
227  BOOL landscape = UIInterfaceOrientationIsLandscape(orient);
228  BOOL fullscreen = CGRectEqualToRect(screen.bounds, frame);
229 
230  /* The orientation flip doesn't make sense when the window is smaller
231  * than the screen (iPad Split View, for example). */
232  if (fullscreen && (landscape != (frame.size.width > frame.size.height))) {
233  float height = frame.size.width;
234  frame.size.width = frame.size.height;
235  frame.size.height = height;
236  }
237  }
238 #endif
239 
240  return frame;
241 }
242 
243 void
245 {
246 #if !TARGET_OS_TV
247  /* Force the main SDL window to re-evaluate home indicator state */
248  SDL_Window *focus = SDL_GetFocusWindow();
249  if (focus) {
250  SDL_WindowData *data = (__bridge SDL_WindowData *) focus->driverdata;
251  if (data != nil) {
252 #pragma clang diagnostic push
253 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
254  if ([data.viewcontroller respondsToSelector:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden)]) {
255  [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden) withObject:nil waitUntilDone:NO];
256  [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfScreenEdgesDeferringSystemGestures) withObject:nil waitUntilDone:NO];
257  }
258 #pragma clang diagnostic pop
259  }
260  }
261 #endif /* !TARGET_OS_TV */
262 }
263 
264 /*
265  * iOS log support.
266  *
267  * This doesn't really have aything to do with the interfaces of the SDL video
268  * subsystem, but we need to stuff this into an Objective-C source code file.
269  *
270  * NOTE: This is copypasted from src/video/cocoa/SDL_cocoavideo.m! Thus, if
271  * Cocoa is supported, we use that one instead. Be sure both versions remain
272  * identical!
273  */
274 
275 #if !defined(SDL_VIDEO_DRIVER_COCOA)
276 void SDL_NSLog(const char *text)
277 {
278  NSLog(@"%s", text);
279 }
280 #endif /* SDL_VIDEO_DRIVER_COCOA */
281 
282 /*
283  * iOS Tablet detection
284  *
285  * This doesn't really have aything to do with the interfaces of the SDL video
286  * subsystem, but we need to stuff this into an Objective-C source code file.
287  */
288 SDL_bool SDL_IsIPad(void)
289 {
290  return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
291 }
292 
293 #endif /* SDL_VIDEO_DRIVER_UIKIT */
294 
295 /* vi: set ts=4 sw=4 expandtab: */
SDL_uikitmodes.h
screen
SDL_Renderer * screen
Definition: testgamecontroller.c:64
SDL_uikitvideo.h
SDL_mouse.h
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
UIKit_SetWindowFullscreen
void UIKit_SetWindowFullscreen(_THIS, SDL_Window *window, SDL_VideoDisplay *display, SDL_bool fullscreen)
UIKit_GetDisplayUsableBounds
int UIKit_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
UIKit_GetWindowWMInfo
SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window *window, struct SDL_SysWMinfo *info)
UIKit_InitModes
int UIKit_InitModes(_THIS)
SDL_WindowData
Definition: SDL_androidwindow.h:39
UIKit_HasClipboardText
SDL_bool UIKit_HasClipboardText(_THIS)
SDL_WINDOW_FULLSCREEN
@ SDL_WINDOW_FULLSCREEN
Definition: SDL_video.h:99
SDL_VideoData::BOOL
BOOL(WINAPI *CloseTouchInputHandle)(HTOUCHINPUT)
UIKit_ForceUpdateHomeIndicator
void UIKit_ForceUpdateHomeIndicator(void)
UIKit_SetWindowTitle
void UIKit_SetWindowTitle(_THIS, SDL_Window *window)
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:75
SDL_GetFocusWindow
SDL_Window * SDL_GetFocusWindow(void)
Definition: SDL_video.c:2717
UIKit_DestroyWindow
void UIKit_DestroyWindow(_THIS, SDL_Window *window)
SDL_VideoDevice::gl_config
struct SDL_VideoDevice::@255 gl_config
SDL_GetHintBoolean
#define SDL_GetHintBoolean
Definition: SDL_dynapi_overrides.h:608
UIKit_GetClipboardText
char * UIKit_GetClipboardText(_THIS)
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
UIKit_PumpEvents
void UIKit_PumpEvents(_THIS)
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_uikitvulkan.h
UIKit_SetWindowBordered
void UIKit_SetWindowBordered(_THIS, SDL_Window *window, SDL_bool bordered)
SDL_uikitevents.h
UIKit_CreateWindow
int UIKit_CreateWindow(_THIS, SDL_Window *window)
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
UIKit_RaiseWindow
void UIKit_RaiseWindow(_THIS, SDL_Window *window)
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_VideoDevice::driver_loaded
int driver_loaded
Definition: SDL_sysvideo.h:361
UIKit_SuspendScreenSaver
void UIKit_SuspendScreenSaver(_THIS)
UIKit_HideWindow
void UIKit_HideWindow(_THIS, SDL_Window *window)
frame
int frame
Definition: teststreaming.c:60
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
SDL_uikitopengles.h
SDL_VideoDevice
Definition: SDL_sysvideo.h:150
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_uikitwindow.h
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
SDL_HINT_IDLE_TIMER_DISABLED
#define SDL_HINT_IDLE_TIMER_DISABLED
A variable controlling whether the idle timer is disabled on iOS.
Definition: SDL_hints.h:381
SDL_Window::driverdata
void * driverdata
Definition: SDL_sysvideo.h:112
SDL_uikitclipboard.h
UIKit_SetDisplayMode
int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
UIKit_IsSystemVersionAtLeast
SDL_bool UIKit_IsSystemVersionAtLeast(double version)
UIKit_ShowWindow
void UIKit_ShowWindow(_THIS, SDL_Window *window)
UIKit_QuitModes
void UIKit_QuitModes(_THIS)
SDL_hints.h
UIKIT_bootstrap
VideoBootStrap UIKIT_bootstrap
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_video.h
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
VideoBootStrap
Definition: SDL_sysvideo.h:406
SDL_uikitmetalview.h
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
UIKit_SetClipboardText
int UIKit_SetClipboardText(_THIS, const char *text)
SDL_VideoDevice::suspend_screensaver
SDL_bool suspend_screensaver
Definition: SDL_sysvideo.h:322
UIKit_GetDisplayModes
void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay *display)
SDL_VideoData::devindex
int devindex
Definition: SDL_kmsdrmvideo.h:40
SDL_VideoData
Definition: SDL_androidvideo.h:37
SDL_WINDOW_BORDERLESS
@ SDL_WINDOW_BORDERLESS
Definition: SDL_video.h:103