Skip to content

Latest commit

 

History

History
479 lines (395 loc) · 16.7 KB

SDL_uikitwindow.m

File metadata and controls

479 lines (395 loc) · 16.7 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_UIKIT
#include "SDL_syswm.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "SDL_assert.h"
#include "SDL_hints.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
#include "SDL_uikitvideo.h"
#include "SDL_uikitevents.h"
#include "SDL_uikitmodes.h"
#include "SDL_uikitwindow.h"
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitview.h"
#import "SDL_uikitopenglview.h"
#include <Foundation/Foundation.h>
@implementation SDL_WindowData
@synthesize uiwindow;
@synthesize viewcontroller;
@synthesize views;
- (instancetype)init
{
if ((self = [super init])) {
views = [NSMutableArray new];
}
return self;
}
@end
@interface SDL_uikitwindow : UIWindow
- (void)layoutSubviews;
@end
@implementation SDL_uikitwindow
- (void)layoutSubviews
{
Jul 1, 2019
Jul 1, 2019
72
73
74
75
76
77
78
79
80
/* Workaround to fix window orientation issues in iOS 8. */
/* As of July 1 2019, I haven't been able to reproduce any orientation
* issues with this disabled on iOS 12. The issue this is meant to fix might
* only happen on iOS 8, or it might have been fixed another way with other
* code... This code prevents split view (iOS 9+) from working on iPads, so
* we want to avoid using it if possible. */
if (!UIKit_IsSystemVersionAtLeast(9.0)) {
self.frame = self.screen.bounds;
}
81
82
83
84
85
86
[super layoutSubviews];
}
@end
Apr 17, 2019
Apr 17, 2019
87
88
static int
SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
SDL_uikitview *view;
CGRect frame = UIKit_ComputeViewFrame(window, displaydata.uiscreen);
int width = (int) frame.size.width;
int height = (int) frame.size.height;
SDL_WindowData *data = [[SDL_WindowData alloc] init];
if (!data) {
return SDL_OutOfMemory();
}
window->driverdata = (void *) CFBridgingRetain(data);
data.uiwindow = uiwindow;
/* only one window on iOS, always shown */
window->flags &= ~SDL_WINDOW_HIDDEN;
Sep 24, 2016
Sep 24, 2016
110
if (displaydata.uiscreen != [UIScreen mainScreen]) {
111
112
113
114
115
window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizable */
window->flags &= ~SDL_WINDOW_INPUT_FOCUS; /* never has input focus */
window->flags |= SDL_WINDOW_BORDERLESS; /* never has a status bar. */
}
Sep 14, 2016
Sep 14, 2016
116
#if !TARGET_OS_TV
117
if (displaydata.uiscreen == [UIScreen mainScreen]) {
Aug 13, 2017
Aug 13, 2017
118
119
120
121
122
123
124
125
/* SDL_CreateWindow sets the window w&h to the display's bounds if the
* fullscreen flag is set. But the display bounds orientation might not
* match what we want, and GetSupportedOrientations call below uses the
* window w&h. They're overridden below anyway, so we'll just set them
* to the requested size for the purposes of determining orientation. */
window->w = window->windowed.w;
window->h = window->windowed.h;
126
127
128
129
130
131
132
133
134
135
136
NSUInteger orients = UIKit_GetSupportedOrientations(window);
BOOL supportsLandscape = (orients & UIInterfaceOrientationMaskLandscape) != 0;
BOOL supportsPortrait = (orients & (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown)) != 0;
/* Make sure the width/height are oriented correctly */
if ((width > height && !supportsLandscape) || (height > width && !supportsPortrait)) {
int temp = width;
width = height;
height = temp;
}
}
Sep 14, 2016
Sep 14, 2016
137
#endif /* !TARGET_OS_TV */
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
window->x = 0;
window->y = 0;
window->w = width;
window->h = height;
/* The View Controller will handle rotating the view when the device
* orientation changes. This will trigger resize events, if appropriate. */
data.viewcontroller = [[SDL_uikitviewcontroller alloc] initWithSDLWindow:window];
/* The window will initially contain a generic view so resizes, touch events,
* etc. can be handled without an active OpenGL view/context. */
view = [[SDL_uikitview alloc] initWithFrame:frame];
/* Sets this view as the controller's view, and adds the view to the window
* heirarchy. */
[view setSDLWindow:window];
return 0;
}
int
UIKit_CreateWindow(_THIS, SDL_Window *window)
{
@autoreleasepool {
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *data = (__bridge SDL_DisplayData *) display->driverdata;
/* SDL currently puts this window at the start of display's linked list. We rely on this. */
SDL_assert(_this->windows == window);
/* We currently only handle a single window per display on iOS */
if (window->next != NULL) {
return SDL_SetError("Only one window allowed per display.");
}
/* If monitor has a resolution of 0x0 (hasn't been explicitly set by the
* user, so it's in standby), try to force the display to a resolution
* that most closely matches the desired window size. */
Sep 14, 2016
Sep 14, 2016
177
178
#if !TARGET_OS_TV
const CGSize origsize = data.uiscreen.currentMode.size;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
if (display->num_display_modes == 0) {
_this->GetDisplayModes(_this, display);
}
int i;
const SDL_DisplayMode *bestmode = NULL;
for (i = display->num_display_modes; i >= 0; i--) {
const SDL_DisplayMode *mode = &display->display_modes[i];
if ((mode->w >= window->w) && (mode->h >= window->h)) {
bestmode = mode;
}
}
if (bestmode) {
SDL_DisplayModeData *modedata = (__bridge SDL_DisplayModeData *)bestmode->driverdata;
[data.uiscreen setCurrentMode:modedata.uiscreenmode];
/* desktop_mode doesn't change here (the higher level will
* use it to set all the screens back to their defaults
* upon window destruction, SDL_Quit(), etc. */
display->current_mode = *bestmode;
}
}
if (data.uiscreen == [UIScreen mainScreen]) {
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
}
Sep 14, 2016
Sep 14, 2016
211
#endif /* !TARGET_OS_TV */
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* ignore the size user requested, and make a fullscreen window */
/* !!! FIXME: can we have a smaller view? */
UIWindow *uiwindow = [[SDL_uikitwindow alloc] initWithFrame:data.uiscreen.bounds];
/* put the window on an external display if appropriate. */
if (data.uiscreen != [UIScreen mainScreen]) {
[uiwindow setScreen:data.uiscreen];
}
if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
return -1;
}
}
return 1;
}
void
UIKit_SetWindowTitle(_THIS, SDL_Window * window)
{
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
data.viewcontroller.title = @(window->title);
}
}
void
UIKit_ShowWindow(_THIS, SDL_Window * window)
{
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
[data.uiwindow makeKeyAndVisible];
May 20, 2019
May 20, 2019
245
246
247
248
249
250
251
252
/* Make this window the current mouse focus for touch input */
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
if (displaydata.uiscreen == [UIScreen mainScreen]) {
SDL_SetMouseFocus(window);
SDL_SetKeyboardFocus(window);
}
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}
}
void
UIKit_HideWindow(_THIS, SDL_Window * window)
{
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
data.uiwindow.hidden = YES;
}
}
void
UIKit_RaiseWindow(_THIS, SDL_Window * window)
{
/* We don't currently offer a concept of "raising" the SDL window, since
* we only allow one per display, in the iOS fashion.
* However, we use this entry point to rebind the context to the view
* during OnWindowRestored processing. */
_this->GL_MakeCurrent(_this, _this->current_glwin, _this->current_glctx);
}
static void
UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
SDL_uikitviewcontroller *viewcontroller = data.viewcontroller;
Sep 14, 2016
Sep 14, 2016
281
#if !TARGET_OS_TV
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
if (data.uiwindow.screen == [UIScreen mainScreen]) {
if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
/* iOS 7+ won't update the status bar until we tell it to. */
if ([viewcontroller respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[viewcontroller setNeedsStatusBarAppearanceUpdate];
}
}
/* Update the view's frame to account for the status bar change. */
viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
Sep 14, 2016
Sep 14, 2016
297
#endif /* !TARGET_OS_TV */
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifdef SDL_IPHONE_KEYBOARD
/* Make sure the view is offset correctly when the keyboard is visible. */
[viewcontroller updateKeyboard];
#endif
[viewcontroller.view setNeedsLayout];
[viewcontroller.view layoutIfNeeded];
}
void
UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
{
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window);
}
}
void
UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window);
}
}
void
UIKit_DestroyWindow(_THIS, SDL_Window * window)
{
@autoreleasepool {
if (window->driverdata != NULL) {
SDL_WindowData *data = (SDL_WindowData *) CFBridgingRelease(window->driverdata);
NSArray *views = nil;
[data.viewcontroller stopAnimation];
/* Detach all views from this window. We use a copy of the array
* because setSDLWindow will remove the object from the original
* array, which would be undesirable if we were iterating over it. */
views = [data.views copy];
for (SDL_uikitview *view in views) {
[view setSDLWindow:NULL];
}
Nov 13, 2015
Nov 13, 2015
341
Nov 13, 2015
Nov 13, 2015
342
343
344
345
/* iOS may still hold a reference to the window after we release it.
* We want to make sure the SDL view controller isn't accessed in
* that case, because it would contain an invalid pointer to the old
* SDL window. */
Nov 13, 2015
Nov 13, 2015
346
347
data.uiwindow.rootViewController = nil;
data.uiwindow.hidden = YES;
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
}
}
window->driverdata = NULL;
}
SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
if (info->version.major <= SDL_MAJOR_VERSION) {
int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
info->subsystem = SDL_SYSWM_UIKIT;
info->info.uikit.window = data.uiwindow;
/* These struct members were added in SDL 2.0.4. */
if (versionnum >= SDL_VERSIONNUM(2,0,4)) {
Dec 4, 2019
Dec 4, 2019
367
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
368
369
370
371
if ([data.viewcontroller.view isKindOfClass:[SDL_uikitopenglview class]]) {
SDL_uikitopenglview *glview = (SDL_uikitopenglview *)data.viewcontroller.view;
info->info.uikit.framebuffer = glview.drawableFramebuffer;
info->info.uikit.colorbuffer = glview.drawableRenderbuffer;
Jul 19, 2015
Jul 19, 2015
372
info->info.uikit.resolveFramebuffer = glview.msaaResolveFramebuffer;
Dec 4, 2019
Dec 4, 2019
374
375
376
#else
{
#endif
377
378
info->info.uikit.framebuffer = 0;
info->info.uikit.colorbuffer = 0;
Jul 19, 2015
Jul 19, 2015
379
info->info.uikit.resolveFramebuffer = 0;
380
381
382
383
384
}
}
return SDL_TRUE;
} else {
Mar 26, 2017
Mar 26, 2017
385
SDL_SetError("Application not compiled with SDL %d.%d",
386
387
388
389
390
391
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
}
}
Sep 14, 2016
Sep 14, 2016
392
#if !TARGET_OS_TV
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
NSUInteger
UIKit_GetSupportedOrientations(SDL_Window * window)
{
const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
NSUInteger validOrientations = UIInterfaceOrientationMaskAll;
NSUInteger orientationMask = 0;
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
UIApplication *app = [UIApplication sharedApplication];
/* Get all possible valid orientations. If the app delegate doesn't tell
* us, we get the orientations from Info.plist via UIApplication. */
if ([app.delegate respondsToSelector:@selector(application:supportedInterfaceOrientationsForWindow:)]) {
validOrientations = [app.delegate application:app supportedInterfaceOrientationsForWindow:data.uiwindow];
} else if ([app respondsToSelector:@selector(supportedInterfaceOrientationsForWindow:)]) {
validOrientations = [app supportedInterfaceOrientationsForWindow:data.uiwindow];
}
if (hint != NULL) {
NSArray *orientations = [@(hint) componentsSeparatedByString:@" "];
if ([orientations containsObject:@"LandscapeLeft"]) {
orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
}
if ([orientations containsObject:@"LandscapeRight"]) {
orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
}
if ([orientations containsObject:@"Portrait"]) {
orientationMask |= UIInterfaceOrientationMaskPortrait;
}
if ([orientations containsObject:@"PortraitUpsideDown"]) {
orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
/* any orientation is okay. */
orientationMask = UIInterfaceOrientationMaskAll;
}
if (orientationMask == 0) {
if (window->w >= window->h) {
orientationMask |= UIInterfaceOrientationMaskLandscape;
}
if (window->h >= window->w) {
orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
}
/* Don't allow upside-down orientation on phones, so answering calls is in the natural orientation */
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
}
/* If none of the specified orientations are actually supported by the
* app, we'll revert to what the app supports. An exception would be
* thrown by the system otherwise. */
if ((validOrientations & orientationMask) == 0) {
orientationMask = validOrientations;
}
}
return orientationMask;
}
Sep 14, 2016
Sep 14, 2016
458
#endif /* !TARGET_OS_TV */
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
int
SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
{
if (!window || !window->driverdata) {
return SDL_SetError("Invalid window");
}
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
[data.viewcontroller setAnimationCallback:interval
callback:callback
callbackParam:callbackParam];
}
return 0;
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */