src/video/uikit/SDL_uikitwindow.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 14 Oct 2011 20:47:53 -0400
changeset 5997 69875bbf83d8
parent 5640 04b5b11985c8
child 6014 7a8b72b88d86
permissions -rw-r--r--
Fix up some retain/release issues with UIScreenMode objects.

Fixes Bugzilla #1191.

This patch is based on work done by Vittorio Giovara.
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"
tim@5554
    27
#include "SDL_hints.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
icculus@5520
    42
icculus@5520
    43
icculus@4446
    44
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
    45
{
slouken@5246
    46
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
    47
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
    48
    SDL_WindowData *data;
slouken@3685
    49
        
slouken@2765
    50
    /* Allocate the window data */
slouken@2765
    51
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
    52
    if (!data) {
slouken@2765
    53
        SDL_OutOfMemory();
slouken@2765
    54
        return -1;
slouken@2765
    55
    }
slouken@2765
    56
    data->uiwindow = uiwindow;
icculus@5520
    57
    data->viewcontroller = nil;
slouken@3685
    58
    data->view = nil;
slouken@3685
    59
slouken@2765
    60
    /* Fill in the SDL window with the window data */
slouken@3685
    61
    {
slouken@2765
    62
        window->x = 0;
slouken@2765
    63
        window->y = 0;
slouken@2765
    64
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
    65
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
    66
    }
slouken@3685
    67
    
slouken@3685
    68
    window->driverdata = data;
icculus@5520
    69
icculus@5520
    70
    // !!! FIXME: should we force this? Shouldn't specifying FULLSCREEN
icculus@5520
    71
    // !!! FIXME:  imply BORDERLESS?
slouken@3685
    72
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
icculus@5520
    73
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iOS, always shown */
slouken@2765
    74
icculus@4446
    75
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
    76
    // This is only set if the window is on the main screen. Other screens
icculus@4446
    77
    //  just force the window to have the borderless flag.
icculus@5518
    78
    if ([UIScreen mainScreen] != uiscreen) {
icculus@5518
    79
        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
icculus@5518
    80
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
icculus@5520
    81
        window->flags |= SDL_WINDOW_BORDERLESS;  // never has a status bar.
icculus@5518
    82
    } else {
icculus@5518
    83
        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
icculus@5518
    84
icculus@4446
    85
        if (window->flags & SDL_WINDOW_BORDERLESS) {
icculus@4446
    86
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
    87
        } else {
icculus@4446
    88
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
    89
        }
icculus@5518
    90
icculus@5520
    91
        const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
icculus@5520
    92
        const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
icculus@5520
    93
                                   (o == UIDeviceOrientationLandscapeRight);
icculus@5520
    94
        const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
icculus@5520
    95
                              ((window->w < window->h) && (landscape)) );
icculus@5518
    96
icculus@5529
    97
        // The View Controller will handle rotating the view when the
icculus@5529
    98
        //  device orientation changes. This will trigger resize events, if
icculus@5529
    99
        //  appropriate.
icculus@5529
   100
        SDL_uikitviewcontroller *controller;
icculus@5529
   101
        controller = [SDL_uikitviewcontroller alloc];
icculus@5529
   102
        data->viewcontroller = [controller initWithSDLWindow:window];
icculus@5529
   103
        [data->viewcontroller setTitle:@"SDL App"];  // !!! FIXME: hook up SDL_SetWindowTitle()
icculus@5529
   104
        // !!! FIXME: if (rotate), force a "resize" right at the start
slouken@3685
   105
    }
icculus@5518
   106
slouken@2765
   107
    return 0;
slouken@2765
   108
}
slouken@2765
   109
slouken@5056
   110
int
slouken@5246
   111
UIKit_CreateWindow(_THIS, SDL_Window *window)
slouken@5246
   112
{
slouken@5246
   113
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   114
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@5529
   115
    const BOOL external = ([UIScreen mainScreen] != uiscreen);
icculus@4446
   116
icculus@4446
   117
    // SDL currently puts this window at the start of display's linked list. We rely on this.
slouken@5251
   118
    SDL_assert(_this->windows == window);
icculus@4446
   119
icculus@5520
   120
    /* We currently only handle a single window per display on iOS */
icculus@4446
   121
    if (window->next != NULL) {
icculus@4446
   122
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   123
        return -1;
slouken@3685
   124
    }
icculus@4446
   125
icculus@4446
   126
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   127
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   128
    //  this function.
icculus@5529
   129
    if (external) {
icculus@4446
   130
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   131
    }
icculus@4446
   132
icculus@4446
   133
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   134
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   135
    //  that most closely matches the desired window size.
icculus@4446
   136
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   137
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   138
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   139
            if (display->num_display_modes == 0) {
icculus@4446
   140
                _this->GetDisplayModes(_this, display);
icculus@4446
   141
            }
icculus@4446
   142
icculus@4446
   143
            int i;
icculus@4446
   144
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   145
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   146
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   147
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   148
                    bestmode = mode;
icculus@4446
   149
            }
icculus@4446
   150
icculus@4446
   151
            if (bestmode) {
icculus@4446
   152
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   153
                [uiscreen setCurrentMode:uimode];
icculus@5997
   154
icculus@5997
   155
                // desktop_mode doesn't change here (the higher level will
icculus@5997
   156
                //  use it to set all the screens back to their defaults
icculus@5997
   157
                //  upon window destruction, SDL_Quit(), etc.
icculus@5997
   158
                [((UIScreenMode *) display->current_mode.driverdata) release];
icculus@4446
   159
                display->current_mode = *bestmode;
icculus@5997
   160
                [((UIScreenMode *) display->current_mode.driverdata) retain];
icculus@4446
   161
            }
icculus@4446
   162
        }
icculus@4446
   163
    }
icculus@4446
   164
slouken@3685
   165
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   166
    // !!! FIXME: can we have a smaller view?
icculus@4446
   167
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   168
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   169
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   170
    else
icculus@4446
   171
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@5529
   172
    
icculus@5529
   173
    // put the window on an external display if appropriate. This implicitly
icculus@5529
   174
    //  does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
icculus@5529
   175
    //  main display, where we land by default, as that would eat the
icculus@5529
   176
    //  status bar real estate.
icculus@5529
   177
    if (external) {
icculus@4446
   178
        [uiwindow setScreen:uiscreen];
icculus@4446
   179
    }
icculus@4446
   180
slouken@3685
   181
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   182
        [uiwindow release];
slouken@2765
   183
        return -1;
slouken@3685
   184
    }    
slouken@3685
   185
    
slouken@3685
   186
    return 1;
slouken@3685
   187
    
slouken@2765
   188
}
slouken@2765
   189
slouken@5056
   190
void
slouken@5056
   191
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   192
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   193
    if (data) {
icculus@5520
   194
        [data->viewcontroller release];
icculus@4446
   195
        [data->uiwindow release];
icculus@4446
   196
        SDL_free(data);
icculus@4446
   197
        window->driverdata = NULL;
slouken@3685
   198
    }
slouken@2765
   199
}
slouken@2765
   200
slouken@5056
   201
SDL_bool
slouken@5056
   202
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   203
{
slouken@5056
   204
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   205
slouken@5056
   206
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   207
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   208
        info->info.uikit.window = uiwindow;
slouken@5056
   209
        return SDL_TRUE;
slouken@5056
   210
    } else {
slouken@5056
   211
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   212
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   213
        return SDL_FALSE;
slouken@5056
   214
    }
slouken@5056
   215
}
slouken@5056
   216
slouken@2765
   217
/* vi: set ts=4 sw=4 expandtab: */