/
SDL_uikitwindow.m
409 lines (341 loc) · 13.8 KB
1
2
3
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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_uikitopenglview.h"
#include <Foundation/Foundation.h>
44
45
46
47
48
49
50
51
@implementation SDL_WindowData
@synthesize uiwindow;
@synthesize view;
@synthesize viewcontroller;
@end
52
53
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
54
55
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
56
SDL_DisplayData *displaydata = (__bridge SDL_DisplayData *) display->driverdata;
57
58
SDL_WindowData *data = [[SDL_WindowData alloc] init];
59
60
61
if (!data) {
return SDL_OutOfMemory();
}
62
63
data.uiwindow = uiwindow;
64
65
66
/* Fill in the SDL window with the window data */
{
67
CGRect frame = UIKit_ComputeViewFrame(window, displaydata.uiscreen);
68
69
70
int width = (int) frame.size.width;
int height = (int) frame.size.height;
71
72
/* Make sure the width/height are oriented correctly */
73
if (UIKit_IsDisplayLandscape(displaydata.uiscreen) != (width > height)) {
74
75
76
77
78
int temp = width;
width = height;
height = temp;
}
79
80
window->x = 0;
window->y = 0;
81
82
83
84
window->w = width;
window->h = height;
}
85
window->driverdata = (void *) CFBridgingRetain(data);
86
87
88
89
90
91
92
93
/* only one window on iOS, always shown */
window->flags &= ~SDL_WINDOW_HIDDEN;
/* SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
* This is only set if the window is on the main screen. Other screens
* just force the window to have the borderless flag.
*/
94
if (displaydata.uiscreen == [UIScreen mainScreen]) {
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
window->flags |= SDL_WINDOW_INPUT_FOCUS; /* always has input focus */
/* This was setup earlier for our window, and in iOS 7 is controlled by the view, not the application
if ([UIApplication sharedApplication].statusBarHidden) {
window->flags |= SDL_WINDOW_BORDERLESS;
} else {
window->flags &= ~SDL_WINDOW_BORDERLESS;
}
*/
} else {
window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
window->flags &= ~SDL_WINDOW_INPUT_FOCUS; /* never has input focus */
window->flags |= SDL_WINDOW_BORDERLESS; /* never has a status bar. */
}
/* The View Controller will handle rotating the view when the
* device orientation changes. This will trigger resize events, if
* appropriate.
*/
114
data.viewcontroller = [[SDL_uikitviewcontroller alloc] initWithSDLWindow:window];
115
data.viewcontroller.title = @"";
116
117
118
119
120
121
122
return 0;
}
int
UIKit_CreateWindow(_THIS, SDL_Window *window)
{
123
124
@autoreleasepool {
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
125
126
127
SDL_DisplayData *data = (__bridge SDL_DisplayData *) display->driverdata;
const BOOL external = ([UIScreen mainScreen] != data.uiscreen);
const CGSize origsize = [[data.uiscreen currentMode] size];
128
129
130
131
132
133
134
/* 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.");
135
}
136
137
138
139
140
141
142
143
144
/* 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.
*/
if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
if (display->num_display_modes == 0) {
_this->GetDisplayModes(_this, display);
}
145
146
147
148
149
150
151
152
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;
}
153
154
if (bestmode) {
155
156
SDL_DisplayModeData *modedata = (__bridge SDL_DisplayModeData *)bestmode->driverdata;
[data.uiscreen setCurrentMode:modedata.uiscreenmode];
157
158
159
160
161
162
163
/* 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;
}
164
165
}
166
if (data.uiscreen == [UIScreen mainScreen]) {
167
168
169
NSUInteger orientations = UIKit_GetSupportedOrientations(window);
UIApplication *app = [UIApplication sharedApplication];
170
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
171
app.statusBarHidden = YES;
172
} else {
173
app.statusBarHidden = NO;
174
175
}
176
177
178
179
180
181
182
/* Make sure the screen is using a supported orientation. We do it
* now so that SetupWindowData assigns the properly oriented width
* and height to the window's w and h variables.
*/
if (UIKit_IsDisplayLandscape(data.uiscreen)) {
if (!(orientations & UIInterfaceOrientationMaskLandscape)) {
[app setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
183
}
184
185
186
187
188
189
190
} else {
if (!(orientations & (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown))) {
UIInterfaceOrientation orient = UIInterfaceOrientationLandscapeLeft;
if (orientations & UIInterfaceOrientationMaskLandscapeRight) {
orient = UIInterfaceOrientationLandscapeRight;
}
[app setStatusBarOrientation:orient animated:NO];
191
192
193
}
}
}
194
195
196
/* ignore the size user requested, and make a fullscreen window */
/* !!! FIXME: can we have a smaller view? */
197
UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:data.uiscreen.bounds];
198
199
200
201
202
203
204
/* put the window on an external display if appropriate. This implicitly
* does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
* main display, where we land by default, as that would eat the
* status bar real estate.
*/
if (external) {
205
[uiwindow setScreen:data.uiscreen];
206
}
207
208
209
210
if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
return -1;
}
211
212
213
214
215
}
return 1;
}
216
217
218
219
220
221
222
223
224
225
226
227
228
void
UIKit_SetWindowTitle(_THIS, SDL_Window * window)
{
@autoreleasepool {
SDL_uikitviewcontroller *vc = ((__bridge SDL_WindowData *) window->driverdata).viewcontroller;
if (window->title) {
vc.title = @(window->title);
} else {
vc.title = @"";
}
}
}
229
230
231
void
UIKit_ShowWindow(_THIS, SDL_Window * window)
{
232
@autoreleasepool {
233
UIWindow *uiwindow = ((__bridge SDL_WindowData *) window->driverdata).uiwindow;
234
235
[uiwindow makeKeyAndVisible];
}
236
237
238
239
240
}
void
UIKit_HideWindow(_THIS, SDL_Window * window)
{
241
@autoreleasepool {
242
UIWindow *uiwindow = ((__bridge SDL_WindowData *) window->driverdata).uiwindow;
243
244
uiwindow.hidden = YES;
}
245
246
247
248
249
250
251
252
253
254
255
256
257
}
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);
}
258
259
static void
UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
260
{
261
262
SDL_WindowData *windowdata = (__bridge SDL_WindowData *) window->driverdata;
SDL_uikitviewcontroller *viewcontroller = windowdata.viewcontroller;
263
CGRect frame;
264
265
if (windowdata.uiwindow.screen == [UIScreen mainScreen]) {
266
267
268
269
270
if (window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
271
272
273
274
275
/* iOS 7+ won't update the status bar until we tell it to. */
if ([viewcontroller respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[viewcontroller setNeedsStatusBarAppearanceUpdate];
}
276
277
}
278
/* Update the view's frame to account for the status bar change. */
279
frame = UIKit_ComputeViewFrame(window, windowdata.uiwindow.screen);
280
281
282
283
windowdata.view.frame = frame;
[windowdata.view setNeedsLayout];
[windowdata.view layoutIfNeeded];
284
285
/* Get frame dimensions */
286
287
int w = (int) frame.size.width;
int h = (int) frame.size.height;
288
289
/* We can pick either width or height here and we'll rotate the
290
screen to match, so we pick the closest to what we wanted.
291
292
*/
if (window->w >= window->h) {
293
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, SDL_max(w, h), SDL_min(w, h));
294
} else {
295
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, SDL_min(w, h), SDL_max(w, h));
296
297
298
}
}
299
300
301
void
UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
{
302
303
304
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window);
}
305
306
307
308
309
}
void
UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
310
311
312
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window);
}
313
314
}
315
316
317
void
UIKit_DestroyWindow(_THIS, SDL_Window * window)
{
318
@autoreleasepool {
319
320
if (window->driverdata != NULL) {
CFRelease(window->driverdata);
321
}
322
}
323
window->driverdata = NULL;
324
325
326
327
328
}
SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
329
@autoreleasepool {
330
UIWindow *uiwindow = ((__bridge SDL_WindowData *) window->driverdata).uiwindow;
331
332
333
334
335
336
337
338
339
340
if (info->version.major <= SDL_MAJOR_VERSION) {
info->subsystem = SDL_SYSWM_UIKIT;
info->info.uikit.window = uiwindow;
return SDL_TRUE;
} else {
SDL_SetError("Application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
341
342
343
}
}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
NSUInteger
UIKit_GetSupportedOrientations(SDL_Window * window)
{
const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
NSUInteger orientationMask = 0;
@autoreleasepool {
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 the phone, so answering calls is in the natural orientation */
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
return orientationMask;
}
391
392
393
int
SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
{
394
395
@autoreleasepool {
SDL_WindowData *data = window ? (__bridge SDL_WindowData *)window->driverdata : nil;
396
397
398
399
if (!data || !data.view) {
return SDL_SetError("Invalid window or view not set");
}
400
401
[data.view setAnimationCallback:interval callback:callback callbackParam:callbackParam];
402
403
}
404
405
406
407
408
409
return 0;
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */