src/video/uikit/SDL_uikitwindow.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 May 2010 05:08:12 -0400
changeset 4446 8b03a20b320f
parent 3685 64ce267332c6
child 5056 8b7988f42fcb
permissions -rw-r--r--
Much improved multi-display support for iPad.

Fixes most issues and limitations, I think.
slouken@2765
     1
/*
slouken@2765
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@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
#import "SDL_renderer_sw.h"
slouken@2765
    38
slouken@2765
    39
#include <UIKit/UIKit.h>
slouken@2765
    40
#include <Foundation/Foundation.h>
slouken@2765
    41
icculus@4446
    42
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
    43
{
icculus@4446
    44
    SDL_VideoDisplay *display = window->display;
icculus@4446
    45
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
    46
    SDL_WindowData *data;
slouken@3685
    47
        
slouken@2765
    48
    /* Allocate the window data */
slouken@2765
    49
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
    50
    if (!data) {
slouken@2765
    51
        SDL_OutOfMemory();
slouken@2765
    52
        return -1;
slouken@2765
    53
    }
slouken@2765
    54
    data->uiwindow = uiwindow;
slouken@3685
    55
    data->view = nil;
slouken@3685
    56
slouken@2765
    57
    /* Fill in the SDL window with the window data */
slouken@3685
    58
    {
slouken@2765
    59
        window->x = 0;
slouken@2765
    60
        window->y = 0;
slouken@2765
    61
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
    62
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
    63
    }
slouken@3685
    64
    
slouken@3685
    65
    window->driverdata = data;
slouken@3685
    66
    
slouken@3685
    67
    window->flags &= ~SDL_WINDOW_RESIZABLE;        /* window is NEVER resizeable */
slouken@3685
    68
    window->flags |= SDL_WINDOW_OPENGL;            /* window is always OpenGL */
slouken@3685
    69
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
slouken@3685
    70
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iPod touch, always shown */
slouken@3685
    71
    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
slouken@2765
    72
icculus@4446
    73
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
    74
    // This is only set if the window is on the main screen. Other screens
icculus@4446
    75
    //  just force the window to have the borderless flag.
icculus@4446
    76
    if ([UIScreen mainScreen] == uiscreen) {
icculus@4446
    77
        if (window->flags & SDL_WINDOW_BORDERLESS) {
icculus@4446
    78
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
    79
        } else {
icculus@4446
    80
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
    81
        }
slouken@3685
    82
    }
slouken@3685
    83
    
slouken@2765
    84
    return 0;
slouken@3685
    85
    
slouken@2765
    86
}
slouken@2765
    87
slouken@2765
    88
int UIKit_CreateWindow(_THIS, SDL_Window *window) {
slouken@3685
    89
        
icculus@4446
    90
    SDL_VideoDisplay *display = window->display;
icculus@4446
    91
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@4446
    92
icculus@4446
    93
    // SDL currently puts this window at the start of display's linked list. We rely on this.
icculus@4446
    94
    SDL_assert(display->windows == window);
icculus@4446
    95
icculus@4446
    96
    /* We currently only handle a single window per display on iPhone */
icculus@4446
    97
    if (window->next != NULL) {
icculus@4446
    98
        SDL_SetError("Only one window allowed per display.");
slouken@3685
    99
        return -1;
slouken@3685
   100
    }
icculus@4446
   101
icculus@4446
   102
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   103
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   104
    //  this function.
icculus@4446
   105
    if ([UIScreen mainScreen] != uiscreen) {
icculus@4446
   106
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   107
    }
icculus@4446
   108
icculus@4446
   109
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   110
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   111
    //  that most closely matches the desired window size.
icculus@4446
   112
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   113
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   114
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   115
            if (display->num_display_modes == 0) {
icculus@4446
   116
                _this->GetDisplayModes(_this, display);
icculus@4446
   117
            }
icculus@4446
   118
icculus@4446
   119
            int i;
icculus@4446
   120
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   121
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   122
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   123
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   124
                    bestmode = mode;
icculus@4446
   125
            }
icculus@4446
   126
icculus@4446
   127
            if (bestmode) {
icculus@4446
   128
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   129
                [uiscreen setCurrentMode:uimode];
icculus@4446
   130
                display->desktop_mode = *bestmode;
icculus@4446
   131
                display->current_mode = *bestmode;
icculus@4446
   132
            }
icculus@4446
   133
        }
icculus@4446
   134
    }
icculus@4446
   135
slouken@3685
   136
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   137
    // !!! FIXME: can we have a smaller view?
icculus@4446
   138
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   139
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   140
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   141
    else
icculus@4446
   142
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@4446
   143
icculus@4446
   144
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   145
        [uiwindow setScreen:uiscreen];
icculus@4446
   146
    }
icculus@4446
   147
slouken@3685
   148
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   149
        [uiwindow release];
slouken@2765
   150
        return -1;
slouken@3685
   151
    }    
slouken@3685
   152
    
slouken@3685
   153
    return 1;
slouken@3685
   154
    
slouken@2765
   155
}
slouken@2765
   156
slouken@2765
   157
void UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   158
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   159
    if (data) {
icculus@4446
   160
        [data->uiwindow release];
icculus@4446
   161
        SDL_free(data);
icculus@4446
   162
        window->driverdata = NULL;
slouken@3685
   163
    }
slouken@2765
   164
}
slouken@2765
   165
slouken@2765
   166
/* vi: set ts=4 sw=4 expandtab: */