src/video/uikit/SDL_uikitwindow.m
author Ryan C. Gordon <icculus@icculus.org>
Mon, 04 Apr 2011 23:38:15 -0400
changeset 5529 8c0d15077360
parent 5527 9a03d2300486
child 5535 96594ac5fd1a
permissions -rw-r--r--
Some more iOS orientation rotation fixes.

- Always use a UIViewController, even if window is not resizable.
- Let non-resizable windows still flip over, so user can hold device with the
correct orientation, but upside down, if that's more comfortable.
- Don't set the UIScreen unless we're forced to, as it resets some state.
- Minor correction with conventions for -[self init] tapdance.
slouken@2765
     1
/*
slouken@2765
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@2765
     4
slouken@2765
     5
    This library is free software; you can redistribute it and/or
slouken@2765
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2765
     7
    License as published by the Free Software Foundation; either
slouken@2765
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2765
     9
slouken@2765
    10
    This library is distributed in the hope that it will be useful,
slouken@2765
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2765
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2765
    13
    Lesser General Public License for more details.
slouken@2765
    14
slouken@2765
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2765
    16
    License along with this library; if not, write to the Free Software
slouken@2765
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2765
    18
slouken@2765
    19
    Sam Lantinga
slouken@2765
    20
    slouken@libsdl.org
slouken@2765
    21
*/
slouken@2765
    22
#include "SDL_config.h"
slouken@2765
    23
slouken@5056
    24
#include "SDL_syswm.h"
slouken@2765
    25
#include "SDL_video.h"
slouken@2765
    26
#include "SDL_mouse.h"
icculus@4446
    27
#include "SDL_assert.h"
slouken@2765
    28
#include "../SDL_sysvideo.h"
slouken@2765
    29
#include "../SDL_pixels_c.h"
slouken@2765
    30
#include "../../events/SDL_events_c.h"
slouken@2765
    31
slouken@2765
    32
#include "SDL_uikitvideo.h"
slouken@2765
    33
#include "SDL_uikitevents.h"
slouken@2765
    34
#include "SDL_uikitwindow.h"
slouken@2765
    35
#import "SDL_uikitappdelegate.h"
slouken@2765
    36
slouken@2765
    37
#import "SDL_uikitopenglview.h"
slouken@2765
    38
slouken@2765
    39
#include <Foundation/Foundation.h>
slouken@2765
    40
icculus@5520
    41
@implementation SDL_uikitviewcontroller
icculus@5520
    42
icculus@5520
    43
- (id)initWithSDLWindow:(SDL_Window *)_window {
icculus@5529
    44
    if ((self = [self init]) == nil) {
icculus@5529
    45
        return nil;
icculus@5529
    46
    }
icculus@5520
    47
    self->window = _window;
icculus@5520
    48
    return self;
icculus@5520
    49
}
icculus@5520
    50
icculus@5520
    51
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
icculus@5529
    52
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
icculus@5529
    53
        return YES;  // any orientation is okay.
icculus@5529
    54
    }
icculus@5529
    55
icculus@5529
    56
    // If not resizable, allow device to orient to other matching sizes
icculus@5529
    57
    //  (that is, let the user turn the device upside down...same screen
icculus@5529
    58
    //   dimensions, but it lets the user place the device where it's most
icculus@5529
    59
    //   comfortable in relation to its physical buttons, headphone jack, etc).
icculus@5529
    60
    switch (orient) {
icculus@5529
    61
        case UIInterfaceOrientationLandscapeLeft:
icculus@5529
    62
        case UIInterfaceOrientationLandscapeRight:
icculus@5529
    63
            return (self->window->w >= self->window->h);
icculus@5529
    64
icculus@5529
    65
        case UIInterfaceOrientationPortrait:
icculus@5529
    66
        case UIInterfaceOrientationPortraitUpsideDown:
icculus@5529
    67
            return (self->window->h >= self->window->w);
icculus@5529
    68
icculus@5529
    69
        default: break;
icculus@5529
    70
    }
icculus@5529
    71
icculus@5529
    72
    return NO;  // Nothing else is acceptable.
icculus@5520
    73
}
icculus@5520
    74
icculus@5520
    75
- (void)loadView  {
icculus@5520
    76
    // do nothing.
icculus@5520
    77
}
icculus@5520
    78
icculus@5520
    79
// Send a resized event when the orientation changes.
icculus@5527
    80
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
icculus@5529
    81
    if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
icculus@5529
    82
        return;   // don't care, we're just flipping over in this case.
icculus@5529
    83
    }
icculus@5529
    84
icculus@5527
    85
    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
icculus@5520
    86
    SDL_WindowData *data = self->window->driverdata;
icculus@5520
    87
    UIWindow *uiwindow = data->uiwindow;
icculus@5529
    88
    UIScreen *uiscreen = [uiwindow screen];
icculus@5529
    89
    const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
icculus@5529
    90
    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
icculus@5520
    91
    const CGSize size = frame.size;
icculus@5520
    92
    int w, h;
icculus@5520
    93
icculus@5520
    94
    switch (toInterfaceOrientation) {
icculus@5520
    95
        case UIInterfaceOrientationPortrait:
icculus@5520
    96
        case UIInterfaceOrientationPortraitUpsideDown:
icculus@5520
    97
            w = (size.width < size.height) ? size.width : size.height;
icculus@5520
    98
            h = (size.width > size.height) ? size.width : size.height;
icculus@5520
    99
            break;
icculus@5520
   100
icculus@5520
   101
        case UIInterfaceOrientationLandscapeLeft:
icculus@5520
   102
        case UIInterfaceOrientationLandscapeRight:
icculus@5520
   103
            w = (size.width > size.height) ? size.width : size.height;
icculus@5520
   104
            h = (size.width < size.height) ? size.width : size.height;
icculus@5520
   105
            break;
icculus@5520
   106
icculus@5520
   107
        default:
icculus@5520
   108
            SDL_assert(0 && "Unexpected interface orientation!");
icculus@5520
   109
            return;
icculus@5520
   110
    }
icculus@5527
   111
icculus@5520
   112
    frame.size.width = w;
icculus@5520
   113
    frame.size.height = h;
icculus@5529
   114
    frame.origin.x = 0;
icculus@5529
   115
    frame.origin.y = 0;
icculus@5529
   116
icculus@5527
   117
    [uiwindow setFrame:frame];
icculus@5527
   118
    [data->view updateFrame];
icculus@5520
   119
    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
icculus@5520
   120
}
icculus@5520
   121
icculus@5520
   122
@end
icculus@5520
   123
icculus@5520
   124
icculus@5520
   125
icculus@4446
   126
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
   127
{
slouken@5246
   128
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   129
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
   130
    SDL_WindowData *data;
slouken@3685
   131
        
slouken@2765
   132
    /* Allocate the window data */
slouken@2765
   133
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
   134
    if (!data) {
slouken@2765
   135
        SDL_OutOfMemory();
slouken@2765
   136
        return -1;
slouken@2765
   137
    }
slouken@2765
   138
    data->uiwindow = uiwindow;
icculus@5520
   139
    data->viewcontroller = nil;
slouken@3685
   140
    data->view = nil;
slouken@3685
   141
slouken@2765
   142
    /* Fill in the SDL window with the window data */
slouken@3685
   143
    {
slouken@2765
   144
        window->x = 0;
slouken@2765
   145
        window->y = 0;
slouken@2765
   146
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
   147
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
   148
    }
slouken@3685
   149
    
slouken@3685
   150
    window->driverdata = data;
icculus@5520
   151
icculus@5520
   152
    // !!! FIXME: should we force this? Shouldn't specifying FULLSCREEN
icculus@5520
   153
    // !!! FIXME:  imply BORDERLESS?
slouken@3685
   154
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
icculus@5520
   155
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iOS, always shown */
slouken@2765
   156
icculus@4446
   157
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
   158
    // This is only set if the window is on the main screen. Other screens
icculus@4446
   159
    //  just force the window to have the borderless flag.
icculus@5518
   160
    if ([UIScreen mainScreen] != uiscreen) {
icculus@5518
   161
        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
icculus@5518
   162
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
icculus@5520
   163
        window->flags |= SDL_WINDOW_BORDERLESS;  // never has a status bar.
icculus@5518
   164
    } else {
icculus@5518
   165
        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
icculus@5518
   166
icculus@4446
   167
        if (window->flags & SDL_WINDOW_BORDERLESS) {
icculus@4446
   168
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
   169
        } else {
icculus@4446
   170
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
   171
        }
icculus@5518
   172
icculus@5520
   173
        const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
icculus@5520
   174
        const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
icculus@5520
   175
                                   (o == UIDeviceOrientationLandscapeRight);
icculus@5520
   176
        const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
icculus@5520
   177
                              ((window->w < window->h) && (landscape)) );
icculus@5518
   178
icculus@5529
   179
        // The View Controller will handle rotating the view when the
icculus@5529
   180
        //  device orientation changes. This will trigger resize events, if
icculus@5529
   181
        //  appropriate.
icculus@5529
   182
        SDL_uikitviewcontroller *controller;
icculus@5529
   183
        controller = [SDL_uikitviewcontroller alloc];
icculus@5529
   184
        data->viewcontroller = [controller initWithSDLWindow:window];
icculus@5529
   185
        [data->viewcontroller setTitle:@"SDL App"];  // !!! FIXME: hook up SDL_SetWindowTitle()
icculus@5529
   186
        // !!! FIXME: if (rotate), force a "resize" right at the start
slouken@3685
   187
    }
icculus@5518
   188
slouken@2765
   189
    return 0;
slouken@2765
   190
}
slouken@2765
   191
slouken@5056
   192
int
slouken@5246
   193
UIKit_CreateWindow(_THIS, SDL_Window *window)
slouken@5246
   194
{
slouken@5246
   195
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   196
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@5529
   197
    const BOOL external = ([UIScreen mainScreen] != uiscreen);
icculus@4446
   198
icculus@4446
   199
    // SDL currently puts this window at the start of display's linked list. We rely on this.
slouken@5251
   200
    SDL_assert(_this->windows == window);
icculus@4446
   201
icculus@5520
   202
    /* We currently only handle a single window per display on iOS */
icculus@4446
   203
    if (window->next != NULL) {
icculus@4446
   204
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   205
        return -1;
slouken@3685
   206
    }
icculus@4446
   207
icculus@4446
   208
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   209
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   210
    //  this function.
icculus@5529
   211
    if (external) {
icculus@4446
   212
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   213
    }
icculus@4446
   214
icculus@4446
   215
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   216
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   217
    //  that most closely matches the desired window size.
icculus@4446
   218
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   219
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   220
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   221
            if (display->num_display_modes == 0) {
icculus@4446
   222
                _this->GetDisplayModes(_this, display);
icculus@4446
   223
            }
icculus@4446
   224
icculus@4446
   225
            int i;
icculus@4446
   226
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   227
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   228
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   229
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   230
                    bestmode = mode;
icculus@4446
   231
            }
icculus@4446
   232
icculus@4446
   233
            if (bestmode) {
icculus@4446
   234
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   235
                [uiscreen setCurrentMode:uimode];
icculus@4446
   236
                display->desktop_mode = *bestmode;
icculus@4446
   237
                display->current_mode = *bestmode;
icculus@4446
   238
            }
icculus@4446
   239
        }
icculus@4446
   240
    }
icculus@4446
   241
slouken@3685
   242
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   243
    // !!! FIXME: can we have a smaller view?
icculus@4446
   244
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   245
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   246
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   247
    else
icculus@4446
   248
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@5529
   249
    
icculus@5529
   250
    // put the window on an external display if appropriate. This implicitly
icculus@5529
   251
    //  does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
icculus@5529
   252
    //  main display, where we land by default, as that would eat the
icculus@5529
   253
    //  status bar real estate.
icculus@5529
   254
    if (external) {
icculus@4446
   255
        [uiwindow setScreen:uiscreen];
icculus@4446
   256
    }
icculus@4446
   257
slouken@3685
   258
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   259
        [uiwindow release];
slouken@2765
   260
        return -1;
slouken@3685
   261
    }    
slouken@3685
   262
    
slouken@3685
   263
    return 1;
slouken@3685
   264
    
slouken@2765
   265
}
slouken@2765
   266
slouken@5056
   267
void
slouken@5056
   268
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   269
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   270
    if (data) {
icculus@5520
   271
        [data->viewcontroller release];
icculus@4446
   272
        [data->uiwindow release];
icculus@4446
   273
        SDL_free(data);
icculus@4446
   274
        window->driverdata = NULL;
slouken@3685
   275
    }
slouken@2765
   276
}
slouken@2765
   277
slouken@5056
   278
SDL_bool
slouken@5056
   279
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   280
{
slouken@5056
   281
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   282
slouken@5056
   283
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   284
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   285
        info->info.uikit.window = uiwindow;
slouken@5056
   286
        return SDL_TRUE;
slouken@5056
   287
    } else {
slouken@5056
   288
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   289
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   290
        return SDL_FALSE;
slouken@5056
   291
    }
slouken@5056
   292
}
slouken@5056
   293
slouken@2765
   294
/* vi: set ts=4 sw=4 expandtab: */