Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
authorSam Lantinga <slouken@libsdl.org>
Sat, 29 Sep 2012 17:23:40 -0700
changeset 651842948831d16e
parent 6517 4a4031308518
child 6519 fc926eedd78a
Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
src/video/SDL_video.c
src/video/uikit/SDL_uikitmodes.h
src/video/uikit/SDL_uikitmodes.m
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitvideo.h
src/video/uikit/SDL_uikitvideo.m
src/video/uikit/SDL_uikitview.m
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Sat Sep 29 17:22:32 2012 -0700
     1.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Sat Sep 29 17:23:40 2012 -0700
     1.3 @@ -98,6 +98,8 @@
     1.4  		56ED04E3118A8EFD00A56AA6 /* SDL_syspower.m in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */; };
     1.5  		93CB792313FC5E5200BD3E05 /* SDL_uikitviewcontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = 93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */; };
     1.6  		93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */ = {isa = PBXBuildFile; fileRef = 93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */; };
     1.7 +		AA126AD41617C5E7005ABC8F /* SDL_uikitmodes.h in Headers */ = {isa = PBXBuildFile; fileRef = AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */; };
     1.8 +		AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */ = {isa = PBXBuildFile; fileRef = AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */; };
     1.9  		AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */ = {isa = PBXBuildFile; fileRef = AA628AD9159369E3005138DD /* SDL_rotate.c */; };
    1.10  		AA628ADC159369E3005138DD /* SDL_rotate.h in Headers */ = {isa = PBXBuildFile; fileRef = AA628ADA159369E3005138DD /* SDL_rotate.h */; };
    1.11  		AA7558981595D55500BBD41B /* begin_code.h in Headers */ = {isa = PBXBuildFile; fileRef = AA7558651595D55500BBD41B /* begin_code.h */; };
    1.12 @@ -349,6 +351,8 @@
    1.13  		56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SDL_syspower.m; path = ../../src/power/uikit/SDL_syspower.m; sourceTree = SOURCE_ROOT; };
    1.14  		93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitviewcontroller.h; sourceTree = "<group>"; };
    1.15  		93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitviewcontroller.m; sourceTree = "<group>"; };
    1.16 +		AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitmodes.h; sourceTree = "<group>"; };
    1.17 +		AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitmodes.m; sourceTree = "<group>"; };
    1.18  		AA628AD9159369E3005138DD /* SDL_rotate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_rotate.c; sourceTree = "<group>"; };
    1.19  		AA628ADA159369E3005138DD /* SDL_rotate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_rotate.h; sourceTree = "<group>"; };
    1.20  		AA7558651595D55500BBD41B /* begin_code.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = begin_code.h; sourceTree = "<group>"; };
    1.21 @@ -837,25 +841,27 @@
    1.22  		FD689F090E26E5D900F90B21 /* uikit */ = {
    1.23  			isa = PBXGroup;
    1.24  			children = (
    1.25 +				FDC656440E560DF800311C8E /* jumphack.c */,
    1.26  				FDC656450E560DF800311C8E /* jumphack.h */,
    1.27 -				FDC656440E560DF800311C8E /* jumphack.c */,
    1.28 +				FDC261780E3A3FC8001C4554 /* keyinfotable.h */,
    1.29 +				FD689FCD0E26E9D400F90B21 /* SDL_uikitappdelegate.h */,
    1.30 +				FD689FCC0E26E9D400F90B21 /* SDL_uikitappdelegate.m */,
    1.31  				FD689F0C0E26E5D900F90B21 /* SDL_uikitevents.h */,
    1.32  				FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */,
    1.33 +				AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */,
    1.34 +				AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */,
    1.35  				FD689F0E0E26E5D900F90B21 /* SDL_uikitopengles.h */,
    1.36  				FD689F0F0E26E5D900F90B21 /* SDL_uikitopengles.m */,
    1.37 +				FD689F160E26E5D900F90B21 /* SDL_uikitopenglview.h */,
    1.38 +				FD689F170E26E5D900F90B21 /* SDL_uikitopenglview.m */,
    1.39  				FD689F100E26E5D900F90B21 /* SDL_uikitvideo.h */,
    1.40  				FD689F110E26E5D900F90B21 /* SDL_uikitvideo.m */,
    1.41 -				FDC261780E3A3FC8001C4554 /* keyinfotable.h */,
    1.42  				FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */,
    1.43  				FD689F130E26E5D900F90B21 /* SDL_uikitview.m */,
    1.44 +				93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */,
    1.45 +				93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */,
    1.46  				FD689F140E26E5D900F90B21 /* SDL_uikitwindow.h */,
    1.47  				FD689F150E26E5D900F90B21 /* SDL_uikitwindow.m */,
    1.48 -				FD689F160E26E5D900F90B21 /* SDL_uikitopenglview.h */,
    1.49 -				FD689F170E26E5D900F90B21 /* SDL_uikitopenglview.m */,
    1.50 -				FD689FCD0E26E9D400F90B21 /* SDL_uikitappdelegate.h */,
    1.51 -				FD689FCC0E26E9D400F90B21 /* SDL_uikitappdelegate.m */,
    1.52 -				93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */,
    1.53 -				93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */,
    1.54  			);
    1.55  			path = uikit;
    1.56  			sourceTree = "<group>";
    1.57 @@ -1234,6 +1240,7 @@
    1.58  				AA7558C81595D55500BBD41B /* SDL_version.h in Headers */,
    1.59  				AA7558C91595D55500BBD41B /* SDL_video.h in Headers */,
    1.60  				AA7558CA1595D55500BBD41B /* SDL.h in Headers */,
    1.61 +				AA126AD41617C5E7005ABC8F /* SDL_uikitmodes.h in Headers */,
    1.62  			);
    1.63  			runOnlyForDeploymentPostprocessing = 0;
    1.64  		};
    1.65 @@ -1450,6 +1457,7 @@
    1.66  				56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.c in Sources */,
    1.67  				93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */,
    1.68  				AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */,
    1.69 +				AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */,
    1.70  			);
    1.71  			runOnlyForDeploymentPostprocessing = 0;
    1.72  		};
     2.1 --- a/src/video/SDL_video.c	Sat Sep 29 17:22:32 2012 -0700
     2.2 +++ b/src/video/SDL_video.c	Sat Sep 29 17:23:40 2012 -0700
     2.3 @@ -623,8 +623,8 @@
     2.4              SDL_GetDisplayBounds(displayIndex-1, rect);
     2.5              rect->x += rect->w;
     2.6          }
     2.7 -        rect->w = display->desktop_mode.w;
     2.8 -        rect->h = display->desktop_mode.h;
     2.9 +        rect->w = display->current_mode.w;
    2.10 +        rect->h = display->current_mode.h;
    2.11      }
    2.12      return 0;
    2.13  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/uikit/SDL_uikitmodes.h	Sat Sep 29 17:23:40 2012 -0700
     3.3 @@ -0,0 +1,49 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +#include "SDL_config.h"
    3.25 +
    3.26 +#ifndef _SDL_uikitmodes_h
    3.27 +#define _SDL_uikitmodes_h
    3.28 +
    3.29 +#include "SDL_uikitvideo.h"
    3.30 +
    3.31 +typedef struct
    3.32 +{
    3.33 +    UIScreen *uiscreen;
    3.34 +    CGFloat scale;
    3.35 +} SDL_DisplayData;
    3.36 +
    3.37 +typedef struct
    3.38 +{
    3.39 +    UIScreenMode *uiscreenmode;
    3.40 +    CGFloat scale;
    3.41 +} SDL_DisplayModeData;
    3.42 +
    3.43 +extern BOOL SDL_UIKit_supports_multiple_displays;
    3.44 +
    3.45 +extern int UIKit_InitModes(_THIS);
    3.46 +extern void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
    3.47 +extern int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
    3.48 +extern void UIKit_QuitModes(_THIS);
    3.49 +
    3.50 +#endif /* _SDL_uikitmodes_h */
    3.51 +
    3.52 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/uikit/SDL_uikitmodes.m	Sat Sep 29 17:23:40 2012 -0700
     4.3 @@ -0,0 +1,282 @@
     4.4 +/*
     4.5 +  Simple DirectMedia Layer
     4.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4.7 +
     4.8 +  This software is provided 'as-is', without any express or implied
     4.9 +  warranty.  In no event will the authors be held liable for any damages
    4.10 +  arising from the use of this software.
    4.11 +
    4.12 +  Permission is granted to anyone to use this software for any purpose,
    4.13 +  including commercial applications, and to alter it and redistribute it
    4.14 +  freely, subject to the following restrictions:
    4.15 +
    4.16 +  1. The origin of this software must not be misrepresented; you must not
    4.17 +     claim that you wrote the original software. If you use this software
    4.18 +     in a product, an acknowledgment in the product documentation would be
    4.19 +     appreciated but is not required.
    4.20 +  2. Altered source versions must be plainly marked as such, and must not be
    4.21 +     misrepresented as being the original software.
    4.22 +  3. This notice may not be removed or altered from any source distribution.
    4.23 +*/
    4.24 +#include "SDL_config.h"
    4.25 +
    4.26 +#if SDL_VIDEO_DRIVER_UIKIT
    4.27 +
    4.28 +#include "SDL_assert.h"
    4.29 +#include "SDL_uikitmodes.h"
    4.30 +
    4.31 +
    4.32 +BOOL SDL_UIKit_supports_multiple_displays = NO;
    4.33 +
    4.34 +
    4.35 +static int
    4.36 +UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
    4.37 +    UIScreenMode * uiscreenmode, CGFloat scale)
    4.38 +{
    4.39 +    SDL_DisplayModeData *data = NULL;
    4.40 +    
    4.41 +    if (uiscreenmode != nil) {
    4.42 +        /* Allocate the display mode data */
    4.43 +        data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
    4.44 +        if (!data) {
    4.45 +            SDL_OutOfMemory();
    4.46 +            return -1;
    4.47 +        }
    4.48 +        
    4.49 +        data->uiscreenmode = uiscreenmode;
    4.50 +        [data->uiscreenmode retain];
    4.51 +        
    4.52 +        data->scale = scale;
    4.53 +    }
    4.54 +    
    4.55 +    mode->driverdata = data;
    4.56 +    
    4.57 +    return 0;
    4.58 +}
    4.59 +
    4.60 +static void
    4.61 +UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
    4.62 +{
    4.63 +    if (!SDL_UIKit_supports_multiple_displays) {
    4.64 +        // Not on at least iPhoneOS 3.2 (versions prior to iPad).
    4.65 +        SDL_assert(mode->driverdata == NULL);
    4.66 +    } else if (mode->driverdata != NULL) {
    4.67 +        SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
    4.68 +        [data->uiscreenmode release];
    4.69 +        SDL_free(data);
    4.70 +        mode->driverdata = NULL;
    4.71 +    }
    4.72 +}
    4.73 +
    4.74 +static int
    4.75 +UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
    4.76 +    UIScreenMode * uiscreenmode, CGFloat scale)
    4.77 +{
    4.78 +    SDL_DisplayMode mode;
    4.79 +    SDL_zero(mode);
    4.80 +    
    4.81 +    mode.format = SDL_PIXELFORMAT_ABGR8888;
    4.82 +    mode.refresh_rate = 0;
    4.83 +    if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
    4.84 +        return -1;
    4.85 +    }
    4.86 +    
    4.87 +    mode.w = w;
    4.88 +    mode.h = h;
    4.89 +    if (SDL_AddDisplayMode(display, &mode)) {
    4.90 +        return 0;
    4.91 +    } else {
    4.92 +        UIKit_FreeDisplayModeData(&mode);
    4.93 +        return -1;
    4.94 +    }
    4.95 +}
    4.96 +
    4.97 +static int
    4.98 +UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
    4.99 +                     UIScreenMode * uiscreenmode, BOOL addRotated)
   4.100 +{
   4.101 +    if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
   4.102 +        return -1;
   4.103 +    }
   4.104 +    
   4.105 +    if (addRotated) {
   4.106 +        // Add the rotated version
   4.107 +        if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
   4.108 +            return -1;
   4.109 +        }
   4.110 +    }
   4.111 +    
   4.112 +    return 0;
   4.113 +}
   4.114 +
   4.115 +static int
   4.116 +UIKit_AddDisplay(UIScreen *uiscreen)
   4.117 +{
   4.118 +    CGSize size = [uiscreen bounds].size;
   4.119 +
   4.120 +    // When dealing with UIKit all coordinates are specified in terms of
   4.121 +    // what Apple refers to as points. On earlier devices without the
   4.122 +    // so called "Retina" display, there is a one to one mapping between
   4.123 +    // points and pixels. In other cases [UIScreen scale] indicates the
   4.124 +    // relationship between points and pixels. Since SDL has no notion
   4.125 +    // of points, we must compensate in all cases where dealing with such
   4.126 +    // units.
   4.127 +    CGFloat scale;
   4.128 +    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
   4.129 +        scale = [uiscreen scale]; // iOS >= 4.0
   4.130 +    } else {
   4.131 +        scale = 1.0f; // iOS < 4.0
   4.132 +    }
   4.133 +	
   4.134 +    SDL_VideoDisplay display;
   4.135 +    SDL_DisplayMode mode;
   4.136 +    SDL_zero(mode);
   4.137 +    mode.format = SDL_PIXELFORMAT_ABGR8888;
   4.138 +    mode.w = (int)(size.width * scale);
   4.139 +    mode.h = (int)(size.height * scale);
   4.140 + 
   4.141 +    UIScreenMode * uiscreenmode = nil;
   4.142 +    // UIScreenMode showed up in 3.2 (the iPad and later). We're
   4.143 +    //  misusing this supports_multiple_displays flag here for that.
   4.144 +    if (SDL_UIKit_supports_multiple_displays) {
   4.145 +        uiscreenmode = [uiscreen currentMode];
   4.146 +    }
   4.147 +    
   4.148 +    if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
   4.149 +        return -1;
   4.150 +    }
   4.151 +
   4.152 +    SDL_zero(display);
   4.153 +    display.desktop_mode = mode;
   4.154 +    display.current_mode = mode;
   4.155 +
   4.156 +    /* Allocate the display data */
   4.157 +    SDL_DisplayData *data = (SDL_DisplayData *) SDL_malloc(sizeof(*data));
   4.158 +    if (!data) {
   4.159 +        SDL_OutOfMemory();
   4.160 +        UIKit_FreeDisplayModeData(&display.desktop_mode);
   4.161 +        return -1;
   4.162 +    }
   4.163 +	
   4.164 +    [uiscreen retain];
   4.165 +    data->uiscreen = uiscreen;
   4.166 +    data->scale = scale;
   4.167 +	
   4.168 +    display.driverdata = data;
   4.169 +    SDL_AddVideoDisplay(&display);
   4.170 +    
   4.171 +    return 0;
   4.172 +}
   4.173 +
   4.174 +
   4.175 +int
   4.176 +UIKit_InitModes(_THIS)
   4.177 +{
   4.178 +    // this tells us whether we are running on ios >= 3.2
   4.179 +    SDL_UIKit_supports_multiple_displays = [UIScreen instancesRespondToSelector:@selector(currentMode)];
   4.180 +
   4.181 +    // Add the main screen.
   4.182 +    if (UIKit_AddDisplay([UIScreen mainScreen]) < 0) {
   4.183 +        return -1;
   4.184 +    }
   4.185 +
   4.186 +    // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   4.187 +    //  The iPad added both a larger main screen and the ability to use
   4.188 +    //  external displays. So, add the other displays (screens in UI speak).
   4.189 +    if (SDL_UIKit_supports_multiple_displays) {
   4.190 +        for (UIScreen *uiscreen in [UIScreen screens]) {
   4.191 +            // Only add the other screens
   4.192 +            if (uiscreen != [UIScreen mainScreen]) {
   4.193 +                if (UIKit_AddDisplay(uiscreen) < 0) {
   4.194 +                    return -1;
   4.195 +                }
   4.196 +            }
   4.197 +        }
   4.198 +    }
   4.199 +
   4.200 +    /* We're done! */
   4.201 +    return 0;
   4.202 +}
   4.203 +
   4.204 +void
   4.205 +UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   4.206 +{
   4.207 +    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   4.208 +
   4.209 +    if (SDL_UIKit_supports_multiple_displays) {
   4.210 +        // availableModes showed up in 3.2 (the iPad and later). We should only
   4.211 +        //  land here for at least that version of the OS.
   4.212 +        for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
   4.213 +            CGSize size = [uimode size];
   4.214 +            int w = (int)size.width;
   4.215 +            int h = (int)size.height;
   4.216 +            BOOL addRotated = (data->uiscreen == [UIScreen mainScreen]);
   4.217 +            
   4.218 +            // Add the native screen resolution.
   4.219 +            UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotated);
   4.220 +
   4.221 +            if (data->scale != 1.0f) {
   4.222 +                // Add the native screen resolution divided by its scale.
   4.223 +                // This is so devices capable of e.g. 640x960 also advertise
   4.224 +                // 320x480.
   4.225 +                UIKit_AddDisplayMode(display,
   4.226 +                    (int)(size.width / data->scale),
   4.227 +                    (int)(size.height / data->scale),
   4.228 +                    1.0f, uimode, addRotated);
   4.229 +            }
   4.230 +        }
   4.231 +    } else {
   4.232 +        const CGRect rect = [data->uiscreen bounds];
   4.233 +        UIKit_AddDisplayMode(display,
   4.234 +            (int)rect.size.width, (int)rect.size.height,
   4.235 +            1.0f, nil, YES);
   4.236 +    } 
   4.237 +}
   4.238 +
   4.239 +int
   4.240 +UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   4.241 +{
   4.242 +    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   4.243 +    if (!SDL_UIKit_supports_multiple_displays) {
   4.244 +        // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   4.245 +        SDL_assert(mode->driverdata == NULL);
   4.246 +    } else {
   4.247 +        SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
   4.248 +        [data->uiscreen setCurrentMode:modedata->uiscreenmode];
   4.249 +
   4.250 +        if (mode->w > mode->h) {
   4.251 +            if (!UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
   4.252 +                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
   4.253 +        } else if (mode->w < mode->h) {
   4.254 +            if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
   4.255 +                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
   4.256 +        }
   4.257 +    }
   4.258 +
   4.259 +    return 0;
   4.260 +}
   4.261 +
   4.262 +void
   4.263 +UIKit_QuitModes(_THIS)
   4.264 +{
   4.265 +    // Release Objective-C objects, so higher level doesn't free() them.
   4.266 +    int i, j;
   4.267 +    for (i = 0; i < _this->num_displays; i++) {
   4.268 +        SDL_VideoDisplay *display = &_this->displays[i];
   4.269 +
   4.270 +        UIKit_FreeDisplayModeData(&display->desktop_mode);
   4.271 +        for (j = 0; j < display->num_display_modes; j++) {
   4.272 +            SDL_DisplayMode *mode = &display->display_modes[j];
   4.273 +            UIKit_FreeDisplayModeData(mode);
   4.274 +        }
   4.275 +
   4.276 +        SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   4.277 +        [data->uiscreen release];
   4.278 +        SDL_free(data);
   4.279 +        display->driverdata = NULL;
   4.280 +    }
   4.281 +}
   4.282 +
   4.283 +#endif /* SDL_VIDEO_DRIVER_UIKIT */
   4.284 +
   4.285 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/uikit/SDL_uikitopengles.m	Sat Sep 29 17:22:32 2012 -0700
     5.2 +++ b/src/video/uikit/SDL_uikitopengles.m	Sat Sep 29 17:23:40 2012 -0700
     5.3 @@ -25,6 +25,7 @@
     5.4  #include "SDL_uikitopengles.h"
     5.5  #include "SDL_uikitopenglview.h"
     5.6  #include "SDL_uikitappdelegate.h"
     5.7 +#include "SDL_uikitmodes.h"
     5.8  #include "SDL_uikitwindow.h"
     5.9  #include "jumphack.h"
    5.10  #include "../SDL_sysvideo.h"
     6.1 --- a/src/video/uikit/SDL_uikitvideo.h	Sat Sep 29 17:22:32 2012 -0700
     6.2 +++ b/src/video/uikit/SDL_uikitvideo.h	Sat Sep 29 17:23:40 2012 -0700
     6.3 @@ -23,23 +23,7 @@
     6.4  
     6.5  #include <UIKit/UIKit.h>
     6.6  
     6.7 -extern BOOL SDL_UIKit_supports_multiple_displays;
     6.8 -
     6.9 -typedef struct SDL_DisplayData SDL_DisplayData;
    6.10 -
    6.11 -struct SDL_DisplayData
    6.12 -{
    6.13 -    UIScreen *uiscreen;
    6.14 -    CGFloat scale;
    6.15 -};
    6.16 -
    6.17 -typedef struct SDL_DisplayModeData SDL_DisplayModeData;
    6.18 -
    6.19 -struct SDL_DisplayModeData
    6.20 -{
    6.21 -    UIScreenMode *uiscreenmode;
    6.22 -    CGFloat scale;
    6.23 -};
    6.24 +#include "../SDL_sysvideo.h"
    6.25  
    6.26  #endif /* _SDL_uikitvideo_h */
    6.27  
     7.1 --- a/src/video/uikit/SDL_uikitvideo.m	Sat Sep 29 17:22:32 2012 -0700
     7.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Sat Sep 29 17:23:40 2012 -0700
     7.3 @@ -32,22 +32,16 @@
     7.4  
     7.5  #include "SDL_uikitvideo.h"
     7.6  #include "SDL_uikitevents.h"
     7.7 +#include "SDL_uikitmodes.h"
     7.8  #include "SDL_uikitwindow.h"
     7.9  #include "SDL_uikitopengles.h"
    7.10  
    7.11 -#include "SDL_assert.h"
    7.12 -
    7.13  #define UIKITVID_DRIVER_NAME "uikit"
    7.14  
    7.15  /* Initialization/Query functions */
    7.16  static int UIKit_VideoInit(_THIS);
    7.17 -static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    7.18 -static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    7.19 -                                SDL_DisplayMode * mode);
    7.20  static void UIKit_VideoQuit(_THIS);
    7.21  
    7.22 -BOOL SDL_UIKit_supports_multiple_displays = NO;
    7.23 -
    7.24  /* DUMMY driver bootstrap functions */
    7.25  
    7.26  static int
    7.27 @@ -118,274 +112,21 @@
    7.28  };
    7.29  
    7.30  
    7.31 -/*
    7.32 -!!! FIXME:
    7.33 -
    7.34 -The main screen should list a AxB mode for portrait orientation, and then
    7.35 - also list BxA for landscape mode. When setting a given resolution, we should
    7.36 - rotate the view's transform appropriately (extra credit if you check the
    7.37 - accelerometer and rotate the display so it's never upside down).
    7.38 -
    7.39 -  http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
    7.40 -
    7.41 -*/
    7.42 -
    7.43 -static int
    7.44 -UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
    7.45 -    UIScreenMode * uiscreenmode, CGFloat scale)
    7.46 -{
    7.47 -    SDL_DisplayModeData *data = NULL;
    7.48 -    
    7.49 -    if (uiscreenmode != nil) {
    7.50 -        /* Allocate the display mode data */
    7.51 -        data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
    7.52 -        if (!data) {
    7.53 -            SDL_OutOfMemory();
    7.54 -            return -1;
    7.55 -        }
    7.56 -        
    7.57 -        data->uiscreenmode = uiscreenmode;
    7.58 -        [data->uiscreenmode retain];
    7.59 -        
    7.60 -        data->scale = scale;
    7.61 -    }
    7.62 -    
    7.63 -    mode->driverdata = data;
    7.64 -    
    7.65 -    return 0;
    7.66 -}
    7.67 -
    7.68 -static void
    7.69 -UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
    7.70 -{
    7.71 -    if (!SDL_UIKit_supports_multiple_displays) {
    7.72 -        // Not on at least iPhoneOS 3.2 (versions prior to iPad).
    7.73 -        SDL_assert(mode->driverdata == NULL);
    7.74 -    } else if (mode->driverdata != NULL) {
    7.75 -        SDL_DisplayModeData *data = (SDL_DisplayModeData *)mode->driverdata;
    7.76 -        [data->uiscreenmode release];
    7.77 -        SDL_free(data);
    7.78 -        mode->driverdata = NULL;
    7.79 -    }
    7.80 -}
    7.81 -
    7.82 -static int
    7.83 -UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
    7.84 -    UIScreenMode * uiscreenmode, CGFloat scale)
    7.85 -{
    7.86 -    SDL_DisplayMode mode;
    7.87 -    SDL_zero(mode);
    7.88 -    
    7.89 -    mode.format = SDL_PIXELFORMAT_ABGR8888;
    7.90 -    mode.refresh_rate = 0;
    7.91 -    if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
    7.92 -        return -1;
    7.93 -    }
    7.94 -    
    7.95 -    mode.w = w;
    7.96 -    mode.h = h;
    7.97 -    if (SDL_AddDisplayMode(display, &mode)) {
    7.98 -        return 0;
    7.99 -    }
   7.100 -    
   7.101 -    // Failure case; free resources
   7.102 -    SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode.driverdata;
   7.103 -    
   7.104 -    if (data != NULL) {
   7.105 -        [data->uiscreenmode release];
   7.106 -        SDL_free(data);
   7.107 -    }
   7.108 -
   7.109 -    return -1;
   7.110 -}
   7.111 -
   7.112 -static int
   7.113 -UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
   7.114 -                     UIScreenMode * uiscreenmode, BOOL rotated)
   7.115 -{
   7.116 -    if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
   7.117 -        return -1;
   7.118 -    }
   7.119 -    
   7.120 -    if (rotated) {
   7.121 -        // Add the rotated version
   7.122 -        if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
   7.123 -            return -1;
   7.124 -        }
   7.125 -    }
   7.126 -    
   7.127 -    return 0;
   7.128 -}
   7.129 -
   7.130 -static void
   7.131 -UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   7.132 -{
   7.133 -    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   7.134 -
   7.135 -    if (SDL_UIKit_supports_multiple_displays) {
   7.136 -        // availableModes showed up in 3.2 (the iPad and later). We should only
   7.137 -        //  land here for at least that version of the OS.
   7.138 -        for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
   7.139 -            BOOL mainscreen = (data->uiscreen == [UIScreen mainScreen]);
   7.140 -            CGSize size = [uimode size];
   7.141 -            int w = (int)size.width;
   7.142 -            int h = (int)size.height;
   7.143 -            
   7.144 -            // Add the native screen resolution.
   7.145 -            UIKit_AddDisplayMode(display, w, h, data->scale, uimode, mainscreen);
   7.146 -            
   7.147 -            if (data->scale != 1.0f) {
   7.148 -                // Add the native screen resolution divided by its scale.
   7.149 -                // This is so devices capable of e.g. 640x960 also advertise
   7.150 -                // 320x480.
   7.151 -                UIKit_AddDisplayMode(display,
   7.152 -                    (int)(w / data->scale), (int)(h / data->scale),
   7.153 -                    1.0f, uimode, mainscreen);
   7.154 -            }
   7.155 -        }
   7.156 -    } else {
   7.157 -        const CGRect rect = [data->uiscreen bounds];
   7.158 -        UIKit_AddDisplayMode(display,
   7.159 -            (int)rect.size.width, (int)rect.size.height,
   7.160 -            1.0f, nil, YES);
   7.161 -    } 
   7.162 -}
   7.163 -
   7.164 -
   7.165 -static int
   7.166 -UIKit_AddDisplay(UIScreen *uiscreen, CGSize size)
   7.167 -{
   7.168 -    // When dealing with UIKit all coordinates are specified in terms of
   7.169 -    // what Apple refers to as points. On earlier devices without the
   7.170 -    // so called "Retina" display, there is a one to one mapping between
   7.171 -    // points and pixels. In other cases [UIScreen scale] indicates the
   7.172 -    // relationship between points and pixels. Since SDL has no notion
   7.173 -    // of points, we must compensate in all cases where dealing with such
   7.174 -    // units.
   7.175 -    CGFloat scale;
   7.176 -    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
   7.177 -        scale = [uiscreen scale]; // iOS >= 4.0
   7.178 -    } else {
   7.179 -        scale = 1.0f; // iOS < 4.0
   7.180 -    }
   7.181 -	
   7.182 -    SDL_VideoDisplay display;
   7.183 -    SDL_DisplayMode mode;
   7.184 -    SDL_zero(mode);
   7.185 -    mode.format = SDL_PIXELFORMAT_ABGR8888;
   7.186 -    mode.w = (int)(size.width * scale);
   7.187 -    mode.h = (int)(size.height * scale);
   7.188 -    mode.refresh_rate = 0;
   7.189 - 
   7.190 -    UIScreenMode * uiscreenmode = nil;
   7.191 -    // UIScreenMode showed up in 3.2 (the iPad and later). We're
   7.192 -    //  misusing this supports_multiple_displays flag here for that.
   7.193 -    if (SDL_UIKit_supports_multiple_displays) {
   7.194 -        uiscreenmode = [uiscreen currentMode];
   7.195 -    }
   7.196 -    
   7.197 -    if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) {
   7.198 -        return -1;
   7.199 -    }
   7.200 -
   7.201 -    SDL_zero(display);
   7.202 -    display.desktop_mode = mode;
   7.203 -    display.current_mode = mode;
   7.204 -
   7.205 -    /* Allocate the display data */
   7.206 -    SDL_DisplayData *data = (SDL_DisplayData *) SDL_malloc(sizeof(*data));
   7.207 -    if (!data) {
   7.208 -        SDL_OutOfMemory();
   7.209 -        UIKit_FreeDisplayModeData(&display.desktop_mode);
   7.210 -        return -1;
   7.211 -    }
   7.212 -	
   7.213 -    [uiscreen retain];
   7.214 -    data->uiscreen = uiscreen;
   7.215 -    data->scale = scale;
   7.216 -	
   7.217 -    display.driverdata = data;
   7.218 -    SDL_AddVideoDisplay(&display);
   7.219 -    
   7.220 -    return 0;
   7.221 -}
   7.222 -
   7.223 -
   7.224  int
   7.225  UIKit_VideoInit(_THIS)
   7.226  {
   7.227      _this->gl_config.driver_loaded = 1;
   7.228  
   7.229 -    // this tells us whether we are running on ios >= 3.2
   7.230 -    SDL_UIKit_supports_multiple_displays = [UIScreen instancesRespondToSelector:@selector(currentMode)];
   7.231 -
   7.232 -    // Add the main screen.
   7.233 -    UIScreen *uiscreen = [UIScreen mainScreen];
   7.234 -    CGSize size = [uiscreen bounds].size;
   7.235 -
   7.236 -    if (UIKit_AddDisplay(uiscreen, size) < 0) {
   7.237 +    if (UIKit_InitModes(_this) < 0) {
   7.238          return -1;
   7.239      }
   7.240 -
   7.241 -    // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   7.242 -    //  The iPad added both a larger main screen and the ability to use
   7.243 -    //  external displays. So, add the other displays (screens in UI speak).
   7.244 -    if (SDL_UIKit_supports_multiple_displays) {
   7.245 -        for (UIScreen *uiscreen in [UIScreen screens]) {
   7.246 -            // Only add the other screens
   7.247 -            if (uiscreen != [UIScreen mainScreen]) {
   7.248 -                size = [uiscreen bounds].size;
   7.249 -                if (UIKit_AddDisplay(uiscreen, size) < 0) {
   7.250 -                    return -1;
   7.251 -                }
   7.252 -            }
   7.253 -        }
   7.254 -    }
   7.255 -
   7.256 -    /* We're done! */
   7.257 -    return 0;
   7.258 -}
   7.259 -
   7.260 -static int
   7.261 -UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   7.262 -{
   7.263 -    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   7.264 -    if (!SDL_UIKit_supports_multiple_displays) {
   7.265 -        // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   7.266 -        SDL_assert(mode->driverdata == NULL);
   7.267 -    } else {
   7.268 -        SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
   7.269 -        [data->uiscreen setCurrentMode:modedata->uiscreenmode];
   7.270 -
   7.271 -        if (mode->w > mode->h) {
   7.272 -            if (!UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
   7.273 -                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
   7.274 -        } else if (mode->w < mode->h) {
   7.275 -            if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
   7.276 -                [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
   7.277 -        }
   7.278 -    }
   7.279 -
   7.280      return 0;
   7.281  }
   7.282  
   7.283  void
   7.284  UIKit_VideoQuit(_THIS)
   7.285  {
   7.286 -    // Release Objective-C objects, so higher level doesn't free() them.
   7.287 -    int i, j;
   7.288 -    for (i = 0; i < _this->num_displays; i++) {
   7.289 -        SDL_VideoDisplay *display = &_this->displays[i];
   7.290 -        SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
   7.291 -        [data->uiscreen release];
   7.292 -        SDL_free(data);
   7.293 -        display->driverdata = NULL;
   7.294 -        UIKit_FreeDisplayModeData(&display->desktop_mode);
   7.295 -        for (j = 0; j < display->num_display_modes; j++) {
   7.296 -            SDL_DisplayMode *mode = &display->display_modes[j];
   7.297 -            UIKit_FreeDisplayModeData(mode);
   7.298 -        }
   7.299 -    }
   7.300 +    UIKit_QuitModes(_this);
   7.301  }
   7.302  
   7.303  #endif /* SDL_VIDEO_DRIVER_UIKIT */
     8.1 --- a/src/video/uikit/SDL_uikitview.m	Sat Sep 29 17:22:32 2012 -0700
     8.2 +++ b/src/video/uikit/SDL_uikitview.m	Sat Sep 29 17:23:40 2012 -0700
     8.3 @@ -22,16 +22,17 @@
     8.4  
     8.5  #if SDL_VIDEO_DRIVER_UIKIT
     8.6  
     8.7 -#import "SDL_uikitview.h"
     8.8 +#include "SDL_uikitview.h"
     8.9  
    8.10  #include "../../events/SDL_keyboard_c.h"
    8.11  #include "../../events/SDL_mouse_c.h"
    8.12  #include "../../events/SDL_touch_c.h"
    8.13  
    8.14  #if SDL_IPHONE_KEYBOARD
    8.15 -#import "keyinfotable.h"
    8.16 -#import "SDL_uikitappdelegate.h"
    8.17 -#import "SDL_uikitwindow.h"
    8.18 +#include "keyinfotable.h"
    8.19 +#include "SDL_uikitappdelegate.h"
    8.20 +#include "SDL_uikitmodes.h"
    8.21 +#include "SDL_uikitwindow.h"
    8.22  #endif
    8.23  
    8.24  @implementation SDL_uikitview
     9.1 --- a/src/video/uikit/SDL_uikitviewcontroller.m	Sat Sep 29 17:22:32 2012 -0700
     9.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Sat Sep 29 17:23:40 2012 -0700
     9.3 @@ -28,9 +28,11 @@
     9.4  #include "../SDL_sysvideo.h"
     9.5  #include "../../events/SDL_events_c.h"
     9.6  
     9.7 -#include "SDL_uikitwindow.h"
     9.8  #include "SDL_uikitviewcontroller.h"
     9.9  #include "SDL_uikitvideo.h"
    9.10 +#include "SDL_uikitmodes.h"
    9.11 +#include "SDL_uikitwindow.h"
    9.12 +
    9.13  
    9.14  #ifndef __IPHONE_6_0
    9.15  // This enum isn't available in older SDKs, but we use it for our own purposes on iOS 5.1 and for the system on iOS 6.0
    10.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sat Sep 29 17:22:32 2012 -0700
    10.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Sat Sep 29 17:23:40 2012 -0700
    10.3 @@ -33,6 +33,7 @@
    10.4  
    10.5  #include "SDL_uikitvideo.h"
    10.6  #include "SDL_uikitevents.h"
    10.7 +#include "SDL_uikitmodes.h"
    10.8  #include "SDL_uikitwindow.h"
    10.9  #import "SDL_uikitappdelegate.h"
   10.10