Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
393 lines (322 loc) · 12.1 KB

SDL_uikitvideo.m

File metadata and controls

393 lines (322 loc) · 12.1 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
*/
Oct 31, 2011
Oct 31, 2011
21
#include "SDL_config.h"
Jun 22, 2011
Jun 22, 2011
22
Oct 31, 2011
Oct 31, 2011
23
#if SDL_VIDEO_DRIVER_UIKIT
Jun 22, 2011
Jun 22, 2011
24
Oct 31, 2011
Oct 31, 2011
25
#import <UIKit/UIKit.h>
Jun 22, 2011
Jun 22, 2011
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
#include "SDL_video.h"
#include "SDL_mouse.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_uikitwindow.h"
#include "SDL_uikitopengles.h"
#include "SDL_assert.h"
#define UIKITVID_DRIVER_NAME "uikit"
/* Initialization/Query functions */
static int UIKit_VideoInit(_THIS);
static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
SDL_DisplayMode * mode);
static void UIKit_VideoQuit(_THIS);
BOOL SDL_UIKit_supports_multiple_displays = NO;
/* DUMMY driver bootstrap functions */
static int
UIKit_Available(void)
{
Sep 27, 2011
Sep 27, 2011
56
return 1;
Jun 22, 2011
Jun 22, 2011
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
}
static void UIKit_DeleteDevice(SDL_VideoDevice * device)
{
SDL_free(device);
}
static SDL_VideoDevice *
UIKit_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
if (device) {
SDL_free(device);
}
return (0);
}
/* Set the function pointers */
device->VideoInit = UIKit_VideoInit;
device->VideoQuit = UIKit_VideoQuit;
device->GetDisplayModes = UIKit_GetDisplayModes;
device->SetDisplayMode = UIKit_SetDisplayMode;
device->PumpEvents = UIKit_PumpEvents;
device->CreateWindow = UIKit_CreateWindow;
Jul 22, 2012
Jul 22, 2012
86
87
device->ShowWindow = UIKit_ShowWindow;
device->HideWindow = UIKit_HideWindow;
Sep 12, 2012
Sep 12, 2012
88
device->RaiseWindow = UIKit_RaiseWindow;
Nov 9, 2011
Nov 9, 2011
89
device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
Jun 22, 2011
Jun 22, 2011
90
91
device->DestroyWindow = UIKit_DestroyWindow;
device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
Sep 27, 2011
Sep 27, 2011
92
Sep 13, 2012
Sep 13, 2012
93
94
/* !!! FIXME: implement SetWindowBordered */
Aug 11, 2012
Aug 11, 2012
95
96
97
98
99
device->SDL_HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
device->SDL_ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
device->SDL_HideScreenKeyboard = UIKit_HideScreenKeyboard;
device->SDL_ToggleScreenKeyboard = UIKit_ToggleScreenKeyboard;
device->SDL_IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
Sep 27, 2011
Sep 27, 2011
100
Jun 22, 2011
Jun 22, 2011
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* OpenGL (ES) functions */
device->GL_MakeCurrent = UIKit_GL_MakeCurrent;
device->GL_SwapWindow = UIKit_GL_SwapWindow;
device->GL_CreateContext = UIKit_GL_CreateContext;
device->GL_DeleteContext = UIKit_GL_DeleteContext;
device->GL_GetProcAddress = UIKit_GL_GetProcAddress;
device->GL_LoadLibrary = UIKit_GL_LoadLibrary;
device->free = UIKit_DeleteDevice;
device->gl_config.accelerated = 1;
return device;
}
VideoBootStrap UIKIT_bootstrap = {
UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
UIKit_Available, UIKit_CreateDevice
};
/*
!!! FIXME:
The main screen should list a AxB mode for portrait orientation, and then
also list BxA for landscape mode. When setting a given resolution, we should
rotate the view's transform appropriately (extra credit if you check the
accelerometer and rotate the display so it's never upside down).
http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
*/
Jan 25, 2012
Jan 25, 2012
133
static int
Feb 21, 2012
Feb 21, 2012
134
UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
Jan 25, 2012
Jan 25, 2012
135
UIScreenMode * uiscreenmode, CGFloat scale)
Jun 22, 2011
Jun 22, 2011
136
{
Jan 25, 2012
Jan 25, 2012
137
SDL_DisplayModeData *data = NULL;
Feb 21, 2012
Feb 21, 2012
138
Jan 25, 2012
Jan 25, 2012
139
140
141
142
143
144
145
146
147
if (uiscreenmode != nil) {
/* Allocate the display mode data */
data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
if (!data) {
SDL_OutOfMemory();
return -1;
}
data->uiscreenmode = uiscreenmode;
Feb 21, 2012
Feb 21, 2012
148
149
[data->uiscreenmode retain];
Jan 25, 2012
Jan 25, 2012
150
data->scale = scale;
Jun 22, 2011
Jun 22, 2011
151
}
Jan 25, 2012
Jan 25, 2012
152
Feb 21, 2012
Feb 21, 2012
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
mode->driverdata = data;
return 0;
}
static void
UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
{
if (!SDL_UIKit_supports_multiple_displays) {
// Not on at least iPhoneOS 3.2 (versions prior to iPad).
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);
Jan 25, 2012
Jan 25, 2012
179
180
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0;
Feb 21, 2012
Feb 21, 2012
181
182
183
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
return -1;
}
Jan 25, 2012
Jan 25, 2012
184
185
186
187
188
mode.w = w;
mode.h = h;
if (SDL_AddDisplayMode(display, &mode)) {
return 0;
Jun 22, 2011
Jun 22, 2011
189
}
Jan 25, 2012
Jan 25, 2012
190
Feb 21, 2012
Feb 21, 2012
191
192
193
194
195
196
197
// Failure case; free resources
SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode.driverdata;
if (data != NULL) {
[data->uiscreenmode release];
SDL_free(data);
}
Jan 25, 2012
Jan 25, 2012
198
199
return -1;
Jun 22, 2011
Jun 22, 2011
200
201
}
Jan 25, 2012
Jan 25, 2012
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
static int
UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
UIScreenMode * uiscreenmode, BOOL rotated)
{
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
return -1;
}
if (rotated) {
// Add the rotated version
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
return -1;
}
}
return 0;
}
Jun 22, 2011
Jun 22, 2011
219
220
static void
Jan 25, 2012
Jan 25, 2012
221
UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
Jun 22, 2011
Jun 22, 2011
222
{
Jan 25, 2012
Jan 25, 2012
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
if (SDL_UIKit_supports_multiple_displays) {
// availableModes showed up in 3.2 (the iPad and later). We should only
// land here for at least that version of the OS.
for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
BOOL mainscreen = (data->uiscreen == [UIScreen mainScreen]);
CGSize size = [uimode size];
int w = (int)size.width;
int h = (int)size.height;
// Add the native screen resolution.
UIKit_AddDisplayMode(display, w, h, data->scale, uimode, mainscreen);
if (data->scale != 1.0f) {
// Add the native screen resolution divided by its scale.
// This is so devices capable of e.g. 640x960 also advertise
// 320x480.
UIKit_AddDisplayMode(display,
(int)(w / data->scale), (int)(h / data->scale),
1.0f, uimode, mainscreen);
}
}
} else {
const CGRect rect = [data->uiscreen bounds];
UIKit_AddDisplayMode(display,
(int)rect.size.width, (int)rect.size.height,
1.0f, nil, YES);
}
}
static int
UIKit_AddDisplay(UIScreen *uiscreen, CGSize size)
{
// 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.
CGFloat scale;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
scale = [uiscreen scale]; // iOS >= 4.0
} else {
scale = 1.0f; // iOS < 4.0
}
Jun 22, 2011
Jun 22, 2011
272
273
274
275
SDL_VideoDisplay display;
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888;
Jan 25, 2012
Jan 25, 2012
276
277
mode.w = (int)(size.width * scale);
mode.h = (int)(size.height * scale);
Jun 22, 2011
Jun 22, 2011
278
mode.refresh_rate = 0;
Feb 21, 2012
Feb 21, 2012
279
280
UIScreenMode * uiscreenmode = nil;
Oct 18, 2011
Oct 18, 2011
281
282
// UIScreenMode showed up in 3.2 (the iPad and later). We're
// misusing this supports_multiple_displays flag here for that.
Nov 8, 2011
Nov 8, 2011
283
if (SDL_UIKit_supports_multiple_displays) {
Feb 21, 2012
Feb 21, 2012
284
285
286
287
288
uiscreenmode = [uiscreen currentMode];
}
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
return -1;
Oct 18, 2011
Oct 18, 2011
289
}
Jun 22, 2011
Jun 22, 2011
290
291
292
293
294
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
Jan 25, 2012
Jan 25, 2012
295
/* Allocate the display data */
Feb 21, 2012
Feb 21, 2012
296
SDL_DisplayData *data = (SDL_DisplayData *) SDL_malloc(sizeof(*data));
Jan 25, 2012
Jan 25, 2012
297
298
if (!data) {
SDL_OutOfMemory();
Feb 21, 2012
Feb 21, 2012
299
UIKit_FreeDisplayModeData(&display.desktop_mode);
Jan 25, 2012
Jan 25, 2012
300
301
302
return -1;
}
Jun 22, 2011
Jun 22, 2011
303
[uiscreen retain];
Jan 25, 2012
Jan 25, 2012
304
305
306
307
data->uiscreen = uiscreen;
data->scale = scale;
display.driverdata = data;
Jun 22, 2011
Jun 22, 2011
308
SDL_AddVideoDisplay(&display);
Jan 25, 2012
Jan 25, 2012
309
310
return 0;
Jun 22, 2011
Jun 22, 2011
311
312
313
314
315
316
317
318
}
int
UIKit_VideoInit(_THIS)
{
_this->gl_config.driver_loaded = 1;
Nov 20, 2011
Nov 20, 2011
319
320
// this tells us whether we are running on ios >= 3.2
SDL_UIKit_supports_multiple_displays = [UIScreen instancesRespondToSelector:@selector(currentMode)];
Jun 22, 2011
Jun 22, 2011
321
Oct 9, 2011
Oct 9, 2011
322
323
// Add the main screen.
UIScreen *uiscreen = [UIScreen mainScreen];
Sep 29, 2012
Sep 29, 2012
324
CGSize size = [uiscreen bounds].size;
Jan 25, 2012
Jan 25, 2012
325
326
327
328
if (UIKit_AddDisplay(uiscreen, size) < 0) {
return -1;
}
Oct 9, 2011
Oct 9, 2011
329
Jun 22, 2011
Jun 22, 2011
330
331
// 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
Oct 9, 2011
Oct 9, 2011
332
333
// external displays. So, add the other displays (screens in UI speak).
if (SDL_UIKit_supports_multiple_displays) {
Sep 28, 2011
Sep 28, 2011
334
for (UIScreen *uiscreen in [UIScreen screens]) {
Oct 9, 2011
Oct 9, 2011
335
336
// Only add the other screens
if (uiscreen != [UIScreen mainScreen]) {
Sep 29, 2012
Sep 29, 2012
337
size = [uiscreen bounds].size;
Jan 25, 2012
Jan 25, 2012
338
339
340
if (UIKit_AddDisplay(uiscreen, size) < 0) {
return -1;
}
Oct 9, 2011
Oct 9, 2011
341
}
Jun 22, 2011
Jun 22, 2011
342
343
344
345
346
347
348
349
350
351
}
}
/* We're done! */
return 0;
}
static int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
Jan 25, 2012
Jan 25, 2012
352
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
Jun 22, 2011
Jun 22, 2011
353
354
355
356
if (!SDL_UIKit_supports_multiple_displays) {
// Not on at least iPhoneOS 3.2 (versions prior to iPad).
SDL_assert(mode->driverdata == NULL);
} else {
Jan 25, 2012
Jan 25, 2012
357
358
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
[data->uiscreen setCurrentMode:modedata->uiscreenmode];
Nov 10, 2011
Nov 10, 2011
359
Sep 29, 2012
Sep 29, 2012
360
361
362
363
364
365
if (mode->w > mode->h) {
if (!UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
} else if (mode->w < mode->h) {
if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
Nov 10, 2011
Nov 10, 2011
366
}
Jun 22, 2011
Jun 22, 2011
367
368
369
370
371
372
373
374
375
376
377
378
}
return 0;
}
void
UIKit_VideoQuit(_THIS)
{
// Release Objective-C objects, so higher level doesn't free() them.
int i, j;
for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i];
Jan 25, 2012
Jan 25, 2012
379
380
381
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
[data->uiscreen release];
SDL_free(data);
Jun 22, 2011
Jun 22, 2011
382
display->driverdata = NULL;
Feb 21, 2012
Feb 21, 2012
383
UIKit_FreeDisplayModeData(&display->desktop_mode);
Jun 22, 2011
Jun 22, 2011
384
385
for (j = 0; j < display->num_display_modes; j++) {
SDL_DisplayMode *mode = &display->display_modes[j];
Feb 21, 2012
Feb 21, 2012
386
UIKit_FreeDisplayModeData(mode);
Jun 22, 2011
Jun 22, 2011
387
388
389
390
}
}
}
Oct 31, 2011
Oct 31, 2011
391
392
#endif /* SDL_VIDEO_DRIVER_UIKIT */
Jun 22, 2011
Jun 22, 2011
393
/* vi: set ts=4 sw=4 expandtab: */