src/video/uikit/SDL_uikitvideo.m
author Ryan C. Gordon
Sun, 27 Mar 2011 01:35:19 -0400
changeset 5518 f84dd424d514
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rw-r--r--
iOS: Report both landscape and portrait orientation as display modes.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 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_assert.h"
    39 
    40 #define UIKITVID_DRIVER_NAME "uikit"
    41 
    42 /* Initialization/Query functions */
    43 static int UIKit_VideoInit(_THIS);
    44 static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
    45 static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
    46                                 SDL_DisplayMode * mode);
    47 static void UIKit_VideoQuit(_THIS);
    48 
    49 BOOL SDL_UIKit_supports_multiple_displays = NO;
    50 
    51 /* DUMMY driver bootstrap functions */
    52 
    53 static int
    54 UIKit_Available(void)
    55 {
    56     return (1);
    57 }
    58 
    59 static void UIKit_DeleteDevice(SDL_VideoDevice * device)
    60 {
    61     SDL_free(device);
    62 }
    63 
    64 static SDL_VideoDevice *
    65 UIKit_CreateDevice(int devindex)
    66 {
    67     SDL_VideoDevice *device;
    68 
    69     /* Initialize all variables that we clean on shutdown */
    70     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    71     if (!device) {
    72         SDL_OutOfMemory();
    73         if (device) {
    74             SDL_free(device);
    75         }
    76         return (0);
    77     }
    78 
    79     /* Set the function pointers */
    80     device->VideoInit = UIKit_VideoInit;
    81     device->VideoQuit = UIKit_VideoQuit;
    82     device->GetDisplayModes = UIKit_GetDisplayModes;
    83     device->SetDisplayMode = UIKit_SetDisplayMode;
    84     device->PumpEvents = UIKit_PumpEvents;
    85     device->CreateWindow = UIKit_CreateWindow;
    86     device->DestroyWindow = UIKit_DestroyWindow;
    87     device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
    88     
    89     
    90     /* OpenGL (ES) functions */
    91     device->GL_MakeCurrent        = UIKit_GL_MakeCurrent;
    92     device->GL_SwapWindow        = UIKit_GL_SwapWindow;
    93     device->GL_CreateContext    = UIKit_GL_CreateContext;
    94     device->GL_DeleteContext    = UIKit_GL_DeleteContext;
    95     device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
    96     device->GL_LoadLibrary        = UIKit_GL_LoadLibrary;
    97     device->free = UIKit_DeleteDevice;
    98 
    99     device->gl_config.accelerated = 1;
   100 
   101     return device;
   102 }
   103 
   104 VideoBootStrap UIKIT_bootstrap = {
   105     UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
   106     UIKit_Available, UIKit_CreateDevice
   107 };
   108 
   109 
   110 /*
   111 !!! FIXME:
   112 
   113 The main screen should list a AxB mode for portrait orientation, and then
   114  also list BxA for landscape mode. When setting a given resolution, we should
   115  rotate the view's transform appropriately (extra credit if you check the
   116  accelerometer and rotate the display so it's never upside down).
   117 
   118   http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
   119 
   120 */
   121 
   122 static CGSize
   123 UIKit_ForcePortrait(const CGSize size)
   124 {
   125     CGSize retval;
   126     if (size.width < size.height) { // portrait
   127         retval = size;
   128     } else {  // landscape
   129         retval.width = size.height;
   130         retval.height = size.width;
   131     }
   132     return retval;
   133 }
   134 
   135 static CGSize
   136 UIKit_ForceLandscape(const CGSize size)
   137 {
   138     CGSize retval;
   139     if (size.width > size.height) { // landscape
   140         retval = size;
   141     } else {  // portrait
   142         retval.width = size.height;
   143         retval.height = size.width;
   144     }
   145     return retval;
   146 }
   147 
   148 static void
   149 UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   150 {
   151     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   152     SDL_DisplayMode mode;
   153     SDL_zero(mode);
   154 
   155     // availableModes showed up in 3.2 (the iPad and later). We should only
   156     //  land here for at least that version of the OS.
   157     if (!SDL_UIKit_supports_multiple_displays) {
   158         const CGRect rect = [uiscreen bounds];
   159         mode.format = SDL_PIXELFORMAT_ABGR8888;
   160         mode.w = (int) rect.size.width;
   161         mode.h = (int) rect.size.height;
   162         mode.refresh_rate = 0;
   163         mode.driverdata = NULL;
   164         SDL_AddDisplayMode(display, &mode);
   165         mode.w = (int) rect.size.height;  // swap the orientation, add again.
   166         mode.h = (int) rect.size.width;
   167         SDL_AddDisplayMode(display, &mode);
   168         return;
   169     }
   170 
   171     const int ismain = (uiscreen == [UIScreen mainScreen]);
   172     const NSArray *modes = [uiscreen availableModes];
   173     const NSUInteger mode_count = [modes count];
   174     NSUInteger i;
   175     for (i = 0; i < mode_count; i++) {
   176         UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
   177         CGSize size = [uimode size];
   178         mode.format = SDL_PIXELFORMAT_ABGR8888;
   179         mode.refresh_rate = 0;
   180         mode.driverdata = uimode;
   181         mode.w = (int) size.width;
   182         mode.h = (int) size.height;
   183         if (SDL_AddDisplayMode(display, &mode))
   184             [uimode retain];
   185 
   186         if (ismain) {
   187             // Add the mode twice, flipped to portrait and landscape.
   188             //  SDL_AddDisplayMode() will ignore duplicates.
   189             size = UIKit_ForcePortrait([uimode size]);
   190             mode.w = (int) size.width;
   191             mode.h = (int) size.height;
   192             if (SDL_AddDisplayMode(display, &mode))
   193                 [uimode retain];
   194 
   195             size = UIKit_ForceLandscape(size);
   196             mode.w = (int) size.width;
   197             mode.h = (int) size.height;
   198             if (SDL_AddDisplayMode(display, &mode))
   199                 [uimode retain];
   200         }
   201     }
   202 }
   203 
   204 
   205 static void
   206 UIKit_AddDisplay(UIScreen *uiscreen, int w, int h)
   207 {
   208     SDL_VideoDisplay display;
   209     SDL_DisplayMode mode;
   210     SDL_zero(mode);
   211     mode.format = SDL_PIXELFORMAT_ABGR8888;
   212     mode.w = w;
   213     mode.h = h;
   214     mode.refresh_rate = 0;
   215 
   216     SDL_zero(display);
   217     display.desktop_mode = mode;
   218     display.current_mode = mode;
   219 
   220     [uiscreen retain];
   221     display.driverdata = uiscreen;
   222     SDL_AddVideoDisplay(&display);
   223 }
   224 
   225 
   226 int
   227 UIKit_VideoInit(_THIS)
   228 {
   229     _this->gl_config.driver_loaded = 1;
   230 
   231     NSString *reqSysVer = @"3.2";
   232     NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
   233     if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
   234         SDL_UIKit_supports_multiple_displays = YES;
   235 
   236     // If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
   237     //  The iPad added both a larger main screen and the ability to use
   238     //  external displays.
   239     if (!SDL_UIKit_supports_multiple_displays) {
   240         // Just give 'em the whole main screen.
   241         UIScreen *uiscreen = [UIScreen mainScreen];
   242         const CGRect rect = [uiscreen bounds];
   243         UIKit_AddDisplay(uiscreen, (int)rect.size.width, (int)rect.size.height);
   244     } else {
   245         const NSArray *screens = [UIScreen screens];
   246         const NSUInteger screen_count = [screens count];
   247         NSUInteger i;
   248         for (i = 0; i < screen_count; i++) {
   249             // the main screen is the first element in the array.
   250             UIScreen *uiscreen = (UIScreen *) [screens objectAtIndex:i];
   251             const CGSize size = [[uiscreen currentMode] size];
   252             UIKit_AddDisplay(uiscreen, (int) size.width, (int) size.height);
   253         }
   254     }
   255 
   256     /* We're done! */
   257     return 0;
   258 }
   259 
   260 static int
   261 UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   262 {
   263     UIScreen *uiscreen = (UIScreen *) display->driverdata;
   264     if (!SDL_UIKit_supports_multiple_displays) {
   265         // Not on at least iPhoneOS 3.2 (versions prior to iPad).
   266         SDL_assert(mode->driverdata == NULL);
   267     } else {
   268         UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   269         [uiscreen setCurrentMode:uimode];
   270     }
   271 
   272     return 0;
   273 }
   274 
   275 void
   276 UIKit_VideoQuit(_THIS)
   277 {
   278     // Release Objective-C objects, so higher level doesn't free() them.
   279     int i, j;
   280     for (i = 0; i < _this->num_displays; i++) {
   281         SDL_VideoDisplay *display = &_this->displays[i];
   282         UIScreen *uiscreen = (UIScreen *) display->driverdata;
   283         [uiscreen release];
   284         display->driverdata = NULL;
   285         for (j = 0; j < display->num_display_modes; j++) {
   286             SDL_DisplayMode *mode = &display->display_modes[j];
   287             UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
   288             if (uimode) {
   289                 [uimode release];
   290                 mode->driverdata = NULL;
   291             }
   292         }
   293     }
   294 }
   295 
   296 /* vi: set ts=4 sw=4 expandtab: */