src/video/uikit/SDL_uikitwindow.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 22:51:26 +0200
changeset 6003 fddf81967e2d
parent 6001 3ae3d1e58d59
child 6014 7a8b72b88d86
permissions -rw-r--r--
Use a consistent source code format in src/video/uikit

For example:
* Opening bracket of a method/function on a new line at column 0
* space after "if", "while", etc
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;
kees@6001
    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
    }
kees@6001
    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@4446
   154
                display->desktop_mode = *bestmode;
icculus@4446
   155
                display->current_mode = *bestmode;
icculus@4446
   156
            }
icculus@4446
   157
        }
icculus@4446
   158
    }
icculus@4446
   159
slouken@3685
   160
    /* ignore the size user requested, and make a fullscreen window */
icculus@4446
   161
    // !!! FIXME: can we have a smaller view?
icculus@4446
   162
    UIWindow *uiwindow = [UIWindow alloc];
icculus@4446
   163
    if (window->flags & SDL_WINDOW_BORDERLESS)
icculus@4446
   164
        uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
icculus@4446
   165
    else
icculus@4446
   166
        uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
kees@6001
   167
icculus@5529
   168
    // put the window on an external display if appropriate. This implicitly
icculus@5529
   169
    //  does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
icculus@5529
   170
    //  main display, where we land by default, as that would eat the
icculus@5529
   171
    //  status bar real estate.
icculus@5529
   172
    if (external) {
icculus@4446
   173
        [uiwindow setScreen:uiscreen];
icculus@4446
   174
    }
icculus@4446
   175
slouken@3685
   176
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
   177
        [uiwindow release];
slouken@2765
   178
        return -1;
kees@6001
   179
    }
kees@6001
   180
slouken@3685
   181
    return 1;
kees@6001
   182
slouken@2765
   183
}
slouken@2765
   184
slouken@5056
   185
void
kees@6003
   186
UIKit_DestroyWindow(_THIS, SDL_Window * window)
kees@6003
   187
{
slouken@3685
   188
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   189
    if (data) {
icculus@5520
   190
        [data->viewcontroller release];
icculus@4446
   191
        [data->uiwindow release];
icculus@4446
   192
        SDL_free(data);
icculus@4446
   193
        window->driverdata = NULL;
slouken@3685
   194
    }
slouken@2765
   195
}
slouken@2765
   196
slouken@5056
   197
SDL_bool
slouken@5056
   198
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@5056
   199
{
slouken@5056
   200
    UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
slouken@5056
   201
slouken@5056
   202
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@5056
   203
        info->subsystem = SDL_SYSWM_UIKIT;
slouken@5056
   204
        info->info.uikit.window = uiwindow;
slouken@5056
   205
        return SDL_TRUE;
slouken@5056
   206
    } else {
slouken@5056
   207
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@5056
   208
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@5056
   209
        return SDL_FALSE;
slouken@5056
   210
    }
slouken@5056
   211
}
slouken@5056
   212
slouken@2765
   213
/* vi: set ts=4 sw=4 expandtab: */