src/video/uikit/SDL_uikitwindow.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Mar 2011 01:35:19 -0400
changeset 5518 f84dd424d514
parent 5262 b530ef003506
child 5520 09b500e0656d
permissions -rw-r--r--
iOS: Report both landscape and portrait orientation as display modes.
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@4446
    41
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
    42
{
slouken@5246
    43
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
    44
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
    45
    SDL_WindowData *data;
slouken@3685
    46
        
slouken@2765
    47
    /* Allocate the window data */
slouken@2765
    48
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
    49
    if (!data) {
slouken@2765
    50
        SDL_OutOfMemory();
slouken@2765
    51
        return -1;
slouken@2765
    52
    }
slouken@2765
    53
    data->uiwindow = uiwindow;
slouken@3685
    54
    data->view = nil;
slouken@3685
    55
slouken@2765
    56
    /* Fill in the SDL window with the window data */
slouken@3685
    57
    {
slouken@2765
    58
        window->x = 0;
slouken@2765
    59
        window->y = 0;
slouken@2765
    60
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
    61
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
    62
    }
slouken@3685
    63
    
slouken@3685
    64
    window->driverdata = data;
slouken@3685
    65
    
slouken@3685
    66
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
slouken@3685
    67
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iPod touch, always shown */
slouken@2765
    68
icculus@4446
    69
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
    70
    // This is only set if the window is on the main screen. Other screens
icculus@4446
    71
    //  just force the window to have the borderless flag.
icculus@5518
    72
    if ([UIScreen mainScreen] != uiscreen) {
icculus@5518
    73
        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
icculus@5518
    74
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
icculus@5518
    75
    } else {
icculus@5518
    76
        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
icculus@5518
    77
icculus@4446
    78
        if (window->flags & SDL_WINDOW_BORDERLESS) {
icculus@4446
    79
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
    80
        } else {
icculus@4446
    81
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
    82
        }
icculus@5518
    83
icculus@5518
    84
        // Rotate the view if we have to, but only on the main screen
icculus@5518
    85
        //  (presumably, an external display doesn't report orientation).
icculus@5518
    86
        const CGSize uisize = [[uiscreen currentMode] size];
icculus@5518
    87
        if ( ((window->w > window->h) && (uisize.width < uisize.height)) ||
icculus@5518
    88
             ((window->w < window->h) && (uisize.width > uisize.height)) ) {
icculus@5518
    89
            // !!! FIXME: flip orientation.
icculus@5518
    90
        }
icculus@5518
    91
icculus@5518
    92
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@5518
    93
            // !!! FIXME: register for orientation change alerts.
icculus@5518
    94
        }
slouken@3685
    95
    }
icculus@5518
    96
slouken@2765
    97
    return 0;
slouken@3685
    98
    
slouken@2765
    99
}
slouken@2765
   100
slouken@5056
   101
int
slouken@5246
   102
UIKit_CreateWindow(_THIS, SDL_Window *window)
slouken@5246
   103
{
slouken@5246
   104
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   105
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@4446
   106
icculus@4446
   107
    // SDL currently puts this window at the start of display's linked list. We rely on this.
slouken@5251
   108
    SDL_assert(_this->windows == window);
icculus@4446
   109
icculus@4446
   110
    /* We currently only handle a single window per display on iPhone */
icculus@4446
   111
    if (window->next != NULL) {
icculus@4446
   112
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   113
        return -1;
slouken@3685
   114
    }
icculus@4446
   115
icculus@4446
   116
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   117
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   118
    //  this function.
icculus@4446
   119
    if ([UIScreen mainScreen] != uiscreen) {
icculus@4446
   120
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   121
    }
icculus@4446
   122
icculus@4446
   123
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   124
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   125
    //  that most closely matches the desired window size.
icculus@4446
   126
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   127
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   128
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   129
            if (display->num_display_modes == 0) {
icculus@4446
   130
                _this->GetDisplayModes(_this, display);
icculus@4446
   131
            }
icculus@4446
   132
icculus@4446
   133
            int i;
icculus@4446
   134
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   135
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   136
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   137
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   138
                    bestmode = mode;
icculus@4446
   139
            }
icculus@4446
   140
icculus@4446
   141
            if (bestmode) {
icculus@4446
   142
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   143
                [uiscreen setCurrentMode:uimode];
icculus@4446
   144
                display->desktop_mode = *bestmode;
icculus@4446
   145
                display->current_mode = *bestmode;
icculus@4446
   146
            }
icculus@4446
   147
        }
icculus@4446
   148
    }
icculus@4446
   149
slouken@3685
   150
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   151
    // !!! FIXME: can we have a smaller view?
icculus@4446
   152
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   153
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   154
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   155
    else
icculus@4446
   156
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@4446
   157
icculus@4446
   158
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   159
        [uiwindow setScreen:uiscreen];
icculus@4446
   160
    }
icculus@4446
   161
slouken@3685
   162
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   163
        [uiwindow release];
slouken@2765
   164
        return -1;
slouken@3685
   165
    }    
slouken@3685
   166
    
slouken@3685
   167
    return 1;
slouken@3685
   168
    
slouken@2765
   169
}
slouken@2765
   170
slouken@5056
   171
void
slouken@5056
   172
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   173
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   174
    if (data) {
icculus@4446
   175
        [data->uiwindow release];
icculus@4446
   176
        SDL_free(data);
icculus@4446
   177
        window->driverdata = NULL;
slouken@3685
   178
    }
slouken@2765
   179
}
slouken@2765
   180
slouken@5056
   181
SDL_bool
slouken@5056
   182
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   183
{
slouken@5056
   184
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   185
slouken@5056
   186
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   187
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   188
        info->info.uikit.window = uiwindow;
slouken@5056
   189
        return SDL_TRUE;
slouken@5056
   190
    } else {
slouken@5056
   191
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   192
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   193
        return SDL_FALSE;
slouken@5056
   194
    }
slouken@5056
   195
}
slouken@5056
   196
slouken@2765
   197
/* vi: set ts=4 sw=4 expandtab: */