src/video/uikit/SDL_uikitvideo.m
author Ryan C. Gordon
Thu, 29 Apr 2010 22:53:18 -0400
changeset 4443 c598e440c87c
parent 3643 41253c0fa9b3
child 4445 06becafcac89
permissions -rw-r--r--
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.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #import <UIKit/UIKit.h>
    24 
    25 #include "SDL_config.h"
    26 
    27 #include "SDL_video.h"
    28 #include "SDL_mouse.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "../SDL_pixels_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 
    33 #include "SDL_uikitvideo.h"
    34 #include "SDL_uikitevents.h"
    35 #include "SDL_uikitwindow.h"
    36 #include "SDL_uikitopengles.h"
    37 
    38 #include "SDL_renderer_sw.h"
    39 #include "SDL_renderer_gles.h"
    40 
    41 #include "SDL_assert.h"
    42 
    43 #define UIKITVID_DRIVER_NAME "uikit"
    44 
    45 /* Initialization/Query functions */
    46 static int UIKit_VideoInit(_THIS);
    47 static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    48 static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    49                                 SDL_DisplayMode * mode);
    50 static void UIKit_VideoQuit(_THIS);
    51 
    52 static BOOL supports_multiple_displays = NO;
    53 
    54 /* DUMMY driver bootstrap functions */
    55 
    56 static int
    57 UIKit_Available(void)
    58 {
    59 	return (1);
    60 }
    61 
    62 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
    63 {
    64     SDL_free(device);
    65 }
    66 
    67 static SDL_VideoDevice *
    68 UIKit_CreateDevice(int devindex)
    69 {
    70     SDL_VideoDevice *device;
    71 
    72     /* Initialize all variables that we clean on shutdown */
    73     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    74     if (!device) {
    75         SDL_OutOfMemory();
    76         if (device) {
    77             SDL_free(device);
    78         }
    79         return (0);
    80     }
    81 
    82     /* Set the function pointers */
    83     device->VideoInit = UIKit_VideoInit;
    84     device->VideoQuit = UIKit_VideoQuit;
    85     device->GetDisplayModes = UIKit_GetDisplayModes;
    86     device->SetDisplayMode = UIKit_SetDisplayMode;
    87     device->PumpEvents = UIKit_PumpEvents;
    88 	device->CreateWindow = UIKit_CreateWindow;
    89 	device->DestroyWindow = UIKit_DestroyWindow;
    90 	
    91 	
    92 	/* OpenGL (ES) functions */
    93 	device->GL_MakeCurrent		= UIKit_GL_MakeCurrent;
    94 	device->GL_SwapWindow		= UIKit_GL_SwapWindow;
    95 	device->GL_CreateContext	= UIKit_GL_CreateContext;
    96 	device->GL_DeleteContext    = UIKit_GL_DeleteContext;
    97 	device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
    98 	device->GL_LoadLibrary	    = UIKit_GL_LoadLibrary;
    99 	device->free = UIKit_DeleteDevice;
   100 
   101 	device->gl_config.accelerated = 1;
   102 	
   103     return device;
   104 }
   105 
   106 VideoBootStrap UIKIT_bootstrap = {
   107     UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
   108     UIKit_Available, UIKit_CreateDevice
   109 };
   110 
   111 
   112 /*
   113 !!! FIXME:
   114 
   115 The main screen should list a AxB mode for portrait orientation, and then
   116  also list BxA for landscape mode. When setting a given resolution, we should
   117  rotate the view's transform appropriately (extra credit if you check the
   118  accelerometer and rotate the display so it's never upside down).
   119 
   120   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   121 
   122 */
   123 
   124 static void
   125 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   126 {
   127     const UIScreen *screen = (UIScreen *) display->driverdata;
   128     SDL_DisplayMode mode;
   129     SDL_zero(mode);
   130 
   131     // availableModes showed up in 3.2 (the iPad and later). We should only
   132     //  land here for at least that version of the OS.
   133     if (!supports_multiple_displays) {
   134         const CGRect rect = [screen bounds];
   135         mode.format = SDL_PIXELFORMAT_ABGR8888;
   136         mode.w = (int) rect.size.width;
   137         mode.h = (int) rect.size.height;
   138         mode.refresh_rate = 0;
   139         mode.driverdata = NULL;
   140         SDL_AddDisplayMode(display, &mode);
   141         return;
   142     }
   143 
   144     const NSArray *modes = [screen availableModes];
   145     const NSUInteger mode_count = [modes count];
   146     NSUInteger i;
   147     for (i = 0; i < mode_count; i++) {
   148         UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
   149         const CGSize size = [uimode size];
   150         mode.format = SDL_PIXELFORMAT_ABGR8888;
   151         mode.w = (int) size.width;
   152         mode.h = (int) size.height;
   153         mode.refresh_rate = 0;
   154         mode.driverdata = uimode;
   155         [uimode retain];
   156         SDL_AddDisplayMode(display, &mode);
   157     }
   158 }
   159 
   160 
   161 static void
   162 UIKit_AddDisplay(UIScreen *screen, int w, int h)
   163 {
   164     SDL_VideoDisplay display;
   165     SDL_DisplayMode mode;
   166 
   167     SDL_zero(mode);
   168     mode.format = SDL_PIXELFORMAT_ABGR8888;
   169     mode.w = w;
   170     mode.h = h;
   171     mode.refresh_rate = 0;
   172 
   173     SDL_zero(display);
   174     display.desktop_mode = mode;
   175     display.current_mode = mode;
   176     display.driverdata = screen;
   177     [screen retain];
   178     SDL_AddVideoDisplay(&display);
   179 }
   180 
   181 
   182 int
   183 UIKit_VideoInit(_THIS)
   184 {
   185     _this->gl_config.driver_loaded = 1;
   186 
   187     const float version = [[[UIDevice currentDevice] systemVersion] floatValue];
   188     supports_multiple_displays = (version >= 3.2f);
   189 
   190     // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   191     //  The iPad added both a larger main screen and the ability to use
   192     //  external displays.
   193     if (!supports_multiple_displays) {
   194         // Just give 'em the whole main screen.
   195         UIScreen *screen = [UIScreen mainScreen];
   196         const CGRect rect = [screen bounds];
   197         UIKit_AddDisplay(screen, (int)rect.size.width, (int)rect.size.height);
   198     } else {
   199         const NSArray *screens = [UIScreen screens];
   200         const NSUInteger screen_count = [screens count];
   201         NSUInteger i;
   202         for (i = 0; i < screen_count; i++) {
   203             // the main screen is the first element in the array.
   204             UIScreen *screen = (UIScreen *) [screens objectAtIndex:i];
   205             const CGSize size = [[screen currentMode] size];
   206             UIKit_AddDisplay(screen, (int) size.width, (int) size.height);
   207         }
   208     }
   209 
   210     /* We're done! */
   211     return 0;
   212 }
   213 
   214 static int
   215 UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   216 {
   217     UIScreen *screen = (UIScreen *) display->driverdata;
   218     if (!supports_multiple_displays) {
   219         // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   220         SDL_assert(mode->driverdata == NULL);
   221     } else {
   222         UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   223         [screen setCurrentMode:uimode];
   224     }
   225 
   226     return 0;
   227 }
   228 
   229 void
   230 UIKit_VideoQuit(_THIS)
   231 {
   232     // Release Objective-C objects, so higher level doesn't free() them.
   233     int i, j;
   234     for (i = 0; i < _this->num_displays; i++) {
   235         SDL_VideoDisplay *display = &_this->displays[i];
   236         UIScreen *screen = (UIScreen *) display->driverdata;
   237         [((UIScreen *) display->driverdata) release];
   238         display->driverdata = NULL;
   239         for (j = 0; j < display->num_display_modes; j++) {
   240             SDL_DisplayMode *mode = &display->display_modes[j];
   241             UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   242             if (uimode) {
   243                 [uimode release];
   244                 mode->driverdata = NULL;
   245             }
   246         }
   247     }
   248 }
   249 
   250 /* vi: set ts=4 sw=4 expandtab: */