Skip to content

Latest commit

 

History

History
270 lines (218 loc) · 7.38 KB

SDL_uikitmodes.m

File metadata and controls

270 lines (218 loc) · 7.38 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
/*
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_assert.h"
#include "SDL_uikitmodes.h"
Aug 6, 2014
Aug 6, 2014
28
29
30
31
32
33
34
35
36
37
38
39
@implementation SDL_DisplayData
@synthesize uiscreen;
@end
@implementation SDL_DisplayModeData
@synthesize uiscreenmode;
@end
40
41
42
static int
UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
Jul 15, 2014
Jul 15, 2014
43
UIScreenMode * uiscreenmode)
Aug 6, 2014
Aug 6, 2014
45
SDL_DisplayModeData *data = nil;
46
47
48
if (uiscreenmode != nil) {
/* Allocate the display mode data */
Aug 6, 2014
Aug 6, 2014
49
data = [[SDL_DisplayModeData alloc] init];
50
51
52
53
if (!data) {
return SDL_OutOfMemory();
}
Aug 6, 2014
Aug 6, 2014
54
data.uiscreenmode = uiscreenmode;
55
56
}
Aug 6, 2014
Aug 6, 2014
57
mode->driverdata = (void *) CFBridgingRetain(data);
58
59
60
61
62
63
64
return 0;
}
static void
UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
{
Jun 21, 2014
Jun 21, 2014
65
if (mode->driverdata != NULL) {
Aug 6, 2014
Aug 6, 2014
66
CFRelease(mode->driverdata);
67
68
69
70
71
72
mode->driverdata = NULL;
}
}
static int
UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
Jul 15, 2014
Jul 15, 2014
73
UIScreenMode * uiscreenmode)
74
75
76
77
78
79
{
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0;
Jul 15, 2014
Jul 15, 2014
80
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) {
81
82
83
84
85
86
87
88
89
90
91
92
93
94
return -1;
}
mode.w = w;
mode.h = h;
if (SDL_AddDisplayMode(display, &mode)) {
return 0;
} else {
UIKit_FreeDisplayModeData(&mode);
return -1;
}
}
static int
Jul 15, 2014
Jul 15, 2014
95
UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h,
96
97
UIScreenMode * uiscreenmode, SDL_bool addRotation)
{
Jul 15, 2014
Jul 15, 2014
98
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode) < 0) {
99
100
101
102
103
return -1;
}
if (addRotation) {
/* Add the rotated version */
Jul 15, 2014
Jul 15, 2014
104
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode) < 0) {
105
106
107
108
109
110
111
112
113
114
return -1;
}
}
return 0;
}
static int
UIKit_AddDisplay(UIScreen *uiscreen)
{
Jan 15, 2015
Jan 15, 2015
115
CGSize size = uiscreen.bounds.size;
116
117
118
119
120
121
122
123
124
125
126
127
/* Make sure the width/height are oriented correctly */
if (UIKit_IsDisplayLandscape(uiscreen) != (size.width > size.height)) {
CGFloat height = size.width;
size.width = size.height;
size.height = height;
}
SDL_VideoDisplay display;
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888;
Jul 15, 2014
Jul 15, 2014
128
129
mode.w = (int) size.width;
mode.h = (int) size.height;
Jan 15, 2015
Jan 15, 2015
131
UIScreenMode *uiscreenmode = uiscreen.currentMode;
Jul 15, 2014
Jul 15, 2014
133
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) {
134
135
136
137
138
139
140
141
return -1;
}
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
/* Allocate the display data */
Aug 6, 2014
Aug 6, 2014
142
SDL_DisplayData *data = [[SDL_DisplayData alloc] init];
143
144
145
146
147
if (!data) {
UIKit_FreeDisplayModeData(&display.desktop_mode);
return SDL_OutOfMemory();
}
Aug 6, 2014
Aug 6, 2014
148
data.uiscreen = uiscreen;
Aug 6, 2014
Aug 6, 2014
150
display.driverdata = (void *) CFBridgingRetain(data);
151
152
153
154
155
156
157
158
159
SDL_AddVideoDisplay(&display);
return 0;
}
SDL_bool
UIKit_IsDisplayLandscape(UIScreen *uiscreen)
{
if (uiscreen == [UIScreen mainScreen]) {
Jan 15, 2015
Jan 15, 2015
160
return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
161
} else {
Jan 15, 2015
Jan 15, 2015
162
CGSize size = uiscreen.bounds.size;
163
164
165
166
167
168
169
return (size.width > size.height);
}
}
int
UIKit_InitModes(_THIS)
{
Jul 29, 2014
Jul 29, 2014
170
171
172
173
174
@autoreleasepool {
for (UIScreen *uiscreen in [UIScreen screens]) {
if (UIKit_AddDisplay(uiscreen) < 0) {
return -1;
}
175
176
177
178
179
180
181
182
183
}
}
return 0;
}
void
UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
Jul 29, 2014
Jul 29, 2014
184
@autoreleasepool {
Aug 6, 2014
Aug 6, 2014
185
186
187
188
189
SDL_DisplayData *data = (__bridge SDL_DisplayData *) display->driverdata;
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data.uiscreen);
SDL_bool addRotation = (data.uiscreen == [UIScreen mainScreen]);
CGFloat scale = data.uiscreen.scale;
Jul 29, 2014
Jul 29, 2014
190
Sep 14, 2014
Sep 14, 2014
191
192
193
194
195
196
197
198
#ifdef __IPHONE_8_0
/* The UIScreenMode of an iPhone 6 Plus should be 1080x1920 rather than
* 1242x2208 (414x736@3x), so we should use the native scale. */
if ([data.uiscreen respondsToSelector:@selector(nativeScale)]) {
scale = data.uiscreen.nativeScale;
}
#endif
Jan 15, 2015
Jan 15, 2015
199
for (UIScreenMode *uimode in data.uiscreen.availableModes) {
Sep 14, 2014
Sep 14, 2014
200
201
202
203
/* The size of a UIScreenMode is in pixels, but we deal exclusively
* in points (except in SDL_GL_GetDrawableSize.) */
int w = (int)(uimode.size.width / scale);
int h = (int)(uimode.size.height / scale);
Jul 29, 2014
Jul 29, 2014
204
205
206
207
208
209
210
/* Make sure the width/height are oriented correctly */
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}
Jul 29, 2014
Jul 29, 2014
212
213
UIKit_AddDisplayMode(display, w, h, uimode, addRotation);
}
214
215
216
217
218
219
}
}
int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
Jul 29, 2014
Jul 29, 2014
220
@autoreleasepool {
Aug 6, 2014
Aug 6, 2014
221
222
223
224
SDL_DisplayData *data = (__bridge SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *modedata = (__bridge SDL_DisplayModeData *)mode->driverdata;
[data.uiscreen setCurrentMode:modedata.uiscreenmode];
Jul 29, 2014
Jul 29, 2014
225
Aug 6, 2014
Aug 6, 2014
226
if (data.uiscreen == [UIScreen mainScreen]) {
Jan 15, 2015
Jan 15, 2015
227
228
229
/* [UIApplication setStatusBarOrientation:] no longer works reliably
* in recent iOS versions, so we can't rotate the screen when setting
* the display mode. */
Jul 29, 2014
Jul 29, 2014
230
if (mode->w > mode->h) {
Aug 6, 2014
Aug 6, 2014
231
if (!UIKit_IsDisplayLandscape(data.uiscreen)) {
Jan 15, 2015
Jan 15, 2015
232
return SDL_SetError("Screen orientation does not match display mode size");
Jul 29, 2014
Jul 29, 2014
233
234
}
} else if (mode->w < mode->h) {
Aug 6, 2014
Aug 6, 2014
235
if (UIKit_IsDisplayLandscape(data.uiscreen)) {
Jan 15, 2015
Jan 15, 2015
236
return SDL_SetError("Screen orientation does not match display mode size");
Jul 29, 2014
Jul 29, 2014
237
}
238
239
240
}
}
}
Jun 21, 2014
Jun 21, 2014
241
242
243
244
245
246
247
248
249
return 0;
}
void
UIKit_QuitModes(_THIS)
{
/* Release Objective-C objects, so higher level doesn't free() them. */
int i, j;
Jul 29, 2014
Jul 29, 2014
250
251
252
253
254
255
256
257
258
@autoreleasepool {
for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i];
UIKit_FreeDisplayModeData(&display->desktop_mode);
for (j = 0; j < display->num_display_modes; j++) {
SDL_DisplayMode *mode = &display->display_modes[j];
UIKit_FreeDisplayModeData(mode);
}
Aug 6, 2014
Aug 6, 2014
260
261
262
263
if (display->driverdata != NULL) {
CFRelease(display->driverdata);
display->driverdata = NULL;
}
264
265
266
267
268
269
270
}
}
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */