src/video/uikit/SDL_uikitwindow.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Nov 2011 00:17:41 -0500
changeset 6064 498e0234dfe6
parent 6044 35448a5ea044
child 6073 9738f2a8eee4
permissions -rw-r--r--
To answer the FIXME, no, we shouldn't force this. The fullscreen flag implies borderless behavior even though the flag isn't set on the window (in case fullscreen is toggled)
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@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@6044
    24
slouken@5056
    25
#include "SDL_syswm.h"
slouken@2765
    26
#include "SDL_video.h"
slouken@2765
    27
#include "SDL_mouse.h"
icculus@4446
    28
#include "SDL_assert.h"
tim@5554
    29
#include "SDL_hints.h"
slouken@2765
    30
#include "../SDL_sysvideo.h"
slouken@2765
    31
#include "../SDL_pixels_c.h"
slouken@2765
    32
#include "../../events/SDL_events_c.h"
slouken@2765
    33
slouken@2765
    34
#include "SDL_uikitvideo.h"
slouken@2765
    35
#include "SDL_uikitevents.h"
slouken@2765
    36
#include "SDL_uikitwindow.h"
slouken@2765
    37
#import "SDL_uikitappdelegate.h"
slouken@2765
    38
slouken@2765
    39
#import "SDL_uikitopenglview.h"
slouken@2765
    40
slouken@2765
    41
#include <Foundation/Foundation.h>
slouken@2765
    42
icculus@5520
    43
icculus@5520
    44
icculus@5520
    45
icculus@4446
    46
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
icculus@4446
    47
{
slouken@5246
    48
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
    49
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
slouken@2765
    50
    SDL_WindowData *data;
kees@6001
    51
slouken@2765
    52
    /* Allocate the window data */
slouken@2765
    53
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
    54
    if (!data) {
slouken@2765
    55
        SDL_OutOfMemory();
slouken@2765
    56
        return -1;
slouken@2765
    57
    }
slouken@2765
    58
    data->uiwindow = uiwindow;
icculus@5520
    59
    data->viewcontroller = nil;
slouken@3685
    60
    data->view = nil;
slouken@3685
    61
slouken@2765
    62
    /* Fill in the SDL window with the window data */
slouken@3685
    63
    {
slouken@2765
    64
        window->x = 0;
slouken@2765
    65
        window->y = 0;
slouken@2765
    66
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
    67
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
    68
    }
kees@6001
    69
slouken@3685
    70
    window->driverdata = data;
icculus@5520
    71
icculus@5520
    72
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iOS, always shown */
slouken@2765
    73
icculus@4446
    74
    // SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
icculus@4446
    75
    // This is only set if the window is on the main screen. Other screens
icculus@4446
    76
    //  just force the window to have the borderless flag.
icculus@5518
    77
    if ([UIScreen mainScreen] != uiscreen) {
icculus@5518
    78
        window->flags &= ~SDL_WINDOW_RESIZABLE;  // window is NEVER resizeable
icculus@5518
    79
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;  // never has input focus
icculus@5520
    80
        window->flags |= SDL_WINDOW_BORDERLESS;  // never has a status bar.
icculus@5518
    81
    } else {
icculus@5518
    82
        window->flags |= SDL_WINDOW_INPUT_FOCUS;  // always has input focus
icculus@5518
    83
slouken@6064
    84
        if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
icculus@4446
    85
            [UIApplication sharedApplication].statusBarHidden = YES;
icculus@4446
    86
        } else {
icculus@4446
    87
            [UIApplication sharedApplication].statusBarHidden = NO;
icculus@4446
    88
        }
icculus@5518
    89
slouken@6044
    90
        //const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
slouken@6044
    91
        //const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
slouken@6044
    92
        //                           (o == UIDeviceOrientationLandscapeRight);
slouken@6044
    93
        //const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
slouken@6044
    94
        //                      ((window->w < window->h) && (landscape)) );
icculus@5518
    95
icculus@5529
    96
        // The View Controller will handle rotating the view when the
icculus@5529
    97
        //  device orientation changes. This will trigger resize events, if
icculus@5529
    98
        //  appropriate.
icculus@5529
    99
        SDL_uikitviewcontroller *controller;
icculus@5529
   100
        controller = [SDL_uikitviewcontroller alloc];
icculus@5529
   101
        data->viewcontroller = [controller initWithSDLWindow:window];
icculus@5529
   102
        [data->viewcontroller setTitle:@"SDL App"];  // !!! FIXME: hook up SDL_SetWindowTitle()
icculus@5529
   103
        // !!! FIXME: if (rotate), force a "resize" right at the start
slouken@3685
   104
    }
icculus@5518
   105
slouken@2765
   106
    return 0;
slouken@2765
   107
}
slouken@2765
   108
slouken@5056
   109
int
slouken@5246
   110
UIKit_CreateWindow(_THIS, SDL_Window *window)
slouken@5246
   111
{
slouken@5246
   112
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
icculus@4446
   113
    UIScreen *uiscreen = (UIScreen *) display->driverdata;
icculus@5529
   114
    const BOOL external = ([UIScreen mainScreen] != uiscreen);
icculus@4446
   115
icculus@4446
   116
    // SDL currently puts this window at the start of display's linked list. We rely on this.
slouken@5251
   117
    SDL_assert(_this->windows == window);
icculus@4446
   118
icculus@5520
   119
    /* We currently only handle a single window per display on iOS */
icculus@4446
   120
    if (window->next != NULL) {
icculus@4446
   121
        SDL_SetError("Only one window allowed per display.");
slouken@3685
   122
        return -1;
slouken@3685
   123
    }
icculus@4446
   124
icculus@4446
   125
    // Non-mainscreen windows must be force to borderless, as there's no
icculus@4446
   126
    //  status bar there, and we want to get the right dimensions later in
icculus@4446
   127
    //  this function.
icculus@5529
   128
    if (external) {
icculus@4446
   129
        window->flags |= SDL_WINDOW_BORDERLESS;
icculus@4446
   130
    }
icculus@4446
   131
icculus@4446
   132
    // If monitor has a resolution of 0x0 (hasn't been explicitly set by the
icculus@4446
   133
    //  user, so it's in standby), try to force the display to a resolution
icculus@4446
   134
    //  that most closely matches the desired window size.
icculus@4446
   135
    if (SDL_UIKit_supports_multiple_displays) {
icculus@4446
   136
        const CGSize origsize = [[uiscreen currentMode] size];
icculus@4446
   137
        if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
icculus@4446
   138
            if (display->num_display_modes == 0) {
icculus@4446
   139
                _this->GetDisplayModes(_this, display);
icculus@4446
   140
            }
icculus@4446
   141
icculus@4446
   142
            int i;
icculus@4446
   143
            const SDL_DisplayMode *bestmode = NULL;
icculus@4446
   144
            for (i = display->num_display_modes; i >= 0; i--) {
icculus@4446
   145
                const SDL_DisplayMode *mode = &display->display_modes[i];
icculus@4446
   146
                if ((mode->w >= window->w) && (mode->h >= window->h))
icculus@4446
   147
                    bestmode = mode;
icculus@4446
   148
            }
icculus@4446
   149
icculus@4446
   150
            if (bestmode) {
icculus@4446
   151
                UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
icculus@4446
   152
                [uiscreen setCurrentMode:uimode];
icculus@5997
   153
icculus@5997
   154
                // desktop_mode doesn't change here (the higher level will
icculus@5997
   155
                //  use it to set all the screens back to their defaults
icculus@5997
   156
                //  upon window destruction, SDL_Quit(), etc.
icculus@5997
   157
                [((UIScreenMode *) display->current_mode.driverdata) release];
icculus@4446
   158
                display->current_mode = *bestmode;
icculus@5997
   159
                [((UIScreenMode *) display->current_mode.driverdata) retain];
icculus@4446
   160
            }
icculus@4446
   161
        }
icculus@4446
   162
    }
icculus@4446
   163
slouken@3685
   164
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   165
    // !!! FIXME: can we have a smaller view?
icculus@4446
   166
    UIWindow *uiwindow = [UIWindow alloc];
slouken@6064
   167
    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS))
icculus@4446
   168
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   169
    else
icculus@4446
   170
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
kees@6001
   171
icculus@5529
   172
    // put the window on an external display if appropriate. This implicitly
icculus@5529
   173
    //  does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
icculus@5529
   174
    //  main display, where we land by default, as that would eat the
icculus@5529
   175
    //  status bar real estate.
icculus@5529
   176
    if (external) {
icculus@4446
   177
        [uiwindow setScreen:uiscreen];
icculus@4446
   178
    }
icculus@4446
   179
slouken@3685
   180
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   181
        [uiwindow release];
slouken@2765
   182
        return -1;
kees@6001
   183
    }
kees@6001
   184
slouken@3685
   185
    return 1;
kees@6001
   186
slouken@2765
   187
}
slouken@2765
   188
slouken@5056
   189
void
kees@6003
   190
UIKit_DestroyWindow(_THIS, SDL_Window * window)
kees@6003
   191
{
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@6044
   217
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   218
slouken@2765
   219
/* vi: set ts=4 sw=4 expandtab: */