This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_uikitmodes.m
322 lines (269 loc) · 9.76 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2013 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
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_config.h"
#if SDL_VIDEO_DRIVER_UIKIT
#include "SDL_assert.h"
#include "SDL_uikitmodes.h"
BOOL SDL_UIKit_supports_multiple_displays = NO;
static int
UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
UIScreenMode * uiscreenmode, CGFloat scale)
{
SDL_DisplayModeData *data = NULL;
37
38
39
40
41
if (uiscreenmode != nil) {
/* Allocate the display mode data */
data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
if (!data) {
42
return SDL_OutOfMemory();
43
}
44
45
46
data->uiscreenmode = uiscreenmode;
[data->uiscreenmode retain];
47
48
49
data->scale = scale;
}
50
51
mode->driverdata = data;
52
53
54
55
56
57
58
59
return 0;
}
static void
UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
{
if (!SDL_UIKit_supports_multiple_displays) {
60
/* Not on at least iPhoneOS 3.2 (versions prior to iPad). */
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
SDL_assert(mode->driverdata == NULL);
} else if (mode->driverdata != NULL) {
SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
[data->uiscreenmode release];
SDL_free(data);
mode->driverdata = NULL;
}
}
static int
UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
UIScreenMode * uiscreenmode, CGFloat scale)
{
SDL_DisplayMode mode;
SDL_zero(mode);
76
77
78
79
80
81
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0;
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
return -1;
}
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
mode.w = w;
mode.h = h;
if (SDL_AddDisplayMode(display, &mode)) {
return 0;
} else {
UIKit_FreeDisplayModeData(&mode);
return -1;
}
}
static int
UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
UIScreenMode * uiscreenmode, SDL_bool addRotation)
{
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
return -1;
}
100
101
if (addRotation) {
102
/* Add the rotated version */
103
104
105
106
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
return -1;
}
}
107
108
109
110
111
112
113
114
115
return 0;
}
static int
UIKit_AddDisplay(UIScreen *uiscreen)
{
CGSize size = [uiscreen bounds].size;
116
/* Make sure the width/height are oriented correctly */
117
118
119
120
121
122
if (UIKit_IsDisplayLandscape(uiscreen) != (size.width > size.height)) {
CGFloat height = size.width;
size.width = size.height;
size.height = height;
}
123
124
125
126
127
128
129
130
/* When dealing with UIKit all coordinates are specified in terms of
* what Apple refers to as points. On earlier devices without the
* so called "Retina" display, there is a one to one mapping between
* points and pixels. In other cases [UIScreen scale] indicates the
* relationship between points and pixels. Since SDL has no notion
* of points, we must compensate in all cases where dealing with such
* units.
*/
131
132
CGFloat scale;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
133
scale = [uiscreen scale]; /* iOS >= 4.0 */
134
} else {
135
scale = 1.0f; /* iOS < 4.0 */
136
137
138
139
140
141
142
143
}
SDL_VideoDisplay display;
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.w = (int)(size.width * scale);
mode.h = (int)(size.height * scale);
144
145
UIScreenMode * uiscreenmode = nil;
146
147
148
/* UIScreenMode showed up in 3.2 (the iPad and later). We're
* misusing this supports_multiple_displays flag here for that.
*/
149
150
151
if (SDL_UIKit_supports_multiple_displays) {
uiscreenmode = [uiscreen currentMode];
}
152
153
154
155
156
157
158
159
160
161
162
163
164
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
return -1;
}
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
/* Allocate the display data */
SDL_DisplayData *data = (SDL_DisplayData *) SDL_malloc(sizeof(*data));
if (!data) {
UIKit_FreeDisplayModeData(&display.desktop_mode);
165
return SDL_OutOfMemory();
166
}
167
168
169
170
[uiscreen retain];
data->uiscreen = uiscreen;
data->scale = scale;
171
172
173
display.driverdata = data;
SDL_AddVideoDisplay(&display);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
return 0;
}
SDL_bool
UIKit_IsDisplayLandscape(UIScreen *uiscreen)
{
if (uiscreen == [UIScreen mainScreen]) {
return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
} else {
CGSize size = [uiscreen bounds].size;
return (size.width > size.height);
}
}
int
UIKit_InitModes(_THIS)
{
192
/* this tells us whether we are running on ios >= 3.2 */
193
194
SDL_UIKit_supports_multiple_displays = [UIScreen instancesRespondToSelector:@selector(currentMode)];
195
/* Add the main screen. */
196
197
198
199
if (UIKit_AddDisplay([UIScreen mainScreen]) < 0) {
return -1;
}
200
201
202
203
/* If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels. */
/* The iPad added both a larger main screen and the ability to use
* external displays. So, add the other displays (screens in UI speak).
*/
204
205
if (SDL_UIKit_supports_multiple_displays) {
for (UIScreen *uiscreen in [UIScreen screens]) {
206
/* Only add the other screens */
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
if (uiscreen != [UIScreen mainScreen]) {
if (UIKit_AddDisplay(uiscreen) < 0) {
return -1;
}
}
}
}
/* We're done! */
return 0;
}
void
UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
if (SDL_UIKit_supports_multiple_displays) {
228
229
230
/* availableModes showed up in 3.2 (the iPad and later). We should only
* land here for at least that version of the OS.
*/
231
232
233
234
for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
CGSize size = [uimode size];
int w = (int)size.width;
int h = (int)size.height;
235
236
/* Make sure the width/height are oriented correctly */
237
238
239
240
241
242
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}
243
/* Add the native screen resolution. */
244
245
246
UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotation);
if (data->scale != 1.0f) {
247
248
249
/* Add the native screen resolution divided by its scale.
* This is so devices capable of e.g. 640x960 also advertise 320x480.
*/
250
251
252
253
254
255
256
257
258
259
260
UIKit_AddDisplayMode(display,
(int)(size.width / data->scale),
(int)(size.height / data->scale),
1.0f, uimode, addRotation);
}
}
} else {
const CGSize size = [data->uiscreen bounds].size;
int w = (int)size.width;
int h = (int)size.height;
261
/* Make sure the width/height are oriented correctly */
262
263
264
265
266
267
268
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}
UIKit_AddDisplayMode(display, w, h, 1.0f, nil, addRotation);
269
}
270
271
272
273
274
275
276
277
}
int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
if (!SDL_UIKit_supports_multiple_displays) {
278
/* Not on at least iPhoneOS 3.2 (versions prior to iPad). */
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
SDL_assert(mode->driverdata == NULL);
} else {
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
[data->uiscreen setCurrentMode:modedata->uiscreenmode];
if (data->uiscreen == [UIScreen mainScreen]) {
if (mode->w > mode->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (mode->w < mode->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}
}
}
return 0;
}
void
UIKit_QuitModes(_THIS)
{
302
/* Release Objective-C objects, so higher level doesn't free() them. */
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
int i, j;
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);
}
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
[data->uiscreen release];
SDL_free(data);
display->driverdata = NULL;
}
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */