/
SDL_uikitmodes.m
253 lines (208 loc) · 7.03 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
/*
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"
static int
UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
31
UIScreenMode * uiscreenmode)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
SDL_DisplayModeData *data = NULL;
if (uiscreenmode != nil) {
/* Allocate the display mode data */
data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
if (!data) {
return SDL_OutOfMemory();
}
data->uiscreenmode = uiscreenmode;
[data->uiscreenmode retain];
}
mode->driverdata = data;
return 0;
}
static void
UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
{
54
if (mode->driverdata != NULL) {
55
56
57
58
59
60
61
62
63
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,
64
UIScreenMode * uiscreenmode)
65
66
67
68
69
70
{
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0;
71
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
return -1;
}
mode.w = w;
mode.h = h;
if (SDL_AddDisplayMode(display, &mode)) {
return 0;
} else {
UIKit_FreeDisplayModeData(&mode);
return -1;
}
}
static int
86
UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h,
87
88
UIScreenMode * uiscreenmode, SDL_bool addRotation)
{
89
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode) < 0) {
90
91
92
93
94
return -1;
}
if (addRotation) {
/* Add the rotated version */
95
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode) < 0) {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
return -1;
}
}
return 0;
}
static int
UIKit_AddDisplay(UIScreen *uiscreen)
{
CGSize size = [uiscreen bounds].size;
/* 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;
119
120
mode.w = (int) size.width;
mode.h = (int) size.height;
121
122
UIScreenMode *uiscreenmode = [uiscreen currentMode];
123
124
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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);
return SDL_OutOfMemory();
}
[uiscreen retain];
data->uiscreen = uiscreen;
display.driverdata = data;
SDL_AddVideoDisplay(&display);
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)
{
162
163
164
165
166
@autoreleasepool {
for (UIScreen *uiscreen in [UIScreen screens]) {
if (UIKit_AddDisplay(uiscreen) < 0) {
return -1;
}
167
168
169
170
171
172
173
174
175
176
177
}
}
return 0;
}
void
UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@autoreleasepool {
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
CGFloat scale = data->uiscreen.scale;
for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
/* The size of a UIScreenMode is in pixels, but we deal exclusively in
* points (except in SDL_GL_GetDrawableSize.) */
CGSize size = [uimode size];
int w = (int)(size.width / scale);
int h = (int)(size.height / scale);
/* Make sure the width/height are oriented correctly */
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}
196
197
198
199
/* Add the native screen resolution. */
UIKit_AddDisplayMode(display, w, h, uimode, addRotation);
}
200
201
202
203
204
205
206
}
}
int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
207
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
208
209
210
211
212
213
214
215
216
217
218
219
220
@autoreleasepool {
[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];
}
221
222
223
}
}
}
224
225
226
227
228
229
230
231
232
return 0;
}
void
UIKit_QuitModes(_THIS)
{
/* Release Objective-C objects, so higher level doesn't free() them. */
int i, j;
233
234
235
236
237
238
239
240
241
@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);
}
242
243
244
245
246
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
[data->uiscreen release];
SDL_free(data);
display->driverdata = NULL;
247
248
249
250
251
252
253
}
}
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */