Implemented display mode functionality for UIKit.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 29 Apr 2010 22:53:18 -0400
changeset 4443c598e440c87c
parent 4441 e3033ab628a1
child 4444 42e1092225f9
Implemented display mode functionality for UIKit.

The iPad (and iPhoneOS 3.2) introduce both a larger screen and the ability to
enumerate and configure external displays. This hooks up SDL's multi-display
API to this new functionality, and removes the hardcoded iPhone resolution.
src/video/uikit/SDL_uikitvideo.m
     1.1 --- a/src/video/uikit/SDL_uikitvideo.m	Fri Apr 23 02:19:34 2010 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Thu Apr 29 22:53:18 2010 -0400
     1.3 @@ -19,6 +19,9 @@
     1.4      Sam Lantinga
     1.5      slouken@libsdl.org
     1.6  */
     1.7 +
     1.8 +#import <UIKit/UIKit.h>
     1.9 +
    1.10  #include "SDL_config.h"
    1.11  
    1.12  #include "SDL_video.h"
    1.13 @@ -35,14 +38,19 @@
    1.14  #include "SDL_renderer_sw.h"
    1.15  #include "SDL_renderer_gles.h"
    1.16  
    1.17 +#include "SDL_assert.h"
    1.18 +
    1.19  #define UIKITVID_DRIVER_NAME "uikit"
    1.20  
    1.21  /* Initialization/Query functions */
    1.22  static int UIKit_VideoInit(_THIS);
    1.23 +static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    1.24  static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    1.25                                  SDL_DisplayMode * mode);
    1.26  static void UIKit_VideoQuit(_THIS);
    1.27  
    1.28 +static BOOL supports_multiple_displays = NO;
    1.29 +
    1.30  /* DUMMY driver bootstrap functions */
    1.31  
    1.32  static int
    1.33 @@ -74,6 +82,7 @@
    1.34      /* Set the function pointers */
    1.35      device->VideoInit = UIKit_VideoInit;
    1.36      device->VideoQuit = UIKit_VideoQuit;
    1.37 +    device->GetDisplayModes = UIKit_GetDisplayModes;
    1.38      device->SetDisplayMode = UIKit_SetDisplayMode;
    1.39      device->PumpEvents = UIKit_PumpEvents;
    1.40  	device->CreateWindow = UIKit_CreateWindow;
    1.41 @@ -100,33 +109,103 @@
    1.42  };
    1.43  
    1.44  
    1.45 +/*
    1.46 +!!! FIXME:
    1.47 +
    1.48 +The main screen should list a AxB mode for portrait orientation, and then
    1.49 + also list BxA for landscape mode. When setting a given resolution, we should
    1.50 + rotate the view's transform appropriately (extra credit if you check the
    1.51 + accelerometer and rotate the display so it's never upside down).
    1.52 +
    1.53 +  http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
    1.54 +
    1.55 +*/
    1.56 +
    1.57 +static void
    1.58 +UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
    1.59 +{
    1.60 +    const UIScreen *screen = (UIScreen *) display->driverdata;
    1.61 +    SDL_DisplayMode mode;
    1.62 +    SDL_zero(mode);
    1.63 +
    1.64 +    // availableModes showed up in 3.2 (the iPad and later). We should only
    1.65 +    //  land here for at least that version of the OS.
    1.66 +    if (!supports_multiple_displays) {
    1.67 +        const CGRect rect = [screen bounds];
    1.68 +        mode.format = SDL_PIXELFORMAT_ABGR8888;
    1.69 +        mode.w = (int) rect.size.width;
    1.70 +        mode.h = (int) rect.size.height;
    1.71 +        mode.refresh_rate = 0;
    1.72 +        mode.driverdata = NULL;
    1.73 +        SDL_AddDisplayMode(display, &mode);
    1.74 +        return;
    1.75 +    }
    1.76 +
    1.77 +    const NSArray *modes = [screen availableModes];
    1.78 +    const NSUInteger mode_count = [modes count];
    1.79 +    NSUInteger i;
    1.80 +    for (i = 0; i < mode_count; i++) {
    1.81 +        UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
    1.82 +        const CGSize size = [uimode size];
    1.83 +        mode.format = SDL_PIXELFORMAT_ABGR8888;
    1.84 +        mode.w = (int) size.width;
    1.85 +        mode.h = (int) size.height;
    1.86 +        mode.refresh_rate = 0;
    1.87 +        mode.driverdata = uimode;
    1.88 +        [uimode retain];
    1.89 +        SDL_AddDisplayMode(display, &mode);
    1.90 +    }
    1.91 +}
    1.92 +
    1.93 +
    1.94 +static void
    1.95 +UIKit_AddDisplay(UIScreen *screen, int w, int h)
    1.96 +{
    1.97 +    SDL_VideoDisplay display;
    1.98 +    SDL_DisplayMode mode;
    1.99 +
   1.100 +    SDL_zero(mode);
   1.101 +    mode.format = SDL_PIXELFORMAT_ABGR8888;
   1.102 +    mode.w = w;
   1.103 +    mode.h = h;
   1.104 +    mode.refresh_rate = 0;
   1.105 +
   1.106 +    SDL_zero(display);
   1.107 +    display.desktop_mode = mode;
   1.108 +    display.current_mode = mode;
   1.109 +    display.driverdata = screen;
   1.110 +    [screen retain];
   1.111 +    SDL_AddVideoDisplay(&display);
   1.112 +}
   1.113 +
   1.114 +
   1.115  int
   1.116  UIKit_VideoInit(_THIS)
   1.117  {
   1.118 -    SDL_DisplayMode mode;
   1.119 -		
   1.120 -	_this->gl_config.driver_loaded = 1;
   1.121 +    _this->gl_config.driver_loaded = 1;
   1.122  
   1.123 -	SDL_VideoDisplay display;
   1.124 -	SDL_zero(display);
   1.125 +    const float version = [[[UIDevice currentDevice] systemVersion] floatValue];
   1.126 +    supports_multiple_displays = (version >= 3.2f);
   1.127  
   1.128 -    /* Use a 32-bpp desktop mode */
   1.129 -    SDL_zero(mode);
   1.130 -	mode.format = SDL_PIXELFORMAT_ABGR8888;
   1.131 -    mode.w = 320;
   1.132 -    mode.h = 480;
   1.133 -    mode.refresh_rate = 0;
   1.134 -    mode.driverdata = NULL;
   1.135 -		
   1.136 -	display.num_display_modes = 1;
   1.137 -	display.max_display_modes = 1;
   1.138 -	display.display_modes = (SDL_DisplayMode *)SDL_malloc(display.max_display_modes * sizeof(SDL_DisplayMode));
   1.139 -	
   1.140 -	display.display_modes[0] = mode;
   1.141 -	display.desktop_mode = mode;
   1.142 -	display.current_mode = mode;
   1.143 -		
   1.144 -	SDL_AddVideoDisplay(&display);
   1.145 +    // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   1.146 +    //  The iPad added both a larger main screen and the ability to use
   1.147 +    //  external displays.
   1.148 +    if (!supports_multiple_displays) {
   1.149 +        // Just give 'em the whole main screen.
   1.150 +        UIScreen *screen = [UIScreen mainScreen];
   1.151 +        const CGRect rect = [screen bounds];
   1.152 +        UIKit_AddDisplay(screen, (int)rect.size.width, (int)rect.size.height);
   1.153 +    } else {
   1.154 +        const NSArray *screens = [UIScreen screens];
   1.155 +        const NSUInteger screen_count = [screens count];
   1.156 +        NSUInteger i;
   1.157 +        for (i = 0; i < screen_count; i++) {
   1.158 +            // the main screen is the first element in the array.
   1.159 +            UIScreen *screen = (UIScreen *) [screens objectAtIndex:i];
   1.160 +            const CGSize size = [[screen currentMode] size];
   1.161 +            UIKit_AddDisplay(screen, (int) size.width, (int) size.height);
   1.162 +        }
   1.163 +    }
   1.164  
   1.165      /* We're done! */
   1.166      return 0;
   1.167 @@ -135,12 +214,37 @@
   1.168  static int
   1.169  UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   1.170  {
   1.171 +    UIScreen *screen = (UIScreen *) display->driverdata;
   1.172 +    if (!supports_multiple_displays) {
   1.173 +        // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   1.174 +        SDL_assert(mode->driverdata == NULL);
   1.175 +    } else {
   1.176 +        UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   1.177 +        [screen setCurrentMode:uimode];
   1.178 +    }
   1.179 +
   1.180      return 0;
   1.181  }
   1.182  
   1.183  void
   1.184  UIKit_VideoQuit(_THIS)
   1.185  {
   1.186 +    // Release Objective-C objects, so higher level doesn't free() them.
   1.187 +    int i, j;
   1.188 +    for (i = 0; i < _this->num_displays; i++) {
   1.189 +        SDL_VideoDisplay *display = &_this->displays[i];
   1.190 +        UIScreen *screen = (UIScreen *) display->driverdata;
   1.191 +        [((UIScreen *) display->driverdata) release];
   1.192 +        display->driverdata = NULL;
   1.193 +        for (j = 0; j < display->num_display_modes; j++) {
   1.194 +            SDL_DisplayMode *mode = &display->display_modes[j];
   1.195 +            UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   1.196 +            if (uimode) {
   1.197 +                [uimode release];
   1.198 +                mode->driverdata = NULL;
   1.199 +            }
   1.200 +        }
   1.201 +    }
   1.202  }
   1.203  
   1.204  /* vi: set ts=4 sw=4 expandtab: */