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

Latest commit

 

History

History
325 lines (267 loc) · 11.5 KB

SDL_uikitwindow.m

File metadata and controls

325 lines (267 loc) · 11.5 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 8, 2011
Apr 8, 2011
5
6
7
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.
Apr 8, 2011
Apr 8, 2011
9
10
11
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:
Apr 8, 2011
Apr 8, 2011
13
14
15
16
17
18
19
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.
20
21
22
*/
#include "SDL_config.h"
Jan 21, 2011
Jan 21, 2011
23
#include "SDL_syswm.h"
24
25
#include "SDL_video.h"
#include "SDL_mouse.h"
May 2, 2010
May 2, 2010
26
#include "SDL_assert.h"
Jun 10, 2011
Jun 10, 2011
27
#include "SDL_hints.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
#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"
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitopenglview.h"
#include <Foundation/Foundation.h>
Mar 29, 2011
Mar 29, 2011
41
42
43
@implementation SDL_uikitviewcontroller
- (id)initWithSDLWindow:(SDL_Window *)_window {
Apr 5, 2011
Apr 5, 2011
44
45
46
if ((self = [self init]) == nil) {
return nil;
}
Mar 29, 2011
Mar 29, 2011
47
48
49
50
51
self->window = _window;
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
Jun 10, 2011
Jun 10, 2011
52
53
54
55
56
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
const char *orientationsCString;
if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
BOOL rotate = NO;
NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
encoding:NSUTF8StringEncoding];
NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@" "]];
switch (orient) {
case UIInterfaceOrientationLandscapeLeft:
rotate = [orientations containsObject:@"LandscapeLeft"];
break;
case UIInterfaceOrientationLandscapeRight:
rotate = [orientations containsObject:@"LandscapeRight"];
break;
case UIInterfaceOrientationPortrait:
rotate = [orientations containsObject:@"Portrait"];
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotate = [orientations containsObject:@"PortraitUpsideDown"];
break;
default: break;
}
return rotate;
}
Apr 5, 2011
Apr 5, 2011
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
if (self->window->flags & SDL_WINDOW_RESIZABLE) {
return YES; // any orientation is okay.
}
// If not resizable, allow device to orient to other matching sizes
// (that is, let the user turn the device upside down...same screen
// dimensions, but it lets the user place the device where it's most
// comfortable in relation to its physical buttons, headphone jack, etc).
switch (orient) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
return (self->window->w >= self->window->h);
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
return (self->window->h >= self->window->w);
default: break;
}
return NO; // Nothing else is acceptable.
Mar 29, 2011
Mar 29, 2011
104
105
106
107
108
109
110
}
- (void)loadView {
// do nothing.
}
// Send a resized event when the orientation changes.
Apr 3, 2011
Apr 3, 2011
111
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
Apr 5, 2011
Apr 5, 2011
112
113
114
115
if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
return; // don't care, we're just flipping over in this case.
}
Apr 3, 2011
Apr 3, 2011
116
const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
Mar 29, 2011
Mar 29, 2011
117
118
SDL_WindowData *data = self->window->driverdata;
UIWindow *uiwindow = data->uiwindow;
Apr 5, 2011
Apr 5, 2011
119
120
121
UIScreen *uiscreen = [uiwindow screen];
const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
Mar 29, 2011
Mar 29, 2011
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const CGSize size = frame.size;
int w, h;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
w = (size.width < size.height) ? size.width : size.height;
h = (size.width > size.height) ? size.width : size.height;
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
w = (size.width > size.height) ? size.width : size.height;
h = (size.width < size.height) ? size.width : size.height;
break;
default:
SDL_assert(0 && "Unexpected interface orientation!");
return;
}
Apr 3, 2011
Apr 3, 2011
142
Mar 29, 2011
Mar 29, 2011
143
144
frame.size.width = w;
frame.size.height = h;
Apr 5, 2011
Apr 5, 2011
145
146
147
frame.origin.x = 0;
frame.origin.y = 0;
Apr 3, 2011
Apr 3, 2011
148
149
[uiwindow setFrame:frame];
[data->view updateFrame];
Mar 29, 2011
Mar 29, 2011
150
151
152
153
154
155
156
SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
}
@end
May 2, 2010
May 2, 2010
157
158
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
{
Feb 10, 2011
Feb 10, 2011
159
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
May 2, 2010
May 2, 2010
160
UIScreen *uiscreen = (UIScreen *) display->driverdata;
161
SDL_WindowData *data;
Jan 21, 2010
Jan 21, 2010
162
163
164
165
166
167
168
169
/* Allocate the window data */
data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
if (!data) {
SDL_OutOfMemory();
return -1;
}
data->uiwindow = uiwindow;
Mar 29, 2011
Mar 29, 2011
170
data->viewcontroller = nil;
Jan 21, 2010
Jan 21, 2010
171
172
data->view = nil;
173
/* Fill in the SDL window with the window data */
Jan 21, 2010
Jan 21, 2010
174
{
175
176
177
178
179
window->x = 0;
window->y = 0;
window->w = (int)uiwindow.frame.size.width;
window->h = (int)uiwindow.frame.size.height;
}
Jan 21, 2010
Jan 21, 2010
180
181
window->driverdata = data;
Mar 29, 2011
Mar 29, 2011
182
183
184
// !!! FIXME: should we force this? Shouldn't specifying FULLSCREEN
// !!! FIXME: imply BORDERLESS?
Jan 21, 2010
Jan 21, 2010
185
window->flags |= SDL_WINDOW_FULLSCREEN; /* window is always fullscreen */
Mar 29, 2011
Mar 29, 2011
186
window->flags |= SDL_WINDOW_SHOWN; /* only one window on iOS, always shown */
Jan 21, 2010
Jan 21, 2010
187
May 2, 2010
May 2, 2010
188
189
190
// 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.
Mar 27, 2011
Mar 27, 2011
191
192
193
if ([UIScreen mainScreen] != uiscreen) {
window->flags &= ~SDL_WINDOW_RESIZABLE; // window is NEVER resizeable
window->flags &= ~SDL_WINDOW_INPUT_FOCUS; // never has input focus
Mar 29, 2011
Mar 29, 2011
194
window->flags |= SDL_WINDOW_BORDERLESS; // never has a status bar.
Mar 27, 2011
Mar 27, 2011
195
196
197
} else {
window->flags |= SDL_WINDOW_INPUT_FOCUS; // always has input focus
May 2, 2010
May 2, 2010
198
199
200
201
202
if (window->flags & SDL_WINDOW_BORDERLESS) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
Mar 27, 2011
Mar 27, 2011
203
Mar 29, 2011
Mar 29, 2011
204
205
206
207
208
const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
(o == UIDeviceOrientationLandscapeRight);
const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
((window->w < window->h) && (landscape)) );
Mar 27, 2011
Mar 27, 2011
209
Apr 5, 2011
Apr 5, 2011
210
211
212
213
214
215
216
217
// 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()
// !!! FIXME: if (rotate), force a "resize" right at the start
Jan 21, 2010
Jan 21, 2010
218
}
Mar 27, 2011
Mar 27, 2011
219
220
221
222
return 0;
}
Jan 21, 2011
Jan 21, 2011
223
int
Feb 10, 2011
Feb 10, 2011
224
225
226
UIKit_CreateWindow(_THIS, SDL_Window *window)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
May 2, 2010
May 2, 2010
227
UIScreen *uiscreen = (UIScreen *) display->driverdata;
Apr 5, 2011
Apr 5, 2011
228
const BOOL external = ([UIScreen mainScreen] != uiscreen);
May 2, 2010
May 2, 2010
229
230
// SDL currently puts this window at the start of display's linked list. We rely on this.
Feb 11, 2011
Feb 11, 2011
231
SDL_assert(_this->windows == window);
May 2, 2010
May 2, 2010
232
Mar 29, 2011
Mar 29, 2011
233
/* We currently only handle a single window per display on iOS */
May 2, 2010
May 2, 2010
234
235
if (window->next != NULL) {
SDL_SetError("Only one window allowed per display.");
Jan 21, 2010
Jan 21, 2010
236
237
return -1;
}
May 2, 2010
May 2, 2010
238
239
240
241
// Non-mainscreen windows must be force to borderless, as there's no
// status bar there, and we want to get the right dimensions later in
// this function.
Apr 5, 2011
Apr 5, 2011
242
if (external) {
May 2, 2010
May 2, 2010
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
272
window->flags |= SDL_WINDOW_BORDERLESS;
}
// 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) {
const CGSize origsize = [[uiscreen currentMode] size];
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) {
UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
[uiscreen setCurrentMode:uimode];
display->desktop_mode = *bestmode;
display->current_mode = *bestmode;
}
}
}
Jan 21, 2010
Jan 21, 2010
273
/* ignore the size user requested, and make a fullscreen window */
May 2, 2010
May 2, 2010
274
275
276
277
278
279
// !!! FIXME: can we have a smaller view?
UIWindow *uiwindow = [UIWindow alloc];
if (window->flags & SDL_WINDOW_BORDERLESS)
uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
else
uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
Apr 5, 2011
Apr 5, 2011
280
281
282
283
284
285
// 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) {
May 2, 2010
May 2, 2010
286
287
288
[uiwindow setScreen:uiscreen];
}
Jan 21, 2010
Jan 21, 2010
289
if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
290
291
[uiwindow release];
return -1;
Jan 21, 2010
Jan 21, 2010
292
293
294
295
}
return 1;
Jan 21, 2011
Jan 21, 2011
298
299
void
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
Jan 21, 2010
Jan 21, 2010
300
301
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
if (data) {
Mar 29, 2011
Mar 29, 2011
302
[data->viewcontroller release];
May 2, 2010
May 2, 2010
303
304
305
[data->uiwindow release];
SDL_free(data);
window->driverdata = NULL;
Jan 21, 2010
Jan 21, 2010
306
}
Jan 21, 2011
Jan 21, 2011
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
if (info->version.major <= SDL_MAJOR_VERSION) {
info->subsystem = SDL_SYSWM_UIKIT;
info->info.uikit.window = uiwindow;
return SDL_TRUE;
} else {
SDL_SetError("Application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
}
325
/* vi: set ts=4 sw=4 expandtab: */