src/video/uikit/SDL_uikitwindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 16:05:59 -0800
changeset 5056 8b7988f42fcb
parent 4446 8b03a20b320f
child 5154 fb424691cfc7
permissions -rw-r--r--
Added the ability to get the UIKit window through the SDL API.
You can also do this through the native API:
UIWindow *window = [[UIApplication sharedApplication] keyWindow];

Also needed to name the union for events and window info.
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@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
#import "SDL_renderer_sw.h"
slouken@2765
    39
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@5056
    88
int
slouken@5056
    89
UIKit_CreateWindow(_THIS, SDL_Window *window) {
slouken@3685
    90
        
icculus@4446
    91
    SDL_VideoDisplay *display = window->display;
icculus@4446
    92
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@4446
    93
icculus@4446
    94
    // SDL currently puts this window at the start of display's linked list. We rely on this.
icculus@4446
    95
    SDL_assert(display->windows == window);
icculus@4446
    96
icculus@4446
    97
    /* We currently only handle a single window per display on iPhone */
icculus@4446
    98
    if (window->next != NULL) {
icculus@4446
    99
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   100
        return -1;
slouken@3685
   101
    }
icculus@4446
   102
icculus@4446
   103
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   104
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   105
    //  this function.
icculus@4446
   106
    if ([UIScreen mainScreen] != uiscreen) {
icculus@4446
   107
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   108
    }
icculus@4446
   109
icculus@4446
   110
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   111
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   112
    //  that most closely matches the desired window size.
icculus@4446
   113
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   114
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   115
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   116
            if (display->num_display_modes == 0) {
icculus@4446
   117
                _this->GetDisplayModes(_this, display);
icculus@4446
   118
            }
icculus@4446
   119
icculus@4446
   120
            int i;
icculus@4446
   121
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   122
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   123
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   124
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   125
                    bestmode = mode;
icculus@4446
   126
            }
icculus@4446
   127
icculus@4446
   128
            if (bestmode) {
icculus@4446
   129
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   130
                [uiscreen setCurrentMode:uimode];
icculus@4446
   131
                display->desktop_mode = *bestmode;
icculus@4446
   132
                display->current_mode = *bestmode;
icculus@4446
   133
            }
icculus@4446
   134
        }
icculus@4446
   135
    }
icculus@4446
   136
slouken@3685
   137
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   138
    // !!! FIXME: can we have a smaller view?
icculus@4446
   139
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   140
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   141
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   142
    else
icculus@4446
   143
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
icculus@4446
   144
icculus@4446
   145
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   146
        [uiwindow setScreen:uiscreen];
icculus@4446
   147
    }
icculus@4446
   148
slouken@3685
   149
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   150
        [uiwindow release];
slouken@2765
   151
        return -1;
slouken@3685
   152
    }    
slouken@3685
   153
    
slouken@3685
   154
    return 1;
slouken@3685
   155
    
slouken@2765
   156
}
slouken@2765
   157
slouken@5056
   158
void
slouken@5056
   159
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   160
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   161
    if (data) {
icculus@4446
   162
        [data->uiwindow release];
icculus@4446
   163
        SDL_free(data);
icculus@4446
   164
        window->driverdata = NULL;
slouken@3685
   165
    }
slouken@2765
   166
}
slouken@2765
   167
slouken@5056
   168
SDL_bool
slouken@5056
   169
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   170
{
slouken@5056
   171
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   172
slouken@5056
   173
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   174
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   175
        info->info.uikit.window = uiwindow;
slouken@5056
   176
        return SDL_TRUE;
slouken@5056
   177
    } else {
slouken@5056
   178
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   179
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   180
        return SDL_FALSE;
slouken@5056
   181
    }
slouken@5056
   182
}
slouken@5056
   183
slouken@2765
   184
/* vi: set ts=4 sw=4 expandtab: */