src/video/uikit/SDL_uikitwindow.m
author Ryan C. Gordon <icculus@icculus.org>
Mon, 28 Mar 2011 23:21:22 -0400
changeset 5520 09b500e0656d
parent 5518 f84dd424d514
child 5523 6dc03727fadc
permissions -rw-r--r--
Added orientation rotation for iOS.
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@5520
    44
    [self init];
icculus@5520
    45
    self->window = _window;
icculus@5520
    46
    return self;
icculus@5520
    47
}
icculus@5520
    48
icculus@5520
    49
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
icculus@5520
    50
    return YES;
icculus@5520
    51
}
icculus@5520
    52
icculus@5520
    53
- (void)loadView  {
icculus@5520
    54
    // do nothing.
icculus@5520
    55
}
icculus@5520
    56
icculus@5520
    57
// Send a resized event when the orientation changes.
icculus@5520
    58
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
icculus@5520
    59
    SDL_WindowData *data = self->window->driverdata;
icculus@5520
    60
    UIWindow *uiwindow = data->uiwindow;
icculus@5520
    61
    CGRect frame = [uiwindow frame];
icculus@5520
    62
    const CGSize size = frame.size;
icculus@5520
    63
    int w, h;
icculus@5520
    64
icculus@5520
    65
    switch (toInterfaceOrientation) {
icculus@5520
    66
        case UIInterfaceOrientationPortrait:
icculus@5520
    67
        case UIInterfaceOrientationPortraitUpsideDown:
icculus@5520
    68
            w = (size.width < size.height) ? size.width : size.height;
icculus@5520
    69
            h = (size.width > size.height) ? size.width : size.height;
icculus@5520
    70
            break;
icculus@5520
    71
icculus@5520
    72
        case UIInterfaceOrientationLandscapeLeft:
icculus@5520
    73
        case UIInterfaceOrientationLandscapeRight:
icculus@5520
    74
            w = (size.width > size.height) ? size.width : size.height;
icculus@5520
    75
            h = (size.width < size.height) ? size.width : size.height;
icculus@5520
    76
            break;
icculus@5520
    77
icculus@5520
    78
        default:
icculus@5520
    79
            SDL_assert(0 && "Unexpected interface orientation!");
icculus@5520
    80
            return;
icculus@5520
    81
    }
icculus@5520
    82
    self->window->w = w;
icculus@5520
    83
    self->window->h = h;
icculus@5520
    84
    frame.size.width = w;
icculus@5520
    85
    frame.size.height = h;
icculus@5520
    86
    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
icculus@5520
    87
}
icculus@5520
    88
icculus@5520
    89
@end
icculus@5520
    90
icculus@5520
    91
icculus@5520
    92
icculus@4446
    93
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
    94
{
slouken@5246
    95
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
    96
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
    97
    SDL_WindowData *data;
slouken@3685
    98
        
slouken@2765
    99
    /* Allocate the window data */
slouken@2765
   100
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
   101
    if (!data) {
slouken@2765
   102
        SDL_OutOfMemory();
slouken@2765
   103
        return -1;
slouken@2765
   104
    }
slouken@2765
   105
    data->uiwindow = uiwindow;
icculus@5520
   106
    data->viewcontroller = nil;
slouken@3685
   107
    data->view = nil;
slouken@3685
   108
slouken@2765
   109
    /* Fill in the SDL window with the window data */
slouken@3685
   110
    {
slouken@2765
   111
        window->x = 0;
slouken@2765
   112
        window->y = 0;
slouken@2765
   113
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
   114
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
   115
    }
slouken@3685
   116
    
slouken@3685
   117
    window->driverdata = data;
icculus@5520
   118
icculus@5520
   119
    // !!! FIXME: should we force this? Shouldn't specifying FULLSCREEN
icculus@5520
   120
    // !!! FIXME:  imply BORDERLESS?
slouken@3685
   121
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
icculus@5520
   122
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iOS, always shown */
slouken@2765
   123
icculus@4446
   124
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
   125
    // This is only set if the window is on the main screen. Other screens
icculus@4446
   126
    //  just force the window to have the borderless flag.
icculus@5518
   127
    if ([UIScreen mainScreen] != uiscreen) {
icculus@5518
   128
        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
icculus@5518
   129
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
icculus@5520
   130
        window->flags |= SDL_WINDOW_BORDERLESS;  // never has a status bar.
icculus@5518
   131
    } else {
icculus@5518
   132
        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
icculus@5518
   133
icculus@4446
   134
        if (window->flags & SDL_WINDOW_BORDERLESS) {
icculus@4446
   135
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
   136
        } else {
icculus@4446
   137
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
   138
        }
icculus@5518
   139
icculus@5518
   140
        const CGSize uisize = [[uiscreen currentMode] size];
icculus@5520
   141
        const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
icculus@5520
   142
        const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
icculus@5520
   143
                                   (o == UIDeviceOrientationLandscapeRight);
icculus@5520
   144
        const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
icculus@5520
   145
                              ((window->w < window->h) && (landscape)) );
icculus@5518
   146
icculus@5518
   147
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@5520
   148
            // The View Controller will handle rotating the view when the
icculus@5520
   149
            //  device orientation changes. We expose these as resize events.
icculus@5520
   150
            SDL_uikitviewcontroller *controller;
icculus@5520
   151
            controller = [SDL_uikitviewcontroller alloc];
icculus@5520
   152
            data->viewcontroller = [controller initWithSDLWindow:window];
icculus@5520
   153
            [data->viewcontroller setTitle:@"SDL App"];  // !!! FIXME: hook up SDL_SetWindowTitle()
icculus@5520
   154
            // !!! FIXME: if (rotate), force a "resize" right at the start
icculus@5520
   155
        } else {
icculus@5520
   156
            // Rotate the view if we have to, but only on the main screen
icculus@5520
   157
            //  (presumably, an external display doesn't report orientation).
icculus@5520
   158
            if (rotate) {
icculus@5520
   159
                #define D2R(x) (M_PI * (x) / 180.0)   // degrees to radians.
icculus@5520
   160
                [uiwindow setTransform:CGAffineTransformIdentity];
icculus@5520
   161
                [uiwindow setTransform:CGAffineTransformMakeRotation(D2R(90))];
icculus@5520
   162
                #undef D2R
icculus@5520
   163
            }
icculus@5518
   164
        }
slouken@3685
   165
    }
icculus@5518
   166
slouken@2765
   167
    return 0;
slouken@2765
   168
}
slouken@2765
   169
slouken@5056
   170
int
slouken@5246
   171
UIKit_CreateWindow(_THIS, SDL_Window *window)
slouken@5246
   172
{
slouken@5246
   173
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   174
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@4446
   175
icculus@4446
   176
    // SDL currently puts this window at the start of display's linked list. We rely on this.
slouken@5251
   177
    SDL_assert(_this->windows == window);
icculus@4446
   178
icculus@5520
   179
    /* We currently only handle a single window per display on iOS */
icculus@4446
   180
    if (window->next != NULL) {
icculus@4446
   181
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   182
        return -1;
slouken@3685
   183
    }
icculus@4446
   184
icculus@4446
   185
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   186
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   187
    //  this function.
icculus@4446
   188
    if ([UIScreen mainScreen] != uiscreen) {
icculus@4446
   189
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   190
    }
icculus@4446
   191
icculus@4446
   192
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   193
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   194
    //  that most closely matches the desired window size.
icculus@4446
   195
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   196
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   197
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   198
            if (display->num_display_modes == 0) {
icculus@4446
   199
                _this->GetDisplayModes(_this, display);
icculus@4446
   200
            }
icculus@4446
   201
icculus@4446
   202
            int i;
icculus@4446
   203
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   204
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   205
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   206
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   207
                    bestmode = mode;
icculus@4446
   208
            }
icculus@4446
   209
icculus@4446
   210
            if (bestmode) {
icculus@4446
   211
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   212
                [uiscreen setCurrentMode:uimode];
icculus@4446
   213
                display->desktop_mode = *bestmode;
icculus@4446
   214
                display->current_mode = *bestmode;
icculus@4446
   215
            }
icculus@4446
   216
        }
icculus@4446
   217
    }
icculus@4446
   218
slouken@3685
   219
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   220
    // !!! FIXME: can we have a smaller view?
icculus@4446
   221
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   222
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   223
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   224
    else
icculus@4446
   225
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@4446
   226
icculus@4446
   227
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   228
        [uiwindow setScreen:uiscreen];
icculus@4446
   229
    }
icculus@4446
   230
slouken@3685
   231
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   232
        [uiwindow release];
slouken@2765
   233
        return -1;
slouken@3685
   234
    }    
slouken@3685
   235
    
slouken@3685
   236
    return 1;
slouken@3685
   237
    
slouken@2765
   238
}
slouken@2765
   239
slouken@5056
   240
void
slouken@5056
   241
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   242
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   243
    if (data) {
icculus@5520
   244
        [data->viewcontroller release];
icculus@4446
   245
        [data->uiwindow release];
icculus@4446
   246
        SDL_free(data);
icculus@4446
   247
        window->driverdata = NULL;
slouken@3685
   248
    }
slouken@2765
   249
}
slouken@2765
   250
slouken@5056
   251
SDL_bool
slouken@5056
   252
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   253
{
slouken@5056
   254
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   255
slouken@5056
   256
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   257
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   258
        info->info.uikit.window = uiwindow;
slouken@5056
   259
        return SDL_TRUE;
slouken@5056
   260
    } else {
slouken@5056
   261
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   262
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   263
        return SDL_FALSE;
slouken@5056
   264
    }
slouken@5056
   265
}
slouken@5056
   266
slouken@2765
   267
/* vi: set ts=4 sw=4 expandtab: */