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