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

Latest commit

 

History

History
330 lines (274 loc) · 11 KB

SDL_uikitwindow.m

File metadata and controls

330 lines (274 loc) · 11 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 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
21
22
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"
Oct 31, 2011
Oct 31, 2011
23
24
#if SDL_VIDEO_DRIVER_UIKIT
Jun 22, 2011
Jun 22, 2011
25
26
27
28
29
30
31
32
33
34
35
#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"
Sep 30, 2012
Sep 30, 2012
36
#include "SDL_uikitmodes.h"
Jun 22, 2011
Jun 22, 2011
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "SDL_uikitwindow.h"
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitopenglview.h"
#include <Foundation/Foundation.h>
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
Jan 25, 2012
Jan 25, 2012
50
51
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
Jun 22, 2011
Jun 22, 2011
52
SDL_WindowData *data;
Sep 27, 2011
Sep 27, 2011
53
Jun 22, 2011
Jun 22, 2011
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Allocate the window data */
data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
if (!data) {
SDL_OutOfMemory();
return -1;
}
data->uiwindow = uiwindow;
data->viewcontroller = nil;
data->view = nil;
/* Fill in the SDL window with the window data */
{
window->x = 0;
window->y = 0;
Nov 10, 2011
Nov 10, 2011
68
Sep 19, 2012
Sep 19, 2012
69
70
71
72
73
74
75
CGRect bounds;
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
bounds = [displaydata->uiscreen bounds];
} else {
bounds = [displaydata->uiscreen applicationFrame];
}
Jan 25, 2012
Jan 25, 2012
76
/* Get frame dimensions in pixels */
Sep 19, 2012
Sep 19, 2012
77
78
int width = (int)(bounds.size.width * displaymodedata->scale);
int height = (int)(bounds.size.height * displaymodedata->scale);
Sep 30, 2012
Sep 30, 2012
79
80
81
82
83
84
// Make sure the width/height are oriented correctly
if (UIKit_IsDisplayLandscape(displaydata->uiscreen) != (width > height)) {
int temp = width;
width = height;
height = temp;
Nov 10, 2011
Nov 10, 2011
85
}
Sep 30, 2012
Sep 30, 2012
86
87
88
window->w = width;
window->h = height;
Jun 22, 2011
Jun 22, 2011
89
}
Sep 27, 2011
Sep 27, 2011
90
Jun 22, 2011
Jun 22, 2011
91
92
window->driverdata = data;
Jan 23, 2012
Jan 23, 2012
93
94
/* only one window on iOS, always shown */
window->flags &= ~SDL_WINDOW_HIDDEN;
Jun 22, 2011
Jun 22, 2011
95
96
97
98
// 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.
Sep 30, 2012
Sep 30, 2012
99
if (displaydata->uiscreen == [UIScreen mainScreen]) {
Jun 22, 2011
Jun 22, 2011
100
window->flags |= SDL_WINDOW_INPUT_FOCUS; // always has input focus
Sep 29, 2012
Sep 29, 2012
101
Sep 30, 2012
Sep 30, 2012
102
103
if ([UIApplication sharedApplication].statusBarHidden) {
window->flags |= SDL_WINDOW_BORDERLESS;
Jun 22, 2011
Jun 22, 2011
104
} else {
Sep 30, 2012
Sep 30, 2012
105
window->flags &= ~SDL_WINDOW_BORDERLESS;
Jun 22, 2011
Jun 22, 2011
106
}
Sep 29, 2012
Sep 29, 2012
107
108
109
110
} 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.
Jun 22, 2011
Jun 22, 2011
111
112
}
Sep 29, 2012
Sep 29, 2012
113
114
115
116
117
118
119
120
// The View Controller will handle rotating the view when the
// device orientation changes. This will trigger resize events, if
// appropriate.
SDL_uikitviewcontroller *controller;
controller = [SDL_uikitviewcontroller alloc];
data->viewcontroller = [controller initWithSDLWindow:window];
[data->viewcontroller setTitle:@"SDL App"]; // !!! FIXME: hook up SDL_SetWindowTitle()
Jun 22, 2011
Jun 22, 2011
121
122
123
124
125
126
127
return 0;
}
int
UIKit_CreateWindow(_THIS, SDL_Window *window)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
Jan 25, 2012
Jan 25, 2012
128
129
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
const BOOL external = ([UIScreen mainScreen] != data->uiscreen);
Jun 22, 2011
Jun 22, 2011
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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) {
SDL_SetError("Only one window allowed per display.");
return -1;
}
// 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 (SDL_UIKit_supports_multiple_displays) {
Jan 25, 2012
Jan 25, 2012
144
const CGSize origsize = [[data->uiscreen currentMode] size];
Jun 22, 2011
Jun 22, 2011
145
146
147
148
149
150
151
152
153
154
155
156
157
158
if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
if (display->num_display_modes == 0) {
_this->GetDisplayModes(_this, display);
}
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;
}
if (bestmode) {
Jan 25, 2012
Jan 25, 2012
159
160
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)bestmode->driverdata;
[data->uiscreen setCurrentMode:modedata->uiscreenmode];
Oct 15, 2011
Oct 15, 2011
161
162
163
164
// 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.
Jun 22, 2011
Jun 22, 2011
165
166
167
168
display->current_mode = *bestmode;
}
}
}
Sep 30, 2012
Sep 30, 2012
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
if (data->uiscreen == [UIScreen mainScreen]) {
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
}
if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
if (window->w > window->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (window->w < window->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}
}
Jun 22, 2011
Jun 22, 2011
189
190
191
192
/* ignore the size user requested, and make a fullscreen window */
// !!! FIXME: can we have a smaller view?
UIWindow *uiwindow = [UIWindow alloc];
Sep 19, 2012
Sep 19, 2012
193
uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
Sep 27, 2011
Sep 27, 2011
194
Jun 22, 2011
Jun 22, 2011
195
196
197
198
199
// 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) {
Jan 25, 2012
Jan 25, 2012
200
[uiwindow setScreen:data->uiscreen];
Jun 22, 2011
Jun 22, 2011
201
}
Jul 21, 2012
Jul 21, 2012
202
Jun 22, 2011
Jun 22, 2011
203
204
205
if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
[uiwindow release];
return -1;
Sep 27, 2011
Sep 27, 2011
206
207
}
Jun 22, 2011
Jun 22, 2011
208
return 1;
Sep 27, 2011
Sep 27, 2011
209
Jun 22, 2011
Jun 22, 2011
210
211
}
Jul 22, 2012
Jul 22, 2012
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
void
UIKit_ShowWindow(_THIS, SDL_Window * window)
{
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
[uiwindow makeKeyAndVisible];
}
void
UIKit_HideWindow(_THIS, SDL_Window * window)
{
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
uiwindow.hidden = YES;
}
Sep 12, 2012
Sep 12, 2012
228
229
230
231
232
233
234
235
236
237
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);
}
Nov 9, 2011
Nov 9, 2011
238
239
240
void
UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
Jan 25, 2012
Jan 25, 2012
241
242
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
Nov 9, 2011
Nov 9, 2011
243
244
245
246
247
248
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
if (fullscreen) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
Sep 19, 2012
Sep 19, 2012
249
250
251
252
253
254
255
}
CGRect bounds;
if (fullscreen) {
bounds = [displaydata->uiscreen bounds];
} else {
bounds = [displaydata->uiscreen applicationFrame];
Nov 9, 2011
Nov 9, 2011
256
}
Nov 10, 2011
Nov 10, 2011
257
Jan 25, 2012
Jan 25, 2012
258
/* Get frame dimensions in pixels */
Sep 19, 2012
Sep 19, 2012
259
260
int width = (int)(bounds.size.width * displaymodedata->scale);
int height = (int)(bounds.size.height * displaymodedata->scale);
Jan 25, 2012
Jan 25, 2012
261
Nov 10, 2011
Nov 10, 2011
262
263
264
265
/* We can pick either width or height here and we'll rotate the
screen to match, so we pick the closest to what we wanted.
*/
if (window->w >= window->h) {
Sep 19, 2012
Sep 19, 2012
266
if (width > height) {
Jan 25, 2012
Jan 25, 2012
267
268
window->w = width;
window->h = height;
Nov 10, 2011
Nov 10, 2011
269
} else {
Jan 25, 2012
Jan 25, 2012
270
271
window->w = height;
window->h = width;
Nov 10, 2011
Nov 10, 2011
272
273
}
} else {
Sep 19, 2012
Sep 19, 2012
274
if (width > height) {
Jan 25, 2012
Jan 25, 2012
275
276
window->w = height;
window->h = width;
Nov 10, 2011
Nov 10, 2011
277
} else {
Jan 25, 2012
Jan 25, 2012
278
279
window->w = width;
window->h = height;
Nov 10, 2011
Nov 10, 2011
280
281
}
}
Nov 9, 2011
Nov 9, 2011
282
283
}
Jun 22, 2011
Jun 22, 2011
284
void
Sep 27, 2011
Sep 27, 2011
285
286
UIKit_DestroyWindow(_THIS, SDL_Window * window)
{
Jun 22, 2011
Jun 22, 2011
287
288
289
290
291
292
293
294
295
296
297
298
299
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
if (data) {
[data->viewcontroller release];
[data->uiwindow release];
SDL_free(data);
window->driverdata = NULL;
}
}
SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
Jul 19, 2012
Jul 19, 2012
300
UIViewController *uiviewcontroller = ((SDL_WindowData *) window->driverdata)->viewcontroller;
Jun 22, 2011
Jun 22, 2011
301
302
303
304
if (info->version.major <= SDL_MAJOR_VERSION) {
info->subsystem = SDL_SYSWM_UIKIT;
info->info.uikit.window = uiwindow;
Jul 19, 2012
Jul 19, 2012
305
info->info.uikit.viewcontroller = uiviewcontroller;
Jun 22, 2011
Jun 22, 2011
306
307
308
309
310
311
312
313
return SDL_TRUE;
} else {
SDL_SetError("Application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
}
Jun 22, 2012
Jun 22, 2012
314
315
316
317
318
319
320
321
322
323
324
325
326
327
int
SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
{
SDL_WindowData *data = window ? (SDL_WindowData *)window->driverdata : NULL;
if (!data || !data->view) {
SDL_SetError("Invalid window or view not set");
return -1;
}
[data->view setAnimationCallback:interval callback:callback callbackParam:callbackParam];
return 0;
}
Oct 31, 2011
Oct 31, 2011
328
329
#endif /* SDL_VIDEO_DRIVER_UIKIT */
Jun 22, 2011
Jun 22, 2011
330
/* vi: set ts=4 sw=4 expandtab: */